PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/uuid_test.go

https://bitbucket.org/kisom/uuid
Go | 49 lines | 44 code | 5 blank | 0 comment | 14 complexity | 41e778b712a7a0811a1e21f70c0d0f91 MD5 | raw file
Possible License(s): 0BSD
  1. package uuid
  2. import (
  3. "fmt"
  4. "regexp"
  5. "testing"
  6. )
  7. const hexOctet = "[A-F0-9]"
  8. var uuidV4RegexStr = fmt.Sprintf("^%s{8}-%s{4}-4%s{3}-[89AB]%s{3}-%s{12}$",
  9. hexOctet, hexOctet, hexOctet, hexOctet, hexOctet)
  10. var uuidVersion4Regex = regexp.MustCompile(uuidV4RegexStr)
  11. func TestGenerateV4(t *testing.T) {
  12. fmt.Println("[+] testing GenerateV4()")
  13. var u []byte
  14. var err error
  15. u, err = GenerateV4()
  16. if err != nil {
  17. fmt.Printf("[!] %s\n", err.Error())
  18. t.FailNow()
  19. } else if len(u) != 36 {
  20. fmt.Printf("[!] UUID length mismatch: %s\n", u)
  21. t.FailNow()
  22. } else if !uuidVersion4Regex.Match(u) {
  23. fmt.Printf("[!] invalid UUID: %s\n", u)
  24. t.FailNow()
  25. }
  26. fmt.Printf("\t[*] generate UUID: %s\n", string(u))
  27. }
  28. func TestGenerateV4String(t *testing.T) {
  29. fmt.Println("[+] testing GenerateV4String()")
  30. var u string
  31. var err error
  32. u, err = GenerateV4String()
  33. if err != nil {
  34. fmt.Printf("[!] %s\n", err.Error())
  35. t.FailNow()
  36. } else if len(u) != 36 {
  37. fmt.Printf("[!] UUID length mismatch: %s\n", u)
  38. t.FailNow()
  39. } else if !uuidVersion4Regex.MatchString(u) {
  40. fmt.Printf("[!] invalid UUID: %s\n", u)
  41. t.FailNow()
  42. }
  43. fmt.Printf("\t[*] generate UUID: %s\n", u)
  44. }