1package dns
2
3import (
4	"testing"
5)
6
7func TestCmToM(t *testing.T) {
8	s := cmToM(0, 0)
9	if s != "0.00" {
10		t.Error("0, 0")
11	}
12
13	s = cmToM(1, 0)
14	if s != "0.01" {
15		t.Error("1, 0")
16	}
17
18	s = cmToM(3, 1)
19	if s != "0.30" {
20		t.Error("3, 1")
21	}
22
23	s = cmToM(4, 2)
24	if s != "4" {
25		t.Error("4, 2")
26	}
27
28	s = cmToM(5, 3)
29	if s != "50" {
30		t.Error("5, 3")
31	}
32
33	s = cmToM(7, 5)
34	if s != "7000" {
35		t.Error("7, 5")
36	}
37
38	s = cmToM(9, 9)
39	if s != "90000000" {
40		t.Error("9, 9")
41	}
42}
43
44func TestSplitN(t *testing.T) {
45	xs := splitN("abc", 5)
46	if len(xs) != 1 && xs[0] != "abc" {
47		t.Errorf("failure to split abc")
48	}
49
50	s := ""
51	for i := 0; i < 255; i++ {
52		s += "a"
53	}
54
55	xs = splitN(s, 255)
56	if len(xs) != 1 && xs[0] != s {
57		t.Errorf("failure to split 255 char long string")
58	}
59
60	s += "b"
61	xs = splitN(s, 255)
62	if len(xs) != 2 || xs[1] != "b" {
63		t.Errorf("failure to split 256 char long string: %d", len(xs))
64	}
65
66	// Make s longer
67	for i := 0; i < 255; i++ {
68		s += "a"
69	}
70	xs = splitN(s, 255)
71	if len(xs) != 3 || xs[2] != "a" {
72		t.Errorf("failure to split 510 char long string: %d", len(xs))
73	}
74}
75
76func TestSprintName(t *testing.T) {
77	got := sprintName("abc\\.def\007\"\127@\255\x05\xef\\")
78
79	if want := "abc\\.def\\007\\\"W\\@\\173\\005\\239"; got != want {
80		t.Errorf("expected %q, got %q", got, want)
81	}
82}
83
84func TestSprintTxtOctet(t *testing.T) {
85	got := sprintTxtOctet("abc\\.def\007\"\127@\255\x05\xef\\")
86
87	if want := "\"abc\\.def\\007\"W@\\173\\005\\239\""; got != want {
88		t.Errorf("expected %q, got %q", got, want)
89	}
90}
91
92func TestSprintTxt(t *testing.T) {
93	got := sprintTxt([]string{
94		"abc\\.def\007\"\127@\255\x05\xef\\",
95		"example.com",
96	})
97
98	if want := "\"abc.def\\007\\\"W@\\173\\005\\239\" \"example.com\""; got != want {
99		t.Errorf("expected %q, got %q", got, want)
100	}
101}
102
103func TestRPStringer(t *testing.T) {
104	rp := &RP{
105		Hdr: RR_Header{
106			Name:   "test.example.com.",
107			Rrtype: TypeRP,
108			Class:  ClassINET,
109			Ttl:    600,
110		},
111		Mbox: "\x05first.example.com.",
112		Txt:  "second.\x07example.com.",
113	}
114
115	const expected = "test.example.com.\t600\tIN\tRP\t\\005first.example.com. second.\\007example.com."
116	if rp.String() != expected {
117		t.Errorf("expected %v, got %v", expected, rp)
118	}
119
120	_, err := NewRR(rp.String())
121	if err != nil {
122		t.Fatalf("error parsing %q: %v", rp, err)
123	}
124}
125
126func BenchmarkSprintName(b *testing.B) {
127	for n := 0; n < b.N; n++ {
128		got := sprintName("abc\\.def\007\"\127@\255\x05\xef\\")
129
130		if want := "abc\\.def\\007\\\"W\\@\\173\\005\\239"; got != want {
131			b.Fatalf("expected %q, got %q", got, want)
132		}
133	}
134}
135
136func BenchmarkSprintName_NoEscape(b *testing.B) {
137	for n := 0; n < b.N; n++ {
138		got := sprintName("large.example.com")
139
140		if want := "large.example.com"; got != want {
141			b.Fatalf("expected %q, got %q", got, want)
142		}
143	}
144}
145
146func BenchmarkSprintTxtOctet(b *testing.B) {
147	for n := 0; n < b.N; n++ {
148		got := sprintTxtOctet("abc\\.def\007\"\127@\255\x05\xef\\")
149
150		if want := "\"abc\\.def\\007\"W@\\173\\005\\239\""; got != want {
151			b.Fatalf("expected %q, got %q", got, want)
152		}
153	}
154}
155
156func BenchmarkSprintTxt(b *testing.B) {
157	txt := []string{
158		"abc\\.def\007\"\127@\255\x05\xef\\",
159		"example.com",
160	}
161
162	b.ResetTimer()
163	for n := 0; n < b.N; n++ {
164		got := sprintTxt(txt)
165
166		if want := "\"abc.def\\007\\\"W@\\173\\005\\239\" \"example.com\""; got != want {
167			b.Fatalf("expected %q, got %q", got, want)
168		}
169	}
170}
171