Skip to content

Commit

Permalink
Bringing back and changing some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atomirex committed Dec 22, 2024
1 parent 4204667 commit df66b75
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
8 changes: 4 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ func (c *Conn) writeToSocket(
}
}

func (c *Conn) createAnswer(id uint16, q dnsmessage.Question, addr netip.Addr, config *Config) (dnsmessage.Message, error) {
func createAnswer(id uint16, q dnsmessage.Question, addr netip.Addr, config *Config) (dnsmessage.Message, error) {
packedName, err := dnsmessage.NewName(q.Name.String())
if err != nil {
return dnsmessage.Message{}, err
Expand Down Expand Up @@ -767,7 +767,7 @@ func (c *Conn) createAnswer(id uint16, q dnsmessage.Question, addr netip.Addr, c
}

func (c *Conn) sendAnswer(queryID uint16, q dnsmessage.Question, ifIndex int, result netip.Addr, dst *net.UDPAddr, config *Config) {
answer, err := c.createAnswer(queryID, q, result, config)
answer, err := createAnswer(queryID, q, result, config)
if err != nil {
c.log.Warnf("[%s] failed to create mDNS answer %v", c.name, err)
return
Expand Down Expand Up @@ -1066,7 +1066,7 @@ func (c *Conn) readLoop(name string, pktConn ipPacketConn, inboundBufferSize int
for _, query := range queries {
queryCopy := query
if queryCopy.nameWithSuffix == a.Header.Name.String() {
addr, err := addrFromAnswerHeader(a)
addr, err := addrFromAnswer(a)
if err != nil {
c.log.Warnf("[%s] failed to parse mDNS answer %v", c.name, err)
return
Expand Down Expand Up @@ -1166,7 +1166,7 @@ func (c *Conn) start(started chan<- struct{}, inboundBufferSize int, config *Con
}
}

func addrFromAnswerHeader(answer dnsmessage.Resource) (*netip.Addr, error) {
func addrFromAnswer(answer dnsmessage.Resource) (*netip.Addr, error) {
switch answer.Header.Type {
case dnsmessage.TypeA:
if a, ok := answer.Body.(*dnsmessage.AResource); ok {
Expand Down
67 changes: 67 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,73 @@ func TestQueryRespectClose(t *testing.T) {
}
}

func testResourceParsing(t *testing.T, echoQuery bool) {
lookForIP := func(msg dnsmessage.Message, expectedIP []byte, t *testing.T) {
actualAddr, err := addrFromAnswer(msg.Answers[0])
if err != nil {
t.Fatal(err)
}

if echoQuery {
if len(msg.Questions) == 0 {
t.Fatal("Echoed query not included in answer")
}
} else {
if len(msg.Questions) > 0 {
t.Fatal("Echoed query erroneously included in answer")
}
}

if !bytes.Equal(actualAddr.AsSlice(), expectedIP) {
t.Fatalf("Expected(%v) and Actual(%v) IP don't match", expectedIP, actualAddr)
}
}

name := "test-server."

config := &Config{
DoNotEchoQueryWithAnswer: !echoQuery,
}

t.Run("A Record", func(t *testing.T) {
answer, err := createAnswer(1, dnsmessage.Question{
Name: dnsmessage.MustNewName(name),
Type: dnsmessage.TypeA,
}, mustAddr(net.IP{127, 0, 0, 1}), config)
if err != nil {
t.Fatal(err)
}
lookForIP(answer, []byte{127, 0, 0, 1}, t)
})

t.Run("AAAA Record", func(t *testing.T) {
answer, err := createAnswer(1, dnsmessage.Question{
Name: dnsmessage.MustNewName(name),
Type: dnsmessage.TypeAAAA,
}, netip.MustParseAddr("::1"), config)
if err != nil {
t.Fatal(err)
}
lookForIP(answer, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, t)
})
}

func TestResourceParsingWithEchoedQuery(t *testing.T) {
testResourceParsing(t, true)
}

func TestResourceParsingWithoutEchoedQuery(t *testing.T) {
testResourceParsing(t, false)
}

func mustAddr(ip net.IP) netip.Addr {
addr, ok := netip.AddrFromSlice(ip)
if !ok {
panic(ipToAddrError{ip})
}
return addr
}

func TestIPToBytes(t *testing.T) {
expectedIP := []byte{127, 0, 0, 1}
actualAddr4, err := ipv4ToBytes(netip.MustParseAddr("127.0.0.1"))
Expand Down

0 comments on commit df66b75

Please sign in to comment.