stringid_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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) != 64 {
  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. }