2023-11-28 13:40:52 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUpstreamConfigValidator(t *testing.T) {
|
|
|
|
goodHandler := dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
|
|
|
|
err := w.WriteMsg(new(dns.Msg).SetReply(m))
|
|
|
|
require.NoError(testutil.PanicT{}, err)
|
|
|
|
})
|
|
|
|
badHandler := dns.HandlerFunc(func(w dns.ResponseWriter, _ *dns.Msg) {
|
|
|
|
err := w.WriteMsg(new(dns.Msg))
|
|
|
|
require.NoError(testutil.PanicT{}, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
goodUps := (&url.URL{
|
|
|
|
Scheme: "tcp",
|
|
|
|
Host: newLocalUpstreamListener(t, 0, goodHandler).String(),
|
|
|
|
}).String()
|
|
|
|
badUps := (&url.URL{
|
|
|
|
Scheme: "tcp",
|
|
|
|
Host: newLocalUpstreamListener(t, 0, badHandler).String(),
|
|
|
|
}).String()
|
|
|
|
|
|
|
|
goodAndBadUps := strings.Join([]string{goodUps, badUps}, " ")
|
|
|
|
|
|
|
|
// upsTimeout restricts the checking process to prevent the test from
|
|
|
|
// hanging.
|
|
|
|
const upsTimeout = 100 * time.Millisecond
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
want map[string]string
|
|
|
|
name string
|
|
|
|
general []string
|
|
|
|
fallback []string
|
|
|
|
private []string
|
|
|
|
}{{
|
|
|
|
name: "success",
|
|
|
|
general: []string{goodUps},
|
|
|
|
want: map[string]string{
|
|
|
|
goodUps: "OK",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "broken",
|
|
|
|
general: []string{badUps},
|
|
|
|
want: map[string]string{
|
|
|
|
badUps: `couldn't communicate with upstream: exchanging with ` +
|
|
|
|
badUps + ` over tcp: dns: id mismatch`,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "both",
|
|
|
|
general: []string{goodUps, badUps, goodUps},
|
|
|
|
want: map[string]string{
|
|
|
|
goodUps: "OK",
|
|
|
|
badUps: `couldn't communicate with upstream: exchanging with ` +
|
|
|
|
badUps + ` over tcp: dns: id mismatch`,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "domain_specific_error",
|
|
|
|
general: []string{"[/domain.example/]" + badUps},
|
|
|
|
want: map[string]string{
|
|
|
|
badUps: `WARNING: couldn't communicate ` +
|
|
|
|
`with upstream: exchanging with ` + badUps + ` over tcp: ` +
|
|
|
|
`dns: id mismatch`,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "fallback_success",
|
|
|
|
fallback: []string{goodUps},
|
|
|
|
want: map[string]string{
|
|
|
|
goodUps: "OK",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "fallback_broken",
|
|
|
|
fallback: []string{badUps},
|
|
|
|
want: map[string]string{
|
|
|
|
badUps: `couldn't communicate with upstream: exchanging with ` +
|
|
|
|
badUps + ` over tcp: dns: id mismatch`,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "multiple_domain_specific_upstreams",
|
|
|
|
general: []string{"[/domain.example/]" + goodAndBadUps},
|
|
|
|
want: map[string]string{
|
|
|
|
goodUps: "OK",
|
|
|
|
badUps: `WARNING: couldn't communicate ` +
|
|
|
|
`with upstream: exchanging with ` + badUps + ` over tcp: ` +
|
|
|
|
`dns: id mismatch`,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "bad_specification",
|
|
|
|
general: []string{"[/domain.example/]/]1.2.3.4"},
|
|
|
|
want: map[string]string{
|
2024-02-05 13:12:27 +00:00
|
|
|
"[/domain.example/]/]1.2.3.4": generalTextLabel + " 1: parsing error",
|
2023-11-28 13:40:52 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "all_different",
|
|
|
|
general: []string{"[/domain.example/]" + goodAndBadUps},
|
|
|
|
fallback: []string{"[/domain.example/]" + goodAndBadUps},
|
|
|
|
private: []string{"[/domain.example/]" + goodAndBadUps},
|
|
|
|
want: map[string]string{
|
|
|
|
goodUps: "OK",
|
|
|
|
badUps: `WARNING: couldn't communicate ` +
|
|
|
|
`with upstream: exchanging with ` + badUps + ` over tcp: ` +
|
|
|
|
`dns: id mismatch`,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "bad_specific_domains",
|
|
|
|
general: []string{"[/example/]/]" + goodUps},
|
|
|
|
fallback: []string{"[/example/" + goodUps},
|
|
|
|
private: []string{"[/example//bad.123/]" + goodUps},
|
|
|
|
want: map[string]string{
|
2024-02-05 13:12:27 +00:00
|
|
|
"[/example/]/]" + goodUps: generalTextLabel + " 1: parsing error",
|
|
|
|
"[/example/" + goodUps: fallbackTextLabel + " 1: parsing error",
|
|
|
|
"[/example//bad.123/]" + goodUps: privateTextLabel + " 1: parsing error",
|
2023-11-28 13:40:52 +00:00
|
|
|
},
|
|
|
|
}, {
|
2024-02-05 13:12:27 +00:00
|
|
|
name: "bad_proto",
|
2023-11-28 13:40:52 +00:00
|
|
|
general: []string{
|
2024-02-05 13:12:27 +00:00
|
|
|
"bad://1.2.3.4",
|
2023-11-28 13:40:52 +00:00
|
|
|
},
|
|
|
|
want: map[string]string{
|
2024-02-05 13:12:27 +00:00
|
|
|
"bad://1.2.3.4": generalTextLabel + " 1: parsing error",
|
2023-11-28 13:40:52 +00:00
|
|
|
},
|
|
|
|
}, {
|
2024-02-05 13:12:27 +00:00
|
|
|
name: "truncated_line",
|
2023-11-28 13:40:52 +00:00
|
|
|
general: []string{
|
2024-02-05 13:12:27 +00:00
|
|
|
"This is a very long line. It will cause a parsing error and will be truncated here.",
|
2023-11-28 13:40:52 +00:00
|
|
|
},
|
|
|
|
want: map[string]string{
|
2024-02-05 13:12:27 +00:00
|
|
|
"This is a very long line. It will cause a parsing error and will be truncated …": "upstream_dns 1: parsing error",
|
2023-11-28 13:40:52 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cv := newUpstreamConfigValidator(tc.general, tc.fallback, tc.private, &upstream.Options{
|
|
|
|
Timeout: upsTimeout,
|
|
|
|
Bootstrap: net.DefaultResolver,
|
|
|
|
})
|
|
|
|
cv.check()
|
|
|
|
cv.close()
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, cv.status())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpstreamConfigValidator_Check_once(t *testing.T) {
|
2023-12-06 15:56:14 +00:00
|
|
|
type signal = struct{}
|
2023-11-28 13:40:52 +00:00
|
|
|
|
2023-12-06 15:56:14 +00:00
|
|
|
reqCh := make(chan signal)
|
2023-11-28 13:40:52 +00:00
|
|
|
hdlr := dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
|
2023-12-06 15:56:14 +00:00
|
|
|
pt := testutil.PanicT{}
|
|
|
|
|
2023-11-28 13:40:52 +00:00
|
|
|
err := w.WriteMsg(new(dns.Msg).SetReply(m))
|
2023-12-06 15:56:14 +00:00
|
|
|
require.NoError(pt, err)
|
|
|
|
|
|
|
|
testutil.RequireSend(pt, reqCh, signal{}, testTimeout)
|
2023-11-28 13:40:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
addr := (&url.URL{
|
|
|
|
Scheme: "tcp",
|
|
|
|
Host: newLocalUpstreamListener(t, 0, hdlr).String(),
|
|
|
|
}).String()
|
|
|
|
twoAddrs := strings.Join([]string{addr, addr}, " ")
|
|
|
|
|
2023-12-06 15:56:14 +00:00
|
|
|
wantStatus := map[string]string{
|
|
|
|
addr: "OK",
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:40:52 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
ups []string
|
|
|
|
}{{
|
|
|
|
name: "common",
|
|
|
|
ups: []string{addr, addr, addr},
|
|
|
|
}, {
|
|
|
|
name: "domain-specific",
|
|
|
|
ups: []string{"[/one.example/]" + addr, "[/two.example/]" + twoAddrs},
|
|
|
|
}, {
|
|
|
|
name: "both",
|
|
|
|
ups: []string{addr, "[/one.example/]" + addr, addr, "[/two.example/]" + twoAddrs},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cv := newUpstreamConfigValidator(tc.ups, nil, nil, &upstream.Options{
|
2023-12-06 15:56:14 +00:00
|
|
|
Timeout: testTimeout,
|
2023-11-28 13:40:52 +00:00
|
|
|
})
|
|
|
|
|
2023-12-06 15:56:14 +00:00
|
|
|
go func() {
|
|
|
|
cv.check()
|
|
|
|
testutil.RequireSend(testutil.PanicT{}, reqCh, signal{}, testTimeout)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for the only request to be sent.
|
|
|
|
testutil.RequireReceive(t, reqCh, testTimeout)
|
|
|
|
|
|
|
|
// Wait for the check to finish.
|
|
|
|
testutil.RequireReceive(t, reqCh, testTimeout)
|
|
|
|
|
|
|
|
cv.close()
|
|
|
|
require.Equal(t, wantStatus, cv.status())
|
2023-11-28 13:40:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|