| 1 | package callstest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/calls/protocol" |
| 13 | ) |
| 14 | |
| 15 | // Spec §15.29 (2026-10-28): the per-peer detail written flat, so `carlos |
| 16 | // logs` shows it; why a call was relayed; and the automatic report a |
| 17 | // struggling client sends without anyone pressing anything. |
| 18 | |
| 19 | // relayedQuality is a report from the side of a relayed call whose own |
| 20 | // network found no public address: a Safari guest, sharing nothing, |
| 21 | // receiving a camera and a screen. |
| 22 | func relayedQuality(remote string) protocol.QualityReport { |
| 23 | q := sampleQuality(remote) |
| 24 | q.Client = protocol.ReportClient{App: "web", Version: "47afcb9", Browser: "safari", BrowserVersion: "26.5", OS: "macos", OSVersion: "10.15.7"} |
| 25 | p := &q.Peers[0] |
| 26 | p.Path = &protocol.Path{Local: "relay", Remote: "srflx", Protocol: "tls"} |
| 27 | p.RTT, p.RTTAvg, p.RTTMax = num(310), num(402.5), num(1312) |
| 28 | p.JitterAvg, p.JitterMax = num(18), num(96) |
| 29 | p.Candidates = &protocol.Candidates{Host: num(2), Relay: num(1), RemoteHost: num(1), RemoteSrflx: num(1), RemoteRelay: num(1), UDPOK: false} |
| 30 | p.Out = &protocol.Outbound{ |
| 31 | Audio: &protocol.OutStream{Kbps: num(31.5), Packets: num(1500), Lost: num(15), RTT: num(420)}, |
| 32 | Video: &protocol.OutStream{Kbps: num(412), Width: num(640), Height: num(360), FPS: num(24), Limit: "bandwidth", |
| 33 | LimitedBandwidthSeconds: num(27.5), LimitedCPUSeconds: num(0), Encoder: "hardware", Codec: "h264"}, |
| 34 | } |
| 35 | p.In = &protocol.Inbound{ |
| 36 | Audio: &protocol.InAudio{Kbps: num(30), Packets: num(1480), Lost: num(20), Jitter: num(22), Samples: num(1440000), Concealed: num(14400), JitterBufferMs: num(140)}, |
| 37 | Video: &protocol.InVideo{Kbps: num(380), Packets: num(1200), Lost: num(12), Width: num(1280), Height: num(720), FPS: num(12.5), |
| 38 | Freezes: num(4), FreezeSeconds: num(6.1), Decoded: num(375), Dropped: num(3), Decoder: "hardware", Codec: "vp8"}, |
| 39 | Screen: &protocol.InVideo{Kbps: num(900), Width: num(1920), Height: num(1246), FPS: num(4), Freezes: num(3), FreezeSeconds: num(9), |
| 40 | Dropped: num(0), Decoder: "software", Codec: "h264", Stalls: num(1), StallsNoPackets: num(1)}, |
| 41 | } |
| 42 | p.Events = &protocol.PeerEvents{Reconnecting: num(1), ReconnectingSeconds: num(6.4), AutoRestarts: num(2)} |
| 43 | return q |
| 44 | } |
| 45 | |
| 46 | // peerLines are the `calls: quality peer` lines. |
| 47 | func (r *reportRig) peerLines(t *testing.T) []map[string]any { |
| 48 | t.Helper() |
| 49 | return r.reports.lines(t, "calls: quality peer") |
| 50 | } |
| 51 | |
| 52 | func TestTheQualityPeerLineIsFlatAndCarriesTheDetail(t *testing.T) { |
| 53 | r := newReportRig(t, "") |
| 54 | if err := r.x.ctl.SetRelay(relayContract(t)); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | if resp, body := r.b.report(r.sqid, "quality", r.tokB, mustJSON(t, relayedQuality(r.peerA))); resp.StatusCode != http.StatusAccepted { |
| 58 | t.Fatalf("quality: %d %s", resp.StatusCode, body) |
| 59 | } |
| 60 | heads := r.reports.lines(t, "calls: quality") |
| 61 | lines := r.peerLines(t) |
| 62 | if len(heads) != 1 || len(lines) != 1 { |
| 63 | t.Fatalf("%d quality lines and %d peer lines, want one of each", len(heads), len(lines)) |
| 64 | } |
| 65 | head, line := heads[0], lines[0] |
| 66 | // The same tags as the head, and the remote peer by its tag: A's, |
| 67 | // which is what A's own lines are under. |
| 68 | if line["call"] != head["call"] || line["peer"] != head["peer"] || line["level"] != "INFO" { |
| 69 | t.Fatalf("peer line tags %v, head %v", line, head) |
| 70 | } |
| 71 | if resp, _ := r.a.report(r.sqid, "quality", r.tokA, mustJSON(t, sampleQuality(r.peerB))); resp.StatusCode != http.StatusAccepted { |
| 72 | t.Fatal("A's report") |
| 73 | } |
| 74 | if tagA := r.reports.lines(t, "calls: quality")[1]["peer"]; line["remote"] != tagA { |
| 75 | t.Fatalf("remote = %v, want A's tag %v", line["remote"], tagA) |
| 76 | } |
| 77 | want := map[string]any{ |
| 78 | "pathLocal": "relay", "pathRemote": "srflx", "pathProtocol": "tls", "relayRegion": "eu-west-1", |
| 79 | "rtt": 310.0, "rttAvg": 402.5, "rttMax": 1312.0, "jitterAvg": 18.0, "jitterMax": 96.0, |
| 80 | "candHost": 2.0, "candRelay": 1.0, "candRemoteHost": 1.0, "candRemoteSrflx": 1.0, "candRemoteRelay": 1.0, |
| 81 | "directOk": false, "localDirectOk": false, "udpOk": false, |
| 82 | "outAudioKbps": 31.5, "outAudioLossPct": 1.0, "outAudioRtt": 420.0, |
| 83 | "outCameraKbps": 412.0, "outCameraWidth": 640.0, "outCameraHeight": 360.0, "outCameraFps": 24.0, |
| 84 | "outCameraLimit": "bandwidth", "outCameraLimitedBandwidthSeconds": 27.5, "outCameraLimitedCpuSeconds": 0.0, |
| 85 | "outCameraEncoder": "hardware", "outCameraCodec": "h264", |
| 86 | "inAudioKbps": 30.0, "inAudioLossPct": 1.3, "inAudioJitter": 22.0, "inAudioConcealedPct": 1.0, "inAudioJitterBufferMs": 140.0, |
| 87 | "inCameraKbps": 380.0, "inCameraWidth": 1280.0, "inCameraHeight": 720.0, "inCameraFps": 12.5, "inCameraLossPct": 1.0, |
| 88 | "inCameraFreezes": 4.0, "inCameraFreezeSeconds": 6.1, "inCameraDropped": 3.0, "inCameraDecoder": "hardware", "inCameraCodec": "vp8", |
| 89 | "inScreenWidth": 1920.0, "inScreenHeight": 1246.0, "inScreenFps": 4.0, "inScreenFreezes": 3.0, "inScreenDecoder": "software", |
| 90 | "inScreenStalls": 1.0, "inScreenStallsNoPackets": 1.0, |
| 91 | "reconnecting": 1.0, "reconnectingSeconds": 6.4, "autoRestarts": 2.0, |
| 92 | } |
| 93 | for k, v := range want { |
| 94 | if line[k] != v { |
| 95 | t.Errorf("peer line %s = %#v, want %#v", k, line[k], v) |
| 96 | } |
| 97 | } |
| 98 | // Flat: nothing nested for carlos to drop, and every key a name |
| 99 | // quality-summary's reader takes. |
| 100 | key := regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`) |
| 101 | for k, v := range line { |
| 102 | switch v.(type) { |
| 103 | case map[string]any, []any: |
| 104 | t.Errorf("peer line member %s is nested: %v", k, v) |
| 105 | } |
| 106 | if !key.MatchString(k) { |
| 107 | t.Errorf("peer line key %q is not letters and digits", k) |
| 108 | } |
| 109 | } |
| 110 | // Not measured is left off, not written as zero. |
| 111 | for _, k := range []string{"outScreenKbps", "inScreenKbps0", "candSrflx", "noSound", "outScreenMode"} { |
| 112 | if _, ok := line[k]; ok { |
| 113 | t.Errorf("peer line has %s, which the report did not measure", k) |
| 114 | } |
| 115 | } |
| 116 | // The head's maxRtt reads the window's highest, not only the latest. |
| 117 | if head["maxRtt"] != 1312.0 { |
| 118 | t.Errorf("head maxRtt = %v, want the window's 1312", head["maxRtt"]) |
| 119 | } |
| 120 | r.assertClean(t) |
| 121 | } |
| 122 | |
| 123 | // A report from a client before §15.29 — none of the new members — is |
| 124 | // taken as it always was, and its peer line says what it can. |
| 125 | func TestAnOldClientsReportStillMakesAPeerLine(t *testing.T) { |
| 126 | r := newReportRig(t, "") |
| 127 | if resp, _ := r.a.report(r.sqid, "quality", r.tokA, mustJSON(t, sampleQuality(r.peerB))); resp.StatusCode != http.StatusAccepted { |
| 128 | t.Fatalf("an old client's report: %d", resp.StatusCode) |
| 129 | } |
| 130 | lines := r.peerLines(t) |
| 131 | if len(lines) != 1 { |
| 132 | t.Fatalf("%d peer lines", len(lines)) |
| 133 | } |
| 134 | l := lines[0] |
| 135 | if l["pathLocal"] != "srflx" || l["pathRemote"] != "relay" || l["outCameraLimit"] != "bandwidth" || l["inCameraFreezes"] != 2.0 { |
| 136 | t.Fatalf("old client's peer line %v", l) |
| 137 | } |
| 138 | // No relay configured here: the region is not guessed. |
| 139 | for _, k := range []string{"relayRegion", "directOk", "candHost", "rttMax", "outCameraEncoder", "autoReport"} { |
| 140 | if _, ok := l[k]; ok { |
| 141 | t.Errorf("old client's peer line has %s", k) |
| 142 | } |
| 143 | } |
| 144 | if _, ok := r.reports.lines(t, "calls: quality")[0]["autoReport"]; ok { |
| 145 | t.Error("a quality line with no auto report says autoReport") |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestTheNewMembersAreStrict(t *testing.T) { |
| 150 | r := newReportRig(t, "") |
| 151 | cases := map[string]func(*protocol.QualityReport){ |
| 152 | "an encoder by name": func(q *protocol.QualityReport) { q.Peers[0].Out.Video.Encoder = "VideoToolbox" }, |
| 153 | "a codec off the list": func(q *protocol.QualityReport) { q.Peers[0].Out.Video.Codec = "theora" }, |
| 154 | "a share mode off the list": func(q *protocol.QualityReport) { q.Peers[0].Out.Screen = &protocol.OutStream{Mode: "fast"} }, |
| 155 | "a decoder by name": func(q *protocol.QualityReport) { q.Peers[0].In.Video.Decoder = "libvpx" }, |
| 156 | "an inbound codec": func(q *protocol.QualityReport) { q.Peers[0].In.Screen = &protocol.InVideo{Codec: "H264"} }, |
| 157 | "an auto report ref": func(q *protocol.QualityReport) { q.AutoReport = "not-a-ref" }, |
| 158 | "a lowercase ref": func(q *protocol.QualityReport) { q.AutoReport = "k7q2m9pd" }, |
| 159 | "rttMax over its ceiling": func(q *protocol.QualityReport) { q.Peers[0].RTTMax = num(600_001) }, |
| 160 | "a negative jitter": func(q *protocol.QualityReport) { q.Peers[0].JitterAvg = num(-1) }, |
| 161 | "limited seconds too long": func(q *protocol.QualityReport) { q.Peers[0].Out.Video.LimitedBandwidthSeconds = num(601) }, |
| 162 | "a candidate count too big": func(q *protocol.QualityReport) { q.Peers[0].Candidates = &protocol.Candidates{Host: num(2e9)} }, |
| 163 | } |
| 164 | for name, edit := range cases { |
| 165 | q := relayedQuality(r.peerB) |
| 166 | edit(&q) |
| 167 | r.x.advance(10 * time.Second) |
| 168 | if resp, _ := r.a.report(r.sqid, "quality", r.tokA, mustJSON(t, q)); resp.StatusCode != http.StatusBadRequest { |
| 169 | t.Errorf("%s: %d, want 400", name, resp.StatusCode) |
| 170 | } |
| 171 | } |
| 172 | // And the good values are taken. |
| 173 | q := relayedQuality(r.peerB) |
| 174 | q.AutoReport = "K7Q2M9PD" |
| 175 | q.Peers[0].Out.Screen = &protocol.OutStream{Kbps: num(2400), Mode: "detail", Encoder: "software", Codec: "h264", Limit: "cpu", LimitedCPUSeconds: num(12)} |
| 176 | r.x.advance(10 * time.Second) |
| 177 | if resp, body := r.a.report(r.sqid, "quality", r.tokA, mustJSON(t, q)); resp.StatusCode != http.StatusAccepted { |
| 178 | t.Fatalf("a good report: %d %s", resp.StatusCode, body) |
| 179 | } |
| 180 | head := r.reports.lines(t, "calls: quality")[0] |
| 181 | if head["autoReport"] != "K7Q2M9PD" { |
| 182 | t.Errorf("head autoReport = %v", head["autoReport"]) |
| 183 | } |
| 184 | l := r.peerLines(t)[0] |
| 185 | if l["outScreenMode"] != "detail" || l["outScreenEncoder"] != "software" || l["outScreenLimit"] != "cpu" || l["outScreenLimitedCpuSeconds"] != 12.0 { |
| 186 | t.Errorf("screen fields %v", l) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // The largest per-peer line the schema allows, every field present and at |
| 191 | // its widest, fits the platform's line. |
| 192 | func TestTheLargestPeerLineFitsThePlatformsLine(t *testing.T) { |
| 193 | r := newReportRig(t, "") |
| 194 | if err := r.x.ctl.SetRelay(relayContract(t)); err != nil { |
| 195 | t.Fatal(err) |
| 196 | } |
| 197 | q := largestQuality(r.peerB) |
| 198 | if resp, body := r.a.report(r.sqid, "quality", r.tokA, mustJSON(t, q)); resp.StatusCode != http.StatusAccepted { |
| 199 | t.Fatalf("the largest report: %d %s", resp.StatusCode, body) |
| 200 | } |
| 201 | lines := strings.Split(strings.TrimSpace(r.reports.String()), "\n") |
| 202 | if len(lines) != 1+protocol.MaxQualityPeers { |
| 203 | t.Fatalf("%d lines, want the head and %d peer lines", len(lines), protocol.MaxQualityPeers) |
| 204 | } |
| 205 | widest := 0 |
| 206 | for _, l := range lines { |
| 207 | widest = max(widest, len(l)) |
| 208 | if len(l) > 8192 { |
| 209 | t.Fatalf("a line of %d bytes; the platform keeps 8192:\n%.300s", len(l), l) |
| 210 | } |
| 211 | } |
| 212 | peer := r.peerLines(t)[0] |
| 213 | if len(peer) < 90 { |
| 214 | t.Errorf("the largest peer line has only %d fields", len(peer)) |
| 215 | } |
| 216 | t.Logf("widest line %d bytes; a peer line %d bytes with %d fields", widest, len(lines[1]), len(peer)) |
| 217 | } |
| 218 | |
| 219 | // largestQuality is the widest report the schema allows: every peer, |
| 220 | // every number at its widest, every enum at its longest. |
| 221 | func largestQuality(remote string) protocol.QualityReport { |
| 222 | big := func() *float64 { return num(123456789.1) } |
| 223 | ms := func() *float64 { return num(599999.9) } |
| 224 | secs := func() *float64 { return num(599.9) } |
| 225 | out := func(mode string) *protocol.OutStream { |
| 226 | return &protocol.OutStream{Kbps: num(999999.9), Packets: big(), Lost: big(), RTT: ms(), Jitter: ms(), Limit: "bandwidth", |
| 227 | Width: num(16384), Height: num(16384), FPS: num(240), Keyframes: big(), Rebuilds: big(), Reshares: big(), Failures: big(), |
| 228 | Damped: big(), EncoderStalls: big(), LimitedBandwidthSeconds: secs(), LimitedCPUSeconds: secs(), |
| 229 | Encoder: "software", Codec: "other", Mode: mode} |
| 230 | } |
| 231 | in := func() *protocol.InVideo { |
| 232 | return &protocol.InVideo{Kbps: num(999999.9), Packets: big(), Lost: big(), Jitter: ms(), Freezes: big(), FreezeSeconds: secs(), |
| 233 | Decoded: big(), Dropped: big(), Width: num(16384), Height: num(16384), FPS: num(240), Reattaches: big(), |
| 234 | Stalls: big(), StallsNoPackets: big(), StallsUndecoded: big(), Decoder: "software", Codec: "other"} |
| 235 | } |
| 236 | q := sampleQuality(remote) |
| 237 | q.AutoReport = "K7Q2M9PD" |
| 238 | q.Peers = nil |
| 239 | for i := 0; i < protocol.MaxQualityPeers; i++ { |
| 240 | q.Peers = append(q.Peers, protocol.PeerQuality{ |
| 241 | Peer: protocol.PeerID(fmt.Sprintf("%032x", i)), |
| 242 | Path: &protocol.Path{Local: "relay", Remote: "relay", Protocol: "unknown"}, RTT: ms(), |
| 243 | RTTAvg: ms(), RTTMax: ms(), JitterAvg: ms(), JitterMax: ms(), |
| 244 | Candidates: &protocol.Candidates{Host: big(), Srflx: big(), Prflx: big(), Relay: big(), RemoteHost: big(), RemoteSrflx: big(), |
| 245 | RemotePrflx: big(), RemoteRelay: big(), DirectOK: true, LocalDirectOK: true, UDPOK: true}, |
| 246 | Out: &protocol.Outbound{Audio: out(""), Video: out(""), Screen: out("balanced")}, |
| 247 | In: &protocol.Inbound{ |
| 248 | Audio: &protocol.InAudio{Kbps: num(999999.9), Packets: big(), Lost: big(), Jitter: ms(), Samples: big(), Concealed: big(), Inserted: big(), Removed: big(), JitterBufferMs: ms()}, |
| 249 | Video: in(), Screen: in(), |
| 250 | }, |
| 251 | Events: &protocol.PeerEvents{Reconnecting: big(), ReconnectingSeconds: secs(), CantHearSeconds: secs(), |
| 252 | NoSound: true, NoSoundFromUs: true, AudioBlocked: true, ScreenStalled: true, AutoRestarts: big(), AutoRebuilds: big()}, |
| 253 | }) |
| 254 | } |
| 255 | return q |
| 256 | } |
| 257 | |
| 258 | // autoBody is an automatic report as a client sends it. |
| 259 | func autoBody(t *testing.T, trigger, diagnostics string) []byte { |
| 260 | return mustJSON(t, protocol.AutoReport{Trigger: trigger, Diagnostics: diagnostics, |
| 261 | Client: protocol.ReportClient{App: "web", Version: "47afcb9", Browser: "safari", BrowserVersion: "26.5", OS: "macos", OSVersion: "10.15.7"}}) |
| 262 | } |
| 263 | |
| 264 | func TestAnAutoReportIsLoggedUnderItsReferenceScrubbedAndWithoutANote(t *testing.T) { |
| 265 | r := newReportRig(t, "") |
| 266 | diag := fmt.Sprintf("Calls web diagnostics\n## Call\npeer %s: connection connected, ICE connected\nopened %s/m/%s\nfull id %s\n", |
| 267 | r.peerA[:6], testOrigin, r.sqid, r.peerA) |
| 268 | for len(diag) < 14_000 { |
| 269 | diag += "12:00:00.000 screen from " + r.peerA[:6] + " stalled here: no packets arriving\n" |
| 270 | } |
| 271 | // A client that sent a note anyway: the schema has none, so it is |
| 272 | // dropped unread. |
| 273 | var withNote map[string]any |
| 274 | _ = json.Unmarshal(autoBody(t, "freezes", diag), &withNote) |
| 275 | withNote["note"] = "Ana's video froze" |
| 276 | resp, body := r.b.report(r.sqid, "auto-report", r.tokB, mustJSON(t, withNote)) |
| 277 | if resp.StatusCode != http.StatusOK { |
| 278 | t.Fatalf("auto report: %d %s", resp.StatusCode, body) |
| 279 | } |
| 280 | var receipt protocol.ProblemReceipt |
| 281 | if err := json.Unmarshal(body, &receipt); err != nil || !regexp.MustCompile(`^[0-9A-HJKMNP-TV-Z]{8}$`).MatchString(receipt.Ref) { |
| 282 | t.Fatalf("receipt %s", body) |
| 283 | } |
| 284 | heads := r.reports.lines(t, "calls: auto report") |
| 285 | if len(heads) != 1 { |
| 286 | t.Fatalf("%d auto report lines", len(heads)) |
| 287 | } |
| 288 | head := heads[0] |
| 289 | if head["ref"] != receipt.Ref || head["trigger"] != "freezes" || head["level"] != "WARN" || head["live"] != true || head["browser"] != "safari" { |
| 290 | t.Fatalf("auto report head %v", head) |
| 291 | } |
| 292 | if _, ok := head["note"]; ok { |
| 293 | t.Errorf("an auto report head carries a note: %v", head) |
| 294 | } |
| 295 | if strings.Contains(r.reports.String(), "Ana's video") { |
| 296 | t.Error("the note a client added reached the log") |
| 297 | } |
| 298 | parts := r.reports.lines(t, "calls: auto report diagnostics") |
| 299 | if len(parts) < 3 || float64(len(parts)) != head["parts"] { |
| 300 | t.Fatalf("%d diagnostics parts, head says %v", len(parts), head["parts"]) |
| 301 | } |
| 302 | var text strings.Builder |
| 303 | for i, p := range parts { |
| 304 | if p["ref"] != receipt.Ref || p["part"] != float64(i+1) || p["call"] != head["call"] || p["peer"] != head["peer"] { |
| 305 | t.Fatalf("part %d: %v", i+1, p) |
| 306 | } |
| 307 | text.WriteString(p["text"].(string)) |
| 308 | } |
| 309 | if !strings.Contains(text.String(), "/m/[meeting]") || !strings.Contains(text.String(), "full id peer:") || strings.Contains(text.String(), r.peerA[:6]) { |
| 310 | t.Fatalf("diagnostics not scrubbed:\n%.300s", text.String()) |
| 311 | } |
| 312 | for _, line := range strings.Split(r.reports.String(), "\n") { |
| 313 | if len(line) > 8192 { |
| 314 | t.Fatalf("a line of %d bytes", len(line)) |
| 315 | } |
| 316 | } |
| 317 | // Not a problem report: quality-summary tells them apart. |
| 318 | if n := len(r.reports.lines(t, "calls: problem report")); n != 0 { |
| 319 | t.Errorf("%d problem report lines from an auto report", n) |
| 320 | } |
| 321 | r.assertClean(t) |
| 322 | |
| 323 | // The schema: a trigger off the list, a client off the list, the cap. |
| 324 | r.x.advance(11 * time.Minute) |
| 325 | for name, b := range map[string][]byte{ |
| 326 | "no trigger": autoBody(t, "", "x"), |
| 327 | "a trigger by name": autoBody(t, "my video froze", "x"), |
| 328 | "a bad client": mustJSON(t, protocol.AutoReport{Trigger: "rtt", Client: protocol.ReportClient{App: "Paul's Mac"}}), |
| 329 | "not json": []byte("trigger=rtt"), |
| 330 | } { |
| 331 | r.x.advance(11 * time.Minute) |
| 332 | if resp, _ := r.a.report(r.sqid, "auto-report", r.tokA, b); resp.StatusCode != http.StatusBadRequest { |
| 333 | t.Errorf("%s: %d, want 400", name, resp.StatusCode) |
| 334 | } |
| 335 | } |
| 336 | r.x.advance(11 * time.Minute) |
| 337 | if resp, _ := r.a.report(r.sqid, "auto-report", r.tokA, autoBody(t, "rtt", strings.Repeat("a", 65<<10))); resp.StatusCode != http.StatusRequestEntityTooLarge { |
| 338 | t.Errorf("65 KB of diagnostics: %d, want 413", resp.StatusCode) |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | func TestAutoReportsAreOneEveryTenMinutesAndThreeACall(t *testing.T) { |
| 343 | r := newReportRig(t, "") |
| 344 | send := func(c *client, tok string) *http.Response { |
| 345 | resp, _ := c.report(r.sqid, "auto-report", tok, autoBody(t, "rtt", "Calls web diagnostics")) |
| 346 | return resp |
| 347 | } |
| 348 | if resp := send(r.a, r.tokA); resp.StatusCode != http.StatusOK { |
| 349 | t.Fatalf("the first: %d", resp.StatusCode) |
| 350 | } |
| 351 | resp := send(r.a, r.tokA) |
| 352 | if resp.StatusCode != http.StatusTooManyRequests || resp.Header.Get("Retry-After") == "" { |
| 353 | t.Fatalf("a second at once: %d (Retry-After %q), want 429", resp.StatusCode, resp.Header.Get("Retry-After")) |
| 354 | } |
| 355 | // Per participation: B's are its own. |
| 356 | if resp := send(r.b, r.tokB); resp.StatusCode != http.StatusOK { |
| 357 | t.Fatalf("B's first, after A's: %d", resp.StatusCode) |
| 358 | } |
| 359 | r.x.advance(9*time.Minute + 50*time.Second) |
| 360 | if resp := send(r.a, r.tokA); resp.StatusCode != http.StatusTooManyRequests { |
| 361 | t.Fatalf("nine minutes fifty later: %d, want 429", resp.StatusCode) |
| 362 | } |
| 363 | // Two minutes idle is longer than the problem limiter's memory; this |
| 364 | // limit remembers. |
| 365 | r.x.advance(10 * time.Second) |
| 366 | if resp := send(r.a, r.tokA); resp.StatusCode != http.StatusOK { |
| 367 | t.Fatalf("ten minutes later: %d, want 200", resp.StatusCode) |
| 368 | } |
| 369 | r.x.advance(10 * time.Minute) |
| 370 | if resp := send(r.a, r.tokA); resp.StatusCode != http.StatusOK { |
| 371 | t.Fatalf("the third, twenty minutes in: %d", resp.StatusCode) |
| 372 | } |
| 373 | r.x.advance(time.Hour) |
| 374 | if resp := send(r.a, r.tokA); resp.StatusCode != http.StatusTooManyRequests { |
| 375 | t.Fatalf("a fourth an hour later: %d, want 429 (three a call)", resp.StatusCode) |
| 376 | } |
| 377 | if n := len(r.reports.lines(t, "calls: auto report")); n != 4 { |
| 378 | t.Fatalf("%d auto reports logged, want A's three and B's one", n) |
| 379 | } |
| 380 | // Problem reports are the person's, and spend nothing of this. |
| 381 | if resp, _ := r.a.report(r.sqid, "problem", r.tokA, mustJSON(t, protocol.ProblemReport{Note: "x", Client: protocol.ReportClient{App: "web"}})); resp.StatusCode != http.StatusOK { |
| 382 | t.Fatalf("a problem report after the auto reports ran out: %d", resp.StatusCode) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | func TestAutoReportsNeedTheTokenAndFollowTheKillSwitch(t *testing.T) { |
| 387 | r := newReportRig(t, "") |
| 388 | if resp, _ := r.a.report(r.sqid, "auto-report", strings.Repeat("0", 32), autoBody(t, "rtt", "x")); resp.StatusCode != http.StatusNotFound { |
| 389 | t.Fatalf("a made-up token: %d, want 404", resp.StatusCode) |
| 390 | } |
| 391 | if resp, _ := r.a.report(r.sqid, "auto-report", r.tokB, autoBody(t, "rtt", "x")); resp.StatusCode != http.StatusNotFound { |
| 392 | t.Fatalf("B's token from A's session: %d, want 404", resp.StatusCode) |
| 393 | } |
| 394 | |
| 395 | off := newReportRig(t, "off") |
| 396 | for _, token := range []string{off.tokA, ""} { |
| 397 | if resp, _ := off.a.report(off.sqid, "auto-report", token, autoBody(t, "freezes", "x")); resp.StatusCode != http.StatusNoContent { |
| 398 | t.Fatalf("quality reports off (token %q): %d, want 204", token, resp.StatusCode) |
| 399 | } |
| 400 | } |
| 401 | if n := len(off.reports.lines(t, "calls: auto report")); n != 0 { |
| 402 | t.Fatalf("%d auto report lines with reports off", n) |
| 403 | } |
| 404 | } |
| 405 | |