-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefid_test.go
270 lines (227 loc) · 7.26 KB
/
refid_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
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
270
// Copyright (c) 2023 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package refid
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"testing"
"time"
"gotest.tools/v3/assert"
)
var (
refTagTest = byte(1)
testValWoutTag = "1hh39ed2w3n01fwsmk9w4d4x44"
testValWithTag = "1hh39ed2w3n03fwsmk9w4d4x44"
)
func TestParseVarious(t *testing.T) {
//// time prefix types
// no tag
_, err := Parse("065f5e3p0gk013cvyvm171gn9m")
assert.NilError(t, err)
_, err = Parse("018af2b8760426008d9bf6e81386154d")
assert.NilError(t, err)
_, err = Parse("AYryuHYEJgCNm_boE4YVTQ")
assert.NilError(t, err)
// with tag
_, err = Parse("065f5ef3q4k03p495rqw0g92sr")
assert.NilError(t, err)
_, err = Parse("018af2b9e3b92601d8892e2fc04122ce")
assert.NilError(t, err)
_, err = Parse("AYryueO5JgHYiS4vwEEizg")
assert.NilError(t, err)
//// random time prefix types
// no tag
_, err = Parse("sqpwgp85q3sg1jftqhyefasemc")
assert.NilError(t, err)
_, err = Parse("cdedc85905b8f300c9fabc7ce7ab2ea3")
assert.NilError(t, err)
_, err = Parse("ze3IWQW48wDJ-rx856suow")
assert.NilError(t, err)
// with tag
_, err = Parse("e02ddgb2zkyg3rmeza7jvwnn6g")
assert.NilError(t, err)
_, err = Parse("7004d6c162fcfd01e28efa8f2df2b534")
assert.NilError(t, err)
_, err = Parse("cATWwWL8_QHijvqPLfK1NA")
assert.NilError(t, err)
// bad ones
_, err = Parse("nope")
assert.Assert(t, err != nil, "expected to fail parsing invalid refid")
_, err = Parse("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")
assert.Assert(t, err != nil, "expected to fail parsing invalid refid")
_, err = Parse("!!!!!!!!!!!!!!!!!!!!!!")
assert.Assert(t, err != nil, "expected to fail parsing invalid refid")
_, err = Parse("!!!!!!!!!!!!!!!!!!!!!!!!!!")
assert.Assert(t, err != nil, "expected to fail parsing invalid refid")
maxTime := time.UnixMilli(35184372088831)
minTime := time.UnixMilli(0)
// max value with type set to TimePrefixed
x, err := Parse("zzzzzzzzzzzfzzzzzzzzzzzzzw")
assert.NilError(t, err)
assert.Assert(t, x.Tag() == 0xff)
assert.Assert(t, x.Time().Equal(maxTime))
// max value with type set to RandomPrefixed
x, err = Parse("zzzzzzzzzzzzzzzzzzzzzzzzzw")
assert.NilError(t, err)
assert.Assert(t, x.Tag() == 0xff)
assert.Assert(t, x.Time().Equal(minTime))
// base32 padding at the end, so >w is truncated to w
// TimePrefixed
x, err = Parse("zzzzzzzzzzzfzzzzzzzzzzzzzz")
assert.NilError(t, err)
assert.Assert(t, x.Tag() == 0xff)
assert.Assert(t, x.Time().Equal(maxTime))
// base32 padding at the end, so >w is truncated to w
// RandomPrefixed
x, err = Parse("zzzzzzzzzzzzzzzzzzzzzzzzzz")
assert.NilError(t, err)
assert.Assert(t, x.Tag() == 0xff)
// RandomPrefixed has zero time value
assert.Assert(t, x.Time().Equal(minTime))
// min value with type set to TimePrefixed
x, err = Parse("00000000000000000000000000")
assert.NilError(t, err)
assert.Assert(t, x.Tag() == 0x00)
assert.Assert(t, x.Time().Equal(minTime))
val_max := [16]byte{}
val_min := [16]byte{}
for i := 0; i < 16; i++ {
val_max[i] = 0xff
val_min[i] = 0x00
}
x, err = FromBytes(val_max[:])
assert.NilError(t, err)
assert.Assert(t, x.Tag() == 0xff)
assert.Assert(t, x.Type() == RandomPrefixed)
assert.Assert(t, x.Time().Equal(minTime))
x, err = FromBytes(val_min[:])
assert.NilError(t, err)
assert.Assert(t, x.Tag() == 0x00)
assert.Assert(t, x.Type() == TimePrefixed)
assert.Assert(t, x.Time().Equal(minTime))
}
func TestGetTime(t *testing.T) {
t.Parallel()
// divide times by 10, so we are close enough
t0 := time.Now().UTC().Unix() / 10
r := Must(New())
vz := r.Time().UTC().Unix() / 10
assert.Equal(t, t0, vz)
r2 := Must(Parse(testValWoutTag))
fmt.Println(r2.Time())
ts, _ := time.Parse(time.RFC3339, "2023-12-07T23:22:43.676Z")
assert.Equal(t, ts.UTC(), r2.Time().UTC())
}
func TestSetTime(t *testing.T) {
t.Parallel()
ts, _ := time.Parse(time.RFC3339, "2023-01-14T18:29:00Z")
r := Must(New())
r.SetTime(ts)
assert.Equal(t, ts.UTC(), r.Time().UTC())
// try to set a time too far into the future
err := r.SetTime(time.UnixMilli(maxTime + 1))
fmt.Printf("ERRRR: %s\n", err)
assert.Assert(t, err != nil, "expected to error")
r = Must(NewRandom())
// should error with random type
err = r.SetTime(ts)
assert.Assert(t, err != nil, "expected to error")
assert.Assert(t, r.Time().Equal(time.UnixMilli(0)))
}
func TestBase64RoundTrip(t *testing.T) {
t.Parallel()
r := Must(Parse(testValWithTag))
b64 := r.ToBase64String()
r2, err := Parse(b64)
assert.NilError(t, err)
assert.Equal(t, r.String(), r2.String())
}
func TestHexRoundTrip(t *testing.T) {
t.Parallel()
r := Must(Parse(testValWithTag))
b64 := r.ToHexString()
r2, err := Parse(b64)
assert.NilError(t, err)
assert.Equal(t, r.String(), r2.String())
}
func TestRoundTrip(t *testing.T) {
t.Parallel()
u := Must(New())
r := Must(Parse(u.String()))
assert.Check(t, !u.HasTag(refTagTest))
assert.Check(t, !r.HasTag(refTagTest))
assert.Equal(t, u.String(), r.String())
u = Must(NewTagged(refTagTest))
r = Must(Parse(u.String()))
assert.Check(t, u.HasTag(refTagTest))
assert.Check(t, r.HasTag(refTagTest))
assert.Equal(t, u.String(), r.String())
u = Must(NewRandom())
r = Must(Parse(u.String()))
assert.Check(t, !u.HasTag(refTagTest))
assert.Check(t, !r.HasTag(refTagTest))
assert.Equal(t, u.String(), r.String())
u = Must(NewRandomTagged(refTagTest))
r = Must(Parse(u.String()))
assert.Check(t, u.HasTag(refTagTest))
assert.Check(t, r.HasTag(refTagTest))
assert.Equal(t, u.String(), r.String())
}
func TestSetTag(t *testing.T) {
t.Parallel()
r := Must(Parse(testValWoutTag))
assert.Check(t, !r.HasTag(refTagTest))
assert.Equal(t, r.String(), testValWoutTag)
assert.Equal(t, (&r).String(), testValWoutTag)
r.SetTag(refTagTest)
assert.Check(t, r.HasTag(refTagTest))
assert.Equal(t, r.String(), testValWithTag)
assert.Equal(t, (&r).String(), testValWithTag)
r.ClearTag()
assert.Check(t, !r.HasTag(refTagTest))
assert.Equal(t, r.String(), testValWoutTag)
assert.Equal(t, (&r).String(), testValWoutTag)
r2 := Must(Parse(testValWoutTag))
r2.SetTag(1)
assert.Equal(t, r2.ToHexString(), "0c6234b9a2e0ea01bf99a4d3c2349d21")
r2.ClearTag()
assert.Equal(t, r2.ToHexString(), "0c6234b9a2e0ea00bf99a4d3c2349d21")
r2.SetTag(2)
assert.Equal(t, r2.ToHexString(), "0c6234b9a2e0ea02bf99a4d3c2349d21")
}
func TestAmbiguous(t *testing.T) {
t.Parallel()
rd0 := Must(Parse(testValWoutTag))
rd1 := Must(Parse(testValWoutTag))
rd2 := Must(Parse(testValWoutTag))
assert.Assert(t, rd0.String() == rd1.String() && rd1.String() == rd2.String())
assert.Assert(t, rd0.Equal(rd1) && rd1.Equal(rd2))
}
func TestTemplateStringer(t *testing.T) {
t.Parallel()
s := Must(Parse(testValWoutTag))
assert.Equal(t, fmt.Sprintf("%s", s), testValWoutTag)
tpl := template.Must(template.New("name").Parse(`{{.}}`))
var b bytes.Buffer
err := tpl.Execute(&b, s)
assert.NilError(t, err)
assert.Equal(t, b.String(), testValWoutTag)
}
func TestJsonMarshal(t *testing.T) {
t.Parallel()
s := Must(Parse(testValWoutTag))
j, err := json.Marshal(s)
assert.NilError(t, err)
assert.Equal(t, string(j), fmt.Sprintf("%q", s.String()))
}
func TestJsonUnmarshal(t *testing.T) {
t.Parallel()
data := fmt.Sprintf("%q", testValWoutTag)
var r ID
err := json.Unmarshal([]byte(data), &r)
assert.NilError(t, err)
assert.Equal(t, r.String(), testValWoutTag)
}