github.com/eagleql/xray-core@v1.4.4/common/uuid/uuid_test.go (about) 1 package uuid_test 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 8 "github.com/eagleql/xray-core/common" 9 . "github.com/eagleql/xray-core/common/uuid" 10 ) 11 12 func TestParseBytes(t *testing.T) { 13 str := "2418d087-648d-4990-86e8-19dca1d006d3" 14 bytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3} 15 16 uuid, err := ParseBytes(bytes) 17 common.Must(err) 18 if diff := cmp.Diff(uuid.String(), str); diff != "" { 19 t.Error(diff) 20 } 21 22 _, err = ParseBytes([]byte{1, 3, 2, 4}) 23 if err == nil { 24 t.Fatal("Expect error but nil") 25 } 26 } 27 28 func TestParseString(t *testing.T) { 29 str := "2418d087-648d-4990-86e8-19dca1d006d3" 30 expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3} 31 32 uuid, err := ParseString(str) 33 common.Must(err) 34 if r := cmp.Diff(expectedBytes, uuid.Bytes()); r != "" { 35 t.Fatal(r) 36 } 37 38 u0, _ := ParseString("example") 39 u5, _ := ParseString("feb54431-301b-52bb-a6dd-e1e93e81bb9e") 40 if r := cmp.Diff(u0, u5); r != "" { 41 t.Fatal(r) 42 } 43 44 _, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3") 45 if err == nil { 46 t.Fatal("Expect error but nil") 47 } 48 } 49 50 func TestNewUUID(t *testing.T) { 51 uuid := New() 52 uuid2, err := ParseString(uuid.String()) 53 54 common.Must(err) 55 if uuid.String() != uuid2.String() { 56 t.Error("uuid string: ", uuid.String(), " != ", uuid2.String()) 57 } 58 if r := cmp.Diff(uuid.Bytes(), uuid2.Bytes()); r != "" { 59 t.Error(r) 60 } 61 } 62 63 func TestRandom(t *testing.T) { 64 uuid := New() 65 uuid2 := New() 66 67 if uuid.String() == uuid2.String() { 68 t.Error("duplicated uuid") 69 } 70 } 71 72 func TestEquals(t *testing.T) { 73 var uuid *UUID 74 var uuid2 *UUID 75 if !uuid.Equals(uuid2) { 76 t.Error("empty uuid should equal") 77 } 78 79 uuid3 := New() 80 if uuid.Equals(&uuid3) { 81 t.Error("nil uuid equals non-nil uuid") 82 } 83 }