1package dns_test
2
3import (
4	"context"
5	"net/url"
6	"testing"
7	"time"
8
9	"github.com/google/go-cmp/cmp"
10
11	. "github.com/xtls/xray-core/app/dns"
12	"github.com/xtls/xray-core/common"
13	"github.com/xtls/xray-core/common/net"
14	dns_feature "github.com/xtls/xray-core/features/dns"
15)
16
17func TestTCPLocalNameServer(t *testing.T) {
18	url, err := url.Parse("tcp+local://8.8.8.8")
19	common.Must(err)
20	s, err := NewTCPLocalNameServer(url)
21	common.Must(err)
22	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
23	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
24		IPv4Enable: true,
25		IPv6Enable: true,
26	}, false)
27	cancel()
28	common.Must(err)
29	if len(ips) == 0 {
30		t.Error("expect some ips, but got 0")
31	}
32}
33
34func TestTCPLocalNameServerWithCache(t *testing.T) {
35	url, err := url.Parse("tcp+local://8.8.8.8")
36	common.Must(err)
37	s, err := NewTCPLocalNameServer(url)
38	common.Must(err)
39	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
40	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
41		IPv4Enable: true,
42		IPv6Enable: true,
43	}, false)
44	cancel()
45	common.Must(err)
46	if len(ips) == 0 {
47		t.Error("expect some ips, but got 0")
48	}
49
50	ctx2, cancel := context.WithTimeout(context.Background(), time.Second*5)
51	ips2, err := s.QueryIP(ctx2, "google.com", net.IP(nil), dns_feature.IPOption{
52		IPv4Enable: true,
53		IPv6Enable: true,
54	}, true)
55	cancel()
56	common.Must(err)
57	if r := cmp.Diff(ips2, ips); r != "" {
58		t.Fatal(r)
59	}
60}
61