github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/app/dns/dnscommon_test.go (about) 1 // +build !confonly 2 3 package dns 4 5 import ( 6 "math/rand" 7 "testing" 8 "time" 9 10 "github.com/google/go-cmp/cmp" 11 "github.com/miekg/dns" 12 "golang.org/x/net/dns/dnsmessage" 13 "v2ray.com/core/common" 14 "v2ray.com/core/common/net" 15 ) 16 17 func Test_parseResponse(t *testing.T) { 18 var p [][]byte 19 20 ans := new(dns.Msg) 21 ans.Id = 0 22 p = append(p, common.Must2(ans.Pack()).([]byte)) 23 24 p = append(p, []byte{}) 25 26 ans = new(dns.Msg) 27 ans.Id = 1 28 ans.Answer = append(ans.Answer, 29 common.Must2(dns.NewRR("google.com. IN CNAME m.test.google.com")).(dns.RR), 30 common.Must2(dns.NewRR("google.com. IN CNAME fake.google.com")).(dns.RR), 31 common.Must2(dns.NewRR("google.com. IN A 8.8.8.8")).(dns.RR), 32 common.Must2(dns.NewRR("google.com. IN A 8.8.4.4")).(dns.RR), 33 ) 34 p = append(p, common.Must2(ans.Pack()).([]byte)) 35 36 ans = new(dns.Msg) 37 ans.Id = 2 38 ans.Answer = append(ans.Answer, 39 common.Must2(dns.NewRR("google.com. IN CNAME m.test.google.com")).(dns.RR), 40 common.Must2(dns.NewRR("google.com. IN CNAME fake.google.com")).(dns.RR), 41 common.Must2(dns.NewRR("google.com. IN CNAME m.test.google.com")).(dns.RR), 42 common.Must2(dns.NewRR("google.com. IN CNAME test.google.com")).(dns.RR), 43 common.Must2(dns.NewRR("google.com. IN AAAA 2001::123:8888")).(dns.RR), 44 common.Must2(dns.NewRR("google.com. IN AAAA 2001::123:8844")).(dns.RR), 45 ) 46 p = append(p, common.Must2(ans.Pack()).([]byte)) 47 48 tests := []struct { 49 name string 50 want *IPRecord 51 wantErr bool 52 }{ 53 {"empty", 54 &IPRecord{0, []net.Address(nil), time.Time{}, dnsmessage.RCodeSuccess}, 55 false, 56 }, 57 {"error", 58 nil, 59 true, 60 }, 61 {"a record", 62 &IPRecord{1, []net.Address{net.ParseAddress("8.8.8.8"), net.ParseAddress("8.8.4.4")}, 63 time.Time{}, dnsmessage.RCodeSuccess}, 64 false, 65 }, 66 {"aaaa record", 67 &IPRecord{2, []net.Address{net.ParseAddress("2001::123:8888"), net.ParseAddress("2001::123:8844")}, time.Time{}, dnsmessage.RCodeSuccess}, 68 false, 69 }, 70 } 71 for i, tt := range tests { 72 t.Run(tt.name, func(t *testing.T) { 73 got, err := parseResponse(p[i]) 74 if (err != nil) != tt.wantErr { 75 t.Errorf("handleResponse() error = %v, wantErr %v", err, tt.wantErr) 76 return 77 } 78 79 if got != nil { 80 // reset the time 81 got.Expire = time.Time{} 82 } 83 if cmp.Diff(got, tt.want) != "" { 84 t.Errorf(cmp.Diff(got, tt.want)) 85 // t.Errorf("handleResponse() = %#v, want %#v", got, tt.want) 86 } 87 }) 88 } 89 } 90 91 func Test_buildReqMsgs(t *testing.T) { 92 93 stubID := func() uint16 { 94 return uint16(rand.Uint32()) 95 } 96 type args struct { 97 domain string 98 option IPOption 99 reqOpts *dnsmessage.Resource 100 } 101 tests := []struct { 102 name string 103 args args 104 want int 105 }{ 106 {"dual stack", args{"test.com", IPOption{true, true}, nil}, 2}, 107 {"ipv4 only", args{"test.com", IPOption{true, false}, nil}, 1}, 108 {"ipv6 only", args{"test.com", IPOption{false, true}, nil}, 1}, 109 {"none/error", args{"test.com", IPOption{false, false}, nil}, 0}, 110 } 111 for _, tt := range tests { 112 t.Run(tt.name, func(t *testing.T) { 113 if got := buildReqMsgs(tt.args.domain, tt.args.option, stubID, tt.args.reqOpts); !(len(got) == tt.want) { 114 t.Errorf("buildReqMsgs() = %v, want %v", got, tt.want) 115 } 116 }) 117 } 118 } 119 120 func Test_genEDNS0Options(t *testing.T) { 121 type args struct { 122 clientIP net.IP 123 } 124 tests := []struct { 125 name string 126 args args 127 want *dnsmessage.Resource 128 }{ 129 // TODO: Add test cases. 130 {"ipv4", args{net.ParseIP("4.3.2.1")}, nil}, 131 {"ipv6", args{net.ParseIP("2001::4321")}, nil}, 132 } 133 for _, tt := range tests { 134 t.Run(tt.name, func(t *testing.T) { 135 if got := genEDNS0Options(tt.args.clientIP); got == nil { 136 t.Errorf("genEDNS0Options() = %v, want %v", got, tt.want) 137 } 138 }) 139 } 140 } 141 142 func TestFqdn(t *testing.T) { 143 type args struct { 144 domain string 145 } 146 tests := []struct { 147 name string 148 args args 149 want string 150 }{ 151 {"with fqdn", args{"www.v2ray.com."}, "www.v2ray.com."}, 152 {"without fqdn", args{"www.v2ray.com"}, "www.v2ray.com."}, 153 } 154 for _, tt := range tests { 155 t.Run(tt.name, func(t *testing.T) { 156 if got := Fqdn(tt.args.domain); got != tt.want { 157 t.Errorf("Fqdn() = %v, want %v", got, tt.want) 158 } 159 }) 160 } 161 }