go.uuid/uuid_test.go

521 lines
14 KiB
Go
Raw Normal View History

2015-02-14 20:26:48 +03:00
// Copyright (C) 2013, 2015 by Maxim Bublis <b@codemonkey.ru>
2013-07-24 16:49:33 +04:00
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2013-07-23 18:08:05 +04:00
package uuid
2013-06-18 13:50:18 +04:00
import (
2013-12-05 13:51:57 +04:00
"bytes"
2013-06-18 13:50:18 +04:00
"testing"
)
2013-12-05 13:51:57 +04:00
func TestBytes(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
bytes1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
if !bytes.Equal(u.Bytes(), bytes1) {
t.Errorf("Incorrect bytes representation for UUID: %s", u)
}
}
2013-06-18 20:14:33 +04:00
func TestString(t *testing.T) {
2013-07-23 18:08:05 +04:00
if NamespaceDNS.String() != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
t.Errorf("Incorrect string representation for UUID: %s", NamespaceDNS.String())
2013-06-18 20:14:33 +04:00
}
}
2013-06-18 20:17:07 +04:00
func TestEqual(t *testing.T) {
2013-07-23 18:08:05 +04:00
if !Equal(NamespaceDNS, NamespaceDNS) {
t.Errorf("Incorrect comparison of %s and %s", NamespaceDNS, NamespaceDNS)
2013-06-18 20:17:07 +04:00
}
2013-07-23 18:08:05 +04:00
if Equal(NamespaceDNS, NamespaceURL) {
t.Errorf("Incorrect comparison of %s and %s", NamespaceDNS, NamespaceURL)
2013-06-18 20:17:07 +04:00
}
}
2013-12-03 20:01:39 +04:00
func TestOr(t *testing.T) {
u1 := UUID{0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff}
u2 := UUID{0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00}
u := UUID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
if !Equal(u, Or(u1, u2)) {
t.Errorf("Incorrect bitwise OR result %s", Or(u1, u2))
}
}
func TestAnd(t *testing.T) {
u1 := UUID{0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff}
u2 := UUID{0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00}
u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
if !Equal(u, And(u1, u2)) {
t.Errorf("Incorrect bitwise AND result %s", And(u1, u2))
}
}
2013-06-18 13:50:18 +04:00
func TestVersion(t *testing.T) {
2013-07-23 18:08:05 +04:00
u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
2013-06-18 13:50:18 +04:00
if u.Version() != 1 {
t.Errorf("Incorrect version for UUID: %d", u.Version())
}
}
func TestSetVersion(t *testing.T) {
2013-12-03 20:01:22 +04:00
u := UUID{}
2013-07-09 14:35:21 +04:00
u.SetVersion(4)
2013-06-18 13:50:18 +04:00
if u.Version() != 4 {
t.Errorf("Incorrect version for UUID after u.setVersion(4): %d", u.Version())
}
}
func TestVariant(t *testing.T) {
2013-07-23 18:08:05 +04:00
u1 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
2013-06-18 14:51:58 +04:00
2013-07-23 18:08:05 +04:00
if u1.Variant() != VariantNCS {
t.Errorf("Incorrect variant for UUID variant %d: %d", VariantNCS, u1.Variant())
2013-06-18 14:51:58 +04:00
}
2013-07-23 18:08:05 +04:00
u2 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
2013-06-18 14:51:58 +04:00
2013-07-23 18:08:05 +04:00
if u2.Variant() != VariantRFC4122 {
t.Errorf("Incorrect variant for UUID variant %d: %d", VariantRFC4122, u2.Variant())
2013-06-18 14:51:58 +04:00
}
2013-07-23 18:08:05 +04:00
u3 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
2013-06-18 14:51:58 +04:00
2013-07-23 18:08:05 +04:00
if u3.Variant() != VariantMicrosoft {
t.Errorf("Incorrect variant for UUID variant %d: %d", VariantMicrosoft, u3.Variant())
2013-06-18 14:51:58 +04:00
}
2013-07-23 18:08:05 +04:00
u4 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
2013-06-18 14:51:58 +04:00
2013-07-23 18:08:05 +04:00
if u4.Variant() != VariantFuture {
t.Errorf("Incorrect variant for UUID variant %d: %d", VariantFuture, u4.Variant())
2013-06-18 14:51:58 +04:00
}
2013-06-18 13:50:18 +04:00
}
func TestSetVariant(t *testing.T) {
2013-07-23 18:08:05 +04:00
u := new(UUID)
2013-07-09 14:35:21 +04:00
u.SetVariant()
2013-06-18 14:51:58 +04:00
2013-07-23 18:08:05 +04:00
if u.Variant() != VariantRFC4122 {
2013-06-18 14:51:58 +04:00
t.Errorf("Incorrect variant for UUID after u.setVariant(): %d", u.Variant())
}
2013-06-18 13:50:18 +04:00
}
2013-12-04 15:49:52 +04:00
func TestFromBytes(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
2013-12-04 15:49:52 +04:00
u1, err := FromBytes(b1)
2013-12-04 15:49:52 +04:00
if err != nil {
t.Errorf("Error parsing UUID from bytes: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
b2 := []byte{}
2013-12-04 15:49:52 +04:00
_, err = FromBytes(b2)
2013-12-04 15:49:52 +04:00
if err == nil {
t.Errorf("Should return error parsing from empty byte slice, got %s", err)
}
}
func TestMarshalBinary(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b2, err := u.MarshalBinary()
if err != nil {
t.Errorf("Error marshaling UUID: %s", err)
}
if !bytes.Equal(b1, b2) {
t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
}
}
func TestUnmarshalBinary(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
u1 := UUID{}
err := u1.UnmarshalBinary(b1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
b2 := []byte{}
u2 := UUID{}
err = u2.UnmarshalBinary(b2)
if err == nil {
t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
}
}
2013-12-04 19:01:44 +04:00
func TestFromString(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
s2 := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
s3 := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
_, err := FromString("")
if err == nil {
t.Errorf("Should return error trying to parse empty string, got %s", err)
}
u1, err := FromString(s1)
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
u2, err := FromString(s2)
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
if !Equal(u, u2) {
t.Errorf("UUIDs should be equal: %s and %s", u, u2)
}
u3, err := FromString(s3)
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
if !Equal(u, u3) {
t.Errorf("UUIDs should be equal: %s and %s", u, u3)
}
}
func TestFromStringShort(t *testing.T) {
// Invalid 35-character UUID string
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c"
for i := len(s1); i >= 0; i-- {
_, err := FromString(s1[:i])
if err == nil {
t.Errorf("Should return error trying to parse too short string, got %s", err)
}
}
}
2015-07-21 09:28:03 +03:00
func TestFromStringOrNil(t *testing.T) {
u := FromStringOrNil("")
if u != Nil {
t.Errorf("Should return Nil UUID on parse failure, got %s", u)
}
}
2015-07-22 12:38:27 +03:00
func TestFromBytesOrNil(t *testing.T) {
b := []byte{}
u := FromBytesOrNil(b)
if u != Nil {
t.Errorf("Should return Nil UUID on parse failure, got %s", u)
}
}
func TestMarshalText(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
b2, err := u.MarshalText()
if err != nil {
t.Errorf("Error marshaling UUID: %s", err)
}
if !bytes.Equal(b1, b2) {
t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
}
}
func TestUnmarshalText(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
u1 := UUID{}
err := u1.UnmarshalText(b1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
b2 := []byte("")
u2 := UUID{}
err = u2.UnmarshalText(b2)
if err == nil {
t.Errorf("Should return error trying to unmarshal from empty string")
}
}
2015-10-28 11:07:30 +03:00
func TestValue(t *testing.T) {
u, err := FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
if err != nil {
t.Errorf("Error parsing UUID from string: %s", err)
}
val, err := u.Value()
if err != nil {
t.Errorf("Error getting UUID value: %s", err)
}
if val != u.String() {
t.Errorf("Wrong value returned, should be equal: %s and %s", val, u)
}
}
2015-02-05 07:20:22 +03:00
func TestScanBinary(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
u1 := UUID{}
err := u1.Scan(b1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
b2 := []byte{}
u2 := UUID{}
err = u2.Scan(b2)
if err == nil {
t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
}
}
func TestScanString(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
u1 := UUID{}
err := u1.Scan(s1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
s2 := ""
u2 := UUID{}
err = u2.Scan(s2)
if err == nil {
t.Errorf("Should return error trying to unmarshal from empty string")
}
}
func TestScanText(t *testing.T) {
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
u1 := UUID{}
err := u1.Scan(b1)
if err != nil {
t.Errorf("Error unmarshaling UUID: %s", err)
}
if !Equal(u, u1) {
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
}
b2 := []byte("")
u2 := UUID{}
err = u2.Scan(b2)
if err == nil {
t.Errorf("Should return error trying to unmarshal from empty string")
}
}
func TestScanUnsupported(t *testing.T) {
u := UUID{}
err := u.Scan(true)
if err == nil {
t.Errorf("Should return error trying to unmarshal from bool")
}
}
2013-06-18 18:58:10 +04:00
func TestNewV1(t *testing.T) {
u := NewV1()
2013-06-18 18:58:10 +04:00
if u.Version() != 1 {
t.Errorf("UUIDv1 generated with incorrect version: %d", u.Version())
}
2013-07-23 18:08:05 +04:00
if u.Variant() != VariantRFC4122 {
2013-06-18 18:58:10 +04:00
t.Errorf("UUIDv1 generated with incorrect variant: %d", u.Variant())
}
2013-06-18 21:00:58 +04:00
u1 := NewV1()
u2 := NewV1()
2013-06-18 21:00:58 +04:00
2013-07-23 18:08:05 +04:00
if Equal(u1, u2) {
2013-06-18 21:00:58 +04:00
t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u1, u2)
}
2013-07-29 17:07:35 +04:00
oldFunc := epochFunc
epochFunc = func() uint64 { return 0 }
u3 := NewV1()
u4 := NewV1()
2013-07-23 18:08:05 +04:00
if Equal(u3, u4) {
t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u3, u4)
}
2013-07-29 17:07:35 +04:00
epochFunc = oldFunc
2013-06-18 18:58:10 +04:00
}
func TestNewV2(t *testing.T) {
u1 := NewV2(DomainPerson)
2013-07-09 17:02:40 +04:00
if u1.Version() != 2 {
t.Errorf("UUIDv2 generated with incorrect version: %d", u1.Version())
}
2013-07-23 18:08:05 +04:00
if u1.Variant() != VariantRFC4122 {
2013-07-09 17:02:40 +04:00
t.Errorf("UUIDv2 generated with incorrect variant: %d", u1.Variant())
}
u2 := NewV2(DomainGroup)
2013-07-09 17:02:40 +04:00
if u2.Version() != 2 {
t.Errorf("UUIDv2 generated with incorrect version: %d", u2.Version())
}
2013-07-23 18:08:05 +04:00
if u2.Variant() != VariantRFC4122 {
2013-07-09 17:02:40 +04:00
t.Errorf("UUIDv2 generated with incorrect variant: %d", u2.Variant())
}
}
2013-06-18 17:23:53 +04:00
func TestNewV3(t *testing.T) {
u := NewV3(NamespaceDNS, "www.example.com")
2013-06-18 17:23:53 +04:00
if u.Version() != 3 {
t.Errorf("UUIDv3 generated with incorrect version: %d", u.Version())
}
2013-07-23 18:08:05 +04:00
if u.Variant() != VariantRFC4122 {
2013-06-18 17:23:53 +04:00
t.Errorf("UUIDv3 generated with incorrect variant: %d", u.Variant())
}
2013-06-18 20:14:33 +04:00
if u.String() != "5df41881-3aed-3515-88a7-2f4a814cf09e" {
t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
}
u = NewV3(NamespaceDNS, "python.org")
2013-06-18 20:14:33 +04:00
if u.String() != "6fa459ea-ee8a-3ca4-894e-db77e160355e" {
t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
}
2013-06-18 21:00:58 +04:00
u1 := NewV3(NamespaceDNS, "golang.org")
u2 := NewV3(NamespaceDNS, "golang.org")
2013-07-23 18:08:05 +04:00
if !Equal(u1, u2) {
2013-06-18 21:00:58 +04:00
t.Errorf("UUIDv3 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
}
u3 := NewV3(NamespaceDNS, "example.com")
2013-07-23 18:08:05 +04:00
if Equal(u1, u3) {
2013-06-18 21:00:58 +04:00
t.Errorf("UUIDv3 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
}
u4 := NewV3(NamespaceURL, "golang.org")
2013-07-23 18:08:05 +04:00
if Equal(u1, u4) {
2013-06-18 21:00:58 +04:00
t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
}
2013-06-18 17:23:53 +04:00
}
2013-06-18 13:50:18 +04:00
func TestNewV4(t *testing.T) {
u := NewV4()
2013-06-18 13:50:18 +04:00
if u.Version() != 4 {
t.Errorf("UUIDv4 generated with incorrect version: %d", u.Version())
}
2013-06-18 14:51:58 +04:00
2013-07-23 18:08:05 +04:00
if u.Variant() != VariantRFC4122 {
2013-06-18 14:51:58 +04:00
t.Errorf("UUIDv4 generated with incorrect variant: %d", u.Variant())
}
2013-06-18 13:50:18 +04:00
}
2013-06-18 17:23:53 +04:00
func TestNewV5(t *testing.T) {
u := NewV5(NamespaceDNS, "www.example.com")
2013-06-18 17:23:53 +04:00
if u.Version() != 5 {
t.Errorf("UUIDv5 generated with incorrect version: %d", u.Version())
}
2013-07-23 18:08:05 +04:00
if u.Variant() != VariantRFC4122 {
2013-06-18 17:23:53 +04:00
t.Errorf("UUIDv5 generated with incorrect variant: %d", u.Variant())
}
2013-06-18 20:14:33 +04:00
u = NewV5(NamespaceDNS, "python.org")
2013-06-18 20:14:33 +04:00
if u.String() != "886313e1-3b8a-5372-9b90-0c9aee199e5d" {
t.Errorf("UUIDv5 generated incorrectly: %s", u.String())
}
2013-06-18 21:00:58 +04:00
u1 := NewV5(NamespaceDNS, "golang.org")
u2 := NewV5(NamespaceDNS, "golang.org")
2013-07-23 18:08:05 +04:00
if !Equal(u1, u2) {
2013-06-18 21:00:58 +04:00
t.Errorf("UUIDv5 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
}
u3 := NewV5(NamespaceDNS, "example.com")
2013-07-23 18:08:05 +04:00
if Equal(u1, u3) {
2013-06-18 21:00:58 +04:00
t.Errorf("UUIDv5 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
}
u4 := NewV5(NamespaceURL, "golang.org")
2013-07-23 18:08:05 +04:00
if Equal(u1, u4) {
2013-06-18 21:00:58 +04:00
t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
}
2013-06-18 17:23:53 +04:00
}