Files
Xray-core/infra/conf/dns_test.go
T

125 lines
3.7 KiB
Go
Raw Normal View History

2020-11-25 19:01:53 +08:00
package conf_test
import (
"encoding/json"
"testing"
2026-04-14 00:42:29 +08:00
"github.com/google/go-cmp/cmp"
2020-12-04 09:36:16 +08:00
"github.com/xtls/xray-core/app/dns"
2026-04-14 00:42:29 +08:00
"github.com/xtls/xray-core/common/geodata"
2020-12-04 09:36:16 +08:00
"github.com/xtls/xray-core/common/net"
. "github.com/xtls/xray-core/infra/conf"
"google.golang.org/protobuf/proto"
2026-04-14 00:42:29 +08:00
"google.golang.org/protobuf/testing/protocmp"
2020-11-25 19:01:53 +08:00
)
func TestDNSConfigParsing(t *testing.T) {
parserCreator := func() func(string) (proto.Message, error) {
return func(s string) (proto.Message, error) {
config := new(DNSConfig)
if err := json.Unmarshal([]byte(s), config); err != nil {
return nil, err
}
return config.Build()
}
}
expectedServeStale := true
2025-11-21 13:38:06 +08:00
expectedServeExpiredTTL := uint32(172800)
2026-04-14 00:42:29 +08:00
testCases := []TestCase{
2020-11-25 19:01:53 +08:00
{
Input: `{
"servers": [{
"address": "8.8.8.8",
"port": 5353,
2021-10-16 21:02:51 +08:00
"skipFallback": true,
2025-11-21 13:38:06 +08:00
"domains": ["domain:example.com"],
"serveStale": true,
"serveExpiredTTL": 172800
2020-11-25 19:01:53 +08:00
}],
"hosts": {
"domain:example.com": "google.com",
2021-10-16 21:02:51 +08:00
"example.com": "127.0.0.1",
"keyword:google": ["8.8.8.8", "8.8.4.4"],
"regexp:.*\\.com": "8.8.4.4",
"www.example.org": ["127.0.0.1", "127.0.0.2"]
2020-11-25 19:01:53 +08:00
},
2021-10-16 21:02:51 +08:00
"clientIp": "10.0.0.1",
"queryStrategy": "UseIPv4",
"disableCache": true,
2025-11-21 13:38:06 +08:00
"serveStale": false,
"serveExpiredTTL": 86400,
2021-10-16 21:02:51 +08:00
"disableFallback": true
2020-11-25 19:01:53 +08:00
}`,
Parser: parserCreator(),
Output: &dns.Config{
NameServer: []*dns.NameServer{
{
Address: &net.Endpoint{
Address: &net.IPOrDomain{
Address: &net.IPOrDomain_Ip{
Ip: []byte{8, 8, 8, 8},
},
},
Network: net.Network_UDP,
Port: 5353,
},
2021-10-16 21:02:51 +08:00
SkipFallback: true,
2026-04-14 00:42:29 +08:00
Domain: []*geodata.DomainRule{
2020-11-25 19:01:53 +08:00
{
2026-04-14 00:42:29 +08:00
Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Domain, Value: "example.com"}},
2020-11-25 19:01:53 +08:00
},
},
ServeStale: &expectedServeStale,
2025-11-21 13:38:06 +08:00
ServeExpiredTTL: &expectedServeExpiredTTL,
2025-11-21 13:45:42 +08:00
PolicyID: 1, // Servers with certain identical fields share this ID, incrementing starting from 1. See: Build PolicyID
2020-11-25 19:01:53 +08:00
},
},
StaticHosts: []*dns.Config_HostMapping{
{
2026-04-14 00:42:29 +08:00
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Domain, Value: "example.com"}}},
2020-11-25 19:01:53 +08:00
ProxiedDomain: "google.com",
},
2021-01-30 21:01:20 +08:00
{
2026-04-14 00:42:29 +08:00
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Full, Value: "example.com"}}},
2021-01-30 21:01:20 +08:00
Ip: [][]byte{{127, 0, 0, 1}},
},
2020-11-25 19:01:53 +08:00
{
2026-04-14 00:42:29 +08:00
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Substr, Value: "google"}}},
2021-10-16 21:02:51 +08:00
Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}},
2020-11-25 19:01:53 +08:00
},
{
2026-04-14 00:42:29 +08:00
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Regex, Value: ".*\\.com"}}},
2020-11-25 19:01:53 +08:00
Ip: [][]byte{{8, 8, 4, 4}},
},
2021-10-16 21:02:51 +08:00
{
2026-04-14 00:42:29 +08:00
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Full, Value: "www.example.org"}}},
2021-10-16 21:02:51 +08:00
Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
},
2020-11-25 19:01:53 +08:00
},
2021-10-16 21:02:51 +08:00
ClientIp: []byte{10, 0, 0, 1},
QueryStrategy: dns.QueryStrategy_USE_IP4,
DisableCache: true,
2025-11-21 13:38:06 +08:00
ServeStale: false,
ServeExpiredTTL: 86400,
2021-10-16 21:02:51 +08:00
DisableFallback: true,
2020-11-25 19:01:53 +08:00
},
},
2026-04-14 00:42:29 +08:00
}
for _, testCase := range testCases {
actual, err := testCase.Parser(testCase.Input)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(
testCase.Output,
actual,
protocmp.Transform(),
protocmp.SortRepeatedFields(&dns.Config{}, "static_hosts"),
); diff != "" {
t.Fatalf("Failed in test case:\n%s\nDiff (-want +got):\n%s", testCase.Input, diff)
}
}
2020-11-25 19:01:53 +08:00
}