snmp 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. data/README +193 -0
  2. data/Rakefile +52 -0
  3. data/data/ruby/snmp/mibs/ACCOUNTING-CONTROL-MIB.yaml +44 -0
  4. data/data/ruby/snmp/mibs/ADSL-LINE-EXT-MIB.yaml +66 -0
  5. data/data/ruby/snmp/mibs/ADSL-LINE-MIB.yaml +238 -0
  6. data/data/ruby/snmp/mibs/ADSL-TC-MIB.yaml +2 -0
  7. data/data/ruby/snmp/mibs/AGENTX-MIB.yaml +40 -0
  8. data/data/ruby/snmp/mibs/APM-MIB.yaml +98 -0
  9. data/data/ruby/snmp/mibs/APPC-MIB.yaml +295 -0
  10. data/data/ruby/snmp/mibs/APPLETALK-MIB.yaml +284 -0
  11. data/data/ruby/snmp/mibs/APPLICATION-MIB.yaml +161 -0
  12. data/data/ruby/snmp/mibs/APPN-DLUR-MIB.yaml +41 -0
  13. data/data/ruby/snmp/mibs/APPN-MIB.yaml +356 -0
  14. data/data/ruby/snmp/mibs/APPN-TRAP-MIB.yaml +8 -0
  15. data/data/ruby/snmp/mibs/APS-MIB.yaml +62 -0
  16. data/data/ruby/snmp/mibs/ATM-ACCOUNTING-INFORMATION-MIB.yaml +37 -0
  17. data/data/ruby/snmp/mibs/ATM-MIB.yaml +112 -0
  18. data/data/ruby/snmp/mibs/ATM-TC-MIB.yaml +19 -0
  19. data/data/ruby/snmp/mibs/ATM2-MIB.yaml +182 -0
  20. data/data/ruby/snmp/mibs/BGP4-MIB.yaml +56 -0
  21. data/data/ruby/snmp/mibs/BLDG-HVAC-MIB.yaml +35 -0
  22. data/data/ruby/snmp/mibs/BRIDGE-MIB.yaml +63 -0
  23. data/data/ruby/snmp/mibs/CHARACTER-MIB.yaml +48 -0
  24. data/data/ruby/snmp/mibs/CIRCUIT-IF-MIB.yaml +21 -0
  25. data/data/ruby/snmp/mibs/CLNS-MIB.yaml +116 -0
  26. data/data/ruby/snmp/mibs/COFFEE-POT-MIB.yaml +13 -0
  27. data/data/ruby/snmp/mibs/COPS-CLIENT-MIB.yaml +54 -0
  28. data/data/ruby/snmp/mibs/DECNET-PHIV-MIB.yaml +237 -0
  29. data/data/ruby/snmp/mibs/DIAL-CONTROL-MIB.yaml +84 -0
  30. data/data/ruby/snmp/mibs/DIFFSERV-CONFIG-MIB.yaml +15 -0
  31. data/data/ruby/snmp/mibs/DIFFSERV-DSCP-TC.yaml +2 -0
  32. data/data/ruby/snmp/mibs/DIFFSERV-MIB.yaml +171 -0
  33. data/data/ruby/snmp/mibs/DIRECTORY-SERVER-MIB.yaml +52 -0
  34. data/data/ruby/snmp/mibs/DISMAN-EVENT-MIB.yaml +110 -0
  35. data/data/ruby/snmp/mibs/DISMAN-EXPRESSION-MIB.yaml +55 -0
  36. data/data/ruby/snmp/mibs/DISMAN-NSLOOKUP-MIB.yaml +23 -0
  37. data/data/ruby/snmp/mibs/DISMAN-PING-MIB.yaml +57 -0
  38. data/data/ruby/snmp/mibs/DISMAN-SCHEDULE-MIB.yaml +32 -0
  39. data/data/ruby/snmp/mibs/DISMAN-SCRIPT-MIB.yaml +80 -0
  40. data/data/ruby/snmp/mibs/DISMAN-TRACEROUTE-MIB.yaml +72 -0
  41. data/data/ruby/snmp/mibs/DLSW-MIB.yaml +193 -0
  42. data/data/ruby/snmp/mibs/DNS-RESOLVER-MIB.yaml +87 -0
  43. data/data/ruby/snmp/mibs/DNS-SERVER-MIB.yaml +73 -0
  44. data/data/ruby/snmp/mibs/DOCS-BPI-MIB.yaml +101 -0
  45. data/data/ruby/snmp/mibs/DOCS-CABLE-DEVICE-MIB.yaml +107 -0
  46. data/data/ruby/snmp/mibs/DOCS-IF-MIB.yaml +143 -0
  47. data/data/ruby/snmp/mibs/DOT12-IF-MIB.yaml +33 -0
  48. data/data/ruby/snmp/mibs/DS0-MIB.yaml +18 -0
  49. data/data/ruby/snmp/mibs/DS0BUNDLE-MIB.yaml +17 -0
  50. data/data/ruby/snmp/mibs/DS1-MIB.yaml +118 -0
  51. data/data/ruby/snmp/mibs/DS3-MIB.yaml +106 -0
  52. data/data/ruby/snmp/mibs/DSA-MIB.yaml +44 -0
  53. data/data/ruby/snmp/mibs/DSMON-MIB.yaml +219 -0
  54. data/data/ruby/snmp/mibs/EBN-MIB.yaml +47 -0
  55. data/data/ruby/snmp/mibs/ENTITY-MIB.yaml +51 -0
  56. data/data/ruby/snmp/mibs/ENTITY-SENSOR-MIB.yaml +16 -0
  57. data/data/ruby/snmp/mibs/ETHER-CHIPSET-MIB.yaml +79 -0
  58. data/data/ruby/snmp/mibs/ETHER-WIS.yaml +28 -0
  59. data/data/ruby/snmp/mibs/EtherLike-MIB.yaml +58 -0
  60. data/data/ruby/snmp/mibs/FDDI-SMT73-MIB.yaml +128 -0
  61. data/data/ruby/snmp/mibs/FIBRE-CHANNEL-FE-MIB.yaml +122 -0
  62. data/data/ruby/snmp/mibs/FLOW-METER-MIB.yaml +112 -0
  63. data/data/ruby/snmp/mibs/FR-ATM-PVC-SERVICE-IWF-MIB.yaml +45 -0
  64. data/data/ruby/snmp/mibs/FR-MFR-MIB.yaml +52 -0
  65. data/data/ruby/snmp/mibs/FRAME-RELAY-DTE-MIB.yaml +54 -0
  66. data/data/ruby/snmp/mibs/FRNETSERV-MIB.yaml +109 -0
  67. data/data/ruby/snmp/mibs/FRSLD-MIB.yaml +82 -0
  68. data/data/ruby/snmp/mibs/Finisher-MIB.yaml +53 -0
  69. data/data/ruby/snmp/mibs/GSMP-MIB.yaml +82 -0
  70. data/data/ruby/snmp/mibs/HC-ALARM-MIB.yaml +32 -0
  71. data/data/ruby/snmp/mibs/HC-PerfHist-TC-MIB.yaml +2 -0
  72. data/data/ruby/snmp/mibs/HC-RMON-MIB.yaml +193 -0
  73. data/data/ruby/snmp/mibs/HCNUM-TC.yaml +2 -0
  74. data/data/ruby/snmp/mibs/HDSL2-SHDSL-LINE-MIB.yaml +115 -0
  75. data/data/ruby/snmp/mibs/HOST-RESOURCES-MIB.yaml +98 -0
  76. data/data/ruby/snmp/mibs/HOST-RESOURCES-TYPES.yaml +56 -0
  77. data/data/ruby/snmp/mibs/HPR-IP-MIB.yaml +24 -0
  78. data/data/ruby/snmp/mibs/HPR-MIB.yaml +81 -0
  79. data/data/ruby/snmp/mibs/IF-INVERTED-STACK-MIB.yaml +9 -0
  80. data/data/ruby/snmp/mibs/IF-MIB.yaml +73 -0
  81. data/data/ruby/snmp/mibs/IGMP-STD-MIB.yaml +33 -0
  82. data/data/ruby/snmp/mibs/INET-ADDRESS-MIB.yaml +2 -0
  83. data/data/ruby/snmp/mibs/INTEGRATED-SERVICES-GUARANTEED-MIB.yaml +13 -0
  84. data/data/ruby/snmp/mibs/INTEGRATED-SERVICES-MIB.yaml +44 -0
  85. data/data/ruby/snmp/mibs/INTERFACETOPN-MIB.yaml +29 -0
  86. data/data/ruby/snmp/mibs/IP-FORWARD-MIB.yaml +42 -0
  87. data/data/ruby/snmp/mibs/IP-MIB.yaml +66 -0
  88. data/data/ruby/snmp/mibs/IPATM-IPMC-MIB.yaml +203 -0
  89. data/data/ruby/snmp/mibs/IPMROUTE-STD-MIB.yaml +64 -0
  90. data/data/ruby/snmp/mibs/IPOA-MIB.yaml +85 -0
  91. data/data/ruby/snmp/mibs/IPV6-FLOW-LABEL-MIB.yaml +2 -0
  92. data/data/ruby/snmp/mibs/IPV6-ICMP-MIB.yaml +42 -0
  93. data/data/ruby/snmp/mibs/IPV6-MIB.yaml +88 -0
  94. data/data/ruby/snmp/mibs/IPV6-MLD-MIB.yaml +30 -0
  95. data/data/ruby/snmp/mibs/IPV6-TCP-MIB.yaml +14 -0
  96. data/data/ruby/snmp/mibs/IPV6-UDP-MIB.yaml +11 -0
  97. data/data/ruby/snmp/mibs/ISDN-MIB.yaml +71 -0
  98. data/data/ruby/snmp/mibs/Job-Monitoring-MIB.yaml +43 -0
  99. data/data/ruby/snmp/mibs/L2TP-MIB.yaml +156 -0
  100. data/data/ruby/snmp/mibs/MALLOC-MIB.yaml +83 -0
  101. data/data/ruby/snmp/mibs/MAU-MIB.yaml +110 -0
  102. data/data/ruby/snmp/mibs/MIOX25-MIB.yaml +38 -0
  103. data/data/ruby/snmp/mibs/MIP-MIB.yaml +180 -0
  104. data/data/ruby/snmp/mibs/MPLS-FTN-STD-MIB.yaml +44 -0
  105. data/data/ruby/snmp/mibs/MPLS-LDP-ATM-STD-MIB.yaml +35 -0
  106. data/data/ruby/snmp/mibs/MPLS-LDP-FRAME-RELAY-STD-MIB.yaml +28 -0
  107. data/data/ruby/snmp/mibs/MPLS-LDP-GENERIC-STD-MIB.yaml +15 -0
  108. data/data/ruby/snmp/mibs/MPLS-LDP-STD-MIB.yaml +118 -0
  109. data/data/ruby/snmp/mibs/MPLS-LSR-STD-MIB.yaml +98 -0
  110. data/data/ruby/snmp/mibs/MPLS-TC-STD-MIB.yaml +3 -0
  111. data/data/ruby/snmp/mibs/MPLS-TE-STD-MIB.yaml +120 -0
  112. data/data/ruby/snmp/mibs/MTA-MIB.yaml +64 -0
  113. data/data/ruby/snmp/mibs/Modem-MIB.yaml +106 -0
  114. data/data/ruby/snmp/mibs/NETWORK-SERVICES-MIB.yaml +33 -0
  115. data/data/ruby/snmp/mibs/NHRP-MIB.yaml +163 -0
  116. data/data/ruby/snmp/mibs/NOTIFICATION-LOG-MIB.yaml +51 -0
  117. data/data/ruby/snmp/mibs/OPT-IF-MIB.yaml +431 -0
  118. data/data/ruby/snmp/mibs/OSPF-MIB.yaml +150 -0
  119. data/data/ruby/snmp/mibs/OSPF-TRAP-MIB.yaml +11 -0
  120. data/data/ruby/snmp/mibs/P-BRIDGE-MIB.yaml +51 -0
  121. data/data/ruby/snmp/mibs/PARALLEL-MIB.yaml +24 -0
  122. data/data/ruby/snmp/mibs/PIM-MIB.yaml +66 -0
  123. data/data/ruby/snmp/mibs/PINT-MIB.yaml +51 -0
  124. data/data/ruby/snmp/mibs/POWER-ETHERNET-MIB.yaml +36 -0
  125. data/data/ruby/snmp/mibs/PPP-BRIDGE-NCP-MIB.yaml +25 -0
  126. data/data/ruby/snmp/mibs/PPP-IP-NCP-MIB.yaml +13 -0
  127. data/data/ruby/snmp/mibs/PPP-LCP-MIB.yaml +47 -0
  128. data/data/ruby/snmp/mibs/PPP-SEC-MIB.yaml +20 -0
  129. data/data/ruby/snmp/mibs/PTOPO-MIB.yaml +39 -0
  130. data/data/ruby/snmp/mibs/PerfHist-TC-MIB.yaml +2 -0
  131. data/data/ruby/snmp/mibs/Printer-MIB.yaml +209 -0
  132. data/data/ruby/snmp/mibs/Q-BRIDGE-MIB.yaml +99 -0
  133. data/data/ruby/snmp/mibs/RADIUS-ACC-CLIENT-MIB.yaml +26 -0
  134. data/data/ruby/snmp/mibs/RADIUS-ACC-SERVER-MIB.yaml +35 -0
  135. data/data/ruby/snmp/mibs/RADIUS-AUTH-CLIENT-MIB.yaml +28 -0
  136. data/data/ruby/snmp/mibs/RADIUS-AUTH-SERVER-MIB.yaml +37 -0
  137. data/data/ruby/snmp/mibs/RDBMS-MIB.yaml +82 -0
  138. data/data/ruby/snmp/mibs/RFC1065-SMI.yaml +9 -0
  139. data/data/ruby/snmp/mibs/RFC1155-SMI.yaml +9 -0
  140. data/data/ruby/snmp/mibs/RFC1158-MIB.yaml +202 -0
  141. data/data/ruby/snmp/mibs/RFC1213-MIB.yaml +202 -0
  142. data/data/ruby/snmp/mibs/RFC1269-MIB.yaml +29 -0
  143. data/data/ruby/snmp/mibs/RFC1271-MIB.yaml +214 -0
  144. data/data/ruby/snmp/mibs/RFC1285-MIB.yaml +104 -0
  145. data/data/ruby/snmp/mibs/RFC1316-MIB.yaml +42 -0
  146. data/data/ruby/snmp/mibs/RFC1381-MIB.yaml +60 -0
  147. data/data/ruby/snmp/mibs/RFC1382-MIB.yaml +169 -0
  148. data/data/ruby/snmp/mibs/RFC1414-MIB.yaml +10 -0
  149. data/data/ruby/snmp/mibs/RIPv2-MIB.yaml +34 -0
  150. data/data/ruby/snmp/mibs/RMON-MIB.yaml +219 -0
  151. data/data/ruby/snmp/mibs/RMON2-MIB.yaml +282 -0
  152. data/data/ruby/snmp/mibs/ROHC-MIB.yaml +64 -0
  153. data/data/ruby/snmp/mibs/ROHC-RTP-MIB.yaml +29 -0
  154. data/data/ruby/snmp/mibs/ROHC-UNCOMPRESSED-MIB.yaml +11 -0
  155. data/data/ruby/snmp/mibs/RS-232-MIB.yaml +54 -0
  156. data/data/ruby/snmp/mibs/RSVP-MIB.yaml +156 -0
  157. data/data/ruby/snmp/mibs/RTP-MIB.yaml +57 -0
  158. data/data/ruby/snmp/mibs/SFLOW-MIB.yaml +22 -0
  159. data/data/ruby/snmp/mibs/SIP-MIB.yaml +70 -0
  160. data/data/ruby/snmp/mibs/SLAPM-MIB.yaml +144 -0
  161. data/data/ruby/snmp/mibs/SMON-MIB.yaml +63 -0
  162. data/data/ruby/snmp/mibs/SNA-NAU-MIB.yaml +137 -0
  163. data/data/ruby/snmp/mibs/SNA-SDLC-MIB.yaml +147 -0
  164. data/data/ruby/snmp/mibs/SNMP-COMMUNITY-MIB.yaml +22 -0
  165. data/data/ruby/snmp/mibs/SNMP-FRAMEWORK-MIB.yaml +14 -0
  166. data/data/ruby/snmp/mibs/SNMP-MPD-MIB.yaml +11 -0
  167. data/data/ruby/snmp/mibs/SNMP-NOTIFICATION-MIB.yaml +25 -0
  168. data/data/ruby/snmp/mibs/SNMP-PROXY-MIB.yaml +17 -0
  169. data/data/ruby/snmp/mibs/SNMP-REPEATER-MIB.yaml +135 -0
  170. data/data/ruby/snmp/mibs/SNMP-TARGET-MIB.yaml +29 -0
  171. data/data/ruby/snmp/mibs/SNMP-USER-BASED-SM-MIB.yaml +35 -0
  172. data/data/ruby/snmp/mibs/SNMP-USM-DH-OBJECTS-MIB.yaml +21 -0
  173. data/data/ruby/snmp/mibs/SNMP-VIEW-BASED-ACM-MIB.yaml +37 -0
  174. data/data/ruby/snmp/mibs/SNMPv2-MIB.yaml +58 -0
  175. data/data/ruby/snmp/mibs/SNMPv2-SMI.yaml +17 -0
  176. data/data/ruby/snmp/mibs/SNMPv2-TM.yaml +9 -0
  177. data/data/ruby/snmp/mibs/SNMPv2-USEC-MIB.yaml +19 -0
  178. data/data/ruby/snmp/mibs/SONET-MIB.yaml +131 -0
  179. data/data/ruby/snmp/mibs/SOURCE-ROUTING-MIB.yaml +30 -0
  180. data/data/ruby/snmp/mibs/SYSAPPL-MIB.yaml +81 -0
  181. data/data/ruby/snmp/mibs/TCP-MIB.yaml +27 -0
  182. data/data/ruby/snmp/mibs/TCPIPX-MIB.yaml +24 -0
  183. data/data/ruby/snmp/mibs/TN3270E-MIB.yaml +105 -0
  184. data/data/ruby/snmp/mibs/TN3270E-RT-MIB.yaml +43 -0
  185. data/data/ruby/snmp/mibs/TOKEN-RING-RMON-MIB.yaml +183 -0
  186. data/data/ruby/snmp/mibs/TOKENRING-MIB.yaml +58 -0
  187. data/data/ruby/snmp/mibs/TOKENRING-STATION-SR-MIB.yaml +12 -0
  188. data/data/ruby/snmp/mibs/TRANSPORT-ADDRESS-MIB.yaml +19 -0
  189. data/data/ruby/snmp/mibs/TUNNEL-MIB.yaml +23 -0
  190. data/data/ruby/snmp/mibs/UDP-MIB.yaml +14 -0
  191. data/data/ruby/snmp/mibs/UPS-MIB.yaml +117 -0
  192. data/data/ruby/snmp/mibs/VDSL-LINE-MIB.yaml +173 -0
  193. data/data/ruby/snmp/mibs/VRRP-MIB.yaml +50 -0
  194. data/data/ruby/snmp/mibs/WWW-MIB.yaml +91 -0
  195. data/examples/dump.rb +15 -0
  196. data/examples/get.rb +7 -0
  197. data/examples/iftable.rb +14 -0
  198. data/examples/log_traps.rb +23 -0
  199. data/examples/set.rb +8 -0
  200. data/examples/walk.rb +9 -0
  201. data/lib/snmp.rb +10 -0
  202. data/lib/snmp/agent.rb +74 -0
  203. data/lib/snmp/ber.rb +332 -0
  204. data/lib/snmp/manager.rb +502 -0
  205. data/lib/snmp/mib.rb +259 -0
  206. data/lib/snmp/pdu.rb +363 -0
  207. data/lib/snmp/varbind.rb +520 -0
  208. data/setup.rb +1360 -0
  209. data/test/test_ber.rb +236 -0
  210. data/test/test_manager.rb +192 -0
  211. data/test/test_mib.rb +66 -0
  212. data/test/test_pdu.rb +184 -0
  213. data/test/test_varbind.rb +270 -0
  214. metadata +251 -0
@@ -0,0 +1,236 @@
1
+ require 'test/unit'
2
+ require 'snmp/ber'
3
+
4
+ class ASN1_Test < Test::Unit::TestCase
5
+
6
+ include SNMP::BER
7
+
8
+ def test_decode_tlv_empty
9
+ tag, value, data = decode_tlv("\001\000")
10
+ assert_equal(1, tag)
11
+ assert_equal("", value)
12
+ assert_equal("", data)
13
+
14
+ tag, value, data = decode_tlv("\001\000\111")
15
+ assert_equal(1, tag)
16
+ assert_equal("", value)
17
+ assert_equal("\111", data)
18
+ end
19
+
20
+ # Decode TLV data with the short-format length field.
21
+ def test_decode_tlv_short
22
+ tag, value, data = decode_tlv("\001\002\023\044")
23
+ assert_equal(1, tag)
24
+ assert_equal("\023\044", value)
25
+ assert_equal("", data)
26
+ end
27
+
28
+ # Extra bytes of data are returned.
29
+ def test_decode_tlv_short_extra
30
+ tag, value, data = decode_tlv("\001\002\023\044\000")
31
+ assert_equal(1, tag)
32
+ assert_equal("\023\044", value)
33
+ assert_equal("\000", data)
34
+ end
35
+
36
+ # Decode TLV data with long-format length field
37
+ def test_decode_tlv_long
38
+ long_data = ""; 128.times { |i| long_data << i.chr }
39
+ tag, value, data = decode_tlv("\001\201\200" + long_data)
40
+ assert_equal(1, tag)
41
+ assert_equal(long_data, value)
42
+ assert_equal("", data)
43
+ end
44
+
45
+ # Long format with extra bytes - use four bytes for length
46
+ def test_decode_tlv_long_extra
47
+ long_data = ""; 129.times { |i| long_data << i.chr }
48
+ tag, value, data = decode_tlv("\001\204\000\000\000\201" + long_data + "\123\123\123")
49
+ assert_equal(1, tag)
50
+ assert_equal(long_data, value)
51
+ assert_equal("\123\123\123", data)
52
+ end
53
+
54
+ # Check invalid length - ASN.1 says that first length octet can't be 255.
55
+ def test_bad_length
56
+ assert_raise(InvalidLength) {
57
+ decode_tlv("\001\377\001")
58
+ }
59
+ end
60
+
61
+ # Check if input data is too short
62
+ def test_out_of_data
63
+ assert_raise(OutOfData) {
64
+ decode_tlv("\001\001")
65
+ }
66
+ assert_raise(OutOfData) {
67
+ decode_tlv("\001")
68
+ }
69
+ assert_raise(OutOfData) {
70
+ decode_tlv("")
71
+ }
72
+ end
73
+
74
+ # Decode single-octet ASN.1 integer.
75
+ # Number is negative (2's complement) if high order bit is 1.
76
+ def test_decode_integer_octet
77
+ i, data = decode_integer("\002\001\042")
78
+ assert_equal(34, i)
79
+ assert_equal("", data)
80
+ i, data = decode_integer("\002\001\377")
81
+ assert_equal(-1, i)
82
+ assert_equal("", data)
83
+ end
84
+
85
+ # Decode multi-octet ASN.1 integer.
86
+ def test_decode_integer
87
+ i, data = decode_integer("\002\004\001\002\003\004")
88
+ assert_equal(16909060, i)
89
+ assert_equal("", data)
90
+
91
+ i, data = decode_integer("\002\004\377\377\377\376")
92
+ assert_equal(-2, i)
93
+ assert_equal("", data)
94
+
95
+ i, data = decode_integer("\002\003\000\377\376")
96
+ assert_equal(65534, i)
97
+ assert_equal("", data)
98
+
99
+ i, data = decode_integer("\002\002\000\377")
100
+ assert_equal(255, i)
101
+ assert_equal("", data)
102
+
103
+ assert_raise(InvalidTag) {
104
+ decode_integer("\001\004\001\002\003\004")
105
+ }
106
+ end
107
+
108
+ def test_decode_timeticks
109
+ i, data = decode_timeticks("\103\004\001\002\003\004")
110
+ assert_equal(16909060, i)
111
+ assert_equal("", data)
112
+
113
+ assert_raise(InvalidTag) {
114
+ decode_timeticks("\002\004\001\002\003\004")
115
+ }
116
+ end
117
+
118
+ # Decode ASN.1 octet string
119
+ def test_decode_octet_string
120
+ s, data = decode_octet_string("\004\202\000\005hello")
121
+ assert_equal("hello",s)
122
+ assert_raise(InvalidTag) {
123
+ decode_octet_string("\005\202\000\005hello")
124
+ }
125
+ end
126
+
127
+ def test_decode_ip_address
128
+ ip, data = decode_ip_address("@\004\001\002\003\004")
129
+ assert_equal(ip, "\001\002\003\004")
130
+ assert_raise(InvalidTag) {
131
+ decode_ip_address("\004\004\001\002\003\004")
132
+ }
133
+ assert_raise(InvalidLength) {
134
+ decode_ip_address("@\005\001\002\003\004\005")
135
+ }
136
+ end
137
+
138
+ # Decode ASN.1 sequence
139
+ def test_decode_sequence
140
+ seq, data = decode_sequence("\060\003\002\001\077")
141
+ assert_equal("\002\001\077", seq)
142
+ assert_equal("", data)
143
+
144
+ seq, data = decode_sequence("\060\003\002\001\077\002\001\001")
145
+ assert_equal("\002\001\077", seq)
146
+ assert_equal("\002\001\001", data)
147
+
148
+ assert_raise(InvalidTag) {
149
+ decode_sequence("\061\003\002\001\077")
150
+ }
151
+ end
152
+
153
+ def test_decode_object_id
154
+ # Handle an empty object identifier because net-snmp does it and
155
+ # they probably had a good reason.
156
+ object_id, remainder = decode_object_id("\006\000")
157
+ assert_equal([], object_id)
158
+ assert_equal("", remainder)
159
+
160
+ object_id, remainder = decode_object_id("\006\001+")
161
+ assert_equal([1,3], object_id);
162
+ assert_equal("", remainder)
163
+
164
+ object_id, remainder = decode_object_id("\006\002+\006")
165
+ assert_equal([1,3,6], object_id);
166
+ assert_equal("", remainder)
167
+
168
+ object_id, remainder = decode_object_id("\006\003+\202\001")
169
+ assert_equal([1,3,257], object_id);
170
+ assert_equal("", remainder)
171
+
172
+ object_id, remainder = decode_object_id("\006\003S\202\001")
173
+ assert_equal([2,3,257], object_id);
174
+ assert_equal("", remainder)
175
+
176
+ object_id, remainder = decode_object_id("\006\001\000")
177
+ assert_equal([0,0], object_id);
178
+ assert_equal("", remainder)
179
+
180
+ assert_raise(InvalidTag) do
181
+ decode_object_id("\007\001+")
182
+ end
183
+ end
184
+
185
+ def test_encode_length
186
+ assert_equal("\000", encode_length(0))
187
+ assert_equal("\001", encode_length(1))
188
+ assert_equal("\177", encode_length(127))
189
+ assert_equal("\201\200", encode_length(128))
190
+ assert_equal("\202\002\001", encode_length(513))
191
+ assert_raise(InvalidLength) { encode_length(-1) }
192
+ end
193
+
194
+ def test_encode_integer
195
+ assert_equal("\002\001\000", encode_integer(0))
196
+ assert_equal("\002\001\001", encode_integer(1))
197
+ assert_equal("\002\001\177", encode_integer(127))
198
+ assert_equal("\002\002\000\200", encode_integer(128))
199
+ assert_equal("\002\002\000\377", encode_integer(255))
200
+ assert_equal("\002\002\001\000", encode_integer(256))
201
+
202
+ assert_equal("\002\001\377", encode_integer(-1))
203
+ assert_equal("\002\001\200", encode_integer(-128))
204
+ assert_equal("\002\002\377\177", encode_integer(-129))
205
+ end
206
+
207
+ def test_encode_octet_string
208
+ assert_equal("\004\015Dave was here", encode_octet_string("Dave was here"))
209
+ assert_equal("\004\000", encode_octet_string(""))
210
+ end
211
+
212
+ def test_encode_sequence
213
+ assert_equal("0\015Dave was here", encode_sequence("Dave was here"))
214
+ assert_equal("0\000", encode_sequence(""))
215
+ end
216
+
217
+ def test_encode_null
218
+ assert_equal("\005\000", encode_null)
219
+ end
220
+
221
+ def test_encode_exception
222
+ assert_equal("\200\000", encode_exception(0x80))
223
+ end
224
+
225
+ def test_encode_object_id
226
+ assert_equal("\006\001" << 80.chr, encode_object_id([2]))
227
+ assert_equal("\006\001\000", encode_object_id([0,0]))
228
+ assert_equal("\006\001+", encode_object_id([1,3]))
229
+ assert_equal("\006\002+\006", encode_object_id([1,3,6]))
230
+ assert_equal("\006\003+\202\001", encode_object_id([1,3,257]))
231
+ assert_equal("\006\003" << 82.chr << "\202\001", encode_object_id([2,2,257]))
232
+ assert_raise(InvalidObjectId) { encode_object_id([3,2,257]) }
233
+ assert_raise(InvalidObjectId) { encode_object_id([]) }
234
+ end
235
+ end
236
+
@@ -0,0 +1,192 @@
1
+ require 'test/unit'
2
+ require 'snmp/manager'
3
+
4
+ class EchoTransport
5
+ def initialize(host, port)
6
+ end
7
+
8
+ def close
9
+ end
10
+
11
+ def send(data)
12
+ @data = data
13
+ end
14
+
15
+ def recv(max_bytes)
16
+ @data[0,max_bytes]
17
+ end
18
+ end
19
+
20
+ class TestManager < Test::Unit::TestCase
21
+
22
+ include SNMP
23
+
24
+ def setup
25
+ @manager = Manager.new(:Transport => EchoTransport)
26
+ end
27
+
28
+ def teardown
29
+ @manager.close
30
+ end
31
+
32
+ def test_defaults
33
+ assert_equal('localhost', @manager.config[:Host])
34
+ assert_equal(161, @manager.config[:Port])
35
+ assert_equal('public', @manager.config[:WriteCommunity])
36
+ end
37
+
38
+ def test_community
39
+ @manager = Manager.new(:WriteCommunity=>'private', :Transport => EchoTransport)
40
+ assert_equal('public', @manager.config[:Community])
41
+ assert_equal('private', @manager.config[:WriteCommunity])
42
+
43
+ @manager = Manager.new(:Community=>'test', :Transport => EchoTransport)
44
+ assert_equal('test', @manager.config[:Community])
45
+ assert_equal('test', @manager.config[:WriteCommunity])
46
+
47
+ @manager = Manager.new(:Community=>'test', :WriteCommunity=>'private',
48
+ :Transport => EchoTransport)
49
+ assert_equal('test', @manager.config[:Community])
50
+ assert_equal('private', @manager.config[:WriteCommunity])
51
+ end
52
+
53
+ def test_get
54
+ response = @manager.get("1.2.3.4")
55
+ assert_equal("1.2.3.4", response.varbind_list.first.name.to_s)
56
+
57
+ response = @manager.get([ObjectId.new("1.2.3.4"), ObjectId.new("1.2.3.4.5")])
58
+ assert_equal(ObjectId.new("1.2.3.4.5"), response.varbind_list[1].name)
59
+ end
60
+
61
+ def test_get_next
62
+ response = @manager.get_next("1.2.3.4")
63
+ assert_equal("1.2.3.4", response.varbind_list.first.name.to_s)
64
+
65
+ response = @manager.get_next([ObjectId.new("1.2.3.4"), ObjectId.new("1.2.3.4.5")])
66
+ assert_equal(ObjectId.new("1.2.3.4.5"), response.varbind_list[1].name)
67
+ end
68
+
69
+ def test_set
70
+ v0 = VarBind.new("1.3.6.1.3.1.1.1.0", OctetString.new("Hello1"))
71
+ v1 = VarBind.new("1.3.6.1.3.1.1.1.0", OctetString.new("Hello2"))
72
+ response = @manager.set([v0, v1])
73
+ assert_equal("Hello1", response.varbind_list[0].value.to_s)
74
+ assert_equal("Hello2", response.varbind_list[1].value.to_s)
75
+ end
76
+
77
+ def test_single_set
78
+ varbind = VarBind.new("1.3.6.1.3.1.1.1.0", OctetString.new("Hello"))
79
+ response = @manager.set(varbind)
80
+ assert_equal("Hello", response.varbind_list.first.value.to_s)
81
+ end
82
+
83
+ def test_get_bulk
84
+ response = @manager.get_bulk(1, 2, ["1.3.6.1.3.1.1.1.0", "1.3.6.1.3.1.1.2.0"])
85
+ assert(1, response.error_status)
86
+ assert(2, response.error_index)
87
+ assert(2, response.varbind_list.length)
88
+ assert_equal("1.3.6.1.3.1.1.1.0", response.varbind_list[0].name.to_s)
89
+ assert_equal("1.3.6.1.3.1.1.2.0", response.varbind_list[1].name.to_s)
90
+ end
91
+
92
+ def test_request_id
93
+ srand(100)
94
+ id = RequestId.new
95
+ assert_equal(186422792, id.next)
96
+
97
+ id.force_next(1)
98
+ assert_equal(1, id.next)
99
+
100
+ id.force_next(RequestId::MAX_REQUEST_ID-1)
101
+ assert_equal(RequestId::MAX_REQUEST_ID-1, id.next)
102
+
103
+ assert_raise(RuntimeError) { id.force_next(RequestId::MAX_REQUEST_ID) }
104
+ assert_raise(RuntimeError) { id.force_next(0) }
105
+ end
106
+ end
107
+
108
+ class TrapTestTransport
109
+ include SNMP
110
+ def initialize(port)
111
+ @count = 0
112
+ sys_up_varbind = VarBind.new(ObjectId.new("1.3.6.1.2.1.1.3.0"),
113
+ TimeTicks.new(1234))
114
+ trap_oid_varbind = VarBind.new(ObjectId.new("1.3.6.1.6.3.1.1.4.1.0"),
115
+ ObjectId.new("1.2.3"))
116
+ trap = SNMPv2_Trap.new(42, VarBindList.new([sys_up_varbind, trap_oid_varbind]))
117
+ message = Message.new(:SNMPv2c, "public", trap)
118
+ @data = message.encode
119
+ end
120
+
121
+ def close
122
+ end
123
+
124
+ def recvfrom(max_bytes)
125
+ @count += 1
126
+ case @count
127
+ when 1
128
+ return @data, "127.0.0.1"
129
+ when 2
130
+ Thread.exit
131
+ else
132
+ raise "Huh?"
133
+ end
134
+ end
135
+ end
136
+
137
+
138
+ class TestTrapListener < Test::Unit::TestCase
139
+ include SNMP
140
+
141
+ def test_init_no_handlers
142
+ init_called = false
143
+ m = TrapListener.new(:ServerTransport => TrapTestTransport) do |manager|
144
+ init_called = true
145
+ end
146
+ m.join
147
+ assert(init_called)
148
+ end
149
+
150
+ def test_v2c_handler
151
+ default_called = false
152
+ v2c_called = false
153
+ m = TrapListener.new(:ServerTransport => TrapTestTransport) do |manager|
154
+ manager.on_trap_default { default_called = true }
155
+ manager.on_trap_v2c { v2c_called = true }
156
+ end
157
+ m.join
158
+ assert(!default_called)
159
+ assert(v2c_called)
160
+ end
161
+
162
+ def test_oid_handler
163
+ default_called = false
164
+ v2c_called = false
165
+ oid_called = false
166
+ m = TrapListener.new(:ServerTransport => TrapTestTransport) do |manager|
167
+ manager.on_trap("1.2.3") do |trap|
168
+ assert_equal(ObjectId.new("1.2.3"), trap.trap_oid)
169
+ assert_equal("127.0.0.1", trap.source_ip)
170
+ oid_called = true
171
+ end
172
+ manager.on_trap_default { default_called = true }
173
+ manager.on_trap_v2c { v2c_called = true }
174
+ end
175
+ m.join
176
+ assert(!default_called)
177
+ assert(!v2c_called)
178
+ assert(oid_called)
179
+ end
180
+
181
+ def test_reject_wrong_community
182
+ default_called = false
183
+ m = TrapListener.new(
184
+ :Community => "test",
185
+ :ServerTransport => TrapTestTransport) do |manager|
186
+ manager.on_trap_default { default_called = true }
187
+ end
188
+ m.join
189
+ assert(!default_called)
190
+ end
191
+
192
+ end
@@ -0,0 +1,66 @@
1
+ require 'snmp/mib'
2
+ require 'test/unit'
3
+
4
+ module SNMP
5
+
6
+ class TestMib < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @mib = MIB.new
10
+ @mib.load_module("SNMPv2-MIB")
11
+ @mib.load_module("IF-MIB")
12
+ end
13
+
14
+ def test_name_to_oid
15
+ oid = @mib.oid("1.2.3.4")
16
+ assert_equal("1.2.3.4", oid.to_s)
17
+
18
+ oid = @mib.oid("ifTable")
19
+ assert_equal("1.3.6.1.2.1.2.2", oid.to_s)
20
+
21
+ oid = @mib.oid("sysDescr.0")
22
+ assert_equal("1.3.6.1.2.1.1.1.0", oid.to_s)
23
+
24
+ oid = @mib.oid("ifTable.1.23")
25
+ assert_equal("1.3.6.1.2.1.2.2.1.23", oid.to_s)
26
+
27
+ oid = @mib.oid("IF-MIB::ifTable.1.23")
28
+ assert_equal("1.3.6.1.2.1.2.2.1.23", oid.to_s)
29
+
30
+ assert_raise(MIB::ModuleNotLoadedError) {
31
+ @mib.oid("IFMIB::ifTable.1.23")
32
+ }
33
+
34
+ assert_raise(ArgumentError) {
35
+ @mib.oid("IF-MIB::")
36
+ }
37
+ end
38
+
39
+ def test_varbind_list
40
+ vb_list = @mib.varbind_list("1.2.3.4")
41
+ assert_equal(1, vb_list.length)
42
+ assert_equal("1.2.3.4", vb_list.first.name.to_s)
43
+
44
+ vb_list = @mib.varbind_list(["IF-MIB::ifTable.1.23", "1.2.3.4"])
45
+ assert_equal(2, vb_list.length)
46
+ assert_equal("1.3.6.1.2.1.2.2.1.23", vb_list.first.name.to_s)
47
+ end
48
+
49
+ def test_varbind
50
+ vb = @mib.varbind("1.2.3.4", Null)
51
+ assert_equal("1.2.3.4", vb.name.to_s)
52
+ assert_equal(Null, vb.value)
53
+
54
+ vb = @mib.varbind("IF-MIB::ifTable.3.45", Integer.new(2))
55
+ assert_equal("1.3.6.1.2.1.2.2.3.45", vb.name.to_s)
56
+ assert_equal(2, vb.value)
57
+ end
58
+
59
+ def test_list
60
+ list = MIB.list_imported(/SNMPv2/)
61
+ assert_equal(4, list.length)
62
+ end
63
+
64
+ end
65
+
66
+ end