-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgs_test.go
151 lines (133 loc) · 4.45 KB
/
msgs_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
package netconf
import (
"bytes"
"flag"
"strings"
"testing"
"time"
"github.com/freeconf/yang/fc"
"github.com/freeconf/yang/nodeutil"
"github.com/freeconf/yang/patch/xml"
)
var updateFlag = flag.Bool("update", false, "update golden files instead of verifying against them")
func TestMsg(t *testing.T) {
msg := &Msg{
XMLName: xml.Name{Local: "x", Space: "Y"},
Elems: []*Msg{
{
XMLName: xml.Name{Local: "y"},
},
},
}
var buf bytes.Buffer
fc.AssertEqual(t, nil, WriteResponseWithOptions(msg, &buf, false, false))
fc.AssertEqual(t, `<x xmlns="Y"><y></y></x>`, buf.String())
}
func TestGetConfig(t *testing.T) {
payload := `
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<get-config>
<source><running/></source>
<filter type="subtree">
<top xmlns="http://example.com/schema/1.2/config">
<users />
</top>
</filter>
</get-config>
</rpc>`
// read/decode
msg, err := DecodeRequest(strings.NewReader(payload))
fc.RequireEqual(t, nil, err)
fc.RequireEqual(t, true, msg.Rpc != nil)
fc.RequireEqual(t, true, msg.Rpc.GetConfig != nil)
fc.AssertEqual(t, "101", msg.Rpc.MessageId)
fc.AssertEqual(t, true, msg.Rpc.GetConfig.Filter != nil)
fc.AssertEqual(t, "subtree", msg.Rpc.GetConfig.Filter.Type)
fc.AssertEqual(t, 1, len(msg.Rpc.GetConfig.Filter.Elems))
// only nec. when trying to write back what we read. RFC explicitly states
// the whitespace might matter.
CleanWhitespace(msg.Rpc.GetConfig.Source.Elems...)
CleanWhitespace(msg.Rpc.GetConfig.Filter.Elems...)
// write/encode
var buf bytes.Buffer
fc.AssertEqual(t, nil, WriteResponseWithOptions(msg.Rpc, &buf, false, true))
fc.Gold(t, *updateFlag, buf.Bytes(), "testdata/gold/get-config.xml")
}
func CleanWhitespace(elems ...*Msg) {
for _, x := range elems {
x.Content = strings.TrimSpace(x.Content)
x.XMLName.Space = ""
CleanWhitespace(x.Elems...)
}
}
func TestAction(t *testing.T) {
payload := `
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<rock-the-house xmlns="urn:example:rock">
<zip-code>27606-0100</zip-code>
</rock-the-house>
</rpc>`
// read/decode
msg, err := DecodeRequest(strings.NewReader(payload))
fc.RequireEqual(t, nil, err)
fc.RequireEqual(t, true, msg.Rpc != nil)
fc.RequireEqual(t, true, msg.Rpc.Action != nil)
fc.AssertEqual(t, "rock-the-house", msg.Rpc.Action.XMLName.Local)
}
func TestHello(t *testing.T) {
payload := `
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.1</capability>
<capability>urn:ietf:params:netconf:capability:startup:1.0</capability>
<capability>http://example.net/router/2.3/myfeature</capability>
</capabilities>
<session-id>4</session-id>
</hello>`
// read/decode
msg, err := DecodeRequest(strings.NewReader(payload))
fc.RequireEqual(t, nil, err)
fc.RequireEqual(t, true, msg.Hello != nil)
fc.RequireEqual(t, "4", msg.Hello.SessionId)
fc.RequireEqual(t, 3, len(msg.Hello.Capabilities))
fc.RequireEqual(t, "http://example.net/router/2.3/myfeature", strings.TrimSpace(string(msg.Hello.Capabilities[2].Content)))
// write/encode
var buf bytes.Buffer
fc.AssertEqual(t, nil, WriteResponseWithOptions(msg.Hello, &buf, false, true))
fc.Gold(t, *updateFlag, buf.Bytes(), "testdata/gold/hello.xml")
}
func TestCreateSupscription(t *testing.T) {
payload := `
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<create-subscription xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
<stream>car:update</stream>
</create-subscription>
</rpc>`
// read/decode
msg, err := DecodeRequest(strings.NewReader(payload))
fc.RequireEqual(t, nil, err)
fc.RequireEqual(t, true, msg.Rpc != nil)
fc.RequireEqual(t, true, msg.Rpc.CreateSubscription != nil)
fc.AssertEqual(t, "car:update", msg.Rpc.CreateSubscription.Stream)
// write/encode
var buf bytes.Buffer
fc.AssertEqual(t, nil, WriteResponseWithOptions(msg.Rpc, &buf, false, true))
fc.Gold(t, *updateFlag, buf.Bytes(), "testdata/gold/create-subscription.xml")
}
func TestNotification(t *testing.T) {
// any fixed date will do
t0, _ := time.Parse("2006-01-02T15:04:05Z", "2014-01-01T12:00:00Z")
resp := Notification{
EventTime: t0,
Elems: []*nodeutil.XMLWtr2{
{
XMLName: xml.Name{Space: "x", Local: "y"},
Content: "hello",
},
},
}
var actual bytes.Buffer
err := WriteResponse(resp, &actual)
fc.AssertEqual(t, nil, err)
fc.Gold(t, *updateFlag, actual.Bytes(), "testdata/gold/notification.xml")
}