snmp 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_walk.rb DELETED
@@ -1,193 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'snmp'
4
- require 'minitest/autorun'
5
- require 'yaml'
6
-
7
- include SNMP
8
-
9
- ##
10
- # Accept get and get-next requests, returning the data from the
11
- # provided YAML data file. The file contains a list of OID, value, and
12
- # value type.
13
- #
14
- class YamlDataTransport
15
- def self.load_data(yaml_file)
16
- values = YAML.load(File.new(yaml_file))
17
- @@get_map = {}
18
- @@get_next_map = {}
19
- values.each_index do |i|
20
- name, value, klass = values[i]
21
- if i < values.length - 1
22
- next_name, next_value, next_klass = values[i + 1]
23
- else
24
- next_value = SNMP::EndOfMibView
25
- next_klass = SNMP::EndOfMibView
26
- end
27
- @@get_map[name] = [name, value, klass]
28
- @@get_next_map[name] = [next_name, next_value, next_klass]
29
- end
30
- end
31
-
32
- def initialize
33
- @responses = []
34
- end
35
-
36
- def close
37
- end
38
-
39
- def send(data, host, port)
40
- msg = Message.decode(data)
41
- req_class = msg.pdu.class
42
- if req_class == SNMP::GetRequest
43
- oid_map = @@get_map
44
- elsif req_class == SNMP::GetNextRequest
45
- oid_map = @@get_next_map
46
- else
47
- raise "request not supported: " + req_class
48
- end
49
-
50
- resp = msg.response
51
- resp.pdu.vb_list.each do |vb|
52
- name, value, klass = oid_map[vb.name.to_s]
53
- vb.name = ObjectId.new(name)
54
- if klass == "SNMP::NoSuchObject" or klass == "SNMP::NoSuchInstance"
55
- vb.value = eval(klass)
56
- elsif value
57
- vb.value = eval("#{klass}.new(value)")
58
- else
59
- vb.value = SNMP::NoSuchInstance
60
- end
61
- end
62
- @responses << resp.encode
63
- end
64
-
65
- def recv(max_bytes)
66
- @responses.shift
67
- end
68
- end
69
-
70
- class TestTransport < Minitest::Test
71
-
72
- def test_get
73
- YamlDataTransport.load_data(File.dirname(__FILE__) + "/if_table6.yaml")
74
- SNMP::Manager.open(:Transport => YamlDataTransport.new) do |snmp|
75
- value = snmp.get_value("ifDescr.1")
76
- assert_equal("lo0", value)
77
- end
78
- end
79
-
80
- def test_get_next
81
- YamlDataTransport.load_data(File.dirname(__FILE__) + "/if_table6.yaml")
82
- SNMP::Manager.open(:Transport => YamlDataTransport.new) do |snmp|
83
- vb = snmp.get_next("ifDescr.1")
84
- assert_equal("gif0", vb.vb_list.first.value)
85
- end
86
- end
87
-
88
- end
89
-
90
- class TestWalk < Minitest::Test
91
-
92
- ##
93
- # A single string or single ObjectId can be passed to walk()
94
- #
95
- def test_single_object
96
- list = []
97
- ifTable6_manager.walk("ifDescr") do |vb|
98
- assert(vb.kind_of?(VarBind), "Expected a VarBind")
99
- list << vb
100
- end
101
- assert_equal(6, list.length)
102
-
103
- list = []
104
- ifTable6_manager.walk(ObjectId.new("1.3.6.1.2.1.2.2.1.2")) do |vb|
105
- assert(vb.kind_of?(VarBind), "Expected a VarBind")
106
- list << vb
107
- end
108
- assert_equal(6, list.length)
109
- end
110
-
111
-
112
- ##
113
- # If a list of one element is passed to walk() then a list of
114
- # one element is passed as the block parameter.
115
- #
116
- def test_single_object_list
117
- executed_block = false
118
- ifTable6_manager.walk(["1.3.6.1.2.1.2.2.1.2"]) do |vb_list|
119
- executed_block = true
120
- assert_equal(1, vb_list.length)
121
- assert_equal("IF-MIB::ifDescr.1", vb_list.first.name.to_s)
122
- break
123
- end
124
- assert(executed_block, "Did not execute block")
125
- end
126
-
127
- ##
128
- # If a list of multiple items are passed to walk() then
129
- # multiple items are passed to the block.
130
- #
131
- def test_object_list
132
- list1 = []
133
- list2 = []
134
- ifTable6_manager.walk(["ifIndex", "ifDescr"]) do |vb1, vb2|
135
- list1 << vb1
136
- list2 << vb2
137
- end
138
- assert_equal(6, list1.length)
139
- assert_equal(6, list2.length)
140
-
141
- list1 = []
142
- list2 = []
143
- ifTable6_manager.walk(["ifIndex", "ifDescr"], 1) do |vb1, vb2|
144
- list1 << vb1
145
- list2 << vb2
146
- end
147
- assert_equal(6, list1.length)
148
- assert_equal(6, list2.length)
149
- end
150
-
151
- def test_empty
152
- ifTable6_manager.walk("1.3.6.1.2.1.2.2.1.2.1") do |vb|
153
- fail("Expected block to not be executed")
154
- end
155
- end
156
-
157
- def test_one
158
- list = []
159
- ifTable1_manager.walk(["1.3.6.1.2.1.2.2.1.1", "1.3.6.1.2.1.2.2.1.2"]) do |vb|
160
- assert_equal("IF-MIB::ifIndex.1", vb[0].name.to_s)
161
- assert_equal("IF-MIB::ifDescr.1", vb[1].name.to_s)
162
- list << vb
163
- end
164
- assert_equal(1, list.length)
165
- end
166
-
167
- def test_hole_in_one
168
- list = []
169
- ifTable1_manager.walk(["ifIndex", "ifDescr", "ifType"]) do |vb|
170
- assert_equal("IF-MIB::ifIndex.1", vb[0].name.to_s)
171
- assert_equal(1, vb[0].value)
172
- assert_equal("IF-MIB::ifDescr.1", vb[1].name.to_s)
173
- assert_equal("lo0", vb[1].value)
174
- assert_equal("IF-MIB::ifType.1", vb[2].name.to_s)
175
- assert_equal(NoSuchInstance, vb[2].value)
176
- list << vb
177
- break
178
- end
179
- assert_equal(1, list.length)
180
- end
181
-
182
- private
183
-
184
- def ifTable1_manager
185
- YamlDataTransport.load_data(File.dirname(__FILE__) + "/if_table1.yaml")
186
- Manager.new(:Transport => YamlDataTransport.new)
187
- end
188
-
189
- def ifTable6_manager
190
- YamlDataTransport.load_data(File.dirname(__FILE__) + "/if_table6.yaml")
191
- Manager.new(:Transport => YamlDataTransport.new)
192
- end
193
- end