uuid_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // Copyright 2011 Google Inc. 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 uuid
  5. import (
  6. "bytes"
  7. "fmt"
  8. "os"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. type test struct {
  14. in string
  15. version Version
  16. variant Variant
  17. isuuid bool
  18. }
  19. var tests = []test{
  20. {"f47ac10b-58cc-0372-8567-0e02b2c3d479", 0, RFC4122, true},
  21. {"f47ac10b-58cc-1372-8567-0e02b2c3d479", 1, RFC4122, true},
  22. {"f47ac10b-58cc-2372-8567-0e02b2c3d479", 2, RFC4122, true},
  23. {"f47ac10b-58cc-3372-8567-0e02b2c3d479", 3, RFC4122, true},
  24. {"f47ac10b-58cc-4372-8567-0e02b2c3d479", 4, RFC4122, true},
  25. {"f47ac10b-58cc-5372-8567-0e02b2c3d479", 5, RFC4122, true},
  26. {"f47ac10b-58cc-6372-8567-0e02b2c3d479", 6, RFC4122, true},
  27. {"f47ac10b-58cc-7372-8567-0e02b2c3d479", 7, RFC4122, true},
  28. {"f47ac10b-58cc-8372-8567-0e02b2c3d479", 8, RFC4122, true},
  29. {"f47ac10b-58cc-9372-8567-0e02b2c3d479", 9, RFC4122, true},
  30. {"f47ac10b-58cc-a372-8567-0e02b2c3d479", 10, RFC4122, true},
  31. {"f47ac10b-58cc-b372-8567-0e02b2c3d479", 11, RFC4122, true},
  32. {"f47ac10b-58cc-c372-8567-0e02b2c3d479", 12, RFC4122, true},
  33. {"f47ac10b-58cc-d372-8567-0e02b2c3d479", 13, RFC4122, true},
  34. {"f47ac10b-58cc-e372-8567-0e02b2c3d479", 14, RFC4122, true},
  35. {"f47ac10b-58cc-f372-8567-0e02b2c3d479", 15, RFC4122, true},
  36. {"urn:uuid:f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, Reserved, true},
  37. {"URN:UUID:f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, Reserved, true},
  38. {"f47ac10b-58cc-4372-0567-0e02b2c3d479", 4, Reserved, true},
  39. {"f47ac10b-58cc-4372-1567-0e02b2c3d479", 4, Reserved, true},
  40. {"f47ac10b-58cc-4372-2567-0e02b2c3d479", 4, Reserved, true},
  41. {"f47ac10b-58cc-4372-3567-0e02b2c3d479", 4, Reserved, true},
  42. {"f47ac10b-58cc-4372-4567-0e02b2c3d479", 4, Reserved, true},
  43. {"f47ac10b-58cc-4372-5567-0e02b2c3d479", 4, Reserved, true},
  44. {"f47ac10b-58cc-4372-6567-0e02b2c3d479", 4, Reserved, true},
  45. {"f47ac10b-58cc-4372-7567-0e02b2c3d479", 4, Reserved, true},
  46. {"f47ac10b-58cc-4372-8567-0e02b2c3d479", 4, RFC4122, true},
  47. {"f47ac10b-58cc-4372-9567-0e02b2c3d479", 4, RFC4122, true},
  48. {"f47ac10b-58cc-4372-a567-0e02b2c3d479", 4, RFC4122, true},
  49. {"f47ac10b-58cc-4372-b567-0e02b2c3d479", 4, RFC4122, true},
  50. {"f47ac10b-58cc-4372-c567-0e02b2c3d479", 4, Microsoft, true},
  51. {"f47ac10b-58cc-4372-d567-0e02b2c3d479", 4, Microsoft, true},
  52. {"f47ac10b-58cc-4372-e567-0e02b2c3d479", 4, Future, true},
  53. {"f47ac10b-58cc-4372-f567-0e02b2c3d479", 4, Future, true},
  54. {"f47ac10b158cc-5372-a567-0e02b2c3d479", 0, Invalid, false},
  55. {"f47ac10b-58cc25372-a567-0e02b2c3d479", 0, Invalid, false},
  56. {"f47ac10b-58cc-53723a567-0e02b2c3d479", 0, Invalid, false},
  57. {"f47ac10b-58cc-5372-a56740e02b2c3d479", 0, Invalid, false},
  58. {"f47ac10b-58cc-5372-a567-0e02-2c3d479", 0, Invalid, false},
  59. {"g47ac10b-58cc-4372-a567-0e02b2c3d479", 0, Invalid, false},
  60. }
  61. var constants = []struct {
  62. c interface{}
  63. name string
  64. }{
  65. {Person, "Person"},
  66. {Group, "Group"},
  67. {Org, "Org"},
  68. {Invalid, "Invalid"},
  69. {RFC4122, "RFC4122"},
  70. {Reserved, "Reserved"},
  71. {Microsoft, "Microsoft"},
  72. {Future, "Future"},
  73. {Domain(17), "Domain17"},
  74. {Variant(42), "BadVariant42"},
  75. }
  76. func testTest(t *testing.T, in string, tt test) {
  77. uuid := Parse(in)
  78. if ok := (uuid != nil); ok != tt.isuuid {
  79. t.Errorf("Parse(%s) got %v expected %v\b", in, ok, tt.isuuid)
  80. }
  81. if uuid == nil {
  82. return
  83. }
  84. if v := uuid.Variant(); v != tt.variant {
  85. t.Errorf("Variant(%s) got %d expected %d\b", in, v, tt.variant)
  86. }
  87. if v, _ := uuid.Version(); v != tt.version {
  88. t.Errorf("Version(%s) got %d expected %d\b", in, v, tt.version)
  89. }
  90. }
  91. func TestUUID(t *testing.T) {
  92. for _, tt := range tests {
  93. testTest(t, tt.in, tt)
  94. testTest(t, strings.ToUpper(tt.in), tt)
  95. }
  96. }
  97. func TestConstants(t *testing.T) {
  98. for x, tt := range constants {
  99. v, ok := tt.c.(fmt.Stringer)
  100. if !ok {
  101. t.Errorf("%x: %v: not a stringer", x, v)
  102. } else if s := v.String(); s != tt.name {
  103. v, _ := tt.c.(int)
  104. t.Errorf("%x: Constant %T:%d gives %q, expected %q\n", x, tt.c, v, s, tt.name)
  105. }
  106. }
  107. }
  108. func TestRandomUUID(t *testing.T) {
  109. m := make(map[string]bool)
  110. for x := 1; x < 32; x++ {
  111. uuid := NewRandom()
  112. s := uuid.String()
  113. if m[s] {
  114. t.Errorf("NewRandom returned duplicated UUID %s\n", s)
  115. }
  116. m[s] = true
  117. if v, _ := uuid.Version(); v != 4 {
  118. t.Errorf("Random UUID of version %s\n", v)
  119. }
  120. if uuid.Variant() != RFC4122 {
  121. t.Errorf("Random UUID is variant %d\n", uuid.Variant())
  122. }
  123. }
  124. }
  125. func TestNew(t *testing.T) {
  126. m := make(map[string]bool)
  127. for x := 1; x < 32; x++ {
  128. s := New()
  129. if m[s] {
  130. t.Errorf("New returned duplicated UUID %s\n", s)
  131. }
  132. m[s] = true
  133. uuid := Parse(s)
  134. if uuid == nil {
  135. t.Errorf("New returned %q which does not decode\n", s)
  136. continue
  137. }
  138. if v, _ := uuid.Version(); v != 4 {
  139. t.Errorf("Random UUID of version %s\n", v)
  140. }
  141. if uuid.Variant() != RFC4122 {
  142. t.Errorf("Random UUID is variant %d\n", uuid.Variant())
  143. }
  144. }
  145. }
  146. func clockSeq(t *testing.T, uuid UUID) int {
  147. seq, ok := uuid.ClockSequence()
  148. if !ok {
  149. t.Fatalf("%s: invalid clock sequence\n", uuid)
  150. }
  151. return seq
  152. }
  153. func TestClockSeq(t *testing.T) {
  154. // Fake time.Now for this test to return a monotonically advancing time; restore it at end.
  155. defer func(orig func() time.Time) { timeNow = orig }(timeNow)
  156. monTime := time.Now()
  157. timeNow = func() time.Time {
  158. monTime = monTime.Add(1 * time.Second)
  159. return monTime
  160. }
  161. SetClockSequence(-1)
  162. uuid1 := NewUUID()
  163. uuid2 := NewUUID()
  164. if clockSeq(t, uuid1) != clockSeq(t, uuid2) {
  165. t.Errorf("clock sequence %d != %d\n", clockSeq(t, uuid1), clockSeq(t, uuid2))
  166. }
  167. SetClockSequence(-1)
  168. uuid2 = NewUUID()
  169. // Just on the very off chance we generated the same sequence
  170. // two times we try again.
  171. if clockSeq(t, uuid1) == clockSeq(t, uuid2) {
  172. SetClockSequence(-1)
  173. uuid2 = NewUUID()
  174. }
  175. if clockSeq(t, uuid1) == clockSeq(t, uuid2) {
  176. t.Errorf("Duplicate clock sequence %d\n", clockSeq(t, uuid1))
  177. }
  178. SetClockSequence(0x1234)
  179. uuid1 = NewUUID()
  180. if seq := clockSeq(t, uuid1); seq != 0x1234 {
  181. t.Errorf("%s: expected seq 0x1234 got 0x%04x\n", uuid1, seq)
  182. }
  183. }
  184. func TestCoding(t *testing.T) {
  185. text := "7d444840-9dc0-11d1-b245-5ffdce74fad2"
  186. urn := "urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2"
  187. data := UUID{
  188. 0x7d, 0x44, 0x48, 0x40,
  189. 0x9d, 0xc0,
  190. 0x11, 0xd1,
  191. 0xb2, 0x45,
  192. 0x5f, 0xfd, 0xce, 0x74, 0xfa, 0xd2,
  193. }
  194. if v := data.String(); v != text {
  195. t.Errorf("%x: encoded to %s, expected %s\n", data, v, text)
  196. }
  197. if v := data.URN(); v != urn {
  198. t.Errorf("%x: urn is %s, expected %s\n", data, v, urn)
  199. }
  200. uuid := Parse(text)
  201. if !Equal(uuid, data) {
  202. t.Errorf("%s: decoded to %s, expected %s\n", text, uuid, data)
  203. }
  204. }
  205. func TestVersion1(t *testing.T) {
  206. uuid1 := NewUUID()
  207. uuid2 := NewUUID()
  208. if Equal(uuid1, uuid2) {
  209. t.Errorf("%s:duplicate uuid\n", uuid1)
  210. }
  211. if v, _ := uuid1.Version(); v != 1 {
  212. t.Errorf("%s: version %s expected 1\n", uuid1, v)
  213. }
  214. if v, _ := uuid2.Version(); v != 1 {
  215. t.Errorf("%s: version %s expected 1\n", uuid2, v)
  216. }
  217. n1 := uuid1.NodeID()
  218. n2 := uuid2.NodeID()
  219. if !bytes.Equal(n1, n2) {
  220. t.Errorf("Different nodes %x != %x\n", n1, n2)
  221. }
  222. t1, ok := uuid1.Time()
  223. if !ok {
  224. t.Errorf("%s: invalid time\n", uuid1)
  225. }
  226. t2, ok := uuid2.Time()
  227. if !ok {
  228. t.Errorf("%s: invalid time\n", uuid2)
  229. }
  230. q1, ok := uuid1.ClockSequence()
  231. if !ok {
  232. t.Errorf("%s: invalid clock sequence\n", uuid1)
  233. }
  234. q2, ok := uuid2.ClockSequence()
  235. if !ok {
  236. t.Errorf("%s: invalid clock sequence", uuid2)
  237. }
  238. switch {
  239. case t1 == t2 && q1 == q2:
  240. t.Errorf("time stopped\n")
  241. case t1 > t2 && q1 == q2:
  242. t.Errorf("time reversed\n")
  243. case t1 < t2 && q1 != q2:
  244. t.Errorf("clock sequence chaned unexpectedly\n")
  245. }
  246. }
  247. func TestNodeAndTime(t *testing.T) {
  248. // Time is February 5, 1998 12:30:23.136364800 AM GMT
  249. uuid := Parse("7d444840-9dc0-11d1-b245-5ffdce74fad2")
  250. node := []byte{0x5f, 0xfd, 0xce, 0x74, 0xfa, 0xd2}
  251. ts, ok := uuid.Time()
  252. if ok {
  253. c := time.Unix(ts.UnixTime())
  254. want := time.Date(1998, 2, 5, 0, 30, 23, 136364800, time.UTC)
  255. if !c.Equal(want) {
  256. t.Errorf("Got time %v, want %v", c, want)
  257. }
  258. } else {
  259. t.Errorf("%s: bad time\n", uuid)
  260. }
  261. if !bytes.Equal(node, uuid.NodeID()) {
  262. t.Errorf("Expected node %v got %v\n", node, uuid.NodeID())
  263. }
  264. }
  265. func TestMD5(t *testing.T) {
  266. uuid := NewMD5(NameSpace_DNS, []byte("python.org")).String()
  267. want := "6fa459ea-ee8a-3ca4-894e-db77e160355e"
  268. if uuid != want {
  269. t.Errorf("MD5: got %q expected %q\n", uuid, want)
  270. }
  271. }
  272. func TestSHA1(t *testing.T) {
  273. uuid := NewSHA1(NameSpace_DNS, []byte("python.org")).String()
  274. want := "886313e1-3b8a-5372-9b90-0c9aee199e5d"
  275. if uuid != want {
  276. t.Errorf("SHA1: got %q expected %q\n", uuid, want)
  277. }
  278. }
  279. func TestNodeID(t *testing.T) {
  280. nid := []byte{1, 2, 3, 4, 5, 6}
  281. SetNodeInterface("")
  282. s := NodeInterface()
  283. if s == "" || s == "user" {
  284. t.Errorf("NodeInterface %q after SetInteface\n", s)
  285. }
  286. node1 := NodeID()
  287. if node1 == nil {
  288. t.Errorf("NodeID nil after SetNodeInterface\n", s)
  289. }
  290. SetNodeID(nid)
  291. s = NodeInterface()
  292. if s != "user" {
  293. t.Errorf("Expected NodeInterface %q got %q\n", "user", s)
  294. }
  295. node2 := NodeID()
  296. if node2 == nil {
  297. t.Errorf("NodeID nil after SetNodeID\n", s)
  298. }
  299. if bytes.Equal(node1, node2) {
  300. t.Errorf("NodeID not changed after SetNodeID\n", s)
  301. } else if !bytes.Equal(nid, node2) {
  302. t.Errorf("NodeID is %x, expected %x\n", node2, nid)
  303. }
  304. }
  305. func testDCE(t *testing.T, name string, uuid UUID, domain Domain, id uint32) {
  306. if uuid == nil {
  307. t.Errorf("%s failed\n", name)
  308. return
  309. }
  310. if v, _ := uuid.Version(); v != 2 {
  311. t.Errorf("%s: %s: expected version 2, got %s\n", name, uuid, v)
  312. return
  313. }
  314. if v, ok := uuid.Domain(); !ok || v != domain {
  315. if !ok {
  316. t.Errorf("%s: %d: Domain failed\n", name, uuid)
  317. } else {
  318. t.Errorf("%s: %s: expected domain %d, got %d\n", name, uuid, domain, v)
  319. }
  320. }
  321. if v, ok := uuid.Id(); !ok || v != id {
  322. if !ok {
  323. t.Errorf("%s: %d: Id failed\n", name, uuid)
  324. } else {
  325. t.Errorf("%s: %s: expected id %d, got %d\n", name, uuid, id, v)
  326. }
  327. }
  328. }
  329. func TestDCE(t *testing.T) {
  330. testDCE(t, "NewDCESecurity", NewDCESecurity(42, 12345678), 42, 12345678)
  331. testDCE(t, "NewDCEPerson", NewDCEPerson(), Person, uint32(os.Getuid()))
  332. testDCE(t, "NewDCEGroup", NewDCEGroup(), Group, uint32(os.Getgid()))
  333. }
  334. type badRand struct{}
  335. func (r badRand) Read(buf []byte) (int, error) {
  336. for i, _ := range buf {
  337. buf[i] = byte(i)
  338. }
  339. return len(buf), nil
  340. }
  341. func TestBadRand(t *testing.T) {
  342. SetRand(badRand{})
  343. uuid1 := New()
  344. uuid2 := New()
  345. if uuid1 != uuid2 {
  346. t.Errorf("execpted duplicates, got %q and %q\n", uuid1, uuid2)
  347. }
  348. SetRand(nil)
  349. uuid1 = New()
  350. uuid2 = New()
  351. if uuid1 == uuid2 {
  352. t.Errorf("unexecpted duplicates, got %q\n", uuid1)
  353. }
  354. }