sing-box/dns/transport_test.go

94 lines
2.7 KiB
Go
Raw Normal View History

package dns_test
2022-07-06 10:48:11 +00:00
import (
"context"
"testing"
"time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/dns"
2022-07-06 10:48:11 +00:00
"github.com/sagernet/sing-box/log"
2022-07-08 15:03:57 +00:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2022-07-06 10:48:11 +00:00
"github.com/stretchr/testify/require"
"golang.org/x/net/dns/dnsmessage"
)
func TestTCPDNS(t *testing.T) {
t.Parallel()
2022-07-06 10:48:11 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := dns.NewTCPTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:53"))
2022-07-06 10:48:11 +00:00
response, err := transport.Exchange(ctx, makeQuery())
cancel()
require.NoError(t, err)
require.NotEmpty(t, response.Answers, "no answers")
for _, answer := range response.Answers {
t.Log(answer)
}
}
func TestTLSDNS(t *testing.T) {
t.Parallel()
2022-07-06 10:48:11 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := dns.NewTLSTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:853"))
2022-07-06 10:48:11 +00:00
response, err := transport.Exchange(ctx, makeQuery())
cancel()
require.NoError(t, err)
require.NotEmpty(t, response.Answers, "no answers")
for _, answer := range response.Answers {
t.Log(answer)
}
}
func TestHTTPSDNS(t *testing.T) {
t.Parallel()
2022-07-06 10:48:11 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := dns.NewHTTPSTransport(N.SystemDialer, "https://1.0.0.1:443/dns-query")
2022-07-06 10:48:11 +00:00
response, err := transport.Exchange(ctx, makeQuery())
cancel()
require.NoError(t, err)
require.NotEmpty(t, response.Answers, "no answers")
for _, answer := range response.Answers {
t.Log(answer)
}
}
func TestUDPDNS(t *testing.T) {
t.Parallel()
2022-07-06 10:48:11 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := dns.NewUDPTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:53"))
2022-07-06 10:48:11 +00:00
response, err := transport.Exchange(ctx, makeQuery())
cancel()
require.NoError(t, err)
require.NotEmpty(t, response.Answers, "no answers")
for _, answer := range response.Answers {
t.Log(answer)
}
}
func TestLocalDNS(t *testing.T) {
t.Parallel()
2022-07-06 10:48:11 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := dns.NewLocalTransport()
2022-07-06 10:48:11 +00:00
response, err := transport.Lookup(ctx, "google.com", C.DomainStrategyAsIS)
cancel()
require.NoError(t, err)
require.NotEmpty(t, response, "no answers")
for _, answer := range response {
t.Log(answer)
}
}
func makeQuery() *dnsmessage.Message {
message := &dnsmessage.Message{}
message.Header.ID = 1
message.Header.RecursionDesired = true
message.Questions = append(message.Questions, dnsmessage.Question{
Name: dnsmessage.MustNewName("google.com."),
Type: dnsmessage.TypeA,
Class: dnsmessage.ClassINET,
})
return message
}