File: dns_test.go

package info (click to toggle)
coyim 0.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,064 kB
  • ctags: 4,528
  • sloc: xml: 5,120; sh: 328; python: 286; makefile: 235; ruby: 51
file content (54 lines) | stat: -rw-r--r-- 1,450 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package xmpp

import (
	"encoding/hex"
	"net"

	. "gopkg.in/check.v1"
)

type DNSXmppSuite struct{}

var _ = Suite(&DNSXmppSuite{})

func fakeTCPConnToDNS(answer []byte) (net.Conn, error) {
	fakeResolver, _ := net.Listen("tcp", "127.0.0.1:0")
	go func() {
		conn, _ := fakeResolver.Accept()

		var dest [46]byte
		conn.Read(dest[:])
		conn.Write(answer)

		conn.Close()
	}()

	return net.Dial("tcp", fakeResolver.Addr().String())
}

func (s *DNSXmppSuite) Test_Resolve_resolvesCorrectly(c *C) {
	dec, _ := hex.DecodeString("00511eea818000010001000000000c5f786d70702d636c69656e74045f746370076f6c6162696e690273650000210001c00c0021000100000258001700000005146604786d7070076f6c6162696e6902736500")

	p := &mockProxy{}
	p.Expects(func(network, addr string) (net.Conn, error) {
		c.Check(network, Equals, "tcp")
		c.Check(addr, Equals, "208.67.222.222:53")

		return fakeTCPConnToDNS(dec)
	})

	hostport, err := ResolveSRVWithProxy(p, "olabini.se")
	c.Assert(err, IsNil)
	c.Assert(hostport[0], Equals, "xmpp.olabini.se:5222")
	c.Check(p, MatchesExpectations)
}

// WARNING: this test requires a real live connection to the Internet. Not so good...
func (s *DNSXmppSuite) Test_Resolve_handlesErrors(c *C) {
	_, err := Resolve("doesntexist.olabini.se")

	//It only happens when using golang resolver
	//ResolveSRVWithProxy will not return an error
	c.Assert(err, NotNil)
	c.Assert(err.Error(), Matches, "lookup _xmpp-client._tcp.doesntexist.olabini.se.*?")
}