This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid_test.go
232 lines (221 loc) · 7.14 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package uuid
import (
"bytes"
"crypto/rand"
"reflect"
"testing"
)
func TestParse(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
wantUUID UUID
wantErr bool
}{
{"Normal", args{"686e7778-f9f0-4622-a13e-c2441ce4ae41"}, UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"UpperCase", args{"686E7778-F9F0-4622-A13E-C2441CE4AE41"}, UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"MixedCase", args{"686E7778-F9f0-4622-A13e-C2441cE4aE41"}, UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"WrongLength", args{"01234567-89ab-cdef"}, UUID{}, true},
{"WrongDashPlacement", args{"0123-456-89ab-cdef-012345-6789abcdef"}, UUID{}, true},
{"NonHexCharactersSection1", args{"ghijklmn-abcd-abcd-abcd-0123456789ab"}, UUID{}, true},
{"NonHexCharactersSection2", args{"abcdef01-opqr-abcd-abcd-0123456789ab"}, UUID{}, true},
{"NonHexCharactersSection3", args{"abcdef01-abcd-stuv-abcd-0123456789ab"}, UUID{}, true},
{"NonHexCharactersSection4", args{"abcdef01-abcd-abcd-wxyz-0123456789ab"}, UUID{}, true},
{"NonHexCharactersSection5", args{"abcdef01-abcd-abcd-abcd-ghijklmnopqr"}, UUID{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotUUID, err := Parse(tt.args.str)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotUUID, tt.wantUUID) {
t.Errorf("Parse() = %v, want %v", gotUUID, tt.wantUUID)
}
})
}
}
func TestParseBytes(t *testing.T) {
type args struct {
bytes []byte
}
tests := []struct {
name string
args args
wantUUID UUID
wantErr bool
}{
{"Normal", args{[]byte{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}}, UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"WrongLength", args{[]byte{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e}}, UUID{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotUUID, err := ParseBytes(tt.args.bytes)
if (err != nil) != tt.wantErr {
t.Errorf("ParseBytes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotUUID, tt.wantUUID) {
t.Errorf("ParseBytes() = %v, want %v", gotUUID, tt.wantUUID)
}
})
}
}
func TestNew(t *testing.T) {
id, err := New()
if reflect.TypeOf(id).String() != "uuid.UUID" {
t.Error("New should generate new UUID of type uuid.UUID")
}
if err != nil {
t.Errorf("Failed to generate UUID. Random generator failed: %s", err.Error())
}
random := rand.Reader
rand.Reader = bytes.NewBuffer(nil)
_, err = New()
if err == nil {
t.Error("New did not fail when no random data was available")
}
rand.Reader = random
}
func TestUUID_String(t *testing.T) {
tests := []struct {
name string
UUID *UUID
wantOut string
}{
{"ConvertToString", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, "686e7778-f9f0-4622-a13e-c2441ce4ae41"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOut := tt.UUID.String(); gotOut != tt.wantOut {
t.Errorf("UUID.String() = %v, want %v", gotOut, tt.wantOut)
}
})
}
}
func TestNewString(t *testing.T) {
id, err := NewString()
if err != nil {
t.Errorf("Failed to generate UUID. Random generator failed: %s", err.Error())
}
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)
}
random := rand.Reader
rand.Reader = bytes.NewBuffer(nil)
_, err = NewString()
if err == nil {
t.Error("NewString did not fail when no random data was available")
}
rand.Reader = random
}
func TestUUID_IsEmpty(t *testing.T) {
tests := []struct {
name string
UUID *UUID
want bool
}{
{"NotEmpty", &UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
{"Empty", &UUID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.UUID.IsEmpty(); got != tt.want {
t.Errorf("UUID.IsEmpty() = %v, want %v", got, tt.want)
}
})
}
}
func TestUUID_MarshalText(t *testing.T) {
tests := []struct {
name string
uuid UUID
want []byte
wantErr bool
}{
{"Normal", UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, []byte("686e7778-f9f0-4622-a13e-c2441ce4ae41"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.uuid.MarshalText()
if (err != nil) != tt.wantErr {
t.Errorf("UUID.MarshalText() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UUID.MarshalText() = %v, want %v", got, tt.want)
}
})
}
}
func TestUUID_UnmarshalText(t *testing.T) {
type args struct {
in []byte
}
tests := []struct {
name string
uuid *UUID
args args
wantErr bool
}{
{"Normal", &UUID{}, args{[]byte{0x36, 0x38, 0x36, 0x65, 0x37, 0x37, 0x37, 0x38, 0x2d, 0x66, 0x39, 0x66, 0x30, 0x2d, 0x34, 0x36, 0x32, 0x32, 0x2d, 0x61, 0x31, 0x33, 0x65, 0x2d, 0x63, 0x32, 0x34, 0x34, 0x31, 0x63, 0x65, 0x34, 0x61, 0x65, 0x34, 0x31}}, false},
{"InvalidID", &UUID{}, args{[]byte{}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.uuid.UnmarshalText(tt.args.in); (err != nil) != tt.wantErr {
t.Errorf("UUID.UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestUUID_MarshalBinary(t *testing.T) {
tests := []struct {
name string
uuid UUID
want []byte
wantErr bool
}{
{"Normal", UUID{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, []byte{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.uuid.MarshalBinary()
if (err != nil) != tt.wantErr {
t.Errorf("UUID.MarshalBinary() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UUID.MarshalBinary() = %v, want %v", got, tt.want)
}
})
}
}
func TestUUID_UnmarshalBinary(t *testing.T) {
type args struct {
in []byte
}
tests := []struct {
name string
uuid *UUID
args args
wantErr bool
}{
{"Normal", &UUID{}, args{[]byte{0x68, 0x6e, 0x77, 0x78, 0xf9, 0xf0, 0x46, 0x22, 0xa1, 0x3e, 0xc2, 0x44, 0x1c, 0xe4, 0xae, 0x41}}, false},
{"InvalidID", &UUID{}, args{[]byte{}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.uuid.UnmarshalBinary(tt.args.in); (err != nil) != tt.wantErr {
t.Errorf("UUID.UnmarshalBinary() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}