Files
zapret-autotune/cmd/zapret-autotune/recover_test.go
T
2026-10-09 23:10:25 +03:00

194 lines
6.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestRecoverGooglevideo(t *testing.T) {
for _, mode := range []string{"newest-pass", "older-pass", "no-candidates", "unqualified", "all-failed", "runner-error", "cancelled", "malformed", "missing-report", "incomplete", "wrong-host", "wrong-args", "incomplete-passes", "discovery-error", "database-open", "database-read", "database-save"} {
t.Run(mode, func(t *testing.T) {
dir := t.TempDir()
t.Setenv("TMPDIR", dir)
t.Setenv("ZAPRET_AUTOTUNE_DB", filepath.Join(dir, "autotune.db"))
config := filepath.Join(dir, "active.conf")
if err := os.WriteFile(config, []byte("ACTIVE UNCHANGED"), 0600); err != nil {
t.Fatal(err)
}
db, err := openStore(databasePath())
if err != nil {
t.Fatal(err)
}
oldArgs, newArgs := "--lua-desync=fake", "--lua-desync=multisplit"
seeded := 0
if mode != "no-candidates" && mode != "unqualified" {
for _, args := range []string{oldArgs, newArgs} {
if err := saveGooglevideoRun(db, time.Now(), "old.googlevideo.com", args, []byte(combinedReportFixture(args)), nil); err != nil {
t.Fatal(err)
}
seeded++
}
}
if mode != "no-candidates" {
// Newest incomplete endpoint × TLS evidence must never be eligible.
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
if _, err := insertStrategyCandidate(tx, "googlevideo", []string{"old.googlevideo.com", "other.googlevideo.com"}, "--lua-desync=disorder", nil); err != nil {
t.Fatal(err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
}
if mode == "database-read" {
if _, err := db.Exec(`DROP TABLE candidate_evidence`); err != nil {
t.Fatal(err)
}
}
if mode == "database-save" {
if _, err := db.Exec(`CREATE TRIGGER reject_recovery BEFORE INSERT ON candidate_evidence BEGIN SELECT RAISE(ABORT, 'injected failure'); END`); err != nil {
t.Fatal(err)
}
}
db.Close()
if mode == "database-open" {
t.Setenv("ZAPRET_AUTOTUNE_DB", dir)
}
var order, checked []string
player := cachePlayer(t, &order)
if mode == "discovery-error" {
player = func(context.Context, []string, io.Writer, io.Writer) error {
order = append(order, "discover")
return fmt.Errorf("SECRET discovery failure")
}
}
scans := 0
scan := func(_ context.Context, _, host string, _, _ io.Writer) (string, error) {
scans++
order = append(order, "scan")
if host != cacheTestHost {
t.Fatal(host)
}
path := filepath.Join(dir, "scan")
return path, os.WriteFile(path+".check.json", []byte(reportFixture(t)), 0600)
}
check := func(_ context.Context, binary, path, args, host string, _, _ io.Writer) error {
if binary != "blockcheckw" || host != cacheTestHost {
t.Fatalf("wrong fresh scope: %s %s", binary, host)
}
order = append(order, "check")
if scans == 0 {
checked = append(checked, args)
}
report := combinedReportFixture(args)
if scans == 0 {
switch mode {
case "runner-error":
return fmt.Errorf("SECRET runner failure")
case "cancelled":
return context.Canceled
case "missing-report":
return nil
case "malformed":
report = `{"strategies":null}`
case "incomplete":
report = strings.Replace(report, "HTTPS/TLS1.3", "HTTPS/TLS1.2", 1)
case "wrong-host":
report = strings.ReplaceAll(report, `"protocol":`, `"domains":["old.googlevideo.com"],"protocol":`)
case "wrong-args":
report = combinedReportFixture(oldArgs)
case "incomplete-passes":
report = strings.ReplaceAll(report, `"passes_total":3`, `"passes_total":4`)
case "older-pass", "all-failed":
if mode == "all-failed" || args == newArgs {
report = strings.Replace(report, `"passes_ok":3`, `"passes_ok":2`, 1)
}
}
}
return os.WriteFile(path+".combined.json", []byte(report), 0600)
}
var stdout, stderr bytes.Buffer
code := recoverGooglevideo(cacheTestFlags, &stdout, &stderr, player, scan, check)
fallback := mode == "no-candidates" || mode == "unqualified" || mode == "all-failed"
success := fallback || mode == "newest-pass" || mode == "older-pass"
if (code == 0) != success || strings.Contains(stdout.String()+stderr.String(), "SECRET") {
t.Fatalf("code %d output %s diagnostics %s", code, stdout.String(), stderr.String())
}
wantScans := 0
if fallback {
wantScans = 1
}
if scans != wantScans {
t.Fatalf("scans %d want %d", scans, wantScans)
}
if mode != "database-open" && (len(order) == 0 || order[0] != "discover" || strings.Count(strings.Join(order, ","), "discover") != 1) {
t.Fatal(order)
}
if len(checked) > 0 && checked[0] != newArgs {
t.Fatal(checked)
}
if mode == "older-pass" || mode == "all-failed" {
if strings.Join(checked, ",") != newArgs+","+oldArgs {
t.Fatal(checked)
}
} else if mode == "newest-pass" && len(checked) != 1 {
t.Fatal(checked)
}
if !success && stdout.Len() != 0 {
t.Fatal("failed recovery emitted proposal")
}
if success && (!strings.Contains(stdout.String(), "--hostlist-domains=^"+cacheTestHost+" ") || !strings.Contains(stderr.String(), "NOT ACTIVE")) {
t.Fatal(stdout.String(), stderr.String())
}
active, err := os.ReadFile(config)
if err != nil || string(active) != "ACTIVE UNCHANGED" {
t.Fatalf("active config changed: %s %v", active, err)
}
if mode == "database-open" || mode == "database-read" {
return
}
db, err = openStore(databasePath())
if err != nil {
t.Fatal(err)
}
defer db.Close()
var legacy, proposals int
if err := db.QueryRow(`SELECT count(*) FROM tune_runs`).Scan(&legacy); err != nil || legacy != 0 {
t.Fatalf("frontend changed: %d %v", legacy, err)
}
if err := db.QueryRow(`SELECT count(*) FROM googlevideo_tune_runs WHERE state = 'PROPOSED'`).Scan(&proposals); err != nil {
t.Fatal(err)
}
want := seeded
if success {
want++
}
if proposals != want {
t.Fatalf("proposals %d want %d", proposals, want)
}
if success {
var id int64
if err := db.QueryRow(`SELECT candidate_id FROM googlevideo_tune_runs ORDER BY id DESC LIMIT 1`).Scan(&id); err != nil {
t.Fatal(err)
}
c, err := readStrategyCandidate(db, id)
if err != nil || !c.Qualified || len(c.Endpoints) != 1 || c.Endpoints[0] != cacheTestHost {
t.Fatalf("fresh candidate: %+v %v", c, err)
}
if mode == "newest-pass" && c.Args != newArgs || mode == "older-pass" && c.Args != oldArgs {
t.Fatal(c.Args)
}
}
})
}
}