-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptr_test.go
88 lines (85 loc) · 1.92 KB
/
ptr_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
package dns
import (
"bytes"
"reflect"
"testing"
)
func TestPtr_Parse(t *testing.T) {
type args struct {
buf []byte
ptr int
domains *Domains
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "Simple domain name",
args: args{
buf: []byte("\x06golang\x03com\x00"),
domains: NewDomains(),
},
want: "golang.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := &Ptr{}
if err := n.Parse(bytes.NewBuffer(tt.args.buf), tt.args.ptr, tt.args.domains); (err != nil) != tt.wantErr {
t.Errorf("Ptr.Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(n.Name, tt.want) {
t.Errorf("Ptr.Parse() = %v, want %v", n.Name, tt.want)
}
})
}
}
func TestPtr_Build(t *testing.T) {
tests := []struct {
name string
recordName string
want []byte
wantLength int
wantErr bool
}{
{
name: "Simple domain",
recordName: "golang.com",
want: []byte("\x06golang\x03com\x00"),
wantLength: 12,
},
{
name: "Name with multibyte(3) chars",
recordName: "ㄶ.golang.com",
want: []byte("\x03\xE3\x84\xB6\x06golang\x03com\x00"),
wantLength: 16,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := &Ptr{
Name: tt.recordName,
}
buf := new(bytes.Buffer)
if length, err := n.PreBuild(&Record{}, NewDomains()); err != nil || length != tt.wantLength {
if err != nil {
t.Errorf("Ptr.PreBuild() error = %v", err)
} else {
t.Errorf("Ptr.PreBuild() = %v, want %v", length, tt.wantLength)
}
return
}
if err := n.Build(buf, NewDomains()); (err != nil) != tt.wantErr {
t.Errorf("Ptr.Build() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(buf.Bytes(), tt.want) {
t.Errorf("Ptr.Build() = %v, want %v", buf.Bytes(), tt.want)
}
})
}
}