This repository has no description
1package models
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestCacheEntryValidate(t *testing.T) {
9 valid := CacheEntry{Key: "go-mod-v1", Hash: []string{"go.sum", "sub/dir/package-lock.json"}, Paths: []string{"/workspace/go/pkg/mod", "/root/.cache"}}
10 if err := valid.Validate(); err != nil {
11 t.Fatalf("valid entry: %v", err)
12 }
13
14 cases := []struct {
15 name string
16 entry CacheEntry
17 want string
18 }{
19 {"empty key", CacheEntry{Key: "", Paths: []string{"/x"}}, "invalid key"},
20 {"key with slash", CacheEntry{Key: "a/b", Paths: []string{"/x"}}, "invalid key"},
21 {"no paths", CacheEntry{Key: "ok"}, "no paths"},
22 {"empty path", CacheEntry{Key: "ok", Paths: []string{""}}, "empty path"},
23 {"path with space", CacheEntry{Key: "ok", Paths: []string{"/my dir"}}, "unsupported characters"},
24 {"path with quote", CacheEntry{Key: "ok", Paths: []string{"/x'$(rm)"}}, "unsupported characters"},
25 {"path traversal", CacheEntry{Key: "ok", Paths: []string{"/x/../y"}}, ".."},
26 {"duplicate path", CacheEntry{Key: "ok", Paths: []string{"/x", "/x"}}, "duplicate"},
27 {"level too high", CacheEntry{Key: "ok", Paths: []string{"/x"}, CompressionLevel: 20}, "out of range"},
28 {"negative level", CacheEntry{Key: "ok", Paths: []string{"/x"}, CompressionLevel: -1}, "out of range"},
29 {"bad when", CacheEntry{Key: "ok", Paths: []string{"/x"}, When: "sometimes"}, "not one of"},
30 {"absolute hash path", CacheEntry{Key: "ok", Hash: []string{"/go.sum"}, Paths: []string{"/x"}}, "not repo-relative"},
31 {"empty hash path", CacheEntry{Key: "ok", Hash: []string{""}, Paths: []string{"/x"}}, "not repo-relative"},
32 {"hash path traversal", CacheEntry{Key: "ok", Hash: []string{"../secret"}, Paths: []string{"/x"}}, "not repo-relative"},
33 {"hash path inner traversal", CacheEntry{Key: "ok", Hash: []string{"a/../../b"}, Paths: []string{"/x"}}, "unsupported characters"},
34 {"hash path with colon", CacheEntry{Key: "ok", Hash: []string{"rev:go.sum"}, Paths: []string{"/x"}}, "unsupported characters"},
35 {"hash path with space", CacheEntry{Key: "ok", Hash: []string{"my lock"}, Paths: []string{"/x"}}, "unsupported characters"},
36 {"hash path with glob", CacheEntry{Key: "ok", Hash: []string{"*.sum"}, Paths: []string{"/x"}}, "unsupported characters"},
37 }
38 for _, tc := range cases {
39 err := tc.entry.Validate()
40 if err == nil || !strings.Contains(err.Error(), tc.want) {
41 t.Errorf("%s: got %v, want error containing %q", tc.name, err, tc.want)
42 }
43 }
44
45 ok := CacheEntry{Key: "go-mod", Hash: []string{"go.sum"}, Paths: []string{"node_modules", "/root/.cache"}, CompressionLevel: 19, When: "always"}
46 if err := ok.Validate(); err != nil {
47 t.Errorf("relative and absolute paths should validate, got %v", err)
48 }
49}