1111
1212class TestSnmp < Test ::Unit ::TestCase
1313
14- SnmpRequest = "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 "
14+ 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 "
1515
1616 def setup
1717 end
@@ -27,18 +27,40 @@ def test_invalid_packet
2727
2828 end
2929
30+ # The method String#read_ber! added by Net::BER consumes a well-formed BER object
31+ # from the head of a string. If it doesn't find a complete, well-formed BER object,
32+ # it returns nil and leaves the string unchanged. If it finds an object, it returns
33+ # the object and removes it from the head of the string. This is good for handling
34+ # partially-received data streams, such as from network connections.
3035 def test_consume_string
3136 data = "xxx"
3237 assert_equal ( nil , data . read_ber! )
3338 assert_equal ( "xxx" , data )
3439
35- data = SnmpRequest + "!!!"
40+ data = SnmpGetRequest + "!!!"
3641 ary = data . read_ber! ( Net ::SNMP ::AsnSyntax )
3742 assert_equal ( "!!!" , data )
3843 assert ary . is_a? ( Array )
3944 assert ary . is_a? ( Net ::BER ::BerIdentifiedArray )
4045 end
4146
47+ def test_weird_packet
48+ assert_raise ( Net ::SnmpPdu ::Error ) {
49+ Net ::SnmpPdu . new ( "aaaaaaaaaaaaaa" )
50+ }
51+ end
52+
53+ def test_packet
54+ data = SnmpGetRequest . dup
55+ pkt = data . read_ber ( Net ::SNMP ::AsnSyntax )
56+ assert pkt . is_a? ( Net ::BER ::BerIdentifiedArray )
57+ assert_equal ( 48 , pkt . ber_identifier ) # Constructed [0], signifies GetRequest
58+
59+ pdu = Net ::SnmpPdu . new ( pkt )
60+ assert_equal ( :get_request , pdu . pdu_type )
61+ 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 )
63+ end
4264end
4365
4466
0 commit comments