2013-07-09 14:35:21 +04:00
|
|
|
package uuid_test
|
2013-06-18 13:50:18 +04:00
|
|
|
|
|
|
|
import (
|
2013-07-09 14:35:21 +04:00
|
|
|
"github.com/satori/uuid"
|
2013-06-18 13:50:18 +04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-06-18 20:14:33 +04:00
|
|
|
func TestString(t *testing.T) {
|
2013-07-09 14:35:21 +04:00
|
|
|
if uuid.NamespaceDNS.String() != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
|
|
|
|
t.Errorf("Incorrect string representation for UUID: %s", uuid.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-09 14:35:21 +04:00
|
|
|
if !uuid.Equal(uuid.NamespaceDNS, uuid.NamespaceDNS) {
|
|
|
|
t.Errorf("Incorrect comparison of %s and %s", uuid.NamespaceDNS, uuid.NamespaceDNS)
|
2013-06-18 20:17:07 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
if uuid.Equal(uuid.NamespaceDNS, uuid.NamespaceURL) {
|
|
|
|
t.Errorf("Incorrect comparison of %s and %s", uuid.NamespaceDNS, uuid.NamespaceURL)
|
2013-06-18 20:17:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 13:50:18 +04:00
|
|
|
func TestVersion(t *testing.T) {
|
2013-07-09 14:35:21 +04:00
|
|
|
u := uuid.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-07-09 14:35:21 +04:00
|
|
|
u := new(uuid.UUID)
|
|
|
|
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-09 14:35:21 +04:00
|
|
|
u1 := uuid.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-09 14:35:21 +04:00
|
|
|
if u1.Variant() != uuid.VariantNCS {
|
|
|
|
t.Errorf("Incorrect variant for UUID variant %d: %d", uuid.VariantNCS, u1.Variant())
|
2013-06-18 14:51:58 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u2 := uuid.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-09 14:35:21 +04:00
|
|
|
if u2.Variant() != uuid.VariantRFC4122 {
|
|
|
|
t.Errorf("Incorrect variant for UUID variant %d: %d", uuid.VariantRFC4122, u2.Variant())
|
2013-06-18 14:51:58 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u3 := uuid.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-09 14:35:21 +04:00
|
|
|
if u3.Variant() != uuid.VariantMicrosoft {
|
|
|
|
t.Errorf("Incorrect variant for UUID variant %d: %d", uuid.VariantMicrosoft, u3.Variant())
|
2013-06-18 14:51:58 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u4 := uuid.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-09 14:35:21 +04:00
|
|
|
if u4.Variant() != uuid.VariantFuture {
|
|
|
|
t.Errorf("Incorrect variant for UUID variant %d: %d", uuid.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-09 14:35:21 +04:00
|
|
|
u := new(uuid.UUID)
|
|
|
|
u.SetVariant()
|
2013-06-18 14:51:58 +04:00
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
if u.Variant() != uuid.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-06-18 18:58:10 +04:00
|
|
|
func TestNewV1(t *testing.T) {
|
2013-07-09 14:35:21 +04:00
|
|
|
u, err := uuid.NewV1()
|
2013-06-18 18:58:10 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("UUIDv1 generated with error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Version() != 1 {
|
|
|
|
t.Errorf("UUIDv1 generated with incorrect version: %d", u.Version())
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
if u.Variant() != uuid.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
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u1, _ := uuid.NewV1()
|
|
|
|
u2, _ := uuid.NewV1()
|
2013-06-18 21:00:58 +04:00
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
if uuid.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-09 17:02:26 +04:00
|
|
|
|
2013-07-09 17:10:32 +04:00
|
|
|
oldFunc := uuid.GetEpochFunc()
|
|
|
|
|
2013-07-17 15:03:51 +04:00
|
|
|
uuid.SetEpochFunc(func() uint64 { return 0 })
|
2013-07-09 17:02:26 +04:00
|
|
|
u3, _ := uuid.NewV1()
|
|
|
|
u4, _ := uuid.NewV1()
|
|
|
|
|
|
|
|
if uuid.Equal(u3, u4) {
|
|
|
|
t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u3, u4)
|
|
|
|
}
|
2013-07-09 17:10:32 +04:00
|
|
|
|
2013-07-09 17:02:26 +04:00
|
|
|
uuid.SetEpochFunc(oldFunc)
|
2013-06-18 18:58:10 +04:00
|
|
|
}
|
|
|
|
|
2013-06-19 01:48:27 +04:00
|
|
|
func TestNewV2(t *testing.T) {
|
2013-07-09 17:02:40 +04:00
|
|
|
u1, err := uuid.NewV2(uuid.DomainPerson)
|
2013-06-19 01:48:27 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("UUIDv2 generated with error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2013-07-09 17:02:40 +04:00
|
|
|
if u1.Version() != 2 {
|
|
|
|
t.Errorf("UUIDv2 generated with incorrect version: %d", u1.Version())
|
2013-06-19 01:48:27 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 17:02:40 +04:00
|
|
|
if u1.Variant() != uuid.VariantRFC4122 {
|
|
|
|
t.Errorf("UUIDv2 generated with incorrect variant: %d", u1.Variant())
|
|
|
|
}
|
|
|
|
|
|
|
|
u2, err := uuid.NewV2(uuid.DomainGroup)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("UUIDv2 generated with error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if u2.Version() != 2 {
|
|
|
|
t.Errorf("UUIDv2 generated with incorrect version: %d", u2.Version())
|
|
|
|
}
|
|
|
|
|
|
|
|
if u2.Variant() != uuid.VariantRFC4122 {
|
|
|
|
t.Errorf("UUIDv2 generated with incorrect variant: %d", u2.Variant())
|
|
|
|
}
|
|
|
|
|
2013-07-17 16:06:54 +04:00
|
|
|
u3, err := uuid.NewV2(uuid.DomainOrg)
|
2013-07-09 17:02:40 +04:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("UUIDv2 should be generated with error, otherwise error is: %s", err)
|
2013-06-19 01:48:27 +04:00
|
|
|
}
|
2013-07-17 16:06:54 +04:00
|
|
|
|
|
|
|
if u3 != nil {
|
|
|
|
t.Errorf("UUIDv2 should be nil in case of error, otherwise: %s", u3)
|
|
|
|
}
|
2013-06-19 01:48:27 +04:00
|
|
|
}
|
|
|
|
|
2013-06-18 17:23:53 +04:00
|
|
|
func TestNewV3(t *testing.T) {
|
2013-07-09 14:35:21 +04:00
|
|
|
u, err := uuid.NewV3(uuid.NamespaceDNS, "www.example.com")
|
2013-06-18 17:23:53 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("UUIDv3 generated with error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Version() != 3 {
|
|
|
|
t.Errorf("UUIDv3 generated with incorrect version: %d", u.Version())
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
if u.Variant() != uuid.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())
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u, _ = uuid.NewV3(uuid.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
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u1, _ := uuid.NewV3(uuid.NamespaceDNS, "golang.org")
|
|
|
|
u2, _ := uuid.NewV3(uuid.NamespaceDNS, "golang.org")
|
|
|
|
if !uuid.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)
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u3, _ := uuid.NewV3(uuid.NamespaceDNS, "example.com")
|
|
|
|
if uuid.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)
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u4, _ := uuid.NewV3(uuid.NamespaceURL, "golang.org")
|
|
|
|
if uuid.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) {
|
2013-07-09 14:35:21 +04:00
|
|
|
u, err := uuid.NewV4()
|
2013-06-18 13:50:18 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("UUIDv4 generated with error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Version() != 4 {
|
|
|
|
t.Errorf("UUIDv4 generated with incorrect version: %d", u.Version())
|
|
|
|
}
|
2013-06-18 14:51:58 +04:00
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
if u.Variant() != uuid.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) {
|
2013-07-09 14:35:21 +04:00
|
|
|
u, err := uuid.NewV5(uuid.NamespaceDNS, "www.example.com")
|
2013-06-18 17:23:53 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("UUIDv5 generated with error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Version() != 5 {
|
|
|
|
t.Errorf("UUIDv5 generated with incorrect version: %d", u.Version())
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
if u.Variant() != uuid.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
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u, _ = uuid.NewV5(uuid.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
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u1, _ := uuid.NewV5(uuid.NamespaceDNS, "golang.org")
|
|
|
|
u2, _ := uuid.NewV5(uuid.NamespaceDNS, "golang.org")
|
|
|
|
if !uuid.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)
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u3, _ := uuid.NewV5(uuid.NamespaceDNS, "example.com")
|
|
|
|
if uuid.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)
|
|
|
|
}
|
|
|
|
|
2013-07-09 14:35:21 +04:00
|
|
|
u4, _ := uuid.NewV5(uuid.NamespaceURL, "golang.org")
|
|
|
|
if uuid.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
|
|
|
}
|