2018-06-26 16:34:05 -04:00
|
|
|
package dns_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-11-15 05:17:20 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2018-06-26 16:34:05 -04:00
|
|
|
. "v2ray.com/core/app/dns"
|
2018-11-15 05:17:20 -05:00
|
|
|
"v2ray.com/core/common"
|
2018-06-26 16:34:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStaticHosts(t *testing.T) {
|
|
|
|
pb := []*Config_HostMapping{
|
|
|
|
{
|
2018-08-24 15:51:03 -04:00
|
|
|
Type: DomainMatchingType_Full,
|
2018-06-26 16:34:05 -04:00
|
|
|
Domain: "v2ray.com",
|
|
|
|
Ip: [][]byte{
|
|
|
|
{1, 1, 1, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-08-24 15:51:03 -04:00
|
|
|
Type: DomainMatchingType_Subdomain,
|
2018-06-26 16:34:05 -04:00
|
|
|
Domain: "v2ray.cn",
|
|
|
|
Ip: [][]byte{
|
|
|
|
{2, 2, 2, 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts, err := NewStaticHosts(pb, nil)
|
2018-11-15 05:17:20 -05:00
|
|
|
common.Must(err)
|
2018-06-26 16:34:05 -04:00
|
|
|
|
|
|
|
{
|
2018-11-19 14:42:02 -05:00
|
|
|
ips := hosts.LookupIP("v2ray.com", IPOption{
|
|
|
|
IPv4Enable: true,
|
|
|
|
IPv6Enable: true,
|
|
|
|
})
|
2018-11-15 05:17:20 -05:00
|
|
|
if len(ips) != 1 {
|
|
|
|
t.Error("expect 1 IP, but got ", len(ips))
|
|
|
|
}
|
2019-01-30 15:04:29 -05:00
|
|
|
if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
|
2018-11-15 05:17:20 -05:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
2018-06-26 16:34:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-11-19 14:42:02 -05:00
|
|
|
ips := hosts.LookupIP("www.v2ray.cn", IPOption{
|
|
|
|
IPv4Enable: true,
|
|
|
|
IPv6Enable: true,
|
|
|
|
})
|
2018-11-15 05:17:20 -05:00
|
|
|
if len(ips) != 1 {
|
|
|
|
t.Error("expect 1 IP, but got ", len(ips))
|
|
|
|
}
|
2019-01-30 15:04:29 -05:00
|
|
|
if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
|
2018-11-15 05:17:20 -05:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
2018-06-26 16:34:05 -04:00
|
|
|
}
|
|
|
|
}
|