CI / test (push) Has been cancelled
Phase 4.2 (PR #12) and test coverage improvement (PR #13) independently added overlapping test functions. Remove duplicates from PR #13 files: - internal/dns/iterative_test.go: remove TestExtractNSNames, TestExtractNSNamesEmpty, TestIterativeQueryWithExchangeSuccess, TestIterativeQueryWithExchangeNilResponse, TestIterativeQueryWithExchangeTCPFallback (kept in roots_test.go and query_test.go from PR #12) - internal/output/coverage_test.go: remove TestRRDataStringDefault, TestContainsString, TestCollectUniqueServerIPs, TestNewFormatterNilWriter (kept in stats_test.go and formatter_test.go from PR #12) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: multica-agent <github@multica.ai>
123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
package dns
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
func TestIterativeQueryWithExchangeNilConfig(t *testing.T) {
|
|
resp := new(dns.Msg)
|
|
resp.SetReply(new(dns.Msg))
|
|
|
|
exchangeFn := func(ctx context.Context, server string, msg *dns.Msg, useTCP bool) (*dns.Msg, error) {
|
|
return resp.Copy(), nil
|
|
}
|
|
|
|
server := net.ParseIP("8.8.8.8")
|
|
_, err := IterativeQueryWithExchange(context.Background(), server, "example.com", TypeA, nil, exchangeFn)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error with nil config: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestIterativeQueryWithExchangeAlwaysTCP(t *testing.T) {
|
|
resp := new(dns.Msg)
|
|
resp.SetReply(new(dns.Msg))
|
|
|
|
var mu sync.Mutex
|
|
calls := []bool{}
|
|
exchangeFn := func(ctx context.Context, server string, msg *dns.Msg, useTCP bool) (*dns.Msg, error) {
|
|
mu.Lock()
|
|
calls = append(calls, useTCP)
|
|
mu.Unlock()
|
|
return resp.Copy(), nil
|
|
}
|
|
|
|
cfg := &QueryConfig{
|
|
UDPSize: 2048,
|
|
Retries: 1,
|
|
UseTCP: true,
|
|
}
|
|
|
|
server := net.ParseIP("8.8.8.8")
|
|
_, err := IterativeQueryWithExchange(context.Background(), server, "example.com", TypeA, cfg, exchangeFn)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(calls) != 1 || !calls[0] {
|
|
t.Errorf("expected single TCP call, got %v", calls)
|
|
}
|
|
}
|
|
|
|
func TestIterativeQueryWithExchangeRetriesOnFailure(t *testing.T) {
|
|
var mu sync.Mutex
|
|
callCount := 0
|
|
exchangeFn := func(ctx context.Context, server string, msg *dns.Msg, useTCP bool) (*dns.Msg, error) {
|
|
mu.Lock()
|
|
callCount++
|
|
mu.Unlock()
|
|
return nil, errors.New("connection refused")
|
|
}
|
|
|
|
cfg := &QueryConfig{
|
|
UDPSize: 2048,
|
|
Retries: 3,
|
|
UseTCP: false,
|
|
}
|
|
|
|
server := net.ParseIP("8.8.8.8")
|
|
_, err := IterativeQueryWithExchange(context.Background(), server, "example.com", TypeA, cfg, exchangeFn)
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if callCount != 3 {
|
|
t.Errorf("expected 3 calls, got %d", callCount)
|
|
}
|
|
}
|
|
|
|
func TestIterativeQueryWithExchangeContextCancel(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
exchangeFn := func(ctx context.Context, server string, msg *dns.Msg, useTCP bool) (*dns.Msg, error) {
|
|
return nil, ctx.Err()
|
|
}
|
|
|
|
cfg := &QueryConfig{
|
|
UDPSize: 2048,
|
|
Retries: 1,
|
|
UseTCP: false,
|
|
}
|
|
|
|
server := net.ParseIP("8.8.8.8")
|
|
_, err := IterativeQueryWithExchange(ctx, server, "example.com", TypeA, cfg, exchangeFn)
|
|
if err == nil {
|
|
t.Fatal("expected error on cancelled context")
|
|
}
|
|
}
|
|
|
|
func TestIterativeQueryWithExchangeZeroUDPSize(t *testing.T) {
|
|
resp := new(dns.Msg)
|
|
resp.SetReply(new(dns.Msg))
|
|
|
|
exchangeFn := func(ctx context.Context, server string, msg *dns.Msg, useTCP bool) (*dns.Msg, error) {
|
|
return resp.Copy(), nil
|
|
}
|
|
|
|
cfg := &QueryConfig{
|
|
UDPSize: 0, // should use default
|
|
Retries: 1,
|
|
}
|
|
|
|
server := net.ParseIP("8.8.8.8")
|
|
_, err := IterativeQueryWithExchange(context.Background(), server, "example.com", TypeA, cfg, exchangeFn)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|