]> Dogcows Code - chaz/p5-File-KDBX/blob - t/yubikey.t
Fix YubiKey unit test portability issues
[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 Config;
10 use File::KDBX::Key::YubiKey;
11 use Test::More;
12
13 @ENV{qw(YKCHALRESP YKCHALRESP_FLAGS)} = ($Config{perlpath}, testfile(qw{bin ykchalresp}));
14 @ENV{qw(YKINFO YKINFO_FLAGS)} = ($Config{perlpath}, 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, 'Do not throw during non-blocking response';
24 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';
25 is length($resp), 20, 'Response is the proper length';
26 is $pre, 1, 'The pre-challenge callback is called';
27 is $post, 1, 'The post-challenge callback is called';
28 }
29
30 {
31 my $key = File::KDBX::Key::YubiKey->new;
32 local $ENV{YKCHALRESP_MOCK} = 'error';
33 like exception { $key->challenge('foo') }, qr/Yubikey core error:/i,
34 'Throw if challenge-response program errored out';
35 }
36
37 {
38 my $key = File::KDBX::Key::YubiKey->new;
39 local $ENV{YKCHALRESP_MOCK} = 'usberror';
40 like exception { $key->challenge('foo') }, qr/USB error:/i,
41 'Throw if challenge-response program had a USB error';
42 }
43
44 {
45 my $key = File::KDBX::Key::YubiKey->new(timeout => 0, device => 3, slot => 2);
46 local $ENV{YKCHALRESP_MOCK} = 'block';
47
48 like exception { $key->challenge('foo') }, qr/operation would block/i,
49 'Throw if challenge would block but we do not want to wait';
50
51 $key->timeout(1);
52 like exception { $key->challenge('foo') }, qr/timed out/i,
53 'Timed out while waiting for response';
54
55 $key->timeout(-1);
56 my $resp;
57 is exception { $resp = $key->challenge('foo') }, undef,
58 'Do not throw during blocking response';
59 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';
60 }
61
62 {
63 my $key = File::KDBX::Key::YubiKey->new(device => 0, slot => 1);
64 is $key->name, 'YubiKey NEO FIDO v2.0.0 [123] (slot #1)',
65 'Get name for a new, unscanned key';
66 is $key->serial, 123, 'We have the serial number of the new key';
67 }
68
69 {
70 my ($key, @other) = File::KDBX::Key::YubiKey->scan;
71 is $key->name, 'YubiKey 4/5 OTP v3.0.1 [456] (slot #2)',
72 'Find expected YubiKey';
73 is $key->serial, 456, 'We have the serial number of the scanned key';
74 is scalar @other, 0, 'Do not find any other YubiKeys';
75 }
76
77 {
78 local $ENV{YKCHALRESP} = testfile(qw{bin nonexistent});
79 local $ENV{YKCHALRESP_FLAGS} = undef;
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.037354 seconds and 4 git commands to generate.