-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_test.go
77 lines (73 loc) · 1.56 KB
/
a_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
package dns
import (
"bytes"
"reflect"
"testing"
"net/netip"
)
func TestIPv4_Parse(t *testing.T) {
type args struct {
buf []byte
}
tests := []struct {
name string
args args
want IPv4
wantErr bool
}{
{
name: "Simple IPv4 address",
args: args{buf: []byte("\xC0\xA8\x16\xBC")},
want: IPv4{netip.MustParseAddr("192.168.22.188")},
},
{
name: "Not enough data in buffer",
args: args{buf: []byte("\xC0\xA8\x16")},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ip := IPv4{}
if err := ip.Parse(bytes.NewBuffer(tt.args.buf), 0, &Domains{}); (err != nil) != tt.wantErr {
t.Errorf("IPv4.Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(ip, tt.want) {
t.Errorf("IPv4.Parse() = %v, want %v", ip, tt.want)
}
})
}
}
func TestIPv4_Build(t *testing.T) {
type fields struct {
netip.Addr
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{
{
name: "Build simple address",
fields: fields{netip.MustParseAddr("192.168.22.188")},
want: []byte("\xC0\xA8\x16\xBC"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ip := &IPv4{
Addr: tt.fields.Addr,
}
buf := new(bytes.Buffer)
if err := ip.Build(buf, NewDomains()); (err != nil) != tt.wantErr {
t.Errorf("IPv4.Build() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(buf.Bytes(), tt.want) {
t.Errorf("IPv4.Build() = %v, want %v", buf.Bytes(), tt.want)
}
})
}
}