clash/test/dns_test.go

101 lines
2 KiB
Go
Raw Normal View History

2021-05-17 12:33:00 +00:00
package main
import (
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
2022-05-21 09:37:06 +00:00
"github.com/stretchr/testify/require"
2021-05-17 12:33:00 +00:00
)
func exchange(address, domain string, tp uint16) ([]dns.RR, error) {
client := dns.Client{}
query := &dns.Msg{}
query.SetQuestion(dns.Fqdn(domain), tp)
r, _, err := client.Exchange(query, address)
if err != nil {
return nil, err
}
return r.Answer, nil
}
func TestClash_DNS(t *testing.T) {
basic := `
log-level: silent
dns:
enable: true
listen: 0.0.0.0:8553
nameserver:
- 119.29.29.29
`
2022-05-21 09:37:06 +00:00
err := parseAndApply(basic)
require.NoError(t, err)
2021-05-17 12:33:00 +00:00
defer cleanup()
time.Sleep(waitTime)
rr, err := exchange("127.0.0.1:8553", "1.1.1.1.nip.io", dns.TypeA)
assert.NoError(t, err)
2022-05-21 09:37:06 +00:00
assert.NotEmptyf(t, rr, "record empty")
2021-05-17 12:33:00 +00:00
record := rr[0].(*dns.A)
assert.Equal(t, record.A.String(), "1.1.1.1")
rr, err = exchange("127.0.0.1:8553", "2606-4700-4700--1111.sslip.io", dns.TypeAAAA)
assert.NoError(t, err)
assert.Empty(t, rr)
}
func TestClash_DNSHostAndFakeIP(t *testing.T) {
basic := `
log-level: silent
hosts:
foo.clash.dev: 1.1.1.1
dns:
enable: true
listen: 0.0.0.0:8553
ipv6: true
enhanced-mode: fake-ip
fake-ip-range: 198.18.0.1/16
fake-ip-filter:
- .sslip.io
nameserver:
- 119.29.29.29
`
2022-05-21 09:37:06 +00:00
err := parseAndApply(basic)
require.NoError(t, err)
2021-05-17 12:33:00 +00:00
defer cleanup()
time.Sleep(waitTime)
type domainPair struct {
domain string
ip string
}
list := []domainPair{
2022-06-07 02:45:32 +00:00
{"foo.org", "198.18.0.3"},
{"bar.org", "198.18.0.4"},
{"foo.org", "198.18.0.3"},
2021-05-17 12:33:00 +00:00
{"foo.clash.dev", "1.1.1.1"},
}
for _, pair := range list {
rr, err := exchange("127.0.0.1:8553", pair.domain, dns.TypeA)
assert.NoError(t, err)
assert.NotEmpty(t, rr)
record := rr[0].(*dns.A)
assert.Equal(t, record.A.String(), pair.ip)
}
rr, err := exchange("127.0.0.1:8553", "2606-4700-4700--1111.sslip.io", dns.TypeAAAA)
assert.NoError(t, err)
assert.NotEmpty(t, rr)
assert.Equal(t, rr[0].(*dns.AAAA).AAAA.String(), "2606:4700:4700::1111")
}