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_pdu.rb DELETED
@@ -1,201 +0,0 @@
1
- # encoding: ascii-8bit
2
- # frozen_string_literal: true
3
-
4
- require 'snmp/pdu'
5
- require 'minitest/autorun'
6
-
7
- class TestProtocol < Minitest::Test
8
-
9
- include SNMP
10
-
11
- def test_message_decode_v1
12
- message = SNMP::Message.decode("0'\002\001\000\004\006public\240\032\002\002\003\350\002\001\000\002\001\0000\0160\f\006\010+\006\001\002\001\001\001\000\005\000")
13
- assert_equal(:SNMPv1, message.version)
14
- assert_equal("public", message.community)
15
- assert_equal(SNMP::GetRequest, message.pdu.class)
16
- varbind_list = message.pdu.vb_list;
17
- assert_equal(1, varbind_list.length)
18
- assert_equal([1,3,6,1,2,1,1,1,0], varbind_list.first.name)
19
- assert_equal(SNMP::Null, varbind_list.first.value)
20
- end
21
-
22
- def test_message_decode_v2c
23
- message = SNMP::Message.decode("0)\002\001\001\004\006public\240\034\002\0040\265\020\202\002\001\000\002\001\0000\0160\f\006\010+\006\001\002\001\001\001\000\005\000")
24
- assert_equal(:SNMPv2c, message.version)
25
- assert_equal("public", message.community)
26
- varbind_list = message.pdu.vb_list;
27
- assert_equal(1, varbind_list.length)
28
- assert_equal([1,3,6,1,2,1,1,1,0], varbind_list.first.name)
29
- assert_equal(SNMP::Null, varbind_list.first.value)
30
- end
31
-
32
- def test_message_decoder_v3
33
- assert_raises(SNMP::UnsupportedVersion) do
34
- SNMP::Message.decode("0>\002\001\0030\021\002\004&\266\342\314\002\003\000\377\343\004\001\004\002\001\003\004\0200\016\004\000\002\001\000\002\001\000\004\000\004\000\004\0000\024\004\000\004\000\240\016\002\004\v\3623\233\002\001\000\002\001\0000\000")
35
- end
36
- end
37
-
38
- def test_encode_message
39
- varbind = SNMP::VarBind.new([1,3,6,1234], SNMP::OctetString.new("value"))
40
- list = SNMP::VarBindList.new
41
- list << varbind << varbind;
42
- pdu = SNMP::Response.new(12345, list)
43
- message = SNMP::Message.new(:SNMPv2c, "public", pdu)
44
- assert_equal("07\002\001\001\004\006public\242*\002\00209\002\001\000\002\001\0000\0360\r\006\004+\006\211R\004\005value0\r\006\004+\006\211R\004\005value", message.encode)
45
- end
46
-
47
- def test_get_request_from_single_string
48
- request = SNMP::GetRequest.new(42, VarBindList.new(["1.3.6.1"]))
49
- assert_equal(42, request.request_id)
50
- assert_equal(1, request.varbind_list.length)
51
- assert_equal([1,3,6,1], request.varbind_list.first.name)
52
- assert_equal(SNMP::Null, request.varbind_list.first.value)
53
- end
54
-
55
- def test_get_request_from_multi_string
56
- request = SNMP::GetRequest.new(42, VarBindList.new(["1.3.6.1", "1.3.6.2"]))
57
- assert_equal(2, request.varbind_list.length)
58
- assert_equal([1,3,6,1], request.varbind_list.first.name)
59
- end
60
-
61
- def test_get_request_from_varbind
62
- request = GetRequest.new(42, VarBindList.new(VarBind.new([1,3,6,1], Null)))
63
- assert_equal(1, request.varbind_list.length)
64
- assert_equal([1,3,6,1], request.varbind_list.first.name)
65
- end
66
-
67
- def test_get_next_from_single_string
68
- request = SNMP::GetNextRequest.new(42, VarBindList.new("1.3.5.1"))
69
- assert_equal(42, request.request_id)
70
- assert_equal(1, request.varbind_list.length)
71
- assert_equal([1,3,5,1], request.varbind_list.first.name)
72
- end
73
-
74
- def test_get_next_from_single_object_id
75
- request = SNMP::GetNextRequest.new(42, VarBindList.new([ObjectId.new("1.3.5.1")]))
76
- assert_equal(42, request.request_id)
77
- assert_equal(1, request.varbind_list.length)
78
- assert_equal([1,3,5,1], request.varbind_list.first.name)
79
- end
80
-
81
- def test_each_varbind
82
- request = SNMP::GetRequest.new(42, VarBindList.new(["1.3.6.1", "1.3.6.2"]))
83
- count = 0
84
- request.each_varbind do |v|
85
- case count
86
- when 0
87
- assert_equal(ObjectName.new("1.3.6.1"), v.name)
88
- when 1
89
- assert_equal(ObjectName.new("1.3.6.2"), v.name)
90
- else
91
- fail "Unexpected count"
92
- end
93
- count +=1
94
- end
95
- end
96
-
97
- def test_get_bulk_create
98
- request = SNMP::GetBulkRequest.new(1234, VarBindList.new("1.3.6.2"), 20, 10)
99
- assert_equal(1234, request.request_id)
100
- assert_equal(20, request.non_repeaters)
101
- assert_equal(10, request.max_repetitions)
102
- assert_equal(1, request.varbind_list.length)
103
- assert_equal("1.3.6.2", request.varbind_list.first.name.to_s)
104
- end
105
-
106
- def test_get_bulk_encode
107
- request = SNMP::GetBulkRequest.new(1234, VarBindList.new, 0, 10)
108
- assert_equal("\245\f\002\002\004\322\002\001\000\002\001\n0\000", request.encode)
109
- end
110
-
111
- def test_error_status
112
- request = GetRequest.new(42, VarBindList.new("1.3.12.23.4"))
113
- assert_equal(:noError, request.error_status)
114
-
115
- request.error_status = :noCreation
116
- assert_equal(:noCreation, request.error_status)
117
-
118
- request.error_status = 2
119
- assert_equal(:noSuchName, request.error_status)
120
-
121
- request.error_status = 42
122
- assert_equal(42, request.error_status)
123
-
124
- assert_raises(InvalidErrorStatus) {request.error_status = "myErrorString"}
125
-
126
- assert_raises(InvalidErrorStatus) {request.error_status = :myErrorSymbol}
127
- end
128
-
129
- def test_snmpv2_trap
130
- sys_up_varbind = VarBind.new(ObjectId.new("1.3.6.1.2.1.1.3.0"),
131
- TimeTicks.new(1234))
132
- trap_oid_varbind = VarBind.new(ObjectId.new("1.3.6.1.6.3.1.1.4.1.0"),
133
- ObjectId.new("1.2.3"))
134
- trap = SNMPv2_Trap.new(42, VarBindList.new([sys_up_varbind, trap_oid_varbind]))
135
- assert_equal("\247-\002\001*\002\001\000\002\001\0000\"0\016\006\010+\006\001\002\001\001\003\000C\002\004\3220\020\006\n+\006\001\006\003\001\001\004\001\000\006\002*\003", trap.encode)
136
- assert_equal(1234, trap.sys_up_time.to_i)
137
- assert_equal("1.2.3", trap.trap_oid.to_s)
138
- end
139
-
140
- def test_snmpv2_invalid_trap
141
- trap = SNMPv2_Trap.new(42, VarBindList.new([]))
142
- assert_raises(InvalidTrapVarbind) { trap.sys_up_time }
143
- assert_raises(InvalidTrapVarbind) { trap.trap_oid }
144
- end
145
-
146
- def test_snmpv1_generic_trap
147
- trap = SNMPv1_Trap.new(nil, nil, 0, nil, nil, nil)
148
- assert_equal(:coldStart, trap.generic_trap)
149
-
150
- trap.generic_trap = :warmStart
151
- assert_equal(:warmStart, trap.generic_trap)
152
-
153
- trap.generic_trap = 6
154
- assert_equal(:enterpriseSpecific, trap.generic_trap)
155
-
156
- assert_raises(InvalidGenericTrap) { trap.generic_trap = -1 }
157
- assert_raises(InvalidGenericTrap) { trap.generic_trap = 7 }
158
- end
159
-
160
- def test_snmpv1_trap_encode
161
- enterprise = ObjectId.new("1.3.6.1.123")
162
- agent_addr = IpAddress.new("1.2.3.4")
163
- generic_trap = :linkDown
164
- specific_trap = 0
165
- timestamp = TimeTicks.new(2176117721)
166
- varbinds = VarBindList.new([VarBind.new("1.3.6.2", SNMP::Integer.new(1))])
167
- trap = SNMPv1_Trap.new(enterprise, agent_addr, generic_trap, specific_trap, timestamp, varbinds)
168
- assert_equal("\244%\006\004+\006\001{@\004\001\002\003\004\002\001\002\002\001\000C\005\000\201\264\353\3310\n0\010\006\003+\006\002\002\001\001", trap.encode)
169
-
170
- encoded = Message.new(:SNMPv1, "public", trap).encode
171
- trap = Message.decode(encoded).pdu
172
- assert_equal(enterprise, trap.enterprise)
173
- assert_equal(agent_addr, trap.agent_addr)
174
- assert_equal(:linkDown, trap.generic_trap)
175
- assert_equal(0, trap.specific_trap)
176
- assert_equal(2176117721, trap.timestamp.to_i)
177
- assert_equal(1, trap.varbind_list.length)
178
- assert_equal(ObjectId.new("1.3.6.2"), trap.varbind_list.first.name)
179
- end
180
-
181
- def test_response_pdu
182
- pdu = Response.new(2147483647, VarBindList.new, :noError, 0)
183
- assert_equal("\242\016\002\004\177\377\377\377\002\001\000\002\001\0000\000", pdu.encode)
184
-
185
- encoded = Message.new(:SNMPv2c, "public", pdu).encode
186
- pdu = Message.decode(encoded).pdu
187
- assert_equal(2147483647, pdu.request_id)
188
- assert_equal(:noError, pdu.error_status)
189
- assert_equal(0, pdu.error_index)
190
- assert_equal(0, pdu.varbind_list.length)
191
- end
192
-
193
- def test_response_pdu_unknown_error
194
- pdu = Response.new(2147483647, VarBindList.new, 6883501, 0)
195
- assert_equal("\xA2\x10\x02\x04\x7F\xFF\xFF\xFF\x02\x03\x69\x08\xAD\x02\x01\x00\x30\x00", pdu.encode)
196
-
197
- encoded = Message.new(:SNMPv2c, "public", pdu).encode
198
- pdu = Message.decode(encoded).pdu
199
- assert_equal(6883501, pdu.error_status)
200
- end
201
- end
data/test/test_retry.rb DELETED
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
- require 'snmp'
5
-
6
- class TimeoutManager < SNMP::Manager
7
- attr_accessor :response_count
8
-
9
- def initialize(config)
10
- super(config)
11
- @response_count = 0
12
- @raise_on_send = false
13
- @raise_on_get = false
14
- end
15
-
16
- def raise_send_error
17
- @raise_on_send = true
18
- end
19
-
20
- def raise_get_error
21
- @raise_on_get = true
22
- end
23
-
24
- def send_request(request, community, host, port)
25
- raise RuntimeError if @raise_on_send
26
- end
27
-
28
- def get_response(request)
29
- @response_count += 1
30
- raise RuntimeError if @raise_on_get
31
- raise Timeout::Error, "testing retry count"
32
- end
33
- end
34
-
35
- class MismatchIdTransport
36
- def initialize
37
- @data = []
38
- end
39
-
40
- def close
41
- end
42
-
43
- def send(data, host, port)
44
- bad_id_data = data.dup
45
- bad_msg = SNMP::Message.decode(bad_id_data)
46
- bad_msg.pdu.request_id -= 3 # corrupt request_id
47
- @data << bad_msg.encode # insert corrupted PDU before real data
48
- @data << bad_id_data
49
- end
50
-
51
- def recv(max_bytes)
52
- raise "receive queue is empty" unless @data.first
53
- SNMP::Message.decode(@data.shift).response.encode[0,max_bytes]
54
- end
55
- end
56
-
57
-
58
- class TestRetry < Minitest::Test
59
- def test_retry_count
60
- assert_response_count(0, 1, SNMP::RequestTimeout)
61
- assert_response_count(1, 2, SNMP::RequestTimeout)
62
- assert_response_count(5, 6, SNMP::RequestTimeout)
63
- end
64
-
65
- def test_retry_on_error
66
- assert_response_count(5, 0, RuntimeError) { |m| m.raise_send_error }
67
- old_verbose = $VERBOSE
68
- $VERBOSE = nil
69
- assert_response_count(5, 6, SNMP::RequestTimeout) { |m| m.raise_get_error }
70
- $VERBOSE = old_verbose
71
- end
72
-
73
- def assert_response_count(retry_count, response_count, exception)
74
- m = TimeoutManager.new( :Retries => retry_count )
75
- yield m if block_given?
76
- assert_raises(exception) { m.get("1.2.3.4") }
77
- assert_equal(response_count, m.response_count)
78
- end
79
-
80
- def test_drop_mismatched_id
81
- m = SNMP::Manager.new(:Transport => MismatchIdTransport.new)
82
- m.get("1.2.3.4")
83
- end
84
- end
data/test/test_smi.rb DELETED
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
-
5
- begin
6
- require 'smi'
7
-
8
- class TestSmi < MiniTest::Unit::TestCase
9
-
10
- include SNMP::SMI
11
-
12
- def test_load
13
- name, oid_hash = load_smi_module('test/mibs/IF-MIB')
14
- assert_equal("IF-MIB", name)
15
- assert_equal("1.3.6.1.2.1.2.2", oid_hash["ifTable"])
16
- end
17
-
18
- end
19
- rescue LoadError
20
- # snmp/smi may not always be available because it is a C extention
21
- end
data/test/test_varbind.rb DELETED
@@ -1,383 +0,0 @@
1
- # encoding: ascii-8bit
2
- # frozen_string_literal: true
3
-
4
- require 'snmp/varbind'
5
- require 'minitest/autorun'
6
-
7
- class TestVarBind < Minitest::Test
8
-
9
- include SNMP
10
-
11
- def test_varbind_encode
12
- v = VarBind.new([1,3,6,1], OctetString.new("test"))
13
- assert_equal("0\v\006\003+\006\001\004\004test", v.encode)
14
- refute_nil(v.asn1_type)
15
- assert_equal("[name=1.3.6.1, value=test (OCTET STRING)]", v.to_s)
16
- end
17
-
18
- def test_varbind_decode
19
- varbind, remainder = VarBind.decode("0\f\006\010+\006\001\002\001\001\001\000\005\000")
20
- assert_equal(Null, varbind.value)
21
- assert_equal("", remainder)
22
-
23
- varbind, remainder = VarBind.decode("0\f\006\010+\006\001\002\001\001\001\000\005\0000\f\006\010+\006\001\002\001\001\002\000\005\000")
24
- assert_equal(Null, varbind.value)
25
- assert_equal("0\f\006\010+\006\001\002\001\001\002\000\005\000", remainder)
26
- end
27
-
28
- def test_varbind_to_s
29
- mib = MIB.new
30
- mib.load_module("IF-MIB")
31
-
32
- vb = VarBind.new("1.3.6.1.2.1.2.2.1.2.1.1", OctetString.new("description")).with_mib(mib)
33
- assert_equal "[name=IF-MIB::ifDescr.1.1, value=description (OCTET STRING)]", vb.to_s
34
-
35
- vb = VarBind.new("1.3.6.1.2.1.2.2.1.2.1.1", ObjectId.new("1.3.6.1.2.1.2.2.1.2.1.1")).with_mib(mib)
36
- assert_equal "[name=IF-MIB::ifDescr.1.1, value=IF-MIB::ifDescr.1.1 (OBJECT IDENTIFIER)]", vb.to_s
37
- end
38
-
39
- def test_varbind_name_alias_oid
40
- vb = VarBind.new("1.2.3.4", OctetString.new("blah"))
41
- assert_equal(ObjectId.new("1.2.3.4"), vb.name)
42
- assert_equal(ObjectId.new("1.2.3.4"), vb.oid)
43
- end
44
-
45
- def test_varbind_list_create
46
- list = VarBindList.new
47
- assert_equal(0, list.length)
48
-
49
- check_varbind_list_create(VarBindList.new(["1.2.3.4.5"]))
50
- check_varbind_list_create(VarBindList.new([ObjectId.new("1.2.3.4.5")]))
51
- check_varbind_list_create(VarBindList.new([VarBind.new("1.2.3.4.5", Null)]))
52
-
53
- check_varbind_list_create(VarBindList.new(
54
- ["1.2.3.4.5", "1.2.3.4.6"]), 2)
55
-
56
- check_varbind_list_create(VarBindList.new(
57
- [ObjectId.new("1.2.3.4.5"),
58
- ObjectId.new("1.2.3.4.6")]
59
- ), 2)
60
-
61
- check_varbind_list_create(VarBindList.new(
62
- [VarBind.new("1.2.3.4.5", Null),
63
- VarBind.new("1.2.3.4.6", SNMP::Integer.new(123)),
64
- ObjectId.new("1.2.3.4.7")]
65
- ), 3)
66
-
67
- list = VarBindList.new([VarBind.new("1.3.6.2", SNMP::Integer.new(1))])
68
- assert_equal(1, list.length)
69
- assert_equal("1.3.6.2", list.first.name.to_s)
70
- assert_equal(1, list.first.value.to_i)
71
- end
72
-
73
- def check_varbind_list_create(list, n=1)
74
- assert_equal(n, list.length)
75
- assert_equal("1.2.3.4.5", list.first.name.to_s)
76
- assert_equal(Null, list.first.value)
77
- end
78
-
79
- def test_varbind_list_encode
80
- list = VarBindList.new
81
- assert_equal("0\000", list.encode)
82
- refute_nil(list.asn1_type)
83
-
84
- list << VarBind.new([1,3,6,1], OctetString.new("test"))
85
- assert_equal("0\r0\v\006\003+\006\001\004\004test", list.encode)
86
-
87
- list << VarBind.new([1,3,6,1], OctetString.new("blah"))
88
- assert_equal("0\0320\v\006\003+\006\001\004\004test0\v\006\003+\006\001\004\004blah", list.encode)
89
- end
90
-
91
- def test_varbind_list_decode
92
- list, remainder = VarBindList.decode("0\r0\v\006\003+\006\001\004\004test")
93
- assert_equal(1, list.length)
94
- assert_equal("", remainder)
95
-
96
- list, remainder = VarBindList.decode("0\0320\v\006\003+\006\001\004\004test0\v\006\003+\006\001\004\004blah")
97
- assert_equal(2, list.length)
98
- assert_equal("", remainder)
99
-
100
- list, remainder = VarBindList.decode("0\000")
101
- assert_equal(0, list.length)
102
- assert_equal("", remainder)
103
- end
104
-
105
- def test_octet_string
106
- string = OctetString.new("test")
107
- assert_equal("test", string.to_s)
108
- assert_equal("\004\004test", string.encode)
109
- refute_nil(string.asn1_type)
110
- end
111
-
112
- def test_octet_string_equals
113
- s1 = OctetString.new("test")
114
- s2 = "test"
115
- s3 = OctetString.new("test")
116
- assert_equal(s1, s2)
117
- assert(s1 == s2)
118
- refute_same(s1, s3)
119
- assert_equal(s1, s3)
120
- end
121
-
122
- def test_octet_string_to_oid
123
- s = OctetString.new("test")
124
- assert_equal(ObjectId.new([116, 101, 115, 116]), s.to_oid)
125
- end
126
-
127
- def test_object_id
128
- id = ObjectId.new([1,3,6,1])
129
- assert_equal("1.3.6.1", id.to_s)
130
- assert_equal("\006\003+\006\001", id.encode)
131
- refute_nil(id.asn1_type)
132
- assert_equal("1.3.6.1", id.to_varbind.name.to_s)
133
-
134
- assert_raises(ArgumentError) {
135
- ObjectId.new("xyzzy")
136
- }
137
-
138
- assert_equal("", ObjectId.new.to_s)
139
- end
140
-
141
- def test_object_id_to_s_with_mib
142
- mib = MIB.new
143
- mib.load_module("IF-MIB")
144
- id = ObjectId.new("1.3.6.1.2.1.2.2.1.2.1.1", mib)
145
- assert_equal("IF-MIB::ifDescr.1.1", id.to_s)
146
- assert_equal("1.3.6.1.2.1.2.2.1.2.1.1", id.to_str)
147
- assert_equal("[1.3.6.1.2.1.2.2.1.2.1.1]", id.inspect)
148
- end
149
-
150
- def test_object_id_create
151
- assert_equal("1.3.6.1", ObjectId.new("1.3.6.1").to_s)
152
- assert_equal("1.3.6.1", ObjectId.new([1,3,6,1]).to_s)
153
- assert_equal("1.3.6.1", ObjectId.new(ObjectId.new("1.3.6.1")).to_s)
154
- end
155
-
156
- def test_object_id_equals
157
- id1 = ObjectId.new("1.3.3.4")
158
- id2 = ObjectId.new([1,3,3,4])
159
- refute_same(id1, id2)
160
- assert(id1 == id2)
161
- assert_equal(id1, id2)
162
- end
163
-
164
- def test_object_id_comparable
165
- id1 = ObjectId.new("1.3.3.4")
166
- id2 = ObjectId.new("1.3.3.4.1")
167
- id3 = ObjectId.new("1.3.3.4.5")
168
- id4 = ObjectId.new("1.3.3.5")
169
- assert(id1 < id2)
170
- assert(id2 > id1)
171
- assert(id2 < id3)
172
- assert(id3 > id2)
173
- assert(id3 < id4)
174
- assert(id4 > id3)
175
- end
176
-
177
- def test_object_id_subtree
178
- id1 = ObjectId.new("1.3.3.4")
179
- id2 = ObjectId.new("1.3.3.4.1")
180
- id3 = ObjectId.new("1.3.3.4.5")
181
- id4 = ObjectId.new("1.3.3.5")
182
- assert(id2.subtree_of?(id1))
183
- assert(id3.subtree_of?(id1))
184
- assert(!id3.subtree_of?(id2))
185
- assert(!id1.subtree_of?(id2))
186
- assert(!id4.subtree_of?(id1))
187
- assert(!id4.subtree_of?(id3))
188
- assert(id1.subtree_of?("1.3.3.4"))
189
- assert(!id4.subtree_of?("1.3.3.4"))
190
- end
191
-
192
- def test_object_id_index
193
- id1 = ObjectId.new("1.3.3.4")
194
- id2 = ObjectId.new("1.3.3.4.1")
195
- id3 = ObjectId.new("1.3.3.4.1.2")
196
- assert_equal(ObjectId.new("1"), id2.index(id1))
197
- assert_equal(ObjectId.new("1.2"), id3.index(id1))
198
- assert_equal(ObjectId.new("1.2"), id3.index("1.3.3.4"))
199
- assert_raises(ArgumentError) { id1.index(id3) }
200
- assert_raises(ArgumentError) { id1.index(id1) }
201
- end
202
-
203
- def test_object_name_from_string
204
- id = ObjectName.new("1.3.4.5.6")
205
- assert_equal("1.3.4.5.6", id.to_s)
206
- assert_equal("\006\004+\004\005\006", id.encode)
207
- end
208
-
209
- def test_integer_create
210
- i = SNMP::Integer.new(12345)
211
- assert_equal("12345", i.to_s)
212
- assert_equal(12345, i.to_i)
213
- assert_equal("\002\00209", i.encode)
214
- refute_nil(i.asn1_type)
215
- end
216
-
217
- def test_integer_create_from_string
218
- i = SNMP::Integer.new("12345")
219
- assert_equal(12345, i.to_i)
220
- end
221
-
222
- def test_integer_decode
223
- i = SNMP::Integer.decode("09")
224
- assert_equal(12345, i.to_i)
225
- end
226
-
227
- def test_integer_equal
228
- i1 = SNMP::Integer.new(12345)
229
- i2 = SNMP::Integer.new(12345)
230
- i3 = 12345.2
231
- i4 = 12345
232
- refute_same(i1, i2)
233
- assert_equal(i1, i2)
234
- assert_equal(i4, i1)
235
- assert_equal(i1, i4)
236
- assert_equal(i1, i3)
237
- end
238
-
239
- def test_integer_comparable
240
- i1 = SNMP::Integer.new(12345)
241
- i2 = SNMP::Integer.new(54321.0)
242
- assert(i1 < i2)
243
- assert(i2 > i1)
244
- assert(123 < i1)
245
- assert(123.0 < i1)
246
- assert(i2 > 54000)
247
- assert(i1 != NoSuchInstance)
248
- assert(NoSuchInstance != i1)
249
- end
250
-
251
- def test_integer_to_oid
252
- assert_equal(ObjectId.new("123"), SNMP::Integer.new(123).to_oid)
253
- assert_equal(ObjectId.new("0"), SNMP::Integer.new(0).to_oid)
254
-
255
- i = SNMP::Integer.new(-1)
256
- assert_raises(RangeError) { i.to_oid }
257
- end
258
-
259
- def test_ip_address_from_string
260
- ip = IpAddress.new("10.0.255.1")
261
- assert_equal("10.0.255.1", ip.to_s)
262
- assert_raises(InvalidIpAddress) { IpAddress.new("1233.2.3.4") }
263
- assert_raises(InvalidIpAddress) { IpAddress.new("1.2.3.-1") }
264
- assert_raises(InvalidIpAddress) { IpAddress.new("1.2.3") }
265
- end
266
-
267
- def test_ip_address_from_self
268
- ip1 = IpAddress.new("1.2.3.4")
269
- ip2 = IpAddress.new(ip1)
270
- assert_equal(ip1, ip2)
271
- end
272
-
273
- def test_ip_address_create
274
- ip = IpAddress.new("\001\002\003\004")
275
- assert_equal("1.2.3.4", ip.to_s)
276
- assert_equal("\001\002\003\004", ip.to_str)
277
- assert_equal("@\004\001\002\003\004", ip.encode)
278
- refute_nil(ip.asn1_type)
279
- end
280
-
281
- def test_ip_address_decode
282
- ip = IpAddress.decode("\001\002\003\004")
283
- assert_equal("1.2.3.4", ip.to_s)
284
- end
285
-
286
- def test_ip_address_decode_extra_octets
287
- ip = IpAddress.decode("\000\000\000\000\000\000\000\000")
288
- assert_equal("0.0.0.0.0.0.0.0", ip.to_s)
289
-
290
- ip = IpAddress.decode("\001\002\003")
291
- assert_equal("1.2.3", ip.to_s)
292
-
293
- ip = IpAddress.decode("\001\002\003\004\005")
294
- assert_equal("1.2.3.4.5", ip.to_s)
295
- end
296
-
297
- def test_ip_address_equals
298
- ip1 = IpAddress.new("1.2.3.4")
299
- ip2 = IpAddress.new("1.2.3.4")
300
- ip3 = IpAddress.new("10.2.3.4")
301
- assert(ip1 == ip2)
302
- assert(ip1.eql?(ip2))
303
- assert(!ip1.equal?(ip2))
304
- assert(ip1 != ip3)
305
- assert(ip1.hash == ip2.hash)
306
- assert(ip1.hash != ip3.hash)
307
- assert(!ip1.eql?(12))
308
- end
309
-
310
- def test_ip_address_to_oid
311
- ip = IpAddress.new("1.2.3.4")
312
- assert_equal(ObjectId.new("1.2.3.4"), ip.to_oid)
313
- end
314
-
315
- def test_counter32_create
316
- i = Counter32.new(12345)
317
- assert_equal("12345", i.to_s)
318
- assert_equal(12345, i.to_i)
319
- assert_equal("\x41\00209", i.encode)
320
- refute_nil(i.asn1_type)
321
- end
322
-
323
- def test_counter32_decode
324
- i = Counter32.decode("09")
325
- assert_equal(12345, i.to_i)
326
- end
327
-
328
- # Decode as a positive number even though high bit is set.
329
- # Not strict ASN.1, but implemented in some agents.
330
- def test_unsigned_decode
331
- i = Counter32.decode("\201\264\353\331")
332
- assert_equal(2176117721, i.to_i)
333
-
334
- i = TimeTicks.decode("\201\264\353\331")
335
- assert_equal(2176117721, i.to_i)
336
- end
337
-
338
- def test_counter64
339
- i = Counter64.new(18446744073709551615)
340
- assert_equal(18446744073709551615, i.to_i)
341
- assert_equal("18446744073709551615", i.to_s)
342
- assert_equal("F\t\000\377\377\377\377\377\377\377\377", i.encode)
343
- assert_equal(i, Counter64.decode("\000\377\377\377\377\377\377\377\377"))
344
- refute_nil(i.asn1_type)
345
- end
346
-
347
- def test_opaque
348
- q = Opaque.new("test")
349
- assert_equal("D\004test", q.encode)
350
- assert_equal("test", Opaque.decode("test"))
351
- assert_equal("test", q.to_s)
352
- refute_nil(q.asn1_type)
353
- end
354
-
355
- def test_exception_methods
356
- exception_types = [NoSuchObject, NoSuchInstance, EndOfMibView]
357
- exception_types.each do |type|
358
- assert(type.respond_to?(:decode))
359
- assert(type.respond_to?(:encode))
360
- assert_equal(type.asn1_type, type.to_s)
361
- end
362
- end
363
-
364
- def test_timeticks
365
- assert_equal("00:00:00.00", TimeTicks.new(0).to_s)
366
- assert_equal("00:00:00.01", TimeTicks.new(1).to_s)
367
- assert_equal("00:00:01.00", TimeTicks.new(100).to_s)
368
- assert_equal("00:01:00.00", TimeTicks.new(60 * 100).to_s)
369
- assert_equal("01:00:00.00", TimeTicks.new(60 * 60 * 100).to_s)
370
- assert_equal("23:59:59.99", TimeTicks.new(24 * 60 * 60 * 100 - 1).to_s)
371
- assert_equal("1 day, 00:00:00.00", TimeTicks.new(24 * 60 * 60 * 100).to_s)
372
- assert_equal("1 day, 23:59:59.99", TimeTicks.new(48 * 60 * 60 * 100 - 1).to_s)
373
- assert_equal("2 days, 00:00:00.00", TimeTicks.new(48 * 60 * 60 * 100).to_s)
374
- assert_equal("497 days, 02:27:52.95", TimeTicks.new(4294967295).to_s)
375
- assert_equal(4294967295, TimeTicks.new(4294967295).to_i)
376
- assert_raises(ArgumentError) {
377
- TimeTicks.new(4294967296)
378
- }
379
- assert_raises(ArgumentError) {
380
- TimeTicks.new(-1)
381
- }
382
- end
383
- end