uuid_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // Copyright (C) 2013 by Maxim Bublis <b@codemonkey.ru>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package uuid
  22. import (
  23. "bytes"
  24. "testing"
  25. )
  26. func TestBytes(t *testing.T) {
  27. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  28. bytes1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  29. if !bytes.Equal(u.Bytes(), bytes1) {
  30. t.Errorf("Incorrect bytes representation for UUID: %s", u)
  31. }
  32. }
  33. func TestString(t *testing.T) {
  34. if NamespaceDNS.String() != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
  35. t.Errorf("Incorrect string representation for UUID: %s", NamespaceDNS.String())
  36. }
  37. }
  38. func TestEqual(t *testing.T) {
  39. if !Equal(NamespaceDNS, NamespaceDNS) {
  40. t.Errorf("Incorrect comparison of %s and %s", NamespaceDNS, NamespaceDNS)
  41. }
  42. if Equal(NamespaceDNS, NamespaceURL) {
  43. t.Errorf("Incorrect comparison of %s and %s", NamespaceDNS, NamespaceURL)
  44. }
  45. }
  46. func TestOr(t *testing.T) {
  47. u1 := UUID{0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff}
  48. u2 := UUID{0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00}
  49. u := UUID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  50. if !Equal(u, Or(u1, u2)) {
  51. t.Errorf("Incorrect bitwise OR result %s", Or(u1, u2))
  52. }
  53. }
  54. func TestAnd(t *testing.T) {
  55. u1 := UUID{0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff}
  56. u2 := UUID{0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00}
  57. u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  58. if !Equal(u, And(u1, u2)) {
  59. t.Errorf("Incorrect bitwise AND result %s", And(u1, u2))
  60. }
  61. }
  62. func TestVersion(t *testing.T) {
  63. u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  64. if u.Version() != 1 {
  65. t.Errorf("Incorrect version for UUID: %d", u.Version())
  66. }
  67. }
  68. func TestSetVersion(t *testing.T) {
  69. u := UUID{}
  70. u.SetVersion(4)
  71. if u.Version() != 4 {
  72. t.Errorf("Incorrect version for UUID after u.setVersion(4): %d", u.Version())
  73. }
  74. }
  75. func TestVariant(t *testing.T) {
  76. u1 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  77. if u1.Variant() != VariantNCS {
  78. t.Errorf("Incorrect variant for UUID variant %d: %d", VariantNCS, u1.Variant())
  79. }
  80. u2 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  81. if u2.Variant() != VariantRFC4122 {
  82. t.Errorf("Incorrect variant for UUID variant %d: %d", VariantRFC4122, u2.Variant())
  83. }
  84. u3 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  85. if u3.Variant() != VariantMicrosoft {
  86. t.Errorf("Incorrect variant for UUID variant %d: %d", VariantMicrosoft, u3.Variant())
  87. }
  88. u4 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  89. if u4.Variant() != VariantFuture {
  90. t.Errorf("Incorrect variant for UUID variant %d: %d", VariantFuture, u4.Variant())
  91. }
  92. }
  93. func TestSetVariant(t *testing.T) {
  94. u := new(UUID)
  95. u.SetVariant()
  96. if u.Variant() != VariantRFC4122 {
  97. t.Errorf("Incorrect variant for UUID after u.setVariant(): %d", u.Variant())
  98. }
  99. }
  100. func TestFromBytes(t *testing.T) {
  101. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  102. b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  103. u1, err := FromBytes(b1)
  104. if err != nil {
  105. t.Errorf("Error parsing UUID from bytes: %s", err)
  106. }
  107. if !Equal(u, u1) {
  108. t.Errorf("UUIDs should be equal: %s and %s", u, u1)
  109. }
  110. b2 := []byte{}
  111. _, err = FromBytes(b2)
  112. if err == nil {
  113. t.Errorf("Should return error parsing from empty byte slice, got %s", err)
  114. }
  115. }
  116. func TestMarshalBinary(t *testing.T) {
  117. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  118. b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  119. b2, err := u.MarshalBinary()
  120. if err != nil {
  121. t.Errorf("Error marshaling UUID: %s", err)
  122. }
  123. if !bytes.Equal(b1, b2) {
  124. t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
  125. }
  126. }
  127. func TestUnmarshalBinary(t *testing.T) {
  128. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  129. b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  130. u1 := UUID{}
  131. err := u1.UnmarshalBinary(b1)
  132. if err != nil {
  133. t.Errorf("Error unmarshaling UUID: %s", err)
  134. }
  135. if !Equal(u, u1) {
  136. t.Errorf("UUIDs should be equal: %s and %s", u, u1)
  137. }
  138. b2 := []byte{}
  139. u2 := UUID{}
  140. err = u2.UnmarshalBinary(b2)
  141. if err == nil {
  142. t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
  143. }
  144. }
  145. func TestFromString(t *testing.T) {
  146. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  147. s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  148. s2 := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
  149. s3 := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  150. _, err := FromString("")
  151. if err == nil {
  152. t.Errorf("Should return error trying to parse empty string, got %s", err)
  153. }
  154. u1, err := FromString(s1)
  155. if err != nil {
  156. t.Errorf("Error parsing UUID from string: %s", err)
  157. }
  158. if !Equal(u, u1) {
  159. t.Errorf("UUIDs should be equal: %s and %s", u, u1)
  160. }
  161. u2, err := FromString(s2)
  162. if err != nil {
  163. t.Errorf("Error parsing UUID from string: %s", err)
  164. }
  165. if !Equal(u, u2) {
  166. t.Errorf("UUIDs should be equal: %s and %s", u, u2)
  167. }
  168. u3, err := FromString(s3)
  169. if err != nil {
  170. t.Errorf("Error parsing UUID from string: %s", err)
  171. }
  172. if !Equal(u, u3) {
  173. t.Errorf("UUIDs should be equal: %s and %s", u, u3)
  174. }
  175. }
  176. func TestMarshalText(t *testing.T) {
  177. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  178. b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  179. b2, err := u.MarshalText()
  180. if err != nil {
  181. t.Errorf("Error marshaling UUID: %s", err)
  182. }
  183. if !bytes.Equal(b1, b2) {
  184. t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
  185. }
  186. }
  187. func TestUnmarshalText(t *testing.T) {
  188. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  189. b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  190. u1 := UUID{}
  191. err := u1.UnmarshalText(b1)
  192. if err != nil {
  193. t.Errorf("Error unmarshaling UUID: %s", err)
  194. }
  195. if !Equal(u, u1) {
  196. t.Errorf("UUIDs should be equal: %s and %s", u, u1)
  197. }
  198. b2 := []byte("")
  199. u2 := UUID{}
  200. err = u2.UnmarshalText(b2)
  201. if err == nil {
  202. t.Errorf("Should return error trying to unmarshal from empty string")
  203. }
  204. }
  205. func TestNewV1(t *testing.T) {
  206. u := NewV1()
  207. if u.Version() != 1 {
  208. t.Errorf("UUIDv1 generated with incorrect version: %d", u.Version())
  209. }
  210. if u.Variant() != VariantRFC4122 {
  211. t.Errorf("UUIDv1 generated with incorrect variant: %d", u.Variant())
  212. }
  213. u1 := NewV1()
  214. u2 := NewV1()
  215. if Equal(u1, u2) {
  216. t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u1, u2)
  217. }
  218. oldFunc := epochFunc
  219. epochFunc = func() uint64 { return 0 }
  220. u3 := NewV1()
  221. u4 := NewV1()
  222. if Equal(u3, u4) {
  223. t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u3, u4)
  224. }
  225. epochFunc = oldFunc
  226. }
  227. func TestNewV2(t *testing.T) {
  228. u1 := NewV2(DomainPerson)
  229. if u1.Version() != 2 {
  230. t.Errorf("UUIDv2 generated with incorrect version: %d", u1.Version())
  231. }
  232. if u1.Variant() != VariantRFC4122 {
  233. t.Errorf("UUIDv2 generated with incorrect variant: %d", u1.Variant())
  234. }
  235. u2 := NewV2(DomainGroup)
  236. if u2.Version() != 2 {
  237. t.Errorf("UUIDv2 generated with incorrect version: %d", u2.Version())
  238. }
  239. if u2.Variant() != VariantRFC4122 {
  240. t.Errorf("UUIDv2 generated with incorrect variant: %d", u2.Variant())
  241. }
  242. }
  243. func TestNewV3(t *testing.T) {
  244. u := NewV3(NamespaceDNS, "www.example.com")
  245. if u.Version() != 3 {
  246. t.Errorf("UUIDv3 generated with incorrect version: %d", u.Version())
  247. }
  248. if u.Variant() != VariantRFC4122 {
  249. t.Errorf("UUIDv3 generated with incorrect variant: %d", u.Variant())
  250. }
  251. if u.String() != "5df41881-3aed-3515-88a7-2f4a814cf09e" {
  252. t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
  253. }
  254. u = NewV3(NamespaceDNS, "python.org")
  255. if u.String() != "6fa459ea-ee8a-3ca4-894e-db77e160355e" {
  256. t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
  257. }
  258. u1 := NewV3(NamespaceDNS, "golang.org")
  259. u2 := NewV3(NamespaceDNS, "golang.org")
  260. if !Equal(u1, u2) {
  261. t.Errorf("UUIDv3 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
  262. }
  263. u3 := NewV3(NamespaceDNS, "example.com")
  264. if Equal(u1, u3) {
  265. t.Errorf("UUIDv3 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
  266. }
  267. u4 := NewV3(NamespaceURL, "golang.org")
  268. if Equal(u1, u4) {
  269. t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
  270. }
  271. }
  272. func TestNewV4(t *testing.T) {
  273. u := NewV4()
  274. if u.Version() != 4 {
  275. t.Errorf("UUIDv4 generated with incorrect version: %d", u.Version())
  276. }
  277. if u.Variant() != VariantRFC4122 {
  278. t.Errorf("UUIDv4 generated with incorrect variant: %d", u.Variant())
  279. }
  280. }
  281. func TestNewV5(t *testing.T) {
  282. u := NewV5(NamespaceDNS, "www.example.com")
  283. if u.Version() != 5 {
  284. t.Errorf("UUIDv5 generated with incorrect version: %d", u.Version())
  285. }
  286. if u.Variant() != VariantRFC4122 {
  287. t.Errorf("UUIDv5 generated with incorrect variant: %d", u.Variant())
  288. }
  289. u = NewV5(NamespaceDNS, "python.org")
  290. if u.String() != "886313e1-3b8a-5372-9b90-0c9aee199e5d" {
  291. t.Errorf("UUIDv5 generated incorrectly: %s", u.String())
  292. }
  293. u1 := NewV5(NamespaceDNS, "golang.org")
  294. u2 := NewV5(NamespaceDNS, "golang.org")
  295. if !Equal(u1, u2) {
  296. t.Errorf("UUIDv5 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
  297. }
  298. u3 := NewV5(NamespaceDNS, "example.com")
  299. if Equal(u1, u3) {
  300. t.Errorf("UUIDv5 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
  301. }
  302. u4 := NewV5(NamespaceURL, "golang.org")
  303. if Equal(u1, u4) {
  304. t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
  305. }
  306. }