|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-github/v67/github" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + |
| 14 | + "github.com/trufflesecurity/trufflehog/v3/pkg/context" |
| 15 | +) |
| 16 | + |
| 17 | +func testClientForServer(t *testing.T, serverURL string) *github.Client { |
| 18 | + t.Helper() |
| 19 | + client := github.NewClient(nil) |
| 20 | + base, err := url.Parse(serverURL + "/") |
| 21 | + assert.NoError(t, err) |
| 22 | + client.BaseURL = base |
| 23 | + return client |
| 24 | +} |
| 25 | + |
| 26 | +// downloadByPathServer answers the exact-path contents lookup, the raw |
| 27 | +// download it points at, and the repository lookup repoReachable makes. |
| 28 | +func downloadByPathServer(t *testing.T, fileStatus, rawStatus, repoStatus int) *httptest.Server { |
| 29 | + t.Helper() |
| 30 | + var server *httptest.Server |
| 31 | + server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 32 | + switch r.URL.Path { |
| 33 | + case "/repos/o/r/contents/dir/f.txt": |
| 34 | + w.Header().Set("Content-Type", "application/json") |
| 35 | + w.WriteHeader(fileStatus) |
| 36 | + if fileStatus == http.StatusOK { |
| 37 | + _, _ = fmt.Fprintf(w, `{"type":"file","name":"f.txt","path":"dir/f.txt","download_url":"%s/raw/dir/f.txt"}`, server.URL) |
| 38 | + return |
| 39 | + } |
| 40 | + _, _ = w.Write([]byte(`{"message":"Not Found"}`)) |
| 41 | + case "/raw/dir/f.txt": |
| 42 | + w.WriteHeader(rawStatus) |
| 43 | + if rawStatus == http.StatusOK { |
| 44 | + _, _ = w.Write([]byte("file body")) |
| 45 | + return |
| 46 | + } |
| 47 | + _, _ = w.Write([]byte("raw host error page")) |
| 48 | + case "/repos/o/r": |
| 49 | + w.Header().Set("Content-Type", "application/json") |
| 50 | + w.WriteHeader(repoStatus) |
| 51 | + if repoStatus == http.StatusOK { |
| 52 | + _, _ = w.Write([]byte(`{"id":1,"name":"r","full_name":"o/r"}`)) |
| 53 | + return |
| 54 | + } |
| 55 | + _, _ = w.Write([]byte(`{"message":"Not Found"}`)) |
| 56 | + default: |
| 57 | + t.Errorf("unexpected request: %s", r.URL.Path) |
| 58 | + w.WriteHeader(http.StatusInternalServerError) |
| 59 | + } |
| 60 | + })) |
| 61 | + return server |
| 62 | +} |
| 63 | + |
| 64 | +func TestDownloadContentsByPathFetchesFileBody(t *testing.T) { |
| 65 | + server := downloadByPathServer(t, http.StatusOK, http.StatusOK, http.StatusOK) |
| 66 | + defer server.Close() |
| 67 | + |
| 68 | + s := &Source{} |
| 69 | + client := testClientForServer(t, server.URL) |
| 70 | + rc, resp, err := s.downloadContentsByPath(context.Background(), client, "o", "r", "dir/f.txt", "abc123") |
| 71 | + assert.NoError(t, err) |
| 72 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 73 | + body, err := io.ReadAll(rc) |
| 74 | + assert.NoError(t, err) |
| 75 | + assert.NoError(t, rc.Close()) |
| 76 | + assert.Equal(t, "file body", string(body)) |
| 77 | +} |
| 78 | + |
| 79 | +func TestDownloadContentsByPathReturns404Response(t *testing.T) { |
| 80 | + server := downloadByPathServer(t, http.StatusNotFound, http.StatusOK, http.StatusOK) |
| 81 | + defer server.Close() |
| 82 | + |
| 83 | + s := &Source{} |
| 84 | + client := testClientForServer(t, server.URL) |
| 85 | + rc, resp, err := s.downloadContentsByPath(context.Background(), client, "o", "r", "dir/f.txt", "abc123") |
| 86 | + assert.Error(t, err) |
| 87 | + assert.Nil(t, rc) |
| 88 | + // The 404 is the exact-path lookup's, which the caller combines with |
| 89 | + // repoReachable to classify the target as gone. |
| 90 | + assert.NotNil(t, resp) |
| 91 | + assert.Equal(t, http.StatusNotFound, resp.StatusCode) |
| 92 | +} |
| 93 | + |
| 94 | +func TestDownloadContentsByPathNon404Failure(t *testing.T) { |
| 95 | + server := downloadByPathServer(t, http.StatusForbidden, http.StatusOK, http.StatusOK) |
| 96 | + defer server.Close() |
| 97 | + |
| 98 | + s := &Source{} |
| 99 | + client := testClientForServer(t, server.URL) |
| 100 | + _, resp, err := s.downloadContentsByPath(context.Background(), client, "o", "r", "dir/f.txt", "abc123") |
| 101 | + assert.Error(t, err) |
| 102 | + assert.NotNil(t, resp) |
| 103 | + assert.Equal(t, http.StatusForbidden, resp.StatusCode) |
| 104 | +} |
| 105 | + |
| 106 | +func TestRepoReachable(t *testing.T) { |
| 107 | + server := downloadByPathServer(t, http.StatusNotFound, http.StatusOK, http.StatusOK) |
| 108 | + defer server.Close() |
| 109 | + |
| 110 | + s := &Source{} |
| 111 | + client := testClientForServer(t, server.URL) |
| 112 | + assert.True(t, s.repoReachable(context.Background(), client, "o", "r")) |
| 113 | +} |
| 114 | + |
| 115 | +func TestRepoNotReachable(t *testing.T) { |
| 116 | + server := downloadByPathServer(t, http.StatusNotFound, http.StatusOK, http.StatusNotFound) |
| 117 | + defer server.Close() |
| 118 | + |
| 119 | + s := &Source{} |
| 120 | + client := testClientForServer(t, server.URL) |
| 121 | + // The repo 404s too: could be lost access rather than deletion, so the |
| 122 | + // caller must not classify the target as gone. |
| 123 | + assert.False(t, s.repoReachable(context.Background(), client, "o", "r")) |
| 124 | +} |
| 125 | + |
| 126 | +func TestRepoReachableServerDown(t *testing.T) { |
| 127 | + server := downloadByPathServer(t, http.StatusNotFound, http.StatusOK, http.StatusOK) |
| 128 | + server.Close() |
| 129 | + |
| 130 | + s := &Source{} |
| 131 | + client := testClientForServer(t, server.URL) |
| 132 | + assert.False(t, s.repoReachable(context.Background(), client, "o", "r")) |
| 133 | +} |
| 134 | + |
| 135 | +func TestDownloadContentsByPathNon200Download(t *testing.T) { |
| 136 | + server := downloadByPathServer(t, http.StatusOK, http.StatusInternalServerError, http.StatusOK) |
| 137 | + defer server.Close() |
| 138 | + |
| 139 | + s := &Source{} |
| 140 | + client := testClientForServer(t, server.URL) |
| 141 | + rc, resp, err := s.downloadContentsByPath(context.Background(), client, "o", "r", "dir/f.txt", "abc123") |
| 142 | + assert.Error(t, err) |
| 143 | + assert.ErrorContains(t, err, "unexpected HTTP response status") |
| 144 | + assert.Nil(t, rc) |
| 145 | + // The returned response is the exact-path lookup's (200), not the failed |
| 146 | + // download's, so the caller cannot mistake a download failure for the |
| 147 | + // target being gone. |
| 148 | + assert.NotNil(t, resp) |
| 149 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 150 | +} |
0 commit comments