uuid_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bytes"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. func TestUUIDNil(t *testing.T) {
  12. var uuid UUID
  13. want, got := "00000000-0000-0000-0000-000000000000", uuid.String()
  14. if want != got {
  15. t.Fatalf("TestNil: expected %q got %q", want, got)
  16. }
  17. }
  18. var testsUUID = []struct {
  19. input string
  20. variant int
  21. version int
  22. }{
  23. {"b4f00409-cef8-4822-802c-deb20704c365", VariantIETF, 4},
  24. {"B4F00409-CEF8-4822-802C-DEB20704C365", VariantIETF, 4}, //Use capital letters
  25. {"f81d4fae-7dec-11d0-a765-00a0c91e6bf6", VariantIETF, 1},
  26. {"00000000-7dec-11d0-a765-00a0c91e6bf6", VariantIETF, 1},
  27. {"3051a8d7-aea7-1801-e0bf-bc539dd60cf3", VariantFuture, 1},
  28. {"3051a8d7-aea7-2801-e0bf-bc539dd60cf3", VariantFuture, 2},
  29. {"3051a8d7-aea7-3801-e0bf-bc539dd60cf3", VariantFuture, 3},
  30. {"3051a8d7-aea7-4801-e0bf-bc539dd60cf3", VariantFuture, 4},
  31. {"3051a8d7-aea7-3801-e0bf-bc539dd60cf3", VariantFuture, 5},
  32. {"d0e817e1-e4b1-1801-3fe6-b4b60ccecf9d", VariantNCSCompat, 0},
  33. {"d0e817e1-e4b1-1801-bfe6-b4b60ccecf9d", VariantIETF, 1},
  34. {"d0e817e1-e4b1-1801-dfe6-b4b60ccecf9d", VariantMicrosoft, 0},
  35. {"d0e817e1-e4b1-1801-ffe6-b4b60ccecf9d", VariantFuture, 0},
  36. }
  37. func TestPredefinedUUID(t *testing.T) {
  38. for i := range testsUUID {
  39. uuid, err := ParseUUID(testsUUID[i].input)
  40. if err != nil {
  41. t.Errorf("ParseUUID #%d: %v", i, err)
  42. continue
  43. }
  44. if str := uuid.String(); str != strings.ToLower(testsUUID[i].input) {
  45. t.Errorf("String #%d: expected %q got %q", i, testsUUID[i].input, str)
  46. continue
  47. }
  48. if variant := uuid.Variant(); variant != testsUUID[i].variant {
  49. t.Errorf("Variant #%d: expected %d got %d", i, testsUUID[i].variant, variant)
  50. }
  51. if testsUUID[i].variant == VariantIETF {
  52. if version := uuid.Version(); version != testsUUID[i].version {
  53. t.Errorf("Version #%d: expected %d got %d", i, testsUUID[i].version, version)
  54. }
  55. }
  56. json, err := uuid.MarshalJSON()
  57. if err != nil {
  58. t.Errorf("MarshalJSON #%d: %v", i, err)
  59. }
  60. expectedJson := `"` + strings.ToLower(testsUUID[i].input) + `"`
  61. if string(json) != expectedJson {
  62. t.Errorf("MarshalJSON #%d: expected %v got %v", i, expectedJson, string(json))
  63. }
  64. var unmarshaled UUID
  65. err = unmarshaled.UnmarshalJSON(json)
  66. if err != nil {
  67. t.Errorf("UnmarshalJSON #%d: %v", i, err)
  68. }
  69. if unmarshaled != uuid {
  70. t.Errorf("UnmarshalJSON #%d: expected %v got %v", i, uuid, unmarshaled)
  71. }
  72. }
  73. }
  74. func TestInvalidUUIDCharacter(t *testing.T) {
  75. _, err := ParseUUID("z4f00409-cef8-4822-802c-deb20704c365")
  76. if err == nil || !strings.Contains(err.Error(), "invalid UUID") {
  77. t.Fatalf("expected invalid UUID error, got '%v' ", err)
  78. }
  79. }
  80. func TestInvalidUUIDLength(t *testing.T) {
  81. _, err := ParseUUID("4f00")
  82. if err == nil || !strings.Contains(err.Error(), "invalid UUID") {
  83. t.Fatalf("expected invalid UUID error, got '%v' ", err)
  84. }
  85. _, err = UUIDFromBytes(TimeUUID().Bytes()[:15])
  86. if err == nil || err.Error() != "UUIDs must be exactly 16 bytes long" {
  87. t.Fatalf("expected error '%v', got '%v'", "UUIDs must be exactly 16 bytes long", err)
  88. }
  89. }
  90. func TestRandomUUID(t *testing.T) {
  91. for i := 0; i < 20; i++ {
  92. uuid, err := RandomUUID()
  93. if err != nil {
  94. t.Errorf("RandomUUID: %v", err)
  95. }
  96. if variant := uuid.Variant(); variant != VariantIETF {
  97. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  98. }
  99. if version := uuid.Version(); version != 4 {
  100. t.Errorf("wrong version. expected %d got %d", 4, version)
  101. }
  102. }
  103. }
  104. func TestRandomUUIDInvalidAPICalls(t *testing.T) {
  105. uuid, err := RandomUUID()
  106. if err != nil {
  107. t.Fatalf("unexpected error %v", err)
  108. }
  109. if node := uuid.Node(); node != nil {
  110. t.Fatalf("expected nil, got %v", node)
  111. }
  112. if stamp := uuid.Timestamp(); stamp != 0 {
  113. t.Fatalf("expceted 0, got %v", stamp)
  114. }
  115. zeroT := time.Time{}
  116. if to := uuid.Time(); to != zeroT {
  117. t.Fatalf("expected %v, got %v", zeroT, to)
  118. }
  119. }
  120. func TestUUIDFromTime(t *testing.T) {
  121. date := time.Date(1982, 5, 5, 12, 34, 56, 0, time.UTC)
  122. uuid := UUIDFromTime(date)
  123. if uuid.Time() != date {
  124. t.Errorf("embedded time incorrect. Expected %v got %v", date, uuid.Time())
  125. }
  126. }
  127. func TestParseUUID(t *testing.T) {
  128. uuid, _ := ParseUUID("486f3a88-775b-11e3-ae07-d231feb1dc81")
  129. if uuid.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
  130. t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", uuid.Time())
  131. }
  132. }
  133. func TestTimeUUID(t *testing.T) {
  134. var node []byte
  135. timestamp := int64(0)
  136. for i := 0; i < 20; i++ {
  137. uuid := TimeUUID()
  138. if variant := uuid.Variant(); variant != VariantIETF {
  139. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  140. }
  141. if version := uuid.Version(); version != 1 {
  142. t.Errorf("wrong version. expected %d got %d", 1, version)
  143. }
  144. if n := uuid.Node(); !bytes.Equal(n, node) && i > 0 {
  145. t.Errorf("wrong node. expected %x, got %x", node, n)
  146. } else if i == 0 {
  147. node = n
  148. }
  149. ts := uuid.Timestamp()
  150. if ts < timestamp {
  151. t.Errorf("timestamps must grow")
  152. }
  153. timestamp = ts
  154. }
  155. }
  156. func TestUnmarshalJSON(t *testing.T) {
  157. var withHyphens, withoutHypens, tooLong UUID
  158. withHyphens.UnmarshalJSON([]byte(`"486f3a88-775b-11e3-ae07-d231feb1dc81"`))
  159. if withHyphens.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
  160. t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", withHyphens.Time())
  161. }
  162. withoutHypens.UnmarshalJSON([]byte(`"486f3a88775b11e3ae07d231feb1dc81"`))
  163. if withoutHypens.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
  164. t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", withoutHypens.Time())
  165. }
  166. err := tooLong.UnmarshalJSON([]byte(`"486f3a88-775b-11e3-ae07-d231feb1dc81486f3a88"`))
  167. if err == nil {
  168. t.Errorf("no error for invalid JSON UUID")
  169. }
  170. }