-
Notifications
You must be signed in to change notification settings - Fork 351
/
gosnmp_test.go
231 lines (206 loc) · 4.88 KB
/
gosnmp_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2012 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
package netsnmp
import (
"encoding/base64"
"flag"
"fmt"
"math"
"net"
"os"
"path/filepath"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
"github.com/gosnmp/gosnmp"
)
var (
rec = flag.Bool("rec", false, "record and use net-snmp expected outputs, else use recorded values [if true requires libsnmp/cgo]. ")
pcap = flag.String("pcap", "", "dir to put exp and got data as pcap files, no pcaps made if blank.")
)
func TestPDU(t *testing.T) {
flag.Parse()
if isPlayback() {
t.Log("playback mode enabled")
}
recdir := filepath.Join("testdata", t.Name())
if *rec && !isPlayback() {
if err := os.MkdirAll(recdir, 0755); err != nil {
t.Fatalf("error creating record dir: %s", err)
}
} else if *rec {
t.Fatal("record mode requires `-tags netsnmp` and libsnmpd installed")
}
pcapdir := ""
if *pcap != "" {
pcapdir = filepath.Join(*pcap, t.Name())
if err := os.MkdirAll(pcapdir, 0755); err != nil {
t.Fatalf("error creating pcap dir: %s", err)
}
}
tstpdus := []gosnmp.SnmpPDU{
{
Name: ".1.3.6.1.2.1.1.7.0",
Type: gosnmp.Integer,
Value: 104,
},
{
Name: ".1.3.6.1.2.1.2.2.1.10.1",
Type: gosnmp.Counter32,
Value: uint32(271070065),
},
{
Name: ".1.3.6.1.2.1.2.2.1.5.1",
Type: gosnmp.Gauge32,
Value: uint32(math.MaxUint32),
},
{
Name: ".1.3.6.1.2.1.1.4.0",
Type: gosnmp.OctetString,
Value: []byte("Administrator"),
},
{
Name: ".1.3.6.1.2.1.4.21.1.1.127.0.0.1",
Type: gosnmp.IPAddress,
Value: "127.0.0.1",
},
{
Name: ".1.3.6.1.4.1.6574.4.2.12.1.0",
Type: gosnmp.OpaqueFloat,
Value: float32(10.0),
},
{
Name: ".1.3.6.1.4.1.6574.4.2.12.1.1",
Type: gosnmp.OpaqueFloat,
Value: float32(0.0),
},
{
Name: ".1.3.6.1.4.1.6574.4.2.12.2.0",
Type: gosnmp.OpaqueDouble,
Value: float64(10.0),
},
{
Name: ".1.3.6.1.4.1.6574.4.2.12.2.1",
Type: gosnmp.OpaqueDouble,
Value: float64(0.0),
},
{
Name: ".1.3.6.1.2.1.2.2.1.10.1",
Type: gosnmp.Counter32,
Value: uint32(271070065),
},
{
Name: ".1.3.6.1.2.1.1.3.0",
Type: gosnmp.TimeTicks,
Value: uint32(318870100),
},
{
Name: ".1.3.6.1.2.1.1.3.1",
Type: gosnmp.Uinteger32,
Value: uint32(math.MaxInt32),
},
{
Name: ".1.3.6.1.2.1.1.3.2",
Type: gosnmp.Counter64,
Value: uint64(math.MaxInt64),
},
}
sess := gosnmp.Default
sess.Version = gosnmp.Version2c
for i, tstpdu := range tstpdus {
tname := fmt.Sprintf("test%d_PDU%s", i, tstpdu.Type.String())
t.Run(tname, func(t *testing.T) {
fname := filepath.Join(recdir, tname+"_pkt.b64")
pdus := []gosnmp.SnmpPDU{tstpdu}
pkt := sess.MkSnmpPacket(gosnmp.SetRequest, pdus, 0, 0)
pkt.RequestID++
exp, err := netSnmpPduPkt(fname, pdus[0], sess, pkt.RequestID, testing.Verbose())
if err != nil {
t.Fatal(err)
}
if *rec {
pktrec := base64.StdEncoding.EncodeToString(exp)
if err = os.WriteFile(fname, []byte(pktrec), 0600); err != nil {
t.Logf("error writing record file: %s", err)
}
}
got, err := pkt.MarshalMsg()
if err != nil {
t.Fatal(err)
}
if *pcap != "" {
savePcap(t, filepath.Join(pcapdir, tname), exp, got)
}
if diff := cmp.Diff(exp, got); diff != "" {
t.Fatalf("\ngot(%d): %q\nexp(%d): %q\ndiff:\n%s", len(got), got, len(exp), exp, diff)
}
})
}
}
func writePcap(fn string, payload []byte) error {
fn += ".pcap"
f, err := os.OpenFile(fn, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
l3 := &layers.IPv4{
SrcIP: net.ParseIP("192.168.2.1"),
DstIP: net.ParseIP("192.168.2.2"),
Protocol: layers.IPProtocolUDP,
Version: 4,
}
l4 := &layers.UDP{
SrcPort: 161,
DstPort: 161,
}
err = l4.SetNetworkLayerForChecksum(l3)
if err != nil {
return err
}
buf := gopacket.NewSerializeBuffer()
err = gopacket.SerializeLayers(
buf,
gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
},
l3,
l4,
gopacket.Payload(payload),
)
if err != nil {
return err
}
pkt := gopacket.NewPacket(buf.Bytes(),
layers.LinkTypeIPv4,
gopacket.Default)
w := pcapgo.NewWriter(f)
err = w.WriteFileHeader(1600, layers.LinkTypeIPv4)
if err != nil {
return err
}
err = w.WritePacket(gopacket.CaptureInfo{
Timestamp: time.Now(),
CaptureLength: len(pkt.Data()),
Length: len(pkt.Data()),
}, pkt.Data())
if err != nil {
return err
}
return nil
}
func savePcap(t *testing.T, fp string, exp, got []byte) {
err := writePcap(fp+"_exp", exp)
if err != nil {
t.Logf("error saving exp pcap: %s", err.Error())
}
err = writePcap(fp+"_got", got)
if err != nil {
t.Logf("error saving got pcap: %s", err.Error())
}
}