File: alphabets.t

package info (click to toggle)
libsession-token-perl 1.503-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: ansic: 166; perl: 90; makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,457 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
use Session::Token;

## The point of this test is to test the various alphabet, length, and
## entropy interfaces are behaving according to the design. It also
## checks that passing args in as a hash-ref works.

use strict;

use Test::More tests => 10;

my $ctx;

## default entropy is 128 bits and default alphabet is length 62
$ctx = Session::Token->new;
is(length($ctx->get), 22);

## can specify length directly
$ctx = Session::Token->new( alphabet => '01', length => 30, );
is(length($ctx->get), 30);
like($ctx->get, qr/^[01]+$/);

$ctx = Session::Token->new( alphabet => '01', length => 100000, );
is(length($ctx->get), 100000);

$ctx = Session::Token->new( alphabet => '01', entropy => 4, );
is(length($ctx->get), 4);

$ctx = Session::Token->new( alphabet => '01', entropy => 8, );
is(length($ctx->get), 8);

$ctx = Session::Token->new( alphabet => '01', entropy => 8.1, );
is(length($ctx->get), 9);

## verify hash-ref interface
$ctx = Session::Token->new({ alphabet => '01', entropy => 8.1, });
is(length($ctx->get), 9);

## alphabet has 1.585 bits of entropy, 10/1.585 = 6.309, round up to 7 chars
$ctx = Session::Token->new( alphabet => '012', entropy => 10, );
is(length($ctx->get), 7);

## alphabet has 4.700 bits of entropy, 256/4.700 = 54.463, round up to 55 chars
##   - also tests passing the alphabet in as an array ref instead of string
$ctx = Session::Token->new( alphabet => ['a' .. 'z'], entropy => 256, );
is(length($ctx->get), 55);