You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

633 lines
17 KiB

  1. // Copyright (C) 2013, 2015 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 TestFromStringShort(t *testing.T) {
  177. // Invalid 35-character UUID string
  178. s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c"
  179. for i := len(s1); i >= 0; i-- {
  180. _, err := FromString(s1[:i])
  181. if err == nil {
  182. t.Errorf("Should return error trying to parse too short string, got %s", err)
  183. }
  184. }
  185. }
  186. func TestFromStringLong(t *testing.T) {
  187. // Invalid 37+ character UUID string
  188. s := []string{
  189. "6ba7b810-9dad-11d1-80b4-00c04fd430c8=",
  190. "6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
  191. "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}f",
  192. "6ba7b810-9dad-11d1-80b4-00c04fd430c800c04fd430c8",
  193. }
  194. for _, str := range s {
  195. _, err := FromString(str)
  196. if err == nil {
  197. t.Errorf("Should return error trying to parse too long string, passed %s", str)
  198. }
  199. }
  200. }
  201. func TestFromStringInvalid(t *testing.T) {
  202. // Invalid UUID string formats
  203. s := []string{
  204. "6ba7b8109dad11d180b400c04fd430c8",
  205. "6ba7b8109dad11d180b400c04fd430c86ba7b8109dad11d180b400c04fd430c8",
  206. "urn:uuid:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
  207. "6ba7b8109-dad-11d1-80b4-00c04fd430c8",
  208. "6ba7b810-9dad1-1d1-80b4-00c04fd430c8",
  209. "6ba7b810-9dad-11d18-0b4-00c04fd430c8",
  210. "6ba7b810-9dad-11d1-80b40-0c04fd430c8",
  211. "6ba7b810+9dad+11d1+80b4+00c04fd430c8",
  212. "6ba7b810-9dad11d180b400c04fd430c8",
  213. "6ba7b8109dad-11d180b400c04fd430c8",
  214. "6ba7b8109dad11d1-80b400c04fd430c8",
  215. "6ba7b8109dad11d180b4-00c04fd430c8",
  216. }
  217. for _, str := range s {
  218. _, err := FromString(str)
  219. if err == nil {
  220. t.Errorf("Should return error trying to parse invalid string, passed %s", str)
  221. }
  222. }
  223. }
  224. func TestFromStringOrNil(t *testing.T) {
  225. u := FromStringOrNil("")
  226. if u != Nil {
  227. t.Errorf("Should return Nil UUID on parse failure, got %s", u)
  228. }
  229. }
  230. func TestFromBytesOrNil(t *testing.T) {
  231. b := []byte{}
  232. u := FromBytesOrNil(b)
  233. if u != Nil {
  234. t.Errorf("Should return Nil UUID on parse failure, got %s", u)
  235. }
  236. }
  237. func TestMarshalText(t *testing.T) {
  238. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  239. b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  240. b2, err := u.MarshalText()
  241. if err != nil {
  242. t.Errorf("Error marshaling UUID: %s", err)
  243. }
  244. if !bytes.Equal(b1, b2) {
  245. t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
  246. }
  247. }
  248. func TestUnmarshalText(t *testing.T) {
  249. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  250. b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  251. u1 := UUID{}
  252. err := u1.UnmarshalText(b1)
  253. if err != nil {
  254. t.Errorf("Error unmarshaling UUID: %s", err)
  255. }
  256. if !Equal(u, u1) {
  257. t.Errorf("UUIDs should be equal: %s and %s", u, u1)
  258. }
  259. b2 := []byte("")
  260. u2 := UUID{}
  261. err = u2.UnmarshalText(b2)
  262. if err == nil {
  263. t.Errorf("Should return error trying to unmarshal from empty string")
  264. }
  265. }
  266. func TestValue(t *testing.T) {
  267. u, err := FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  268. if err != nil {
  269. t.Errorf("Error parsing UUID from string: %s", err)
  270. }
  271. val, err := u.Value()
  272. if err != nil {
  273. t.Errorf("Error getting UUID value: %s", err)
  274. }
  275. if val != u.String() {
  276. t.Errorf("Wrong value returned, should be equal: %s and %s", val, u)
  277. }
  278. }
  279. func TestValueNil(t *testing.T) {
  280. u := UUID{}
  281. val, err := u.Value()
  282. if err != nil {
  283. t.Errorf("Error getting UUID value: %s", err)
  284. }
  285. if val != Nil.String() {
  286. t.Errorf("Wrong value returned, should be equal to UUID.Nil: %s", val)
  287. }
  288. }
  289. func TestNullUUIDValueNil(t *testing.T) {
  290. u := NullUUID{}
  291. val, err := u.Value()
  292. if err != nil {
  293. t.Errorf("Error getting UUID value: %s", err)
  294. }
  295. if val != nil {
  296. t.Errorf("Wrong value returned, should be nil: %s", val)
  297. }
  298. }
  299. func TestScanBinary(t *testing.T) {
  300. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  301. b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  302. u1 := UUID{}
  303. err := u1.Scan(b1)
  304. if err != nil {
  305. t.Errorf("Error unmarshaling UUID: %s", err)
  306. }
  307. if !Equal(u, u1) {
  308. t.Errorf("UUIDs should be equal: %s and %s", u, u1)
  309. }
  310. b2 := []byte{}
  311. u2 := UUID{}
  312. err = u2.Scan(b2)
  313. if err == nil {
  314. t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
  315. }
  316. }
  317. func TestScanString(t *testing.T) {
  318. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  319. s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  320. u1 := UUID{}
  321. err := u1.Scan(s1)
  322. if err != nil {
  323. t.Errorf("Error unmarshaling UUID: %s", err)
  324. }
  325. if !Equal(u, u1) {
  326. t.Errorf("UUIDs should be equal: %s and %s", u, u1)
  327. }
  328. s2 := ""
  329. u2 := UUID{}
  330. err = u2.Scan(s2)
  331. if err == nil {
  332. t.Errorf("Should return error trying to unmarshal from empty string")
  333. }
  334. }
  335. func TestScanText(t *testing.T) {
  336. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  337. b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  338. u1 := UUID{}
  339. err := u1.Scan(b1)
  340. if err != nil {
  341. t.Errorf("Error unmarshaling UUID: %s", err)
  342. }
  343. if !Equal(u, u1) {
  344. t.Errorf("UUIDs should be equal: %s and %s", u, u1)
  345. }
  346. b2 := []byte("")
  347. u2 := UUID{}
  348. err = u2.Scan(b2)
  349. if err == nil {
  350. t.Errorf("Should return error trying to unmarshal from empty string")
  351. }
  352. }
  353. func TestScanUnsupported(t *testing.T) {
  354. u := UUID{}
  355. err := u.Scan(true)
  356. if err == nil {
  357. t.Errorf("Should return error trying to unmarshal from bool")
  358. }
  359. }
  360. func TestScanNil(t *testing.T) {
  361. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  362. err := u.Scan(nil)
  363. if err == nil {
  364. t.Errorf("Error UUID shouldn't allow unmarshalling from nil")
  365. }
  366. }
  367. func TestNullUUIDScanValid(t *testing.T) {
  368. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  369. s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  370. u1 := NullUUID{}
  371. err := u1.Scan(s1)
  372. if err != nil {
  373. t.Errorf("Error unmarshaling NullUUID: %s", err)
  374. }
  375. if !u1.Valid {
  376. t.Errorf("NullUUID should be valid")
  377. }
  378. if !Equal(u, u1.UUID) {
  379. t.Errorf("UUIDs should be equal: %s and %s", u, u1.UUID)
  380. }
  381. }
  382. func TestNullUUIDScanNil(t *testing.T) {
  383. u := NullUUID{UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}, true}
  384. err := u.Scan(nil)
  385. if err != nil {
  386. t.Errorf("Error unmarshaling NullUUID: %s", err)
  387. }
  388. if u.Valid {
  389. t.Errorf("NullUUID should not be valid")
  390. }
  391. if !Equal(u.UUID, Nil) {
  392. t.Errorf("NullUUID value should be equal to Nil: %v", u)
  393. }
  394. }
  395. func TestNewV1(t *testing.T) {
  396. u := NewV1()
  397. if u.Version() != 1 {
  398. t.Errorf("UUIDv1 generated with incorrect version: %d", u.Version())
  399. }
  400. if u.Variant() != VariantRFC4122 {
  401. t.Errorf("UUIDv1 generated with incorrect variant: %d", u.Variant())
  402. }
  403. u1 := NewV1()
  404. u2 := NewV1()
  405. if Equal(u1, u2) {
  406. t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u1, u2)
  407. }
  408. oldFunc := epochFunc
  409. epochFunc = func() uint64 { return 0 }
  410. u3 := NewV1()
  411. u4 := NewV1()
  412. if Equal(u3, u4) {
  413. t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u3, u4)
  414. }
  415. epochFunc = oldFunc
  416. }
  417. func TestNewV2(t *testing.T) {
  418. u1 := NewV2(DomainPerson)
  419. if u1.Version() != 2 {
  420. t.Errorf("UUIDv2 generated with incorrect version: %d", u1.Version())
  421. }
  422. if u1.Variant() != VariantRFC4122 {
  423. t.Errorf("UUIDv2 generated with incorrect variant: %d", u1.Variant())
  424. }
  425. u2 := NewV2(DomainGroup)
  426. if u2.Version() != 2 {
  427. t.Errorf("UUIDv2 generated with incorrect version: %d", u2.Version())
  428. }
  429. if u2.Variant() != VariantRFC4122 {
  430. t.Errorf("UUIDv2 generated with incorrect variant: %d", u2.Variant())
  431. }
  432. }
  433. func TestNewV3(t *testing.T) {
  434. u := NewV3(NamespaceDNS, "www.example.com")
  435. if u.Version() != 3 {
  436. t.Errorf("UUIDv3 generated with incorrect version: %d", u.Version())
  437. }
  438. if u.Variant() != VariantRFC4122 {
  439. t.Errorf("UUIDv3 generated with incorrect variant: %d", u.Variant())
  440. }
  441. if u.String() != "5df41881-3aed-3515-88a7-2f4a814cf09e" {
  442. t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
  443. }
  444. u = NewV3(NamespaceDNS, "python.org")
  445. if u.String() != "6fa459ea-ee8a-3ca4-894e-db77e160355e" {
  446. t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
  447. }
  448. u1 := NewV3(NamespaceDNS, "golang.org")
  449. u2 := NewV3(NamespaceDNS, "golang.org")
  450. if !Equal(u1, u2) {
  451. t.Errorf("UUIDv3 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
  452. }
  453. u3 := NewV3(NamespaceDNS, "example.com")
  454. if Equal(u1, u3) {
  455. t.Errorf("UUIDv3 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
  456. }
  457. u4 := NewV3(NamespaceURL, "golang.org")
  458. if Equal(u1, u4) {
  459. t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
  460. }
  461. }
  462. func TestNewV4(t *testing.T) {
  463. u := NewV4()
  464. if u.Version() != 4 {
  465. t.Errorf("UUIDv4 generated with incorrect version: %d", u.Version())
  466. }
  467. if u.Variant() != VariantRFC4122 {
  468. t.Errorf("UUIDv4 generated with incorrect variant: %d", u.Variant())
  469. }
  470. }
  471. func TestNewV5(t *testing.T) {
  472. u := NewV5(NamespaceDNS, "www.example.com")
  473. if u.Version() != 5 {
  474. t.Errorf("UUIDv5 generated with incorrect version: %d", u.Version())
  475. }
  476. if u.Variant() != VariantRFC4122 {
  477. t.Errorf("UUIDv5 generated with incorrect variant: %d", u.Variant())
  478. }
  479. u = NewV5(NamespaceDNS, "python.org")
  480. if u.String() != "886313e1-3b8a-5372-9b90-0c9aee199e5d" {
  481. t.Errorf("UUIDv5 generated incorrectly: %s", u.String())
  482. }
  483. u1 := NewV5(NamespaceDNS, "golang.org")
  484. u2 := NewV5(NamespaceDNS, "golang.org")
  485. if !Equal(u1, u2) {
  486. t.Errorf("UUIDv5 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
  487. }
  488. u3 := NewV5(NamespaceDNS, "example.com")
  489. if Equal(u1, u3) {
  490. t.Errorf("UUIDv5 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
  491. }
  492. u4 := NewV5(NamespaceURL, "golang.org")
  493. if Equal(u1, u4) {
  494. t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
  495. }
  496. }