feat(storage): persist verified strategy scenarios
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
cf79e89efd
commit
7fad9b3b5c
7 files changed
+466
-12
No files matched your search
+1
-1
@@ -6,7 +6,7 @@
|
||||
|
||||
- CLI на Go: `tune youtube`, `discover youtube`, `status`, `history`, `strategies youtube`, `render`. `check youtube` пока намеренно отказывает; `apply` и `rollback` не реализованы.
|
||||
- `tune youtube` сканирует frontend `youtube.com`, проверяет shortlist через `blockcheckw` и выдаёт один TLS-профиль только после совместной проверки TLS 1.2 и 1.3 по 3/3. Это предложение конфигурации, оно ничего не применяет.
|
||||
- SQLite без CGO: миграции до схемы v2, атомарное сохранение успешных/неуспешных tune-запусков, SHA-256 fingerprint нормализованных аргументов, `status`, read-only `history` и список предложенных стратегий. База и каталоги создаются с ограниченными правами.
|
||||
- SQLite без CGO: миграции до схемы v3, атомарное сохранение успешных/неуспешных tune-запусков, SHA-256 fingerprint нормализованных аргументов, `status`, read-only `history` и список предложенных стратегий. v3 добавляет неизменяемые определения кандидатов и evidence по endpoint/protocol без active-состояния; старые предложения не переносятся в кандидаты. База и каталоги создаются с ограниченными правами.
|
||||
- `discover youtube --video-id ID --proxy socks5://HOST:PORT` делает запрос к YouTube Android Player API, валидирует `serverAbrStreamingUrl` и выдаёт только hostname из `googlevideo.com`; подписанный URL не выводится.
|
||||
- Проверки кода после последних изменений: локальные `go test ./...` и `go build` прошли. Docker build проходил после добавления SQLite-зависимости и исправления `.dockerignore`.
|
||||
|
||||
|
||||
@@ -50,8 +50,18 @@ it must be writable. In Docker, mount that directory to retain history:
|
||||
docker run --rm -v autotune-data:/var/lib/zapret-autotune zapret-autotune status
|
||||
```
|
||||
|
||||
Schema version 2 is initialized transactionally on `tune` and `status`; subsequent
|
||||
Schema version 3 is initialized transactionally on `tune` and `status`; subsequent
|
||||
opens are idempotent and newer, unsupported schema versions are rejected.
|
||||
|
||||
Schema v3 adds immutable strategy candidate definitions (group, declared exact
|
||||
endpoints, args and normalized-args fingerprint) and endpoint/protocol evidence.
|
||||
Each newly successful tune saves its exact verified combined frontend profile for
|
||||
`youtube.com`, both TLS measurements, raw measurement JSON and checked timestamps
|
||||
in the same transaction as the existing run. Qualification requires every declared
|
||||
endpoint to pass exactly 3/3 for both TLS1.2 and TLS1.3, with full success/coverage.
|
||||
Existing v2 history is unchanged and is not backfilled into independent candidates.
|
||||
Candidates have no active/installed state; apply, monitor and GGC integration remain
|
||||
unimplemented.
|
||||
Proposed runs store a SHA-256 fingerprint of validated arguments with separator
|
||||
whitespace normalized and argument order preserved; repeated proposals retain
|
||||
separate history rows. Failed runs have no fingerprint. Version 1 history is
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Additive only: old proposals are not independently qualified candidates.
|
||||
const candidateSchema = `
|
||||
CREATE TABLE strategy_candidates (
|
||||
id INTEGER PRIMARY KEY,
|
||||
source_run_id INTEGER UNIQUE REFERENCES tune_runs(id),
|
||||
strategy_group TEXT NOT NULL CHECK (strategy_group IN ('frontend', 'googlevideo')),
|
||||
endpoints TEXT NOT NULL CHECK (json_valid(endpoints) AND json_type(endpoints) = 'array' AND json_array_length(endpoints) > 0),
|
||||
args TEXT NOT NULL CHECK (length(args) > 0),
|
||||
fingerprint TEXT NOT NULL CHECK (length(fingerprint) = 64 AND length(CAST(fingerprint AS BLOB)) = 64 AND fingerprint NOT GLOB '*[^0-9a-f]*')
|
||||
);
|
||||
CREATE TRIGGER immutable_candidate_update BEFORE UPDATE ON strategy_candidates
|
||||
BEGIN SELECT RAISE(ABORT, 'candidate definitions are immutable'); END;
|
||||
CREATE TRIGGER immutable_candidate_delete BEFORE DELETE ON strategy_candidates
|
||||
BEGIN SELECT RAISE(ABORT, 'candidate definitions are immutable'); END;
|
||||
CREATE TABLE candidate_evidence (
|
||||
candidate_id INTEGER NOT NULL REFERENCES strategy_candidates(id),
|
||||
endpoint TEXT NOT NULL,
|
||||
protocol TEXT NOT NULL CHECK (protocol IN ('tls12', 'tls13')),
|
||||
passes_ok INTEGER NOT NULL CHECK (passes_ok >= 0 AND passes_ok <= passes_total),
|
||||
passes_total INTEGER NOT NULL CHECK (passes_total >= 0),
|
||||
success_rate REAL NOT NULL CHECK (success_rate >= 0 AND success_rate <= 1),
|
||||
coverage REAL NOT NULL CHECK (coverage >= 0 AND coverage <= 1),
|
||||
median_latency_ms REAL NOT NULL CHECK (median_latency_ms >= 0),
|
||||
median_speed_kbps REAL NOT NULL CHECK (median_speed_kbps >= 0),
|
||||
checked_at TEXT NOT NULL CHECK (length(checked_at) > 0),
|
||||
raw_measurement TEXT NOT NULL CHECK (json_valid(raw_measurement)),
|
||||
PRIMARY KEY (candidate_id, endpoint, protocol)
|
||||
);
|
||||
CREATE TRIGGER evidence_scope_insert BEFORE INSERT ON candidate_evidence
|
||||
WHEN NOT EXISTS (SELECT 1 FROM strategy_candidates c, json_each(c.endpoints) e
|
||||
WHERE c.id = NEW.candidate_id AND e.value = NEW.endpoint)
|
||||
BEGIN SELECT RAISE(ABORT, 'evidence endpoint outside candidate scope'); END;
|
||||
CREATE TRIGGER evidence_scope_update BEFORE UPDATE ON candidate_evidence
|
||||
WHEN NOT EXISTS (SELECT 1 FROM strategy_candidates c, json_each(c.endpoints) e
|
||||
WHERE c.id = NEW.candidate_id AND e.value = NEW.endpoint)
|
||||
BEGIN SELECT RAISE(ABORT, 'evidence endpoint outside candidate scope'); END;
|
||||
PRAGMA user_version = 3;`
|
||||
|
||||
func insertStrategyCandidate(tx *sql.Tx, group string, endpoints []string, args string, sourceRun interface{}) (int64, error) {
|
||||
c := candidate{Group: group, Domains: append([]string(nil), endpoints...), Args: args}
|
||||
if err := c.validateMeasurements(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for i := 1; i < len(c.Domains); i++ {
|
||||
if c.Domains[i] == c.Domains[i-1] {
|
||||
return 0, fmt.Errorf("duplicate endpoint")
|
||||
}
|
||||
}
|
||||
fingerprint, err := strategyFingerprint(args)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
scope, err := json.Marshal(c.Domains)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
result, err := tx.Exec(`INSERT INTO strategy_candidates (source_run_id, strategy_group, endpoints, args, fingerprint) VALUES (?, ?, ?, ?, ?)`, sourceRun, group, string(scope), args, fingerprint)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
// Evidence is tied to the immutable definition, never inferred from scan output.
|
||||
func saveCandidateEvidence(tx *sql.Tx, id int64, endpoint, protocol string, raw []byte, checked time.Time) error {
|
||||
if !validDomain(endpoint) || (protocol != "tls12" && protocol != "tls13") || checked.IsZero() {
|
||||
return fmt.Errorf("invalid evidence endpoint, protocol or checked timestamp")
|
||||
}
|
||||
endpoint = strings.ToLower(endpoint)
|
||||
var group, args string
|
||||
if err := tx.QueryRow(`SELECT strategy_group, args FROM strategy_candidates WHERE id = ?`, id).Scan(&group, &args); err != nil {
|
||||
return err
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &fields); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, key := range []string{"args", "success_rate", "coverage", "passes_ok", "passes_total", "median_latency_ms", "median_speed_kbps"} {
|
||||
if value, ok := fields[key]; !ok || bytes.Equal(bytes.TrimSpace(value), []byte("null")) {
|
||||
return fmt.Errorf("missing evidence %s", key)
|
||||
}
|
||||
}
|
||||
c := candidate{Group: group, Domains: []string{endpoint}}
|
||||
if err := json.Unmarshal(raw, &c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.validateMeasurements(); err != nil {
|
||||
return err
|
||||
}
|
||||
if c.Args != args || c.Group != group || len(c.Domains) != 1 || c.Domains[0] != endpoint {
|
||||
return fmt.Errorf("evidence does not match candidate definition")
|
||||
}
|
||||
var rowProtocol struct {
|
||||
Protocol string `json:"protocol"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &rowProtocol); err != nil {
|
||||
return err
|
||||
}
|
||||
if normalizeProtocol(rowProtocol.Protocol) != protocol {
|
||||
return fmt.Errorf("evidence protocol mismatch")
|
||||
}
|
||||
_, err := tx.Exec(`INSERT INTO candidate_evidence (candidate_id, endpoint, protocol, passes_ok, passes_total, success_rate, coverage, median_latency_ms, median_speed_kbps, checked_at, raw_measurement) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, id, endpoint, protocol, c.PassesOK, c.PassesTotal, c.SuccessRate, c.Coverage, c.MedianLatency, c.MedianSpeed, checked.UTC().Format(time.RFC3339Nano), string(raw))
|
||||
return err
|
||||
}
|
||||
|
||||
type storedCandidate struct {
|
||||
ID int64
|
||||
Group, Args, Fingerprint string
|
||||
Endpoints []string
|
||||
Qualified bool
|
||||
}
|
||||
|
||||
// One shared qualification rule for the complete endpoint × TLS matrix.
|
||||
// Missing or failed evidence is never a qualification; this is not active state.
|
||||
func readStrategyCandidate(db *sql.DB, id int64) (storedCandidate, error) {
|
||||
var c storedCandidate
|
||||
var scope string
|
||||
err := db.QueryRow(`SELECT id, strategy_group, args, fingerprint, endpoints,
|
||||
NOT EXISTS (SELECT 1 FROM json_each(c.endpoints) e CROSS JOIN
|
||||
(SELECT 'tls12' AS protocol UNION ALL SELECT 'tls13') p
|
||||
WHERE NOT EXISTS (SELECT 1 FROM candidate_evidence r WHERE r.candidate_id = c.id
|
||||
AND r.endpoint = e.value AND r.protocol = p.protocol AND r.passes_ok = 3
|
||||
AND r.passes_total = 3 AND r.success_rate = 1 AND r.coverage = 1))
|
||||
FROM strategy_candidates c WHERE id = ?`, id).Scan(&c.ID, &c.Group, &c.Args, &c.Fingerprint, &scope, &c.Qualified)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(scope), &c.Endpoints)
|
||||
return c, err
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func candidateCounts(t *testing.T, db *sql.DB, candidates, evidence int) {
|
||||
t.Helper()
|
||||
for table, want := range map[string]int{"strategy_candidates": candidates, "candidate_evidence": evidence} {
|
||||
var got int
|
||||
if err := db.QueryRow("SELECT count(*) FROM " + table).Scan(&got); err != nil || got != want {
|
||||
t.Fatalf("%s count=%d want=%d err=%v", table, got, want, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertSavedCandidate(t *testing.T, db *sql.DB, runID int64, args string) {
|
||||
t.Helper()
|
||||
var id int64
|
||||
if err := db.QueryRow("SELECT id FROM strategy_candidates WHERE source_run_id = ?", runID).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := readStrategyCandidate(db, id)
|
||||
want, fingerprintErr := strategyFingerprint(args)
|
||||
if err != nil || fingerprintErr != nil || !c.Qualified || c.Group != "frontend" || c.Args != args || c.Fingerprint != want || len(c.Endpoints) != 1 || c.Endpoints[0] != "youtube.com" {
|
||||
t.Fatalf("candidate=%+v err=%v fingerprint err=%v", c, err, fingerprintErr)
|
||||
}
|
||||
var report struct {
|
||||
Strategies []json.RawMessage `json:"strategies"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(combinedReportFixture(args)), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i, protocol := range []string{"tls12", "tls13"} {
|
||||
var ok, total int
|
||||
var success, coverage, latency, speed float64
|
||||
var checked, raw string
|
||||
err := db.QueryRow(`SELECT passes_ok, passes_total, success_rate, coverage, median_latency_ms, median_speed_kbps, checked_at, raw_measurement FROM candidate_evidence WHERE candidate_id = ? AND endpoint = 'youtube.com' AND protocol = ?`, id, protocol).Scan(&ok, &total, &success, &coverage, &latency, &speed, &checked, &raw)
|
||||
if err != nil || ok != 3 || total != 3 || success != 1 || coverage != 1 || latency != 10 || speed != 100 || raw != string(report.Strategies[i]) {
|
||||
t.Fatalf("%s evidence=%d/%d %v/%v %v/%v raw=%q err=%v", protocol, ok, total, success, coverage, latency, speed, raw, err)
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339Nano, checked); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use the existing populated source fixture, retain its real legacy v2 schema,
|
||||
// and remove only additive v3 objects to exercise a populated v2 upgrade.
|
||||
func populatedV2Store(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
db := populatedV1Store(t, " --lua-desync=fake --lua-desync=multisplit ")
|
||||
if err := migrate(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`DROP TABLE candidate_evidence; DROP TABLE strategy_candidates; PRAGMA user_version = 2;`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func legacySnapshot(t *testing.T, db *sql.DB) string {
|
||||
t.Helper()
|
||||
var runs, protocols, schema string
|
||||
for _, q := range []struct {
|
||||
query string
|
||||
dest *string
|
||||
}{
|
||||
{`SELECT group_concat(row, ';') FROM (SELECT quote(id)||','||quote(started_at)||','||quote(finished_at)||','||quote(target)||','||quote(state)||','||quote(args)||','||quote(output)||','||quote(error)||','||quote(fingerprint) AS row FROM tune_runs ORDER BY id)`, &runs},
|
||||
{`SELECT group_concat(row, ';') FROM (SELECT quote(run_id)||','||quote(protocol)||','||quote(passes_ok)||','||quote(passes_total) AS row FROM protocol_results ORDER BY run_id, protocol)`, &protocols},
|
||||
{`SELECT group_concat(sql, ';') FROM (SELECT sql FROM sqlite_master WHERE name IN ('tune_runs', 'protocol_results', 'proposed_results_only', 'fingerprint_before_insert', 'fingerprint_before_update') ORDER BY name)`, &schema},
|
||||
} {
|
||||
if err := db.QueryRow(q.query).Scan(q.dest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return runs + "\n" + protocols + "\n" + schema
|
||||
}
|
||||
|
||||
func TestPopulatedV2CandidateMigration(t *testing.T) {
|
||||
db := populatedV2Store(t)
|
||||
before := legacySnapshot(t, db)
|
||||
history, err := tuneHistory(db, 20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
latest, err := latestRun(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := migrate(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var version int
|
||||
if err := db.QueryRow("PRAGMA user_version").Scan(&version); err != nil || version != 3 {
|
||||
t.Fatalf("version=%d err=%v", version, err)
|
||||
}
|
||||
if before != legacySnapshot(t, db) {
|
||||
t.Fatal("v2 history/schema changed")
|
||||
}
|
||||
afterHistory, err := tuneHistory(db, 20)
|
||||
if err != nil || afterHistory != history {
|
||||
t.Fatalf("history changed: %q err=%v", afterHistory, err)
|
||||
}
|
||||
afterLatest, err := latestRun(db)
|
||||
if err != nil || afterLatest != latest {
|
||||
t.Fatalf("latest changed: %+v err=%v", afterLatest, err)
|
||||
}
|
||||
candidateCounts(t, db, 0, 0)
|
||||
}
|
||||
|
||||
func TestV2CandidateMigrationRollback(t *testing.T) {
|
||||
db := populatedV2Store(t)
|
||||
before := legacySnapshot(t, db)
|
||||
if _, err := db.Exec("CREATE TABLE candidate_evidence (conflict INTEGER)"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := migrate(db); err == nil {
|
||||
t.Fatal("expected migration failure")
|
||||
}
|
||||
var version, partial int
|
||||
if err := db.QueryRow("PRAGMA user_version").Scan(&version); err != nil || version != 2 {
|
||||
t.Fatalf("version=%d err=%v", version, err)
|
||||
}
|
||||
if err := db.QueryRow("SELECT count(*) FROM sqlite_master WHERE name = 'strategy_candidates'").Scan(&partial); err != nil || partial != 0 || before != legacySnapshot(t, db) {
|
||||
t.Fatalf("partial migration=%d err=%v", partial, err)
|
||||
}
|
||||
if _, err := db.Exec("DROP TABLE candidate_evidence"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := migrate(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCandidatePersistenceAtomic(t *testing.T) {
|
||||
for _, table := range []string{"strategy_candidates", "candidate_evidence", "protocol_results"} {
|
||||
t.Run(table, func(t *testing.T) {
|
||||
db := testStore(t)
|
||||
args := "--lua-desync=multisplit"
|
||||
output := saveProposal(t, db, args)
|
||||
assertSavedCandidate(t, db, 1, args)
|
||||
when := ""
|
||||
if table != "strategy_candidates" {
|
||||
when = " WHEN NEW.protocol = 'tls13'"
|
||||
}
|
||||
if _, err := db.Exec(`CREATE TRIGGER reject_candidate_save BEFORE INSERT ON ` + table + when + ` BEGIN SELECT RAISE(ABORT, 'injected failure'); END;`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := saveRun(db, time.Now(), args, output, []byte(combinedReportFixture(args)), nil); err == nil {
|
||||
t.Fatal("expected save failure")
|
||||
}
|
||||
assertRowCounts(t, db, 1, 2)
|
||||
candidateCounts(t, db, 1, 2)
|
||||
assertSavedCandidate(t, db, 1, args)
|
||||
})
|
||||
}
|
||||
db := testStore(t)
|
||||
if err := saveRun(db, time.Now(), "", "", nil, errors.New("scan failed")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
candidateCounts(t, db, 0, 0)
|
||||
}
|
||||
|
||||
func TestCandidateQualificationMatrix(t *testing.T) {
|
||||
db := testStore(t)
|
||||
args := "--lua-desync=multisplit"
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
id, err := insertStrategyCandidate(tx, "frontend", []string{"youtube.com", "www.youtube.com"}, args, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertQualified := func(want bool) {
|
||||
t.Helper()
|
||||
c, err := readStrategyCandidate(db, id)
|
||||
if err != nil || c.Qualified != want {
|
||||
t.Fatalf("qualified=%v want=%v err=%v", c.Qualified, want, err)
|
||||
}
|
||||
}
|
||||
var report struct {
|
||||
Strategies []json.RawMessage `json:"strategies"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(combinedReportFixture(args)), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertQualified(false)
|
||||
for i, endpoint := range []string{"youtube.com", "www.youtube.com"} {
|
||||
for j, protocol := range []string{"tls12", "tls13"} {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := saveCandidateEvidence(tx, id, endpoint, protocol, report.Strategies[j], time.Now()); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertQualified(i == 1 && j == 1)
|
||||
}
|
||||
}
|
||||
for _, endpoint := range []string{"youtube.com", "www.youtube.com"} {
|
||||
for _, protocol := range []string{"tls12", "tls13"} {
|
||||
for _, damage := range []string{"passes_ok = 2", "passes_total = 4", "success_rate = 0.5", "coverage = 0.5"} {
|
||||
if _, err := db.Exec("UPDATE candidate_evidence SET "+damage+" WHERE candidate_id = ? AND endpoint = ? AND protocol = ?", id, endpoint, protocol); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertQualified(false)
|
||||
if _, err := db.Exec("UPDATE candidate_evidence SET passes_ok = 3, passes_total = 3, success_rate = 1, coverage = 1 WHERE candidate_id = ? AND endpoint = ? AND protocol = ?", id, endpoint, protocol); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertQualified(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCandidateValidationAndImmutability(t *testing.T) {
|
||||
db := testStore(t)
|
||||
args := "--lua-desync=multisplit"
|
||||
saveProposal(t, db, args)
|
||||
for _, statement := range []string{
|
||||
`UPDATE strategy_candidates SET args = '--lua-desync=fake'`,
|
||||
`UPDATE strategy_candidates SET endpoints = '["example.com"]'`,
|
||||
`DELETE FROM strategy_candidates`,
|
||||
`UPDATE candidate_evidence SET endpoint = 'example.com'`,
|
||||
`UPDATE candidate_evidence SET protocol = 'quic'`,
|
||||
} {
|
||||
if _, err := db.Exec(statement); err == nil {
|
||||
t.Fatalf("accepted %s", statement)
|
||||
}
|
||||
}
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
for _, scope := range []struct {
|
||||
group string
|
||||
domains []string
|
||||
}{
|
||||
{"unknown", []string{"youtube.com"}}, {"frontend", nil},
|
||||
{"frontend", []string{"bad/domain"}}, {"frontend", []string{"youtube.com", "YOUTUBE.COM"}},
|
||||
} {
|
||||
if _, err := insertStrategyCandidate(tx, scope.group, scope.domains, args, nil); err == nil {
|
||||
t.Fatalf("accepted scope %+v", scope)
|
||||
}
|
||||
}
|
||||
var report struct {
|
||||
Strategies []json.RawMessage `json:"strategies"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(combinedReportFixture(args)), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, endpoint := range []string{"example.com", "bad/domain"} {
|
||||
if err := saveCandidateEvidence(tx, 1, endpoint, "tls12", report.Strategies[0], time.Now()); err == nil {
|
||||
t.Fatalf("accepted endpoint %q", endpoint)
|
||||
}
|
||||
}
|
||||
for _, raw := range []string{"{}", strings.Replace(string(report.Strategies[0]), args, "--lua-desync=fake", 1), strings.Replace(string(report.Strategies[0]), `"coverage":1`, `"coverage":null`, 1)} {
|
||||
if err := saveCandidateEvidence(tx, 1, "youtube.com", "tls12", []byte(raw), time.Now()); err == nil {
|
||||
t.Fatalf("accepted raw %q", raw)
|
||||
}
|
||||
}
|
||||
if err := saveCandidateEvidence(tx, 1, "youtube.com", "quic", report.Strategies[0], time.Now()); err == nil {
|
||||
t.Fatal("accepted unsupported protocol")
|
||||
}
|
||||
}
|
||||
@@ -117,7 +117,7 @@ func TestPopulatedV1FingerprintMigration(t *testing.T) {
|
||||
}
|
||||
assertRowCounts(t, db, 3, 4)
|
||||
var version int
|
||||
if err := db.QueryRow("PRAGMA user_version").Scan(&version); err != nil || version != 2 {
|
||||
if err := db.QueryRow("PRAGMA user_version").Scan(&version); err != nil || version != 3 {
|
||||
t.Fatalf("version=%d err=%v", version, err)
|
||||
}
|
||||
for id, input := range map[int]string{7: "--lua-desync=fake", 9: args} {
|
||||
|
||||
@@ -114,7 +114,7 @@ func migrate(db *sql.DB) error {
|
||||
if err = conn.QueryRowContext(ctx, "PRAGMA user_version").Scan(&version); err != nil {
|
||||
return err
|
||||
}
|
||||
if version > 2 {
|
||||
if version > 3 {
|
||||
return fmt.Errorf("unsupported database schema version %d", version)
|
||||
}
|
||||
if version == 0 {
|
||||
@@ -222,6 +222,11 @@ PRAGMA user_version = 1;`)
|
||||
return err
|
||||
}
|
||||
}
|
||||
if version < 3 {
|
||||
if _, err := conn.ExecContext(ctx, candidateSchema); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err = conn.ExecContext(ctx, "COMMIT")
|
||||
return err
|
||||
}
|
||||
@@ -250,11 +255,7 @@ func strategyFingerprint(args string) (string, error) {
|
||||
// Validation is shared with tune's output path; only combined 3/3 evidence is stored.
|
||||
func saveRun(db *sql.DB, started time.Time, args, output string, report []byte, runErr error) error {
|
||||
var evidence struct {
|
||||
Strategies []struct {
|
||||
Protocol string `json:"protocol"`
|
||||
PassesOK int `json:"passes_ok"`
|
||||
PassesTotal int `json:"passes_total"`
|
||||
} `json:"strategies"`
|
||||
Strategies []json.RawMessage `json:"strategies"`
|
||||
}
|
||||
state := "FAILED"
|
||||
var savedArgs, savedOutput, savedError, fingerprint interface{}
|
||||
@@ -291,10 +292,28 @@ func saveRun(db *sql.DB, started time.Time, args, output string, report []byte,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, row := range evidence.Strategies {
|
||||
var candidateID int64
|
||||
checked := time.Now().UTC()
|
||||
if runErr == nil {
|
||||
candidateID, err = insertStrategyCandidate(tx, "frontend", []string{"youtube.com"}, args, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, raw := range evidence.Strategies {
|
||||
var row struct {
|
||||
candidate
|
||||
Protocol string `json:"protocol"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &row); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO protocol_results (run_id, protocol, passes_ok, passes_total) VALUES (?, ?, ?, ?)`, id, normalizeProtocol(row.Protocol), row.PassesOK, row.PassesTotal); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := saveCandidateEvidence(tx, candidateID, "youtube.com", normalizeProtocol(row.Protocol), raw, checked); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ func assertProposedRun(t *testing.T, args, output string) {
|
||||
t.Fatalf("latest=%+v err=%v", r, err)
|
||||
}
|
||||
assertRowCounts(t, db, 1, 2)
|
||||
assertSavedCandidate(t, db, r.ID, args)
|
||||
for _, protocol := range []string{"tls12", "tls13"} {
|
||||
var ok, total int
|
||||
if err := db.QueryRow("SELECT passes_ok, passes_total FROM protocol_results WHERE run_id = ? AND protocol = ?", r.ID, protocol).Scan(&ok, &total); err != nil || ok != 3 || total != 3 {
|
||||
@@ -223,7 +224,7 @@ func TestConcurrentFirstOpens(t *testing.T) {
|
||||
}
|
||||
for _, db := range stores {
|
||||
var version, objects int
|
||||
if err := db.QueryRow("PRAGMA user_version").Scan(&version); err != nil || version != 2 {
|
||||
if err := db.QueryRow("PRAGMA user_version").Scan(&version); err != nil || version != 3 {
|
||||
t.Fatalf("version=%d err=%v", version, err)
|
||||
}
|
||||
if err := db.QueryRow(`SELECT count(*) FROM sqlite_master WHERE
|
||||
@@ -376,7 +377,7 @@ func TestSchemaConstraintsAndIncompleteEvidence(t *testing.T) {
|
||||
|
||||
func TestUnsupportedSchemaVersion(t *testing.T) {
|
||||
db := testStore(t)
|
||||
if _, err := db.Exec("PRAGMA user_version = 3"); err != nil {
|
||||
if _, err := db.Exec("PRAGMA user_version = 4"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := migrate(db); err == nil || !strings.Contains(err.Error(), "unsupported") {
|
||||
|
||||
Reference in new issue
Block a user