1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 10:45:22 +00:00
v2fly/common/uuid/uuid_test.go

83 lines
1.7 KiB
Go
Raw Normal View History

2015-12-12 10:32:40 +00:00
package uuid_test
import (
"testing"
2018-11-15 10:17:20 +00:00
"github.com/google/go-cmp/cmp"
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common"
. "github.com/v2fly/v2ray-core/v4/common/uuid"
2015-12-12 10:32:40 +00:00
)
2015-12-12 12:11:49 +00:00
func TestParseBytes(t *testing.T) {
str := "2418d087-648d-4990-86e8-19dca1d006d3"
bytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
uuid, err := ParseBytes(bytes)
2018-07-13 12:36:09 +00:00
common.Must(err)
2018-11-15 10:17:20 +00:00
if diff := cmp.Diff(uuid.String(), str); diff != "" {
t.Error(diff)
2018-07-13 12:36:09 +00:00
}
2016-01-21 12:11:23 +00:00
2016-04-25 22:46:04 +00:00
_, err = ParseBytes([]byte{1, 3, 2, 4})
2018-07-13 12:36:09 +00:00
if err == nil {
t.Fatal("Expect error but nil")
}
2015-12-12 12:11:49 +00:00
}
2015-12-12 10:32:40 +00:00
func TestParseString(t *testing.T) {
str := "2418d087-648d-4990-86e8-19dca1d006d3"
expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
uuid, err := ParseString(str)
2018-07-13 12:36:09 +00:00
common.Must(err)
2019-01-07 10:37:06 +00:00
if r := cmp.Diff(expectedBytes, uuid.Bytes()); r != "" {
2019-01-06 23:12:04 +00:00
t.Fatal(r)
2018-07-13 12:36:09 +00:00
}
_, err = ParseString("2418d087")
if err == nil {
t.Fatal("Expect error but nil")
}
_, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3")
if err == nil {
t.Fatal("Expect error but nil")
}
2015-12-12 10:32:40 +00:00
}
func TestNewUUID(t *testing.T) {
uuid := New()
uuid2, err := ParseString(uuid.String())
2019-02-02 21:19:30 +00:00
common.Must(err)
2019-02-09 14:46:48 +00:00
if uuid.String() != uuid2.String() {
t.Error("uuid string: ", uuid.String(), " != ", uuid2.String())
}
if r := cmp.Diff(uuid.Bytes(), uuid2.Bytes()); r != "" {
t.Error(r)
}
2015-12-12 10:32:40 +00:00
}
func TestRandom(t *testing.T) {
uuid := New()
uuid2 := New()
2019-02-09 14:46:48 +00:00
if uuid.String() == uuid2.String() {
t.Error("duplicated uuid")
}
2015-12-12 10:32:40 +00:00
}
func TestEquals(t *testing.T) {
2018-11-13 08:51:55 +00:00
var uuid *UUID
var uuid2 *UUID
2019-02-09 14:46:48 +00:00
if !uuid.Equals(uuid2) {
t.Error("empty uuid should equal")
}
2018-01-18 22:25:48 +00:00
uuid3 := New()
2019-02-09 14:46:48 +00:00
if uuid.Equals(&uuid3) {
t.Error("nil uuid equals non-nil uuid")
}
}