package storage import ( "errors" "path/filepath" "testing" ) func newTestStore(t *testing.T) *Store { t.Helper() dir := t.TempDir() return New(filepath.Join(dir, "layers"), filepath.Join(dir, "uploads")) } func TestNew(t *testing.T) { s := newTestStore(t) if s == nil { t.Fatal("New returned nil") } if s.layersPath == "" { t.Fatal("layersPath is empty") } if s.uploadsPath == "" { t.Fatal("uploadsPath is empty") } } func TestValidateDigest(t *testing.T) { valid := []string{ "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "sha256:0000000000000000000000000000000000000000000000000000000000000000", "sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789", } for _, d := range valid { if err := validateDigest(d); err != nil { t.Errorf("validateDigest(%q) = %v, want nil", d, err) } } invalid := []string{ "", "sha256:", "sha256:abc", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85", // 63 chars "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8555", // 65 chars "sha256:E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855", // uppercase "md5:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", // wrong algo "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", // missing prefix "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85g", // non-hex char } for _, d := range invalid { if err := validateDigest(d); !errors.Is(err, ErrInvalidDigest) { t.Errorf("validateDigest(%q) = %v, want ErrInvalidDigest", d, err) } } } func TestBlobPath(t *testing.T) { s := New("/data/layers", "/data/uploads") digest := "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" got := s.blobPath(digest) want := filepath.Join("/data/layers", "sha256", "e3", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") if got != want { t.Fatalf("blobPath(%q)\n got %q\nwant %q", digest, got, want) } }