1212class TestSnmp < Test ::Unit ::TestCase
1313
1414 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 "
15+ 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"
16+
1517
1618 def setup
1719 end
@@ -46,7 +48,7 @@ def test_consume_string
4648
4749 def test_weird_packet
4850 assert_raise ( Net ::SnmpPdu ::Error ) {
49- Net ::SnmpPdu . new ( "aaaaaaaaaaaaaa" )
51+ Net ::SnmpPdu . parse ( "aaaaaaaaaaaaaa" )
5052 }
5153 end
5254
@@ -56,11 +58,50 @@ def test_get_request
5658 assert pkt . is_a? ( Net ::BER ::BerIdentifiedArray )
5759 assert_equal ( 48 , pkt . ber_identifier ) # Constructed [0], signifies GetRequest
5860
59- pdu = Net ::SnmpPdu . new ( pkt )
61+ pdu = Net ::SnmpPdu . parse ( pkt )
6062 assert_equal ( :get_request , pdu . pdu_type )
6163 assert_equal ( 16170 , pdu . request_id ) # whatever was in the test data. 16170 is not magic.
62- assert_equal ( [ [ 1 , 3 , 6 , 1 , 2 , 1 , 1 , 1 , 0 ] ] , pdu . variables )
64+ assert_equal ( [ [ [ 1 , 3 , 6 , 1 , 2 , 1 , 1 , 1 , 0 ] , nil ] ] , pdu . variables )
65+
66+ assert_equal ( pdu . to_ber_string , SnmpGetRequest )
67+ end
68+
69+ def test_empty_pdu
70+ pdu = Net ::SnmpPdu . new
71+ assert_raise ( Net ::SnmpPdu ::Error ) {
72+ pdu . to_ber_string
73+ }
74+ end
75+
76+ def test_malformations
77+ pdu = Net ::SnmpPdu . new
78+ assert_raise ( Net ::SnmpPdu ::Error ) {
79+ pdu . version = 100
80+ }
81+
82+ pdu . pdu_type = :get_request
83+ pdu . pdu_type = :get_next_request
84+ pdu . pdu_type = :get_response
85+ pdu . pdu_type = :set_request
86+ pdu . pdu_type = :trap
87+ assert_raise ( Net ::SnmpPdu ::Error ) {
88+ pdu . pdu_type = :something_else
89+ }
90+ end
91+
92+ def test_make_response
93+ pdu = Net ::SnmpPdu . new
94+ pdu . version = 0
95+ pdu . community = "public"
96+ pdu . pdu_type = :get_response
97+ pdu . request_id = 9999
98+ pdu . error_status = 0
99+ pdu . error_index = 0
100+ pdu . add_variable_binding [ 1 , 3 , 6 , 1 , 2 , 1 , 1 , 1 , 0 ] , "test"
101+
102+ assert_equal ( SnmpGetResponse , pdu . to_ber_string )
63103 end
104+
64105end
65106
66107
0 commit comments