1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
)
// An end-to-end pass over a real HTTP server: drop a recording, follow the
// link from the notification, play it, and delete it. This exercises the
// routing, which the handler-level tests bypass by calling handlers directly.
func TestEndToEndSubmitReviewDelete(t *testing.T) {
s := newTestServer(t)
mux := http.NewServeMux()
mux.HandleFunc("POST /submit", s.handleSubmit)
mux.HandleFunc("GET /submit", s.handleSubmitStatus)
mux.HandleFunc("GET /inbox/{token}", s.handleReview)
mux.HandleFunc("GET /inbox/{token}/audio", s.handleReviewAudio)
mux.HandleFunc("POST /inbox/{token}/delete", s.handleReviewDelete)
srv := httptest.NewServer(mux)
defer srv.Close()
s.baseURL = srv.URL
// The inbox says it is open.
var st struct {
Open bool `json:"open"`
MaxBytes int64 `json:"maxBytes"`
}
resp, err := http.Get(srv.URL + "/submit")
if err != nil {
t.Fatal(err)
}
json.NewDecoder(resp.Body).Decode(&st)
resp.Body.Close()
if !st.Open {
t.Fatal("a fresh inbox reports itself closed")
}
// Drop a recording.
audio := m4aBytes(4096)
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
mw.WriteField("passphrase", testPassphrase)
mw.WriteField("note", "here is a thought")
fw, _ := mw.CreateFormFile("file", "reply.m4a")
fw.Write(audio)
mw.Close()
resp, err = http.Post(srv.URL+"/submit", mw.FormDataContentType(), &buf)
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("submit: status %d, body %s", resp.StatusCode, body)
}
// The submitter is told nothing about where it went: the token is only
// ever disclosed to the recipient, by mail.
if strings.Contains(string(body), "inbox/") {
t.Errorf("the submission response leaks the token: %s", body)
}
// Recover the token the way the notification would carry it.
var token string
if err := s.db.QueryRow(
`SELECT token FROM submission WHERE deleted_at IS NULL`).Scan(&token); err != nil {
t.Fatal(err)
}
// Follow the link, in the slashless form the notification mail carries.
pageURL, err := url.Parse(srv.URL + "/inbox/" + token)
if err != nil {
t.Fatal(err)
}
resp, err = http.Get(pageURL.String())
if err != nil {
t.Fatal(err)
}
page, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("review: status %d", resp.StatusCode)
}
if !strings.Contains(string(page), "here is a thought") {
t.Error("review page does not show the submitted note")
}
// From here on, use the URLs the page itself emits, resolved against the
// page's own address the way a browser does. Building them by hand here is
// what let a page whose links all 404ed pass this test.
audioURL := resolveFrom(t, pageURL, string(page), `<audio[^>]*\ssrc="([^"]+)"`, "audio src")
deleteURL := resolveFrom(t, pageURL, string(page), `<form[^>]*\saction="([^"]+)"`, "form action")
// Play it, and seek within it.
req, _ := http.NewRequest("GET", audioURL, nil)
req.Header.Set("Range", "bytes=10-109")
resp, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
part, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusPartialContent {
t.Fatalf("range request: status %d, want 206", resp.StatusCode)
}
if !bytes.Equal(part, audio[10:110]) {
t.Error("the served range does not match what was stored")
}
// Delete it.
resp, err = http.Post(deleteURL, "", nil)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("delete: status %d", resp.StatusCode)
}
// The link is dead and the space is back.
resp, err = http.Get(pageURL.String())
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
t.Errorf("deleted submission still reachable: status %d", resp.StatusCode)
}
if used, _ := usedBytes(s.db); used != 0 {
t.Errorf("quota still holds %d bytes", used)
}
}
// The whole path for the case that motivated the probing: a recording made in
// a browser, which arrives without a duration in its header, is submitted and
// then reviewed. By the time the review page is rendered it has to show a
// length, and the audio it serves has to declare one — otherwise the player's
// scrubber has no extent until the recording has been played through once.
func TestEndToEndWebmGetsAPlayableDuration(t *testing.T) {
ffprobeBin, ffmpegBin := requireFFmpeg(t)
s := newTestServer(t)
s.ffprobeBin, s.ffmpegBin = ffprobeBin, ffmpegBin
mux := http.NewServeMux()
mux.HandleFunc("POST /submit", s.handleSubmit)
mux.HandleFunc("GET /inbox/{token}", s.handleReview)
mux.HandleFunc("GET /inbox/{token}/audio", s.handleReviewAudio)
srv := httptest.NewServer(mux)
defer srv.Close()
s.baseURL = srv.URL
audio := makeAudio(t, ffmpegBin, "webm-nodur")
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
mw.WriteField("passphrase", testPassphrase)
fw, _ := mw.CreateFormFile("file", "recording.webm")
fw.Write(audio)
mw.Close()
resp, err := http.Post(srv.URL+"/submit", mw.FormDataContentType(), &buf)
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("submit: status %d, body %s", resp.StatusCode, body)
}
// Measuring happens after the response, so the submitter never waits for
// it. Wait for the duration to land rather than sleeping a fixed time.
var token string
if err := s.db.QueryRow(
`SELECT token FROM submission WHERE deleted_at IS NULL`).Scan(&token); err != nil {
t.Fatal(err)
}
deadline := time.Now().Add(30 * time.Second)
var sub *Submission
for {
sub, err = submissionByToken(s.db, token)
if err != nil {
t.Fatal(err)
}
if sub.DurationMs.Valid && sub.DurationMs.Int64 > 0 {
break
}
if time.Now().After(deadline) {
t.Fatal("the submission never got a duration")
}
time.Sleep(20 * time.Millisecond)
}
if sub.DurationMs.Int64 < 1900 || sub.DurationMs.Int64 > 2200 {
t.Errorf("duration = %d ms, want about 2000", sub.DurationMs.Int64)
}
// The page states it.
page, err := http.Get(srv.URL + "/inbox/" + token)
if err != nil {
t.Fatal(err)
}
html, _ := io.ReadAll(page.Body)
page.Body.Close()
if !strings.Contains(string(html), "0:02") {
t.Errorf("the review page does not show the duration:\n%s", html)
}
// And the bytes it serves carry it, which is what the player reads.
resp, err = http.Get(srv.URL + "/inbox/" + token + "/audio")
if err != nil {
t.Fatal(err)
}
served, _ := io.ReadAll(resp.Body)
resp.Body.Close()
dir := t.TempDir()
path := filepath.Join(dir, "served.webm")
if err := os.WriteFile(path, served, 0o600); err != nil {
t.Fatal(err)
}
ms, err := probeDurationMs(context.Background(), ffprobeBin, path)
if err != nil {
t.Fatalf("the audio served to the player still declares no duration: %v", err)
}
if ms < 1900 || ms > 2200 {
t.Errorf("served audio declares %d ms, want about 2000", ms)
}
// The quota has to agree with what is actually stored, since the stored
// bytes were replaced by the remux.
used, err := usedBytes(s.db)
if err != nil {
t.Fatal(err)
}
if used != int64(len(served)) {
t.Errorf("quota counts %d bytes, %d are served", used, len(served))
}
}
// resolveFrom pulls one URL out of the page and resolves it against the page's
// own address, which is what a browser does with src and action.
func resolveFrom(t *testing.T, page *url.URL, body, pattern, what string) string {
t.Helper()
m := regexp.MustCompile(pattern).FindStringSubmatch(body)
if m == nil {
t.Fatalf("review page has no %s:\n%s", what, body)
}
ref, err := url.Parse(m[1])
if err != nil {
t.Fatalf("page emitted an unparseable %s %q: %v", what, m[1], err)
}
return page.ResolveReference(ref).String()
}
|