File: rand.t

package info (click to toggle)
libcrypt-urandom-perl 0.55-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212 kB
  • sloc: perl: 559; makefile: 3
file content (47 lines) | stat: -rw-r--r-- 1,443 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
use Test::More tests => 11;
use Crypt::URandom();

foreach my $correct (qw(500000 500 50)) {
	my $actual = length Crypt::URandom::urandom($correct);
        ok($actual == $correct, "Crypt::URandom::urandom($correct) returned $actual bytes");
	$actual = length Crypt::URandom::urandom_ub($correct);
        ok($actual == $correct, "Crypt::URandom::urandom_ub($correct) returned $actual bytes");
}
SKIP: {
	eval { require Encode; };
	if ($@) {
		skip("Encode module cannot be loaded", 1);
	} else {
		my $returns_binary_data = 1;
		if (Encode::is_utf8(Crypt::URandom::urandom(2))) {
			$returns_binary_data = 0;
		}
		ok($returns_binary_data, 'Crypt::Urandom::urandom returns binary data');
		$returns_binary_data = 1;
		if (Encode::is_utf8(Crypt::URandom::urandom_ub(2))) {
			$returns_binary_data = 0;
		}
		ok($returns_binary_data, 'Crypt::Urandom::urandom_ub returns binary data');
	}
}
my $exception_thrown = 1;
eval {
	Crypt::URandom::urandom();
	$exception_thrown = 0;
};
chomp $@;
ok($exception_thrown, "Correctly throws exception with no parameter:$@");
$exception_thrown = 1;
eval {
	Crypt::URandom::urandom("sdfadsf");
	$exception_thrown = 0;
};
chomp $@;
ok($exception_thrown, "Correctly throws exception with non integer parameter:$@");
$exception_thrown = 1;
eval {
	Crypt::URandom::getrandom(-1);
	$exception_thrown = 0;
};
chomp $@;
ok($exception_thrown, "getrandom correctly throws exception with less than zero parameter:$@");