-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_test.go
211 lines (196 loc) · 7.21 KB
/
uuid_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
package uuid
import (
"bytes"
"crypto/rand"
"net"
"reflect"
"testing"
"time"
)
const (
testVecTimeCustom int64 = 1621171244987654321 // 2021-05-16T13:20:44.987654321Z
testVecTimeRFC int64 = 1645557742000000000 // 2022-02-22T07:22:22.00Z
)
func testTime(tm int64) {
CurrentTime = func() time.Time { return time.Unix(0, tm) }
}
func trueTime() {
CurrentTime = time.Now
}
var random = rand.Reader // store rand.Reader since it is replaced by static data by some tests
func testRand(b ...byte) {
rand.Reader = bytes.NewBuffer(b)
}
func trueRand() {
rand.Reader = random
}
func TestMustString(t *testing.T) {
trueRand()
id := MustString(NewV4())
if len(id) != 36 {
t.Errorf("Generated UUID length is invalid. Should be 36 but is: %d", len(id))
}
if id[8] != '-' || id[13] != '-' || id[18] != '-' || id[23] != '-' {
t.Errorf("Dash placement invalid. Should be 8, 13, 18, 23 but is %s", id)
}
}
func TestUUID_IsNil(t *testing.T) {
tests := []struct {
name string
UUID *UUID
want bool
}{
{"NotNil", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"Nil", &UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.UUID.IsNil(); got != tt.want {
t.Errorf("UUID.IsNil() = %v, want %v", got, tt.want)
}
})
}
}
func TestUUID_IsMax(t *testing.T) {
tests := []struct {
name string
UUID *UUID
want bool
}{
{"NotMax", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"Max", &UUID{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.UUID.IsMax(); got != tt.want {
t.Errorf("UUID.IsMax() = %v, want %v", got, tt.want)
}
})
}
}
func TestUUID_Version(t *testing.T) {
tests := []struct {
name string
UUID *UUID
want int
}{
{"V0", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x06, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 0},
{"V1", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x16, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 1},
{"V2", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x26, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 2},
{"V3", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x36, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 3},
{"V4", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 4},
{"V5", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x56, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 5},
{"V6", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x66, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 6},
{"V7", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x76, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 7},
{"V8", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x86, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 8},
{"V9", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x96, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 9},
{"V10", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0xA6, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 10},
{"V11", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0xB6, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 11},
{"V12", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0xC6, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 12},
{"V13", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0xD6, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 13},
{"V14", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0xE6, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 14},
{"V15", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0xF6, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, 15},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.UUID.Version(); got != tt.want {
t.Errorf("UUID.Version() = %v, want %v", got, tt.want)
}
})
}
}
func TestUUID_Timestamp(t *testing.T) {
tests := []struct {
name string
uuid UUID
want time.Time
}{
{"UUIDv7", UUID{0x01, 0x79, 0x75, 0x56, 0x0F, 0xBB, 0x70, 0x00, 0x81, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, time.Unix(1621171244, 987*1000000)},
{"UUIDv4", UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, time.Unix(0, 0)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.uuid.Timestamp(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("UUID.Timestamp() = %v, want %v", got, tt.want)
}
})
}
}
func Test_intervalsSinceEpoch(t *testing.T) {
tests := []struct {
name string
fakeTime int64
want int64
}{
{"TestTime", testVecTimeCustom, 138404640449876543},
{"TestTime+1d", testVecTimeCustom + 86400000000000, 138404640449876543 + 864000000000},
{"TestTime-3y", testVecTimeCustom - 94672800000000000, 138404640449876543 - 946728000000000},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testTime(tt.fakeTime)
diff1 := time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC).Sub(time.Date(1582, 10, 15, 0, 0, 0, 0, time.UTC)).Nanoseconds() / 100
diff2 := CurrentTime().Sub(time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC)).Nanoseconds() / 100
diff := diff1 + diff2
if got := intervalsSinceEpoch(); got != tt.want {
t.Errorf("intervalsSinceEpoch() = %v, want %v, calculated %v", got, tt.want, diff)
}
})
}
}
func Test_getHWAddr(t *testing.T) {
tests := []struct {
name string
useHWMAC bool
fakeRandom []byte
resetRandom bool
want net.HardwareAddr
wantErr bool
}{
{"DeviceMAC", true, nil, false, nil, false},
{"RandomMAC", false, []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}, false, []byte{0x03, 0x23, 0x45, 0x67, 0x89, 0xAB}, false},
{"SameRandomMAC", false, nil, false, []byte{0x03, 0x23, 0x45, 0x67, 0x89, 0xAB}, false},
{"DifferentRandomMAC", false, []byte{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}, true, []byte{0xFF, 0xDC, 0xBA, 0x98, 0x76, 0x54}, false},
{"NoRandom", false, nil, true, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
UseHardwareMAC = tt.useHWMAC
testRand(tt.fakeRandom...)
if tt.resetRandom {
RandomMAC = nil
}
got, err := getHWAddr()
if (err != nil) != tt.wantErr {
t.Errorf("getHWAddr() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.want == nil && !tt.wantErr && (got == nil || got[0]&0x03 != 0x00) {
t.Errorf("getHWAddr() = %v, which is not global unicast", got)
}
if tt.want != nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("getHWAddr() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getHWAddrRandom(t *testing.T) {
trueRand()
UseHardwareMAC = false
for i := 0; i < 1000; i++ {
RandomMAC = nil
addr, err := getHWAddr()
if err != nil {
t.Errorf("getHWAddr() error = %v", err)
}
if addr == nil {
t.Errorf("getHWAddr() returned nil value without error")
}
if len(addr) != 6 {
t.Errorf("getHWAddr() returned invalid length %d", len(addr))
}
if addr[0]&0x03 != 0x03 {
t.Errorf("getHWAddr() = %v, which is not local multicast", addr)
}
}
}