| 1 |
package uuid |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
) |
| 9 |
|
| 10 |
func TestUuid_Bytes(t *testing.T) { |
| 11 |
id := make(Uuid, length) |
| 12 |
copy(id, NameSpaceDNS.Bytes()) |
| 13 |
assert.Equal(t, id.Bytes(), NameSpaceDNS.Bytes(), "Bytes should be the same") |
| 14 |
} |
| 15 |
|
| 16 |
func TestUuid_Size(t *testing.T) { |
| 17 |
id := make(Uuid, length) |
| 18 |
assert.Equal(t, 16, id.Size(), "The size of the array should be sixteen") |
| 19 |
} |
| 20 |
|
| 21 |
func TestUuid_String(t *testing.T) { |
| 22 |
id := Uuid(uuidBytes) |
| 23 |
assert.Equal(t, idString, id.String(), "The Format given should match the output") |
| 24 |
} |
| 25 |
|
| 26 |
func TestUuid_Variant(t *testing.T) { |
| 27 |
bytes := make(Uuid, length) |
| 28 |
copy(bytes, uuidBytes[:]) |
| 29 |
|
| 30 |
for _, v := range uuidVariants { |
| 31 |
for i := 0; i <= 255; i++ { |
| 32 |
bytes[variantIndex] = byte(i) |
| 33 |
id := createMarshaler(bytes, 4, v) |
| 34 |
b := id[variantIndex] >> 4 |
| 35 |
tVariantConstraint(v, b, id, t) |
| 36 |
assert.Equal(t, v, id.Variant(), "%x does not resolve to %x", id.Variant(), v) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
assert.True(t, didMarshalerSetVariantPanic(bytes[:]), "Array creation should panic if invalid variant") |
| 41 |
} |
| 42 |
|
| 43 |
func didMarshalerSetVariantPanic(bytes []byte) bool { |
| 44 |
return func() (didPanic bool) { |
| 45 |
defer func() { |
| 46 |
if recover() != nil { |
| 47 |
didPanic = true |
| 48 |
} |
| 49 |
}() |
| 50 |
|
| 51 |
createMarshaler(bytes[:], 4, 0xbb) |
| 52 |
return |
| 53 |
}() |
| 54 |
} |
| 55 |
|
| 56 |
func TestUuid_Version(t *testing.T) { |
| 57 |
id := make(Uuid, length) |
| 58 |
|
| 59 |
bytes := make(Uuid, length) |
| 60 |
copy(bytes, uuidBytes[:]) |
| 61 |
|
| 62 |
assert.Equal(t, Unknown, id.Version(), "The version should be 0") |
| 63 |
|
| 64 |
for v := 0; v < 16; v++ { |
| 65 |
for i := 0; i <= 255; i++ { |
| 66 |
bytes[versionIndex] = byte(i) |
| 67 |
copy(id, bytes) |
| 68 |
setVersion(&id[versionIndex], v) |
| 69 |
if v > 0 && v < 6 { |
| 70 |
assert.Equal(t, Version(v), id.Version(), "%x does not resolve to %x", id.Version(), v) |
| 71 |
} else { |
| 72 |
assert.Equal(t, Version(v), getVersion(id), "%x does not resolve to %x", getVersion(id), v) |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestImmutable_Bytes(t *testing.T) { |
| 79 |
b := make([]byte, length) |
| 80 |
copy(b[:], NameSpaceDNS.Bytes()) |
| 81 |
|
| 82 |
id := Immutable(b) |
| 83 |
|
| 84 |
assert.Equal(t, NameSpaceDNS.Bytes(), id.Bytes()) |
| 85 |
} |
| 86 |
|
| 87 |
func TestImmutable_Size(t *testing.T) { |
| 88 |
assert.Equal(t, 16, Nil.Size(), "The size of the array should be sixteen") |
| 89 |
} |
| 90 |
|
| 91 |
func TestImmutable_String(t *testing.T) { |
| 92 |
id := Immutable(uuidBytes) |
| 93 |
assert.Equal(t, idString, id.String(), "The Format given should match the output") |
| 94 |
} |
| 95 |
|
| 96 |
func TestImmutable_Variant(t *testing.T) { |
| 97 |
bytes := make(Uuid, length) |
| 98 |
copy(bytes, uuidBytes[:]) |
| 99 |
|
| 100 |
for _, v := range uuidVariants { |
| 101 |
for i := 0; i <= 255; i++ { |
| 102 |
bytes[variantIndex] = byte(i) |
| 103 |
id := createMarshaler(bytes, 4, v) |
| 104 |
b := id[variantIndex] >> 4 |
| 105 |
tVariantConstraint(v, b, id, t) |
| 106 |
id2 := Immutable(id) |
| 107 |
assert.Equal(t, v, id2.Variant(), "%x does not resolve to %x", id2.Variant(), v) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestImmutable_Version(t *testing.T) { |
| 113 |
|
| 114 |
id := make(Uuid, length) |
| 115 |
bytes := make(Uuid, length) |
| 116 |
copy(bytes, uuidBytes[:]) |
| 117 |
|
| 118 |
for v := 0; v < 16; v++ { |
| 119 |
for i := 0; i <= 255; i++ { |
| 120 |
bytes[versionIndex] = byte(i) |
| 121 |
copy(id, bytes) |
| 122 |
setVersion(&id[versionIndex], v) |
| 123 |
id2 := Immutable(id) |
| 124 |
|
| 125 |
if v > 0 && v < 6 { |
| 126 |
assert.Equal(t, Version(v), id2.Version(), "%x does not resolve to %x", id2.Version(), v) |
| 127 |
} else { |
| 128 |
assert.Equal(t, Version(v), getVersion(Uuid(id)), "%x does not resolve to %x", getVersion(Uuid(id)), v) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestUuid_MarshalBinary(t *testing.T) { |
| 135 |
id := Uuid(uuidBytes) |
| 136 |
bytes, err := id.MarshalBinary() |
| 137 |
assert.Nil(t, err, "There should be no error") |
| 138 |
assert.Equal(t, uuidBytes[:], bytes, "Byte should be the same") |
| 139 |
} |
| 140 |
|
| 141 |
func TestUuid_UnmarshalBinary(t *testing.T) { |
| 142 |
|
| 143 |
assert.True(t, didUnmarshalPanic(), "Should panic") |
| 144 |
|
| 145 |
u := Uuid{} |
| 146 |
err := u.UnmarshalBinary([]byte{1, 2, 3, 4, 5}) |
| 147 |
|
| 148 |
assert.Error(t, err, "Expect length error") |
| 149 |
|
| 150 |
err = u.UnmarshalBinary(uuidBytes[:]) |
| 151 |
|
| 152 |
u = Uuid{} |
| 153 |
|
| 154 |
err = u.UnmarshalBinary(uuidBytes[:]) |
| 155 |
|
| 156 |
assert.Nil(t, err, "There should be no error but got %s", err) |
| 157 |
|
| 158 |
for k, v := range namespaces { |
| 159 |
id, _ := Parse(v) |
| 160 |
u = Uuid{} |
| 161 |
u.UnmarshalBinary(id.Bytes()) |
| 162 |
|
| 163 |
assert.Equal(t, id.Bytes(), u.Bytes(), "The array id should equal the uuid id") |
| 164 |
assert.Equal(t, k.Bytes(), u.Bytes(), "The array id should equal the uuid id") |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func didUnmarshalPanic() bool { |
| 169 |
return func() (didPanic bool) { |
| 170 |
defer func() { |
| 171 |
if recover() != nil { |
| 172 |
didPanic = true |
| 173 |
} |
| 174 |
}() |
| 175 |
u := make(Uuid, length) |
| 176 |
u.UnmarshalBinary(uuidBytes[:]) |
| 177 |
return |
| 178 |
}() |
| 179 |
} |
| 180 |
|
| 181 |
func TestUuid_Scan(t *testing.T) { |
| 182 |
var v Uuid |
| 183 |
assert.Nil(t, v) |
| 184 |
|
| 185 |
err := v.Scan(nil) |
| 186 |
assert.NoError(t, err, "When nil there should be no error") |
| 187 |
assert.Empty(t, v, "Should have no data") |
| 188 |
|
| 189 |
err = v.Scan("") |
| 190 |
assert.NoError(t, err, "When nil there should be no error") |
| 191 |
assert.Empty(t, v, "Should have no data") |
| 192 |
|
| 193 |
var v2 Uuid |
| 194 |
err = v2.Scan(NameSpaceDNS.Bytes()) |
| 195 |
assert.NoError(t, err, "When nil there should be no error") |
| 196 |
assert.Equal(t, NameSpaceDNS.Bytes(), v2.Bytes(), "Values should be the same") |
| 197 |
|
| 198 |
err = v.Scan(NameSpaceDNS.String()) |
| 199 |
assert.NoError(t, err, "When nil there should be no error") |
| 200 |
assert.Equal(t, NameSpaceDNS.String(), v.String(), "Values should be the same") |
| 201 |
|
| 202 |
var v3 Uuid |
| 203 |
err = v3.Scan([]byte(NameSpaceDNS.String())) |
| 204 |
assert.NoError(t, err, "When []byte represents string should be no error") |
| 205 |
assert.Equal(t, NameSpaceDNS.String(), v3.String(), "Values should be the same") |
| 206 |
|
| 207 |
err = v.Scan(22) |
| 208 |
assert.Error(t, err, "When wrong type should error") |
| 209 |
} |
| 210 |
|
| 211 |
func TestUuid_Value(t *testing.T) { |
| 212 |
var v Uuid |
| 213 |
assert.Nil(t, v) |
| 214 |
|
| 215 |
id, err := v.Value() |
| 216 |
assert.Nil(t, id, "There hsould be no driver valuie") |
| 217 |
assert.NoError(t, err, "There should be no error") |
| 218 |
|
| 219 |
ns := Uuid(NameSpaceDNS) |
| 220 |
|
| 221 |
id, err = ns.Value() |
| 222 |
assert.NotNil(t, id, "Ther hsould be a vliad driver value") |
| 223 |
assert.NoError(t, err, "There should be no error") |
| 224 |
} |
| 225 |
|
| 226 |
func getVersion(pId Uuid) Version { |
| 227 |
return Version(pId[versionIndex] >> 4) |
| 228 |
} |
| 229 |
|
| 230 |
func createMarshaler(pData []byte, pVersion int, pVariant uint8) Uuid { |
| 231 |
o := make(Uuid, length) |
| 232 |
copy(o, pData) |
| 233 |
setVersion(&o[versionIndex], pVersion) |
| 234 |
setVariant(&o[variantIndex], pVariant) |
| 235 |
return o |
| 236 |
} |
| 237 |
|
| 238 |
func setVersion(pByte *byte, pVersion int) { |
| 239 |
*pByte &= 0x0f |
| 240 |
*pByte |= uint8(pVersion << 4) |
| 241 |
} |
| 242 |
|
| 243 |
func setVariant(pByte *byte, pVariant uint8) { |
| 244 |
switch pVariant { |
| 245 |
case VariantRFC4122: |
| 246 |
*pByte &= variantSet |
| 247 |
case VariantFuture, VariantMicrosoft: |
| 248 |
*pByte &= 0x1F |
| 249 |
case VariantNCS: |
| 250 |
*pByte &= 0x7F |
| 251 |
default: |
| 252 |
panic(errors.New("uuid.setVariant: invalid variant mask")) |
| 253 |
} |
| 254 |
*pByte |= pVariant |
| 255 |
} |