示例#1
0
文件: rpdns.go 项目: jedisct1/rpdns
func sendTruncated(w dns.ResponseWriter, msgHdr dns.MsgHdr) {
	emptyResp := new(dns.Msg)
	emptyResp.MsgHdr = msgHdr
	emptyResp.Response = true
	if _, isTCP := w.RemoteAddr().(*net.TCPAddr); isTCP {
		dns.HandleFailed(w, emptyResp)
		return
	}
	emptyResp.Truncated = true
	w.WriteMsg(emptyResp)
}
示例#2
0
func testResolution(t *testing.T, tester ResolveTester, rt ResolveTest) {
	t.Log(" => " + rt.Description)

	// Construct query
	query := new(dns.Msg)
	query.MsgHdr = rt.Header
	query.Question = rt.QuestionSection

	// Get response
	response, err := tester.GetResponse(query)
	was_failure := tester.WasFailure(response, err)
	if rt.ShouldFail {
		// Expecting a failure...
		// ...in fact, if we don't get one, that's BAD
		if !was_failure {
			t.Fatal("Test should have failed, didn't")
		}
		return
	} else {
		// Normal case, should not fail
		if was_failure {
			t.Log(rt)
			t.Fatal(err)
		}
	}

	// Construct expected response
	expected := new(dns.Msg)
	expected.MsgHdr = rt.ExpectedHeader
	expected.Question = query.Question
	expected.Answer = make([]dns.RR, len(rt.ExpectedAnswers))
	for i, answer := range rt.ExpectedAnswers {
		rr, err := dns.NewRR(answer)
		if err != nil {
			t.Fatal(err)
		}
		expected.Answer[i] = rr
	}
	expected.Ns = make([]dns.RR, 0)
	expected.Extra = make([]dns.RR, 0)

	compare_part := func(item1, item2 interface{}, name string) {
		if !reflect.DeepEqual(item1, item2) {
			t.Logf(" * %s is different", name)
			t.Logf("%#v vs %#v", item1, item2)
		}
	}

	// DNS package tends to be loose about some encoding details,
	// only calculating them right before putting the data on the
	// wire.
	sanitize := func(rr_list []dns.RR) {
		for i := range rr_list {
			rr_list[i].Header().Rdlength = 0
		}
	}
	for _, msg := range []*dns.Msg{response, expected} {
		sanitize(msg.Answer)
		sanitize(msg.Ns)
		sanitize(msg.Extra)
	}

	// Confirm equality
	if !reflect.DeepEqual(response, expected) {
		t.Log(response)
		t.Log(expected)
		t.Log("Response not equal to expected response")

		// More DRY to use reflect, but it would also be like
		// chewing broken glass.
		compare_part(response.MsgHdr, expected.MsgHdr, "MsgHdr")
		compare_part(response.Compress, expected.Compress, "Compress")
		compare_part(response.Question, expected.Question, "Question")
		compare_part(response.Answer, expected.Answer, "Answer")
		compare_part(response.Ns, expected.Ns, "Ns")
		compare_part(response.Extra, expected.Extra, "Extra")

		t.FailNow()
	}
}