forked from nemith/netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
160 lines (133 loc) · 3.48 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
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
package netconf
import (
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
type testServer struct {
t *testing.T
in chan []byte
out chan []byte
}
func newTestServer(t *testing.T) *testServer {
return &testServer{
t: t,
in: make(chan []byte),
out: make(chan []byte),
}
}
func (s *testServer) handle(r io.ReadCloser, w io.WriteCloser) {
in, err := io.ReadAll(r)
if err != nil {
panic(fmt.Sprintf("testerver: failed to read incomming message: %v", err))
}
s.t.Logf("testserver recv: %s", in)
go func() { s.in <- in }()
out, ok := <-s.out
if !ok {
panic("testserver: no message to send")
}
s.t.Logf("tesserver send: %s", out)
_, err = w.Write(out)
if err != nil {
panic(fmt.Sprintf("testserver: failed to write message: %v", err))
}
if err := w.Close(); err != nil {
panic("tesserver: failed to close outbound message")
}
}
func (s *testServer) queueResp(p []byte) { go func() { s.out <- p }() }
func (s *testServer) queueRespString(str string) { s.queueResp([]byte(str)) }
func (s *testServer) popReq() ([]byte, error) {
msg, ok := <-s.in
if !ok {
return nil, fmt.Errorf("testserver: no message to read:")
}
return msg, nil
}
func (s *testServer) popReqString() (string, error) {
p, err := s.popReq()
return string(p), err
}
func (s *testServer) transport() *testTransport { return newTestTransport(s.handle) }
type testTransport struct {
handler func(r io.ReadCloser, w io.WriteCloser)
out chan io.ReadCloser
// msgReceived, msgSent int
}
func newTestTransport(handler func(r io.ReadCloser, w io.WriteCloser)) *testTransport {
return &testTransport{
handler: handler,
out: make(chan io.ReadCloser),
}
}
func (s *testTransport) MsgReader() (io.ReadCloser, error) {
return <-s.out, nil
}
func (s *testTransport) MsgWriter() (io.WriteCloser, error) {
inr, inw := io.Pipe()
outr, outw := io.Pipe()
go func() { s.out <- outr }()
go s.handler(inr, outw)
return inw, nil
}
func (s *testTransport) Close() error {
if len(s.out) > 0 {
return fmt.Errorf("testtransport: remaining outboard messages not sent at close")
}
return nil
}
const (
helloGood = `
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
<capability>urn:ietf:params:netconf:base:1.1</capability>
</capabilities>
<session-id>42</session-id>
</hello>`
helloBadXML = `
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities//>
</hello>`
helloNoSessID = `
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
<capability>urn:ietf:params:netconf:base:1.1</capability>
</capabilities>
</hello>`
helloNoCaps = `
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities></capabilities>
<session-id>42</session-id>
</hello>`
)
func TestHello(t *testing.T) {
tt := []struct {
name string
serverHello string
shouldError bool
wantID uint64
}{
{"good", helloGood, false, 42},
{"bad xml", helloBadXML, true, 0},
{"no capabilities", helloNoCaps, true, 0},
{"no session-id", helloNoSessID, true, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
ts := newTestServer(t)
sess := &Session{tr: ts.transport()}
ts.queueRespString(tc.serverHello)
err := sess.handshake()
if !tc.shouldError {
assert.NoError(t, err)
}
_, err = ts.popReqString()
assert.NoError(t, err)
assert.Equal(t, tc.wantID, sess.sessionID)
})
}
}