-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoding_test.go
189 lines (172 loc) · 4.62 KB
/
coding_test.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2022 Teal.Finance/incorruptible contributors
// This file is part of Teal.Finance/incorruptible
// a tiny+secured cookie token licensed under the MIT License.
// SPDX-License-Identifier: MIT
package incorruptible_test
import (
"net"
"reflect"
"testing"
"github.com/teal-finance/incorruptible"
)
func TestUnmarshal(t *testing.T) {
t.Parallel()
for _, c := range codingDataCases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
c.tv.ShortenIP4Length()
b, err := incorruptible.Marshal(c.tv, c.magic)
if (err == nil) == c.wantErr {
t.Errorf("Marshal() error = %v, wantErr %v", err, c.wantErr)
return
}
t.Log("len(b)", len(b))
n := len(b)
if n == 0 {
return
}
if n > 70 {
n = 70 // print max the first 70 bytes
}
t.Logf("b[:%d] %v", n, b[:n])
magic := incorruptible.MagicCode(b)
if magic != c.magic {
t.Errorf("MagicCode() got = %x, want = %x", magic, c.magic)
return
}
if incorruptible.EnablePadding && ((len(b) % 4) != 0) {
t.Errorf("len(b) %d must be 32-bit aligned but gap =%d", len(b), len(b)%4)
return
}
got, err := incorruptible.Unmarshal(b)
if err != nil {
t.Errorf("Unmarshal() error = %v", err)
return
}
min := c.tv.Expires - incorruptible.PrecisionInSeconds
max := c.tv.Expires + incorruptible.PrecisionInSeconds
validExpiry := (min <= got.Expires) && (got.Expires <= max)
if !validExpiry {
t.Errorf("Expiry too different got=%v original=%v want in [%d %d]",
got.Expires, c.tv.Expires, min, max)
}
if (len(got.IP) > 0 || len(c.tv.IP) > 0) &&
!reflect.DeepEqual(got.IP, c.tv.IP) {
t.Errorf("Mismatch IP got %v, want %v", got.IP, c.tv.IP)
}
if (len(got.Values) > 0 || len(c.tv.Values) > 0) &&
!reflect.DeepEqual(got.Values, c.tv.Values) {
t.Errorf("Mismatch Values got %v, want %v", got.Values, c.tv.Values)
}
})
}
}
var codingDataCases = []struct {
name string
magic uint8
wantErr bool
tv incorruptible.TValues
}{
{
"noIP", 109, false, incorruptible.TValues{
Expires: expiry,
IP: nil,
Values: nil,
},
},
{
"noIPnoExpiry", 109, false, incorruptible.TValues{
Expires: 0,
IP: nil,
Values: nil,
},
},
{
"noExpiry", 109, false, incorruptible.TValues{
Expires: 0,
IP: net.IPv4(0, 0, 0, 0),
Values: nil,
},
},
{
"noneIPv4", 0x51, false,
incorruptible.TValues{
Expires: expiry,
IP: net.IPv4(11, 22, 33, 44),
Values: [][]byte{},
},
},
{
"noneIPv6", 0x51, false,
incorruptible.TValues{
Expires: expiry,
IP: net.IP{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
Values: [][]byte{},
},
},
{
"1emptyIPv6", 0x51, false,
incorruptible.TValues{
Expires: expiry,
IP: net.IP{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
Values: [][]byte{[]byte("")},
},
},
{
"4emptyIPv6", 0x51, false,
incorruptible.TValues{
Expires: expiry,
IP: net.IP{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
Values: [][]byte{[]byte(""), []byte(""), []byte(""), []byte("")},
},
},
{
"1smallIPv6", 0x51, false,
incorruptible.TValues{
Expires: expiry,
IP: net.IP{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
Values: [][]byte{[]byte("1")},
},
},
{
"1valIPv6", 0x51, false,
incorruptible.TValues{
Expires: expiry,
IP: net.IP{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
Values: [][]byte{[]byte("123456789-B-123456789-C-123456789-D-123456789-E-123456789")},
},
},
{
"1moreIPv6", 0x51, false,
incorruptible.TValues{
Expires: expiry,
IP: net.IP{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
Values: [][]byte{[]byte("123456789-B-123456789-C-123456789-D-123456789-E-123456789-")},
},
},
{
"Compress 10valIPv6", 0x51, false,
incorruptible.TValues{
Expires: expiry,
IP: net.IP{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
Values: [][]byte{
[]byte("123456789-B-123456789-C-123456789-D-123456789-E-123456789"),
[]byte("123456789-F-123456789-C-123456789-D-123456789-E-123456789"),
[]byte("123456789-G-123456789-C-123456789-D-123456789-E-123456789"),
[]byte("123456789-H-123456789-C-123456789-D-123456789-E-123456789"),
[]byte("123456789-I-123456789-C-123456789-D-123456789-E-123456789"),
[]byte("123456789-J-123456789-C-123456789-D-123456789-E-123456789"),
[]byte("123456789-K-123456789-C-123456789-D-123456789-E-123456789"),
},
},
},
{
"too much values", 0x51, true,
incorruptible.TValues{
Expires: expiry,
IP: net.IP{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
Values: values,
},
},
}