<commit_before>package weavedns

import (
	"github.com/miekg/dns"
	"log"
	"net"
	"testing"
	"time"
)

var (
	test_addr = net.ParseIP("172.16.17.32")
)

func minimalServer(w dns.ResponseWriter, req *dns.Msg) {
	//log.Println("minimalServer received:", req)
	if len(req.Answer) > 0 {
		return // Only interested in questions.
	}

	m := new(dns.Msg)
	m.SetReply(req)

	hdr := dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA,
		Class: dns.ClassINET, Ttl: 3600}
	a := &dns.A{hdr, test_addr}
	m.Answer = append(m.Answer, a)

	buf, err := m.Pack()
	if err != nil {
		log.Fatal(err)
	}
	if buf == nil {
		log.Fatal("Nil buffer")
	}
	//log.Println("minimalServer sending:", buf)
	// This is a bit of a kludge - per the RFC we should send responses from 5353, but that doesn't seem to work
	sendconn, err := net.DialUDP("udp", nil, ipv4Addr)
	if err != nil {
		log.Fatal(err)
	}
	_, err = sendconn.Write(buf)
	sendconn.Close()
	if err != nil {
		log.Fatal(err)
	}
}

func RunLocalMulticastServer() error {
	mux := dns.NewServeMux()
	mux.HandleFunc("weave", minimalServer)
	multicast, err := net.ListenMulticastUDP("udp", nil, ipv4Addr)
	if err != nil {
		return err
	}
	go dns.ActivateAndServe(nil, multicast, mux)
	return nil
}

func TestSimpleQuery(t *testing.T) {
	log.Println("TestSimpleQuery starting")
	mdnsClient, err := NewMDNSClient()
	if err != nil {
		t.Fatal(err)
	}
	err = mdnsClient.Start(nil)
	if err != nil {
		t.Fatal(err)
	}

	err = RunLocalMulticastServer()
	if err != nil {
		t.Fatalf("Unable to run test server: %s", err)
	}

	var received_addr net.IP

	channel := make(chan *ResponseA, 4)
	go func() {
		for resp := range channel {
			log.Printf("Got address response %s addr %s", resp.Name, resp.Addr)
			received_addr = resp.Addr
		}
	}()

	mdnsClient.SendQuery("test.weave.", dns.TypeA, channel)

	time.Sleep(200 * time.Millisecond)

	if !received_addr.Equal(test_addr) {
		t.Log("Unexpected result for test.weave", received_addr)
		t.Fail()
	}
}<commit_msg>Shut down minimal server at end of test<commit_after>package weavedns

import (
	"github.com/miekg/dns"
	"log"
	"net"
	"testing"
	"time"
)

var (
	test_addr = net.ParseIP("172.16.17.32")
)

func minimalServer(w dns.ResponseWriter, req *dns.Msg) {
	//log.Println("minimalServer received:", req)
	if len(req.Answer) > 0 {
		return // Only interested in questions.
	}

	m := new(dns.Msg)
	m.SetReply(req)

	hdr := dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA,
		Class: dns.ClassINET, Ttl: 3600}
	a := &dns.A{hdr, test_addr}
	m.Answer = append(m.Answer, a)

	buf, err := m.Pack()
	if err != nil {
		log.Fatal(err)
	}
	if buf == nil {
		log.Fatal("Nil buffer")
	}
	//log.Println("minimalServer sending:", buf)
	// This is a bit of a kludge - per the RFC we should send responses from 5353, but that doesn't seem to work
	sendconn, err := net.DialUDP("udp", nil, ipv4Addr)
	if err != nil {
		log.Fatal(err)
	}
	_, err = sendconn.Write(buf)
	sendconn.Close()
	if err != nil {
		log.Fatal(err)
	}
}

func RunLocalMulticastServer() (*dns.Server, error) {
	mux := dns.NewServeMux()
	mux.HandleFunc("weave", minimalServer)
	multicast, err := net.ListenMulticastUDP("udp", nil, ipv4Addr)
	if err != nil {
		return nil, err
	}
	server := &dns.Server{Listener: nil, PacketConn: multicast, Handler: mux}
	go server.ActivateAndServe()
	return server, nil
}

func TestSimpleQuery(t *testing.T) {
	log.Println("TestSimpleQuery starting")
	mdnsClient, err := NewMDNSClient()
	if err != nil {
		t.Fatal(err)
	}
	err = mdnsClient.Start(nil)
	if err != nil {
		t.Fatal(err)
	}

	server, err := RunLocalMulticastServer()
	if err != nil {
		t.Fatalf("Unable to run test server: %s", err)
	}
	defer server.Shutdown()

	var received_addr net.IP

	channel := make(chan *ResponseA, 4)
	go func() {
		for resp := range channel {
			log.Printf("Got address response %s addr %s", resp.Name, resp.Addr)
			received_addr = resp.Addr
		}
	}()

	mdnsClient.SendQuery("test.weave.", dns.TypeA, channel)

	time.Sleep(200 * time.Millisecond)

	if !received_addr.Equal(test_addr) {
		t.Log("Unexpected result for test.weave", received_addr)
		t.Fail()
	}
}
{'fraction_non_alphanumeric': 0.1539022611232677, 'fraction_numerical': 0.012156576707999028, 'mean_word_length': 3.8004667444574096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1045416', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1625, 'n_words': 392}