github.com/v2fly/v2ray-core/v4@v4.45.2/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/v2fly/v2ray-core/v4/common" 9 . "github.com/v2fly/v2ray-core/v4/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 _, err = ParseString("2418d087") 39 if err == nil { 40 t.Fatal("Expect error but nil") 41 } 42 43 _, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3") 44 if err == nil { 45 t.Fatal("Expect error but nil") 46 } 47 } 48 49 func TestNewUUID(t *testing.T) { 50 uuid := New() 51 uuid2, err := ParseString(uuid.String()) 52 53 common.Must(err) 54 if uuid.String() != uuid2.String() { 55 t.Error("uuid string: ", uuid.String(), " != ", uuid2.String()) 56 } 57 if r := cmp.Diff(uuid.Bytes(), uuid2.Bytes()); r != "" { 58 t.Error(r) 59 } 60 } 61 62 func TestRandom(t *testing.T) { 63 uuid := New() 64 uuid2 := New() 65 66 if uuid.String() == uuid2.String() { 67 t.Error("duplicated uuid") 68 } 69 } 70 71 func TestEquals(t *testing.T) { 72 var uuid *UUID 73 var uuid2 *UUID 74 if !uuid.Equals(uuid2) { 75 t.Error("empty uuid should equal") 76 } 77 78 uuid3 := New() 79 if uuid.Equals(&uuid3) { 80 t.Error("nil uuid equals non-nil uuid") 81 } 82 }