File: 01_error_checks.t

package info (click to toggle)
libconvert-base32-perl 0.06-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 76 kB
  • sloc: perl: 83; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 1,846 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use strict;
use warnings;

use Test::More;
use Test::Exception;

use Convert::Base32 qw( encode_base32 decode_base32 );

my @tests = (
    [ ''       =>  0            ],

    [ a        => 'bad len'     ],

    [ ae       =>  1            ],
    [ ac       => 'bad padding' ],
    [ ab       => 'bad padding' ],

    [ aaa      => 'bad len'     ],

    [ aaaq     =>  2            ],
    [ aaai     => 'bad padding' ],
    [ aaae     => 'bad padding' ],
    [ aaac     => 'bad padding' ],
    [ aaab     => 'bad padding' ],

    [ aaaac    =>  3            ],
    [ aaaab    => 'bad padding' ],

    [ aaaaaa   => 'bad len'     ],

    [ aaaaaai  =>  4            ],
    [ aaaaaae  => 'bad padding' ],
    [ aaaaaac  => 'bad padding' ],
    [ aaaaaab  => 'bad padding' ],
);

plan tests => 2*@tests + 2*512;

for (@tests) {
    my ($e, $dlen) = @$_;
    if ($dlen =~ /^[0-9]+\z/) {
        lives_and { is length(decode_base32($e)), $dlen } "$e (ok)";
    } else {
        dies_ok { decode_base32($e) } "$e ($dlen)";
    }
}

for (@tests) {
    my ($e, $dlen) = @$_;
    $e = "aaaaaaaa$e";
    if ($dlen =~ /^[0-9]+\z/) {
	lives_and { is length(decode_base32($e)), 5+$dlen } "$e (ok)";
    } else {
	dies_ok { decode_base32($e) } "$e ($dlen)";
    }
}


my %syms = map { $_ => 1 } ( 'a'..'z', 'A'..'Z', '2'..'7' );

for my $o (0..511) {
    my $c = chr($o);

    if ( ( $o >= ord('a') && $o <= ord('z') )
    ||   ( $o >= ord('A') && $o <= ord('Z') )
    ||   ( $o >= ord('2') && $o <= ord('7') ) ) {
	lives_ok { decode_base32("aaaaaaa$c") } sprintf('decode U+%04X (ok)', $o);
    } else {
	dies_ok { decode_base32("aaaaaaa$c") } sprintf('decode U+%04X (bad)', $o)
    }

    if ($o < 256) {
	lives_ok { encode_base32($c) } sprintf('encode U+%04X (ok)', $o);
    } else {
	dies_ok { encode_base32($c) } sprintf('encode U+%04X (bad)', $o);
    }
}