forked from Cyphrme/Coz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormal_test.go
More file actions
269 lines (232 loc) · 6 KB
/
normal_test.go
File metadata and controls
269 lines (232 loc) · 6 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package coze
import (
"fmt"
)
func ExampleCanon() {
b := []byte(`{"z":"z", "a":"a"}`)
can, err := GetCanon(b)
if err != nil {
fmt.Println(err)
}
fmt.Println(can)
// Output: [z a]
}
// ExampleCanonicalHash. See also Example_genCad
func ExampleCanonicalHash() {
canon := []string{"alg", "iat", "msg", "tmb", "typ"}
cad, err := CanonicalHash([]byte(GoldenPay), canon, SHA256)
if err != nil {
fmt.Println(err)
}
fmt.Println(cad.String())
// Without canon
cad, err = CanonicalHash([]byte(GoldenPay), nil, SHA256)
if err != nil {
fmt.Println(err)
}
fmt.Println(cad.String())
// Output:
// aC2YKfNvovfnZOw_RVxSEW6NeaUq41DZXX0oeaOboRg
// LSgWE4vEfyxJZUTFaRaB2JdEclORdZcm4UVH9D8vVto
}
// Example CanonicalHash for all hashing algos.
func ExampleCanonicalHash_permutations() {
canon := []string{"alg", "iat", "msg", "tmb", "typ"}
algs := []string{"SHA-224", "SHA-256", "SHA-384", "SHA-512", "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512", "SHAKE128", "SHAKE256"}
for _, alg := range algs {
cad, err := CanonicalHash([]byte(GoldenPay), canon, ParseHashAlg(alg))
if err != nil {
fmt.Println(err)
}
fmt.Println(cad.String())
}
// Output:
// nrGQqKYvFKeVDFlOMIusP2A2AWn4DX-2XLNfJA
// aC2YKfNvovfnZOw_RVxSEW6NeaUq41DZXX0oeaOboRg
// KC5UHPxzNl567oONOphhWY6cHhuXoSiyOLiNGSmTIcvA8XDtWQf-fr4xNPCLfzCo
// oX2NMgJ_QRW9rf59N5VOSMILg6mzVHld5CqRaOatLCbRWVRh1Y6Rq4tRZGzZNvNKEM0qbYBlWk6y9BcnuRzczA
// GAYOBAxW2x7MvHVYpRLnjX3rUKcuhvDOCVVK3Q
// UfFl2lw4KHc2-0GX-mnqtfpScM1Qf7L_IaTGojR6_Go
// pVo43tSAG8apVs26QLOFG0Cbh3ScrbHd_VGjaFAIQtlCLiXcsgdmsGwOyXoK4zBz
// IA8Xv6tt32B49THtWOzN9AyKtnG5a0v93DSF4IShHsT6S2lWKQl1H2yuyMAYocVKBkMF5dp0miKB58NXROqAMg
// muWDwpDGlR-jwGPOQlj6A6B5FYA_U5nFq2KtwV8B-Uw
// QPfIPjKmLO4qLmiClA6GjYQKBO6MI2wBZUhi9uVTVr0WGP3LgOTQRup6l5Caxz6GtiUnNeQe6JMdVSvhdvLW-Q
}
// ExampleCanonical.
func ExampleCanonical() {
var b []byte
var err error
type ABC struct {
A string `json:"a"`
B string `json:"b,omitempty"`
C string `json:"c"`
}
// []byte (out of order) with nil canon. Missing field "b" should be omitted
// from output.
ca, err := Marshal(map[string]string{"c": "c", "a": "a"})
if err != nil {
fmt.Println(err)
}
b, err = Canonical(ca, nil)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Input: []byte; Canon: nil => %s\n", b)
// []byte (out of order) with struct (in order) canon. Missing field "b"
// should be omitted from output.
ca, err = Marshal(map[string]string{"c": "c", "a": "a"})
if err != nil {
fmt.Println(err)
}
b, err = Canonical(ca, new(ABC))
if err != nil {
fmt.Println(err)
}
fmt.Printf("Input: []byte; Canon: struct => %s\n", b)
// []byte (out of order) with struct (in order) canon.
byteJSON := []byte(`{"c":"c", "a": "a"}`)
b, err = Canonical(byteJSON, nil)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Input: []byte; Canon: nil => %s\n", b)
// Output:
// Input: []byte; Canon: nil => {"a":"a","c":"c"}
// Input: []byte; Canon: struct => {"a":"a","c":"c"}
// Input: []byte; Canon: nil => {"c":"c","a":"a"}
}
func ExampleIsNormal() {
b := []byte(`{"z":"z","a":"a"}`)
by := []byte(`{"z":"z","y":"y","a":"a"}`)
////////////////////
// Canon
////////////////////
fmt.Println("Canon")
// In order, pass
canon := []string{"z", "a"}
// Canon in order, pass
v := IsNormal(b, Canon(canon), nil)
fmt.Println(v)
// Canon Out of order, fail
canon = []string{"a", "z"}
v = IsNormal(b, Canon(canon), nil)
fmt.Println(v)
////////////////////
// Only
////////////////////
fmt.Println("\nOnly")
// Only with extra field, fail
only := []string{"a", "y", "z"}
v = IsNormal(b, Only(only), nil)
fmt.Println(v)
// Only Out of order, pass
only = []string{"a", "z"}
v = IsNormal(b, Only(only), nil)
fmt.Println(v)
// In order only , pass
only = []string{"z", "a"}
v = IsNormal(b, Only(only), nil)
fmt.Println(v)
////////////////////
// Need
////////////////////
fmt.Println("\nNeed")
// Need missing field, fail
need := []string{"a", "y", "z"}
v = IsNormal(b, Need(need), nil)
fmt.Println(v)
// Need out of order, pass
need = []string{"a", "z"}
v = IsNormal(b, Need(need), nil)
fmt.Println(v)
// Need with option missing, pass
need = []string{"a", "z"}
opt := []string{"y"}
v = IsNormal(b, Need(need), Option(opt))
fmt.Println(v)
// Need with option present, pass
v = IsNormal(by, Need(need), Option(opt))
fmt.Println(v)
// Need with option and extra field, fail
need = []string{"a"}
opt = []string{"z"}
v = IsNormal(by, Need(need), opt)
fmt.Println(v)
////////////////////
// Order
////////////////////
fmt.Println("\nOrder")
// Order missing field, fail
order := []string{"z", "a", "y"}
v = IsNormal(b, Order(order), nil)
fmt.Println(v)
// Order out of order, fail
order = []string{"a", "z"}
v = IsNormal(b, Order(order), nil)
fmt.Println(v)
// Order extra field, pass
order = []string{"z", "y"}
v = IsNormal(by, Order(order), nil)
fmt.Println(v)
// Order with option missing, pass
order = []string{"z", "a"}
opt = []string{"y"}
v = IsNormal(b, Order(order), Option(opt))
fmt.Println(v)
// Order with option present, pass
order = []string{"z", "y"}
opt = []string{"a"}
v = IsNormal(by, Order(order), Option(opt))
fmt.Println(v)
// Order with option and extra field in the middle, fail
order = []string{"a"}
opt = []string{"y"}
v = IsNormal(by, Order(order), opt)
fmt.Println(v)
// Order with option and extra field at the end, fail
order = []string{"a"}
opt = []string{"z"}
v = IsNormal(by, Order(order), opt)
fmt.Println(v)
////////////////////
// Option
////////////////////
fmt.Println("\nOption")
// Option with one missing, pass
option := []string{"z", "a", "y"}
v = IsNormal(b, Option(option), nil)
fmt.Println(v)
// Option with one missing, fail
option = []string{"z", "y"}
v = IsNormal(by, Option(option), nil)
fmt.Println(v)
//Output:
// Canon
// true
// false
//
// Only
// false
// true
// true
//
// Need
// false
// true
// true
// true
// false
//
// Order
// false
// false
// true
// true
// true
// false
// false
//
// Option
// true
// false
}