-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzz_binary_handlers_test.go
More file actions
104 lines (95 loc) · 2.25 KB
/
Copy pathzz_binary_handlers_test.go
File metadata and controls
104 lines (95 loc) · 2.25 KB
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
// SPDX-License-Identifier: AGPL-3.0-or-later
package server
import (
"net"
"testing"
"time"
)
// pipeConnPair returns a connected net.Conn pair so binary handlers
// can drain their decode error into a real conn.
func pipeConnPair(t *testing.T) (net.Conn, net.Conn) {
t.Helper()
a, b := net.Pipe()
t.Cleanup(func() {
_ = a.Close()
_ = b.Close()
})
return a, b
}
// TestServer_HandleBinaryHeartbeat_InvalidPayload drives the binary
// heartbeat dispatch shim — the directory handler will decode-error on
// a too-short payload and write an error frame back.
func TestServer_HandleBinaryHeartbeat_InvalidPayload(t *testing.T) {
t.Parallel()
s := newTestServer(t, "")
srv, cli := pipeConnPair(t)
doneCh := make(chan struct{})
go func() {
s.HandleBinaryHeartbeat(srv, []byte{0x01, 0x02}) // too short
close(doneCh)
}()
// Read whatever the handler emitted so the pipe doesn't block.
go func() {
buf := make([]byte, 256)
for {
if _, err := cli.Read(buf); err != nil {
return
}
}
}()
select {
case <-doneCh:
case <-time.After(2 * time.Second):
t.Fatal("HandleBinaryHeartbeat blocked")
}
}
// TestServer_HandleBinaryLookup_InvalidPayload exercises the binary
// lookup decode-error path.
func TestServer_HandleBinaryLookup_InvalidPayload(t *testing.T) {
t.Parallel()
s := newTestServer(t, "")
srv, cli := pipeConnPair(t)
doneCh := make(chan struct{})
go func() {
s.HandleBinaryLookup(srv, []byte{0x00}, "127.0.0.1:1")
close(doneCh)
}()
go func() {
buf := make([]byte, 256)
for {
if _, err := cli.Read(buf); err != nil {
return
}
}
}()
select {
case <-doneCh:
case <-time.After(2 * time.Second):
t.Fatal("HandleBinaryLookup blocked")
}
}
// TestServer_HandleBinaryResolve_InvalidPayload covers the resolve
// decode-error path.
func TestServer_HandleBinaryResolve_InvalidPayload(t *testing.T) {
t.Parallel()
s := newTestServer(t, "")
srv, cli := pipeConnPair(t)
doneCh := make(chan struct{})
go func() {
s.HandleBinaryResolve(srv, []byte{0x00}, "127.0.0.1:1")
close(doneCh)
}()
go func() {
buf := make([]byte, 256)
for {
if _, err := cli.Read(buf); err != nil {
return
}
}
}()
select {
case <-doneCh:
case <-time.After(2 * time.Second):
t.Fatal("HandleBinaryResolve blocked")
}
}