|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/git-pkgs/registries/fetch" |
| 13 | +) |
| 14 | + |
| 15 | +func TestComposerDownloadVerifiesDeclaredShasum(t *testing.T) { |
| 16 | + const archive = "archive bytes" |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + shasum string |
| 21 | + wantStatus int |
| 22 | + }{ |
| 23 | + {"matching shasum", sha1Hex(archive), http.StatusOK}, |
| 24 | + {"mismatched shasum", sha1Hex("other bytes"), http.StatusBadGateway}, |
| 25 | + {"empty shasum", "", http.StatusOK}, |
| 26 | + {"unparseable shasum", "not-a-sha1", http.StatusOK}, |
| 27 | + } |
| 28 | + |
| 29 | + for _, tt := range tests { |
| 30 | + t.Run(tt.name, func(t *testing.T) { |
| 31 | + h, proxy := setupComposerShasum(t, "1.0.0", "/zip/p.zip", tt.shasum, map[string]string{"/zip/p.zip": archive}) |
| 32 | + |
| 33 | + w := httptest.NewRecorder() |
| 34 | + h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/files/v/p/1.0.0/p.zip", nil)) |
| 35 | + if w.Code != tt.wantStatus { |
| 36 | + t.Fatalf("status = %d, want %d; body: %s", w.Code, tt.wantStatus, w.Body.String()) |
| 37 | + } |
| 38 | + if wantCached := tt.wantStatus == http.StatusOK; isComposerArchiveCached(t, proxy, "1.0.0", "p.zip") != wantCached { |
| 39 | + t.Errorf("cached = %v, want %v", !wantCached, wantCached) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// TestComposerDownloadPinnedCommitIgnoresListedShasum checks that the shasum |
| 46 | +// listed for a branch's head is not held against an older commit a lock file |
| 47 | +// pins: it describes another archive. |
| 48 | +func TestComposerDownloadPinnedCommitIgnoresListedShasum(t *testing.T) { |
| 49 | + h, _ := setupComposerShasum(t, "dev-master", "/zip/"+composerCommitB, sha1Hex("commit B"), map[string]string{ |
| 50 | + "/zip/" + composerCommitA: "commit A", |
| 51 | + "/zip/" + composerCommitB: "commit B", |
| 52 | + }) |
| 53 | + |
| 54 | + w := httptest.NewRecorder() |
| 55 | + h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/files/v/p/dev-master/"+composerCommitA+".zip", nil)) |
| 56 | + if w.Code != http.StatusOK || w.Body.String() != "commit A" { |
| 57 | + t.Errorf("status %d, body %q, want 200 %q", w.Code, w.Body.String(), "commit A") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func setupComposerShasum(t *testing.T, version, distPath, shasum string, archives map[string]string) (http.Handler, *Proxy) { |
| 62 | + t.Helper() |
| 63 | + h, proxy, _ := setupComposerShasumCounting(t, version, distPath, shasum, archives) |
| 64 | + return h, proxy |
| 65 | +} |
| 66 | + |
| 67 | +// setupComposerShasumCounting is setupComposerShasum that also reports how |
| 68 | +// many times each archive path was fetched from upstream. |
| 69 | +func setupComposerShasumCounting(t *testing.T, version, distPath, shasum string, archives map[string]string) (http.Handler, *Proxy, func(string) int) { |
| 70 | + t.Helper() |
| 71 | + var mu sync.Mutex |
| 72 | + fetched := map[string]int{} |
| 73 | + var srv *httptest.Server |
| 74 | + srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 75 | + if r.URL.Path == "/p2/v/p.json" || r.URL.Path == "/p2/v/p~dev.json" { |
| 76 | + _, _ = fmt.Fprintf(w, `{"packages":{"v/p":[{"version":%q,"dist":{"url":%q,"type":"zip","shasum":%q}}]}}`, |
| 77 | + version, srv.URL+distPath, shasum) |
| 78 | + return |
| 79 | + } |
| 80 | + body, ok := archives[r.URL.Path] |
| 81 | + if !ok { |
| 82 | + http.NotFound(w, r) |
| 83 | + return |
| 84 | + } |
| 85 | + mu.Lock() |
| 86 | + fetched[r.URL.Path]++ |
| 87 | + mu.Unlock() |
| 88 | + _, _ = io.WriteString(w, body) |
| 89 | + })) |
| 90 | + t.Cleanup(srv.Close) |
| 91 | + |
| 92 | + proxy, _, _, _ := setupTestProxy(t) |
| 93 | + fetcher := fetch.NewFetcher(fetch.WithHTTPClient(srv.Client()), fetch.WithMaxRetries(0)) |
| 94 | + proxy.Fetcher = fetcher |
| 95 | + t.Cleanup(func() { _ = fetcher.Close() }) |
| 96 | + return NewComposerHandlerWithUpstreams(proxy, "http://proxy", srv.URL, srv.URL).Routes(), proxy, func(path string) int { |
| 97 | + mu.Lock() |
| 98 | + defer mu.Unlock() |
| 99 | + return fetched[path] |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func isComposerArchiveCached(t *testing.T, proxy *Proxy, version, filename string) bool { |
| 104 | + t.Helper() |
| 105 | + cached, err := proxy.GetCachedArtifact(context.Background(), "composer", "v/p", version, filename) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("reading cache: %v", err) |
| 108 | + } |
| 109 | + if cached == nil { |
| 110 | + return false |
| 111 | + } |
| 112 | + _ = cached.Reader.Close() |
| 113 | + return true |
| 114 | +} |
| 115 | + |
| 116 | +// TestComposerDevBranchArchivesStayServableFromCache is a regression test: a |
| 117 | +// version's integrity is one value shared by all its cached archives, but a |
| 118 | +// dev branch caches one archive per commit. Recording the head's shasum made |
| 119 | +// a pinned older commit fail its check when served from the cache, so its |
| 120 | +// archive was discarded and fetched again, which in turn cleared the head's |
| 121 | +// recorded shasum. |
| 122 | +func TestComposerDevBranchArchivesStayServableFromCache(t *testing.T) { |
| 123 | + h, proxy, fetched := setupComposerShasumCounting(t, "dev-master", "/zip/"+composerCommitB, sha1Hex("commit B"), map[string]string{ |
| 124 | + "/zip/" + composerCommitA: "commit A", |
| 125 | + "/zip/" + composerCommitB: "commit B", |
| 126 | + }) |
| 127 | + |
| 128 | + for i, commit := range []string{composerCommitA, composerCommitB, composerCommitA, composerCommitB} { |
| 129 | + want := map[string]string{composerCommitA: "commit A", composerCommitB: "commit B"}[commit] |
| 130 | + w := httptest.NewRecorder() |
| 131 | + h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/files/v/p/dev-master/"+commit+".zip", nil)) |
| 132 | + if w.Code != http.StatusOK || w.Body.String() != want { |
| 133 | + t.Errorf("request %d for %s: status %d, body %q; want 200 %q", i+1, commit[:4], w.Code, w.Body.String(), want) |
| 134 | + } |
| 135 | + } |
| 136 | + for _, commit := range []string{composerCommitA, composerCommitB} { |
| 137 | + if n := fetched("/zip/" + commit); n != 1 { |
| 138 | + t.Errorf("commit %s fetched from upstream %d times, want once and then served from the cache", commit[:4], n) |
| 139 | + } |
| 140 | + if !isComposerArchiveCached(t, proxy, "dev-master", commit+".zip") { |
| 141 | + t.Errorf("commit %s is no longer cached", commit[:4]) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments