| 1 |
# session_object.t |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test::More; |
| 6 |
use Test::Fatal; |
| 7 |
|
| 8 |
use Dancer2::Core::Session; |
| 9 |
use Dancer2::Session::Simple; |
| 10 |
|
| 11 |
my $ENGINE = Dancer2::Session::Simple->new; |
| 12 |
|
| 13 |
note "Cryptographically-strong session ID generation"; |
| 14 |
|
| 15 |
subtest 'session attributes' => sub { |
| 16 |
my $s1 = $ENGINE->create; |
| 17 |
|
| 18 |
my $id = $s1->id; |
| 19 |
ok defined($id), 'id is defined'; |
| 20 |
is(exception { $s1->id("new_$id") }, undef, 'id can be set'); |
| 21 |
is($s1->id, "new_$id", '... new value found for id'); |
| 22 |
|
| 23 |
my $s2 = $ENGINE->create; |
| 24 |
isnt($s1->id, $s2->id, "IDs are not the same"); |
| 25 |
}; |
| 26 |
|
| 27 |
my $count = 10_000; |
| 28 |
subtest "$count session IDs and no dups" => sub { |
| 29 |
my $seen = {}; |
| 30 |
my $iteration = 0; |
| 31 |
foreach my $i (1 .. $count) { |
| 32 |
my $s1 = $ENGINE->create; |
| 33 |
my $id = $s1->id; |
| 34 |
if (exists $seen->{$id}) { |
| 35 |
last; |
| 36 |
} |
| 37 |
$seen->{$id} = 1; |
| 38 |
$iteration++; |
| 39 |
} |
| 40 |
|
| 41 |
is $iteration, $count, |
| 42 |
"no duplicate ID after $count iterations (done $iteration)"; |
| 43 |
}; |
| 44 |
|
| 45 |
subtest 'validate_id' => sub { |
| 46 |
my $id = $ENGINE->generate_id; |
| 47 |
|
| 48 |
ok $ENGINE->validate_id($id), 'generated ID validates'; |
| 49 |
ok !$ENGINE->validate_id("$id\n"), 'trailing newline rejected'; |
| 50 |
ok !$ENGINE->validate_id("a\nb"), 'embedded newline rejected'; |
| 51 |
ok $ENGINE->validate_id('a' x 4096), 'maximum-length ID validates'; |
| 52 |
ok !$ENGINE->validate_id('x' x 4097), 'overly long ID rejected'; |
| 53 |
ok !$ENGINE->validate_id(undef), 'undef rejected'; |
| 54 |
}; |
| 55 |
|
| 56 |
done_testing; |