mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
test: stabilize the UDP control-close test and the sealed hook test
The UDP close test raced the asynchronous association close with a goroutine count heuristic; probe until forwarding stops instead. The sealed hook test called InitSecrets with a fresh temp dir on a database already sealed by the package test key; use the package key. gofmt trace.go.
This commit is contained in:
@@ -14,9 +14,6 @@ func TestTrafficTamperHookCRUDSealedAndVersions(t *testing.T) {
|
||||
SkipPGUnavailable(t, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = d.Close() })
|
||||
if err := d.InitSecrets(t.TempDir()); err != nil {
|
||||
t.Fatalf("InitSecrets: %v", err)
|
||||
}
|
||||
|
||||
const srcV1 = `package hook
|
||||
const apiSecret = "super-secret-hmac-key-v1"
|
||||
|
||||
@@ -44,11 +44,11 @@ const (
|
||||
// TraceLine is one row of an exchange's rewrite trace, in order. An exchange is
|
||||
// rewritten when it has at least one applied line.
|
||||
type TraceLine struct {
|
||||
Phase Phase
|
||||
SourceKind string // "rule" (default when empty) | "hook"
|
||||
RuleID int64 // 0 on a global or hook line
|
||||
HookID int64 // 0 on a rule or global line
|
||||
HookVersion int // 0 on a rule or global line
|
||||
Phase Phase
|
||||
SourceKind string // "rule" (default when empty) | "hook"
|
||||
RuleID int64 // 0 on a global or hook line
|
||||
HookID int64 // 0 on a rule or global line
|
||||
HookVersion int // 0 on a rule or global line
|
||||
// RuleSnapshot is the rule or hook metadata as JSON, on its first line in the exchange only.
|
||||
RuleSnapshot string
|
||||
Action string
|
||||
|
||||
+12
-13
@@ -9,7 +9,6 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
@@ -321,7 +320,6 @@ func TestSOCKS5UDPAssociateRelayStoreAndControlConnClose(t *testing.T) {
|
||||
}
|
||||
defer tr.Close()
|
||||
|
||||
baseG := runtime.NumGoroutine()
|
||||
proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{
|
||||
Addr: "127.0.0.1:0",
|
||||
EnableUDP: true,
|
||||
@@ -422,23 +420,24 @@ func TestSOCKS5UDPAssociateRelayStoreAndControlConnClose(t *testing.T) {
|
||||
t.Fatalf("HTTP exchanges rows = (%d, %v), want 0", httpRows, err)
|
||||
}
|
||||
|
||||
// 4. Closing TCP control connection must tear down the UDP association promptly.
|
||||
// 4. Closing the TCP control connection must tear down the UDP association.
|
||||
// The close is processed asynchronously by the association goroutine, so a
|
||||
// single immediate probe races it: probe until a datagram is no longer
|
||||
// forwarded, then assert the relay is closed.
|
||||
_ = ctrl.Close()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
closed := false
|
||||
for time.Now().Before(deadline) {
|
||||
if runtime.NumGoroutine() <= baseG+6 {
|
||||
before := received.Load()
|
||||
_, _ = cliUDP.WriteToUDP(makeClientUDPPacketIPv4(echoAddr.IP, echoAddr.Port, 0x00, []byte("after-close")), relayAddr)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
if received.Load() == before {
|
||||
closed = true
|
||||
break
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
beforeAfterClose := received.Load()
|
||||
_, _ = cliUDP.WriteToUDP(makeClientUDPPacketIPv4(echoAddr.IP, echoAddr.Port, 0x00, []byte("after-close")), relayAddr)
|
||||
_ = cliUDP.SetReadDeadline(time.Now().Add(150 * time.Millisecond))
|
||||
if _, _, rerr := cliUDP.ReadFromUDP(buf); rerr == nil {
|
||||
t.Fatal("expected no UDP reply after TCP control connection closed")
|
||||
}
|
||||
if received.Load() != beforeAfterClose {
|
||||
t.Fatal("datagram was forwarded after TCP control connection closed")
|
||||
if !closed {
|
||||
t.Fatal("UDP association still forwards datagrams 2s after the TCP control connection closed")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user