uuid_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package uuid_test
  2. import (
  3. "testing"
  4. . "github.com/v2ray/v2ray-core/common/uuid"
  5. v2testing "github.com/v2ray/v2ray-core/testing"
  6. "github.com/v2ray/v2ray-core/testing/assert"
  7. )
  8. func TestParseBytes(t *testing.T) {
  9. v2testing.Current(t)
  10. str := "2418d087-648d-4990-86e8-19dca1d006d3"
  11. bytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
  12. uuid, err := ParseBytes(bytes)
  13. assert.Error(err).IsNil()
  14. assert.String(uuid).Equals(str)
  15. }
  16. func TestParseString(t *testing.T) {
  17. v2testing.Current(t)
  18. str := "2418d087-648d-4990-86e8-19dca1d006d3"
  19. expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
  20. uuid, err := ParseString(str)
  21. assert.Error(err).IsNil()
  22. assert.Bytes(uuid.Bytes()).Equals(expectedBytes)
  23. }
  24. func TestNewUUID(t *testing.T) {
  25. v2testing.Current(t)
  26. uuid := New()
  27. uuid2, err := ParseString(uuid.String())
  28. assert.Error(err).IsNil()
  29. assert.StringLiteral(uuid.String()).Equals(uuid2.String())
  30. assert.Bytes(uuid.Bytes()).Equals(uuid2.Bytes())
  31. }
  32. func TestRandom(t *testing.T) {
  33. v2testing.Current(t)
  34. uuid := New()
  35. uuid2 := New()
  36. assert.StringLiteral(uuid.String()).NotEquals(uuid2.String())
  37. assert.Bytes(uuid.Bytes()).NotEquals(uuid2.Bytes())
  38. }