PageRenderTime 71ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Messaging/Tests/GetRequestMessageTestFixture.cs

#
C# | 350 lines | 302 code | 22 blank | 26 comment | 1 complexity | 458d935de6daff30e2a7a1a320952036 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. /*
  2. * Created by SharpDevelop.
  3. * User: lextm
  4. * Date: 2008/4/28
  5. * Time: 18:35
  6. *
  7. * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8. */
  9. using System.Collections.Generic;
  10. using System.Net;
  11. using Lextm.SharpSnmpLib.Security;
  12. using NUnit.Framework;
  13. using System.Net.Sockets;
  14. using System;
  15. #pragma warning disable 1591
  16. namespace Lextm.SharpSnmpLib.Messaging.Tests
  17. {
  18. /// <summary>
  19. /// Description of TestGetMessage.
  20. /// </summary>
  21. [TestFixture]
  22. public class GetRequestMessageTestFixture
  23. {
  24. [Test]
  25. public void Test()
  26. {
  27. byte[] expected = Properties.Resources.get;
  28. ISnmpMessage message = MessageFactory.ParseMessages(expected, new UserRegistry())[0];
  29. Assert.AreEqual(SnmpType.GetRequestPdu, message.TypeCode());
  30. GetRequestPdu pdu = (GetRequestPdu)message.Pdu();
  31. Assert.AreEqual(1, pdu.Variables.Count);
  32. Variable v = pdu.Variables[0];
  33. Assert.AreEqual(new uint[] { 1, 3, 6, 1, 2, 1, 1, 6, 0 }, v.Id.ToNumerical());
  34. Assert.AreEqual(typeof(Null), v.Data.GetType());
  35. Assert.GreaterOrEqual(expected.Length, message.ToBytes().Length);
  36. }
  37. [Test]
  38. public void TestConstructor()
  39. {
  40. List<Variable> list = new List<Variable>(1)
  41. {
  42. new Variable(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 2, 1, 1, 6, 0}),
  43. new Null())
  44. };
  45. GetRequestMessage message = new GetRequestMessage(0, VersionCode.V2, new OctetString("public"), list);
  46. Assert.GreaterOrEqual(Properties.Resources.get.Length, message.ToBytes().Length);
  47. }
  48. [Test]
  49. public void TestConstructorV3Auth1()
  50. {
  51. const string bytes = "30 73" +
  52. "02 01 03 " +
  53. "30 0F " +
  54. "02 02 35 41 " +
  55. "02 03 00 FF E3" +
  56. "04 01 05" +
  57. "02 01 03" +
  58. "04 2E " +
  59. "30 2C" +
  60. "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " +
  61. "02 01 0D " +
  62. "02 01 57 " +
  63. "04 05 6C 65 78 6C 69 " +
  64. "04 0C 1C 6D 67 BF B2 38 ED 63 DF 0A 05 24 " +
  65. "04 00 " +
  66. "30 2D " +
  67. "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " +
  68. "04 00 " +
  69. "A0 1A 02 02 01 AF 02 01 00 02 01 00 30 0E 30 0C 06 08 2B 06 01 02 01 01 03 00 05 00";
  70. ReportMessage report = new ReportMessage(
  71. VersionCode.V3,
  72. new Header(
  73. new Integer32(13633),
  74. new Integer32(0xFFE3),
  75. 0),
  76. new SecurityParameters(
  77. new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")),
  78. new Integer32(0x0d),
  79. new Integer32(0x57),
  80. new OctetString("lexli"),
  81. new OctetString(new byte[12]),
  82. OctetString.Empty),
  83. new Scope(
  84. new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")),
  85. OctetString.Empty,
  86. new ReportPdu(
  87. 0x01AF,
  88. ErrorCode.NoError,
  89. 0,
  90. new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) })),
  91. DefaultPrivacyProvider.DefaultPair);
  92. IPrivacyProvider privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass")));
  93. GetRequestMessage request = new GetRequestMessage(
  94. VersionCode.V3,
  95. 13633,
  96. 0x01AF,
  97. new OctetString("lexli"),
  98. new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) },
  99. privacy,
  100. Messenger.MaxMessageSize,
  101. report);
  102. Assert.AreEqual(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel);
  103. Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
  104. }
  105. [Test]
  106. public void TestConstructorV2AuthMd5PrivDes()
  107. {
  108. const string bytes = "30 81 80 02 01 03 30 0F 02 02 6C 99 02 03 00 FF" +
  109. "E3 04 01 07 02 01 03 04 38 30 36 04 0D 80 00 1F" +
  110. "88 80 E9 63 00 00 D6 1F F4 49 02 01 14 02 01 35" +
  111. "04 07 6C 65 78 6D 61 72 6B 04 0C 80 50 D9 A1 E7" +
  112. "81 B6 19 80 4F 06 C0 04 08 00 00 00 01 44 2C A3" +
  113. "B5 04 30 4B 4F 10 3B 73 E1 E4 BD 91 32 1B CB 41" +
  114. "1B A1 C1 D1 1D 2D B7 84 16 CA 41 BF B3 62 83 C4" +
  115. "29 C5 A4 BC 32 DA 2E C7 65 A5 3D 71 06 3C 5B 56" +
  116. "FB 04 A4";
  117. MD5AuthenticationProvider auth = new MD5AuthenticationProvider(new OctetString("testpass"));
  118. IPrivacyProvider privacy = new DESPrivacyProvider(new OctetString("passtest"), auth);
  119. GetRequestMessage request = new GetRequestMessage(
  120. VersionCode.V3,
  121. new Header(
  122. new Integer32(0x6C99),
  123. new Integer32(0xFFE3),
  124. Levels.Authentication | Levels.Privacy | Levels.Reportable),
  125. new SecurityParameters(
  126. new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")),
  127. new Integer32(0x14),
  128. new Integer32(0x35),
  129. new OctetString("lexmark"),
  130. new OctetString(ByteTool.Convert("80 50 D9 A1 E7 81 B6 19 80 4F 06 C0")),
  131. new OctetString(ByteTool.Convert("00 00 00 01 44 2C A3 B5"))),
  132. new Scope(
  133. new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")),
  134. OctetString.Empty,
  135. new GetRequestPdu(
  136. 0x3A25,
  137. new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) })),
  138. privacy);
  139. Assert.AreEqual(Levels.Authentication | Levels.Privacy | Levels.Reportable, request.Header.SecurityLevel);
  140. Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
  141. }
  142. [Test]
  143. public void TestConstructorV3AuthMd5()
  144. {
  145. const string bytes = "30 73" +
  146. "02 01 03 " +
  147. "30 0F " +
  148. "02 02 35 41 " +
  149. "02 03 00 FF E3" +
  150. "04 01 05" +
  151. "02 01 03" +
  152. "04 2E " +
  153. "30 2C" +
  154. "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " +
  155. "02 01 0D " +
  156. "02 01 57 " +
  157. "04 05 6C 65 78 6C 69 " +
  158. "04 0C 1C 6D 67 BF B2 38 ED 63 DF 0A 05 24 " +
  159. "04 00 " +
  160. "30 2D " +
  161. "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " +
  162. "04 00 " +
  163. "A0 1A 02 02 01 AF 02 01 00 02 01 00 30 0E 30 0C 06 08 2B 06 01 02 01 01 03 00 05 00";
  164. IPrivacyProvider pair = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass")));
  165. GetRequestMessage request = new GetRequestMessage(
  166. VersionCode.V3,
  167. new Header(
  168. new Integer32(13633),
  169. new Integer32(0xFFE3),
  170. Levels.Authentication | Levels.Reportable),
  171. new SecurityParameters(
  172. new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")),
  173. new Integer32(0x0d),
  174. new Integer32(0x57),
  175. new OctetString("lexli"),
  176. new OctetString(ByteTool.Convert("1C 6D 67 BF B2 38 ED 63 DF 0A 05 24")),
  177. OctetString.Empty),
  178. new Scope(
  179. new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")),
  180. OctetString.Empty,
  181. new GetRequestPdu(
  182. 0x01AF,
  183. new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null()) })),
  184. pair);
  185. Assert.AreEqual(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel);
  186. Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
  187. }
  188. [Test]
  189. public void TestConstructorV3AuthSha()
  190. {
  191. const string bytes = "30 77 02 01 03 30 0F 02 02 47 21 02 03 00 FF E3" +
  192. "04 01 05 02 01 03 04 32 30 30 04 0D 80 00 1F 88" +
  193. "80 E9 63 00 00 D6 1F F4 49 02 01 15 02 02 01 5B" +
  194. "04 08 6C 65 78 74 75 64 69 6F 04 0C 7B 62 65 AE" +
  195. "D3 8F E3 7D 58 45 5C 6C 04 00 30 2D 04 0D 80 00" +
  196. "1F 88 80 E9 63 00 00 D6 1F F4 49 04 00 A0 1A 02" +
  197. "02 56 FF 02 01 00 02 01 00 30 0E 30 0C 06 08 2B" +
  198. "06 01 02 01 01 03 00 05 00";
  199. IPrivacyProvider pair = new DefaultPrivacyProvider(new SHA1AuthenticationProvider(new OctetString("password")));
  200. GetRequestMessage request = new GetRequestMessage(
  201. VersionCode.V3,
  202. new Header(
  203. new Integer32(0x4721),
  204. new Integer32(0xFFE3),
  205. Levels.Authentication | Levels.Reportable),
  206. new SecurityParameters(
  207. new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")),
  208. new Integer32(0x15),
  209. new Integer32(0x015B),
  210. new OctetString("lextudio"),
  211. new OctetString(ByteTool.Convert("7B 62 65 AE D3 8F E3 7D 58 45 5C 6C")),
  212. OctetString.Empty),
  213. new Scope(
  214. new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")),
  215. OctetString.Empty,
  216. new GetRequestPdu(
  217. 0x56FF,
  218. new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null()) })),
  219. pair);
  220. Assert.AreEqual(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel);
  221. Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
  222. }
  223. [Test]
  224. public void TestDiscoveryV3()
  225. {
  226. const string bytes = "30 3A 02 01 03 30 0F 02 02 6A 09 02 03 00 FF E3" +
  227. " 04 01 04 02 01 03 04 10 30 0E 04 00 02 01 00 02" +
  228. " 01 00 04 00 04 00 04 00 30 12 04 00 04 00 A0 0C" +
  229. " 02 02 2C 6B 02 01 00 02 01 00 30 00";
  230. GetRequestMessage request = new GetRequestMessage(
  231. VersionCode.V3,
  232. new Header(
  233. new Integer32(0x6A09),
  234. new Integer32(0xFFE3),
  235. Levels.Reportable),
  236. new SecurityParameters(
  237. OctetString.Empty,
  238. Integer32.Zero,
  239. Integer32.Zero,
  240. OctetString.Empty,
  241. OctetString.Empty,
  242. OctetString.Empty),
  243. new Scope(
  244. OctetString.Empty,
  245. OctetString.Empty,
  246. new GetRequestPdu(0x2C6B, new List<Variable>())),
  247. DefaultPrivacyProvider.DefaultPair
  248. );
  249. string test = ByteTool.Convert(request.ToBytes());
  250. Assert.AreEqual(bytes, test);
  251. }
  252. [Test]
  253. public void TestToBytes()
  254. {
  255. const string s = "30 27 02 01 01 04 06 70 75 62 6C 69 63 A0 1A 02" +
  256. "02 4B ED 02 01 00 02 01 00 30 0E 30 0C 06 08 2B" +
  257. "06 01 02 01 01 01 00 05 00 ";
  258. byte[] expected = ByteTool.Convert(s);
  259. GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List<Variable> { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) });
  260. Assert.AreEqual(expected, message.ToBytes());
  261. }
  262. [Test]
  263. [Category("External")]
  264. public void TestTimeOutAsync()
  265. {
  266. // IMPORTANT: this test case requires a local SNMP agent such as
  267. // #SNMP Agent (snmpd),
  268. // Windows SNMP agent service,
  269. // Net-SNMP agent, or
  270. // snmp4j agent.
  271. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  272. GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List<Variable> { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) });
  273. var users = new UserRegistry();
  274. var ar2 = message.BeginGetResponse(new IPEndPoint(IPAddress.Loopback, 161), users, socket, null, null);
  275. var response = message.EndGetResponse(ar2);
  276. Assert.AreEqual(SnmpType.ResponsePdu, response.TypeCode());
  277. }
  278. [Test]
  279. [Category("External")]
  280. public void TestTimeOut()
  281. {
  282. // IMPORTANT: this test case requires a local SNMP agent such as
  283. // #SNMP Agent (snmpd),
  284. // Windows SNMP agent service,
  285. // Net-SNMP agent, or
  286. // snmp4j agent.
  287. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  288. GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List<Variable> { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) });
  289. const int time = 1500;
  290. bool hasException = false;
  291. try
  292. {
  293. message.GetResponse(time, new IPEndPoint(IPAddress.Loopback, 161), socket);
  294. }
  295. catch (TimeoutException)
  296. {
  297. hasException = true;
  298. }
  299. Assert.IsFalse(hasException);
  300. System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
  301. try
  302. {
  303. timer.Start();
  304. //IMPORTANT: test against an agent that doesn't exist.
  305. // ReSharper disable AssignNullToNotNullAttribute
  306. message.GetResponse(time, new IPEndPoint(IPAddress.Parse("192.168.0.233"), 161), socket);
  307. // ReSharper restore AssignNullToNotNullAttribute
  308. }
  309. catch (TimeoutException)
  310. {
  311. hasException = true;
  312. }
  313. timer.Stop();
  314. long elapsedMilliseconds = timer.ElapsedMilliseconds;
  315. Console.WriteLine(@"elapsed: " + elapsedMilliseconds);
  316. Console.WriteLine(@"timeout: " + time);
  317. Assert.LessOrEqual(time, elapsedMilliseconds);
  318. Assert.IsTrue(hasException);
  319. // FIXME: these values are valid on my machine openSUSE 11.2. (lex)
  320. // This test case usually fails on Windows, as strangely WinSock API call adds an extra 500-ms.
  321. if (SnmpMessageExtension.IsRunningOnMono)
  322. {
  323. Assert.LessOrEqual(elapsedMilliseconds, time + 100);
  324. }
  325. }
  326. }
  327. }
  328. #pragma warning restore 1591