Fixing bugs, adding a lookup test

This commit is contained in:
Armon Dadgar 2014-01-29 15:58:17 -08:00
parent c1e906499b
commit de767a076b
2 changed files with 50 additions and 3 deletions

View File

@ -101,7 +101,6 @@ func (s *Server) recv(c *net.UDPConn) {
for !s.shutdown { for !s.shutdown {
n, from, err := c.ReadFrom(buf) n, from, err := c.ReadFrom(buf)
if err != nil { if err != nil {
log.Printf("[ERR] mdns: Failed to read packet: %v", err)
continue continue
} }
if err := s.parsePacket(buf[:n], from); err != nil { if err := s.parsePacket(buf[:n], from); err != nil {
@ -122,19 +121,22 @@ func (s *Server) parsePacket(packet []byte, from net.Addr) error {
// handleQuery is used to handle an incoming query // handleQuery is used to handle an incoming query
func (s *Server) handleQuery(query *dns.Msg, from net.Addr) error { func (s *Server) handleQuery(query *dns.Msg, from net.Addr) error {
log.Printf("[DEBUG] mdns: query from %v", from)
var resp dns.Msg var resp dns.Msg
resp.SetReply(query) resp.SetReply(query)
// Handle each question // Handle each question
if len(query.Question) > 0 { if len(query.Question) > 0 {
if err := s.handleQuestion(query.Question[0], &resp); err != nil { if err := s.handleQuestion(query.Question[0], &resp); err != nil {
log.Printf("[ERR] mdns: Failed to handle question %v: %v", log.Printf("[ERR] mdns: failed to handle question %v: %v",
query.Question[0], err) query.Question[0], err)
} }
} }
// Check if there is an answer // Check if there is an answer
if len(query.Answer) > 0 { log.Printf("[DEBUG] mdns: %d records for %v",
len(resp.Answer), from)
if len(resp.Answer) > 0 {
return s.sendResponse(&resp, from) return s.sendResponse(&resp, from)
} }
return nil return nil

View File

@ -1,7 +1,9 @@
package mdns package mdns
import ( import (
"bytes"
"testing" "testing"
"time"
) )
func TestServer_StartStop(t *testing.T) { func TestServer_StartStop(t *testing.T) {
@ -12,3 +14,46 @@ func TestServer_StartStop(t *testing.T) {
} }
defer serv.Shutdown() defer serv.Shutdown()
} }
func TestServer_Lookup(t *testing.T) {
s := makeService(t)
s.Service = "_foobar._tcp"
s.Init()
serv, err := NewServer(&Config{s})
if err != nil {
t.Fatalf("err: %v", err)
}
defer serv.Shutdown()
entries := make(chan *ServiceEntry, 1)
found := false
go func() {
select {
case e := <-entries:
if e.Name != "hostname._foobar._tcp.local." {
t.Fatalf("bad: %v", e)
}
if !bytes.Equal(e.Addr.To4(), []byte{127, 0, 0, 1}) {
t.Fatalf("bad: %v", e)
}
if e.Port != 80 {
t.Fatalf("bad: %v", e)
}
if e.Info != "Local web server" {
t.Fatalf("bad: %v", e)
}
found = true
case <-time.After(80 * time.Millisecond):
t.Fatalf("timeout")
}
}()
err = LookupDomain("_foobar._tcp", "local", 50*time.Millisecond, entries)
if err != nil {
t.Fatalf("err: %v", err)
}
if !found {
t.Fatalf("record not found")
}
}