<commit_before>package dns

import (
	"encoding/hex"
	"fmt"
	"net"
	"reflect"
	"strings"
	"testing"
)

func TestCompressLength(t *testing.T) {
	m := new(Msg)
	m.SetQuestion("miek.nl", TypeMX)
	ul := m.Len()
	m.Compress = true
	if ul != m.Len() {
		t.Fatalf("should be equal")
	}
}

// Does the predicted length match final packed length?
func TestMsgCompressLength(t *testing.T) {
	makeMsg := func(question string, ans, ns, e []RR) *Msg {
		msg := new(Msg)
		msg.SetQuestion(Fqdn(question), TypeANY)
		msg.Answer = append(msg.Answer, ans...)
		msg.Ns = append(msg.Ns, ns...)
		msg.Extra = append(msg.Extra, e...)
		msg.Compress = true
		return msg
	}

	name1 := "12345678901234567890123456789012345.12345678.123."
	rrA := testRR(name1 + " 3600 IN A 192.0.2.1")
	rrMx := testRR(name1 + " 3600 IN MX 10 " + name1)
	tests := []*Msg{
		makeMsg(name1, []RR{rrA}, nil, nil),
		makeMsg(name1, []RR{rrMx, rrMx}, nil, nil)}

	for _, msg := range tests {
		predicted := msg.Len()
		buf, err := msg.Pack()
		if err != nil {
			t.Error(err)
		}
		if predicted < len(buf) {
			t.Errorf("predicted compressed length is wrong: predicted %s (len=%d) %d, actual %d",
				msg.Question[0].Name, len(msg.Answer), predicted, len(buf))
		}
	}
}

func TestMsgLength(t *testing.T) {
	makeMsg := func(question string, ans, ns, e []RR) *Msg {
		msg := new(Msg)
		msg.Compress = true
		msg.SetQuestion(Fqdn(question), TypeANY)
		msg.Answer = append(msg.Answer, ans...)
		msg.Ns = append(msg.Ns, ns...)
		msg.Extra = append(msg.Extra, e...)
		return msg
	}

	name1 := "12345678901234567890123456789012345.12345678.123."
	rrA := testRR(name1 + " 3600 IN A 192.0.2.1")
	rrMx := testRR(name1 + " 3600 IN MX 10 " + name1)
	tests := []*Msg{
		makeMsg(name1, []RR{rrA}, nil, nil),
		makeMsg(name1, []RR{rrMx, rrMx}, nil, nil)}

	for _, msg := range tests {
		predicted := msg.Len()
		buf, err := msg.Pack()
		if err != nil {
			t.Error(err)
		}
		if predicted < len(buf) {
			t.Errorf("predicted length is wrong: predicted %s (len=%d), actual %d",
				msg.Question[0].Name, predicted, len(buf))
		}
	}
}

func TestCompressionLenHelper(t *testing.T) {
	c := make(map[string]int)
	compressionLenHelper(c, "example.com", 12)
	if c["example.com"] != 12 {
		t.Errorf("bad %d", c["example.com"])
	}
	if c["com"] != 20 {
		t.Errorf("bad %d", c["com"])
	}

	// Test boundaries
	c = make(map[string]int)
	// foo label starts at 16379
	// com label starts at 16384
	compressionLenHelper(c, "foo.com", 16379)
	if c["foo.com"] != 16379 {
		t.Errorf("bad %d", c["foo.com"])
	}
	// com label is accessible
	if c["com"] != 16383 {
		t.Errorf("bad %d", c["com"])
	}

	c = make(map[string]int)
	// foo label starts at 16379
	// com label starts at 16385 => outside range
	compressionLenHelper(c, "foo.com", 16380)
	if c["foo.com"] != 16380 {
		t.Errorf("bad %d", c["foo.com"])
	}
	// com label is NOT accessible
	if c["com"] != 0 {
		t.Errorf("bad %d", c["com"])
	}

	c = make(map[string]int)
	compressionLenHelper(c, "example.com", 16375)
	if c["example.com"] != 16375 {
		t.Errorf("bad %d", c["example.com"])
	}
	// com starts AFTER 16384
	if c["com"] != 16383 {
		t.Errorf("bad %d", c["com"])
	}

	c = make(map[string]int)
	compressionLenHelper(c, "example.com", 16376)
	if c["example.com"] != 16376 {
		t.Errorf("bad %d", c["example.com"])
	}
	// com starts AFTER 16384
	if c["com"] != 0 {
		t.Errorf("bad %d", c["com"])
	}
}

func TestCompressionLenSearch(t *testing.T) {
	c := make(map[string]int)
	compressed, ok, fullSize := compressionLenSearch(c, "a.b.org.")
	if compressed != 0 || ok || fullSize != 14 {
		panic(fmt.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize))
	}
	c["org."] = 3
	compressed, ok, fullSize = compressionLenSearch(c, "a.b.org.")
	if compressed != 4 || !ok || fullSize != 8 {
		panic(fmt.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize))
	}
	c["b.org."] = 5
	compressed, ok, fullSize = compressionLenSearch(c, "a.b.org.")
	if compressed != 6 || !ok || fullSize != 4 {
		panic(fmt.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize))
	}
	// Not found long compression
	c["x.b.org."] = 5
	compressed, ok, fullSize = compressionLenSearch(c, "a.b.org.")
	if compressed != 6 || !ok || fullSize != 4 {
		panic(fmt.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize))
	}
	// Found long compression
	c["a.b.org."] = 5
	compressed, ok, fullSize = compressionLenSearch(c, "a.b.org.")
	if compressed != 8 || !ok || fullSize != 0 {
		panic(fmt.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize))
	}
}

func TestMsgLength2(t *testing.T) {
	// Serialized replies
	var testMessages = []string{
		// google.com. IN A?
		"064e81800001000b0004000506676f6f676c6503636f6d0000010001c00c00010001000000050004adc22986c00c00010001000000050004adc22987c00c00010001000000050004adc22988c00c00010001000000050004adc22989c00c00010001000000050004adc2298ec00c00010001000000050004adc22980c00c00010001000000050004adc22981c00c00010001000000050004adc22982c00c00010001000000050004adc22983c00c00010001000000050004adc22984c00c00010001000000050004adc22985c00c00020001000000050006036e7331c00cc00c00020001000000050006036e7332c00cc00c00020001000000050006036e7333c00cc00c00020001000000050006036e7334c00cc0d800010001000000050004d8ef200ac0ea00010001000000050004d8ef220ac0fc00010001000000050004d8ef240ac10e00010001000000050004d8ef260a0000290500000000050000",<commit_msg>Use t.Errorf not panic in TestCompressionLenSearch (#832)<commit_after>package dns

import (
	"encoding/hex"
	"fmt"
	"net"
	"reflect"
	"strings"
	"testing"
)

func TestCompressLength(t *testing.T) {
	m := new(Msg)
	m.SetQuestion("miek.nl", TypeMX)
	ul := m.Len()
	m.Compress = true
	if ul != m.Len() {
		t.Fatalf("should be equal")
	}
}

// Does the predicted length match final packed length?
func TestMsgCompressLength(t *testing.T) {
	makeMsg := func(question string, ans, ns, e []RR) *Msg {
		msg := new(Msg)
		msg.SetQuestion(Fqdn(question), TypeANY)
		msg.Answer = append(msg.Answer, ans...)
		msg.Ns = append(msg.Ns, ns...)
		msg.Extra = append(msg.Extra, e...)
		msg.Compress = true
		return msg
	}

	name1 := "12345678901234567890123456789012345.12345678.123."
	rrA := testRR(name1 + " 3600 IN A 192.0.2.1")
	rrMx := testRR(name1 + " 3600 IN MX 10 " + name1)
	tests := []*Msg{
		makeMsg(name1, []RR{rrA}, nil, nil),
		makeMsg(name1, []RR{rrMx, rrMx}, nil, nil)}

	for _, msg := range tests {
		predicted := msg.Len()
		buf, err := msg.Pack()
		if err != nil {
			t.Error(err)
		}
		if predicted < len(buf) {
			t.Errorf("predicted compressed length is wrong: predicted %s (len=%d) %d, actual %d",
				msg.Question[0].Name, len(msg.Answer), predicted, len(buf))
		}
	}
}

func TestMsgLength(t *testing.T) {
	makeMsg := func(question string, ans, ns, e []RR) *Msg {
		msg := new(Msg)
		msg.Compress = true
		msg.SetQuestion(Fqdn(question), TypeANY)
		msg.Answer = append(msg.Answer, ans...)
		msg.Ns = append(msg.Ns, ns...)
		msg.Extra = append(msg.Extra, e...)
		return msg
	}

	name1 := "12345678901234567890123456789012345.12345678.123."
	rrA := testRR(name1 + " 3600 IN A 192.0.2.1")
	rrMx := testRR(name1 + " 3600 IN MX 10 " + name1)
	tests := []*Msg{
		makeMsg(name1, []RR{rrA}, nil, nil),
		makeMsg(name1, []RR{rrMx, rrMx}, nil, nil)}

	for _, msg := range tests {
		predicted := msg.Len()
		buf, err := msg.Pack()
		if err != nil {
			t.Error(err)
		}
		if predicted < len(buf) {
			t.Errorf("predicted length is wrong: predicted %s (len=%d), actual %d",
				msg.Question[0].Name, predicted, len(buf))
		}
	}
}

func TestCompressionLenHelper(t *testing.T) {
	c := make(map[string]int)
	compressionLenHelper(c, "example.com", 12)
	if c["example.com"] != 12 {
		t.Errorf("bad %d", c["example.com"])
	}
	if c["com"] != 20 {
		t.Errorf("bad %d", c["com"])
	}

	// Test boundaries
	c = make(map[string]int)
	// foo label starts at 16379
	// com label starts at 16384
	compressionLenHelper(c, "foo.com", 16379)
	if c["foo.com"] != 16379 {
		t.Errorf("bad %d", c["foo.com"])
	}
	// com label is accessible
	if c["com"] != 16383 {
		t.Errorf("bad %d", c["com"])
	}

	c = make(map[string]int)
	// foo label starts at 16379
	// com label starts at 16385 => outside range
	compressionLenHelper(c, "foo.com", 16380)
	if c["foo.com"] != 16380 {
		t.Errorf("bad %d", c["foo.com"])
	}
	// com label is NOT accessible
	if c["com"] != 0 {
		t.Errorf("bad %d", c["com"])
	}

	c = make(map[string]int)
	compressionLenHelper(c, "example.com", 16375)
	if c["example.com"] != 16375 {
		t.Errorf("bad %d", c["example.com"])
	}
	// com starts AFTER 16384
	if c["com"] != 16383 {
		t.Errorf("bad %d", c["com"])
	}

	c = make(map[string]int)
	compressionLenHelper(c, "example.com", 16376)
	if c["example.com"] != 16376 {
		t.Errorf("bad %d", c["example.com"])
	}
	// com starts AFTER 16384
	if c["com"] != 0 {
		t.Errorf("bad %d", c["com"])
	}
}

func TestCompressionLenSearch(t *testing.T) {
	c := make(map[string]int)
	compressed, ok, fullSize := compressionLenSearch(c, "a.b.org.")
	if compressed != 0 || ok || fullSize != 14 {
		t.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize)
	}
	c["org."] = 3
	compressed, ok, fullSize = compressionLenSearch(c, "a.b.org.")
	if compressed != 4 || !ok || fullSize != 8 {
		t.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize)
	}
	c["b.org."] = 5
	compressed, ok, fullSize = compressionLenSearch(c, "a.b.org.")
	if compressed != 6 || !ok || fullSize != 4 {
		t.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize)
	}
	// Not found long compression
	c["x.b.org."] = 5
	compressed, ok, fullSize = compressionLenSearch(c, "a.b.org.")
	if compressed != 6 || !ok || fullSize != 4 {
		t.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize)
	}
	// Found long compression
	c["a.b.org."] = 5
	compressed, ok, fullSize = compressionLenSearch(c, "a.b.org.")
	if compressed != 8 || !ok || fullSize != 0 {
		t.Errorf("Failed: compressed:=%d, ok:=%v, fullSize:=%d", compressed, ok, fullSize)
	}
}

func TestMsgLength2(t *testing.T) {
	// Serialized replies
	var testMessages = []string{
		// google.com. IN A?
		"064e81800001000b0004000506676f6f676c6503636f6d0000010001c00c00010001000000050004adc22986c00c00010001000000050004adc22987c00c00010001000000050004adc22988c00c00010001000000050004adc22989c00c00010001000000050004adc2298ec00c00010001000000050004adc22980c00c00010001000000050004adc22981c00c00010001000000050004adc22982c00c00010001000000050004adc22983c00c00010001000000050004adc22984c00c00010001000000050004adc22985c00c00020001000000050006036e7331c00cc00c00020001000000050006036e7332c00cc00c00020001000000050006036e7333c00cc00c00020001000000050006036e7334c00cc0d800010001000000050004d8ef200ac0ea00010001000000050004d8ef220ac0fc00010001000000050004d8ef240ac10e00010001000000050004d8ef260a0000290500000000050000",
{'fraction_non_alphanumeric': 0.1739289973396936, 'fraction_numerical': 0.1534721585175672, 'mean_word_length': 4.43469591226321, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 40, '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': '3340355', 'n_tokens_mistral': 5624, 'n_tokens_neox': 4283, 'n_words': 909}