File: rand_bits.t

package info (click to toggle)
libdigest-bcrypt-perl 1.212-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 204 kB
  • sloc: perl: 305; makefile: 2
file content (54 lines) | stat: -rwxr-xr-x 1,249 bytes parent folder | download
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
use strict;
use warnings;

use Data::Entropy::Algorithms qw(rand_bits);
use Digest::Bcrypt;
use Test::More;
use Try::Tiny qw(try catch);

my $ctx = try {
    return Digest::Bcrypt->new();
}
catch { return "Couldn't create object: $!"; };
isa_ok($ctx, 'Digest::Bcrypt', 'new: got a proper object');

my $secret = "Super Secret Squirrel";
my $bits   = rand_bits(128);
is(length($bits), 16, 'rand_bits: 16 octets');

subtest "salt tests", sub {
    plan(skip_all => "Couldn't get a Digest::Bcrypt object") unless $ctx;
    my $res;
    my $err;
    $ctx->add($secret);
    try {
        $ctx->cost(5);
        $ctx->salt($bits);
        $res = $ctx->digest;
    }
    catch {
        $err = $_;
    };
    is($err, undef, 'rand_bits salt: no error');
    ok($res, 'rand_bits salt: got a proper digest');
    $ctx->reset;
};

subtest "bad salt tests", sub {
    plan(skip_all => "Couldn't get a Digest::Bcrypt object") unless $ctx;
    my $res;
    my $err;
    $ctx->add($secret);
    try {
        $ctx->cost(5);
        $ctx->salt(rand_bits(256));
        $res = $ctx->digest;
    }
    catch {
        $err = $_;
    };
    like($err, qr/Salt must/, 'bad salt: too many random bits');
    is($res, undef, 'bad salt: no result');
};

done_testing();