aboutsummaryrefslogtreecommitdiff
path: root/t/tests/Net/Hawk/Crypto.t
blob: 7ae6f7e367ed0a786d8f4644e7c7ce408ebc56c7 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!perl 
use strict;
use warnings;
use Test::More;
use Net::Hawk::Crypto;
 
my $c = Net::Hawk::Crypto->new();
 
subtest readme => sub {
    my %credentials = (
        id => 'dh37fgj492je',
        key => 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
        algorithm => 'sha256',
    );
    my %options = (
        credentials => \%credentials,
        timestamp => 1353832234,
        nonce => 'j4h3g2',
        ext => 'some-app-ext-data'
    );
 
    subtest GET => sub {
        my $string = $c->generate_normalized_string(
            header => {
                credentials => \%credentials,
                ts => $options{timestamp},
                nonce => $options{nonce},
                method => 'GET',
                resource => '/resource?a=1&b=2',
                host => 'example.com',
                port => 8000,
                ext => $options{ext},
            }
        );
 
        is(
            $string,
            "hawk.1.header\n1353832234\nj4h3g2\nGET\n/resource?a=1&b=2\nexample.com\n8000\n\nsome-app-ext-data\n",
            'normalized string generated ok',
        );
    };
 
    subtest POST => sub {
        my $payload = 'Thank you for flying Hawk';
        my $content_type = 'text/plain';
 
        my $payload_hash = $c->calculate_payload_hash(
            $payload,
            $credentials{algorithm},
            $content_type,
        );
 
        my $string = $c->generate_normalized_string(
            header => {
                credentials => \%credentials,
                ts => $options{timestamp},
                nonce => $options{nonce},
                method => 'POST',
                resource => '/resource?a=1&b=2',
                host => 'example.com',
                port => 8000,
                hash => $payload_hash,
                ext => $options{ext},
            }
        );
 
        is(
            $string,
            "hawk.1.header\n1353832234\nj4h3g2\nPOST\n/resource?a=1&b=2\nexample.com\n8000\nYi9LfIIFRtBEPt74PVmbTF/xVAwPn7ub15ePICfgnuY=\nsome-app-ext-data\n",
            'normalized string generated ok',
        );
    };
};
 
subtest normalized_string => sub {
    my %args = (
        credentials => {
            key => 'dasdfasdf',
            algorithm => 'sha256',
        },
        ts => 1357747017,
        nonce => 'k3k4j5',
        method => 'GET',
        resource => '/resource/something',
        host => 'example.com',
        port =>8080
    );
    my $string = $c->generate_normalized_string(
        header => \%args,
    );
    is(
        $string,
        "hawk.1.header\n1357747017\nk3k4j5\nGET\n/resource/something\nexample.com\n8080\n\n\n",
        'valid normalized string',
    );
 
    $string = $c->generate_normalized_string(
        header => {
            %args,
            ext => 'this is some app data',
        },
    );
    is(
        $string,
        "hawk.1.header\n1357747017\nk3k4j5\nGET\n/resource/something\nexample.com\n8080\n\nthis is some app data\n",
        'valid normalized string (ext)',
    );
 
    $string = $c->generate_normalized_string(
        header => {
            %args,
            ext => 'this is some app data',
            hash => 'U4MKKSmiVxk37JCCrAVIjV/OhB3y+NdwoCr6RShbVkE=',
        },
    );
    is(
        $string,
        "hawk.1.header\n1357747017\nk3k4j5\nGET\n/resource/something\nexample.com\n8080\nU4MKKSmiVxk37JCCrAVIjV/OhB3y+NdwoCr6RShbVkE=\nthis is some app data\n",
        'valid normalized string (payload + ext)',
    );
};
 
done_testing;