Files
zapret-autotune/cmd/zapret-autotune/resource_qualify_test.go
T
2026-10-14 12:21:52 +03:00

333 lines
12 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"io"
"net"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
)
func qualifierFixture(t *testing.T, candidates ...int) (resourceQualifier, *resourcePlanStore, strategyRegistry, *[]string, *int) {
t.Helper()
dir := t.TempDir()
os.Chmod(dir, 0700)
store, e := openResourcePlanStore(dir, true)
if e != nil {
t.Fatal(e)
}
t.Cleanup(store.close)
raw := `{"version":1,"services":[{"id":"one","resources":[{"id":"good","group":"a","kind":"http","url":"https://a.example/good?SECRET","method":"GET","tls":"both","timeout_ms":1000,"expect":{"statuses":[200],"min_bytes":1,"max_bytes":1000}}]},{"id":"two","resources":[{"id":"other","group":"b","kind":"http","url":"https://a.example/other?SECRET","method":"GET","tls":"both","timeout_ms":1000,"expect":{"statuses":[200],"min_bytes":1,"max_bytes":1000}}]}]}`
cfg, e := parseResourceChecks([]byte(raw), func(string) (string, bool) { return "", false })
if e != nil {
t.Fatal(e)
}
source := resourceMonitorSource{config: cfg, server: "127.0.0.1:5353", proxy: "socks5h://127.0.0.1:1080", fingerprint: resourceMAC(store.files.key, []byte(raw))}
_, _, registry, _, _ := planFixture(t)
count := 1
if len(candidates) > 0 {
count = candidates[0]
}
registry.Strategies = registry.Strategies[:count]
b, _ := json.Marshal(registry)
if store.importDefinitions(b) != nil {
t.Fatal("registry import")
}
runtime, events, _ := assetRuntimeFixture(t)
calls := 0
q := resourceQualifier{runtime: runtime, source: func() (resourceMonitorSource, error) { return source, nil }, guard: func() error { return nil }, now: time.Now, filesHash: func() (string, error) { return strings.Repeat("a", 64), nil }}
q.restore = func(ctx context.Context) error { *events = append(*events, "restore"); return ctx.Err() }
q.attest = func(_ context.Context, pid int, args []string, d strategyDefinition) (qualificationAttestation, error) {
if !strings.Contains(strings.Join(args, " "), "--filter-l7=tls") {
t.Fatal("generic TLS selector not enforced")
}
*events = append(*events, "attest")
return qualificationAttestation{Collector: "owned-resource-nfq-v1", Runtime: "nfqws2-1.0.2", Definition: planDigest(d), LuaHash: strings.Repeat("a", 64), RulesHash: planDigest(managedAssetRules), WorkerArgsHash: planDigest(args), Queue: 200, PID: pid, StartTime: 456}, nil
}
q.checks = resourceCheckDeps{guard: func() error { return nil }, resolve: func(context.Context, string, string) ([]net.IP, error) { return []net.IP{net.ParseIP("8.8.8.8")}, nil }, runner: func(_ context.Context, args []string, stdin io.Reader, out, diagnostics io.Writer) error {
calls++
*events = append(*events, "capture")
b, e := io.ReadAll(stdin)
if e != nil {
return e
}
if !strings.Contains(string(b), "connect-to =") || strings.Contains(strings.Join(args, " "), "SECRET") {
t.Fatal("private pin/argv violated")
}
raw, e := strconv.Unquote(strings.TrimPrefix(strings.Split(string(b), "\n")[0], "url = "))
if e != nil {
return e
}
protocol := "tls12"
if strings.Contains(strings.Join(args, " "), "--tlsv1.3") {
protocol = "tls13"
}
configuredWire(raw, "GET", protocol, "pass", out, diagnostics)
return nil
}}
return q, store, registry, events, &calls
}
func TestResourceQualifierCompletedProducer(t *testing.T) {
q, s, r, events, calls := qualifierFixture(t)
proof, e := q.complete(context.Background(), s, r)
if e != nil {
t.Fatal(e)
}
if *calls != 12 || len(proof.matrix.Runs) != 1 || len(proof.matrix.Runs[0].Checks) != 4 {
t.Fatalf("real repeats %d %+v", *calls, proof)
}
for _, c := range proof.matrix.Runs[0].Checks {
if c.Outcome != "pass" || c.Attempts != 3 {
t.Fatalf("%+v", c)
}
}
last := -1
for _, event := range []string{"start", "ready", "activate", "attest", "capture", "delete", "join", "restore"} {
found := -1
for i := last + 1; i < len(*events); i++ {
if (*events)[i] == event {
found = i
break
}
}
if found < 0 {
t.Fatalf("ordering: %v", *events)
}
last = found
}
if !proof.matrix.Runs[0].Attestation.Joined || !proof.matrix.Runs[0].Attestation.Restored {
t.Fatal("premature seal")
}
if s.saveCompleted(proof) != nil {
t.Fatal("producer publication")
}
before := *calls
if _, e := q.complete(context.Background(), s, r); e == nil || *calls != before {
t.Fatal("retested immutable matrix")
}
}
func TestResourceQualifierCapacityBeforeWorkers(t *testing.T) {
q, _, registry, events, calls := qualifierFixture(t)
dir := t.TempDir()
if e := os.Chmod(dir, 0700); e != nil {
t.Fatal(e)
}
s, e := openResourcePlanStore(dir, true)
if e != nil {
t.Fatal(e)
}
defer s.close()
template := registry.Strategies[0]
registry.Strategies = nil
for i := 0; i < 32; i++ {
definition := template
definition.ID = "strategy-" + strconv.Itoa(i)
registry.Strategies = append(registry.Strategies, definition)
}
b, _ := json.Marshal(registry)
registry, e = parseStrategyRegistry(b)
if e != nil {
t.Fatal(e)
}
if e = s.importDefinitions(b); e != nil {
t.Fatal(e)
}
source, e := q.source()
if e != nil {
t.Fatal(e)
}
spec := source.config.Services[0].Resources[0]
cfg := resourceCheckConfig{Version: 1, Services: []resourceCheckService{{ID: "service"}}}
for i := 0; i < 64; i++ {
resource := spec
resource.ID = "resource-" + strconv.Itoa(i)
cfg.Services[0].Resources = append(cfg.Services[0].Resources, resource)
}
b, _ = json.Marshal(cfg)
cfg, e = parseResourceChecks(b, func(string) (string, bool) { return "", false })
if e != nil {
t.Fatal(e)
}
source.config = cfg
source.fingerprint = resourceMAC(s.files.key, b)
q.source = func() (resourceMonitorSource, error) { return source, nil }
starts := 0
start := q.runtime.start
q.runtime.start = func(args []string) (managedAssetWorker, error) { starts++; return start(args) }
proof, e := q.complete(context.Background(), s, registry)
if !errors.Is(e, errQualificationCapacity) || starts != 0 || *calls != 0 || len(*events) != 0 || proof.seal != "" || s.held[planMatrixName] != nil {
t.Fatalf("capacity rejection too late: err=%v starts=%d calls=%d events=%v", e, starts, *calls, *events)
}
}
func TestResourceQualifierSequentialCandidates(t *testing.T) {
q, s, r, events, calls := qualifierFixture(t, 2)
proof, err := q.complete(context.Background(), s, r)
if err != nil {
t.Fatal(err)
}
if *calls != 24 || len(proof.matrix.Runs) != 2 {
t.Fatal("incomplete sequential batch", *calls)
}
stages := strings.Join(*events, ",")
if strings.Count(stages, "start") != 2 || strings.Count(stages, "restore") != 2 || !strings.Contains(stages, "delete,join,restore,inventory,start") {
t.Fatal("next candidate began before previous cleanup", stages)
}
if s.saveCompleted(proof) != nil {
t.Fatal("sequential producer publication")
}
}
func TestResourceQualifierNegativeAndUnknown(t *testing.T) {
for _, mode := range []string{"negative", "status500", "tls-peer", "timeout", "timeout-infrastructure", "infrastructure", "guard", "cancel", "source", "restore", "hash", "mutation"} {
t.Run(mode, func(t *testing.T) {
q, s, r, _, calls := qualifierFixture(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
original := q.checks.runner
if strings.HasPrefix(mode, "timeout") {
oldSource := q.source
source, e := oldSource()
if e != nil {
t.Fatal(e)
}
for i := range source.config.Services {
for j := range source.config.Services[i].Resources {
source.config.Services[i].Resources[j].TimeoutMS = 100
}
}
q.source = func() (resourceMonitorSource, error) { return source, nil }
}
q.checks.runner = func(ctx context.Context, args []string, stdin io.Reader, out, diag io.Writer) error {
if mode == "tls-peer" {
(*calls)++
return exec.CommandContext(ctx, "/bin/sh", "-c", "exit 35").Run()
}
if strings.HasPrefix(mode, "timeout") {
(*calls)++
<-ctx.Done()
if mode == "timeout-infrastructure" {
return errors.New("local safety failure coincident with timeout")
}
return ctx.Err()
}
if mode == "infrastructure" {
return errors.New("unknown local collector infrastructure")
}
if mode == "cancel" {
cancel()
return context.Canceled
}
if (mode == "negative" || mode == "status500") && *calls == 6 { // First repeat of other URL; still do all repeats.
*calls++
b, e := io.ReadAll(stdin)
if e != nil {
return e
}
raw, e := strconv.Unquote(strings.TrimPrefix(strings.Split(string(b), "\n")[0], "url = "))
if e != nil {
return e
}
wireMode := "redirect"
if mode == "status500" {
wireMode = "status500"
}
configuredWire(raw, "GET", "tls12", wireMode, out, diag)
return nil
}
return original(ctx, args, stdin, out, diag)
}
if mode == "guard" {
q.checks.guard = func() error { return errors.New("ownership lost") }
}
if mode == "source" {
source := q.source
q.source = func() (resourceMonitorSource, error) {
v, e := source()
if *calls > 0 {
v.fingerprint = strings.Repeat("d", 64)
}
return v, e
}
}
if mode == "restore" {
q.restore = func(context.Context) error { return errors.New("unsafe restore") }
}
if mode == "hash" {
q.filesHash = func() (string, error) { return strings.Repeat("b", 64), nil }
}
proof, e := q.complete(ctx, s, r)
if mode == "negative" || mode == "status500" || mode == "timeout" || mode == "tls-peer" {
if e != nil || *calls != 12 {
t.Fatalf("negative incomplete: %v %d", e, *calls)
}
source, _ := q.source()
if mode == "status500" && proof.matrix.Runs[0].Checks[2].Outcome != "expectation_failed" {
t.Fatal("500 must be a complete configured expectation failure")
}
if mode == "timeout" {
for _, c := range proof.matrix.Runs[0].Checks {
if c.Attempts != 3 || c.Outcome != "timeout" {
t.Fatal("timeout contract", c)
}
}
}
p := deriveResourcePlan(source, s.files.key, r, proof.matrix, time.Now())
if len(p.Assignments) != 0 || len(p.Domains[0].Negative) != 1 {
t.Fatalf("bad required URL qualified: %+v", p)
}
return
}
if mode == "mutation" {
if e != nil {
t.Fatal(e)
}
proof.matrix.Runs[0].Checks[0].Outcome = "timeout"
if s.saveCompleted(proof) == nil {
t.Fatal("post-seal mutation")
}
return
}
if e == nil || proof.seal != "" || s.held[planMatrixName] != nil {
t.Fatalf("published incomplete %s: %v", mode, e)
}
})
}
}
func TestResourceQualifierTypedPeerResponseBoundary(t *testing.T) {
for _, mode := range []string{"redirect", "extra"} {
t.Run(mode, func(t *testing.T) {
runner := func(_ context.Context, _ []string, _ io.Reader, out, diag io.Writer) error {
configuredWire("https://a.example/", "GET", "tls12", mode, out, diag)
return nil
}
_, _, err := collectResourceTransferOptions(context.Background(), "socks5h://127.0.0.1:1080", "https://a.example/", "a.example", "tls12", false, runner, resourceTransferOptions{generic: true, method: "GET", limit: 100, timeout: time.Second})
var peer *resourcePeerResponseRejection
var fatal *managedRecoveryFatal
if mode == "redirect" {
if !errors.As(err, &peer) || errors.As(err, &fatal) || !resourceRejectionOnly(err) {
t.Fatalf("redirect cause should be typed peer rejection, got %T", err)
}
} else if err == nil || resourceRejectionOnly(err) || !errors.As(err, &fatal) {
t.Fatal("malformed body/evidence became peer negative")
}
})
}
}
func TestResourceQualifierRejectsRegularTarget(t *testing.T) {
d := managedDependencies(runPlayerCurl)
if qualificationMode("resources", "strategies", d) == nil {
t.Fatal("regular process authorized isolated qualification")
}
}