PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/CORP-RESELLER/amazon-ssm-agent
Go | 449 lines | 370 code | 51 blank | 28 comment | 73 complexity | 134e9b5060df413289ab6532fa797f43 MD5 | raw file
  1. package uuid
  2. /****************
  3. * Date: 3/02/14
  4. * Time: 10:59 PM
  5. ***************/
  6. import (
  7. "crypto/md5"
  8. "crypto/sha1"
  9. "fmt"
  10. "regexp"
  11. "strings"
  12. "testing"
  13. )
  14. const (
  15. secondsPerHour = 60 * 60
  16. secondsPerDay = 24 * secondsPerHour
  17. unixToInternal int64 = (1969*365 + 1969/4 - 1969/100 + 1969/400) * secondsPerDay
  18. internalToUnix int64 = -unixToInternal
  19. )
  20. var (
  21. uuid_goLang Name = "https://google.com/golang.org?q=golang"
  22. printer bool = false
  23. uuid_bytes = []byte{
  24. 0xAA, 0xCF, 0xEE, 0x12,
  25. 0xD4, 0x00,
  26. 0x27, 0x23,
  27. 0x00,
  28. 0xD3,
  29. 0x23, 0x12, 0x4A, 0x11, 0x89, 0xFF,
  30. }
  31. uuid_variants = []byte{
  32. ReservedNCS, ReservedRFC4122, ReservedMicrosoft, ReservedFuture,
  33. }
  34. namespaceUuids = []UUID{
  35. NamespaceDNS, NamespaceURL, NamespaceOID, NamespaceX500,
  36. }
  37. invalidHexStrings = [...]string{
  38. "foo",
  39. "6ba7b814-9dad-11d1-80b4-",
  40. "6ba7b814--9dad-11d1-80b4--00c04fd430c8",
  41. "6ba7b814-9dad7-11d1-80b4-00c04fd430c8999",
  42. "{6ba7b814-9dad-1180b4-00c04fd430c8",
  43. "{6ba7b814--11d1-80b4-00c04fd430c8}",
  44. "urn:uuid:6ba7b814-9dad-1666666680b4-00c04fd430c8",
  45. }
  46. validHexStrings = [...]string{
  47. "6ba7b8149dad-11d1-80b4-00c04fd430c8}",
  48. "{6ba7b8149dad-11d1-80b400c04fd430c8}",
  49. "{6ba7b814-9dad11d180b400c04fd430c8}",
  50. "6ba7b8149dad-11d1-80b4-00c04fd430c8",
  51. "6ba7b814-9dad11d1-80b4-00c04fd430c8",
  52. "6ba7b814-9dad-11d180b4-00c04fd430c8",
  53. "6ba7b814-9dad-11d1-80b400c04fd430c8",
  54. "6ba7b8149dad11d180b400c04fd430c8",
  55. "6ba7b814-9dad-11d1-80b4-00c04fd430c8",
  56. "{6ba7b814-9dad-11d1-80b4-00c04fd430c8}",
  57. "{6ba7b814-9dad-11d1-80b4-00c04fd430c8",
  58. "6ba7b814-9dad-11d1-80b4-00c04fd430c8}",
  59. "(6ba7b814-9dad-11d1-80b4-00c04fd430c8)",
  60. "urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8",
  61. }
  62. )
  63. func TestUUID_Simple(t *testing.T) {
  64. u := NewV1()
  65. outputLn(u)
  66. u, _ = Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  67. u = NewV3(u, Name("test"))
  68. outputLn(u)
  69. if !strings.EqualFold("45a113ac-c7f2-30b0-90a5-a399ab912716", u.String()) {
  70. t.Errorf("Expected string representation to be %s, but got: %s", "45a113ac-c7f2-30b0-90a5-a399ab912716", u.String())
  71. }
  72. u = NewV4()
  73. outputLn(u)
  74. u = NewV5(u, Name("test"))
  75. outputLn(u)
  76. }
  77. func TestUUID_New(t *testing.T) {
  78. u := New(uuid_bytes)
  79. if u == nil {
  80. t.Error("Expected a valid UUID")
  81. }
  82. if u.Version() != 2 {
  83. t.Errorf("Expected correct version %d, but got %d", 2, u.Version())
  84. }
  85. if u.Variant() != ReservedNCS {
  86. t.Errorf("Expected ReservedNCS variant %x, but got %x", ReservedNCS, u.Variant())
  87. }
  88. if !parseUUIDRegex.MatchString(u.String()) {
  89. t.Errorf("Expected string representation to be valid, given: %s", u.String())
  90. }
  91. }
  92. func TestUUID_NewBulk(t *testing.T) {
  93. for i := 0; i < 1000000; i++ {
  94. New(uuid_bytes)
  95. }
  96. }
  97. const (
  98. clean = `[A-Fa-f0-9]{8}[A-Fa-f0-9]{4}[1-5fF][A-Fa-f0-9]{3}[A-Fa-f0-9]{4}[A-Fa-f0-9]{12}`
  99. cleanHexPattern = `^` + clean + `$`
  100. curlyHexPattern = `^\{` + clean + `\}$`
  101. bracketHexPattern = `^\(` + clean + `\)$`
  102. hyphen = `[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[1-5fF][A-Fa-f0-9]{3}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}`
  103. cleanHyphenHexPattern = `^` + hyphen + `$`
  104. curlyHyphenHexPattern = `^\{` + hyphen + `\}$`
  105. bracketHyphenHexPattern = `^\(` + hyphen + `\)$`
  106. goIdHexPattern = `^\[[A-F0-9]{8}-[A-F0-9]{4}-[1-5fF][a-f0-9]{3}-[A-F0-9]{4}-[a-f0-9]{12}\]$`
  107. )
  108. func TestUUID_Formats_String(t *testing.T) {
  109. ids := []UUID{NewV4(), NewV1()}
  110. // Reset default
  111. SwitchFormat(CleanHyphen)
  112. for _, u := range ids {
  113. SwitchFormatUpperCase(CurlyHyphen)
  114. if !regexp.MustCompile(curlyHyphenHexPattern).MatchString(u.String()) {
  115. t.Error("Curly hyphen UUID string output got", u, u.Version())
  116. }
  117. outputLn(u)
  118. // Clean
  119. SwitchFormat(Clean)
  120. if !regexp.MustCompile(cleanHexPattern).MatchString(u.String()) {
  121. t.Error("Clean UUID string output got", u, u.Version())
  122. }
  123. outputLn(u)
  124. // Curly
  125. SwitchFormat(Curly)
  126. if !regexp.MustCompile(curlyHexPattern).MatchString(u.String()) {
  127. t.Error("Curly clean UUID string output got", u, u.Version())
  128. }
  129. outputLn(u)
  130. // Bracket
  131. SwitchFormat(Bracket)
  132. if !regexp.MustCompile(bracketHexPattern).MatchString(u.String()) {
  133. t.Error("Bracket clean UUID string output got", u, u.Version())
  134. }
  135. outputLn(u)
  136. // Clean Hyphen
  137. SwitchFormat(CleanHyphen)
  138. if !regexp.MustCompile(cleanHyphenHexPattern).MatchString(u.String()) {
  139. t.Error("Clean hyphen UUID string output got", u, u.Version())
  140. }
  141. outputLn(u)
  142. // Bracket Hyphen
  143. SwitchFormat(BracketHyphen)
  144. if !regexp.MustCompile(bracketHyphenHexPattern).MatchString(u.String()) {
  145. t.Error("Bracket hyphen UUID string output got", u, u.Version())
  146. }
  147. outputLn(u)
  148. // Bracket Hyphen
  149. SwitchFormat(GoIdFormat)
  150. if !regexp.MustCompile(goIdHexPattern).MatchString(u.String()) {
  151. t.Error("GoId UUID string output expected", u, u.Version())
  152. }
  153. outputLn(u)
  154. // Reset default
  155. SwitchFormat(CleanHyphen)
  156. }
  157. }
  158. func TestUUID_Formatter(t *testing.T) {
  159. ids := []UUID{NewV4(), NewV1()}
  160. for _, u := range ids {
  161. // CurlyHyphen - default
  162. if !regexp.MustCompile(curlyHyphenHexPattern).MatchString(Formatter(u, CurlyHyphen)) {
  163. t.Error("Curly hyphen UUID string output got", Formatter(u, CurlyHyphen))
  164. }
  165. outputLn(Formatter(u, CurlyHyphen))
  166. // Clean
  167. if !regexp.MustCompile(cleanHexPattern).MatchString(Formatter(u, Clean)) {
  168. t.Error("Clean UUID string output got", Formatter(u, Clean))
  169. }
  170. outputLn(Formatter(u, Clean))
  171. // Curly
  172. if !regexp.MustCompile(curlyHexPattern).MatchString(Formatter(u, Curly)) {
  173. t.Error("Curly clean UUID string output", Formatter(u, Curly))
  174. }
  175. outputLn(Formatter(u, Curly))
  176. // Bracket
  177. if !regexp.MustCompile(bracketHexPattern).MatchString(Formatter(u, Bracket)) {
  178. t.Error("Bracket clean UUID string output", Formatter(u, Bracket))
  179. }
  180. outputLn(Formatter(u, Bracket))
  181. // Clean Hyphen
  182. if !regexp.MustCompile(cleanHyphenHexPattern).MatchString(Formatter(u, CleanHyphen)) {
  183. t.Error("Clean hyphen UUID string output", Formatter(u, CleanHyphen))
  184. }
  185. outputLn(Formatter(u, CleanHyphen))
  186. // Bracket Hyphen
  187. if !regexp.MustCompile(bracketHyphenHexPattern).MatchString(Formatter(u, BracketHyphen)) {
  188. t.Error("Bracket hyphen UUID string output", Formatter(u, BracketHyphen))
  189. }
  190. outputLn(Formatter(u, BracketHyphen))
  191. // GoId Format
  192. if !regexp.MustCompile(goIdHexPattern).MatchString(Formatter(u, GoIdFormat)) {
  193. t.Error("GoId UUID string output expected", Formatter(u, GoIdFormat))
  194. }
  195. outputLn(Formatter(u, GoIdFormat))
  196. }
  197. }
  198. func TestUUID_NewHex(t *testing.T) {
  199. s := "f3593cffee9240df408687825b523f13"
  200. u := NewHex(s)
  201. if u == nil {
  202. t.Error("Expected a valid UUID")
  203. }
  204. if u.Version() != 4 {
  205. t.Errorf("Expected correct version %d, but got %d", 4, u.Version())
  206. }
  207. if u.Variant() != ReservedNCS {
  208. t.Errorf("Expected ReservedNCS variant %x, but got %x", ReservedNCS, u.Variant())
  209. }
  210. if !parseUUIDRegex.MatchString(u.String()) {
  211. t.Errorf("Expected string representation to be valid, given: %s", u.String())
  212. }
  213. }
  214. func TestUUID_NewHexBulk(t *testing.T) {
  215. for i := 0; i < 1000000; i++ {
  216. s := "f3593cffee9240df408687825b523f13"
  217. NewHex(s)
  218. }
  219. }
  220. func TestUUID_Parse(t *testing.T) {
  221. for _, v := range invalidHexStrings {
  222. _, err := Parse(v)
  223. if err == nil {
  224. t.Error("Expected error due to invalid UUID string:", v)
  225. }
  226. }
  227. for _, v := range validHexStrings {
  228. _, err := Parse(v)
  229. if err != nil {
  230. t.Error("Expected valid UUID string but got error:", v)
  231. }
  232. }
  233. for _, id := range namespaceUuids {
  234. _, err := Parse(id.String())
  235. if err != nil {
  236. t.Error("Expected valid UUID string but got error:", err)
  237. }
  238. }
  239. }
  240. func TestUUID_Sum(t *testing.T) {
  241. u := new(Array)
  242. Digest(u, NamespaceDNS, uuid_goLang, md5.New())
  243. if u.Bytes() == nil {
  244. t.Error("Expected new data in bytes")
  245. }
  246. output(u.Bytes())
  247. u = new(Array)
  248. Digest(u, NamespaceDNS, uuid_goLang, sha1.New())
  249. if u.Bytes() == nil {
  250. t.Error("Expected new data in bytes")
  251. }
  252. output(u.Bytes())
  253. }
  254. // Tests all possible version numbers and that
  255. // each number returned is the same
  256. func TestUUID_Struct_VersionBits(t *testing.T) {
  257. uStruct := new(Struct)
  258. uStruct.size = length
  259. for v := 0; v < 16; v++ {
  260. for i := 0; i <= 255; i++ {
  261. uuid_bytes[versionIndex] = byte(i)
  262. uStruct.Unmarshal(uuid_bytes)
  263. uStruct.setVersion(v)
  264. output(uStruct)
  265. if uStruct.Version() != v {
  266. t.Errorf("%x does not resolve to %x", byte(uStruct.Version()), v)
  267. }
  268. output("\n")
  269. }
  270. }
  271. }
  272. // Tests all possible variants with their respective bits set
  273. // Tests whether the expected output comes out on each byte case
  274. func TestUUID_Struct_VariantBits(t *testing.T) {
  275. for _, v := range uuid_variants {
  276. for i := 0; i <= 255; i++ {
  277. uuid_bytes[variantIndex] = byte(i)
  278. uStruct := createStruct(uuid_bytes, 4, v)
  279. b := uStruct.sequenceHiAndVariant >> 4
  280. tVariantConstraint(v, b, uStruct, t)
  281. if uStruct.Variant() != v {
  282. t.Errorf("%d does not resolve to %x: get %x", i, v, uStruct.Variant())
  283. }
  284. }
  285. }
  286. }
  287. // Tests all possible variants with their respective bits set
  288. // Tests whether the expected output comes out on each byte case
  289. func TestUUID_Array_VariantBits(t *testing.T) {
  290. for _, v := range uuid_variants {
  291. for i := 0; i <= 255; i++ {
  292. uuid_bytes[variantIndex] = byte(i)
  293. uArray := createArray(uuid_bytes, 4, v)
  294. b := uArray[variantIndex] >> 4
  295. tVariantConstraint(v, b, uArray, t)
  296. if uArray.Variant() != v {
  297. t.Errorf("%d does not resolve to %x", i, v)
  298. }
  299. }
  300. }
  301. }
  302. // Tests all possible version numbers and that
  303. // each number returned is the same
  304. func TestUUID_Array_VersionBits(t *testing.T) {
  305. uArray := new(Array)
  306. for v := 0; v < 16; v++ {
  307. for i := 0; i <= 255; i++ {
  308. uuid_bytes[versionIndex] = byte(i)
  309. uArray.Unmarshal(uuid_bytes)
  310. uArray.setVersion(v)
  311. output(uArray)
  312. if uArray.Version() != v {
  313. t.Errorf("%x does not resolve to %x", byte(uArray.Version()), v)
  314. }
  315. output("\n")
  316. }
  317. }
  318. }
  319. func BenchmarkUUID_Parse(b *testing.B) {
  320. s := "f3593cff-ee92-40df-4086-87825b523f13"
  321. for i := 0; i < b.N; i++ {
  322. _, err := Parse(s)
  323. if err != nil {
  324. b.Fatal(err)
  325. }
  326. }
  327. b.StopTimer()
  328. b.ReportAllocs()
  329. }
  330. // *******************************************************
  331. func createStruct(pData []byte, pVersion int, pVariant byte) *Struct {
  332. o := new(Struct)
  333. o.size = length
  334. o.Unmarshal(pData)
  335. o.setVersion(pVersion)
  336. o.setVariant(pVariant)
  337. return o
  338. }
  339. func createArray(pData []byte, pVersion int, pVariant byte) *Array {
  340. o := new(Array)
  341. o.Unmarshal(pData)
  342. o.setVersion(pVersion)
  343. o.setVariant(pVariant)
  344. return o
  345. }
  346. func tVariantConstraint(v byte, b byte, o UUID, t *testing.T) {
  347. output(o)
  348. switch v {
  349. case ReservedNCS:
  350. switch b {
  351. case 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07:
  352. outputF(": %X ", b)
  353. break
  354. default:
  355. t.Errorf("%X most high bits do not resolve to 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07", b)
  356. }
  357. case ReservedRFC4122:
  358. switch b {
  359. case 0x08, 0x09, 0x0A, 0x0B:
  360. outputF(": %X ", b)
  361. break
  362. default:
  363. t.Errorf("%X most high bits do not resolve to 0x08, 0x09, 0x0A, 0x0B", b)
  364. }
  365. case ReservedMicrosoft:
  366. switch b {
  367. case 0x0C, 0x0D:
  368. outputF(": %X ", b)
  369. break
  370. default:
  371. t.Errorf("%X most high bits do not resolve to 0x0C, 0x0D", b)
  372. }
  373. case ReservedFuture:
  374. switch b {
  375. case 0x0E, 0x0F:
  376. outputF(": %X ", b)
  377. break
  378. default:
  379. t.Errorf("%X most high bits do not resolve to 0x0E, 0x0F", b)
  380. }
  381. }
  382. output("\n")
  383. }
  384. func output(a ...interface{}) {
  385. if printer {
  386. fmt.Print(a...)
  387. }
  388. }
  389. func outputLn(a ...interface{}) {
  390. if printer {
  391. fmt.Println(a...)
  392. }
  393. }
  394. func outputF(format string, a ...interface{}) {
  395. if printer {
  396. fmt.Printf(format, a)
  397. }
  398. }