| index e7fc751..a99aae7 100644 |
| --- a/internal/slidestest/library_test.go |
| +++ b/internal/slidestest/library_test.go |
| @@ -16,7 +16,6 @@ import ( |
| "strings" |
| "sync" |
| "testing" |
| - "time" |
| |
| "amadan.net/oficina/slides/internal/slides" |
| "amadan.net/rastrillo/rastrillo/blobs" |
| @@ -317,18 +316,41 @@ func TestTheAccountMenuListsFonts(t *testing.T) { |
| |
| // gate is a SetFontTrace hook that parks the first request to reach |
| // stage until release is called; reached is closed when it has. |
| +// |
| +// waiter, if set, is a second stage whose Nth arrival (nth, counting |
| +// from 1) closes arrived: the competing request, confirmed to be at its |
| +// point rather than guessed at with a sleep. |
| type gate struct { |
| stage string |
| reached chan struct{} |
| hold chan struct{} |
| once sync.Once |
| + |
| + waiter string |
| + nth int |
| + seen int |
| + arrived chan struct{} |
| + mu sync.Mutex |
| } |
| |
| func newGate(stage string) *gate { |
| - return &gate{stage: stage, reached: make(chan struct{}), hold: make(chan struct{})} |
| + return &gate{stage: stage, reached: make(chan struct{}), hold: make(chan struct{}), arrived: make(chan struct{})} |
| +} |
| + |
| +func (g *gate) waitFor(stage string, nth int) *gate { |
| + g.waiter, g.nth = stage, nth |
| + return g |
| } |
| |
| func (g *gate) trace(stage string) { |
| + if stage == g.waiter { |
| + g.mu.Lock() |
| + g.seen++ |
| + if g.seen == g.nth { |
| + close(g.arrived) |
| + } |
| + g.mu.Unlock() |
| + } |
| if stage != g.stage { |
| return |
| } |
| @@ -356,7 +378,7 @@ func TestConcurrentUploadsRespectTheFamilyLimit(t *testing.T) { |
| t.Fatal(err) |
| } |
| } |
| - g := newGate("checked") |
| + g := newGate("checked").waitFor("queued", 2) |
| a.app.SetFontTrace(g.trace) |
| t.Cleanup(func() { a.app.SetFontTrace(nil) }) |
| |
| @@ -368,7 +390,7 @@ func TestConcurrentUploadsRespectTheFamilyLimit(t *testing.T) { |
| go func() { |
| codes <- uploadFonts(t, a, cookies, upFile{"b.ttf", libFont(t, "LiberationSans-Bold.ttf")}).Code |
| }() |
| - time.Sleep(100 * time.Millisecond) // the second is waiting its turn |
| + <-g.arrived // the second upload is at the lock |
| g.release() |
| c1, c2 := <-codes, <-codes |
| if n := countRows(t, a, "1 = 1"); n != 16 { |
| @@ -415,7 +437,7 @@ func TestADeleteCannotTakeAnUploadsObject(t *testing.T) { |
| t.Fatalf("second person's upload: %d", rec.Code) |
| } |
| |
| - g := newGate("stored") |
| + g := newGate("stored").waitFor("delete", 1) |
| a.app.SetFontTrace(g.trace) |
| t.Cleanup(func() { a.app.SetFontTrace(nil) }) |
| done := make(chan int, 1) |
| @@ -425,7 +447,7 @@ func TestADeleteCannotTakeAnUploadsObject(t *testing.T) { |
| go func() { |
| deleted <- post(t, a, "/fonts/delete", url.Values{"family": {"Liberation Sans"}}, two...).Code |
| }() |
| - time.Sleep(100 * time.Millisecond) |
| + <-g.arrived // the delete is at the lock |
| g.release() |
| if c := <-done; c != http.StatusOK { |
| t.Fatalf("upload: %d", c) |
| @@ -472,3 +494,28 @@ func TestAMalformedMultipartStreamIsNotSilentlyDone(t *testing.T) { |
| t.Errorf("a malformed part after a file says nothing:\n%s", rec.Body) |
| } |
| } |
| + |
| +// A valid file followed by a part cut off mid-body: Go's reader wraps |
| +// io.EOF for that, and it is not the end of the stream. |
| +func TestATruncatedMultipartStreamIsNotSilentlyDone(t *testing.T) { |
| + a := newAppWithStore(t, dirStore(t)) |
| + cookies := signedIn(t, a, allowedAddress) |
| + good := libFont(t, "LiberationSans-Regular.ttf") |
| + body := "--XX\r\nContent-Disposition: form-data; name=\"file\"; filename=\"a.ttf\"\r\n\r\n" + string(good) + "\r\n" + |
| + "--XX\r\nContent-Disposition: form-data; name=\"file\"; filename=\"b.ttf\"\r\n\r\nabc" |
| + req := httptest.NewRequest(http.MethodPost, "/fonts", strings.NewReader(body)) |
| + req.Header.Set("Content-Type", "multipart/form-data; boundary=XX") |
| + req.Header.Set("Origin", testOrigin) |
| + req.Header.Set("Sec-Fetch-Site", "same-origin") |
| + for _, c := range cookies { |
| + req.AddCookie(c) |
| + } |
| + rec := httptest.NewRecorder() |
| + a.ServeHTTP(rec, req) |
| + if n := countRows(t, a, "1 = 1"); n != 1 { |
| + t.Errorf("the complete file was not kept: %d rows", n) |
| + } |
| + if !strings.Contains(html.UnescapeString(rec.Body.String()), "That didn’t work") { |
| + t.Errorf("a truncated part after a file says nothing:\n%s", rec.Body) |
| + } |
| +} |