]> Dogcows Code - chaz/p5-File-KDBX/blob - t/yubikey.t
add initial WIP
[chaz/p5-File-KDBX] / t / yubikey.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use lib 't/lib';
7 use TestCommon;
8
9 use Test::More;
10
11 BEGIN { use_ok 'File::KDBX::Key::YubiKey' }
12
13 local $ENV{YKCHALRESP} = testfile(qw{bin ykchalresp});
14 local $ENV{YKINFO} = testfile(qw{bin ykinfo});
15
16 {
17 my ($pre, $post);
18 my $key = File::KDBX::Key::YubiKey->new(
19 pre_challenge => sub { ++$pre },
20 post_challenge => sub { ++$post },
21 );
22 my $resp;
23 is exception { $resp = $key->challenge('foo') }, undef,
24 'Do not throw during non-blocking response';
25 is $resp, "\xf0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 'Get a non-blocking challenge response';
26 is length($resp), 20, 'Response is the proper length';
27 is $pre, 1, 'The pre-challenge callback is called';
28 is $post, 1, 'The post-challenge callback is called';
29 }
30
31 {
32 my $key = File::KDBX::Key::YubiKey->new;
33 local $ENV{YKCHALRESP_MOCK} = 'error';
34 like exception { $key->challenge('foo') }, qr/Yubikey core error:/i,
35 'Throw if challenge-response program errored out';
36 }
37
38 {
39 my $key = File::KDBX::Key::YubiKey->new;
40 local $ENV{YKCHALRESP_MOCK} = 'usberror';
41 like exception { $key->challenge('foo') }, qr/USB error:/i,
42 'Throw if challenge-response program had a USB error';
43 }
44
45 {
46 my $key = File::KDBX::Key::YubiKey->new(timeout => 0, device => 3, slot => 2);
47 local $ENV{YKCHALRESP_MOCK} = 'block';
48
49 like exception { $key->challenge('foo') }, qr/operation would block/i,
50 'Throw if challenge would block but we do not want to wait';
51
52 $key->timeout(1);
53 like exception { $key->challenge('foo') }, qr/timed out/i,
54 'Timed out while waiting for response';
55
56 $key->timeout(-1);
57 my $resp;
58 is exception { $resp = $key->challenge('foo') }, undef,
59 'Do not throw during blocking response';
60 is $resp, "\xf0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 'Get a blocking challenge response';
61 }
62
63 {
64 my $key = File::KDBX::Key::YubiKey->new(device => 0, slot => 1);
65 is $key->name, 'YubiKey NEO FIDO v2.0.0 [123] (slot #1)',
66 'Get name for a new, unscanned key';
67 is $key->serial, 123, 'We have the serial number of the new key';
68 }
69
70 {
71 my ($key, @other) = File::KDBX::Key::YubiKey->scan;
72 is $key->name, 'YubiKey 4/5 OTP v3.0.1 [456] (slot #2)',
73 'Find expected YubiKey';
74 is $key->serial, 456, 'We have the serial number of the scanned key';
75 is scalar @other, 0, 'Do not find any other YubiKeys';
76 }
77
78 {
79 local $ENV{YKCHALRESP} = testfile(qw{bin nonexistent});
80 my $key = File::KDBX::Key::YubiKey->new;
81 like exception { $key->challenge('foo') }, qr/failed to run|failed to receive challenge response/i,
82 'Throw if the program failed to run';
83 }
84
85 done_testing;
This page took 0.039687 seconds and 4 git commands to generate.