-
Notifications
You must be signed in to change notification settings - Fork 11
/
session_test.go
132 lines (107 loc) · 2.29 KB
/
session_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
package dns
import (
"context"
"net"
"testing"
)
func TestPacketSession(t *testing.T) {
t.Parallel()
srv := mustServer(localhostZone)
addr, err := net.ResolveTCPAddr("tcp", srv.Addr)
if err != nil {
t.Fatal(err)
}
conn, err := new(Transport).DialAddr(context.Background(), addr)
if err != nil {
t.Fatal(err)
}
ps := &packetSession{
session: session{
Conn: conn,
addr: addr,
client: new(Client),
msgerrc: make(chan msgerr),
},
}
msg := new(Message)
for i := 0; i < 120; i++ {
q := Question{
Name: "app.localhost.",
Type: TypeA,
Class: ClassIN,
}
msg.Questions = append(msg.Questions, q)
}
buf, err := msg.Pack(nil, true)
if _, err := ps.Write(buf); err != nil {
t.Fatal(err)
}
// test truncate due to short buffer size
if _, err := ps.Write(buf); err != nil {
t.Fatal(err)
}
buf = make([]byte, 100)
if _, err := ps.Read(buf); err != nil {
t.Fatal(err)
}
_, err = msg.Unpack(buf)
if want, got := errResourceLen, err; want != got {
t.Fatalf("want %v error, got %v", want, got)
}
if want, got := true, msg.Truncated; want != got {
t.Errorf("response message was not truncated")
}
}
func TestStreamSession(t *testing.T) {
t.Parallel()
srv := mustServer(localhostZone)
addr, err := net.ResolveTCPAddr("tcp", srv.Addr)
if err != nil {
t.Fatal(err)
}
conn, err := new(Transport).DialAddr(context.Background(), addr)
if err != nil {
t.Fatal(err)
}
ss := &streamSession{
session: session{
Conn: conn,
addr: addr,
client: new(Client),
msgerrc: make(chan msgerr),
},
}
msg := &Message{
Questions: []Question{
{
Name: "app.localhost.",
Type: TypeA,
Class: ClassIN,
},
},
}
buf, err := msg.Pack(nil, true)
if err != nil {
t.Fatal(err)
}
buf = append(make([]byte, 2), buf...)
nbo.PutUint16(buf[:2], uint16(len(buf)-2))
if _, err := ss.Write(buf); err != nil {
t.Fatal(err)
}
// test 2 byte length prefix read followed by msg read
if _, err := ss.Read(buf[:2]); err != nil {
t.Fatal(err)
}
mlen := nbo.Uint16(buf[:2])
buf = make([]byte, mlen)
if _, err := ss.Read(buf); err != nil {
t.Fatal(err)
}
if buf, err = msg.Unpack(buf); err != nil {
t.Fatal(err)
}
if want, got := 0, len(buf); want != got {
t.Errorf("want %d extra buffer bytes, got %d", want, got)
}
}