Files
zapret-autotune/cmd/zapret-autotune/resource_peer_tls_test.go
T
ZeusinaandGPT-6 Bot 2804d97063 fix: classify resource peer TLS rejections as recoverable damage
Keep verified curl peer TLS negatives unqualified without making them process-wide safety failures. Preserve local proxy/configuration, guard, cleanup and mixed cancellation vetoes; test owned verification, rollback and durable absence cooldown.

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

136 lines
5.0 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"io"
"os/exec"
"strings"
"testing"
"time"
)
func TestResourcePeerTLSCollectorBoundaries(t *testing.T) {
for _, code := range []int{7, 35, 51, 58, 60, 65} {
t.Run(fmt.Sprint(code), func(t *testing.T) {
negative := exec.Command("sh", "-c", fmt.Sprintf("exit %d", code)).Run()
for _, mixed := range []error{nil, context.Canceled, errors.New("unknown infrastructure"), &managedRecoveryFatal{Err: errors.New("guard failed")}} {
runner := func(_ context.Context, args []string, _ io.Reader, _, _ io.Writer) error {
if strings.Contains(strings.Join(args, " "), "--insecure") {
t.Fatal("certificate bypass")
}
return errors.Join(negative, mixed)
}
// Use curl's actual exit type for the unmixed production path.
if mixed == nil {
runner = func(context.Context, []string, io.Reader, io.Writer, io.Writer) error { return negative }
}
m, err := collectAssetMeasurement(context.Background(), "socks5://127.0.0.1:1080", "https://yt3.ggpht.com/fresh?private=secret", "yt3.ggpht.com", "tls12", "", runner)
if err == nil || len(m.Transfers) != 0 || strings.Contains(err.Error(), "secret") {
t.Fatal("accepted/leaked rejection", err)
}
peer := code == 35 || code == 51 || code == 60
var fatal *managedRecoveryFatal
if errors.As(err, &fatal) != (!peer || mixed != nil) {
t.Fatalf("code=%d mixed=%v fatal=%v", code, mixed, err)
}
if managedAssetTransferNonpass(err) != (peer && mixed == nil) || managedAssetHealthNonpass(err) != (peer && mixed == nil) {
t.Fatal("unsafe nonpass classification", err)
}
}
})
}
}
func TestManagedAvatarPeerTLSMonitorNoQualifiedCooldown(t *testing.T) {
for _, tc := range []struct {
code int
stage string
}{{35, "verify"}, {60, "verify"}, {35, "apply"}, {60, "apply"}} {
t.Run(fmt.Sprintf("%d/%s", tc.code, tc.stage), func(t *testing.T) {
d, front, _ := recoveryFixture(t, true)
r, events, _ := assetRuntimeFixture(t)
good := r.curl
negative := exec.Command("sh", "-c", fmt.Sprintf("exit %d", tc.code)).Run()
reject := func(ctx context.Context, args []string, in io.Reader, out, diag io.Writer) error {
if strings.Contains(strings.Join(args, " "), "--insecure") {
t.Fatal("certificate bypass")
}
return negative
}
if tc.stage == "verify" {
r.curl = reject
}
thumbnail := unitAssetCandidate(t, d.db, "i.ytimg.com", "--lua-desync=fake", true)
ggc := managedCandidateFixture(t, d.db, "googlevideo", "fresh.googlevideo.com", "--lua-desync=fake", true)
ids := []int64{front, thumbnail, ggc}
target, _, _ := managedCLIFixture(t)
initManagedCLI(t, target)
d.transaction.Dir = target.directory
if err := d.transaction.Apply(d.db, ids); err != nil {
t.Fatal(err)
}
before, err := d.transaction.read("config")
if 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/fresh", "yt3.ggpht.com": "https://yt3.ggpht.com/fresh"}, proxy: r.proxy, runner: reject}, nil
}
d.verifyAssets = r.verify
d.health = managedServiceHealth(r.proxy, d.frontHealth, d.resources, func(ctx context.Context, args []string, in io.Reader, out, diag io.Writer) error {
config, _ := io.ReadAll(in)
if strings.Contains(string(config), "yt3.ggpht.com") {
return negative
}
return good(ctx, args, strings.NewReader(string(config)), out, diag)
})
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 !errors.Is(err, errNoQualifiedStrategy) {
t.Fatal("not genuine candidate exhaustion", err)
}
}
waits := 0
monitor.wait = func(ctx context.Context, _ time.Duration) error {
state, err := readMonitorState(d.db, monitor.serviceID)
if err != nil || state.RecoveryRunning || state.CooldownUntil.Before(monitor.now().Add(2*time.Hour)) {
t.Fatal("absence pause not durable", state, err)
}
waits++
if waits == 2 {
cancel()
return ctx.Err()
}
return nil
}
if err := runManagedMonitor(ctx, monitor); !errors.Is(err, context.Canceled) {
t.Fatal("peer rejection killed monitor", err)
}
joins := 0
for _, event := range *events {
if event == "join" {
joins++
}
}
if joins != len(managedAssetSeeds) {
t.Fatal("candidate worker not joined", joins, *events)
}
assertActiveCandidates(t, d.transaction, d.db, ids)
after, err := d.transaction.read("config")
if err != nil || string(before) != string(after) {
t.Fatal("peer-negative apply changed committed config", err)
}
})
}
}