|
| 1 | +// SPDX-FileCopyrightText: the secureCodeBox authors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +const testFileContents = `{"findings": []}` |
| 18 | + |
| 19 | +// writeResultFile creates a temporary scan result file and returns its path. |
| 20 | +func writeResultFile(t *testing.T) string { |
| 21 | + t.Helper() |
| 22 | + |
| 23 | + path := filepath.Join(t.TempDir(), "findings.json") |
| 24 | + if err := os.WriteFile(path, []byte(testFileContents), 0o600); err != nil { |
| 25 | + t.Fatalf("failed to write test file: %v", err) |
| 26 | + } |
| 27 | + return path |
| 28 | +} |
| 29 | + |
| 30 | +// stubSleep replaces the backoff sleep with a recorder, so that tests don't |
| 31 | +// have to actually wait. Returns a pointer to the recorded delays. |
| 32 | +func stubSleep(t *testing.T) *[]time.Duration { |
| 33 | + t.Helper() |
| 34 | + |
| 35 | + original := sleep |
| 36 | + delays := []time.Duration{} |
| 37 | + sleep = func(d time.Duration) { |
| 38 | + delays = append(delays, d) |
| 39 | + } |
| 40 | + t.Cleanup(func() { sleep = original }) |
| 41 | + |
| 42 | + return &delays |
| 43 | +} |
| 44 | + |
| 45 | +func TestUploadFileWithRetriesSucceedsOnFirstAttempt(t *testing.T) { |
| 46 | + delays := stubSleep(t) |
| 47 | + |
| 48 | + requests := 0 |
| 49 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 50 | + requests++ |
| 51 | + w.WriteHeader(http.StatusOK) |
| 52 | + })) |
| 53 | + defer server.Close() |
| 54 | + |
| 55 | + if err := uploadFileWithRetries(writeResultFile(t), server.URL); err != nil { |
| 56 | + t.Fatalf("expected upload to succeed, got: %v", err) |
| 57 | + } |
| 58 | + if requests != 1 { |
| 59 | + t.Errorf("expected 1 request, got %d", requests) |
| 60 | + } |
| 61 | + if len(*delays) != 0 { |
| 62 | + t.Errorf("expected no backoff, got %v", *delays) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestUploadFileWithRetriesRetriesUntilSuccess(t *testing.T) { |
| 67 | + delays := stubSleep(t) |
| 68 | + |
| 69 | + requests := 0 |
| 70 | + receivedBodies := []string{} |
| 71 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 72 | + requests++ |
| 73 | + body, err := io.ReadAll(r.Body) |
| 74 | + if err != nil { |
| 75 | + t.Errorf("failed to read request body: %v", err) |
| 76 | + } |
| 77 | + receivedBodies = append(receivedBodies, string(body)) |
| 78 | + |
| 79 | + if requests < 3 { |
| 80 | + w.WriteHeader(http.StatusInternalServerError) |
| 81 | + return |
| 82 | + } |
| 83 | + w.WriteHeader(http.StatusOK) |
| 84 | + })) |
| 85 | + defer server.Close() |
| 86 | + |
| 87 | + if err := uploadFileWithRetries(writeResultFile(t), server.URL); err != nil { |
| 88 | + t.Fatalf("expected upload to succeed after retries, got: %v", err) |
| 89 | + } |
| 90 | + if requests != 3 { |
| 91 | + t.Errorf("expected 3 requests, got %d", requests) |
| 92 | + } |
| 93 | + |
| 94 | + // every attempt, especially the last one, has to send the complete file |
| 95 | + for attempt, body := range receivedBodies { |
| 96 | + if body != testFileContents { |
| 97 | + t.Errorf("attempt %d uploaded %q, expected %q", attempt+1, body, testFileContents) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + expectedDelays := uploadBackoffs[:2] |
| 102 | + if len(*delays) != len(expectedDelays) { |
| 103 | + t.Fatalf("expected %d backoffs, got %v", len(expectedDelays), *delays) |
| 104 | + } |
| 105 | + for i, delay := range *delays { |
| 106 | + if delay != expectedDelays[i] { |
| 107 | + t.Errorf("backoff %d was %s, expected %s", i+1, delay, expectedDelays[i]) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestUploadFileWithRetriesGivesUpAfterAllAttempts(t *testing.T) { |
| 113 | + delays := stubSleep(t) |
| 114 | + |
| 115 | + requests := 0 |
| 116 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 117 | + requests++ |
| 118 | + w.WriteHeader(http.StatusInternalServerError) |
| 119 | + })) |
| 120 | + defer server.Close() |
| 121 | + |
| 122 | + if err := uploadFileWithRetries(writeResultFile(t), server.URL); err == nil { |
| 123 | + t.Fatal("expected upload to fail") |
| 124 | + } |
| 125 | + |
| 126 | + expectedRequests := len(uploadBackoffs) + 1 |
| 127 | + if requests != expectedRequests { |
| 128 | + t.Errorf("expected %d requests, got %d", expectedRequests, requests) |
| 129 | + } |
| 130 | + if len(*delays) != len(uploadBackoffs) { |
| 131 | + t.Errorf("expected %d backoffs, got %v", len(uploadBackoffs), *delays) |
| 132 | + } |
| 133 | + for i, delay := range *delays { |
| 134 | + if delay != uploadBackoffs[i] { |
| 135 | + t.Errorf("backoff %d was %s, expected %s", i+1, delay, uploadBackoffs[i]) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestUploadFileWithRetriesRetriesTransportErrors(t *testing.T) { |
| 141 | + delays := stubSleep(t) |
| 142 | + |
| 143 | + // closing the server up front makes every attempt fail on the transport level |
| 144 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) |
| 145 | + url := server.URL |
| 146 | + server.Close() |
| 147 | + |
| 148 | + if err := uploadFileWithRetries(writeResultFile(t), url); err == nil { |
| 149 | + t.Fatal("expected upload to fail") |
| 150 | + } |
| 151 | + if len(*delays) != len(uploadBackoffs) { |
| 152 | + t.Errorf("expected %d backoffs, got %v", len(uploadBackoffs), *delays) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestUploadFileWithRetriesFailsForMissingFile(t *testing.T) { |
| 157 | + stubSleep(t) |
| 158 | + |
| 159 | + if err := uploadFileWithRetries(filepath.Join(t.TempDir(), "does-not-exist.json"), "http://127.0.0.1:1"); err == nil { |
| 160 | + t.Fatal("expected upload of a missing file to fail") |
| 161 | + } |
| 162 | +} |
0 commit comments