forked from Juniper/go-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_test.go
175 lines (153 loc) · 4.27 KB
/
transport_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package netconf
import (
"bytes"
"encoding/xml"
"io"
"strings"
"testing"
)
// example RPC packets
const (
deviceHelloStr = `<!-- user bbennett, class j-super-user -->
<hello>
<capabilities>
<capability>urn:ietf:params:xml:ns:netconf:base:1.0</capability>
<capability>urn:ietf:params:xml:ns:netconf:capability:candidate:1.0</capability>
<capability>urn:ietf:params:xml:ns:netconf:capability:confirmed-commit:1.0</capability>
<capability>urn:ietf:params:xml:ns:netconf:capability:validate:1.0</capability>
<capability>urn:ietf:params:xml:ns:netconf:capability:url:1.0?protocol=http,ftp,file</capability>
<capability>http://xml.juniper.net/netconf/junos/1.0</capability>
<capability>http://xml.juniper.net/dmi/system/1.0</capability>
</capabilities>
<session-id>19313</session-id>
</hello>
]]>]]>`
loginStr = `SRX240 (ttyp2)
Password:
--- JUNOS 12.1X45-D15.5 built 2013-09-19 07:42:15 UTC
bbennett@SRX240>
`
waitForStringResponseStr = `SRX240 (ttyp2)`
)
// construct a test packet to test WaitFor<X> methods
func waitTestStr(input []byte) []byte {
// the message needs to be at least 4096 for some reason
prefix := make([]byte, 4096)
for i := 0; i < len(prefix); i++ {
prefix[i] = 'x'
}
prefix[4095] = '\n'
// add prefix to the actual information
return append(prefix, input...)
}
// bogus structs for testing purposes
type testTransport struct {
transportBase
}
type testReadCloser struct {
io.Reader
io.Writer
}
func (c *testReadCloser) Close() error {
return nil
}
func newTestTransport(input []byte) (*testTransport, *bytes.Buffer) {
var transport testTransport
r := bytes.NewReader(input)
w := new(bytes.Buffer)
transport.ReadWriteCloser = &testReadCloser{r, w}
return &transport, w
}
type transportTestItem struct {
Name string
TestFunc func(*TransportHelloMessage) interface{}
Expected interface{}
}
// verify that the hello messages being sent to the remote are formatted correctly
func TestSendHello(t *testing.T) {
// define tests
tests := []transportTestItem{
{
Name: "SessionID Nil",
TestFunc: func(h *TransportHelloMessage) interface{} {
return h.SessionID
},
Expected: 0,
},
{
Name: "Capability length",
TestFunc: func(h *TransportHelloMessage) interface{} {
return len(h.Capabilities)
},
Expected: 1,
},
{
Name: "Capability #0",
TestFunc: func(h *TransportHelloMessage) interface{} {
return h.Capabilities[0]
},
Expected: Capability("urn:ietf:params:xml:ns:netconf:base:1.0"),
},
}
transport, out := newTestTransport([]byte(""))
transport.SendHello(&TransportHelloMessage{Capabilities: []Capability{CapabilityNetconfBase}})
sent := out.String()
out.Reset()
hello := &TransportHelloMessage{}
if err := xml.Unmarshal([]byte(sent), hello); err != nil {
t.Fatal(err)
}
for _, item := range tests {
if result := item.TestFunc(hello); result != item.Expected {
t.Fatalf("unexpected test result: %v vs %v", result, item.Expected)
}
}
}
// test that correctly formatted RPC responses from the remote are unmarshalled correctly
func TestRecieveHello(t *testing.T) {
tests := []transportTestItem{
{
Name: "SessionID match",
TestFunc: func(h *TransportHelloMessage) interface{} {
return h.SessionID
},
Expected: 19313,
},
{
Name: "Capability length",
TestFunc: func(h *TransportHelloMessage) interface{} {
return len(h.Capabilities)
},
Expected: 7,
},
{
Name: "Capability #0",
TestFunc: func(h *TransportHelloMessage) interface{} {
return h.Capabilities[0]
},
Expected: Capability("urn:ietf:params:xml:ns:netconf:base:1.0"),
},
}
transport, _ := newTestTransport([]byte(deviceHelloStr))
hello, err := transport.ReceiveHello()
if err != nil {
t.Fatal(err)
}
for _, item := range tests {
if result := item.TestFunc(hello); result != item.Expected {
t.Fatalf("unexpected test result: %v vs %v", result, item.Expected)
}
}
}
// test transportBase's WaitForString function
func TestWaitForString(t *testing.T) {
transport, _ := newTestTransport(waitTestStr([]byte(loginStr)))
out, err := transport.WaitForString("Password:")
if err != nil {
t.Fatal(err)
}
expected := string(waitTestStr([]byte(waitForStringResponseStr)))
if strings.Trim(out, "x\n\t ") != strings.Trim(expected, "x\n\t ") {
t.Fatal("incorrect output")
}
}