Files
zapret-autotune/cmd/zapret-autotune/managed_googlevideo_runtime_test.go
T

297 lines
10 KiB
Go

package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"strings"
"testing"
"time"
)
// No network: fake owned lifecycle and wire replies exercise the producer, not
// hardware readiness. The production factory adds the real guard/Lua hash gate.
func googlevideoRuntimeFixture(t *testing.T) (managedAssetRuntime, storedCandidate, []mediaResource, *[]string) {
t.Helper()
r, events, _ := assetRuntimeFixture(t)
c := storedCandidate{Group: "googlevideo", Mode: QualificationAdaptive, Args: managedGooglevideoArgs, Endpoints: []string{"rr1---sn-alpha.googlevideo.com", "rr2---sn-beta.googlevideo.com", "rr5---sn-gamma.googlevideo.com"}}
var resources []mediaResource
for _, host := range c.Endpoints {
resources = append(resources, mediaResource{url: fmt.Sprintf("https://%s/videoplayback?expire=%d&sig=private-secret", host, time.Now().Add(time.Hour).Unix()), host: host, kind: "video/mp4"})
}
r.curl = func(_ context.Context, args []string, stdin io.Reader, out, diagnostic io.Writer) error {
*events = append(*events, "media")
if strings.Contains(strings.Join(args, " "), "private-secret") || !strings.Contains(strings.Join(args, " "), "--range 0-65535") {
t.Fatal("private URL in argv or no range")
}
b, err := io.ReadAll(stdin)
if err != nil {
return err
}
raw, err := strconv.Unquote(strings.TrimSuffix(strings.TrimPrefix(string(b), "url = "), "\n"))
if err != nil {
return err
}
body := make([]byte, 1024)
copy(body, []byte{0, 0, 0, 24, 'f', 't', 'y', 'p'})
fmt.Fprint(out, "HTTP/1.1 206 Partial Content\r\nContent-Type: video/mp4\r\nContent-Length: 1024\r\nContent-Range: bytes 0-1023/4096\r\n\r\n")
out.Write(body)
metrics, _ := json.Marshal(map[string]interface{}{"url_effective": raw, "ssl_verify_result": 0, "num_redirects": 0, "http_code": 206, "time_total": 0.1, "speed_download": 10000, "size_download": len(body)})
fmt.Fprint(out, resourceTrailer, string(metrics))
tls := "TLSv1.2"
if strings.Contains(strings.Join(args, " "), "--tlsv1.3") {
tls = "TLSv1.3"
}
fmt.Fprintln(diagnostic, "SSL connection using", tls)
return nil
}
return r, c, resources, events
}
func TestManagedGooglevideoOwnedPostWarmupProducer(t *testing.T) {
r, c, resources, events := googlevideoRuntimeFixture(t)
runner, calls := r.curl, 0
r.curl = func(ctx context.Context, args []string, stdin io.Reader, out, diagnostic io.Writer) error {
calls++
if calls <= 2 {
return context.DeadlineExceeded
}
return runner(ctx, args, stdin, out, diagnostic)
}
p, err := r.verifyGooglevideo(context.Background(), c, resources)
if err != nil || p.producerSeal == nil || p.validate(c) != nil {
t.Fatal(p, err)
}
if calls != 26 || p.Hosts[0].WarmupAttempts != 4 || p.Hosts[0].WarmupFailures != 2 || len(p.Hosts[0].Passes) != 6 {
t.Fatal("hidden warmup", p.Hosts, calls)
}
joined := strings.Join(*events, ",")
if strings.Count(joined, "start") != 1 || !strings.HasSuffix(joined, "delete,join") {
t.Fatal(joined)
}
raw, _ := json.Marshal(p)
if strings.Contains(string(raw), "private-secret") || strings.Contains(string(raw), "videoplayback") {
t.Fatal("durable URL leak")
}
for _, h := range p.Hosts {
for _, pass := range h.Passes {
if pass.RangeStart != 0 || pass.RangeEnd != 1023 || pass.ResourceBytes != 4096 {
t.Fatal(pass)
}
}
}
}
func TestManagedGooglevideoRefusesUnresolvedAndUnknownSemantics(t *testing.T) {
for _, mode := range []string{"missing", "one-host", "same-root", "rewritten", "expired", "unsigned", "no-incoming", "blocking-incoming", "detector-payload", "detector", "stage-gap", "ignored-stage", "unknown-init", "unknown-key", "out-range"} {
t.Run(mode, func(t *testing.T) {
r, c, resources, events := googlevideoRuntimeFixture(t)
switch mode {
case "missing":
resources = nil
case "one-host":
resources = resources[:1]
case "same-root":
c.Endpoints[1] = "rr2---sn-alpha.googlevideo.com"
case "rewritten":
resources[0].host = c.Endpoints[1]
case "expired":
resources[0].url = "https://" + resources[0].host + "/videoplayback?expire=1&sig=private-secret"
case "unsigned":
resources[0].url = strings.Split(resources[0].url, "&sig=")[0]
case "no-incoming":
c.Args = strings.Replace(c.Args, " --in-range=-s34228", "", 1)
case "blocking-incoming":
c.Args = strings.Replace(c.Args, "--in-range=-s34228", "--in-range=0", 1)
case "detector-payload":
c.Args = strings.Replace(c.Args, "--payload=all", "--payload=tls_client_hello", 1)
case "detector":
c.Args = strings.Replace(c.Args, "circular:fails=1", "circular:success_detector=cond_true:fails=1", 1)
case "stage-gap":
c.Args = strings.Replace(c.Args, "strategy=2", "strategy=3", 1)
case "ignored-stage":
c.Args = strings.Replace(c.Args, ":strategy=2", "", 1)
case "unknown-init":
c.Args += " --lua-init=@/opt/zapret2/lua/unknown.lua"
case "unknown-key":
c.Args = strings.Replace(c.Args, "circular:fails=1", "circular:unknown=1:fails=1", 1)
case "out-range":
c.Args += " --out-range=0"
}
p, err := r.verifyGooglevideo(context.Background(), c, resources)
if err == nil || p.producerSeal != nil || len(*events) != 0 {
t.Fatal("captured unresolved inventory/config", p, err, *events)
}
})
}
}
func TestManagedGooglevideoWarmupBoundAndCleanupAttestation(t *testing.T) {
for _, mode := range []string{"nonpass", "root404", "cleanup", "queue-lost", "worker-exit"} {
t.Run(mode, func(t *testing.T) {
r, c, resources, events := googlevideoRuntimeFixture(t)
calls := 0
if mode == "nonpass" {
r.curl = func(context.Context, []string, io.Reader, io.Writer, io.Writer) error {
calls++
return context.DeadlineExceeded
}
}
if mode == "root404" {
r.curl = func(_ context.Context, args []string, stdin io.Reader, out, diagnostic io.Writer) error {
calls++
b, err := io.ReadAll(stdin)
if err != nil {
return err
}
raw, err := strconv.Unquote(strings.TrimSuffix(strings.TrimPrefix(string(b), "url = "), "\n"))
if err != nil {
return err
}
var diag bytes.Buffer
resourceWire(raw, "root", out, &diag)
tls := diag.String()
if strings.Contains(strings.Join(args, " "), "--tlsv1.3") {
tls = strings.ReplaceAll(tls, "TLSv1.2", "TLSv1.3")
}
io.WriteString(diagnostic, tls)
return nil
}
}
if mode == "cleanup" {
nft := r.nft
r.nft = func(ctx context.Context, input string) (string, error) {
if strings.HasPrefix(input, "delete") {
return "", errors.New("failed")
}
return nft(ctx, input)
}
}
if mode == "queue-lost" {
queue, n := r.queue, 0
r.queue = func(pid int) (bool, error) {
n++
if n == 3 {
return false, nil
}
return queue(pid)
}
}
if mode == "worker-exit" {
start := r.start
r.start = func(args []string) (managedAssetWorker, error) {
w, err := start(args)
ch := make(chan error, 1)
ch <- errors.New("dead")
w.done = ch
return w, err
}
}
p, err := r.verifyGooglevideo(context.Background(), c, resources)
if err == nil || p.Attestation != nil || p.producerSeal != nil {
t.Fatal("attested failed lifecycle", p, err)
}
if (mode == "nonpass" || mode == "root404") && (calls != 10 || p.Hosts[0].WarmupFailures != 10 || len(p.Hosts[0].Passes) != 0 || !strings.HasSuffix(strings.Join(*events, ","), "delete,join")) {
t.Fatal(p, calls, *events)
}
})
}
}
func TestManagedGooglevideoAdaptiveStorageAndReadGate(t *testing.T) {
r, c, resources, _ := googlevideoRuntimeFixture(t)
p, err := r.verifyGooglevideo(context.Background(), c, resources)
if err != nil {
t.Fatal(err)
}
db := testStore(t)
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
id, err := insertStrategyCandidate(tx, c.Group, c.Endpoints, c.Args, nil, c.Mode)
if err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(p)
var dto GooglevideoQualificationProof
if err := json.Unmarshal(raw, &dto); err != nil {
t.Fatal(err)
}
if dto.validate(c) != nil {
t.Fatal("DTO should be structurally valid")
}
if err := saveAdaptiveGooglevideoEvidence(tx, id, dto); err == nil {
t.Fatal("DTO alone authorized a write")
}
p.Hosts[0].WarmupFailures++
if err := saveAdaptiveGooglevideoEvidence(tx, id, p); err == nil {
t.Fatal("modified producer proof accepted")
}
p.Hosts[0].WarmupFailures--
if err := saveAdaptiveGooglevideoEvidence(tx, id, p); err != nil {
t.Fatal(err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
var n int
if err := db.QueryRow("SELECT count(*) FROM candidate_evidence WHERE candidate_id=?", id).Scan(&n); err != nil || n != 0 {
t.Fatal("manufactured cold metrics", n, err)
}
stored, err := readStrategyCandidate(db, id)
if err != nil || !stored.Qualified || stored.Mode != QualificationAdaptive {
t.Fatal(stored, err)
}
list, err := listQualifiedCandidates(db, "googlevideo", 1)
if err != nil || len(list) != 1 || list[0].ID != id {
t.Fatal(list, err)
}
if err := validateGooglevideoSuffixEvidence(db, stored); err != nil {
t.Fatal(err)
}
for _, mode := range []string{"malformed", "stale", "args", "attestation", "hidden-warmup", "media", "secret"} {
t.Run(mode, func(t *testing.T) {
proof, err := decodeGooglevideoProof(string(raw))
if err != nil {
t.Fatal(err)
}
switch mode {
case "stale":
proof.Attestation.CheckedAt = time.Now().Add(-25 * time.Hour)
case "args":
proof.Args = "--lua-desync=fake"
case "attestation":
proof.Attestation = nil
case "hidden-warmup":
proof.Hosts[0].WarmupAttempts = 0
case "media":
proof.Hosts[0].Passes[0].Status = 404
}
bad, _ := json.Marshal(proof)
if mode == "malformed" {
bad = []byte(`{"version":"bad"}`)
}
if mode == "secret" {
bad = append(bad[:len(bad)-1], []byte(`,"signed_url":"private-secret"}`)...)
}
if _, err := db.Exec("UPDATE googlevideo_scope_evidence SET proof=? WHERE candidate_id=?", string(bad), id); err != nil {
t.Fatal(err)
}
stored, err := readStrategyCandidate(db, id)
if err != nil || stored.Qualified {
t.Fatal(stored, err)
}
list, err := listQualifiedCandidates(db, "googlevideo", 1)
if err != nil || len(list) != 0 {
t.Fatal(list, err)
}
})
}
}