File: dev_random.t

package info (click to toggle)
libcrypt-random-source-perl 0.14-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 360 kB
  • sloc: perl: 556; sh: 6; makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,929 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;

use Test::More 0.88;

BEGIN {
    plan skip_all => "no /dev/random and /dev/urandom" unless -e "/dev/random" and -e "/dev/urandom";
}

use Errno;
use ok 'Crypt::Random::Source::Strong::devrandom';
use ok 'Crypt::Random::Source::Weak::devurandom';

{
    ok( Crypt::Random::Source::Strong::devrandom->available, "/dev/random is available" );

    my $p = Crypt::Random::Source::Strong::devrandom->new;

    isa_ok( $p, "Crypt::Random::Source::Strong" );
    isa_ok( $p, "Crypt::Random::Source::Base::RandomDevice" );
    isa_ok( $p, "Crypt::Random::Source::Base::Handle" );
    isa_ok( $p, "Crypt::Random::Source::Strong::devrandom" );

    $p->blocking(0);

    ok( $p->is_strong, "it's a strong source" );

    can_ok( $p, "get" );

    if ( length( my $buf = $p->get(100) ) ) { # blocking
        cmp_ok( length($buf), '<=', 100, "got up to 100 bytes" );

        # this test should fail around every few universes or so ;-)
        cmp_ok( $buf, 'ne', $p->get(length($buf)), "random data differs" );
    } else {
        ok( $!{EWOULDBLOCK} || $!{EAGAIN}, "would have blocked" )
            or diag "errno is $! (" . ($! + 0) . ')';
    }

    can_ok($p, "seed");
}

{
    ok( Crypt::Random::Source::Weak::devurandom->available, "/dev/random is available" );

    my $p = Crypt::Random::Source::Weak::devurandom->new;

    isa_ok( $p, "Crypt::Random::Source::Weak" );
    isa_ok( $p, "Crypt::Random::Source::Base::RandomDevice" );
    isa_ok( $p, "Crypt::Random::Source::Base::Handle" );
    isa_ok( $p, "Crypt::Random::Source::Weak::devurandom" );

    ok( !$p->is_strong, "it's a weak source" );

    can_ok( $p, "get" );

    my $buf = $p->get(100);

    is( length($buf), 100, "got 100 bytes" );

    # this test should fail around every few universes or so ;-)
    cmp_ok( $buf, 'ne', $p->get(length($buf)), "random data differs" );

    can_ok($p, "seed");
}

done_testing;
# ex: set sw=4 et: