ref_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package blob
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. var testRefs = map[string]Ref{
  7. "mistral:latest": {name: "mistral", tag: "latest"},
  8. "mistral": {name: "mistral"},
  9. "mistral:30B": {name: "mistral", tag: "30B"},
  10. "mistral:7b": {name: "mistral", tag: "7b"},
  11. "mistral:7b+Q4_0": {name: "mistral", tag: "7b", build: "Q4_0"},
  12. "mistral+KQED": {name: "mistral", build: "KQED"},
  13. "mistral.x-3:7b+Q4_0": {name: "mistral.x-3", tag: "7b", build: "Q4_0"},
  14. "mistral:7b+q4_0": {name: "mistral", tag: "7b", build: "Q4_0"},
  15. "llama2": {name: "llama2"},
  16. // invalid (includes fuzzing trophies)
  17. "mistral:7b+Q4_0:latest": {},
  18. "mi tral": {},
  19. "x/y/z/foo": {},
  20. "/0": {},
  21. "0 /0": {},
  22. "0 /": {},
  23. "0/": {},
  24. ":": {},
  25. ":/0": {},
  26. "+0/00000": {},
  27. "0+.\xf2\x80\xf6\x9d00000\xe5\x99\xe6\xd900\xd90\xa60\x91\xdc0\xff\xbf\x99\xe800\xb9\xdc\xd6\xc300\x970\xfb\xfd0\xe0\x8a\xe1\xad\xd40\x9700\xa80\x980\xdd0000\xb00\x91000\xfe0\x89\x9b\x90\x93\x9f0\xe60\xf7\x84\xb0\x87\xa5\xff0\xa000\x9a\x85\xf6\x85\xfe\xa9\xf9\xe9\xde00\xf4\xe0\x8f\x81\xad\xde00\xd700\xaa\xe000000\xb1\xee0\x91": {},
  28. "0//0": {},
  29. "m+^^^": {},
  30. "file:///etc/passwd": {},
  31. "file:///etc/passwd:latest": {},
  32. "file:///etc/passwd:latest+u": {},
  33. strings.Repeat("a", MaxRefLength): {name: strings.Repeat("a", MaxRefLength)},
  34. strings.Repeat("a", MaxRefLength+1): {},
  35. }
  36. func TestRefParts(t *testing.T) {
  37. const wantNumParts = 5
  38. var ref Ref
  39. if len(ref.Parts()) != wantNumParts {
  40. t.Errorf("Parts() = %d; want %d", len(ref.Parts()), wantNumParts)
  41. }
  42. }
  43. func TestParseRef(t *testing.T) {
  44. for s, want := range testRefs {
  45. for _, prefix := range []string{"", "https://", "http://"} {
  46. // We should get the same results with or without the
  47. // http(s) prefixes
  48. s := prefix + s
  49. t.Run(s, func(t *testing.T) {
  50. got := ParseRef(s)
  51. if got != want {
  52. t.Errorf("ParseRef(%q) = %q; want %q", s, got, want)
  53. }
  54. // test round-trip
  55. if ParseRef(got.String()) != got {
  56. t.Errorf("String() = %s; want %s", got.String(), s)
  57. }
  58. })
  59. }
  60. }
  61. }
  62. func TestRefFull(t *testing.T) {
  63. const empty = "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/!(MISSING NAME):!(MISSING TAG)+!(MISSING BUILD)"
  64. cases := []struct {
  65. in string
  66. wantFull string
  67. }{
  68. {"", empty},
  69. {"example.com/mistral:7b+x", "!(MISSING DOMAIN)/example.com/mistral:7b+X"},
  70. {"example.com/mistral:7b+Q4_0", "!(MISSING DOMAIN)/example.com/mistral:7b+Q4_0"},
  71. {"example.com/x/mistral:latest", "example.com/x/mistral:latest+!(MISSING BUILD)"},
  72. {"example.com/x/mistral:latest+Q4_0", "example.com/x/mistral:latest+Q4_0"},
  73. {"mistral:7b+x", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+X"},
  74. {"mistral:7b+q4_0", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+Q4_0"},
  75. {"mistral:7b+Q4_0", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+Q4_0"},
  76. {"mistral:latest", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:latest+!(MISSING BUILD)"},
  77. {"mistral", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:!(MISSING TAG)+!(MISSING BUILD)"},
  78. {"mistral:30b", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:30b+!(MISSING BUILD)"},
  79. }
  80. for _, tt := range cases {
  81. t.Run(tt.in, func(t *testing.T) {
  82. ref := ParseRef(tt.in)
  83. t.Logf("ParseRef(%q) = %#v", tt.in, ref)
  84. if g := ref.Full(); g != tt.wantFull {
  85. t.Errorf("Full(%q) = %q; want %q", tt.in, g, tt.wantFull)
  86. }
  87. })
  88. }
  89. }
  90. func TestParseRefAllocs(t *testing.T) {
  91. // test allocations
  92. var r Ref
  93. allocs := testing.AllocsPerRun(1000, func() {
  94. r = ParseRef("example.com/mistral:7b+Q4_0")
  95. })
  96. _ = r
  97. if allocs > 0 {
  98. t.Errorf("ParseRef allocs = %v; want 0", allocs)
  99. }
  100. }
  101. func BenchmarkParseRef(b *testing.B) {
  102. b.ReportAllocs()
  103. var r Ref
  104. for i := 0; i < b.N; i++ {
  105. r = ParseRef("example.com/mistral:7b+Q4_0")
  106. }
  107. _ = r
  108. }
  109. func FuzzParseRef(f *testing.F) {
  110. f.Add("example.com/mistral:7b+Q4_0")
  111. f.Add("example.com/mistral:7b+q4_0")
  112. f.Add("example.com/mistral:7b+x")
  113. f.Add("x/y/z:8n+I")
  114. f.Fuzz(func(t *testing.T, s string) {
  115. r0 := ParseRef(s)
  116. if !r0.Valid() {
  117. if r0 != (Ref{}) {
  118. t.Errorf("expected invalid ref to be zero value; got %#v", r0)
  119. }
  120. t.Skipf("invalid ref: %q", s)
  121. }
  122. if !strings.EqualFold(r0.String(), s) {
  123. t.Errorf("String() did not round-trip with case insensitivity: %q\ngot = %q\nwant = %q", s, r0.String(), s)
  124. }
  125. r1 := ParseRef(r0.String())
  126. if r0 != r1 {
  127. t.Errorf("round-trip mismatch: %+v != %+v", r0, r1)
  128. }
  129. })
  130. }