]>
Dogcows Code - chaz/p5-File-KDBX/blob - t/crypt.t
9 use Crypt
::Misc
0.029 qw(decode_b64 encode_b64);
10 use File
::KDBX
::Constants
qw(CIPHER_UUID_AES256);
14 BEGIN { use_ok
'File::KDBX::Cipher' }
15 BEGIN { use_ok
'PerlIO::via::File::KDBX::Crypt' }
17 subtest
'Round-trip block stream' => sub {
19 my $block_cipher = File
::KDBX
::Cipher-
>new(uuid
=> CIPHER_UUID_AES256
, key
=> 0x01 x
32, iv
=> 0x01 x
16);
20 test_roundtrip
($block_cipher,
21 'Smell the pretty flowers.',
22 decode_b64
('pB10mV+mhTuh7bKg0KEUl5H1ajFMaP4uPnTZNcDgq6s='),
26 subtest
'Round-trip cipher stream' => sub {
28 my $cipher_stream = File
::KDBX
::Cipher-
>new(stream_id
=> 2, key
=> 0x01 x
16);
29 test_roundtrip
($cipher_stream,
30 'Smell the pretty flowers.',
31 decode_b64
('gNj2Ud9tWtFDy+xDN/U01RxmCoI6MAlTKQ=='),
35 subtest
'Error handling' => sub {
38 my $block_cipher = File
::KDBX
::Cipher-
>new(uuid
=> CIPHER_UUID_AES256
, key
=> 0x01 x
32, iv
=> 0x01 x
16);
39 pipe(my $read, my $write) or die "pipe failed: $!";
40 PerlIO
::via
::File
::KDBX
::Crypt-
>push($read, $block_cipher);
42 print $write 'blah blah blah!!';
43 close($write) or die "close failed: $!";
45 is $read->error, 0, 'Read handle starts out fine';
46 my $plaintext = do { local $/; <$read> };
47 is $read->error, 1, 'Read handle can enter and error state';
49 like
$PerlIO::via
::File
::KDBX
::Crypt
::ERROR
, qr/fatal/i,
50 'Error object is available';
58 my $expected_plaintext = shift;
59 my $expected_ciphertext = shift;
61 pipe(my $read, my $write) or die "pipe failed: $!";
62 PerlIO
::via
::File
::KDBX
::Crypt-
>push($write, $cipher);
64 print $write $expected_plaintext;
65 binmode($write, ':pop'); # finish stream
66 close($write) or die "close failed: $!";
68 my $ciphertext = do { local $/; <$read> };
70 is $ciphertext, $expected_ciphertext, 'Encrypted a string'
71 or diag encode_b64
($ciphertext);
73 my $ciphertext2 = $cipher->encrypt_finish($expected_plaintext);
74 is $ciphertext, $ciphertext2, 'Same result';
76 open(my $fh, '<', \
$ciphertext) or die "open failed: $!\n";
77 PerlIO
::via
::File
::KDBX
::Crypt-
>push($fh, $cipher);
79 my $plaintext = do { local $/; <$fh> };
81 is $plaintext, $expected_plaintext, 'Decrypted a string'
82 or diag encode_b64
($plaintext);
This page took 0.046475 seconds and 5 git commands to generate.