/
gsnmpgo_test.go
184 lines (167 loc) · 4.75 KB
/
gsnmpgo_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
package gsnmpgo
// gsnmpgo is a go/cgo wrapper around gsnmp.
//
// Copyright (C) 2012-2013 Sonia Hamilton sonia@snowfrog.net.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import (
"fmt"
"github.com/petar/GoLLRB/llrb"
"strconv"
"testing"
)
var _ = fmt.Sprintf("dummy") // dummy
var _ = strconv.Itoa(0) // dummy
var oidAsStringTests = []struct {
in []int
out string
ok bool
}{
{[]int{1, 3, 6, 1, 4, 1, 2680, 1, 2, 7, 3, 2, 0}, ".1.3.6.1.4.1.2680.1.2.7.3.2.0", true},
{[]int{}, "", true},
}
func TestOidAsString(t *testing.T) {
for i, test := range oidAsStringTests {
ret := OidAsString(test.in)
if test.ok && ret != test.out {
t.Errorf("#%d: Bad result: %v (expected %v)", i, ret, test.out)
}
}
}
var lessOIDTests = []struct {
oid_a string
oid_b string
less bool
}{
{"1.2.3", "1.2.4", true},
{"1.2.3", "1.2.3.4", true},
{"1.2.3", "1.2", false},
{"", "1.2", true},
{"1.2", "", false},
{"", "", false},
{"1.9", "1.10", true},
{"1.12", "1.10", false},
{"1.12.2", "1.12.1", false},
{"1.12.1", "1.12.2", true},
}
func TestLessOID(t *testing.T) {
for i, test := range lessOIDTests {
astruct := QueryResult{Oid: test.oid_a}
bstruct := QueryResult{Oid: test.oid_b}
if res := LessOID(astruct, bstruct); res != test.less {
t.Errorf("#%d: expected (%t) got (%t) oid_a (%s) oid_b (%s)",
i, test.less, res, test.oid_a, test.oid_b)
}
}
}
var veraxDevices = []struct {
path string
port int
}{
{"testing/device/os/os-linux-std.txt", 161},
{"testing/device/cisco/cisco_router.txt", 162},
}
func TestQueryGets(t *testing.T) {
for i, test := range veraxDevices {
var err error
var vresults *llrb.Tree
if vresults, err = ReadVeraxResults(test.path); err != nil {
t.Errorf("#%d, %s: ReadVeraxResults error: %s", i, test.path, err)
}
var counter int
var uri, oids string
gresults := llrb.New(LessOID)
ch := vresults.IterAscend()
for {
r := <-ch
if r == nil {
break
}
counter += 1
if counter <= 3 { // TODO random number of oids, not hardcoded n
oid := r.(QueryResult)
oids += "," + oid.Oid
} else {
uri = `snmp://public@127.0.0.1:` + strconv.Itoa(test.port) + "//(" + oids[1:] + ")"
counter = 0 // reset
oids = "" // reset
params := &QueryParams{
Uri: uri,
Version: GNET_SNMP_V2C,
Timeout: 200,
Retries: 5,
Tree: gresults,
}
if _, err := Query(params); err != nil {
t.Errorf("Query error: %s. Uri: %s", err, uri)
}
}
}
CompareVerax(t, gresults, vresults)
}
}
var partitionAllPTests = []struct {
current_position int
partition_size int
slice_length int
ok bool
}{
{-1, 3, 8, false}, // test out of range
{8, 3, 8, false}, // test out of range
{0, 3, 8, false}, // test 0-7/3 per doco
{1, 3, 8, false},
{2, 3, 8, true},
{3, 3, 8, false},
{4, 3, 8, false},
{5, 3, 8, true},
{6, 3, 8, false},
{7, 3, 8, true},
{-1, 1, 3, false}, // partition size of one
{0, 1, 3, true},
{1, 1, 3, true},
{2, 1, 3, true},
{3, 1, 3, false},
}
func TestPartitionAllP(t *testing.T) {
for i, test := range partitionAllPTests {
ok := PartitionAllP(test.current_position, test.partition_size, test.slice_length)
if ok != test.ok {
t.Errorf("#%d: Bad result: %v (expected %v)", i, ok, test.ok)
}
}
}
// -----------------------------------------------------------------------------
var tests_uriCountMaxed = []struct {
path string
max int
ok bool
}{
{"", 3, true}, // malformed
{"((", 3, true}, // malformed
{"))", 3, true}, // malformed
{"())", 3, true}, // malformed
{"(,,)", 3, true}, // malformed
{"(,,,,)", 3, false}, // malformed
{"path://()", 3, true}, // zero
{"path://(1.2,1.3)", 3, true}, // less than
{"path://(1.2,1.3,1.4)", 3, true}, // equal
{"path://(1.2,1.3,1.4,1.5)", 3, false}, // greater than
}
func Test_uriCountMaxed(t *testing.T) {
for i, test := range tests_uriCountMaxed {
if err := uriCountMaxed(test.path, test.max); (err == nil) != test.ok {
t.Errorf("#%d: expected %t got |%v| max:%d path:%s", i, test.ok, err, test.max, test.path)
}
}
}