Files
zapret-autotune/cmd/zapret-autotune/image_body_rejection_test.go
T
ZeusinaandGPT-6 Bot e9a28c8cd3 fix: keep rejected image payloads recoverable across verification
Type decoder payload rejections once and share the image nonpass classifier across live health and owned verification. Retain strict qualification, malformed metadata, source-kind separation and mixed safety vetoes; exercise seed fallback, joined workers, fresh evidence and durable absence cooldown.

Co-authored-by: GPT-6 <gpt-6@openai.com>
2026-10-12 22:09:11 +03:00

201 lines
7.2 KiB
Go

package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"image"
"image/jpeg"
"io"
"strings"
"testing"
"time"
)
func rejectedImageRunner(mode string) subprocessRunner {
return func(_ context.Context, args []string, in io.Reader, out, diag io.Writer) error {
config, err := io.ReadAll(in)
if err != nil {
return err
}
raw := strings.TrimSuffix(strings.TrimPrefix(string(config), "url = \""), "\"\n")
body, contentType := resourceFixture(), "image/png"
switch mode {
case "jpeg":
var b bytes.Buffer
if err := jpeg.Encode(&b, image.NewRGBA(image.Rect(0, 0, 8, 8)), nil); err != nil {
return err
}
body, contentType = b.Bytes(), "image/jpeg"
if start := bytes.Index(body, []byte{0xff, 0xda}); start >= 0 {
body = body[:start+12]
}
case "png":
body = body[:len(body)/2]
case "magic":
body = []byte("not an image")
case "gif":
body, contentType = []byte("GIF89a\x01\x00\x01\x00\x00\x00\x00"), "image/gif"
case "dimensions":
body, contentType = []byte("GIF89a\xff\xff\xff\xff\x00\x00\x00"), "image/gif"
case "type":
contentType = "image/jpeg"
case "unknown":
return errors.New("invalid image body")
}
fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n", contentType, len(body))
if _, err := out.Write(body); err != nil {
return err
}
metrics, _ := json.Marshal(map[string]interface{}{"url_effective": raw, "ssl_verify_result": 0, "num_redirects": 0, "http_code": 200, "time_total": 0.1, "speed_download": 10000, "size_download": len(body)})
if mode == "footer" {
metrics = []byte("{malformed")
}
fmt.Fprint(out, resourceTrailer, string(metrics))
protocol := "TLSv1.2"
if strings.Contains(strings.Join(args, " "), "--tlsv1.3") {
protocol = "TLSv1.3"
}
fmt.Fprintln(diag, "SSL connection using "+protocol)
return nil
}
}
func TestImageBodyRejectionCollectorBoundaries(t *testing.T) {
for _, mode := range []string{"jpeg", "png", "magic", "gif", "dimensions", "type", "unknown", "footer"} {
t.Run(mode, func(t *testing.T) {
m, err := collectAssetMeasurement(context.Background(), "socks5://127.0.0.1:1080", "https://i.ytimg.com/image", "i.ytimg.com", "tls12", "", rejectedImageRunner(mode))
if err == nil || len(m.Transfers) != 0 {
t.Fatal("invalid image qualified", m, err)
}
ordinary := mode != "unknown" && mode != "footer"
var fatal *managedRecoveryFatal
if errors.As(err, &fatal) == ordinary || managedAssetTransferNonpass(err) != ordinary || managedAssetHealthNonpass(err) != ordinary {
t.Fatal("wrong rejection class", err)
}
if managedGooglevideoTransferNonpass(err) {
t.Fatal("image rejection became media absence")
}
for _, veto := range []error{context.Canceled, errors.New("unknown infrastructure"), &managedRecoveryFatal{Err: imageBodyRejection("invalid image body")}} {
mixed := resourceFailure(context.Background(), errors.Join(err, veto))
if !errors.As(mixed, &fatal) || managedAssetTransferNonpass(mixed) || managedAssetHealthNonpass(mixed) {
t.Fatal("mixed safety veto lost", mixed)
}
}
})
}
_, _, metadataErr := playerMediaResource([]byte("{malformed"))
if metadataErr == nil {
t.Fatal("malformed Player JSON accepted")
}
for _, err := range []error{metadataErr, errors.New("invalid normalized image evidence")} {
failure := resourceFailure(context.Background(), err)
var fatal *managedRecoveryFatal
if !errors.As(failure, &fatal) || managedAssetTransferNonpass(failure) || managedGooglevideoTransferNonpass(failure) {
t.Fatal("local metadata damage became absence", failure)
}
}
oversized := assetTransfer{Body: make([]byte, (5<<20)+1)}
if err := normalizeAssetBody(&oversized); err == nil || !managedAssetTransferNonpass(resourceFailure(context.Background(), err)) {
t.Fatal("image decoder budget rejection lost", err)
}
}
func TestManagedTruncatedImageMonitorRecovery(t *testing.T) {
for _, recoverable := range []bool{false, true} {
t.Run(fmt.Sprint(recoverable), func(t *testing.T) {
d, front, _ := recoveryFixture(t, true)
target, _, _ := managedCLIFixture(t)
initManagedCLI(t, target)
d.transaction.Dir = target.directory
r, events, _ := assetRuntimeFixture(t)
good, start := r.curl, r.start
attempts := 0
r.start = func(args []string) (managedAssetWorker, error) { attempts++; return start(args) }
r.curl = func(ctx context.Context, args []string, in io.Reader, out, diag io.Writer) error {
if recoverable && attempts >= 2 {
return good(ctx, args, in, out, diag)
}
return rejectedImageRunner("jpeg")(ctx, args, in, out, diag)
}
ggc := managedCandidateFixture(t, d.db, "googlevideo", "fresh.googlevideo.com", "--lua-desync=fake", true)
ids := []int64{front, ggc}
if err := d.transaction.Apply(d.db, ids); err != nil {
t.Fatal(err)
}
d.service = true
d.frontHealth = func(context.Context) (string, error) { return "OK", nil }
d.resources = func(context.Context) (managedServiceInventory, error) {
return managedServiceInventory{images: map[string]string{"i.ytimg.com": "https://i.ytimg.com/image"}, proxy: r.proxy, runner: r.curl}, nil
}
d.verifyAssets = r.verify
d.health = managedServiceHealth(r.proxy, d.frontHealth, d.resources, r.curl)
monitor := monitorRuntimeFixture(t)
monitor.db, monitor.target, monitor.threshold = d.db, target, 1
if err := wireManagedRecovery(&monitor, d, func(error) {}); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
monitor.reportRecoveryError = func(err error) {
if recoverable || !errors.Is(err, errNoQualifiedStrategy) {
t.Fatal("wrong recovery result", err)
}
}
waits := 0
monitor.wait = func(ctx context.Context, _ time.Duration) error {
state, err := readMonitorState(d.db, monitor.serviceID)
if err != nil || state.RecoveryRunning {
t.Fatal("recovery ownership retained", state, err)
}
if !recoverable && state.CooldownUntil.Before(monitor.now().Add(2*time.Hour)) {
t.Fatal("missing two-hour pause", state)
}
waits++
if waits == 2 {
cancel()
return ctx.Err()
}
return nil
}
if err := runManagedMonitor(ctx, monitor); !errors.Is(err, context.Canceled) {
t.Fatal("invalid payload killed monitor", err, *events)
}
want := len(managedAssetSeeds)
if recoverable {
want = 2
}
joins := 0
for _, event := range *events {
if event == "join" {
joins++
}
}
if attempts != want || joins != want {
t.Fatal("seed fallback/worker join failed", attempts, joins, *events)
}
active, err := d.transaction.ActiveCandidates(d.db)
if err != nil {
t.Fatal(err)
}
if recoverable {
if len(active) != 3 {
t.Fatal("fresh image not applied", active)
}
fresh, err := readStrategyCandidate(d.db, active[2])
if err != nil || !fresh.Qualified || fresh.Args != managedAssetSeeds[1] {
t.Fatal("invalid fresh evidence", fresh, err)
}
ids = append(ids, fresh.ID)
var rows int
if err := d.db.QueryRow("SELECT count(*) FROM candidate_evidence WHERE candidate_id=? AND passes_ok=3 AND passes_total=3", fresh.ID).Scan(&rows); err != nil || rows != 2 {
t.Fatal("missing both TLS 3/3 rows", rows, err)
}
}
assertActiveCandidates(t, d.transaction, d.db, ids)
})
}
}