-
Notifications
You must be signed in to change notification settings - Fork 11
/
compression_test.go
162 lines (128 loc) · 2.54 KB
/
compression_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package dns
import (
"bytes"
"testing"
)
func TestCompressor(t *testing.T) {
tests := []struct {
name string
fqdn string
state map[string]int
buf []byte
raw []byte
err error
}{
{
name: ".",
fqdn: ".",
raw: []byte{0x00},
},
{
name: "example.com",
fqdn: "example.com.",
raw: []byte{
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
},
},
{
name: "compressed-example.com",
fqdn: "example.com.",
state: map[string]int{"com.": 5},
buf: make([]byte, 2),
raw: []byte{
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0xC0, 0x05,
},
},
{
name: "invalid-fqdn",
fqdn: "invalid.com",
err: errInvalidFQDN,
},
}
t.Parallel()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
com := compressor{test.state, 0}
length, err := com.Length(test.fqdn)
if err != nil {
if want, got := test.err, err; want != got {
t.Errorf("want err %q, got %q", want, got)
}
} else {
if want, got := len(test.raw), length; want != got {
t.Fatalf("want compressed length %d, got %d", want, got)
}
}
raw, err := com.Pack(test.buf, test.fqdn)
if err != nil {
if want, got := test.err, err; want != got {
t.Errorf("want err %q, got %q", want, got)
}
return
}
if want, got := append(test.buf, test.raw...), raw; !bytes.Equal(want, got) {
t.Errorf("want compressed name %x, got %x", want, got)
}
})
}
}
func TestDecompressor(t *testing.T) {
tests := []struct {
name string
raw []byte
state []byte
fqdn string
err error
}{
{
name: ".",
raw: []byte{0x00},
fqdn: ".",
},
{
name: "example.com",
raw: []byte{
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
},
fqdn: "example.com.",
},
{
name: "compressed-example.com",
raw: []byte{
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0xC0, 0x05,
},
state: []byte{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x03, 'c', 'o', 'm',
0x00,
},
fqdn: "example.com.",
},
}
t.Parallel()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
dec := decompressor(test.state)
fqdn, _, err := dec.Unpack(test.raw)
if err != nil {
if want, got := test.err, err; want != got {
t.Errorf("want err %q, got %q", want, got)
}
return
}
if want, got := test.fqdn, fqdn; want != got {
t.Errorf("want decompressed name %q, got %q", want, got)
}
})
}
}