Skip to content

Commit a339051

Browse files
committed
Added all required test vectors
1 parent 8898c22 commit a339051

File tree

2 files changed

+100
-37
lines changed

2 files changed

+100
-37
lines changed

spec/branca_spec.cr

Lines changed: 92 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,156 @@
11
require "./spec_helper"
22

3+
# Branca test vectors
4+
#
5+
# https://github.com/tuupola/branca-spec/blob/master/test_vectors.json
36
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
1013
end
1114
end
1215

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
1458

1559
describe "#decode" do
16-
it "should decode Hello world! with zero timestamp" do
60+
it "decodes hello world with zero timestamp" do
1761
token = "870S4BYxgHw0KnP3W9fgVUHEhT5g86vJ17etaC5Kh5uIraWHCI1psNQGv298ZmjPwoYbjDQ9chy2z"
1862
payload, timestamp = branca.decode(token)
19-
str = String.new(payload)
20-
str.should eq "Hello world!"
63+
payload.hexstring.should eq "48656c6c6f20776f726c6421"
2164
timestamp.should eq 0
2265
end
23-
it "should decode Hello world! with max timestamp" do
66+
it "decodes hello world with max timestamp" do
2467
token = "89i7YCwu5tWAJNHUDdmIqhzOi5hVHOd4afjZcGMcVmM4enl4yeLiDyYv41eMkNmTX6IwYEFErCSqr"
2568
payload, timestamp = branca.decode(token)
26-
str = String.new(payload)
27-
str.should eq "Hello world!"
69+
payload.hexstring.should eq "48656c6c6f20776f726c6421"
2870
timestamp.should eq 4294967295
2971
end
30-
it "should decode Hello world! with nov 27 timestamp" do
72+
it "decodes hello world with November 27 timestamp" do
3173
token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT"
3274
payload, timestamp = branca.decode(token)
33-
str = String.new(payload)
34-
str.should eq "Hello world!"
75+
payload.hexstring.should eq "48656c6c6f20776f726c6421"
3576
timestamp.should eq 123206400
3677
end
37-
it "should decode eight null bytes with zero timestamp" do
78+
it "decodes eight null bytes with zero timestamp" do
3879
token = "1jIBheHbDdkCDFQmtgw4RUZeQoOJgGwTFJSpwOAk3XYpJJr52DEpILLmmwYl4tjdSbbNqcF1"
3980
payload, timestamp = branca.decode(token)
40-
hex = payload.hexstring
41-
hex.should eq "0000000000000000"
81+
payload.hexstring.should eq "0000000000000000"
4282
timestamp.should eq 0
4383
end
44-
it "should decode eight null bytes with max timestamp" do
84+
it "decodes eight null bytes with max timestamp" do
4585
token = "1jrx6DUu5q06oxykef2e2ZMyTcDRTQot9ZnwgifUtzAphGtjsxfbxXNhQyBEOGtpbkBgvIQx"
4686
payload, timestamp = branca.decode(token)
47-
hex = payload.hexstring
48-
hex.should eq "0000000000000000"
87+
payload.hexstring.should eq "0000000000000000"
4988
timestamp.should eq 4294967295
5089
end
51-
it "should decode eight null bytes with nov 27 timestamp" do
90+
it "decodes eight null bytes with November 27th timestamp" do
5291
token = "1jJDJOEjuwVb9Csz1Ypw1KBWSkr0YDpeBeJN6NzJWx1VgPLmcBhu2SbkpQ9JjZ3nfUf7Aytp"
5392
payload, timestamp = branca.decode(token)
54-
hex = payload.hexstring
55-
hex.should eq "0000000000000000"
93+
payload.hexstring.should eq "0000000000000000"
5694
timestamp.should eq 123206400
5795
end
58-
it "should decode empty payload with zero timestamp" do
96+
it "decodes empty payload" do
5997
token = "4sfD0vPFhIif8cy4nB3BQkHeJqkOkDvinI4zIhMjYX4YXZU5WIq9ycCVjGzB5"
6098
payload, timestamp = branca.decode(token)
61-
str = String.new(payload)
62-
str.should eq ""
99+
payload.hexstring.should eq ""
63100
timestamp.should eq 0
64101
end
65-
it "should decode non utf-8 chars with nov 27 timestamp" do
102+
it "decodes non-UTF8 payload" do
66103
token = "K9u6d0zjXp8RXNUGDyXAsB9AtPo60CD3xxQ2ulL8aQoTzXbvockRff0y1eXoHm"
67104
payload, timestamp = branca.decode(token)
68-
hex = payload.hexstring
69-
hex.should eq "80"
105+
payload.hexstring.should eq "80"
70106
timestamp.should eq 123206400
71107
end
72-
it "should throw with wrong token version" do
108+
it "throws with wrong version 0xBB" do
73109
token = "89mvl3RkwXjpEj5WMxK7GUDEHEeeeZtwjMIOogTthvr44qBfYtQSIZH5MHOTC0GzoutDIeoPVZk3w"
74110
expect_raises(Exception) do
75111
branca.decode(token)
76112
end
77113
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
79127
token = "875GH233SUysT7fQ711EWd9BXpwOjB72ng3ZLnjWFrmOqVy49Bv93b78JU5331LbcY0EEzhLfpmSx"
80128
expect_raises(Exception) do
81129
branca.decode(token)
82130
end
83131
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
85139
token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5Qw6Jpo96myliI3hHD7VbKZBYh"
86140
expect_raises(Exception) do
87141
branca.decode(token)
88142
end
89143
end
90-
it "should throw with modified poly1305 tag" do
144+
it "throws with modified last byte of the Poly1305 tag" do
91145
token = "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trk0"
92146
expect_raises(Exception) do
93147
branca.decode(token)
94148
end
95149
end
96-
it "should throw with wrong secret key" do
150+
it "throws with wrong key" do
97151
token = "870S4BYxgHw0KnP3W9fgVUHEhT5g86vJ17etaC5Kh5uIraWHCI1psNQGv298ZmjPwoYbjDQ9chy2z"
98-
br = Branca.new "wrongsecretkeyyoushouldnotcommit".to_slice
152+
key = "77726f6e677365637265746b6579796f7573686f756c646e6f74636f6d6d6974".hexbytes
153+
br = Branca.new key
99154
expect_raises(Exception) do
100155
br.decode(token)
101156
end

spec/spec_helper.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
require "spec"
22
require "../src/branca"
3+
4+
# Branca Test Object
5+
#
6+
# This sets a user defined nonce for testing.
7+
# Do not do this in production.
8+
struct Branca
9+
@nonce = "beefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef".hexbytes
10+
end

0 commit comments

Comments
 (0)