base64 tests normalization
[Net-Amazon-S3-Policy.git] / t / 01.test.t
bloba2b9a704ce06e4d0c079c2b528092f1a1397b17e
1 # vim: filetype=perl :
2 use strict;
3 use warnings;
5 use Test::More tests => 21; # last test to print
7 my $module;
9 BEGIN {
10    $module = 'Net::Amazon::S3::Policy';
11    use_ok($module);
15    my $expiration = '1220292602';
16    my $policy = $module->new({expiration => $expiration});
18    ok($policy, 'new with expiration time');
19    isa_ok($policy, $module);
20    is($policy->expiration(), '2008-09-01T18:10:02.000Z',
21       'expiration time');
23    $policy->add($_)
24      for (
25       q( prova eq ciao ),
26       q( provaxxx starts-with ciao ),
27       q( 0 <= something <= 123312391 ),
28       q( anything * ),
29       q( anything2 starts_with ),
30       q( anything2 starts_with blah ),
31      );
33    my $conditions = $policy->conditions();
34    ok($conditions, 'conditions exists');
35    is(scalar(@$conditions), 6, 'all conditions added');
36    is_deeply(
37       $conditions,
38       [
39          [qw( eq $prova ciao )],
40          [qw( starts-with $provaxxx ciao )],
41          [qw( something 0 123312391 )],
42          [qw( starts-with $anything ),  ''],
43          [qw( starts-with $anything2 ), ''],
44          [qw( starts-with $anything2 blah )],
45       ],
46       'conditions match'
47    );
49    my $expected_json =
50 '{"conditions":[["eq","$prova","ciao"],["starts-with","$provaxxx","ciao"],["something","0","123312391"],["starts-with","$anything",""],["starts-with","$anything2",""],["starts-with","$anything2","blah"]],"expiration":"2008-09-01T18:10:02.000Z"}';
51    is($policy->stringify({canonical => 1}), $expected_json, 'JSON generation');
55    my $json =
56 '{"conditions":[["eq","$prova","ciao"],["starts-with","$provaxxx","ciao"],["something","0","123312391"],["starts-with","$anything",""],["starts-with","$anything2",""],["starts-with","$anything2","blah"],{"what":"this"}],"expiration":"2008-09-01T18:10:02.000Z"}';
58    my $policy = Net::Amazon::S3::Policy->new(json => $json);
59    ok($policy, 'new with expiration time');
60    isa_ok($policy, $module);
61    is($policy->expiration(), '2008-09-01T18:10:02.000Z',
62       'expiration time');
64    my $conditions = $policy->conditions();
65    ok($conditions, 'conditions exists');
66    is(scalar(@$conditions), 7, 'all conditions present');
67    is_deeply(
68       $conditions,
69       [
70          [qw( eq $prova ciao )],
71          [qw( starts-with $provaxxx ciao )],
72          [qw( something 0 123312391 )],
73          [qw( starts-with $anything ),  ''],
74          [qw( starts-with $anything2 ), ''],
75          [qw( starts-with $anything2 blah )],
76          [qw( eq $what this )],
77       ],
78       'conditions match',
79    );
83    my $json =
84 '{"conditions":[["eq","$prova","ciao"],["starts-with","$provaxxx","ciaoBLACKHEART"],["something","0","123312391"],["starts-with","$anything",""],["starts-with","$anything2",""],["starts-with","$anything2","blah"],{"what":"this"}],"expiration":"2008-09-01T18:10:02.000Z"}';
85    $json =~ s/BLACKHEART/\x{2665}/mxs;
87    my $policy = Net::Amazon::S3::Policy->new(json => $json);
88    ok($policy, 'new with expiration time');
89    isa_ok($policy, $module);
90    is($policy->expiration(), '2008-09-01T18:10:02.000Z',
91       'expiration time');
93    my $conditions = $policy->conditions();
94    ok($conditions, 'conditions exists');
95    is(scalar(@$conditions), 7, 'all conditions present');
96    is_deeply(
97       $conditions,
98       [
99          [qw( eq $prova ciao )],
100          [qw( starts-with $provaxxx ), "ciao\x{2665}"],
101          [qw( something 0 123312391 )],
102          [qw( starts-with $anything ),  ''],
103          [qw( starts-with $anything2 ), ''],
104          [qw( starts-with $anything2 blah )],
105          [qw( eq $what this )],
106       ],
107       'conditions match',
108    );
110    my $base64 = 'undefined';
111    eval { $base64 = $policy->base64({canonical => 1}); };
112    my $expected_base64 = 'eyJleHBpcmF0aW9uIjoiMjAwOC0wOS0wMVQxODoxMDowMi4wMDBaIiwiY29uZGl0aW9ucyI6W1siZXEiLCIkcHJvdmEiLCJjaWFvIl0sWyJzdGFydHMtd2l0aCIsIiRwcm92YXh4eCIsImNpYW/imaUiXSxbInNvbWV0aGluZyIsIjAiLCIxMjMzMTIzOTEiXSxbInN0YXJ0cy13aXRoIiwiJGFueXRoaW5nIiwiIl0sWyJzdGFydHMtd2l0aCIsIiRhbnl0aGluZzIiLCIiXSxbInN0YXJ0cy13aXRoIiwiJGFueXRoaW5nMiIsImJsYWgiXSxbImVxIiwiJHdoYXQiLCJ0aGlzIl1dfQ==';
113    is($base64, $expected_base64, 'base64 encoding');