server_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package dns_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/google/go-cmp/cmp"
  6. "github.com/miekg/dns"
  7. "v2ray.com/core"
  8. "v2ray.com/core/app/dispatcher"
  9. . "v2ray.com/core/app/dns"
  10. "v2ray.com/core/app/policy"
  11. "v2ray.com/core/app/proxyman"
  12. _ "v2ray.com/core/app/proxyman/outbound"
  13. "v2ray.com/core/app/router"
  14. "v2ray.com/core/common"
  15. "v2ray.com/core/common/net"
  16. "v2ray.com/core/common/serial"
  17. feature_dns "v2ray.com/core/features/dns"
  18. "v2ray.com/core/proxy/freedom"
  19. "v2ray.com/core/testing/servers/udp"
  20. )
  21. type staticHandler struct {
  22. }
  23. func (*staticHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
  24. ans := new(dns.Msg)
  25. ans.Id = r.Id
  26. var clientIP net.IP
  27. opt := r.IsEdns0()
  28. if opt != nil {
  29. for _, o := range opt.Option {
  30. if o.Option() == dns.EDNS0SUBNET {
  31. subnet := o.(*dns.EDNS0_SUBNET)
  32. clientIP = subnet.Address
  33. }
  34. }
  35. }
  36. for _, q := range r.Question {
  37. if q.Name == "google.com." && q.Qtype == dns.TypeA {
  38. if clientIP == nil {
  39. rr, _ := dns.NewRR("google.com. IN A 8.8.8.8")
  40. ans.Answer = append(ans.Answer, rr)
  41. } else {
  42. rr, _ := dns.NewRR("google.com. IN A 8.8.4.4")
  43. ans.Answer = append(ans.Answer, rr)
  44. }
  45. } else if q.Name == "facebook.com." && q.Qtype == dns.TypeA {
  46. rr, _ := dns.NewRR("facebook.com. IN A 9.9.9.9")
  47. ans.Answer = append(ans.Answer, rr)
  48. } else if q.Name == "ipv6.google.com." && q.Qtype == dns.TypeA {
  49. rr, err := dns.NewRR("ipv6.google.com. IN A 8.8.8.7")
  50. common.Must(err)
  51. ans.Answer = append(ans.Answer, rr)
  52. } else if q.Name == "ipv6.google.com." && q.Qtype == dns.TypeAAAA {
  53. rr, err := dns.NewRR("ipv6.google.com. IN AAAA 2001:4860:4860::8888")
  54. common.Must(err)
  55. ans.Answer = append(ans.Answer, rr)
  56. } else if q.Name == "notexist.google.com." && q.Qtype == dns.TypeAAAA {
  57. ans.MsgHdr.Rcode = dns.RcodeNameError
  58. } else if q.Name == "hostname." && q.Qtype == dns.TypeA {
  59. rr, _ := dns.NewRR("hostname. IN A 127.0.0.1")
  60. ans.Answer = append(ans.Answer, rr)
  61. } else if q.Name == "hostname.local." && q.Qtype == dns.TypeA {
  62. rr, _ := dns.NewRR("hostname.local. IN A 127.0.0.1")
  63. ans.Answer = append(ans.Answer, rr)
  64. } else if q.Name == "hostname.localdomain." && q.Qtype == dns.TypeA {
  65. rr, _ := dns.NewRR("hostname.localdomain. IN A 127.0.0.1")
  66. ans.Answer = append(ans.Answer, rr)
  67. } else if q.Name == "localhost." && q.Qtype == dns.TypeA {
  68. rr, _ := dns.NewRR("localhost. IN A 127.0.0.2")
  69. ans.Answer = append(ans.Answer, rr)
  70. } else if q.Name == "localhost-a." && q.Qtype == dns.TypeA {
  71. rr, _ := dns.NewRR("localhost-a. IN A 127.0.0.3")
  72. ans.Answer = append(ans.Answer, rr)
  73. } else if q.Name == "localhost-b." && q.Qtype == dns.TypeA {
  74. rr, _ := dns.NewRR("localhost-b. IN A 127.0.0.4")
  75. ans.Answer = append(ans.Answer, rr)
  76. } else if q.Name == "Mijia\\ Cloud." && q.Qtype == dns.TypeA {
  77. rr, _ := dns.NewRR("Mijia\\ Cloud. IN A 127.0.0.1")
  78. ans.Answer = append(ans.Answer, rr)
  79. }
  80. }
  81. w.WriteMsg(ans)
  82. }
  83. func TestUDPServerSubnet(t *testing.T) {
  84. port := udp.PickPort()
  85. dnsServer := dns.Server{
  86. Addr: "127.0.0.1:" + port.String(),
  87. Net: "udp",
  88. Handler: &staticHandler{},
  89. UDPSize: 1200,
  90. }
  91. go dnsServer.ListenAndServe()
  92. time.Sleep(time.Second)
  93. config := &core.Config{
  94. App: []*serial.TypedMessage{
  95. serial.ToTypedMessage(&Config{
  96. NameServers: []*net.Endpoint{
  97. {
  98. Network: net.Network_UDP,
  99. Address: &net.IPOrDomain{
  100. Address: &net.IPOrDomain_Ip{
  101. Ip: []byte{127, 0, 0, 1},
  102. },
  103. },
  104. Port: uint32(port),
  105. },
  106. },
  107. ClientIp: []byte{7, 8, 9, 10},
  108. }),
  109. serial.ToTypedMessage(&dispatcher.Config{}),
  110. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  111. serial.ToTypedMessage(&policy.Config{}),
  112. },
  113. Outbound: []*core.OutboundHandlerConfig{
  114. {
  115. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  116. },
  117. },
  118. }
  119. v, err := core.New(config)
  120. common.Must(err)
  121. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  122. ips, err := client.LookupIP("google.com")
  123. if err != nil {
  124. t.Fatal("unexpected error: ", err)
  125. }
  126. if r := cmp.Diff(ips, []net.IP{{8, 8, 4, 4}}); r != "" {
  127. t.Fatal(r)
  128. }
  129. }
  130. func TestUDPServer(t *testing.T) {
  131. port := udp.PickPort()
  132. dnsServer := dns.Server{
  133. Addr: "127.0.0.1:" + port.String(),
  134. Net: "udp",
  135. Handler: &staticHandler{},
  136. UDPSize: 1200,
  137. }
  138. go dnsServer.ListenAndServe()
  139. time.Sleep(time.Second)
  140. config := &core.Config{
  141. App: []*serial.TypedMessage{
  142. serial.ToTypedMessage(&Config{
  143. NameServers: []*net.Endpoint{
  144. {
  145. Network: net.Network_UDP,
  146. Address: &net.IPOrDomain{
  147. Address: &net.IPOrDomain_Ip{
  148. Ip: []byte{127, 0, 0, 1},
  149. },
  150. },
  151. Port: uint32(port),
  152. },
  153. },
  154. }),
  155. serial.ToTypedMessage(&dispatcher.Config{}),
  156. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  157. serial.ToTypedMessage(&policy.Config{}),
  158. },
  159. Outbound: []*core.OutboundHandlerConfig{
  160. {
  161. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  162. },
  163. },
  164. }
  165. v, err := core.New(config)
  166. common.Must(err)
  167. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  168. {
  169. ips, err := client.LookupIP("google.com")
  170. if err != nil {
  171. t.Fatal("unexpected error: ", err)
  172. }
  173. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  174. t.Fatal(r)
  175. }
  176. }
  177. {
  178. ips, err := client.LookupIP("facebook.com")
  179. if err != nil {
  180. t.Fatal("unexpected error: ", err)
  181. }
  182. if r := cmp.Diff(ips, []net.IP{{9, 9, 9, 9}}); r != "" {
  183. t.Fatal(r)
  184. }
  185. }
  186. {
  187. _, err := client.LookupIP("notexist.google.com")
  188. if err == nil {
  189. t.Fatal("nil error")
  190. }
  191. if r := feature_dns.RCodeFromError(err); r != uint16(dns.RcodeNameError) {
  192. t.Fatal("expected NameError, but got ", r)
  193. }
  194. }
  195. {
  196. clientv6 := client.(feature_dns.IPv6Lookup)
  197. ips, err := clientv6.LookupIPv6("ipv4only.google.com")
  198. if err != feature_dns.ErrEmptyResponse {
  199. t.Fatal("error: ", err)
  200. }
  201. if len(ips) != 0 {
  202. t.Fatal("ips: ", ips)
  203. }
  204. }
  205. dnsServer.Shutdown()
  206. {
  207. ips, err := client.LookupIP("google.com")
  208. if err != nil {
  209. t.Fatal("unexpected error: ", err)
  210. }
  211. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  212. t.Fatal(r)
  213. }
  214. }
  215. }
  216. func TestPrioritizedDomain(t *testing.T) {
  217. port := udp.PickPort()
  218. dnsServer := dns.Server{
  219. Addr: "127.0.0.1:" + port.String(),
  220. Net: "udp",
  221. Handler: &staticHandler{},
  222. UDPSize: 1200,
  223. }
  224. go dnsServer.ListenAndServe()
  225. time.Sleep(time.Second)
  226. config := &core.Config{
  227. App: []*serial.TypedMessage{
  228. serial.ToTypedMessage(&Config{
  229. NameServers: []*net.Endpoint{
  230. {
  231. Network: net.Network_UDP,
  232. Address: &net.IPOrDomain{
  233. Address: &net.IPOrDomain_Ip{
  234. Ip: []byte{127, 0, 0, 1},
  235. },
  236. },
  237. Port: 9999, /* unreachable */
  238. },
  239. },
  240. NameServer: []*NameServer{
  241. {
  242. Address: &net.Endpoint{
  243. Network: net.Network_UDP,
  244. Address: &net.IPOrDomain{
  245. Address: &net.IPOrDomain_Ip{
  246. Ip: []byte{127, 0, 0, 1},
  247. },
  248. },
  249. Port: uint32(port),
  250. },
  251. PrioritizedDomain: []*NameServer_PriorityDomain{
  252. {
  253. Type: DomainMatchingType_Full,
  254. Domain: "google.com",
  255. },
  256. },
  257. },
  258. },
  259. }),
  260. serial.ToTypedMessage(&dispatcher.Config{}),
  261. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  262. serial.ToTypedMessage(&policy.Config{}),
  263. },
  264. Outbound: []*core.OutboundHandlerConfig{
  265. {
  266. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  267. },
  268. },
  269. }
  270. v, err := core.New(config)
  271. common.Must(err)
  272. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  273. startTime := time.Now()
  274. {
  275. ips, err := client.LookupIP("google.com")
  276. if err != nil {
  277. t.Fatal("unexpected error: ", err)
  278. }
  279. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  280. t.Fatal(r)
  281. }
  282. }
  283. endTime := time.Now()
  284. if startTime.After(endTime.Add(time.Second * 2)) {
  285. t.Error("DNS query doesn't finish in 2 seconds.")
  286. }
  287. }
  288. func TestUDPServerIPv6(t *testing.T) {
  289. port := udp.PickPort()
  290. dnsServer := dns.Server{
  291. Addr: "127.0.0.1:" + port.String(),
  292. Net: "udp",
  293. Handler: &staticHandler{},
  294. UDPSize: 1200,
  295. }
  296. go dnsServer.ListenAndServe()
  297. time.Sleep(time.Second)
  298. config := &core.Config{
  299. App: []*serial.TypedMessage{
  300. serial.ToTypedMessage(&Config{
  301. NameServers: []*net.Endpoint{
  302. {
  303. Network: net.Network_UDP,
  304. Address: &net.IPOrDomain{
  305. Address: &net.IPOrDomain_Ip{
  306. Ip: []byte{127, 0, 0, 1},
  307. },
  308. },
  309. Port: uint32(port),
  310. },
  311. },
  312. }),
  313. serial.ToTypedMessage(&dispatcher.Config{}),
  314. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  315. serial.ToTypedMessage(&policy.Config{}),
  316. },
  317. Outbound: []*core.OutboundHandlerConfig{
  318. {
  319. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  320. },
  321. },
  322. }
  323. v, err := core.New(config)
  324. common.Must(err)
  325. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  326. client6 := client.(feature_dns.IPv6Lookup)
  327. {
  328. ips, err := client6.LookupIPv6("ipv6.google.com")
  329. if err != nil {
  330. t.Fatal("unexpected error: ", err)
  331. }
  332. if r := cmp.Diff(ips, []net.IP{{32, 1, 72, 96, 72, 96, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136}}); r != "" {
  333. t.Fatal(r)
  334. }
  335. }
  336. }
  337. func TestStaticHostDomain(t *testing.T) {
  338. port := udp.PickPort()
  339. dnsServer := dns.Server{
  340. Addr: "127.0.0.1:" + port.String(),
  341. Net: "udp",
  342. Handler: &staticHandler{},
  343. UDPSize: 1200,
  344. }
  345. go dnsServer.ListenAndServe()
  346. time.Sleep(time.Second)
  347. config := &core.Config{
  348. App: []*serial.TypedMessage{
  349. serial.ToTypedMessage(&Config{
  350. NameServers: []*net.Endpoint{
  351. {
  352. Network: net.Network_UDP,
  353. Address: &net.IPOrDomain{
  354. Address: &net.IPOrDomain_Ip{
  355. Ip: []byte{127, 0, 0, 1},
  356. },
  357. },
  358. Port: uint32(port),
  359. },
  360. },
  361. StaticHosts: []*Config_HostMapping{
  362. {
  363. Type: DomainMatchingType_Full,
  364. Domain: "example.com",
  365. ProxiedDomain: "google.com",
  366. },
  367. },
  368. }),
  369. serial.ToTypedMessage(&dispatcher.Config{}),
  370. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  371. serial.ToTypedMessage(&policy.Config{}),
  372. },
  373. Outbound: []*core.OutboundHandlerConfig{
  374. {
  375. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  376. },
  377. },
  378. }
  379. v, err := core.New(config)
  380. common.Must(err)
  381. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  382. {
  383. ips, err := client.LookupIP("example.com")
  384. if err != nil {
  385. t.Fatal("unexpected error: ", err)
  386. }
  387. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  388. t.Fatal(r)
  389. }
  390. }
  391. dnsServer.Shutdown()
  392. }
  393. func TestIPMatch(t *testing.T) {
  394. port := udp.PickPort()
  395. dnsServer := dns.Server{
  396. Addr: "127.0.0.1:" + port.String(),
  397. Net: "udp",
  398. Handler: &staticHandler{},
  399. UDPSize: 1200,
  400. }
  401. go dnsServer.ListenAndServe()
  402. time.Sleep(time.Second)
  403. config := &core.Config{
  404. App: []*serial.TypedMessage{
  405. serial.ToTypedMessage(&Config{
  406. NameServer: []*NameServer{
  407. // private dns, not match
  408. {
  409. Address: &net.Endpoint{
  410. Network: net.Network_UDP,
  411. Address: &net.IPOrDomain{
  412. Address: &net.IPOrDomain_Ip{
  413. Ip: []byte{127, 0, 0, 1},
  414. },
  415. },
  416. Port: uint32(port),
  417. },
  418. Geoip: []*router.GeoIP{
  419. {
  420. CountryCode: "local",
  421. Cidr: []*router.CIDR{
  422. {
  423. // inner ip, will not match
  424. Ip: []byte{192, 168, 11, 1},
  425. Prefix: 32,
  426. },
  427. },
  428. },
  429. },
  430. },
  431. // second dns, match ip
  432. {
  433. Address: &net.Endpoint{
  434. Network: net.Network_UDP,
  435. Address: &net.IPOrDomain{
  436. Address: &net.IPOrDomain_Ip{
  437. Ip: []byte{127, 0, 0, 1},
  438. },
  439. },
  440. Port: uint32(port),
  441. },
  442. Geoip: []*router.GeoIP{
  443. {
  444. CountryCode: "test",
  445. Cidr: []*router.CIDR{
  446. {
  447. Ip: []byte{8, 8, 8, 8},
  448. Prefix: 32,
  449. },
  450. },
  451. },
  452. {
  453. CountryCode: "test",
  454. Cidr: []*router.CIDR{
  455. {
  456. Ip: []byte{8, 8, 8, 4},
  457. Prefix: 32,
  458. },
  459. },
  460. },
  461. },
  462. },
  463. },
  464. }),
  465. serial.ToTypedMessage(&dispatcher.Config{}),
  466. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  467. serial.ToTypedMessage(&policy.Config{}),
  468. },
  469. Outbound: []*core.OutboundHandlerConfig{
  470. {
  471. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  472. },
  473. },
  474. }
  475. v, err := core.New(config)
  476. common.Must(err)
  477. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  478. startTime := time.Now()
  479. {
  480. ips, err := client.LookupIP("google.com")
  481. if err != nil {
  482. t.Fatal("unexpected error: ", err)
  483. }
  484. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  485. t.Fatal(r)
  486. }
  487. }
  488. endTime := time.Now()
  489. if startTime.After(endTime.Add(time.Second * 2)) {
  490. t.Error("DNS query doesn't finish in 2 seconds.")
  491. }
  492. }
  493. func TestLocalDomain(t *testing.T) {
  494. port := udp.PickPort()
  495. dnsServer := dns.Server{
  496. Addr: "127.0.0.1:" + port.String(),
  497. Net: "udp",
  498. Handler: &staticHandler{},
  499. UDPSize: 1200,
  500. }
  501. go dnsServer.ListenAndServe()
  502. time.Sleep(time.Second)
  503. config := &core.Config{
  504. App: []*serial.TypedMessage{
  505. serial.ToTypedMessage(&Config{
  506. NameServers: []*net.Endpoint{
  507. {
  508. Network: net.Network_UDP,
  509. Address: &net.IPOrDomain{
  510. Address: &net.IPOrDomain_Ip{
  511. Ip: []byte{127, 0, 0, 1},
  512. },
  513. },
  514. Port: 9999, /* unreachable */
  515. },
  516. },
  517. NameServer: []*NameServer{
  518. {
  519. Address: &net.Endpoint{
  520. Network: net.Network_UDP,
  521. Address: &net.IPOrDomain{
  522. Address: &net.IPOrDomain_Ip{
  523. Ip: []byte{127, 0, 0, 1},
  524. },
  525. },
  526. Port: uint32(port),
  527. },
  528. PrioritizedDomain: []*NameServer_PriorityDomain{
  529. // Equivalent of dotless:localhost
  530. {Type: DomainMatchingType_Regex, Domain: "^[^.]*localhost[^.]*$"},
  531. },
  532. Geoip: []*router.GeoIP{
  533. { // Will match localhost, localhost-a and localhost-b,
  534. CountryCode: "local",
  535. Cidr: []*router.CIDR{
  536. {Ip: []byte{127, 0, 0, 2}, Prefix: 32},
  537. {Ip: []byte{127, 0, 0, 3}, Prefix: 32},
  538. {Ip: []byte{127, 0, 0, 4}, Prefix: 32},
  539. },
  540. },
  541. },
  542. },
  543. {
  544. Address: &net.Endpoint{
  545. Network: net.Network_UDP,
  546. Address: &net.IPOrDomain{
  547. Address: &net.IPOrDomain_Ip{
  548. Ip: []byte{127, 0, 0, 1},
  549. },
  550. },
  551. Port: uint32(port),
  552. },
  553. PrioritizedDomain: []*NameServer_PriorityDomain{
  554. // Equivalent of dotless: and domain:local
  555. {Type: DomainMatchingType_Regex, Domain: "^[^.]*$"},
  556. {Type: DomainMatchingType_Subdomain, Domain: "local"},
  557. {Type: DomainMatchingType_Subdomain, Domain: "localdomain"},
  558. },
  559. },
  560. },
  561. StaticHosts: []*Config_HostMapping{
  562. {
  563. Type: DomainMatchingType_Full,
  564. Domain: "hostnamestatic",
  565. Ip: [][]byte{{127, 0, 0, 53}},
  566. },
  567. {
  568. Type: DomainMatchingType_Full,
  569. Domain: "hostnamealias",
  570. ProxiedDomain: "hostname.localdomain",
  571. },
  572. },
  573. }),
  574. serial.ToTypedMessage(&dispatcher.Config{}),
  575. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  576. serial.ToTypedMessage(&policy.Config{}),
  577. },
  578. Outbound: []*core.OutboundHandlerConfig{
  579. {
  580. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  581. },
  582. },
  583. }
  584. v, err := core.New(config)
  585. common.Must(err)
  586. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  587. startTime := time.Now()
  588. { // Will match dotless:
  589. ips, err := client.LookupIP("hostname")
  590. if err != nil {
  591. t.Fatal("unexpected error: ", err)
  592. }
  593. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 1}}); r != "" {
  594. t.Fatal(r)
  595. }
  596. }
  597. { // Will match domain:local
  598. ips, err := client.LookupIP("hostname.local")
  599. if err != nil {
  600. t.Fatal("unexpected error: ", err)
  601. }
  602. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 1}}); r != "" {
  603. t.Fatal(r)
  604. }
  605. }
  606. { // Will match static ip
  607. ips, err := client.LookupIP("hostnamestatic")
  608. if err != nil {
  609. t.Fatal("unexpected error: ", err)
  610. }
  611. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 53}}); r != "" {
  612. t.Fatal(r)
  613. }
  614. }
  615. { // Will match domain replacing
  616. ips, err := client.LookupIP("hostnamealias")
  617. if err != nil {
  618. t.Fatal("unexpected error: ", err)
  619. }
  620. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 1}}); r != "" {
  621. t.Fatal(r)
  622. }
  623. }
  624. { // Will match dotless:localhost, but not expectIPs: 127.0.0.2, 127.0.0.3, then matches at dotless:
  625. ips, err := client.LookupIP("localhost")
  626. if err != nil {
  627. t.Fatal("unexpected error: ", err)
  628. }
  629. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 2}}); r != "" {
  630. t.Fatal(r)
  631. }
  632. }
  633. { // Will match dotless:localhost, and expectIPs: 127.0.0.2, 127.0.0.3
  634. ips, err := client.LookupIP("localhost-a")
  635. if err != nil {
  636. t.Fatal("unexpected error: ", err)
  637. }
  638. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 3}}); r != "" {
  639. t.Fatal(r)
  640. }
  641. }
  642. { // Will match dotless:localhost, and expectIPs: 127.0.0.2, 127.0.0.3
  643. ips, err := client.LookupIP("localhost-b")
  644. if err != nil {
  645. t.Fatal("unexpected error: ", err)
  646. }
  647. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 4}}); r != "" {
  648. t.Fatal(r)
  649. }
  650. }
  651. { // Will match dotless:
  652. ips, err := client.LookupIP("Mijia Cloud")
  653. if err != nil {
  654. t.Fatal("unexpected error: ", err)
  655. }
  656. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 1}}); r != "" {
  657. t.Fatal(r)
  658. }
  659. }
  660. endTime := time.Now()
  661. if startTime.After(endTime.Add(time.Second * 2)) {
  662. t.Error("DNS query doesn't finish in 2 seconds.")
  663. }
  664. }