This commit is contained in:
RPRX 2020-11-25 19:01:53 +08:00
parent 47d23e9972
commit c7f7c08ead
711 changed files with 82154 additions and 2 deletions

90
common/uuid/uuid.go Normal file
View file

@ -0,0 +1,90 @@
package uuid // import "github.com/xtls/xray-core/v1/common/uuid"
import (
"bytes"
"crypto/rand"
"encoding/hex"
"github.com/xtls/xray-core/v1/common"
"github.com/xtls/xray-core/v1/common/errors"
)
var (
byteGroups = []int{8, 4, 4, 4, 12}
)
type UUID [16]byte
// String returns the string representation of this UUID.
func (u *UUID) String() string {
bytes := u.Bytes()
result := hex.EncodeToString(bytes[0 : byteGroups[0]/2])
start := byteGroups[0] / 2
for i := 1; i < len(byteGroups); i++ {
nBytes := byteGroups[i] / 2
result += "-"
result += hex.EncodeToString(bytes[start : start+nBytes])
start += nBytes
}
return result
}
// Bytes returns the bytes representation of this UUID.
func (u *UUID) Bytes() []byte {
return u[:]
}
// Equals returns true if this UUID equals another UUID by value.
func (u *UUID) Equals(another *UUID) bool {
if u == nil && another == nil {
return true
}
if u == nil || another == nil {
return false
}
return bytes.Equal(u.Bytes(), another.Bytes())
}
// New creates a UUID with random value.
func New() UUID {
var uuid UUID
common.Must2(rand.Read(uuid.Bytes()))
return uuid
}
// ParseBytes converts a UUID in byte form to object.
func ParseBytes(b []byte) (UUID, error) {
var uuid UUID
if len(b) != 16 {
return uuid, errors.New("invalid UUID: ", b)
}
copy(uuid[:], b)
return uuid, nil
}
// ParseString converts a UUID in string form to object.
func ParseString(str string) (UUID, error) {
var uuid UUID
text := []byte(str)
if len(text) < 32 {
return uuid, errors.New("invalid UUID: ", str)
}
b := uuid.Bytes()
for _, byteGroup := range byteGroups {
if text[0] == '-' {
text = text[1:]
}
if _, err := hex.Decode(b[:byteGroup/2], text[:byteGroup]); err != nil {
return uuid, err
}
text = text[byteGroup:]
b = b[byteGroup/2:]
}
return uuid, nil
}

82
common/uuid/uuid_test.go Normal file
View file

@ -0,0 +1,82 @@
package uuid_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/xtls/xray-core/v1/common"
. "github.com/xtls/xray-core/v1/common/uuid"
)
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)
common.Must(err)
if diff := cmp.Diff(uuid.String(), str); diff != "" {
t.Error(diff)
}
_, err = ParseBytes([]byte{1, 3, 2, 4})
if err == nil {
t.Fatal("Expect error but nil")
}
}
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)
common.Must(err)
if r := cmp.Diff(expectedBytes, uuid.Bytes()); r != "" {
t.Fatal(r)
}
_, 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")
}
}
func TestNewUUID(t *testing.T) {
uuid := New()
uuid2, err := ParseString(uuid.String())
common.Must(err)
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)
}
}
func TestRandom(t *testing.T) {
uuid := New()
uuid2 := New()
if uuid.String() == uuid2.String() {
t.Error("duplicated uuid")
}
}
func TestEquals(t *testing.T) {
var uuid *UUID
var uuid2 *UUID
if !uuid.Equals(uuid2) {
t.Error("empty uuid should equal")
}
uuid3 := New()
if uuid.Equals(&uuid3) {
t.Error("nil uuid equals non-nil uuid")
}
}