Files
zapret-autotune/cmd/zapret-autotune/recovery_primitives_test.go
T
2026-10-10 17:44:13 +03:00

533 lines
25 KiB
Go

package main
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
func evidenceSnapshot(t *testing.T, db *sql.DB) string {
t.Helper()
var snapshot string
if err := db.QueryRow(`SELECT group_concat(row, ';') FROM (SELECT
quote(candidate_id)||','||quote(endpoint)||','||quote(protocol)||','||quote(passes_ok)||','||quote(passes_total)||','||quote(success_rate)||','||quote(coverage)||','||quote(median_latency_ms)||','||quote(median_speed_kbps)||','||quote(checked_at)||','||quote(raw_measurement) AS row
FROM candidate_evidence ORDER BY candidate_id, endpoint, protocol)`).Scan(&snapshot); err != nil {
t.Fatal(err)
}
return snapshot
}
func TestListQualifiedCandidates(t *testing.T) {
db := testStore(t)
args := "--lua-desync=multisplit"
for i := 0; i < 12; i++ {
saveProposal(t, db, args)
}
if err := saveGooglevideoRun(db, time.Now(), cacheTestHost, args, []byte(combinedReportFixture(args)), nil); err != nil {
t.Fatal(err)
}
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
if _, err := insertStrategyCandidate(tx, "frontend", []string{"youtube.com"}, args, nil); err != nil {
tx.Rollback()
t.Fatal(err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
before := evidenceSnapshot(t, db)
for _, limit := range []int{1, 3, 10} {
candidates, err := listQualifiedCandidates(db, "frontend", limit)
if err != nil || len(candidates) != limit {
t.Fatalf("limit=%d candidates=%+v err=%v", limit, candidates, err)
}
for i, c := range candidates {
if c.ID != int64(12-i) || !c.Qualified || c.Group != "frontend" {
t.Fatalf("unexpected candidate: %+v", c)
}
}
// Returned slices cannot mutate definitions or later snapshots.
candidates[0].Endpoints[0] = "changed.example"
stored, err := readStrategyCandidate(db, candidates[0].ID)
if err != nil || stored.Endpoints[0] != "youtube.com" {
t.Fatalf("stored snapshot changed: %+v %v", stored, err)
}
}
for _, limit := range []int{-1, 0, 11} {
if _, err := listQualifiedCandidates(db, "frontend", limit); err == nil {
t.Fatalf("accepted limit %d", limit)
}
}
if _, err := listQualifiedCandidates(db, "unknown", 1); err == nil {
t.Fatal("accepted unknown group")
}
ggc, err := listQualifiedCandidates(db, "googlevideo", 10)
if err != nil || len(ggc) != 1 || ggc[0].Group != "googlevideo" {
t.Fatalf("googlevideo=%+v err=%v", ggc, err)
}
if before != evidenceSnapshot(t, db) {
t.Fatal("listing mutated evidence")
}
}
func TestRevalidateCandidate(t *testing.T) {
db := testStore(t)
args := "--lua-desync=multisplit"
saveProposal(t, db, args)
c, err := readStrategyCandidate(db, 1)
if err != nil {
t.Fatal(err)
}
original := c
original.Endpoints = append([]string(nil), c.Endpoints...)
before := evidenceSnapshot(t, db)
passing := combinedReportFixture(args)
var parsed struct {
Strategies []json.RawMessage `json:"strategies"`
}
if err := json.Unmarshal([]byte(passing), &parsed); err != nil {
t.Fatal(err)
}
for _, mode := range []string{"pass", "one-nonpass", "both-nonpass", "nonpass-malformed", "malformed", "incomplete", "duplicate", "wrong-args", "wrong-host", "wrong-group", "missing-metric", "invalid-metric", "incomplete-passes", "trailing-json", "runner", "runner-sentinel", "missing-file", "stale-file", "filesystem", "cancelled"} {
t.Run(mode, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "check")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
report := passing
switch mode {
case "one-nonpass":
report = strings.Replace(report, `"passes_ok":3`, `"passes_ok":2`, 1)
case "both-nonpass":
report = strings.ReplaceAll(report, `"passes_ok":3`, `"passes_ok":0`)
case "nonpass-malformed":
report = strings.Replace(report, `"passes_ok":3`, `"passes_ok":2`, 1)
i := strings.LastIndex(report, `"coverage":1`)
report = report[:i] + strings.Replace(report[i:], `"coverage":1`, `"coverage":null`, 1)
case "malformed":
report = `{`
case "incomplete":
report = `{"strategies":[` + string(parsed.Strategies[0]) + `]}`
case "duplicate":
report = `{"strategies":[` + string(parsed.Strategies[0]) + `,` + string(parsed.Strategies[0]) + `,` + string(parsed.Strategies[1]) + `]}`
case "wrong-args":
report = combinedReportFixture("--lua-desync=fake")
case "wrong-host":
report = strings.ReplaceAll(report, `"protocol":`, `"domains":["other.example"],"protocol":`)
case "wrong-group":
report = strings.ReplaceAll(report, `"protocol":`, `"group":"googlevideo","protocol":`)
case "missing-metric":
report = strings.Replace(report, `"coverage":1`, `"coverage":null`, 1)
case "invalid-metric":
report = strings.Replace(report, `"median_speed_kbps":100`, `"median_speed_kbps":-1`, 1)
case "incomplete-passes":
report = strings.Replace(report, `"passes_total":3`, `"passes_total":4`, 1)
case "trailing-json":
report += `{}`
case "filesystem":
path = filepath.Join(path, "missing", "check")
case "stale-file":
if err := os.WriteFile(path+".combined.json", []byte(passing), 0600); err != nil {
t.Fatal(err)
}
}
check := func(_ context.Context, binary, gotPath, gotArgs, host string, _, _ io.Writer) error {
if binary != "blockcheckw" || gotPath != path || gotArgs != args || host != "youtube.com" {
t.Fatalf("wrong combined check: %s %s %s %s", binary, gotPath, gotArgs, host)
}
switch mode {
case "runner":
return fmt.Errorf("runner failed")
case "runner-sentinel":
return errCandidateNonpass
case "missing-file", "stale-file", "filesystem":
return nil
case "cancelled":
cancel()
}
return os.WriteFile(path+".combined.json", []byte(report), 0600)
}
got, err := revalidateCandidate(ctx, c, "frontend", "youtube.com", path, io.Discard, check)
if mode == "pass" {
if err != nil || string(got) != passing {
t.Fatalf("passing report=%s err=%v", got, err)
}
} else if mode == "one-nonpass" || mode == "both-nonpass" {
if !errors.Is(err, errCandidateNonpass) {
t.Fatalf("expected nonpass, got %v", err)
}
} else if err == nil || errors.Is(err, errCandidateNonpass) {
t.Fatalf("expected infrastructure error, got %v", err)
}
if !reflect.DeepEqual(c, original) || before != evidenceSnapshot(t, db) {
t.Fatal("revalidation mutated candidate or evidence")
}
})
}
}
func TestRevalidateCandidateScope(t *testing.T) {
db := testStore(t)
args := "--lua-desync=multisplit"
if err := saveGooglevideoRun(db, time.Now(), "old.googlevideo.com", args, []byte(combinedReportFixture(args)), nil); err != nil {
t.Fatal(err)
}
c, err := readStrategyCandidate(db, 1)
if err != nil {
t.Fatal(err)
}
for _, target := range []struct{ group, host string }{
{"googlevideo", cacheTestHost}, {"googlevideo", "googlevideo.com"},
{"googlevideo", "^" + cacheTestHost}, {"googlevideo", "*.googlevideo.com"},
{"googlevideo", "youtube.com"}, {"frontend", "youtube.com"}, {"unknown", cacheTestHost},
} {
calls := 0
check := func(_ context.Context, _, path, gotArgs, host string, _, _ io.Writer) error {
calls++
if gotArgs != args || host != cacheTestHost {
t.Fatal("widened scope or changed args")
}
return os.WriteFile(path+".combined.json", []byte(combinedReportFixture(args)), 0600)
}
_, err := revalidateCandidate(context.Background(), c, target.group, target.host, filepath.Join(t.TempDir(), "check"), io.Discard, check)
valid := target.group == "googlevideo" && target.host == cacheTestHost
if valid && (err != nil || calls != 1) || !valid && (err == nil || calls != 0 || errors.Is(err, errCandidateNonpass)) {
t.Fatalf("target=%+v calls=%d err=%v", target, calls, err)
}
}
stored, err := readStrategyCandidate(db, 1)
if err != nil || !reflect.DeepEqual(c, stored) || c.Endpoints[0] != "old.googlevideo.com" {
t.Fatalf("changed stored scope: %+v %v", stored, err)
}
}
func earlyTimeoutDiagnosticsFixture(args, tls12, tls13 string, working int) string {
return fmt.Sprintf("! --workers has no effect on `check` (it verifies strategies sequentially by design)\n"+`=== Checking prerequisites ===
OK nfqws2: /opt/zapret2/binaries/linux-x86_64/nfqws2
OK nft
OK nft queue support
check loaded 2 strategies from /tmp/input.txt, --take 1, --passes 3
ISP: AS3175 ...
=== DNS resolve ===
dns mode: auto
www.youtube.com -> 142.251.156.4 (via doh (auto-fallback))
=== Checking strategies (data transfer) ===
Tip: use --take 10 to stop after 10 verified per protocol
Verifying 2 strategies (3 passes, early-exit on first fail)
[1/2] HTTPS/TLS1.2 nfqws2 %s
%s
────────────────────────────────────────────────────────────
[2/2] HTTPS/TLS1.3 nfqws2 %s
%s
=== Check summary ===
total: 2 | working: %d | elapsed: 7.7s
OK JSON report → /tmp/input.txt.combined.json
`, args, tls12, args, tls13, working)
}
func TestCandidateEarlyTimeoutRemoteCaptures(t *testing.T) {
// Rows/results and variable metadata from profile dev's combined-corpus-{1,3}
// and checker-timeout-20-2 captures; reuse only the identical CLI framing.
const selected = "--payload=tls_client_hello --lua-desync=hostfakesplit:nofake2:tcp_ts=-1000:repeats=1 --lua-desync=multidisorder:pos=midsld"
const original = "--payload=tls_client_hello --lua-desync=hostfakesplit:disorder_after:ip_autottl=-3,3-20:repeats=1 --lua-desync=multidisorder:pos=1,midsld"
row := func(protocol, latency, speed string) string {
return fmt.Sprintf(`{"protocol":%q,"args":%q,"coverage":1,"success_rate":1,"median_latency_ms":%s,"median_speed_kbps":%s,"passes_ok":3,"passes_total":3}`, protocol, selected, latency, speed)
}
for _, c := range []struct {
name, args, rows, tls12, tls13, ips, elapsed, path string
working int
}{
{"combinedrun3", selected, row("HTTPS/TLS1.3", "253", "3684.010622529644"), "FAIL 2/3 timeout", "OK median 253ms, 3684.0 KB/s", "142.251.156.4, 142.251.150.4, 142.251.155.4, 142.251.152.4, 142.251.154.4, 142.251.157.4, 142.251.151.4, 142.251.153.4", "8.1", "2026-10-10_14-36_check.json", 1},
{"timeout20twofail", original, "", "FAIL 1/2 timeout", "FAIL 1/2 timeout", "142.251.150.4, 142.251.153.4, 142.251.155.4, 142.251.151.4, 142.251.152.4, 142.251.154.4, 142.251.157.4, 142.251.156.4", "41.1", "2026-10-10_13-16_check.json", 0},
{"combinedrun1", selected, row("HTTPS/TLS1.3", "260", "3608.5486778846152") + "," + row("HTTPS/TLS1.2", "261", "3564.082704741379"), "OK median 261ms, 3564.1 KB/s", "OK median 260ms, 3608.5 KB/s", "142.251.151.4, 142.251.155.4, 142.251.157.4, 142.251.150.4, 142.251.152.4, 142.251.153.4, 142.251.156.4, 142.251.154.4", "2.4", "2026-10-10_14-35_check.json", 2},
} {
t.Run(c.name, func(t *testing.T) {
report := []byte(fmt.Sprintf(`{"domain":"www.youtube.com","total":2,"working":%d,"strategies":[%s]}`, c.working, c.rows))
diagnostic := strings.TrimPrefix(earlyTimeoutDiagnosticsFixture(c.args, c.tls12, c.tls13, c.working), "! --workers has no effect on `check` (it verifies strategies sequentially by design)\n")
diagnostic = strings.NewReplacer("AS3175 ...", "AS3175 Citytelecom LLC | 89.188.107.29 | Moscow, Moscow, RU", "142.251.156.4 (via", c.ips+" (via", "elapsed: 7.7s", "elapsed: "+c.elapsed+"s", "/tmp/input.txt.combined.json", c.path).Replace(diagnostic)
if c.working == 2 {
diagnostic = strings.Replace(diagnostic, "\n=== Check summary ===", " --take found 1 verified strategies per protocol, stopping\n\n=== Check summary ===", 1)
}
want := c.working < 2
if got := candidateEarlyTimeout(report, []byte(diagnostic), c.args, "frontend", "www.youtube.com"); got != want {
t.Fatalf("candidateEarlyTimeout=%v want=%v", got, want)
}
if !want {
if _, err := selectBlockcheckEvidence(strings.NewReader(string(report)), c.args, "frontend", "www.youtube.com", true); err != nil {
t.Fatalf("passing capture rejected: %v", err)
}
return
}
for _, pair := range [][2]string{
{c.ips, "142.251.156.4, garbage"},
{c.ips, "dead"},
{c.ips, "999.999.999.999"},
{c.ips, "142.251.156.4, dead"},
{c.ips, "999.999.999.999, 142.251.156.4"},
{c.ips, "2001:db8:::1"},
{c.ips, "142.251.156.4,2001:db8::1"},
{c.ips, "142.251.156.4, 2001:db8::1"},
{"via doh (auto-fallback)", "via unknown"},
{c.path, "unknown-output"},
{c.path, c.path + "\nnfqws2 exited with status 1"},
} {
if candidateEarlyTimeout(report, []byte(strings.Replace(diagnostic, pair[0], pair[1], 1)), c.args, "frontend", "www.youtube.com") {
t.Fatalf("unknown metadata/infrastructure accepted: %q", pair[1])
}
}
if !candidateEarlyTimeout(report, []byte(strings.Replace(diagnostic, "via doh (auto-fallback)", "via system", 1)), c.args, "frontend", "www.youtube.com") {
t.Fatal("system-resolved addresses rejected")
}
for _, ips := range []string{"2001:db8::1", "142.251.156.4, 2001:db8::1", "::ffff:192.0.2.1"} {
if !candidateEarlyTimeout(report, []byte(strings.Replace(diagnostic, c.ips, ips, 1)), c.args, "frontend", "www.youtube.com") {
t.Fatalf("valid IP literals rejected: %q", ips)
}
}
})
}
}
func TestCandidateEarlyTimeoutRowValidation(t *testing.T) {
args := "--lua-desync=multisplit"
var passing struct {
Strategies []json.RawMessage `json:"strategies"`
}
if err := json.Unmarshal([]byte(combinedReportFixture(args)), &passing); err != nil {
t.Fatal(err)
}
diagnostics := []byte(earlyTimeoutDiagnosticsFixture(args, "FAIL 1/2 timeout", "OK median 242ms, 3728.2 KB/s", 1))
check := func(t *testing.T, field string, value json.RawMessage) {
t.Helper()
var row map[string]json.RawMessage
if err := json.Unmarshal(passing.Strategies[1], &row); err != nil {
t.Fatal(err)
}
if value == nil {
delete(row, field)
} else {
row[field] = value
}
report, err := json.Marshal(map[string]any{"domain": "www.youtube.com", "total": 2, "working": 1, "strategies": []any{row}})
if err != nil {
t.Fatal(err)
}
if candidateEarlyTimeout(report, diagnostics, args, "frontend", "www.youtube.com") {
t.Fatal("malformed passing row authorized fallback")
}
}
for _, field := range []string{"protocol", "args", "success_rate", "coverage", "passes_ok", "passes_total", "median_latency_ms", "median_speed_kbps"} {
for _, mutation := range []struct {
name string
value json.RawMessage
}{{"missing", nil}, {"null", json.RawMessage(`null`)}, {"wrong-type", json.RawMessage(`{}`)}} {
t.Run(field+"/"+mutation.name, func(t *testing.T) { check(t, field, mutation.value) })
}
}
for _, mutation := range []struct{ field, value string }{
{"protocol", `"HTTP"`}, {"args", `"--lua-desync=fake"`},
{"group", `"googlevideo"`}, {"domains", `["other.example"]`}, {"domains", `[]`},
{"success_rate", `0.9`}, {"success_rate", `1.1`}, {"success_rate", `-1`},
{"coverage", `0.9`}, {"coverage", `1.1`}, {"coverage", `-1`},
{"passes_ok", `2`}, {"passes_ok", `4`}, {"passes_ok", `-1`},
{"passes_total", `2`}, {"passes_total", `4`}, {"passes_total", `-1`},
{"median_latency_ms", `-1`}, {"median_speed_kbps", `-1`},
} {
t.Run(mutation.field+"/"+mutation.value, func(t *testing.T) { check(t, mutation.field, json.RawMessage(mutation.value)) })
}
}
func TestRevalidateCandidateEarlyTimeout(t *testing.T) {
db := testStore(t)
args := "--lua-desync=multisplit"
saveProposal(t, db, args)
c, err := readStrategyCandidate(db, 1)
if err != nil {
t.Fatal(err)
}
var passing struct {
Strategies []json.RawMessage `json:"strategies"`
}
if err := json.Unmarshal([]byte(combinedReportFixture(args)), &passing); err != nil {
t.Fatal(err)
}
report := fmt.Sprintf(`{"domain":"www.youtube.com","total":2,"working":1,"strategies":[%s]}`, passing.Strategies[1])
okResult := "OK median 242ms, 3728.2 KB/s"
diagnostics := earlyTimeoutDiagnosticsFixture(args, "FAIL 1/2 timeout", okResult, 1)
malformedOK := map[string]string{
"ok-garbage": "OK garbage median garbage",
"ok-old-fixture": "OK 3/3 median 10ms 100kbps",
"ok-missing-latency": "OK median ms, 3728.2 KB/s",
"ok-negative-latency": "OK median -242ms, 3728.2 KB/s",
"ok-fractional-latency": "OK median 242.5ms, 3728.2 KB/s",
"ok-missing-speed": "OK median 242ms, KB/s",
"ok-negative-speed": "OK median 242ms, -3728.2 KB/s",
"ok-nan-speed": "OK median 242ms, NaN KB/s",
"ok-inf-speed": "OK median 242ms, Inf KB/s",
"ok-wrong-units": "OK median 242ms, 3728.2 kbps",
"ok-missing-comma": "OK median 242ms 3728.2 KB/s",
"ok-trailing-garbage": okResult + " garbage",
"ok-trailing-space": okResult + " ",
}
modes := []string{
"timeout", "two-timeouts", "tls13-timeout", "wrong-domain", "wrong-row-args", "wrong-heading-args", "missing-artifact", "stale-artifact", "missing-report", "runner-failed", "runner-sentinel", "cancelled",
"nfq-exited", "extra-line", "missing-heading", "duplicate-heading", "wrong-protocol", "missing-fail", "success-not-ok", "fail-on-success", "wrong-total", "missing-total", "negative-working", "wrong-working", "working-two", "missing-working", "null-strategies", "trailing-json", "bad-json",
"zero-count", "equal-counts", "reversed-counts", "excess-count", "not-timeout", "partial-success", "missing-metric", "invalid-metric", "wrong-row-target", "empty-with-one-success",
"first-pass-timeout", "third-pass-timeout", "two-first-pass-timeouts", "unknown-section-line", "infra-section-line", "missing-section-marker", "missing-summary-marker", "duplicate-section", "wrong-verifying-count", "wrong-loaded-count", "wrong-summary-count", "wrong-summary-working", "unindented-heading", "extra-indent-heading", "wrong-dns-host", "unknown-preamble-line",
}
for mode := range malformedOK {
modes = append(modes, mode)
}
for _, mode := range modes {
t.Run(mode, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "check")
r, diagnostic := report, diagnostics
if result, ok := malformedOK[mode]; ok {
diagnostic = strings.Replace(diagnostic, okResult, result, 1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
switch mode {
case "two-timeouts", "empty-with-one-success":
r = `{"domain":"www.youtube.com","total":2,"working":0,"strategies":[]}`
if mode == "two-timeouts" {
diagnostic = earlyTimeoutDiagnosticsFixture(args, "FAIL 1/2 timeout", "FAIL 2/3 timeout", 0)
}
case "two-first-pass-timeouts":
r = `{"domain":"www.youtube.com","total":2,"working":0,"strategies":[]}`
diagnostic = earlyTimeoutDiagnosticsFixture(args, "FAIL 0/1 timeout", "FAIL 0/1 timeout", 0)
case "first-pass-timeout":
diagnostic = strings.Replace(diagnostic, "FAIL 1/2 timeout", "FAIL 0/1 timeout", 1)
case "third-pass-timeout":
diagnostic = strings.Replace(diagnostic, "FAIL 1/2 timeout", "FAIL 2/3 timeout", 1)
case "tls13-timeout":
r = fmt.Sprintf(`{"domain":"www.youtube.com","total":2,"working":1,"strategies":[%s]}`, passing.Strategies[0])
diagnostic = earlyTimeoutDiagnosticsFixture(args, okResult, "FAIL 1/2 timeout", 1)
case "wrong-domain":
r = strings.Replace(r, "www.youtube.com", "other.example", 1)
case "wrong-row-args":
r = strings.Replace(r, args, "--lua-desync=fake", 1)
case "wrong-heading-args":
diagnostic = strings.Replace(diagnostic, args, "--lua-desync=fake", 1)
case "nfq-exited":
diagnostic += "nfqws2 exited with status 1\n"
case "extra-line":
diagnostic += "unexpected infrastructure output\n"
case "missing-heading":
diagnostic = strings.Replace(diagnostic, " [1/2] HTTPS/TLS1.2 nfqws2 "+args+"\n", "", 1)
case "duplicate-heading":
diagnostic = strings.Replace(diagnostic, " [1/2]", " [1/2] HTTPS/TLS1.2 nfqws2 "+args+"\n [1/2]", 1)
case "unknown-section-line", "infra-section-line":
line := "unknown output"
if mode == "infra-section-line" {
line = "nfqws2 exited with status 1"
}
diagnostic = strings.Replace(diagnostic, " FAIL", line+"\n FAIL", 1)
case "missing-section-marker", "missing-summary-marker", "duplicate-section", "wrong-verifying-count", "wrong-loaded-count", "wrong-summary-count", "wrong-summary-working", "unindented-heading", "extra-indent-heading", "wrong-dns-host", "unknown-preamble-line":
replacements := map[string][2]string{
"missing-section-marker": {"=== Checking strategies (data transfer) ===\n", ""},
"missing-summary-marker": {"=== Check summary ===\n", ""},
"duplicate-section": {"=== Checking strategies (data transfer) ===", "=== Checking strategies (data transfer) ===\n=== Checking strategies (data transfer) ==="},
"wrong-verifying-count": {"Verifying 2 strategies", "Verifying 3 strategies"},
"wrong-loaded-count": {"loaded 2 strategies", "loaded 3 strategies"},
"wrong-summary-count": {"total: 2", "total: 3"},
"wrong-summary-working": {"working: 1", "working: 0"},
"unindented-heading": {" [1/2]", "[1/2]"},
"extra-indent-heading": {" [1/2]", " [1/2]"},
"wrong-dns-host": {"www.youtube.com ->", "other.example ->"},
"unknown-preamble-line": {"=== DNS resolve ===", "unknown output\n=== DNS resolve ==="},
}
pair := replacements[mode]
diagnostic = strings.Replace(diagnostic, pair[0], pair[1], 1)
case "wrong-protocol":
diagnostic = strings.Replace(diagnostic, "HTTPS/TLS1.2", "HTTPS/TLS1.3", 1)
case "missing-fail":
diagnostic = strings.Replace(diagnostic, " FAIL 1/2 timeout\n", "", 1)
case "success-not-ok":
diagnostic = strings.Replace(diagnostic, "OK median", "unknown median", 1)
case "fail-on-success":
diagnostic = strings.Replace(diagnostic, okResult, "FAIL 1/2 timeout", 1)
case "wrong-total", "missing-total", "negative-working", "wrong-working", "working-two", "missing-working", "null-strategies":
replacements := map[string][2]string{
"wrong-total": {`"total":2`, `"total":3`}, "missing-total": {`"total":2,`, ""},
"negative-working": {`"working":1`, `"working":-1`}, "wrong-working": {`"working":1`, `"working":0`},
"working-two": {`"working":1`, `"working":2`}, "missing-working": {`"working":1,`, ""},
"null-strategies": {`"strategies":[` + string(passing.Strategies[1]) + `]`, `"strategies":null`},
}
pair := replacements[mode]
r = strings.Replace(r, pair[0], pair[1], 1)
case "trailing-json":
r += `{}`
case "bad-json":
r = `{`
case "zero-count", "equal-counts", "reversed-counts", "excess-count", "not-timeout":
replacements := map[string]string{"zero-count": "FAIL 0/2 timeout", "equal-counts": "FAIL 2/2 timeout", "reversed-counts": "FAIL 3/2 timeout", "excess-count": "FAIL 1/4 timeout", "not-timeout": "FAIL 1/2 connection refused"}
diagnostic = strings.Replace(diagnostic, "FAIL 1/2 timeout", replacements[mode], 1)
case "partial-success":
r = strings.Replace(r, `"passes_ok":3`, `"passes_ok":2`, 1)
case "missing-metric":
r = strings.Replace(r, `"coverage":1`, `"coverage":null`, 1)
case "invalid-metric":
r = strings.Replace(r, `"median_speed_kbps":100`, `"median_speed_kbps":-1`, 1)
case "wrong-row-target":
r = strings.Replace(r, `"protocol":`, `"domains":["other.example"],"protocol":`, 1)
}
if mode == "stale-artifact" {
if err := os.WriteFile(path+".combined.json.stderr", []byte(diagnostic), 0600); err != nil {
t.Fatal(err)
}
}
calls := 0
check := func(_ context.Context, _, gotPath, gotArgs, host string, _, _ io.Writer) error {
calls++
if gotPath != path || gotArgs != args || host != "www.youtube.com" {
t.Fatal("unexpected checker request")
}
if mode != "missing-report" {
if err := os.WriteFile(path+".combined.json", []byte(r), 0600); err != nil {
return err
}
}
if mode != "missing-artifact" {
if err := os.WriteFile(path+".combined.json.stderr", []byte(diagnostic), 0600); err != nil {
return err
}
}
switch mode {
case "runner-failed":
return fmt.Errorf("checker exited")
case "runner-sentinel":
return errCandidateNonpass
case "cancelled":
cancel()
}
return nil
}
_, err := revalidateCandidate(ctx, c, "frontend", "www.youtube.com", path, io.Discard, check)
nonpass := mode == "timeout" || mode == "two-timeouts" || mode == "tls13-timeout" || mode == "first-pass-timeout" || mode == "third-pass-timeout" || mode == "two-first-pass-timeouts"
if err == nil || errors.Is(err, errCandidateNonpass) != nonpass {
t.Fatalf("nonpass=%v err=%v", nonpass, err)
}
if nonpass {
if _, err := combinedBlockcheckOutputForHost(strings.NewReader(r), args, "www.youtube.com"); err == nil {
t.Fatal("early timeout report qualified for apply")
}
}
if mode == "stale-artifact" && calls != 0 {
t.Fatal("checker invoked with stale diagnostic evidence")
}
})
}
}