-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite_test.go
185 lines (157 loc) · 6.41 KB
/
write_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
176
177
178
179
180
181
182
183
184
185
package ipfix_test
import (
"bytes"
"fmt"
"net"
"time"
ipfix "github.com/CN-TU/go-ipfix"
)
func ExampleMakeMessageStream() {
// output of this example will be in buf
buf := new(bytes.Buffer)
// load the iana information elements
ipfix.LoadIANASpec()
now := time.Date(2018, 01, 01, 0, 0, 0, 0, time.UTC) // simulated fixed time
// First create a message stream; mtu=0 chooses the default size
msgStream, err := ipfix.MakeMessageStream(buf, 0, 0)
if err != nil {
fmt.Println("MakeMessageStream failed:", err)
return
}
// Add a new template with three information elements
a, err := ipfix.GetInformationElement("octetDeltaCount")
if err != nil {
fmt.Println("GetInformationElement failed:", err)
}
b, err := ipfix.GetInformationElement("sourceIPv4Address")
if err != nil {
fmt.Println("GetInformationElement failed:", err)
}
c, err := ipfix.GetInformationElement("flowEndNanoseconds")
if err != nil {
fmt.Println("GetInformationElement failed:", err)
}
id, err := msgStream.AddTemplate(now, a, b, c)
if err != nil {
fmt.Println("MessageStream.AddTemplate failed:", err)
return
}
// Export data for this information Element
if err := msgStream.SendData(now, id, uint64(5), net.IP{192, 168, 0, 1}, now); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
return
}
now = now.Add(1 * time.Second)
if err := msgStream.SendData(now, id, uint64(10), net.IP{192, 168, 0, 2}, now); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
return
}
now = now.Add(1 * time.Minute)
if err := msgStream.SendData(now.Add(10*time.Second), id, uint64(2), net.IP{192, 168, 0, 3}, now); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
return
}
// Call finalize
if err := msgStream.Flush(now); err != nil {
fmt.Println("MessageStream.Flush failed:", err)
return
}
// buf holds now the complete ipfix data of this example
fmt.Printf("% x", buf.Bytes())
// Output: 00 0a 00 64 5a 49 7a 3d 00 00 00 00 00 00 00 00 00 02 00 14 01 00 00 03 00 01 00 08 00 08 00 04 00 9d 00 08 01 00 00 40 00 00 00 00 00 00 00 05 c0 a8 00 01 dd f3 f8 80 00 00 00 01 00 00 00 00 00 00 00 0a c0 a8 00 02 dd f3 f8 81 00 00 00 01 00 00 00 00 00 00 00 02 c0 a8 00 03 dd f3 f8 bd 00 00 00 01
}
func ExampleMakeMessageStream_basicList() {
// output of this example will be in buf
buf := new(bytes.Buffer)
// load the iana information elements
ipfix.LoadIANASpec()
now := time.Date(2018, 01, 01, 0, 0, 0, 0, time.UTC) // simulated fixed time
// First create a message stream; mtu=0 chooses the default size
msgStream, err := ipfix.MakeMessageStream(buf, 0, 0)
if err != nil {
fmt.Println("MakeMessageStream failed:", err)
return
}
// Create an information elemenet for the basiclist, holding a variable number of octetDeltaCount
elem, err := ipfix.GetInformationElement("octetDeltaCount")
if err != nil {
fmt.Println("GetInformationElement failed:", err)
}
ie := ipfix.NewBasicList("testlist", elem, 0)
// Add a new template with this information element
id, err := msgStream.AddTemplate(now,
ie,
)
if err != nil {
fmt.Println("MessageStream.AddTemplate failed:", err)
return
}
// write out some data
// Note that despite octetDeltaCount being defined as uint64, you can use differnt types here
// The data will be converted to the right type; There is no automation to export data with shorter
// values
if err := msgStream.SendData(now, id, []uint64{1, 2, 3}); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
}
now = now.Add(1 * time.Second)
if err := msgStream.SendData(now, id, []uint8{4, 5}); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
}
now = now.Add(1 * time.Second)
if err := msgStream.SendData(now, id, []int32{10, 20, 33, 100}); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
}
now = now.Add(1 * time.Second)
if err := msgStream.Flush(now); err != nil {
fmt.Println("MessageStream.Flush failed:", err)
}
// buf holds now the complete ipfix data of this example
fmt.Printf("% x", buf.Bytes())
// Output: 00 0a 00 80 5a 49 7a 03 00 00 00 00 00 00 00 00 00 02 00 0c 01 00 00 01 01 23 ff ff 01 00 00 64 ff 00 1d ff 00 01 00 08 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 03 ff 00 15 ff 00 01 00 08 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 05 ff 00 25 ff 00 01 00 08 00 00 00 00 00 00 00 0a 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00 64
}
func ExampleMakeMessageStream_basicListVariable() {
// output of this example will be in buf
buf := new(bytes.Buffer)
// load the iana information elements
ipfix.LoadIANASpec()
now := time.Date(2018, 01, 01, 0, 0, 0, 0, time.UTC) // simulated fixed time
// First create a message stream; mtu=0 chooses the default size
msgStream, err := ipfix.MakeMessageStream(buf, 0, 0)
if err != nil {
fmt.Println("MakeMessageStream failed:", err)
return
}
// Create an information elemenet for the basiclist, holding a variable number of applicationName, which in turn are also variable length
elem, err := ipfix.GetInformationElement("applicationName")
if err != nil {
fmt.Println("GetInformationElement failed:", err)
}
ie := ipfix.NewBasicList("testlist", elem, 0)
// Add a new template with this information element
id, err := msgStream.AddTemplate(now,
ie,
)
if err != nil {
fmt.Println("MessageStream.AddTemplate failed:", err)
return
}
// write out some data
if err := msgStream.SendData(now, id, []string{"testA", "2", "testB"}); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
}
now = now.Add(1 * time.Second)
if err := msgStream.SendData(now, id, []string{"something longer"}); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
}
now = now.Add(1 * time.Second)
if err := msgStream.SendData(now, id, []string{"short", "test", "some", "more", "tests"}); err != nil {
fmt.Println("MessageStream.SendData failed:", err)
}
now = now.Add(1 * time.Second)
if err := msgStream.Flush(now); err != nil {
fmt.Println("MessageStream.Flush failed:", err)
}
// buf holds now the complete ipfix data of this example
fmt.Printf("% x", buf.Bytes())
// Output: 00 0a 00 72 5a 49 7a 03 00 00 00 00 00 00 00 00 00 02 00 0c 01 00 00 01 01 23 ff ff 01 00 00 56 ff 00 13 ff 00 60 ff ff 05 74 65 73 74 41 01 32 05 74 65 73 74 42 ff 00 16 ff 00 60 ff ff 10 73 6f 6d 65 74 68 69 6e 67 20 6c 6f 6e 67 65 72 ff 00 20 ff 00 60 ff ff 05 73 68 6f 72 74 04 74 65 73 74 04 73 6f 6d 65 04 6d 6f 72 65 05 74 65 73 74 73
}