-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjcs_test.go
More file actions
126 lines (117 loc) · 2.68 KB
/
Copy pathjcs_test.go
File metadata and controls
126 lines (117 loc) · 2.68 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
package peac
import (
"testing"
)
func TestCanonicalizeSimpleObject(t *testing.T) {
input := `{"b": 1, "a": 2}`
expected := `{"a":2,"b":1}`
got, err := Canonicalize([]byte(input))
if err != nil {
t.Fatal(err)
}
if string(got) != expected {
t.Errorf("got %s, want %s", got, expected)
}
}
func TestCanonicalizeNestedObject(t *testing.T) {
input := `{"z": {"b": 1, "a": 2}, "a": 3}`
expected := `{"a":3,"z":{"a":2,"b":1}}`
got, err := Canonicalize([]byte(input))
if err != nil {
t.Fatal(err)
}
if string(got) != expected {
t.Errorf("got %s, want %s", got, expected)
}
}
func TestCanonicalizeArray(t *testing.T) {
input := `[3, 1, "b", "a", true, null]`
expected := `[3,1,"b","a",true,null]`
got, err := Canonicalize([]byte(input))
if err != nil {
t.Fatal(err)
}
if string(got) != expected {
t.Errorf("got %s, want %s", got, expected)
}
}
func TestCanonicalizeNumbers(t *testing.T) {
tests := []struct {
input string
expected string
}{
{`1`, `1`},
{`1.0`, `1`},
{`0`, `0`},
{`-0`, `0`},
{`100`, `100`},
{`0.5`, `0.5`},
{`-1`, `-1`},
}
for _, tc := range tests {
got, err := Canonicalize([]byte(tc.input))
if err != nil {
t.Errorf("input %s: %v", tc.input, err)
continue
}
if string(got) != tc.expected {
t.Errorf("input %s: got %s, want %s", tc.input, got, tc.expected)
}
}
}
func TestCanonicalizeEmptyStructures(t *testing.T) {
tests := []struct {
input string
expected string
}{
{`{}`, `{}`},
{`[]`, `[]`},
{`{"a":{}}`, `{"a":{}}`},
{`{"a":[]}`, `{"a":[]}`},
}
for _, tc := range tests {
got, err := Canonicalize([]byte(tc.input))
if err != nil {
t.Errorf("input %s: %v", tc.input, err)
continue
}
if string(got) != tc.expected {
t.Errorf("input %s: got %s, want %s", tc.input, got, tc.expected)
}
}
}
func TestCanonicalizeStringEscaping(t *testing.T) {
input := `{"key": "hello\nworld\ttab"}`
expected := `{"key":"hello\nworld\ttab"}`
got, err := Canonicalize([]byte(input))
if err != nil {
t.Fatal(err)
}
if string(got) != expected {
t.Errorf("got %s, want %s", got, expected)
}
}
func TestJCSHash(t *testing.T) {
input := `{"b": 1, "a": 2}`
hash, err := JCSHash([]byte(input))
if err != nil {
t.Fatal(err)
}
if len(hash) != 71 { // "sha256:" + 64 hex chars
t.Errorf("hash length = %d, want 71", len(hash))
}
if hash[:7] != "sha256:" {
t.Errorf("hash prefix = %s, want sha256:", hash[:7])
}
}
func TestCanonicalizeBooleansAndNull(t *testing.T) {
input := `{"c": null, "b": false, "a": true}`
expected := `{"a":true,"b":false,"c":null}`
got, err := Canonicalize([]byte(input))
if err != nil {
t.Fatal(err)
}
if string(got) != expected {
t.Errorf("got %s, want %s", got, expected)
}
}