1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/uuid/uuid_test.go

83 lines
1.7 KiB
Go
Raw Normal View History

2015-12-12 05:32:40 -05:00
package uuid_test
import (
"testing"
2018-11-15 05:17:20 -05:00
"github.com/google/go-cmp/cmp"
"github.com/v2fly/v2ray-core/v5/common"
. "github.com/v2fly/v2ray-core/v5/common/uuid"
2015-12-12 05:32:40 -05:00
)
2015-12-12 07:11:49 -05: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 08:36:09 -04:00
common.Must(err)
2018-11-15 05:17:20 -05:00
if diff := cmp.Diff(uuid.String(), str); diff != "" {
t.Error(diff)
2018-07-13 08:36:09 -04:00
}
2016-01-21 07:11:23 -05:00
2016-04-25 18:46:04 -04:00
_, err = ParseBytes([]byte{1, 3, 2, 4})
2018-07-13 08:36:09 -04:00
if err == nil {
t.Fatal("Expect error but nil")
}
2015-12-12 07:11:49 -05:00
}
2015-12-12 05:32:40 -05: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 08:36:09 -04:00
common.Must(err)
2019-01-07 05:37:06 -05:00
if r := cmp.Diff(expectedBytes, uuid.Bytes()); r != "" {
2019-01-06 18:12:04 -05:00
t.Fatal(r)
2018-07-13 08:36:09 -04: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 05:32:40 -05:00
}
func TestNewUUID(t *testing.T) {
uuid := New()
uuid2, err := ParseString(uuid.String())
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-09 09:46:48 -05: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 05:32:40 -05:00
}
func TestRandom(t *testing.T) {
uuid := New()
uuid2 := New()
2019-02-09 09:46:48 -05:00
if uuid.String() == uuid2.String() {
t.Error("duplicated uuid")
}
2015-12-12 05:32:40 -05:00
}
func TestEquals(t *testing.T) {
2018-11-13 03:51:55 -05:00
var uuid *UUID
var uuid2 *UUID
2019-02-09 09:46:48 -05:00
if !uuid.Equals(uuid2) {
t.Error("empty uuid should equal")
}
2018-01-18 17:25:48 -05:00
uuid3 := New()
2019-02-09 09:46:48 -05:00
if uuid.Equals(&uuid3) {
t.Error("nil uuid equals non-nil uuid")
}
}