mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2026-12-01 17:02:33 +00:00
- 让全局 DNS 作用域等待可取消,并按唯一地址计算 PTR 查询预算 - 校验 query ID、ODoH Stamp 和 disabled flavor 的 DNS 调用 - 修复 HTTP/ODoH 失败清理并阻止 AXFR 路径穿越
100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
//go:build !flavor_tiny && !flavor_ntr
|
|
|
|
package dnsclient
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
fatalHelperEnv = "NEXTTRACE_DNS_FATAL_HELPER"
|
|
dnsCryptFatalHelperEnv = "NEXTTRACE_DNSCRYPT_FATAL_HELPER"
|
|
)
|
|
|
|
func TestOutputFatalPathRunsInSubprocess(t *testing.T) {
|
|
if os.Getenv(fatalHelperEnv) == "1" {
|
|
code := Run(context.Background(), []string{
|
|
"--server=" + os.Getenv("NEXTTRACE_DNS_FATAL_SERVER"),
|
|
"--qname=example.com",
|
|
"--type=A",
|
|
"--format=raw",
|
|
"--timeout=1s",
|
|
}, errorWriter{}, os.Stderr)
|
|
os.Exit(code)
|
|
}
|
|
|
|
server := startTestDNSServer(t, "udp")
|
|
ctx, cancel := context.WithTimeout(context.Background(), testCommandTimeout)
|
|
defer cancel()
|
|
command := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestOutputFatalPathRunsInSubprocess$")
|
|
command.Env = append(os.Environ(),
|
|
fatalHelperEnv+"=1",
|
|
"NEXTTRACE_DNS_FATAL_SERVER="+server,
|
|
"HOME="+t.TempDir(),
|
|
"NO_COLOR=1",
|
|
"SSLKEYLOGFILE=",
|
|
)
|
|
output, err := command.CombinedOutput()
|
|
if ctx.Err() != nil {
|
|
t.Fatalf("helper timed out: %v, output=%q", ctx.Err(), output)
|
|
}
|
|
var exitError *exec.ExitError
|
|
if !errors.As(err, &exitError) || exitError.ExitCode() == 0 {
|
|
t.Fatalf("helper error = %v, output=%q", err, output)
|
|
}
|
|
if !strings.Contains(string(output), "forced writer failure") {
|
|
t.Fatalf("helper output = %q", output)
|
|
}
|
|
}
|
|
|
|
func TestDNSCryptFatalPathRunsInSubprocess(t *testing.T) {
|
|
if os.Getenv(dnsCryptFatalHelperEnv) == "1" {
|
|
code := Run(context.Background(), []string{
|
|
"--server=dnscrypt://" + os.Getenv("NEXTTRACE_DNSCRYPT_FATAL_SERVER"),
|
|
"--dnscrypt-key=" + strings.Repeat("00", 32),
|
|
"--dnscrypt-provider=2.dnscrypt-cert.example",
|
|
"--qname=example.com",
|
|
"--type=A",
|
|
"--timeout=1s",
|
|
}, io.Discard, os.Stderr)
|
|
os.Exit(code)
|
|
}
|
|
|
|
server := startTestDNSServer(t, "udp")
|
|
ctx, cancel := context.WithTimeout(context.Background(), testCommandTimeout)
|
|
defer cancel()
|
|
command := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestDNSCryptFatalPathRunsInSubprocess$")
|
|
command.Env = append(os.Environ(),
|
|
dnsCryptFatalHelperEnv+"=1",
|
|
"NEXTTRACE_DNSCRYPT_FATAL_SERVER="+server,
|
|
"HOME="+t.TempDir(),
|
|
"NO_COLOR=1",
|
|
"SSLKEYLOGFILE=",
|
|
)
|
|
output, err := command.CombinedOutput()
|
|
if ctx.Err() != nil {
|
|
t.Fatalf("helper timed out: %v, output=%q", ctx.Err(), output)
|
|
}
|
|
var exitError *exec.ExitError
|
|
if !errors.As(err, &exitError) || exitError.ExitCode() == 0 {
|
|
t.Fatalf("helper error = %v, output=%q", err, output)
|
|
}
|
|
if !strings.Contains(string(output), "failed to dial DNSCrypt server") {
|
|
t.Fatalf("helper output = %q", output)
|
|
}
|
|
}
|
|
|
|
type errorWriter struct{}
|
|
|
|
func (errorWriter) Write([]byte) (int, error) {
|
|
return 0, errors.New("forced writer failure")
|
|
}
|
|
|
|
var _ io.Writer = errorWriter{}
|