-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_test.go
More file actions
217 lines (170 loc) · 5.65 KB
/
Copy pathencode_test.go
File metadata and controls
217 lines (170 loc) · 5.65 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
package core_test
import (
. "dappco.re/go"
)
// --- HexEncode ---
func TestEncode_HexEncode_Good(t *T) {
AssertEqual(t, "68656c6c6f", HexEncode([]byte("hello")))
}
func TestEncode_HexEncode_Bad(t *T) {
AssertEqual(t, "", HexEncode(nil))
AssertEqual(t, HexEncode(nil), HexEncode([]byte{}))
}
func TestEncode_HexEncode_Ugly(t *T) {
src := []byte{0x00, 0x0f, 0x10, 0xff}
encoded := HexEncode(src)
src[0] = 0xff
AssertEqual(t, "000f10ff", encoded)
AssertNotEqual(t, encoded, HexEncode(src))
}
// --- HexDecode ---
func TestEncode_HexDecode_Good(t *T) {
r := HexDecode("68656c6c6f")
AssertTrue(t, r.OK)
AssertEqual(t, []byte("hello"), r.Value)
}
func TestEncode_HexDecode_Bad(t *T) {
r := HexDecode("not-hex")
AssertFalse(t, r.OK)
_, ok := r.Value.(error)
AssertTrue(t, ok)
}
func TestEncode_HexDecode_Ugly(t *T) {
r := HexDecode("abc")
AssertFalse(t, r.OK)
_, ok := r.Value.(error)
AssertTrue(t, ok)
}
// --- Base64Encode ---
func TestEncode_Base64Encode_Good(t *T) {
AssertEqual(t, "aGVsbG8=", Base64Encode([]byte("hello")))
}
func TestEncode_Base64Encode_Bad(t *T) {
AssertEqual(t, "", Base64Encode(nil))
AssertEqual(t, Base64Encode(nil), Base64Encode([]byte{}))
}
func TestEncode_Base64Encode_Ugly(t *T) {
src := []byte{0xfb, 0xff, 0xff}
encoded := Base64Encode(src)
src[0] = 0x00
AssertEqual(t, "+///", encoded)
AssertNotEqual(t, encoded, Base64Encode(src))
}
// --- Base64Decode ---
func TestEncode_Base64Decode_Good(t *T) {
r := Base64Decode("aGVsbG8=")
AssertTrue(t, r.OK)
AssertEqual(t, []byte("hello"), r.Value)
}
func TestEncode_Base64Decode_Bad(t *T) {
r := Base64Decode("not-base64")
AssertFalse(t, r.OK)
_, ok := r.Value.(error)
AssertTrue(t, ok)
}
func TestEncode_Base64Decode_Ugly(t *T) {
r := Base64Decode("aGVsbG8===")
AssertFalse(t, r.OK)
_, ok := r.Value.(error)
AssertTrue(t, ok)
}
// --- Base64URLEncode ---
func TestEncode_Base64URLEncode_Good(t *T) {
AssertEqual(t, "aGVsbG8=", Base64URLEncode([]byte("hello")))
}
func TestEncode_Base64URLEncode_Bad(t *T) {
AssertEqual(t, "", Base64URLEncode(nil))
AssertEqual(t, Base64URLEncode(nil), Base64URLEncode([]byte{}))
}
func TestEncode_Base64URLEncode_Ugly(t *T) {
src := []byte{0xfb, 0xff, 0xff}
encoded := Base64URLEncode(src)
src[0] = 0x00
AssertEqual(t, "-___", encoded)
AssertNotEqual(t, encoded, Base64URLEncode(src))
}
// --- Base64URLDecode ---
func TestEncode_Base64URLDecode_Good(t *T) {
r := Base64URLDecode("aGVsbG8=")
AssertTrue(t, r.OK)
AssertEqual(t, []byte("hello"), r.Value)
}
func TestEncode_Base64URLDecode_Bad(t *T) {
r := Base64URLDecode("not+url")
AssertFalse(t, r.OK)
_, ok := r.Value.(error)
AssertTrue(t, ok)
}
func TestEncode_Base64URLDecode_Ugly(t *T) {
r := Base64URLDecode("aGVsbG8===")
AssertFalse(t, r.OK)
_, ok := r.Value.(error)
AssertTrue(t, ok)
}
// --- BigEndianUint64 ---
func TestEncode_BigEndianUint64_Good(t *T) {
AssertEqual(t, uint64(1), BigEndianUint64([]byte{0, 0, 0, 0, 0, 0, 0, 1}))
}
// Bad: byte order is the whole point — the same bytes read big-endian and
// little-endian must not agree, or a caller has picked the wrong one.
func TestEncode_BigEndianUint64_Bad(t *T) {
b := []byte{1, 0, 0, 0, 0, 0, 0, 0}
AssertEqual(t, uint64(1)<<56, BigEndianUint64(b))
AssertFalse(t, BigEndianUint64(b) == LittleEndianUint64(b))
}
// Ugly: only the first eight bytes are read, so a longer slice is not an error
// and the tail is ignored.
func TestEncode_BigEndianUint64_Ugly(t *T) {
AssertEqual(t, ^uint64(0), BigEndianUint64([]byte{255, 255, 255, 255, 255, 255, 255, 255, 9, 9}))
}
// --- LittleEndianUint64 ---
func TestEncode_LittleEndianUint64_Good(t *T) {
AssertEqual(t, uint64(1), LittleEndianUint64([]byte{1, 0, 0, 0, 0, 0, 0, 0}))
}
func TestEncode_LittleEndianUint64_Bad(t *T) {
AssertEqual(t, uint64(1)<<56, LittleEndianUint64([]byte{0, 0, 0, 0, 0, 0, 0, 1}))
}
// Ugly: round-trips with the write side, which is how Keccak absorbs and
// squeezes its state.
func TestEncode_LittleEndianUint64_Ugly(t *T) {
buf := make([]byte, 8)
PutLittleEndianUint64(buf, 0xDEADBEEFCAFEBABE)
AssertEqual(t, uint64(0xDEADBEEFCAFEBABE), LittleEndianUint64(buf))
}
// --- PutLittleEndianUint64 ---
func TestEncode_PutLittleEndianUint64_Good(t *T) {
buf := make([]byte, 8)
PutLittleEndianUint64(buf, 1)
AssertEqual(t, byte(1), buf[0])
AssertEqual(t, byte(0), buf[7])
}
// Bad: writes exactly eight bytes and nothing beyond them, so a caller can
// write into the middle of a larger buffer without clobbering its neighbours.
func TestEncode_PutLittleEndianUint64_Bad(t *T) {
buf := make([]byte, 10)
buf[8], buf[9] = 7, 7
PutLittleEndianUint64(buf[:8], ^uint64(0))
AssertEqual(t, byte(7), buf[8])
AssertEqual(t, byte(7), buf[9])
}
func TestEncode_PutLittleEndianUint64_Ugly(t *T) {
buf := make([]byte, 16)
PutLittleEndianUint64(buf[8:], 0x0102030405060708)
AssertEqual(t, uint64(0), LittleEndianUint64(buf[:8]))
AssertEqual(t, uint64(0x0102030405060708), LittleEndianUint64(buf[8:]))
}
// --- HexAppendEncode ---
func TestEncode_HexAppendEncode_Good(t *T) {
AssertEqual(t, "0aff", AsString(HexAppendEncode(nil, []byte{0x0a, 0xff})))
}
// Bad: an empty src appends nothing at all — dst comes back untouched rather
// than gaining a separator or a padding byte.
func TestEncode_HexAppendEncode_Bad(t *T) {
AssertEqual(t, "id-", AsString(HexAppendEncode([]byte("id-"), nil)))
}
// Ugly: the reason this exists over HexEncode — it extends dst in place, so a
// caller building an identifier stays at one allocation.
func TestEncode_HexAppendEncode_Ugly(t *T) {
AssertEqual(t, "id-42-0aff", AsString(HexAppendEncode([]byte("id-42-"), []byte{0x0a, 0xff})))
AssertEqual(t, HexEncode([]byte{0x0a, 0xff}), AsString(HexAppendEncode(nil, []byte{0x0a, 0xff})))
}