PageRenderTime 124ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/src/github.com/twinj/uuid/rfc4122_test.go

https://gitlab.com/github-cloud-corp/amazon-ssm-agent
Go | 209 lines | 173 code | 22 blank | 14 comment | 46 complexity | a28c6ad057089882ca505ac0b5a8ba10 MD5 | raw file
  1. package uuid
  2. /****************
  3. * Date: 16/02/14
  4. * Time: 11:29 AM
  5. ***************/
  6. import (
  7. "fmt"
  8. "net/url"
  9. "testing"
  10. )
  11. var (
  12. goLang Name = "https://google.com/golang.org?q=golang"
  13. )
  14. const (
  15. generate = 1000000
  16. )
  17. func TestUUID_NewV1(t *testing.T) {
  18. u := NewV1()
  19. if u.Version() != 1 {
  20. t.Errorf("Expected correct version %d, but got %d", 1, u.Version())
  21. }
  22. if u.Variant() != ReservedRFC4122 {
  23. t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
  24. }
  25. if !parseUUIDRegex.MatchString(u.String()) {
  26. t.Errorf("Expected string representation to be valid, given: %s", u.String())
  27. }
  28. }
  29. func TestUUID_NewV1Bulk(t *testing.T) {
  30. for i := 0; i < generate; i++ {
  31. NewV1()
  32. }
  33. }
  34. // Tests NewV3
  35. func TestUUID_NewV3(t *testing.T) {
  36. u := NewV3(NamespaceURL, goLang)
  37. if u.Version() != 3 {
  38. t.Errorf("Expected correct version %d, but got %d", 3, u.Version())
  39. }
  40. if u.Variant() != ReservedRFC4122 {
  41. t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
  42. }
  43. if !parseUUIDRegex.MatchString(u.String()) {
  44. t.Errorf("Expected string representation to be valid, given: %s", u.String())
  45. }
  46. ur, _ := url.Parse(string(goLang))
  47. // Same NS same name MUST be equal
  48. u2 := NewV3(NamespaceURL, ur)
  49. if !Equal(u2, u) {
  50. t.Errorf("Expected UUIDs generated with same namespace and name to equal but got: %s and %s", u2, u)
  51. }
  52. // Different NS same name MUST NOT be equal
  53. u3 := NewV3(NamespaceDNS, ur)
  54. if Equal(u3, u) {
  55. t.Errorf("Expected UUIDs generated with different namespace and same name to be different but got: %s and %s", u3, u)
  56. }
  57. // Same NS different name MUST NOT be equal
  58. u4 := NewV3(NamespaceURL, u)
  59. if Equal(u4, u) {
  60. t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", u4, u)
  61. }
  62. ids := []UUID{
  63. u, u2, u3, u4,
  64. }
  65. for j, id := range ids {
  66. i := NewV3(NamespaceURL, NewName(string(j), id))
  67. if Equal(id, i) {
  68. t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", id, i)
  69. }
  70. }
  71. }
  72. func TestUUID_NewV3Bulk(t *testing.T) {
  73. for i := 0; i < generate; i++ {
  74. NewV3(NamespaceDNS, goLang)
  75. }
  76. }
  77. func TestUUID_NewV4(t *testing.T) {
  78. u := NewV4()
  79. if u.Version() != 4 {
  80. t.Errorf("Expected correct version %d, but got %d", 4, u.Version())
  81. }
  82. if u.Variant() != ReservedRFC4122 {
  83. t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
  84. }
  85. if !parseUUIDRegex.MatchString(u.String()) {
  86. t.Errorf("Expected string representation to be valid, given: %s", u.String())
  87. }
  88. }
  89. func TestUUID_NewV4Bulk(t *testing.T) {
  90. for i := 0; i < generate; i++ {
  91. NewV4()
  92. }
  93. }
  94. // Tests NewV5
  95. func TestUUID_NewV5(t *testing.T) {
  96. u := NewV5(NamespaceURL, goLang)
  97. if u.Version() != 5 {
  98. t.Errorf("Expected correct version %d, but got %d", 5, u.Version())
  99. }
  100. if u.Variant() != ReservedRFC4122 {
  101. t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
  102. }
  103. if !parseUUIDRegex.MatchString(u.String()) {
  104. t.Errorf("Expected string representation to be valid, given: %s", u.String())
  105. }
  106. ur, _ := url.Parse(string(goLang))
  107. // Same NS same name MUST be equal
  108. u2 := NewV5(NamespaceURL, ur)
  109. if !Equal(u2, u) {
  110. t.Errorf("Expected UUIDs generated with same namespace and name to equal but got: %s and %s", u2, u)
  111. }
  112. // Different NS same name MUST NOT be equal
  113. u3 := NewV5(NamespaceDNS, ur)
  114. if Equal(u3, u) {
  115. t.Errorf("Expected UUIDs generated with different namespace and same name to be different but got: %s and %s", u3, u)
  116. }
  117. // Same NS different name MUST NOT be equal
  118. u4 := NewV5(NamespaceURL, u)
  119. if Equal(u4, u) {
  120. t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", u4, u)
  121. }
  122. ids := []UUID{
  123. u, u2, u3, u4,
  124. }
  125. for j, id := range ids {
  126. i := NewV5(NamespaceURL, NewName(string(j), id))
  127. if Equal(id, i) {
  128. t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", id, i)
  129. }
  130. }
  131. }
  132. func TestUUID_NewV5Bulk(t *testing.T) {
  133. for i := 0; i < generate; i++ {
  134. NewV5(NamespaceDNS, goLang)
  135. }
  136. }
  137. // A small test to test uniqueness across all UUIDs created
  138. func TestUUID_EachIsUnique(t *testing.T) {
  139. s := 1000
  140. ids := make([]UUID, s)
  141. for i := 0; i < s; i++ {
  142. u := NewV1()
  143. ids[i] = u
  144. for j := 0; j < i; j++ {
  145. if Equal(ids[j], u) {
  146. t.Error("Should not create the same V1 UUID", u, ids[j])
  147. }
  148. }
  149. }
  150. ids = make([]UUID, s)
  151. for i := 0; i < s; i++ {
  152. u := NewV3(NamespaceDNS, NewName(string(i), Name(goLang)))
  153. ids[i] = u
  154. for j := 0; j < i; j++ {
  155. if Equal(ids[j], u) {
  156. t.Error("Should not create the same V3 UUID", u, ids[j])
  157. }
  158. }
  159. }
  160. ids = make([]UUID, s)
  161. for i := 0; i < s; i++ {
  162. u := NewV4()
  163. ids[i] = u
  164. for j := 0; j < i; j++ {
  165. if Equal(ids[j], u) {
  166. t.Error("Should not create the same V4 UUID", u, ids[j])
  167. }
  168. }
  169. }
  170. ids = make([]UUID, s)
  171. for i := 0; i < s; i++ {
  172. u := NewV5(NamespaceDNS, NewName(string(i), Name(goLang)))
  173. ids[i] = u
  174. for j := 0; j < i; j++ {
  175. if Equal(ids[j], u) {
  176. t.Error("Should not create the same V5 UUID", u, ids[j])
  177. }
  178. }
  179. }
  180. }
  181. // Not really a test but used for visual verification of the defaults
  182. func UUID_NamespaceDefaults() {
  183. fmt.Println(NamespaceDNS)
  184. fmt.Println(NamespaceURL)
  185. fmt.Println(NamespaceOID)
  186. fmt.Println(NamespaceX500)
  187. }