stringid_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package stringid // import "github.com/docker/docker/pkg/stringid"
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestGenerateRandomID(t *testing.T) {
  7. id := GenerateRandomID()
  8. if len(id) != fullLen {
  9. t.Fatalf("Id returned is incorrect: %s", id)
  10. }
  11. }
  12. func TestShortenId(t *testing.T) {
  13. id := "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2"
  14. truncID := TruncateID(id)
  15. if truncID != "90435eec5c4e" {
  16. t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
  17. }
  18. }
  19. func TestShortenSha256Id(t *testing.T) {
  20. id := "sha256:4e38e38c8ce0b8d9041a9c4fefe786631d1416225e13b0bfe8cfa2321aec4bba"
  21. truncID := TruncateID(id)
  22. if truncID != "4e38e38c8ce0" {
  23. t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
  24. }
  25. }
  26. func TestShortenIdEmpty(t *testing.T) {
  27. id := ""
  28. truncID := TruncateID(id)
  29. if len(truncID) > len(id) {
  30. t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
  31. }
  32. }
  33. func TestShortenIdInvalid(t *testing.T) {
  34. id := "1234"
  35. truncID := TruncateID(id)
  36. if len(truncID) != len(id) {
  37. t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
  38. }
  39. }
  40. func TestIsShortIDNonHex(t *testing.T) {
  41. id := "some non-hex value"
  42. if IsShortID(id) {
  43. t.Fatalf("%s is not a short ID", id)
  44. }
  45. }
  46. func TestIsShortIDNotCorrectSize(t *testing.T) {
  47. id := strings.Repeat("a", shortLen+1)
  48. if IsShortID(id) {
  49. t.Fatalf("%s is not a short ID", id)
  50. }
  51. id = strings.Repeat("a", shortLen-1)
  52. if IsShortID(id) {
  53. t.Fatalf("%s is not a short ID", id)
  54. }
  55. }
  56. var testIDs = []string{
  57. "4e38e38c8ce0",
  58. strings.Repeat("a", shortLen+1),
  59. strings.Repeat("a", 16000),
  60. "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
  61. }
  62. func BenchmarkIsShortID(b *testing.B) {
  63. b.ReportAllocs()
  64. for i := 0; i < b.N; i++ {
  65. for _, id := range testIDs {
  66. _ = IsShortID(id)
  67. }
  68. }
  69. }
  70. func BenchmarkValidateID(b *testing.B) {
  71. b.ReportAllocs()
  72. for i := 0; i < b.N; i++ {
  73. for _, id := range testIDs {
  74. _ = ValidateID(id)
  75. }
  76. }
  77. }