Skip to content

Commit

Permalink
Merge pull request hallidave#25 from hiro-su/error_message
Browse files Browse the repository at this point in the history
Fixed the error message when a list of ObjectId or VarBind Objects is nil
  • Loading branch information
hallidave committed Jul 30, 2013
2 parents f185c7f + 153f268 commit 9986991
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/snmp/mib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def load_module(module_name, mib_dir=DEFAULT_MIB_PATH)
# - list of VarBinds
#
def varbind_list(object_list, option=:KeepValue)
raise ArgumentError, "A list of ObjectId or VarBind objects is NilClass" if object_list.nil?
vb_list = VarBindList.new
if object_list.respond_to? :to_str
vb_list << oid(object_list).to_varbind
Expand Down
28 changes: 28 additions & 0 deletions test/test_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def test_get
assert_equal(ObjectId.new("1.2.3.4.5"), response.varbind_list[1].name)
end

def test_get_with_nil
assert_raise(ArgumentError) { @manager.get(nil) }
end

def test_get_value
value = @manager.get_value("1.2.3.4")
assert_equal(Null, value)
Expand All @@ -124,13 +128,21 @@ def test_get_next
assert_equal(ObjectId.new("1.2.3.4.5"), response.varbind_list[1].name)
end

def test_get_next_with_nil
assert_raise(ArgumentError) { @manager.get_next(nil) }
end

def test_set
v0 = VarBind.new("1.3.6.1.3.1.1.1.0", OctetString.new("Hello1"))
v1 = VarBind.new("1.3.6.1.3.1.1.1.0", OctetString.new("Hello2"))
response = @manager.set([v0, v1])
assert_equal("Hello1", response.varbind_list[0].value.to_s)
assert_equal("Hello2", response.varbind_list[1].value.to_s)
end

def test_set_with_nil
assert_raise(ArgumentError) { @manager.set(nil) }
end

def test_single_set
varbind = VarBind.new("1.3.6.1.3.1.1.1.0", OctetString.new("Hello"))
Expand All @@ -147,6 +159,10 @@ def test_get_bulk
assert_equal("SNMPv2-SMI::experimental.1.1.2.0", response.varbind_list[1].name.to_s)
end

def test_get_bulk_with_nil
assert_raise(ArgumentError) { @manager.get_bulk(nil, nil, nil) }
end

def test_walk
old_verbose = $VERBOSE
$VERBOSE = nil
Expand All @@ -155,6 +171,10 @@ def test_walk
$VERBOSE = old_verbose
end

def test_walk_with_nil
assert_raise(ArgumentError) { @manager.walk(nil) {} }
end

def test_request_id
id = RequestId.new
fail if id.next < 0 or id.next >= 2**31
Expand Down Expand Up @@ -188,6 +208,10 @@ def test_trap_v1
assert_equal(1, pdu.vb_list.length)
end

def test_trap_v1_with_nil
assert_raise(ArgumentError) { @manager.trap_v1(nil) }
end

def test_trap_v2
sent_data = @manager.trap_v2(1234, "1.3.6.1.2.3.4")
pdu = Message.decode(sent_data).pdu
Expand All @@ -203,6 +227,10 @@ def test_trap_v2
assert_equal("1.4.5.6", pdu.vb_list.last.name.to_s)
end

def test_trap_v2_with_nil
assert_raise(ArgumentError) { @manager.trap_v2(nil, nil, nil) }
end

def test_inform
response = @manager.inform(1234, "1.3.6.1.2.3.4")
assert_equal(1234, response.vb_list[0].value)
Expand Down
4 changes: 4 additions & 0 deletions test/test_mib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def test_varbind_list
assert_equal("1.3.6.1.2.1.2.2.1.23", vb_list.first.name.to_s)
end

def test_varbind_list_with_nil
assert_raise(ArgumentError) { @mib.varbind_list(nil) }
end

def test_varbind
vb = @mib.varbind("1.2.3.4", Null)
assert_equal("1.2.3.4", vb.name.to_s)
Expand Down

0 comments on commit 9986991

Please sign in to comment.