44require 'net/snmp'
55
66class TestSnmp < Test ::Unit ::TestCase
7-
87 SnmpGetRequest = "0'\002 \001 \000 \004 \006 public\240 \032 \002 \002 ?*\002 \001 \000 \002 \001 \000 0\016 0\f \006 \b +\006 \001 \002 \001 \001 \001 \000 \005 \000 "
98 SnmpGetResponse = "0+\002 \001 \000 \004 \006 public\242 \036 \002 \002 '\017 \002 \001 \000 \002 \001 \000 0\022 0\020 \006 \b +\006 \001 \002 \001 \001 \001 \000 \004 \004 test"
109
1110 SnmpGetRequestXXX = "0'\002 \001 \000 \004 \006 xxxxxx\240 \032 \002 \002 ?*\002 \001 \000 \002 \001 \000 0\016 0\f \006 \b +\006 \001 \002 \001 \001 \001 \000 \005 \000 "
1211
13-
14- def setup
15- end
16-
17- def teardown
18- end
19-
2012 def test_invalid_packet
2113 data = "xxxx"
22- assert_raise ( Net ::BER ::BerError ) {
14+ assert_raise ( Net ::BER ::BerError ) {
2315ary = data . read_ber ( Net ::SNMP ::AsnSyntax )
2416 }
17+ end
2518
19+ # The method String#read_ber! added by Net::BER consumes a well-formed BER
20+ # object from the head of a string. If it doesn't find a complete,
21+ # well-formed BER object, it returns nil and leaves the string unchanged.
22+ # If it finds an object, it returns the object and removes it from the
23+ # head of the string. This is good for handling partially-received data
24+ # streams, such as from network connections.
25+ def _test_consume_string
26+ data = "xxx"
27+ assert_equal ( nil , data . read_ber! )
28+ assert_equal ( "xxx" , data )
29+
30+ data = SnmpGetRequest + "!!!"
31+ ary = data . read_ber! ( Net ::SNMP ::AsnSyntax )
32+ assert_equal ( "!!!" , data )
33+ assert ary . is_a? ( Array )
34+ assert ary . is_a? ( Net ::BER ::BerIdentifiedArray )
2635 end
2736
2837 def test_weird_packet
29- assert_raise ( Net ::SnmpPdu ::Error ) {
38+ assert_raise ( Net ::SnmpPdu ::Error ) {
3039Net ::SnmpPdu . parse ( "aaaaaaaaaaaaaa" )
3140 }
3241 end
@@ -35,39 +44,33 @@ def test_get_request
3544 data = SnmpGetRequest . dup
3645 pkt = data . read_ber ( Net ::SNMP ::AsnSyntax )
3746 assert pkt . is_a? ( Net ::BER ::BerIdentifiedArray )
38- assert_equal ( 48 , pkt . ber_identifier ) # Constructed [0], signifies GetRequest
47+ assert_equal ( 48 , pkt . ber_identifier ) # Constructed [0], signifies GetRequest
3948
4049 pdu = Net ::SnmpPdu . parse ( pkt )
41- assert_equal ( :get_request , pdu . pdu_type )
42- assert_equal ( 16170 , pdu . request_id ) # whatever was in the test data. 16170 is not magic.
43- assert_equal ( [ [ [ 1 , 3 , 6 , 1 , 2 , 1 , 1 , 1 , 0 ] , nil ] ] , pdu . variables )
50+ assert_equal ( :get_request , pdu . pdu_type )
51+ assert_equal ( 16170 , pdu . request_id ) # whatever was in the test data. 16170 is not magic.
52+ assert_equal ( [ [ [ 1 , 3 , 6 , 1 , 2 , 1 , 1 , 1 , 0 ] , nil ] ] , pdu . variables )
4453
45- assert_equal ( pdu . to_ber_string , SnmpGetRequest )
54+ assert_equal ( pdu . to_ber_string , SnmpGetRequest )
4655 end
4756
4857 def test_empty_pdu
4958 pdu = Net ::SnmpPdu . new
50- assert_raise ( Net ::SnmpPdu ::Error ) {
51- pdu . to_ber_string
52- }
59+ assert_raise ( Net ::SnmpPdu ::Error ) { pdu . to_ber_string }
5360 end
5461
5562 def test_malformations
5663 pdu = Net ::SnmpPdu . new
5764 pdu . version = 0
5865 pdu . version = 2
59- assert_raise ( Net ::SnmpPdu ::Error ) {
60- pdu . version = 100
61- }
66+ assert_raise ( Net ::SnmpPdu ::Error ) { pdu . version = 100 }
6267
6368 pdu . pdu_type = :get_request
6469 pdu . pdu_type = :get_next_request
6570 pdu . pdu_type = :get_response
6671 pdu . pdu_type = :set_request
6772 pdu . pdu_type = :trap
68- assert_raise ( Net ::SnmpPdu ::Error ) {
69- pdu . pdu_type = :something_else
70- }
73+ assert_raise ( Net ::SnmpPdu ::Error ) { pdu . pdu_type = :something_else }
7174 end
7275
7376 def test_make_response
@@ -78,9 +81,9 @@ def test_make_response
7881 pdu . request_id = 9999
7982 pdu . error_status = 0
8083 pdu . error_index = 0
81- pdu . add_variable_binding [ 1 , 3 , 6 , 1 , 2 , 1 , 1 , 1 , 0 ] , "test"
84+ pdu . add_variable_binding [ 1 , 3 , 6 , 1 , 2 , 1 , 1 , 1 , 0 ] , "test"
8285
83- assert_equal ( SnmpGetResponse , pdu . to_ber_string )
86+ assert_equal ( SnmpGetResponse , pdu . to_ber_string )
8487 end
8588
8689 def test_make_bad_response
@@ -94,20 +97,18 @@ def test_make_bad_response
9497
9598 def test_snmp_integers
9699 c32 = Net ::SNMP ::Counter32 . new ( 100 )
97- assert_equal ( "A\001 d" , c32 . to_ber )
100+ assert_equal ( "A\001 d" , c32 . to_ber )
98101 g32 = Net ::SNMP ::Gauge32 . new ( 100 )
99- assert_equal ( "B\001 d" , g32 . to_ber )
102+ assert_equal ( "B\001 d" , g32 . to_ber )
100103 t32 = Net ::SNMP ::TimeTicks32 . new ( 100 )
101- assert_equal ( "C\001 d" , t32 . to_ber )
104+ assert_equal ( "C\001 d" , t32 . to_ber )
102105 end
103106
104107 def test_community
105108 data = SnmpGetRequestXXX . dup
106109 ary = data . read_ber ( Net ::SNMP ::AsnSyntax )
107- pdu = Net ::SnmpPdu . parse ( ary )
108- assert_equal ( "xxxxxx" , pdu . community )
110+ pdu = Net ::SnmpPdu . parse ( ary )
111+ assert_equal ( "xxxxxx" , pdu . community )
109112 end
110113
111114end
112-
113-
0 commit comments