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 {
n, from, err := c.ReadFrom(buf)
if err != nil {
log.Printf("[ERR] mdns: Failed to read packet: %v", err)
continue
}
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
func (s *Server) handleQuery(query *dns.Msg, from net.Addr) error {
log.Printf("[DEBUG] mdns: query from %v", from)
var resp dns.Msg
resp.SetReply(query)
// Handle each question
if len(query.Question) > 0 {
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)
}
}
// 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 nil

View File

@ -1,7 +1,9 @@
package mdns
import (
"bytes"
"testing"
"time"
)
func TestServer_StartStop(t *testing.T) {
@ -12,3 +14,46 @@ func TestServer_StartStop(t *testing.T) {
}
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")
}
}