Clash.Meta/test/dns_test.go

101 lines
2.0 KiB
Go
Raw Normal View History

2021-05-17 20:33:00 +08:00
package main
import (
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
2022-05-21 17:37:06 +08:00
"github.com/stretchr/testify/require"
2021-05-17 20:33:00 +08: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
}
2023-11-03 21:01:45 +08:00
func TestMihomo_DNS(t *testing.T) {
2021-05-17 20:33:00 +08:00
basic := `
log-level: silent
dns:
enable: true
listen: 0.0.0.0:8553
nameserver:
- 119.29.29.29
`
2022-05-21 17:37:06 +08:00
err := parseAndApply(basic)
require.NoError(t, err)
2021-05-17 20:33:00 +08: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 17:37:06 +08:00
assert.NotEmptyf(t, rr, "record empty")
2021-05-17 20:33:00 +08: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)
}
2023-11-03 21:01:45 +08:00
func TestMihomo_DNSHostAndFakeIP(t *testing.T) {
2021-05-17 20:33:00 +08:00
basic := `
log-level: silent
hosts:
2023-11-03 21:01:45 +08:00
foo.mihomo.dev: 1.1.1.1
2021-05-17 20:33:00 +08:00
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 17:37:06 +08:00
err := parseAndApply(basic)
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
defer cleanup()
time.Sleep(waitTime)
type domainPair struct {
domain string
ip string
}
list := []domainPair{
2022-12-02 20:21:31 +08:00
{"foo.org", "198.18.0.4"},
{"bar.org", "198.18.0.5"},
{"foo.org", "198.18.0.4"},
2023-11-03 21:01:45 +08:00
{"foo.mihomo.dev", "1.1.1.1"},
2021-05-17 20:33:00 +08:00
}
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")
}