|
1 | 1 | require "./spec_helper" |
2 | 2 |
|
| 3 | +# Branca test vectors |
| 4 | +# |
| 5 | +# https://github.com/tuupola/branca-spec/blob/master/test_vectors.json |
3 | 6 | describe Branca do |
4 | | - # https://github.com/tuupola/branca-spec/blob/master/test_vectors.json |
5 | | - # test vectors taken from |
6 | | - # https://github.com/tuupola/branca-php/blob/2.x/tests/BrancaTest.php |
7 | | - it "should throw on invalid key" do |
8 | | - expect_raises(Exception, "secret key has to be 32 bytes strong") do |
9 | | - Branca.new "notsosecretkey".to_slice |
| 7 | + branca = Branca.new "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974".hexbytes |
| 8 | + |
| 9 | + it "throws with invalid key" do |
| 10 | + key = "746f6f73686f72746b6579".hexbytes |
| 11 | + expect_raises(Exception) do |
| 12 | + Branca.new key |
10 | 13 | end |
11 | 14 | end |
12 | 15 |
|
13 | | - branca = Branca.new "supersecretkeyyoushouldnotcommit".to_slice |
| 16 | + describe "#encode" do |
| 17 | + it "encodes hello world with zero timestamp" do |
| 18 | + payload = "48656c6c6f20776f726c6421".hexbytes |
| 19 | + token = branca.encode(payload, 0) |
| 20 | + token.should eq "870S4BYxgHw0KnP3W9fgVUHEhT5g86vJ17etaC5Kh5uIraWHCI1psNQGv298ZmjPwoYbjDQ9chy2z" |
| 21 | + end |
| 22 | + it "encodes hello world with max timestamp" do |
| 23 | + payload = "48656c6c6f20776f726c6421".hexbytes |
| 24 | + token = branca.encode(payload, 4294967295) |
| 25 | + token.should eq "89i7YCwu5tWAJNHUDdmIqhzOi5hVHOd4afjZcGMcVmM4enl4yeLiDyYv41eMkNmTX6IwYEFErCSqr" |
| 26 | + end |
| 27 | + it "encodes hello world with November 27 timestamp" do |
| 28 | + payload = "48656c6c6f20776f726c6421".hexbytes |
| 29 | + token = branca.encode(payload, 123206400) |
| 30 | + token.should eq "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT" |
| 31 | + end |
| 32 | + it "encodes eight null bytes with zero timestamp" do |
| 33 | + payload = "0000000000000000".hexbytes |
| 34 | + token = branca.encode(payload, 0) |
| 35 | + token.should eq "1jIBheHbDdkCDFQmtgw4RUZeQoOJgGwTFJSpwOAk3XYpJJr52DEpILLmmwYl4tjdSbbNqcF1" |
| 36 | + end |
| 37 | + it "encodes eight null bytes with max timestamp" do |
| 38 | + payload = "0000000000000000".hexbytes |
| 39 | + token = branca.encode(payload, 4294967295) |
| 40 | + token.should eq "1jrx6DUu5q06oxykef2e2ZMyTcDRTQot9ZnwgifUtzAphGtjsxfbxXNhQyBEOGtpbkBgvIQx" |
| 41 | + end |
| 42 | + it "encodes eight null bytes with November 27th timestamp" do |
| 43 | + payload = "0000000000000000".hexbytes |
| 44 | + token = branca.encode(payload, 123206400) |
| 45 | + token.should eq "1jJDJOEjuwVb9Csz1Ypw1KBWSkr0YDpeBeJN6NzJWx1VgPLmcBhu2SbkpQ9JjZ3nfUf7Aytp" |
| 46 | + end |
| 47 | + it "encodes empty payload" do |
| 48 | + payload = "".hexbytes |
| 49 | + token = branca.encode(payload, 0) |
| 50 | + token.should eq "4sfD0vPFhIif8cy4nB3BQkHeJqkOkDvinI4zIhMjYX4YXZU5WIq9ycCVjGzB5" |
| 51 | + end |
| 52 | + it "encodes non-UTF8 payload" do |
| 53 | + payload = "80".hexbytes |
| 54 | + token = branca.encode(payload, 123206400) |
| 55 | + token.should eq "K9u6d0zjXp8RXNUGDyXAsB9AtPo60CD3xxQ2ulL8aQoTzXbvockRff0y1eXoHm" |
| 56 | + end |
| 57 | + end |
14 | 58 |
|
15 | 59 | describe "#decode" do |
16 | | - it "should decode Hello world! with zero timestamp" do |
| 60 | + it "decodes hello world with zero timestamp" do |
17 | 61 | token = "870S4BYxgHw0KnP3W9fgVUHEhT5g86vJ17etaC5Kh5uIraWHCI1psNQGv298ZmjPwoYbjDQ9chy2z" |
18 | 62 | payload, timestamp = branca.decode(token) |
19 | | - str = String.new(payload) |
20 | | - str.should eq "Hello world!" |
| 63 | + payload.hexstring.should eq "48656c6c6f20776f726c6421" |
21 | 64 | timestamp.should eq 0 |
22 | 65 | end |
23 | | - it "should decode Hello world! with max timestamp" do |
| 66 | + it "decodes hello world with max timestamp" do |
24 | 67 | token = "89i7YCwu5tWAJNHUDdmIqhzOi5hVHOd4afjZcGMcVmM4enl4yeLiDyYv41eMkNmTX6IwYEFErCSqr" |
25 | 68 | payload, timestamp = branca.decode(token) |
26 | | - str = String.new(payload) |
27 | | - str.should eq "Hello world!" |
| 69 | + payload.hexstring.should eq "48656c6c6f20776f726c6421" |
28 | 70 | timestamp.should eq 4294967295 |
29 | 71 | end |
30 | | - it "should decode Hello world! with nov 27 timestamp" do |
| 72 | + it "decodes hello world with November 27 timestamp" do |
31 | 73 | token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT" |
32 | 74 | payload, timestamp = branca.decode(token) |
33 | | - str = String.new(payload) |
34 | | - str.should eq "Hello world!" |
| 75 | + payload.hexstring.should eq "48656c6c6f20776f726c6421" |
35 | 76 | timestamp.should eq 123206400 |
36 | 77 | end |
37 | | - it "should decode eight null bytes with zero timestamp" do |
| 78 | + it "decodes eight null bytes with zero timestamp" do |
38 | 79 | token = "1jIBheHbDdkCDFQmtgw4RUZeQoOJgGwTFJSpwOAk3XYpJJr52DEpILLmmwYl4tjdSbbNqcF1" |
39 | 80 | payload, timestamp = branca.decode(token) |
40 | | - hex = payload.hexstring |
41 | | - hex.should eq "0000000000000000" |
| 81 | + payload.hexstring.should eq "0000000000000000" |
42 | 82 | timestamp.should eq 0 |
43 | 83 | end |
44 | | - it "should decode eight null bytes with max timestamp" do |
| 84 | + it "decodes eight null bytes with max timestamp" do |
45 | 85 | token = "1jrx6DUu5q06oxykef2e2ZMyTcDRTQot9ZnwgifUtzAphGtjsxfbxXNhQyBEOGtpbkBgvIQx" |
46 | 86 | payload, timestamp = branca.decode(token) |
47 | | - hex = payload.hexstring |
48 | | - hex.should eq "0000000000000000" |
| 87 | + payload.hexstring.should eq "0000000000000000" |
49 | 88 | timestamp.should eq 4294967295 |
50 | 89 | end |
51 | | - it "should decode eight null bytes with nov 27 timestamp" do |
| 90 | + it "decodes eight null bytes with November 27th timestamp" do |
52 | 91 | token = "1jJDJOEjuwVb9Csz1Ypw1KBWSkr0YDpeBeJN6NzJWx1VgPLmcBhu2SbkpQ9JjZ3nfUf7Aytp" |
53 | 92 | payload, timestamp = branca.decode(token) |
54 | | - hex = payload.hexstring |
55 | | - hex.should eq "0000000000000000" |
| 93 | + payload.hexstring.should eq "0000000000000000" |
56 | 94 | timestamp.should eq 123206400 |
57 | 95 | end |
58 | | - it "should decode empty payload with zero timestamp" do |
| 96 | + it "decodes empty payload" do |
59 | 97 | token = "4sfD0vPFhIif8cy4nB3BQkHeJqkOkDvinI4zIhMjYX4YXZU5WIq9ycCVjGzB5" |
60 | 98 | payload, timestamp = branca.decode(token) |
61 | | - str = String.new(payload) |
62 | | - str.should eq "" |
| 99 | + payload.hexstring.should eq "" |
63 | 100 | timestamp.should eq 0 |
64 | 101 | end |
65 | | - it "should decode non utf-8 chars with nov 27 timestamp" do |
| 102 | + it "decodes non-UTF8 payload" do |
66 | 103 | token = "K9u6d0zjXp8RXNUGDyXAsB9AtPo60CD3xxQ2ulL8aQoTzXbvockRff0y1eXoHm" |
67 | 104 | payload, timestamp = branca.decode(token) |
68 | | - hex = payload.hexstring |
69 | | - hex.should eq "80" |
| 105 | + payload.hexstring.should eq "80" |
70 | 106 | timestamp.should eq 123206400 |
71 | 107 | end |
72 | | - it "should throw with wrong token version" do |
| 108 | + it "throws with wrong version 0xBB" do |
73 | 109 | token = "89mvl3RkwXjpEj5WMxK7GUDEHEeeeZtwjMIOogTthvr44qBfYtQSIZH5MHOTC0GzoutDIeoPVZk3w" |
74 | 110 | expect_raises(Exception) do |
75 | 111 | branca.decode(token) |
76 | 112 | end |
77 | 113 | end |
78 | | - it "should throw with modified token nonce" do |
| 114 | + it "throws with invalid base62 characters" do |
| 115 | + token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT_" |
| 116 | + expect_raises(Exception) do |
| 117 | + branca.decode(token) |
| 118 | + end |
| 119 | + end |
| 120 | + it "throws with modified version" do |
| 121 | + token = "89mvl3S0BE0UCMIY94xxIux4eg1w5oXrhvCEXrDAjusSbO0Yk7AU6FjjTnbTWTqogLfNPJLzecHVb" |
| 122 | + expect_raises(Exception) do |
| 123 | + branca.decode(token) |
| 124 | + end |
| 125 | + end |
| 126 | + it "throws with modified first byte of the nonce" do |
79 | 127 | token = "875GH233SUysT7fQ711EWd9BXpwOjB72ng3ZLnjWFrmOqVy49Bv93b78JU5331LbcY0EEzhLfpmSx" |
80 | 128 | expect_raises(Exception) do |
81 | 129 | branca.decode(token) |
82 | 130 | end |
83 | 131 | end |
84 | | - it "should throw with modified chiphertext" do |
| 132 | + it "throws with modified timestamp" do |
| 133 | + token = "870g1RCk4lW1YInhaU3TP8u2hGtfol16ettLcTOSoA0JIpjCaQRW7tQeP6dQmTvFIB2s6wL5deMXr" |
| 134 | + expect_raises(Exception) do |
| 135 | + branca.decode(token) |
| 136 | + end |
| 137 | + end |
| 138 | + it "throws with modified last byte of the ciphertext" do |
85 | 139 | token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5Qw6Jpo96myliI3hHD7VbKZBYh" |
86 | 140 | expect_raises(Exception) do |
87 | 141 | branca.decode(token) |
88 | 142 | end |
89 | 143 | end |
90 | | - it "should throw with modified poly1305 tag" do |
| 144 | + it "throws with modified last byte of the Poly1305 tag" do |
91 | 145 | token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trk0" |
92 | 146 | expect_raises(Exception) do |
93 | 147 | branca.decode(token) |
94 | 148 | end |
95 | 149 | end |
96 | | - it "should throw with wrong secret key" do |
| 150 | + it "throws with wrong key" do |
97 | 151 | token = "870S4BYxgHw0KnP3W9fgVUHEhT5g86vJ17etaC5Kh5uIraWHCI1psNQGv298ZmjPwoYbjDQ9chy2z" |
98 | | - br = Branca.new "wrongsecretkeyyoushouldnotcommit".to_slice |
| 152 | + key = "77726f6e677365637265746b6579796f7573686f756c646e6f74636f6d6d6974".hexbytes |
| 153 | + br = Branca.new key |
99 | 154 | expect_raises(Exception) do |
100 | 155 | br.decode(token) |
101 | 156 | end |
|
0 commit comments