From f5a39aed3d4ad03fc6d71ba1834beab19566b38f Mon Sep 17 00:00:00 2001 From: Lex Li Date: Sun, 23 Apr 2017 00:37:46 -0400 Subject: [PATCH] Corrected a few exceptions in Messenger class. Revised TRAP v2 in SNMP v3 processing in pipeline. Removed AuthenticationProviderExtension. Added new integration test cases. --- .../Messaging/GetBulkRequestMessage.cs | 2 +- .../Messaging/GetNextRequestMessage.cs | 30 +- SharpSnmpLib/Messaging/GetRequestMessage.cs | 2 +- .../Messaging/InformRequestMessage.cs | 30 +- SharpSnmpLib/Messaging/MessageFactory.cs | 2 +- SharpSnmpLib/Messaging/Messenger.cs | 28 +- SharpSnmpLib/Messaging/ReportMessage.cs | 10 +- SharpSnmpLib/Messaging/ResponseMessage.cs | 2 +- SharpSnmpLib/Messaging/SetRequestMessage.cs | 2 +- SharpSnmpLib/Messaging/TrapV2Message.cs | 2 +- SharpSnmpLib/Pipeline/SecureSnmpContext.cs | 18 +- .../Pipeline/Version3MembershipProvider.cs | 5 - SharpSnmpLib/Security/AESPrivacyProvider.cs | 60 +- .../AuthenticationProviderExtension.cs | 136 --- SharpSnmpLib/Security/DESPrivacyProvider.cs | 72 +- .../Security/DefaultPrivacyProvider.cs | 8 +- SharpSnmpLib/Security/IPrivacyProvider.cs | 6 + .../Security/PrivacyProviderExtension.cs | 97 +++ SharpSnmpLib/SharpSnmpLib.Portable.csproj | 1 - Tests/Integration/DaemonTestFixture.cs | 402 +++++++++ Tests/Integration/TrapDaemonTestFixture.cs | 191 +++++ .../Tests/GetRequestMessageTestFixture.cs | 779 ------------------ Tests/Properties/Resources.Designer.cs | 2 +- ...henticationProviderExtensionTestFixture.cs | 37 - ...efaultAuthenticationProviderTestFixture.cs | 32 - Tests/Tests.NetStandard.csproj | 2 +- Tests/Tests.csproj | 202 +++-- Tests/{Tests => Unit}/ByteToolTestFixture.cs | 0 Tests/{Tests => Unit}/Counter32TestFixture.cs | 0 Tests/{Tests => Unit}/Counter64TestFixture.cs | 0 .../{Tests => Unit}/DataFactoryTestFixture.cs | 0 .../EndOfMibViewTestFixture.cs | 0 Tests/{Tests => Unit}/Gauge32TestFixture.cs | 0 .../GetBulkRequestPduTestFixture.cs | 0 .../GetNextRequestPduTestFixture.cs | 0 .../GetRequestPduTestFixture.cs | 0 Tests/{Tests => Unit}/HeaderTestFixture.cs | 0 Tests/{Tests => Unit}/IPTestFixture.cs | 0 .../InformRequestPduTestFixture.cs | 0 Tests/{Tests => Unit}/Integer32TestFixture.cs | 0 .../MalformedPduTestFixture.cs | 0 .../Messaging/GetRequestMessageTestFixture.cs | 287 +++++++ .../Messaging}/ListenerBindingTestFixture.cs | 0 .../Messaging}/MessageFactoryTestFixture.cs | 0 .../Messaging}/NumberGeneratorTestFixture.cs | 0 .../Messaging}/ResponseMessageTestFixture.cs | 0 .../Messaging}/TrapV1MessageTestFixture.cs | 0 .../Messaging}/TrapV2MessageTestFixture.cs | 6 +- .../NoSuchInstanceTestFixture.cs | 0 .../NoSuchObjectTestFixture.cs | 0 Tests/{Tests => Unit}/NullTestFixture.cs | 0 .../ObjectIdentifierTestFixture.cs | 0 .../Objects}/SysContactTestFixture.cs | 0 .../Objects}/SysDescrTestFixture.cs | 0 .../Objects}/SysLocationTestFixture.cs | 0 .../Objects}/SysNameTestFixture.cs | 0 .../Objects}/SysORDescrTestFixture.cs | 0 .../Objects}/SysORIDTestFixture.cs | 0 .../Objects}/SysORIndexTestFixture.cs | 0 .../Objects}/SysORLastChangeTestFixture.cs | 0 .../Objects}/SysORTableTestFixture.cs | 0 .../Objects}/SysORUpTimeTestFixture.cs | 0 .../Objects}/SysObjectIdTestFixture.cs | 0 .../Objects}/SysServicesTestFixture.cs | 0 .../Objects}/SysUpTimeTestFixture.cs | 0 .../{Tests => Unit}/OctetStringTestFixture.cs | 0 Tests/{Tests => Unit}/OpaqueTestFixture.cs | 0 .../Pipeline}/EngineGroupTestFixture.cs | 0 .../GetBulkMessageHandlerTestFixture.cs | 0 .../Pipeline}/GetMessageHandlerTestFixture.cs | 0 .../GetNextMessageHandlerTestFixture.cs | 0 .../GetNextV1MessageHandlerTestFixture.cs | 0 .../GetV1MessageHandlerTestFixture.cs | 0 .../InformRequestMessageHandlerTestFixture.cs | 0 ...uestMessageReceivedEventArgsTestFixture.cs | 0 .../Pipeline}/NormalSnmpContextTestFixture.cs | 0 .../Pipeline}/SetMessageHandlerTestFixture.cs | 0 .../SetV1MessageHandlerTestFixture.cs | 0 .../SnmpContextFactoryTestFixture.cs | 0 .../TrapV1MessageHandlerTestFixture.cs | 0 ...apV1MessageReceivedEventArgsTestFixture.cs | 0 .../TrapV2MessageHandlerTestFixture.cs | 0 ...apV2MessageReceivedEventArgsTestFixture.cs | 0 Tests/{Tests => Unit}/ReportPduTestFixture.cs | 0 .../{Tests => Unit}/ResponsePduTestFixture.cs | 0 Tests/{Tests => Unit}/ScopeTestFixture.cs | 0 .../AESPrivacyProviderTestFixture.cs | 0 .../DESPrivacyProviderTestFixture.cs | 0 ...efaultAuthenticationProviderTestFixture.cs | 26 + .../DefaultPrivacyProviderTestFixture.cs | 0 .../Security}/LevelsTestFixture.cs | 0 .../MD5AuthenticationProviderTestFixture.cs | 5 - .../PrivacyProviderExtensionTestFixture.cs | 0 .../SHA1AuthenticationProviderTestFixture.cs | 5 - .../Security}/SaltGeneratorTestFixture.cs | 0 .../Security}/UserRegistryTestFixture.cs | 0 .../Security}/UserTestFixture.cs | 0 .../SecurityParametersTestFixture.cs | 0 Tests/{Tests => Unit}/SequenceTestFixture.cs | 0 .../SetRequestPduTestFixture.cs | 0 .../SnmpDataExtensionTestFixture.cs | 0 .../StreamExtensionTestFixture.cs | 0 Tests/{Tests => Unit}/TimeticksTestFixture.cs | 0 Tests/{Tests => Unit}/TrapV1PduTestFixture.cs | 0 Tests/{Tests => Unit}/TrapV2PduTestFixture.cs | 0 Tests/{Tests => Unit}/VariableTestFixture.cs | 0 Tests/app.config | 12 +- Tests/packages.config | 44 - lib/SharedAssemblyInfo.cs | 4 +- lib/SharedAssemblyInfo.vb | 4 +- 110 files changed, 1276 insertions(+), 1277 deletions(-) delete mode 100644 SharpSnmpLib/Security/AuthenticationProviderExtension.cs create mode 100644 Tests/Integration/DaemonTestFixture.cs create mode 100644 Tests/Integration/TrapDaemonTestFixture.cs delete mode 100644 Tests/Messaging/Tests/GetRequestMessageTestFixture.cs delete mode 100644 Tests/Security/Tests/AuthenticationProviderExtensionTestFixture.cs delete mode 100644 Tests/Security/Tests/DefaultAuthenticationProviderTestFixture.cs rename Tests/{Tests => Unit}/ByteToolTestFixture.cs (100%) rename Tests/{Tests => Unit}/Counter32TestFixture.cs (100%) rename Tests/{Tests => Unit}/Counter64TestFixture.cs (100%) rename Tests/{Tests => Unit}/DataFactoryTestFixture.cs (100%) rename Tests/{Tests => Unit}/EndOfMibViewTestFixture.cs (100%) rename Tests/{Tests => Unit}/Gauge32TestFixture.cs (100%) rename Tests/{Tests => Unit}/GetBulkRequestPduTestFixture.cs (100%) rename Tests/{Tests => Unit}/GetNextRequestPduTestFixture.cs (100%) rename Tests/{Tests => Unit}/GetRequestPduTestFixture.cs (100%) rename Tests/{Tests => Unit}/HeaderTestFixture.cs (100%) rename Tests/{Tests => Unit}/IPTestFixture.cs (100%) rename Tests/{Tests => Unit}/InformRequestPduTestFixture.cs (100%) rename Tests/{Tests => Unit}/Integer32TestFixture.cs (100%) rename Tests/{Tests => Unit}/MalformedPduTestFixture.cs (100%) create mode 100644 Tests/Unit/Messaging/GetRequestMessageTestFixture.cs rename Tests/{Messaging/Tests => Unit/Messaging}/ListenerBindingTestFixture.cs (100%) rename Tests/{Messaging/Tests => Unit/Messaging}/MessageFactoryTestFixture.cs (100%) rename Tests/{Messaging/Tests => Unit/Messaging}/NumberGeneratorTestFixture.cs (100%) rename Tests/{Messaging/Tests => Unit/Messaging}/ResponseMessageTestFixture.cs (100%) rename Tests/{Messaging/Tests => Unit/Messaging}/TrapV1MessageTestFixture.cs (100%) rename Tests/{Messaging/Tests => Unit/Messaging}/TrapV2MessageTestFixture.cs (98%) rename Tests/{Tests => Unit}/NoSuchInstanceTestFixture.cs (100%) rename Tests/{Tests => Unit}/NoSuchObjectTestFixture.cs (100%) rename Tests/{Tests => Unit}/NullTestFixture.cs (100%) rename Tests/{Tests => Unit}/ObjectIdentifierTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysContactTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysDescrTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysLocationTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysNameTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysORDescrTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysORIDTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysORIndexTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysORLastChangeTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysORTableTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysORUpTimeTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysObjectIdTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysServicesTestFixture.cs (100%) rename Tests/{Objects/Tests => Unit/Objects}/SysUpTimeTestFixture.cs (100%) rename Tests/{Tests => Unit}/OctetStringTestFixture.cs (100%) rename Tests/{Tests => Unit}/OpaqueTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/EngineGroupTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/GetBulkMessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/GetMessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/GetNextMessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/GetNextV1MessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/GetV1MessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/InformRequestMessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/InformRequestMessageReceivedEventArgsTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/NormalSnmpContextTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/SetMessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/SetV1MessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/SnmpContextFactoryTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/TrapV1MessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/TrapV1MessageReceivedEventArgsTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/TrapV2MessageHandlerTestFixture.cs (100%) rename Tests/{Pipeline/Tests => Unit/Pipeline}/TrapV2MessageReceivedEventArgsTestFixture.cs (100%) rename Tests/{Tests => Unit}/ReportPduTestFixture.cs (100%) rename Tests/{Tests => Unit}/ResponsePduTestFixture.cs (100%) rename Tests/{Tests => Unit}/ScopeTestFixture.cs (100%) rename Tests/{Security/Tests => Unit/Security}/AESPrivacyProviderTestFixture.cs (100%) rename Tests/{Security/Tests => Unit/Security}/DESPrivacyProviderTestFixture.cs (100%) create mode 100644 Tests/Unit/Security/DefaultAuthenticationProviderTestFixture.cs rename Tests/{Security/Tests => Unit/Security}/DefaultPrivacyProviderTestFixture.cs (100%) rename Tests/{Security/Tests => Unit/Security}/LevelsTestFixture.cs (100%) rename Tests/{Security/Tests => Unit/Security}/MD5AuthenticationProviderTestFixture.cs (70%) rename Tests/{Security/Tests => Unit/Security}/PrivacyProviderExtensionTestFixture.cs (100%) rename Tests/{Security/Tests => Unit/Security}/SHA1AuthenticationProviderTestFixture.cs (60%) rename Tests/{Security/Tests => Unit/Security}/SaltGeneratorTestFixture.cs (100%) rename Tests/{Security/Tests => Unit/Security}/UserRegistryTestFixture.cs (100%) rename Tests/{Security/Tests => Unit/Security}/UserTestFixture.cs (100%) rename Tests/{Tests => Unit}/SecurityParametersTestFixture.cs (100%) rename Tests/{Tests => Unit}/SequenceTestFixture.cs (100%) rename Tests/{Tests => Unit}/SetRequestPduTestFixture.cs (100%) rename Tests/{Tests => Unit}/SnmpDataExtensionTestFixture.cs (100%) rename Tests/{Tests => Unit}/StreamExtensionTestFixture.cs (100%) rename Tests/{Tests => Unit}/TimeticksTestFixture.cs (100%) rename Tests/{Tests => Unit}/TrapV1PduTestFixture.cs (100%) rename Tests/{Tests => Unit}/TrapV2PduTestFixture.cs (100%) rename Tests/{Tests => Unit}/VariableTestFixture.cs (100%) delete mode 100644 Tests/packages.config diff --git a/SharpSnmpLib/Messaging/GetBulkRequestMessage.cs b/SharpSnmpLib/Messaging/GetBulkRequestMessage.cs index 9ae5cade..9d95acee 100644 --- a/SharpSnmpLib/Messaging/GetBulkRequestMessage.cs +++ b/SharpSnmpLib/Messaging/GetBulkRequestMessage.cs @@ -180,7 +180,7 @@ public GetBulkRequestMessage(VersionCode version, int messageId, int requestId, var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId; Scope = new Scope(contextEngineId, scope.ContextName, pdu); - authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy); + Privacy.ComputeHash(Version, Header, Parameters, Scope); _bytes = this.PackMessage(null).ToBytes(); } diff --git a/SharpSnmpLib/Messaging/GetNextRequestMessage.cs b/SharpSnmpLib/Messaging/GetNextRequestMessage.cs index 2109faa6..c0fb1837 100644 --- a/SharpSnmpLib/Messaging/GetNextRequestMessage.cs +++ b/SharpSnmpLib/Messaging/GetNextRequestMessage.cs @@ -37,9 +37,9 @@ namespace Lextm.SharpSnmpLib.Messaging /// GETNEXT request message. /// public sealed class GetNextRequestMessage : ISnmpMessage - { + { private readonly byte[] _bytes; - + /// /// Creates a with all contents. /// @@ -53,17 +53,17 @@ public GetNextRequestMessage(int requestId, VersionCode version, OctetString com { throw new ArgumentNullException("variables"); } - + if (community == null) { throw new ArgumentNullException("community"); } - + if (version == VersionCode.V3) { throw new ArgumentException("only v1 and v2c are supported", "version"); } - + Version = version; Header = Header.Empty; Parameters = SecurityParameters.Create(community); @@ -109,27 +109,27 @@ public GetNextRequestMessage(VersionCode version, int messageId, int requestId, { throw new ArgumentNullException("variables"); } - + if (userName == null) { throw new ArgumentNullException("userName"); } - + if (version != VersionCode.V3) { throw new ArgumentException("only v3 is supported", "version"); } - + if (report == null) { throw new ArgumentNullException("report"); } - + if (privacy == null) { throw new ArgumentNullException("privacy"); } - + Version = version; Privacy = privacy; @@ -150,7 +150,7 @@ public GetNextRequestMessage(VersionCode version, int messageId, int requestId, var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId; Scope = new Scope(contextEngineId, scope.ContextName, pdu); - authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy); + Privacy.ComputeHash(Version, Header, Parameters, Scope); _bytes = this.PackMessage(null).ToBytes(); } @@ -160,17 +160,17 @@ internal GetNextRequestMessage(VersionCode version, Header header, SecurityParam { throw new ArgumentNullException("scope"); } - + if (parameters == null) { throw new ArgumentNullException("parameters"); } - + if (header == null) { throw new ArgumentNullException("header"); } - + if (privacy == null) { throw new ArgumentNullException("privacy"); @@ -184,7 +184,7 @@ internal GetNextRequestMessage(VersionCode version, Header header, SecurityParam _bytes = this.PackMessage(length).ToBytes(); } - + /// /// Gets the header. /// diff --git a/SharpSnmpLib/Messaging/GetRequestMessage.cs b/SharpSnmpLib/Messaging/GetRequestMessage.cs index 9527dd21..e65f9c9e 100644 --- a/SharpSnmpLib/Messaging/GetRequestMessage.cs +++ b/SharpSnmpLib/Messaging/GetRequestMessage.cs @@ -141,7 +141,7 @@ public GetRequestMessage(VersionCode version, int messageId, int requestId, Octe var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId; Scope = new Scope(contextEngineId, scope.ContextName, pdu); - authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy); + Privacy.ComputeHash(Version, Header, Parameters, Scope); _bytes = this.PackMessage(null).ToBytes(); } diff --git a/SharpSnmpLib/Messaging/InformRequestMessage.cs b/SharpSnmpLib/Messaging/InformRequestMessage.cs index e25274a4..10fee7b3 100644 --- a/SharpSnmpLib/Messaging/InformRequestMessage.cs +++ b/SharpSnmpLib/Messaging/InformRequestMessage.cs @@ -55,22 +55,22 @@ public InformRequestMessage(int requestId, VersionCode version, OctetString comm { throw new ArgumentNullException("variables"); } - + if (enterprise == null) { throw new ArgumentNullException("enterprise"); } - + if (community == null) { throw new ArgumentNullException("community"); } - + if (version == VersionCode.V3) { throw new ArgumentException("only v1 and v2c are supported", "version"); } - + Version = version; Enterprise = enterprise; TimeStamp = time; @@ -102,7 +102,7 @@ public InformRequestMessage(int requestId, VersionCode version, OctetString comm [CLSCompliant(false)] [Obsolete("Please use other overloading ones.")] public InformRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, IList variables, IPrivacyProvider privacy, ISnmpMessage report) - : this(version, messageId, requestId, userName, enterprise, time, variables, privacy, 0xFFE3, report) + : this(version, messageId, requestId, userName, enterprise, time, variables, privacy, 0xFFE3, report) { } @@ -126,12 +126,12 @@ public InformRequestMessage(VersionCode version, int messageId, int requestId, O { throw new ArgumentNullException("userName"); } - + if (variables == null) { throw new ArgumentNullException("variables"); } - + if (version != VersionCode.V3) { throw new ArgumentException("only v3 is supported", "version"); @@ -146,7 +146,7 @@ public InformRequestMessage(VersionCode version, int messageId, int requestId, O { throw new ArgumentNullException("report"); } - + if (privacy == null) { throw new ArgumentNullException("privacy"); @@ -176,7 +176,7 @@ public InformRequestMessage(VersionCode version, int messageId, int requestId, O var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId; Scope = new Scope(contextEngineId, scope.ContextName, pdu); - authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy); + Privacy.ComputeHash(Version, Header, Parameters, Scope); _bytes = this.PackMessage(null).ToBytes(); } @@ -186,17 +186,17 @@ internal InformRequestMessage(VersionCode version, Header header, SecurityParame { throw new ArgumentNullException("scope"); } - + if (parameters == null) { throw new ArgumentNullException("parameters"); } - + if (header == null) { throw new ArgumentNullException("header"); } - + if (privacy == null) { throw new ArgumentNullException("privacy"); @@ -210,7 +210,7 @@ internal InformRequestMessage(VersionCode version, Header header, SecurityParame var pdu = (InformRequestPdu)scope.Pdu; Enterprise = pdu.Enterprise; TimeStamp = pdu.TimeStamp; - + _bytes = this.PackMessage(length).ToBytes(); } @@ -237,12 +237,12 @@ internal InformRequestMessage(VersionCode version, Header header, SecurityParame /// Enterprise. /// public ObjectIdentifier Enterprise { get; private set; } - + /// /// Gets the header. /// public Header Header { get; private set; } - + /// /// Converts to byte format. /// diff --git a/SharpSnmpLib/Messaging/MessageFactory.cs b/SharpSnmpLib/Messaging/MessageFactory.cs index 74d4a76d..b693b19f 100644 --- a/SharpSnmpLib/Messaging/MessageFactory.cs +++ b/SharpSnmpLib/Messaging/MessageFactory.cs @@ -187,7 +187,7 @@ private static ISnmpMessage ParseMessage(int first, Stream stream, UserRegistry throw new SnmpException(string.Format(CultureInfo.InvariantCulture, "invalid v3 packets scoped data: {0}", code)); } - if (!privacy.AuthenticationProvider.VerifyHash(version, header, parameters, body[3], privacy, body.GetLengthBytes())) + if (!privacy.VerifyHash(version, header, parameters, body[3], body.GetLengthBytes())) { parameters.IsInvalid = true; } diff --git a/SharpSnmpLib/Messaging/Messenger.cs b/SharpSnmpLib/Messaging/Messenger.cs index 053e8eb0..5c098962 100644 --- a/SharpSnmpLib/Messaging/Messenger.cs +++ b/SharpSnmpLib/Messaging/Messenger.cs @@ -40,7 +40,7 @@ namespace Lextm.SharpSnmpLib.Messaging /// Static methods in Manager or Agent class will be removed in the future. /// /// - /// SNMP v3 is not supported by this class. Please use derived classes directly + /// SNMP v3 is not supported in many methods of this class. Please use derived classes directly /// if you want to do v3 operations. /// public static class Messenger @@ -313,6 +313,7 @@ public static async Task BulkWalkAsync(VersionCode version, IPEndPoint endp /// Specific code. /// Timestamp. /// Variable bindings. + /// This method only supports SNMP v1. [CLSCompliant(false)] public static async Task SendTrapV1Async(EndPoint receiver, IPAddress agent, OctetString community, ObjectIdentifier enterprise, GenericCode generic, int specific, uint timestamp, IList variables) { @@ -330,12 +331,13 @@ public static async Task SendTrapV1Async(EndPoint receiver, IPAddress agent, Oct /// Timestamp. /// Variable bindings. /// Request ID. + /// This method only supports SNMP v2c. [CLSCompliant(false)] public static async Task SendTrapV2Async(int requestId, VersionCode version, EndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList variables) { if (version != VersionCode.V2) { - throw new ArgumentException("Only SNMP v2c is supported", nameof(version)); + throw new NotSupportedException("Only SNMP v2c is supported"); } var message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables); @@ -354,6 +356,7 @@ public static async Task SendTrapV2Async(int requestId, VersionCode version, End /// Variable bindings. /// The privacy provider. /// The report. + /// This method supports SNMP v2c and v3. [CLSCompliant(false)] public static async Task SendInformAsync(int requestId, VersionCode version, IPEndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList variables, IPrivacyProvider privacy, ISnmpMessage report) { @@ -377,6 +380,11 @@ public static async Task SendInformAsync(int requestId, VersionCode version, IPE throw new ArgumentNullException(nameof(variables)); } + if (version == VersionCode.V1) + { + throw new NotSupportedException("SNMP v1 is not supported"); + } + if (version == VersionCode.V3 && privacy == null) { throw new ArgumentNullException(nameof(privacy)); @@ -436,7 +444,7 @@ private static async Task, ISnmpMessage>> BulkHasNex // TODO: report should be updated with latest message from agent. if (version == VersionCode.V1) { - throw new ArgumentException("v1 is not supported", nameof(version)); + throw new NotSupportedException("SNMP v1 is not supported"); } var variables = new List { new Variable(seed.Id) }; @@ -700,6 +708,11 @@ public static int BulkWalk(VersionCode version, IPEndPoint endpoint, OctetString throw new ArgumentNullException(nameof(list)); } + if (version == VersionCode.V1) + { + throw new NotSupportedException("SNMP v1 is not supported"); + } + var tableV = new Variable(table); var seed = tableV; IList next; @@ -770,7 +783,7 @@ public static void SendTrapV2(int requestId, VersionCode version, EndPoint recei { if (version != VersionCode.V2) { - throw new ArgumentException("Only SNMP v2c is supported", nameof(version)); + throw new NotSupportedException("Only SNMP v2c is supported"); } var message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables); @@ -813,6 +826,11 @@ public static void SendInform(int requestId, VersionCode version, IPEndPoint rec throw new ArgumentNullException(nameof(variables)); } + if (version == VersionCode.V1) + { + throw new NotSupportedException("SNMP v1 is not supported"); + } + if (version == VersionCode.V3 && privacy == null) { throw new ArgumentNullException(nameof(privacy)); @@ -873,7 +891,7 @@ private static bool BulkHasNext(VersionCode version, IPEndPoint receiver, OctetS { if (version == VersionCode.V1) { - throw new ArgumentException("v1 is not supported", nameof(version)); + throw new NotSupportedException("SNMP v1 is not supported"); } var variables = new List { new Variable(seed.Id) }; diff --git a/SharpSnmpLib/Messaging/ReportMessage.cs b/SharpSnmpLib/Messaging/ReportMessage.cs index 2cfc5d3d..a99fcdc7 100644 --- a/SharpSnmpLib/Messaging/ReportMessage.cs +++ b/SharpSnmpLib/Messaging/ReportMessage.cs @@ -53,22 +53,22 @@ public ReportMessage(VersionCode version, Header header, SecurityParameters para { throw new ArgumentNullException("scope"); } - + if (parameters == null) { throw new ArgumentNullException("parameters"); } - + if (header == null) { throw new ArgumentNullException("header"); } - + if (privacy == null) { throw new ArgumentNullException("privacy"); } - + if (version != VersionCode.V3) { throw new ArgumentException("only v3 is supported", "version"); @@ -79,7 +79,7 @@ public ReportMessage(VersionCode version, Header header, SecurityParameters para Parameters = parameters; Scope = scope; Privacy = privacy; - Privacy.AuthenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy); + Privacy.ComputeHash(Version, Header, Parameters, Scope); _bytes = this.PackMessage(length).ToBytes(); } diff --git a/SharpSnmpLib/Messaging/ResponseMessage.cs b/SharpSnmpLib/Messaging/ResponseMessage.cs index dccd745e..7c10180b 100644 --- a/SharpSnmpLib/Messaging/ResponseMessage.cs +++ b/SharpSnmpLib/Messaging/ResponseMessage.cs @@ -119,7 +119,7 @@ public ResponseMessage(VersionCode version, Header header, SecurityParameters pa if (needAuthentication) { - Privacy.AuthenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy); + Privacy.ComputeHash(Version, Header, Parameters, Scope); } _bytes = this.PackMessage(length).ToBytes(); diff --git a/SharpSnmpLib/Messaging/SetRequestMessage.cs b/SharpSnmpLib/Messaging/SetRequestMessage.cs index 55f41f82..7e6a527f 100644 --- a/SharpSnmpLib/Messaging/SetRequestMessage.cs +++ b/SharpSnmpLib/Messaging/SetRequestMessage.cs @@ -141,7 +141,7 @@ public SetRequestMessage(VersionCode version, int messageId, int requestId, Octe var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId; Scope = new Scope(contextEngineId, scope.ContextName, pdu); - authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy); + Privacy.ComputeHash(Version, Header, Parameters, Scope); _bytes = this.PackMessage(null).ToBytes(); } diff --git a/SharpSnmpLib/Messaging/TrapV2Message.cs b/SharpSnmpLib/Messaging/TrapV2Message.cs index e6be0729..4f10d421 100644 --- a/SharpSnmpLib/Messaging/TrapV2Message.cs +++ b/SharpSnmpLib/Messaging/TrapV2Message.cs @@ -148,7 +148,7 @@ public TrapV2Message(VersionCode version, int messageId, int requestId, OctetStr // TODO: may expose engine ID in the future. Scope = new Scope(OctetString.Empty, OctetString.Empty, pdu); - authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy); + Privacy.ComputeHash(Version, Header, Parameters, Scope); _bytes = this.PackMessage(null).ToBytes(); } diff --git a/SharpSnmpLib/Pipeline/SecureSnmpContext.cs b/SharpSnmpLib/Pipeline/SecureSnmpContext.cs index e6c0dda0..39812460 100644 --- a/SharpSnmpLib/Pipeline/SecureSnmpContext.cs +++ b/SharpSnmpLib/Pipeline/SecureSnmpContext.cs @@ -166,7 +166,14 @@ public override bool HandleMembership() var request = Request; var parameters = request.Parameters; var typeCode = Request.TypeCode(); - if (parameters.EngineId != Group.EngineId && typeCode != SnmpType.Unknown) + + if (typeCode == SnmpType.Unknown) + { + HandleFailure(Group.DecryptionError); + return false; + } + + if (typeCode != SnmpType.TrapV2Pdu && parameters.EngineId != Group.EngineId) { HandleDiscovery(); return true; @@ -179,9 +186,9 @@ public override bool HandleMembership() return false; } - if (typeCode == SnmpType.Unknown) + if (typeCode == SnmpType.TrapV2Pdu && parameters.EngineId != user.EngineId) { - HandleFailure(Group.DecryptionError); + HandleFailure(Group.UnknownEngineId); return false; } @@ -191,6 +198,11 @@ public override bool HandleMembership() return false; } + if (typeCode == SnmpType.TrapV2Pdu) + { + return true; + } + if ((user.ToSecurityLevel() | Levels.Reportable) != request.Header.SecurityLevel) { HandleFailure(Group.UnsupportedSecurityLevel); diff --git a/SharpSnmpLib/Pipeline/Version3MembershipProvider.cs b/SharpSnmpLib/Pipeline/Version3MembershipProvider.cs index 427cb4bb..bbdafca6 100644 --- a/SharpSnmpLib/Pipeline/Version3MembershipProvider.cs +++ b/SharpSnmpLib/Pipeline/Version3MembershipProvider.cs @@ -42,11 +42,6 @@ public bool AuthenticateRequest(ISnmpContext context) throw new ArgumentNullException("context"); } - if (context.Request.TypeCode() == SnmpType.TrapV2Pdu) - { - return context.Request.Version == Version; - } - return context.Request.Version == Version && context.HandleMembership(); } } diff --git a/SharpSnmpLib/Security/AESPrivacyProvider.cs b/SharpSnmpLib/Security/AESPrivacyProvider.cs index a845caad..5b7ef2f2 100644 --- a/SharpSnmpLib/Security/AESPrivacyProvider.cs +++ b/SharpSnmpLib/Security/AESPrivacyProvider.cs @@ -41,7 +41,7 @@ namespace Lextm.SharpSnmpLib.Security /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "AES", Justification = "definition")] public sealed class AESPrivacyProvider : IPrivacyProvider - { + { private readonly SaltGenerator _salt = new SaltGenerator(); private readonly OctetString _phrase; private const int KeyBytes = 16; @@ -57,18 +57,18 @@ public AESPrivacyProvider(OctetString phrase, IAuthenticationProvider auth) { throw new ArgumentNullException("auth"); } - + if (phrase == null) { throw new ArgumentNullException("phrase"); } - + // IMPORTANT: in this way privacy cannot be non-default. if (auth == DefaultAuthenticationProvider.Instance) { throw new ArgumentException("if authentication is off, then privacy cannot be used"); } - + _phrase = phrase; AuthenticationProvider = auth; } @@ -77,7 +77,13 @@ public AESPrivacyProvider(OctetString phrase, IAuthenticationProvider auth) /// Corresponding . /// public IAuthenticationProvider AuthenticationProvider { get; private set; } - + + /// + /// Engine ID. + /// + /// This is an optional field, and only used by TRAP v2 authentication. + public OctetString EngineId { get; set; } + /// /// Encrypt scoped PDU using DES encryption protocol /// @@ -97,19 +103,19 @@ internal static byte[] Encrypt(byte[] unencryptedData, byte[] key, int engineBoo { throw new ArgumentNullException("key"); } - + if (key.Length < KeyBytes) { throw new ArgumentOutOfRangeException("key", "Invalid key length"); } - + if (unencryptedData == null) { throw new ArgumentNullException("unencryptedData"); } - + var iv = new byte[16]; - + // Set privacy parameters to the local 64 bit salt value var bootsBytes = BitConverter.GetBytes(engineBoots); iv[0] = bootsBytes[3]; @@ -130,11 +136,11 @@ internal static byte[] Encrypt(byte[] unencryptedData, byte[] key, int engineBoo rm.KeySize = KeyBytes * 8; rm.FeedbackSize = 128; rm.BlockSize = 128; - + // we have to use Zeros padding otherwise we get encrypt buffer size exception rm.Padding = PaddingMode.Zeros; rm.Mode = CipherMode.CFB; - + // make sure we have the right key length var pkey = new byte[MinimumKeyLength]; Buffer.BlockCopy(key, 0, pkey, 0, MinimumKeyLength); @@ -143,7 +149,7 @@ internal static byte[] Encrypt(byte[] unencryptedData, byte[] key, int engineBoo using (var cryptor = rm.CreateEncryptor()) { var encryptedData = cryptor.TransformFinalBlock(unencryptedData, 0, unencryptedData.Length); - + // check if encrypted data is the same length as source data if (encryptedData.Length != unencryptedData.Length) { @@ -152,7 +158,7 @@ internal static byte[] Encrypt(byte[] unencryptedData, byte[] key, int engineBoo Buffer.BlockCopy(encryptedData, 0, tmp, 0, unencryptedData.Length); return tmp; } - + return encryptedData; } } @@ -176,12 +182,12 @@ internal static byte[] Decrypt(byte[] encryptedData, byte[] key, int engineBoots { throw new ArgumentNullException("key"); } - + if (encryptedData == null) { throw new ArgumentNullException("encryptedData"); } - + if (key.Length < KeyBytes) { throw new ArgumentOutOfRangeException("key", "Invalid key length"); @@ -220,7 +226,7 @@ internal static byte[] Decrypt(byte[] encryptedData, byte[] key, int engineBoots { rm.Key = key; } - + rm.IV = iv; using (var cryptor = rm.CreateDecryptor()) { @@ -235,7 +241,7 @@ internal static byte[] Decrypt(byte[] encryptedData, byte[] key, int engineBoots var decryptBuffer = new byte[newLength]; Buffer.BlockCopy(buffer, 0, decryptBuffer, 0, buffer.Length); decryptedData = cryptor.TransformFinalBlock(decryptBuffer, 0, decryptBuffer.Length); - + // now remove padding Buffer.BlockCopy(decryptedData, 0, buffer, 0, encryptedData.Length); return buffer; @@ -246,7 +252,7 @@ internal static byte[] Decrypt(byte[] encryptedData, byte[] key, int engineBoots } } } - + /// /// Returns the length of privacyParameters USM header field. For AES, field length is 8. /// @@ -265,7 +271,7 @@ private static int MinimumKeyLength { get { return 16; } } - + /// /// Return maximum encryption/decryption key length. For DES, returned value is 16 /// @@ -277,7 +283,7 @@ public static int MaximumKeyLength get { return 16; } } - #region IPrivacyProvider Members +#region IPrivacyProvider Members /// /// Decrypts the specified data. @@ -291,17 +297,17 @@ public ISnmpData Decrypt(ISnmpData data, SecurityParameters parameters) { throw new ArgumentNullException("parameters"); } - + if (data == null) { throw new ArgumentNullException("data"); } - + if (data.TypeCode != SnmpType.OctetString) { throw new SnmpException("cannot decrypt the scope data"); } - + var octets = (OctetString)data; var bytes = octets.GetRaw(); var pkey = AuthenticationProvider.PasswordToKey(_phrase.GetRaw(), parameters.EngineId.GetRaw()); @@ -323,7 +329,7 @@ public ISnmpData Encrypt(ISnmpData data, SecurityParameters parameters) { throw new ArgumentNullException("parameters"); } - + var pkey = AuthenticationProvider.PasswordToKey(_phrase.GetRaw(), parameters.EngineId.GetRaw()); var bytes = data.ToBytes(); var reminder = bytes.Length % 8; @@ -338,7 +344,7 @@ public ISnmpData Encrypt(ISnmpData data, SecurityParameters parameters) bytes = stream.ToArray(); } - + var encrypted = Encrypt(bytes, pkey, parameters.EngineBoots.ToInt32(), parameters.EngineTime.ToInt32(), parameters.PrivacyParameters.GetRaw()); return new OctetString(encrypted); } @@ -352,8 +358,8 @@ public OctetString Salt get { return new OctetString(_salt.GetSaltBytes()); } } - #endregion - +#endregion + /// /// Returns a string that represents this object. /// diff --git a/SharpSnmpLib/Security/AuthenticationProviderExtension.cs b/SharpSnmpLib/Security/AuthenticationProviderExtension.cs deleted file mode 100644 index 691aadcc..00000000 --- a/SharpSnmpLib/Security/AuthenticationProviderExtension.cs +++ /dev/null @@ -1,136 +0,0 @@ -// IAuthenticationProvider extension class. -// Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy of this -// software and associated documentation files (the "Software"), to deal in the Software -// without restriction, including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons -// to whom the Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all copies or -// substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -using System; - -namespace Lextm.SharpSnmpLib.Security -{ - /// - /// Authentication provider extension. - /// - public static class AuthenticationProviderExtension - { - /// - /// Computes the hash. - /// - /// The authentication provider. - /// The version. - /// The header. - /// The parameters. - /// The scope. - /// The privacy provider. - public static void ComputeHash(this IAuthenticationProvider provider, VersionCode version, Header header, SecurityParameters parameters, ISegment scope, IPrivacyProvider privacy) - { - if (provider == null) - { - throw new ArgumentNullException("provider"); - } - - if (header == null) - { - throw new ArgumentNullException("header"); - } - - if (parameters == null) - { - throw new ArgumentNullException("parameters"); - } - - if (scope == null) - { - throw new ArgumentNullException("scope"); - } - - if (privacy == null) - { - throw new ArgumentNullException("privacy"); - } - - if (provider is DefaultAuthenticationProvider) - { - return; - } - - if (0 == (header.SecurityLevel & Levels.Authentication)) - { - return; - } - - var scopeData = privacy.GetScopeData(header, parameters, scope.GetData(version)); - parameters.AuthenticationParameters = provider.ComputeHash(version, header, parameters, scopeData, privacy, null); // replace the hash. - } - - /// - /// Verifies the hash. - /// - /// The authentication provider. - /// The version. - /// The header. - /// The parameters. - /// The scope bytes. - /// The privacy provider. - /// The length bytes. - /// - /// Returns true if hash matches. Otherwise, returns false. - /// - public static bool VerifyHash(this IAuthenticationProvider provider, VersionCode version, Header header, SecurityParameters parameters, ISnmpData scopeBytes, IPrivacyProvider privacy, byte[] length) - { - if (provider == null) - { - throw new ArgumentNullException("provider"); - } - - if (header == null) - { - throw new ArgumentNullException("header"); - } - - if (parameters == null) - { - throw new ArgumentNullException("parameters"); - } - - if (scopeBytes == null) - { - throw new ArgumentNullException("scopeBytes"); - } - - if (privacy == null) - { - throw new ArgumentNullException("privacy"); - } - - if (provider is DefaultAuthenticationProvider) - { - return true; - } - - if (0 == (header.SecurityLevel & Levels.Authentication)) - { - return true; - } - - var expected = parameters.AuthenticationParameters; - parameters.AuthenticationParameters = provider.CleanDigest; // clean the hash first. - var newHash = provider.ComputeHash(version, header, parameters, scopeBytes, privacy, length); - parameters.AuthenticationParameters = expected; // restore the hash. - return newHash == expected; - } - } -} diff --git a/SharpSnmpLib/Security/DESPrivacyProvider.cs b/SharpSnmpLib/Security/DESPrivacyProvider.cs index 451a297a..c8cbb2b8 100644 --- a/SharpSnmpLib/Security/DESPrivacyProvider.cs +++ b/SharpSnmpLib/Security/DESPrivacyProvider.cs @@ -46,27 +46,33 @@ public DESPrivacyProvider(OctetString phrase, IAuthenticationProvider auth) { throw new ArgumentNullException("phrase"); } - + if (auth == null) { throw new ArgumentNullException("auth"); } - + // IMPORTANT: in this way privacy cannot be non-default. if (auth == DefaultAuthenticationProvider.Instance) { throw new ArgumentException("if authentication is off, then privacy cannot be used"); } - + _phrase = phrase; AuthenticationProvider = auth; } - + /// /// Corresponding . /// public IAuthenticationProvider AuthenticationProvider { get; private set; } + /// + /// Engine ID. + /// + /// This is an optional field, and only used by TRAP v2 authentication. + public OctetString EngineId { get; set; } + /// /// Encrypt scoped PDU using DES encryption protocol /// @@ -88,17 +94,17 @@ public static byte[] Encrypt(byte[] unencryptedData, byte[] key, byte[] privacyP { throw new ArgumentNullException("key"); } - + if (privacyParameters == null) { throw new ArgumentNullException("privacyParameters"); } - + if (key.Length < MinimumKeyLength) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Encryption key length has to 32 bytes or more. Current: {0}", key.Length), "key"); } - + var iv = GetIV(key, privacyParameters); // DES uses 8 byte keys but we need 16 to encrypt ScopedPdu. Get first 8 bytes and use them as encryption key @@ -109,7 +115,7 @@ public static byte[] Encrypt(byte[] unencryptedData, byte[] key, byte[] privacyP { div += 1; } - + var newLength = div * 8; var result = new byte[newLength]; var buffer = new byte[newLength]; @@ -134,13 +140,13 @@ public static byte[] Encrypt(byte[] unencryptedData, byte[] key, byte[] privacyP inbuffer[i] = (byte)(buffer[posIn] ^ cipherText[i]); posIn++; } - + transform.TransformBlock(inbuffer, 0, inbuffer.Length, cipherText, 0); Buffer.BlockCopy(cipherText, 0, result, posResult, cipherText.Length); posResult += cipherText.Length; } } - + des.Clear(); } @@ -163,27 +169,27 @@ public static byte[] Decrypt(byte[] encryptedData, byte[] key, byte[] privacyPar { throw new ArgumentNullException("encryptedData"); } - + if (key == null) { throw new ArgumentNullException("key"); } - + if (privacyParameters == null) { throw new ArgumentNullException("privacyParameters"); } - + if (encryptedData.Length == 0) { throw new ArgumentException("empty encrypted data", "encryptedData"); } - + if ((encryptedData.Length % 8) != 0) { throw new ArgumentException("Encrypted data buffer has to be divisible by 8.", "encryptedData"); } - + if (privacyParameters.Length != PrivacyParametersLength) { throw new ArgumentOutOfRangeException("privacyParameters", "Privacy parameters argument has to be 8 bytes long"); @@ -199,7 +205,7 @@ public static byte[] Decrypt(byte[] encryptedData, byte[] key, byte[] privacyPar { iv[i] = (byte)(key[8 + i] ^ privacyParameters[i]); } - + using (DES des = DES.Create()) { des.Mode = CipherMode.CBC; @@ -232,13 +238,13 @@ private static byte[] GetIV(IList privacyKey, IList salt) { throw new ArgumentException("Invalid privacy key length", "privacyKey"); } - + var iv = new byte[8]; for (var i = 0; i < iv.Length; i++) { iv[i] = (byte)(salt[i] ^ privacyKey[8 + i]); } - + return iv; } @@ -255,12 +261,12 @@ private static byte[] GetKey(byte[] privacyPassword) { throw new ArgumentException("Invalid privacy key length.", "privacyPassword"); } - + var key = new byte[8]; Buffer.BlockCopy(privacyPassword, 0, key, 0, 8); return key; } - + /// /// Returns the length of privacyParameters USM header field. For DES, field length is 8. /// @@ -279,9 +285,9 @@ public static int MinimumKeyLength { get { return MaximumKeyLength; } } - + /// - /// Return maximum encryption/decryption key length. For DES, returned value is 16 + /// Return maximum encryption/decryption key length. For DES, returned value is 16. /// /// DES protocol itself requires an 8 byte key. Additional 8 bytes are used for generating the /// encryption IV. For encryption itself, first 8 bytes of the key are used. @@ -291,7 +297,7 @@ public static int MaximumKeyLength get { return 16; } } - #region IPrivacyProvider Members +#region IPrivacyProvider Members /// /// Decrypts the specified data. @@ -310,17 +316,17 @@ public ISnmpData Decrypt(ISnmpData data, SecurityParameters parameters) { throw new ArgumentNullException("parameters"); } - + var code = data.TypeCode; if (code != SnmpType.OctetString) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "cannot decrypt the scope data: {0}", code), "data"); } - + var octets = (OctetString)data; var bytes = octets.GetRaw(); var pkey = AuthenticationProvider.PasswordToKey(_phrase.GetRaw(), parameters.EngineId.GetRaw()); - + try { // decode encrypted packet @@ -332,7 +338,7 @@ public ISnmpData Decrypt(ISnmpData data, SecurityParameters parameters) newException.SetBytes(bytes); throw newException; } - + return result; } catch (Exception ex) @@ -355,17 +361,17 @@ public ISnmpData Encrypt(ISnmpData data, SecurityParameters parameters) { throw new ArgumentNullException("data"); } - + if (parameters == null) { throw new ArgumentNullException("parameters"); } - + if (data.TypeCode != SnmpType.Sequence && !(data is ISnmpPdu)) { throw new ArgumentException("unencrypted data is expected.", "data"); - } - + } + var pkey = AuthenticationProvider.PasswordToKey(_phrase.GetRaw(), parameters.EngineId.GetRaw()); var bytes = data.ToBytes(); var reminder = bytes.Length % 8; @@ -380,7 +386,7 @@ public ISnmpData Encrypt(ISnmpData data, SecurityParameters parameters) bytes = stream.ToArray(); } - + var encrypted = Encrypt(bytes, pkey, parameters.PrivacyParameters.GetRaw()); return new OctetString(encrypted); } @@ -394,7 +400,7 @@ public OctetString Salt get { return new OctetString(_salt.GetSaltBytes()); } } - #endregion +#endregion } } #endif \ No newline at end of file diff --git a/SharpSnmpLib/Security/DefaultPrivacyProvider.cs b/SharpSnmpLib/Security/DefaultPrivacyProvider.cs index e8024b0e..3d0dc800 100644 --- a/SharpSnmpLib/Security/DefaultPrivacyProvider.cs +++ b/SharpSnmpLib/Security/DefaultPrivacyProvider.cs @@ -54,7 +54,13 @@ public DefaultPrivacyProvider(IAuthenticationProvider authentication) /// Corresponding . /// public IAuthenticationProvider AuthenticationProvider { get; private set; } - + + /// + /// Engine ID. + /// + /// This is an optional field, and only used by TRAP v2 authentication. + public OctetString EngineId { get; set; } + /// /// Decrypts the specified data. /// diff --git a/SharpSnmpLib/Security/IPrivacyProvider.cs b/SharpSnmpLib/Security/IPrivacyProvider.cs index 60415243..7e555cdb 100644 --- a/SharpSnmpLib/Security/IPrivacyProvider.cs +++ b/SharpSnmpLib/Security/IPrivacyProvider.cs @@ -50,5 +50,11 @@ public interface IPrivacyProvider /// Corresponding . /// IAuthenticationProvider AuthenticationProvider { get; } + + /// + /// Engine ID. + /// + /// This is an optional field, and only used by TRAP v2 authentication. + OctetString EngineId { get; } } } diff --git a/SharpSnmpLib/Security/PrivacyProviderExtension.cs b/SharpSnmpLib/Security/PrivacyProviderExtension.cs index 182cccfa..950b694e 100644 --- a/SharpSnmpLib/Security/PrivacyProviderExtension.cs +++ b/SharpSnmpLib/Security/PrivacyProviderExtension.cs @@ -91,5 +91,102 @@ public static ISnmpData GetScopeData(this IPrivacyProvider privacy, Header heade ? privacy.Encrypt(rawScopeData, parameters) : rawScopeData; } + + /// + /// Computes the hash. + /// + /// The version. + /// The header. + /// The parameters. + /// The scope. + /// The privacy provider. + public static void ComputeHash(this IPrivacyProvider privacy, VersionCode version, Header header, SecurityParameters parameters, ISegment scope) + { + if (header == null) + { + throw new ArgumentNullException("header"); + } + + if (parameters == null) + { + throw new ArgumentNullException("parameters"); + } + + if (scope == null) + { + throw new ArgumentNullException("scope"); + } + + if (privacy == null) + { + throw new ArgumentNullException("privacy"); + } + + var provider = privacy.AuthenticationProvider; + if (provider is DefaultAuthenticationProvider) + { + return; + } + + if (0 == (header.SecurityLevel & Levels.Authentication)) + { + return; + } + + var scopeData = privacy.GetScopeData(header, parameters, scope.GetData(version)); + parameters.AuthenticationParameters = provider.ComputeHash(version, header, parameters, scopeData, privacy, null); // replace the hash. + } + + /// + /// Verifies the hash. + /// + /// The version. + /// The header. + /// The parameters. + /// The scope bytes. + /// The privacy provider. + /// The length bytes. + /// + /// Returns true if hash matches. Otherwise, returns false. + /// + public static bool VerifyHash(this IPrivacyProvider privacy, VersionCode version, Header header, SecurityParameters parameters, ISnmpData scopeBytes, byte[] length) + { + if (header == null) + { + throw new ArgumentNullException("header"); + } + + if (parameters == null) + { + throw new ArgumentNullException("parameters"); + } + + if (scopeBytes == null) + { + throw new ArgumentNullException("scopeBytes"); + } + + if (privacy == null) + { + throw new ArgumentNullException("privacy"); + } + + var provider = privacy.AuthenticationProvider; + if (provider is DefaultAuthenticationProvider) + { + return true; + } + + if (0 == (header.SecurityLevel & Levels.Authentication)) + { + return true; + } + + var expected = parameters.AuthenticationParameters; + parameters.AuthenticationParameters = provider.CleanDigest; // clean the hash first. + var newHash = provider.ComputeHash(version, header, parameters, scopeBytes, privacy, length); + parameters.AuthenticationParameters = expected; // restore the hash. + return newHash == expected; + } } } diff --git a/SharpSnmpLib/SharpSnmpLib.Portable.csproj b/SharpSnmpLib/SharpSnmpLib.Portable.csproj index 255ab6c0..1e5cda6d 100644 --- a/SharpSnmpLib/SharpSnmpLib.Portable.csproj +++ b/SharpSnmpLib/SharpSnmpLib.Portable.csproj @@ -82,7 +82,6 @@ - diff --git a/Tests/Integration/DaemonTestFixture.cs b/Tests/Integration/DaemonTestFixture.cs new file mode 100644 index 00000000..881b9c21 --- /dev/null +++ b/Tests/Integration/DaemonTestFixture.cs @@ -0,0 +1,402 @@ +using Lextm.SharpSnmpLib.Messaging; +using Lextm.SharpSnmpLib.Objects; +using Lextm.SharpSnmpLib.Pipeline; +using Lextm.SharpSnmpLib.Security; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Net; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace Lextm.SharpSnmpLib.Integration +{ + public class DaemonTestFixture + { + static NumberGenerator port = new NumberGenerator(40000, 45000); + + private SnmpEngine CreateEngine() + { + // TODO: this is a hack. review it later. + var store = new ObjectStore(); + store.Add(new SysDescr()); + store.Add(new SysObjectId()); + store.Add(new SysUpTime()); + store.Add(new SysContact()); + store.Add(new SysName()); + store.Add(new SysLocation()); + store.Add(new SysServices()); + store.Add(new SysORLastChange()); + store.Add(new SysORTable()); + store.Add(new IfNumber()); + store.Add(new IfTable()); + store.Add(new TimeoutObject()); + + var users = new UserRegistry(); + users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair); + users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication")))); +#if !NETSTANDARD + users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"), + new MD5AuthenticationProvider(new OctetString("authentication")))); +#endif + var getv1 = new GetV1MessageHandler(); + var getv1Mapping = new HandlerMapping("v1", "GET", getv1); + + var getv23 = new GetMessageHandler(); + var getv23Mapping = new HandlerMapping("v2,v3", "GET", getv23); + + var setv1 = new SetV1MessageHandler(); + var setv1Mapping = new HandlerMapping("v1", "SET", setv1); + + var setv23 = new SetMessageHandler(); + var setv23Mapping = new HandlerMapping("v2,v3", "SET", setv23); + + var getnextv1 = new GetNextV1MessageHandler(); + var getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1); + + var getnextv23 = new GetNextMessageHandler(); + var getnextv23Mapping = new HandlerMapping("v2,v3", "GETNEXT", getnextv23); + + var getbulk = new GetBulkMessageHandler(); + var getbulkMapping = new HandlerMapping("v2,v3", "GETBULK", getbulk); + + var v1 = new Version1MembershipProvider(new OctetString("public"), new OctetString("public")); + var v2 = new Version2MembershipProvider(new OctetString("public"), new OctetString("public")); + var v3 = new Version3MembershipProvider(); + var membership = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 }); + var handlerFactory = new MessageHandlerFactory(new[] + { + getv1Mapping, + getv23Mapping, + setv1Mapping, + setv23Mapping, + getnextv1Mapping, + getnextv23Mapping, + getbulkMapping + }); + + var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory); + return new SnmpEngine(pipelineFactory, new Listener { Users = users }, new EngineGroup()); + } + + private class TimeoutObject : ScalarObject + { + public TimeoutObject() + : base(new ObjectIdentifier("1.5.2")) + { + + } + + public override ISnmpData Data + { + get + { + Thread.Sleep(1500 * 2); + throw new NotImplementedException(); + } + + set + { + throw new NotImplementedException(); + } + } + } + + [Fact] + public async Task TestResponseAsync() + { + using (var engine = CreateEngine()) + { + engine.Listener.ClearBindings(); + var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(serverEndPoint); + engine.Start(); + + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), + new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); + + var users1 = new UserRegistry(); + var response = await message.GetResponseAsync(serverEndPoint, users1, socket); + + engine.Stop(); + Assert.Equal(SnmpType.ResponsePdu, response.TypeCode()); + } + } + + [Fact] + public void TestResponse() + { + using (var engine = CreateEngine()) + { + engine.Listener.ClearBindings(); + var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(serverEndPoint); + engine.Start(); + + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), + new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); + + const int time = 1500; + var response = message.GetResponse(time, serverEndPoint, socket); + Assert.Equal(0x4bed, response.RequestId()); + + engine.Stop(); + } + } + + [Theory] +#if NETSTANDARD + [InlineData(64)] +#else + [InlineData(256)] +#endif + public async Task TestResponsesFromMultipleSources(int count) + { + var start = 16102; + var end = start + count; + using (var engine = CreateEngine()) + { + engine.Listener.ClearBindings(); + for (var index = start; index < end; index++) + { + engine.Listener.AddBinding(new IPEndPoint(IPAddress.Loopback, index)); + } + +#if !NETSTANDARD + // IMPORTANT: need to set min thread count so as to boost performance. + int minWorker, minIOC; + // Get the current settings. + ThreadPool.GetMinThreads(out minWorker, out minIOC); + var threads = engine.Listener.Bindings.Count; + ThreadPool.SetMinThreads(threads + 1, minIOC); +#endif + var time = DateTime.Now; + engine.Start(); + + for (int index = start; index < end; index++) + { + GetRequestMessage message = new GetRequestMessage(index, VersionCode.V2, new OctetString("public"), + new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + + Stopwatch watch = new Stopwatch(); + watch.Start(); + var response = + await + message.GetResponseAsync(new IPEndPoint(IPAddress.Loopback, index), new UserRegistry(), + socket); + watch.Stop(); + Assert.Equal(index, response.RequestId()); + } + + engine.Stop(); + } + } + + [Theory] + [InlineData(32)] + public async Task TestResponsesFromSingleSource(int count) + { + var start = 0; + var end = start + count; + using (var engine = CreateEngine()) + { + engine.Listener.ClearBindings(); + var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(serverEndPoint); + + try + { + //// IMPORTANT: need to set min thread count so as to boost performance. + //int minWorker, minIOC; + //// Get the current settings. + //ThreadPool.GetMinThreads(out minWorker, out minIOC); + //var threads = engine.Listener.Bindings.Count; + //ThreadPool.SetMinThreads(threads + 1, minIOC); + + var time = DateTime.Now; + engine.Start(); + + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + for (int index = start; index < end; index++) + { + GetRequestMessage message = new GetRequestMessage(0, VersionCode.V2, new OctetString("public"), + new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); + Stopwatch watch = new Stopwatch(); + watch.Start(); + var response = + await + message.GetResponseAsync(serverEndPoint, new UserRegistry(), socket); + watch.Stop(); + Assert.Equal(0, response.RequestId()); + } + } + catch (Exception ex) + { + Console.WriteLine(serverEndPoint.Port); + } + finally + { + engine.Stop(); + } + } + } + + [Theory] + [InlineData(32)] + public void TestResponsesFromSingleSourceWithMultipleThreads(int count) + { + var start = 0; + var end = start + count; + using (var engine = CreateEngine()) + { + engine.Listener.ClearBindings(); + var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(serverEndPoint); +#if !NETSTANDARD + // IMPORTANT: need to set min thread count so as to boost performance. + int minWorker, minIOC; + // Get the current settings. + ThreadPool.GetMinThreads(out minWorker, out minIOC); + var threads = engine.Listener.Bindings.Count; + ThreadPool.SetMinThreads(threads + 1, minIOC); +#endif + var time = DateTime.Now; + engine.Start(); + + const int timeout = 10000; + + // Uncomment below to reveal wrong sequence number issue. + // Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + + Parallel.For(start, end, index => + { + GetRequestMessage message = new GetRequestMessage(index, VersionCode.V2, + new OctetString("public"), + new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); + // Comment below to reveal wrong sequence number issue. + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + + Stopwatch watch = new Stopwatch(); + watch.Start(); + var response = message.GetResponse(timeout, serverEndPoint, socket); + watch.Stop(); + Assert.Equal(index, response.RequestId()); + } + ); + + engine.Stop(); + } + } + + [Theory] + [InlineData(256)] + public void TestResponsesFromSingleSourceWithMultipleThreadsFromManager(int count) + { + var start = 0; + var end = start + count; + using (var engine = CreateEngine()) + { + engine.Listener.ClearBindings(); + var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(serverEndPoint); + + var time = DateTime.Now; + engine.Start(); + + const int timeout = 60000; + + //for (int index = start; index < end; index++) + Parallel.For(start, end, index => + { + try + { + var result = Messenger.Get(VersionCode.V2, serverEndPoint, new OctetString("public"), + new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }, timeout); + Assert.Equal(1, result.Count); + } + catch (Exception) + { + Console.WriteLine(serverEndPoint.Port); + } + } + ); + + engine.Stop(); + } + } + + [Fact] + public void TestTimeOut() + { + using (var engine = CreateEngine()) + { + engine.Listener.ClearBindings(); + var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(serverEndPoint); + + engine.Start(); + + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), + new List { new Variable(new ObjectIdentifier("1.5.2")) }); + + const int time = 1500; + var timer = new Stopwatch(); + timer.Start(); + //IMPORTANT: test against an agent that doesn't exist. + Assert.Throws(() => message.GetResponse(time, serverEndPoint, socket)); + timer.Stop(); + + long elapsedMilliseconds = timer.ElapsedMilliseconds; + Assert.True(time <= elapsedMilliseconds); + + // FIXME: these values are valid on my machine openSUSE 11.2. (lex) + // This test case usually fails on Windows, as strangely WinSock API call adds an extra 500-ms. + if (SnmpMessageExtension.IsRunningOnMono) + { + Assert.True(elapsedMilliseconds <= time + 100); + } + } + } + + [Fact] + public void TestLargeMessage() + { + using (var engine = CreateEngine()) + { + engine.Listener.ClearBindings(); + var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(serverEndPoint); + + engine.Start(); + + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + var list = new List(); + for (int i = 0; i < 1000; i++) + { + list.Add(new Variable(new ObjectIdentifier("1.3.6.1.1.1.0"))); + } + + GetRequestMessage message = new GetRequestMessage( + 0x4bed, + VersionCode.V2, + new OctetString("public"), + list); + + Assert.True(message.ToBytes().Length > 10000); + + var time = 1500; + //IMPORTANT: test against an agent that doesn't exist. + var result = message.GetResponse(time, serverEndPoint, socket); + + Assert.True(result.Scope.Pdu.ErrorStatus.ToErrorCode() == ErrorCode.NoError); + } + } + + } +} diff --git a/Tests/Integration/TrapDaemonTestFixture.cs b/Tests/Integration/TrapDaemonTestFixture.cs new file mode 100644 index 00000000..d4901ef2 --- /dev/null +++ b/Tests/Integration/TrapDaemonTestFixture.cs @@ -0,0 +1,191 @@ +using Lextm.SharpSnmpLib.Messaging; +using Lextm.SharpSnmpLib.Pipeline; +using Lextm.SharpSnmpLib.Security; +using System.Collections.Generic; +using System.Net; +using System.Threading.Tasks; +using Xunit; + +namespace Lextm.SharpSnmpLib.Integration +{ + public class TrapDaemonTestFixture + { + static NumberGenerator port = new NumberGenerator(40000, 65000); + + [Fact] + public async Task TestTrapV2HandlerWithV2Message() + { + // TODO: this is a hack. review it later. + var users = new UserRegistry(); + users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair); + users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication")))); +#if !NETSTANDARD + users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"), + new MD5AuthenticationProvider(new OctetString("authentication")))); +#endif + var count = 0; + + var trapv1 = new TrapV1MessageHandler(); + var trapv1Mapping = new HandlerMapping("v1", "TRAPV1", trapv1); + + var trapv2 = new TrapV2MessageHandler(); + trapv2.MessageReceived += (sender, args) => { count++; }; + var trapv2Mapping = new HandlerMapping("v2,v3", "TRAPV2", trapv2); + + var inform = new InformRequestMessageHandler(); + var informMapping = new HandlerMapping("v2,v3", "INFORM", inform); + + var store = new ObjectStore(); + var v1 = new Version1MembershipProvider(new OctetString("public"), new OctetString("public")); + var v2 = new Version2MembershipProvider(new OctetString("public"), new OctetString("public")); + var v3 = new Version3MembershipProvider(); + var membership = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 }); + var handlerFactory = new MessageHandlerFactory(new[] { trapv1Mapping, trapv2Mapping, informMapping }); + + var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory); + using (var engine = new SnmpEngine(pipelineFactory, new Listener { Users = users }, new EngineGroup())) + { + var daemonEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(daemonEndPoint); + engine.Start(); + + await Messenger.SendTrapV2Async(1, VersionCode.V2, daemonEndPoint, new OctetString("public"), new ObjectIdentifier("1.3.6.1"), 500, new List()); + await Task.Delay(5000); + + Assert.Equal(1, count); + + engine.Stop(); + } + } + + [Fact] + public async Task TestTrapV2HandlerWithV3Message() + { + // TODO: this is a hack. review it later. + var engineId = new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")); + var users = new UserRegistry(); + users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair); + users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))) + { + EngineId = engineId + }); +#if !NETSTANDARD + users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"), + new MD5AuthenticationProvider(new OctetString("authentication")))); +#endif + var count = 0; + + var trapv1 = new TrapV1MessageHandler(); + var trapv1Mapping = new HandlerMapping("v1", "TRAPV1", trapv1); + + var trapv2 = new TrapV2MessageHandler(); + trapv2.MessageReceived += (sender, args) => { count++; }; + var trapv2Mapping = new HandlerMapping("v2,v3", "TRAPV2", trapv2); + + var inform = new InformRequestMessageHandler(); + var informMapping = new HandlerMapping("v2,v3", "INFORM", inform); + + var store = new ObjectStore(); + var v1 = new Version1MembershipProvider(new OctetString("public"), new OctetString("public")); + var v2 = new Version2MembershipProvider(new OctetString("public"), new OctetString("public")); + var v3 = new Version3MembershipProvider(); + var membership = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 }); + var handlerFactory = new MessageHandlerFactory(new[] { trapv1Mapping, trapv2Mapping, informMapping }); + + var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory); + using (var engine = new SnmpEngine(pipelineFactory, new Listener { Users = users }, new EngineGroup())) + { + var daemonEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(daemonEndPoint); + engine.Start(); + + var privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))); + var trap = new TrapV2Message( + VersionCode.V3, + 1004947569, + 234419641, + new OctetString("authen"), + new ObjectIdentifier("1.3.6"), + 0, + new List(), + privacy, + 0x10000, + engineId, + 0, + 0); + await trap.SendAsync(daemonEndPoint); + await Task.Delay(5000); + + Assert.Equal(1, count); + + engine.Stop(); + } + } + + [Fact] + public async Task TestTrapV2HandlerWithV3MessageAndWrongEngineId() + { + // TODO: this is a hack. review it later. + var engineId = new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")); + var users = new UserRegistry(); + users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair); + users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))) + { + EngineId = engineId + }); +#if !NETSTANDARD + users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"), + new MD5AuthenticationProvider(new OctetString("authentication")))); +#endif + var count = 0; + + var trapv1 = new TrapV1MessageHandler(); + var trapv1Mapping = new HandlerMapping("v1", "TRAPV1", trapv1); + + var trapv2 = new TrapV2MessageHandler(); + trapv2.MessageReceived += (sender, args) => { count++; }; + var trapv2Mapping = new HandlerMapping("v2,v3", "TRAPV2", trapv2); + + var inform = new InformRequestMessageHandler(); + var informMapping = new HandlerMapping("v2,v3", "INFORM", inform); + + var store = new ObjectStore(); + var v1 = new Version1MembershipProvider(new OctetString("public"), new OctetString("public")); + var v2 = new Version2MembershipProvider(new OctetString("public"), new OctetString("public")); + var v3 = new Version3MembershipProvider(); + var membership = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 }); + var handlerFactory = new MessageHandlerFactory(new[] { trapv1Mapping, trapv2Mapping, informMapping }); + + var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory); + var group = new EngineGroup(); + using (var engine = new SnmpEngine(pipelineFactory, new Listener { Users = users }, group)) + { + var daemonEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); + engine.Listener.AddBinding(daemonEndPoint); + engine.Start(); + + var privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))); + var trap = new TrapV2Message( + VersionCode.V3, + 1004947569, + 234419641, + new OctetString("authen"), + new ObjectIdentifier("1.3.6"), + 0, + new List(), + privacy, + 0x10000, + new OctetString(ByteTool.Convert("80001F8880E9630000D61FF450")), + 0, + 0); + await trap.SendAsync(daemonEndPoint); + await Task.Delay(5000); + + Assert.Equal(0, count); + Assert.Equal(new Counter32(1), group.UnknownEngineId.Data); + + engine.Stop(); + } + } + } +} diff --git a/Tests/Messaging/Tests/GetRequestMessageTestFixture.cs b/Tests/Messaging/Tests/GetRequestMessageTestFixture.cs deleted file mode 100644 index a9dc2782..00000000 --- a/Tests/Messaging/Tests/GetRequestMessageTestFixture.cs +++ /dev/null @@ -1,779 +0,0 @@ -/* - * Created by SharpDevelop. - * User: lextm - * Date: 2008/4/28 - * Time: 18:35 - * - * To change this template use Tools | Options | Coding | Edit Standard Headers. - */ - -using System.Collections.Generic; -using System.Net; -using Lextm.SharpSnmpLib.Security; -using System.Net.Sockets; -using System; -using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; -using Lextm.SharpSnmpLib.Objects; -using Lextm.SharpSnmpLib.Pipeline; - -#pragma warning disable 1591 - -namespace Lextm.SharpSnmpLib.Messaging.Tests -{ -#if !NETSTANDARD - using JetBrains.dotMemoryUnit; -#endif - using Xunit; - - /// - /// Description of TestGetMessage. - /// - public class GetRequestMessageTestFixture - { - static NumberGenerator port = new NumberGenerator(40000, 65000); - -#if !NETSTANDARD - [Fact] - public void Test() - { - byte[] expected = Properties.Resources.get; - ISnmpMessage message = MessageFactory.ParseMessages(expected, new UserRegistry())[0]; - Assert.Equal(SnmpType.GetRequestPdu, message.TypeCode()); - GetRequestPdu pdu = (GetRequestPdu)message.Pdu(); - Assert.Equal(1, pdu.Variables.Count); - Variable v = pdu.Variables[0]; - Assert.Equal(new uint[] { 1, 3, 6, 1, 2, 1, 1, 6, 0 }, v.Id.ToNumerical()); - Assert.Equal(typeof(Null), v.Data.GetType()); - Assert.True(expected.Length >= message.ToBytes().Length); - } - - [Fact] - public void TestConstructor() - { - List list = new List(1) - { - new Variable(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 2, 1, 1, 6, 0}), - new Null()) - }; - GetRequestMessage message = new GetRequestMessage(0, VersionCode.V2, new OctetString("public"), list); - Assert.True(Properties.Resources.get.Length >= message.ToBytes().Length); - } -#endif - [Fact] - public void TestConstructorV3Auth1() - { - const string bytes = "30 73" + - "02 01 03 " + - "30 0F " + - "02 02 35 41 " + - "02 03 00 FF E3" + - "04 01 05" + - "02 01 03" + - "04 2E " + - "30 2C" + - "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " + - "02 01 0D " + - "02 01 57 " + - "04 05 6C 65 78 6C 69 " + - "04 0C 1C 6D 67 BF B2 38 ED 63 DF 0A 05 24 " + - "04 00 " + - "30 2D " + - "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " + - "04 00 " + - "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"; - ReportMessage report = new ReportMessage( - VersionCode.V3, - new Header( - new Integer32(13633), - new Integer32(0xFFE3), - 0), - new SecurityParameters( - new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), - new Integer32(0x0d), - new Integer32(0x57), - new OctetString("lexli"), - new OctetString(new byte[12]), - OctetString.Empty), - new Scope( - new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), - OctetString.Empty, - new ReportPdu( - 0x01AF, - ErrorCode.NoError, - 0, - new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) })), - DefaultPrivacyProvider.DefaultPair, - null); - - IPrivacyProvider privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass"))); - GetRequestMessage request = new GetRequestMessage( - VersionCode.V3, - 13633, - 0x01AF, - new OctetString("lexli"), - new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) }, - privacy, - Messenger.MaxMessageSize, - report); - - Assert.Equal(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel); - Assert.Equal(ByteTool.Convert(bytes), request.ToBytes()); - } -#if !NETSTANDARD - [Fact] - public void TestConstructorV2AuthMd5PrivDes() - { - const string bytes = "30 81 80 02 01 03 30 0F 02 02 6C 99 02 03 00 FF" + - "E3 04 01 07 02 01 03 04 38 30 36 04 0D 80 00 1F" + - "88 80 E9 63 00 00 D6 1F F4 49 02 01 14 02 01 35" + - "04 07 6C 65 78 6D 61 72 6B 04 0C 80 50 D9 A1 E7" + - "81 B6 19 80 4F 06 C0 04 08 00 00 00 01 44 2C A3" + - "B5 04 30 4B 4F 10 3B 73 E1 E4 BD 91 32 1B CB 41" + - "1B A1 C1 D1 1D 2D B7 84 16 CA 41 BF B3 62 83 C4" + - "29 C5 A4 BC 32 DA 2E C7 65 A5 3D 71 06 3C 5B 56" + - "FB 04 A4"; - MD5AuthenticationProvider auth = new MD5AuthenticationProvider(new OctetString("testpass")); - IPrivacyProvider privacy = new DESPrivacyProvider(new OctetString("passtest"), auth); - GetRequestMessage request = new GetRequestMessage( - VersionCode.V3, - new Header( - new Integer32(0x6C99), - new Integer32(0xFFE3), - Levels.Authentication | Levels.Privacy | Levels.Reportable), - new SecurityParameters( - new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), - new Integer32(0x14), - new Integer32(0x35), - new OctetString("lexmark"), - new OctetString(ByteTool.Convert("80 50 D9 A1 E7 81 B6 19 80 4F 06 C0")), - new OctetString(ByteTool.Convert("00 00 00 01 44 2C A3 B5"))), - new Scope( - new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), - OctetString.Empty, - new GetRequestPdu( - 0x3A25, - new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) })), - privacy, - null); - Assert.Equal(Levels.Authentication | Levels.Privacy | Levels.Reportable, request.Header.SecurityLevel); - Assert.Equal(ByteTool.Convert(bytes), request.ToBytes()); - } -#endif - [Fact] - public void TestConstructorV3AuthMd5() - { - const string bytes = "30 73" + - "02 01 03 " + - "30 0F " + - "02 02 35 41 " + - "02 03 00 FF E3" + - "04 01 05" + - "02 01 03" + - "04 2E " + - "30 2C" + - "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " + - "02 01 0D " + - "02 01 57 " + - "04 05 6C 65 78 6C 69 " + - "04 0C 1C 6D 67 BF B2 38 ED 63 DF 0A 05 24 " + - "04 00 " + - "30 2D " + - "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " + - "04 00 " + - "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"; - IPrivacyProvider pair = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass"))); - GetRequestMessage request = new GetRequestMessage( - VersionCode.V3, - new Header( - new Integer32(13633), - new Integer32(0xFFE3), - Levels.Authentication | Levels.Reportable), - new SecurityParameters( - new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), - new Integer32(0x0d), - new Integer32(0x57), - new OctetString("lexli"), - new OctetString(ByteTool.Convert("1C 6D 67 BF B2 38 ED 63 DF 0A 05 24")), - OctetString.Empty), - new Scope( - new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), - OctetString.Empty, - new GetRequestPdu( - 0x01AF, - new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null()) })), - pair, - null); - Assert.Equal(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel); - Assert.Equal(ByteTool.Convert(bytes), request.ToBytes()); - } - - [Fact] - public void TestConstructorV3AuthSha() - { - const string bytes = "30 77 02 01 03 30 0F 02 02 47 21 02 03 00 FF E3" + - "04 01 05 02 01 03 04 32 30 30 04 0D 80 00 1F 88" + - "80 E9 63 00 00 D6 1F F4 49 02 01 15 02 02 01 5B" + - "04 08 6C 65 78 74 75 64 69 6F 04 0C 7B 62 65 AE" + - "D3 8F E3 7D 58 45 5C 6C 04 00 30 2D 04 0D 80 00" + - "1F 88 80 E9 63 00 00 D6 1F F4 49 04 00 A0 1A 02" + - "02 56 FF 02 01 00 02 01 00 30 0E 30 0C 06 08 2B" + - "06 01 02 01 01 03 00 05 00"; - IPrivacyProvider pair = new DefaultPrivacyProvider(new SHA1AuthenticationProvider(new OctetString("password"))); - GetRequestMessage request = new GetRequestMessage( - VersionCode.V3, - new Header( - new Integer32(0x4721), - new Integer32(0xFFE3), - Levels.Authentication | Levels.Reportable), - new SecurityParameters( - new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), - new Integer32(0x15), - new Integer32(0x015B), - new OctetString("lextudio"), - new OctetString(ByteTool.Convert("7B 62 65 AE D3 8F E3 7D 58 45 5C 6C")), - OctetString.Empty), - new Scope( - new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), - OctetString.Empty, - new GetRequestPdu( - 0x56FF, - new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null()) })), - pair, - null); - Assert.Equal(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel); - Assert.Equal(ByteTool.Convert(bytes), request.ToBytes()); - } - - [Fact] - public void TestDiscoveryV3() - { - const string bytes = "30 3A 02 01 03 30 0F 02 02 6A 09 02 03 00 FF E3" + - " 04 01 04 02 01 03 04 10 30 0E 04 00 02 01 00 02" + - " 01 00 04 00 04 00 04 00 30 12 04 00 04 00 A0 0C" + - " 02 02 2C 6B 02 01 00 02 01 00 30 00"; - GetRequestMessage request = new GetRequestMessage( - VersionCode.V3, - new Header( - new Integer32(0x6A09), - new Integer32(0xFFE3), - Levels.Reportable), - new SecurityParameters( - OctetString.Empty, - Integer32.Zero, - Integer32.Zero, - OctetString.Empty, - OctetString.Empty, - OctetString.Empty), - new Scope( - OctetString.Empty, - OctetString.Empty, - new GetRequestPdu(0x2C6B, new List())), - DefaultPrivacyProvider.DefaultPair, - null - ); - string test = ByteTool.Convert(request.ToBytes()); - Assert.Equal(bytes, test); - } - - [Fact] - public void TestToBytes() - { - const string s = "30 27 02 01 01 04 06 70 75 62 6C 69 63 A0 1A 02" + - "02 4B ED 02 01 00 02 01 00 30 0E 30 0C 06 08 2B" + - "06 01 02 01 01 01 00 05 00 "; - byte[] expected = ByteTool.Convert(s); - GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); - Assert.Equal(expected, message.ToBytes()); - } - - [Fact] - public async Task TestResponseAsync() - { - using (var engine = CreateEngine()) - { - engine.Listener.ClearBindings(); - var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); - engine.Listener.AddBinding(serverEndPoint); - engine.Start(); - - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), - new List {new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))}); - - var users1 = new UserRegistry(); - var response = await message.GetResponseAsync(serverEndPoint, users1, socket); - - engine.Stop(); - Assert.Equal(SnmpType.ResponsePdu, response.TypeCode()); - } - } - - [Fact] - public void TestResponse() - { - using (var engine = CreateEngine()) - { - engine.Listener.ClearBindings(); - var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); - engine.Listener.AddBinding(serverEndPoint); - engine.Start(); - - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), - new List {new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))}); - - const int time = 1500; - var response = message.GetResponse(time, serverEndPoint, socket); - Assert.Equal(0x4bed, response.RequestId()); - - engine.Stop(); - } - } - - [Theory] -#if NETSTANDARD - [InlineData(64)] -#else - [InlineData(256)] -#endif - public async Task TestResponsesFromMultipleSources(int count) - { - var start = 16102; - var end = start + count; - using (var engine = CreateEngine()) - { - engine.Listener.ClearBindings(); - for (var index = start; index < end; index++) - { - engine.Listener.AddBinding(new IPEndPoint(IPAddress.Loopback, index)); - } - -#if !NETSTANDARD - // IMPORTANT: need to set min thread count so as to boost performance. - int minWorker, minIOC; - // Get the current settings. - ThreadPool.GetMinThreads(out minWorker, out minIOC); - var threads = engine.Listener.Bindings.Count; - ThreadPool.SetMinThreads(threads + 1, minIOC); -#endif - var time = DateTime.Now; - engine.Start(); - - for (int index = start; index < end; index++) - { - GetRequestMessage message = new GetRequestMessage(index, VersionCode.V2, new OctetString("public"), - new List {new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))}); - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - - Stopwatch watch = new Stopwatch(); - watch.Start(); - var response = - await - message.GetResponseAsync(new IPEndPoint(IPAddress.Loopback, index), new UserRegistry(), - socket); - watch.Stop(); - Assert.Equal(index, response.RequestId()); - } - - engine.Stop(); - } - } - - [Theory] - [InlineData(32)] - public async Task TestResponsesFromSingleSource(int count) - { - var start = 0; - var end = start + count; - using (var engine = CreateEngine()) - { - engine.Listener.ClearBindings(); - var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); - engine.Listener.AddBinding(serverEndPoint); - - try - { - //// IMPORTANT: need to set min thread count so as to boost performance. - //int minWorker, minIOC; - //// Get the current settings. - //ThreadPool.GetMinThreads(out minWorker, out minIOC); - //var threads = engine.Listener.Bindings.Count; - //ThreadPool.SetMinThreads(threads + 1, minIOC); - - var time = DateTime.Now; - engine.Start(); - - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - for (int index = start; index < end; index++) - { - GetRequestMessage message = new GetRequestMessage(0, VersionCode.V2, new OctetString("public"), - new List {new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))}); - Stopwatch watch = new Stopwatch(); - watch.Start(); - var response = - await - message.GetResponseAsync(serverEndPoint, new UserRegistry(), socket); - watch.Stop(); - Assert.Equal(0, response.RequestId()); - } - } - catch (Exception ex) - { - Console.WriteLine(serverEndPoint.Port); - } - finally - { - engine.Stop(); - } - } - } - - [Theory] - [InlineData(32)] - public void TestResponsesFromSingleSourceWithMultipleThreads(int count) - { - var start = 0; - var end = start + count; - using (var engine = CreateEngine()) - { - engine.Listener.ClearBindings(); - var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); - engine.Listener.AddBinding(serverEndPoint); -#if !NETSTANDARD - // IMPORTANT: need to set min thread count so as to boost performance. - int minWorker, minIOC; - // Get the current settings. - ThreadPool.GetMinThreads(out minWorker, out minIOC); - var threads = engine.Listener.Bindings.Count; - ThreadPool.SetMinThreads(threads + 1, minIOC); -#endif - var time = DateTime.Now; - engine.Start(); - - const int timeout = 10000; - - // Uncomment below to reveal wrong sequence number issue. - // Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - - Parallel.For(start, end, index => - { - GetRequestMessage message = new GetRequestMessage(index, VersionCode.V2, - new OctetString("public"), - new List {new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))}); - // Comment below to reveal wrong sequence number issue. - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - - Stopwatch watch = new Stopwatch(); - watch.Start(); - var response = message.GetResponse(timeout, serverEndPoint, socket); - watch.Stop(); - Assert.Equal(index, response.RequestId()); - } - ); - - engine.Stop(); - } - } - - [Theory] - [InlineData(256)] - public void TestResponsesFromSingleSourceWithMultipleThreadsFromManager(int count) - { - var start = 0; - var end = start + count; - using (var engine = CreateEngine()) - { - engine.Listener.ClearBindings(); - var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); - engine.Listener.AddBinding(serverEndPoint); - - var time = DateTime.Now; - engine.Start(); - - const int timeout = 60000; - - //for (int index = start; index < end; index++) - Parallel.For(start, end, index => - { - try - { - var result = Messenger.Get(VersionCode.V2, serverEndPoint, new OctetString("public"), - new List {new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))}, timeout); - Assert.Equal(1, result.Count); - } - catch (Exception) - { - Console.WriteLine(serverEndPoint.Port); - } - } - ); - - engine.Stop(); - } - } - - private SnmpEngine CreateEngine() - { - // TODO: this is a hack. review it later. - var store = new ObjectStore(); - store.Add(new SysDescr()); - store.Add(new SysObjectId()); - store.Add(new SysUpTime()); - store.Add(new SysContact()); - store.Add(new SysName()); - store.Add(new SysLocation()); - store.Add(new SysServices()); - store.Add(new SysORLastChange()); - store.Add(new SysORTable()); - store.Add(new IfNumber()); - store.Add(new IfTable()); - store.Add(new TimeoutObject()); - - var users = new UserRegistry(); - users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair); - users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication")))); -#if !NETSTANDARD - users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"), - new MD5AuthenticationProvider(new OctetString("authentication")))); -#endif - var getv1 = new GetV1MessageHandler(); - var getv1Mapping = new HandlerMapping("v1", "GET", getv1); - - var getv23 = new GetMessageHandler(); - var getv23Mapping = new HandlerMapping("v2,v3", "GET", getv23); - - var setv1 = new SetV1MessageHandler(); - var setv1Mapping = new HandlerMapping("v1", "SET", setv1); - - var setv23 = new SetMessageHandler(); - var setv23Mapping = new HandlerMapping("v2,v3", "SET", setv23); - - var getnextv1 = new GetNextV1MessageHandler(); - var getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1); - - var getnextv23 = new GetNextMessageHandler(); - var getnextv23Mapping = new HandlerMapping("v2,v3", "GETNEXT", getnextv23); - - var getbulk = new GetBulkMessageHandler(); - var getbulkMapping = new HandlerMapping("v2,v3", "GETBULK", getbulk); - - var v1 = new Version1MembershipProvider(new OctetString("public"), new OctetString("public")); - var v2 = new Version2MembershipProvider(new OctetString("public"), new OctetString("public")); - var v3 = new Version3MembershipProvider(); - var membership = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 }); - var handlerFactory = new MessageHandlerFactory(new[] - { - getv1Mapping, - getv23Mapping, - setv1Mapping, - setv23Mapping, - getnextv1Mapping, - getnextv23Mapping, - getbulkMapping - }); - - var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory); - return new SnmpEngine(pipelineFactory, new Listener { Users = users }, new EngineGroup()); - } - - [Fact] - public void TestTimeOut() - { - using (var engine = CreateEngine()) - { - engine.Listener.ClearBindings(); - var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); - engine.Listener.AddBinding(serverEndPoint); - - engine.Start(); - - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), - new List {new Variable(new ObjectIdentifier("1.5.2"))}); - - const int time = 1500; - var timer = new Stopwatch(); - timer.Start(); - //IMPORTANT: test against an agent that doesn't exist. - Assert.Throws(() => message.GetResponse(time, serverEndPoint, socket)); - timer.Stop(); - - long elapsedMilliseconds = timer.ElapsedMilliseconds; - Assert.True(time <= elapsedMilliseconds); - - // FIXME: these values are valid on my machine openSUSE 11.2. (lex) - // This test case usually fails on Windows, as strangely WinSock API call adds an extra 500-ms. - if (SnmpMessageExtension.IsRunningOnMono) - { - Assert.True(elapsedMilliseconds <= time + 100); - } - } - } - - [Fact] - public void TestLargeMessage() - { - using (var engine = CreateEngine()) - { - engine.Listener.ClearBindings(); - var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); - engine.Listener.AddBinding(serverEndPoint); - - engine.Start(); - - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - var list = new List(); - for (int i = 0; i < 1000; i++) - { - list.Add(new Variable(new ObjectIdentifier("1.3.6.1.1.1.0"))); - } - - GetRequestMessage message = new GetRequestMessage( - 0x4bed, - VersionCode.V2, - new OctetString("public"), - list); - - Assert.True(message.ToBytes().Length > 10000); - - var time = 1500; - //IMPORTANT: test against an agent that doesn't exist. - var result = message.GetResponse(time, serverEndPoint, socket); - - Assert.True(result.Scope.Pdu.ErrorStatus.ToErrorCode() == ErrorCode.NoError); - } - } - -#if !NETSTANDARD - //[Fact] - //[Category("Default")] - public void TestMemory() - { - GC.Collect(); - - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); - - //IMPORTANT: test against an agent that doesn't exist. - Assert.Throws(() => message.BeginGetResponse(new IPEndPoint(IPAddress.Loopback, 80), new UserRegistry(), socket, ar => - { - var response = message.EndGetResponse(ar); - }, null)); - - GC.Collect(); - var memoryCheckPoint1 = dotMemory.Check(); - - for (int i = 0; i < 100; i++) - { - //Thread.Sleep(100); - Assert.Throws( - () => - message.BeginGetResponse( - new IPEndPoint(IPAddress.Loopback, 80), - new UserRegistry(), - socket, - ar => - { - var response = message.EndGetResponse(ar); - }, - null)); - } - - socket.Close(); - socket = null; - message = null; - - GC.Collect(); - dotMemory.Check(memory => - { - Assert.True(memory.GetDifference(memoryCheckPoint1) - .GetNewObjects().ObjectsCount <= 15); - }); - } - - //[Fact] - //[Category("Default")] - public void TestMemory2() - { - GC.Collect(); - - GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); - { - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - //IMPORTANT: test against an agent that doesn't exist. - Assert.Throws( - () => - message.BeginGetResponse( - new IPEndPoint(IPAddress.Loopback, 80), - new UserRegistry(), - socket, - ar => - { - var response = message.EndGetResponse(ar); - }, - null)); - - socket.Close(); - socket = null; - } - - GC.Collect(); - var memoryCheckPoint1 = dotMemory.Check(); - - for (int i = 0; i < 100; i++) - { - //Thread.Sleep(100); - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - Assert.Throws( - () => - message.BeginGetResponse( - new IPEndPoint(IPAddress.Loopback, 80), - new UserRegistry(), - socket, - ar => - { - var response = message.EndGetResponse(ar); - }, - null)); - - socket.Close(); - socket = null; - } - - message = null; - - GC.Collect(); - dotMemory.Check(memory => - { - Assert.True(memory.GetDifference(memoryCheckPoint1) - .GetNewObjects().ObjectsCount <= 31); - }); - } -#endif - private class TimeoutObject : ScalarObject - { - public TimeoutObject() - : base(new ObjectIdentifier("1.5.2")) - { - - } - - public override ISnmpData Data - { - get - { - Thread.Sleep(1500 * 2); - throw new NotImplementedException(); - } - - set - { - throw new NotImplementedException(); - } - } - } - } -} -#pragma warning restore 1591 diff --git a/Tests/Properties/Resources.Designer.cs b/Tests/Properties/Resources.Designer.cs index 3bae7843..a06d8361 100644 --- a/Tests/Properties/Resources.Designer.cs +++ b/Tests/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34014 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. diff --git a/Tests/Security/Tests/AuthenticationProviderExtensionTestFixture.cs b/Tests/Security/Tests/AuthenticationProviderExtensionTestFixture.cs deleted file mode 100644 index a74234a5..00000000 --- a/Tests/Security/Tests/AuthenticationProviderExtensionTestFixture.cs +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Created by SharpDevelop. - * User: lextm - * Date: 2010/12/5 - * Time: 15:04 - * - * To change this template use Tools | Options | Coding | Edit Standard Headers. - */ -using System; -using Xunit; - -namespace Lextm.SharpSnmpLib.Security.Tests -{ - public class AuthenticationProviderExtensionTestFixture - { - [Fact] - public void TestException() - { - Assert.Throws(() => AuthenticationProviderExtension.ComputeHash(null, VersionCode.V1, null, null, null, null)); - Assert.Throws(() => AuthenticationProviderExtension.ComputeHash(DefaultAuthenticationProvider.Instance, VersionCode.V1, null, null, null, null)); - Assert.Throws(() => AuthenticationProviderExtension.ComputeHash(DefaultAuthenticationProvider.Instance, VersionCode.V1, Header.Empty, null, null, null)); - Assert.Throws(() => AuthenticationProviderExtension.ComputeHash(DefaultAuthenticationProvider.Instance, VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), null, null)); - Assert.Throws(() => DefaultAuthenticationProvider.Instance.ComputeHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), new Scope(OctetString.Empty, OctetString.Empty, new MalformedPdu()), null)); - - var parameters = SecurityParameters.Create(new OctetString("test")); - DefaultAuthenticationProvider.Instance.ComputeHash(VersionCode.V1, Header.Empty, parameters, new Scope(OctetString.Empty, OctetString.Empty, new MalformedPdu()), DefaultPrivacyProvider.DefaultPair); - Assert.Equal(null, parameters.AuthenticationParameters); - - //Assert.Throws(() => AuthenticationProviderExtension.VerifyHash(null, VersionCode.V1, null, null, null, null)); - //Assert.Throws(() => DefaultAuthenticationProvider.Instance.VerifyHash(VersionCode.V1, null, null, null, null)); - //Assert.Throws(() => DefaultAuthenticationProvider.Instance.VerifyHash(VersionCode.V1, Header.Empty, null, null, null)); - //Assert.Throws(() => DefaultAuthenticationProvider.Instance.VerifyHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), null, null)); - //Assert.Throws(() => DefaultAuthenticationProvider.Instance.VerifyHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), new MalformedPdu(), null)); - //Assert.IsTrue(DefaultAuthenticationProvider.Instance.VerifyHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), new MalformedPdu(), DefaultPrivacyProvider.DefaultPair)); - } - } -} diff --git a/Tests/Security/Tests/DefaultAuthenticationProviderTestFixture.cs b/Tests/Security/Tests/DefaultAuthenticationProviderTestFixture.cs deleted file mode 100644 index 5bedd954..00000000 --- a/Tests/Security/Tests/DefaultAuthenticationProviderTestFixture.cs +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Created by SharpDevelop. - * User: lextm - * Date: 2010/12/5 - * Time: 15:23 - * - * To change this template use Tools | Options | Coding | Edit Standard Headers. - */ -using System; -using Xunit; - -namespace Lextm.SharpSnmpLib.Security.Tests -{ - public class DefaultAuthenticationProviderTestFixture - { - [Fact] - public void Test() - { - var provider = DefaultAuthenticationProvider.Instance; - Assert.Equal("Default authentication provider", provider.ToString()); - Assert.Throws(() => provider.PasswordToKey(null, null)); - Assert.Throws(() => provider.PasswordToKey(new byte[0], null)); - Assert.Equal(new byte[0], provider.PasswordToKey(new byte[0], new byte[0])); - - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, null, null, null, null)); - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, null, null, null)); - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), null, null)); - //Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), OctetString.Empty, null)); - //Assert.Equal(OctetString.Empty, provider.ComputeHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), OctetString.Empty, DefaultPrivacyProvider.DefaultPair)); - } - } -} diff --git a/Tests/Tests.NetStandard.csproj b/Tests/Tests.NetStandard.csproj index 96faf46d..69f7fa3f 100644 --- a/Tests/Tests.NetStandard.csproj +++ b/Tests/Tests.NetStandard.csproj @@ -8,7 +8,7 @@ Exe - netcoreapp1.0 + netcoreapp1.1 diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 04c5adf9..9245d260 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,5 +1,5 @@  - + Debug @@ -9,7 +9,7 @@ Properties Lextm.SharpSnmpLib SharpSnmpLib.Tests - v4.5 + v4.5.2 512 C:\Users\Administrator\AppData\Roaming\ICSharpCode/SharpDevelop3.0\Settings.SourceAnalysis true @@ -50,16 +50,6 @@ false - - ..\packages\Castle.Core.4.0.0\lib\net45\Castle.Core.dll - - - ..\packages\JetBrains.dotMemoryUnit.2.3.20160517.113140\lib\dotMemory.Unit.dll - True - - - ..\packages\Moq.4.7.1\lib\net45\Moq.dll - @@ -67,113 +57,98 @@ - - ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - - - ..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll - - - ..\packages\xunit.assert.2.2.0\lib\netstandard1.1\xunit.assert.dll - - - ..\packages\xunit.extensibility.core.2.2.0\lib\netstandard1.1\xunit.core.dll - - - ..\packages\xunit.extensibility.execution.2.2.0\lib\netstandard1.1\xunit.execution.dotnet.dll - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + True True Resources.resx - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -213,6 +188,17 @@ + + + 4.7.8 + + + 2.2.0 + + + 2.2.0 + + diff --git a/Tests/Tests/ByteToolTestFixture.cs b/Tests/Unit/ByteToolTestFixture.cs similarity index 100% rename from Tests/Tests/ByteToolTestFixture.cs rename to Tests/Unit/ByteToolTestFixture.cs diff --git a/Tests/Tests/Counter32TestFixture.cs b/Tests/Unit/Counter32TestFixture.cs similarity index 100% rename from Tests/Tests/Counter32TestFixture.cs rename to Tests/Unit/Counter32TestFixture.cs diff --git a/Tests/Tests/Counter64TestFixture.cs b/Tests/Unit/Counter64TestFixture.cs similarity index 100% rename from Tests/Tests/Counter64TestFixture.cs rename to Tests/Unit/Counter64TestFixture.cs diff --git a/Tests/Tests/DataFactoryTestFixture.cs b/Tests/Unit/DataFactoryTestFixture.cs similarity index 100% rename from Tests/Tests/DataFactoryTestFixture.cs rename to Tests/Unit/DataFactoryTestFixture.cs diff --git a/Tests/Tests/EndOfMibViewTestFixture.cs b/Tests/Unit/EndOfMibViewTestFixture.cs similarity index 100% rename from Tests/Tests/EndOfMibViewTestFixture.cs rename to Tests/Unit/EndOfMibViewTestFixture.cs diff --git a/Tests/Tests/Gauge32TestFixture.cs b/Tests/Unit/Gauge32TestFixture.cs similarity index 100% rename from Tests/Tests/Gauge32TestFixture.cs rename to Tests/Unit/Gauge32TestFixture.cs diff --git a/Tests/Tests/GetBulkRequestPduTestFixture.cs b/Tests/Unit/GetBulkRequestPduTestFixture.cs similarity index 100% rename from Tests/Tests/GetBulkRequestPduTestFixture.cs rename to Tests/Unit/GetBulkRequestPduTestFixture.cs diff --git a/Tests/Tests/GetNextRequestPduTestFixture.cs b/Tests/Unit/GetNextRequestPduTestFixture.cs similarity index 100% rename from Tests/Tests/GetNextRequestPduTestFixture.cs rename to Tests/Unit/GetNextRequestPduTestFixture.cs diff --git a/Tests/Tests/GetRequestPduTestFixture.cs b/Tests/Unit/GetRequestPduTestFixture.cs similarity index 100% rename from Tests/Tests/GetRequestPduTestFixture.cs rename to Tests/Unit/GetRequestPduTestFixture.cs diff --git a/Tests/Tests/HeaderTestFixture.cs b/Tests/Unit/HeaderTestFixture.cs similarity index 100% rename from Tests/Tests/HeaderTestFixture.cs rename to Tests/Unit/HeaderTestFixture.cs diff --git a/Tests/Tests/IPTestFixture.cs b/Tests/Unit/IPTestFixture.cs similarity index 100% rename from Tests/Tests/IPTestFixture.cs rename to Tests/Unit/IPTestFixture.cs diff --git a/Tests/Tests/InformRequestPduTestFixture.cs b/Tests/Unit/InformRequestPduTestFixture.cs similarity index 100% rename from Tests/Tests/InformRequestPduTestFixture.cs rename to Tests/Unit/InformRequestPduTestFixture.cs diff --git a/Tests/Tests/Integer32TestFixture.cs b/Tests/Unit/Integer32TestFixture.cs similarity index 100% rename from Tests/Tests/Integer32TestFixture.cs rename to Tests/Unit/Integer32TestFixture.cs diff --git a/Tests/Tests/MalformedPduTestFixture.cs b/Tests/Unit/MalformedPduTestFixture.cs similarity index 100% rename from Tests/Tests/MalformedPduTestFixture.cs rename to Tests/Unit/MalformedPduTestFixture.cs diff --git a/Tests/Unit/Messaging/GetRequestMessageTestFixture.cs b/Tests/Unit/Messaging/GetRequestMessageTestFixture.cs new file mode 100644 index 00000000..69a33b6f --- /dev/null +++ b/Tests/Unit/Messaging/GetRequestMessageTestFixture.cs @@ -0,0 +1,287 @@ +/* + * Created by SharpDevelop. + * User: lextm + * Date: 2008/4/28 + * Time: 18:35 + * + * To change this template use Tools | Options | Coding | Edit Standard Headers. + */ + +using System.Collections.Generic; +using System.Net; +using Lextm.SharpSnmpLib.Security; +using System.Net.Sockets; +using System; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; +using Lextm.SharpSnmpLib.Objects; +using Lextm.SharpSnmpLib.Pipeline; + +#pragma warning disable 1591 + +namespace Lextm.SharpSnmpLib.Messaging.Tests +{ + using Xunit; + + /// + /// Description of TestGetMessage. + /// + public class GetRequestMessageTestFixture + { +#if !NETSTANDARD + [Fact] + public void Test() + { + byte[] expected = Properties.Resources.get; + ISnmpMessage message = MessageFactory.ParseMessages(expected, new UserRegistry())[0]; + Assert.Equal(SnmpType.GetRequestPdu, message.TypeCode()); + GetRequestPdu pdu = (GetRequestPdu)message.Pdu(); + Assert.Equal(1, pdu.Variables.Count); + Variable v = pdu.Variables[0]; + Assert.Equal(new uint[] { 1, 3, 6, 1, 2, 1, 1, 6, 0 }, v.Id.ToNumerical()); + Assert.Equal(typeof(Null), v.Data.GetType()); + Assert.True(expected.Length >= message.ToBytes().Length); + } + + [Fact] + public void TestConstructor() + { + List list = new List(1) + { + new Variable(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 2, 1, 1, 6, 0}), + new Null()) + }; + GetRequestMessage message = new GetRequestMessage(0, VersionCode.V2, new OctetString("public"), list); + Assert.True(Properties.Resources.get.Length >= message.ToBytes().Length); + } +#endif + [Fact] + public void TestConstructorV3Auth1() + { + const string bytes = "30 73" + + "02 01 03 " + + "30 0F " + + "02 02 35 41 " + + "02 03 00 FF E3" + + "04 01 05" + + "02 01 03" + + "04 2E " + + "30 2C" + + "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " + + "02 01 0D " + + "02 01 57 " + + "04 05 6C 65 78 6C 69 " + + "04 0C 1C 6D 67 BF B2 38 ED 63 DF 0A 05 24 " + + "04 00 " + + "30 2D " + + "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " + + "04 00 " + + "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"; + ReportMessage report = new ReportMessage( + VersionCode.V3, + new Header( + new Integer32(13633), + new Integer32(0xFFE3), + 0), + new SecurityParameters( + new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), + new Integer32(0x0d), + new Integer32(0x57), + new OctetString("lexli"), + new OctetString(new byte[12]), + OctetString.Empty), + new Scope( + new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), + OctetString.Empty, + new ReportPdu( + 0x01AF, + ErrorCode.NoError, + 0, + new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) })), + DefaultPrivacyProvider.DefaultPair, + null); + + IPrivacyProvider privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass"))); + GetRequestMessage request = new GetRequestMessage( + VersionCode.V3, + 13633, + 0x01AF, + new OctetString("lexli"), + new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) }, + privacy, + Messenger.MaxMessageSize, + report); + + Assert.Equal(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel); + Assert.Equal(ByteTool.Convert(bytes), request.ToBytes()); + } +#if !NETSTANDARD + [Fact] + public void TestConstructorV2AuthMd5PrivDes() + { + const string bytes = "30 81 80 02 01 03 30 0F 02 02 6C 99 02 03 00 FF" + + "E3 04 01 07 02 01 03 04 38 30 36 04 0D 80 00 1F" + + "88 80 E9 63 00 00 D6 1F F4 49 02 01 14 02 01 35" + + "04 07 6C 65 78 6D 61 72 6B 04 0C 80 50 D9 A1 E7" + + "81 B6 19 80 4F 06 C0 04 08 00 00 00 01 44 2C A3" + + "B5 04 30 4B 4F 10 3B 73 E1 E4 BD 91 32 1B CB 41" + + "1B A1 C1 D1 1D 2D B7 84 16 CA 41 BF B3 62 83 C4" + + "29 C5 A4 BC 32 DA 2E C7 65 A5 3D 71 06 3C 5B 56" + + "FB 04 A4"; + MD5AuthenticationProvider auth = new MD5AuthenticationProvider(new OctetString("testpass")); + IPrivacyProvider privacy = new DESPrivacyProvider(new OctetString("passtest"), auth); + GetRequestMessage request = new GetRequestMessage( + VersionCode.V3, + new Header( + new Integer32(0x6C99), + new Integer32(0xFFE3), + Levels.Authentication | Levels.Privacy | Levels.Reportable), + new SecurityParameters( + new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), + new Integer32(0x14), + new Integer32(0x35), + new OctetString("lexmark"), + new OctetString(ByteTool.Convert("80 50 D9 A1 E7 81 B6 19 80 4F 06 C0")), + new OctetString(ByteTool.Convert("00 00 00 01 44 2C A3 B5"))), + new Scope( + new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), + OctetString.Empty, + new GetRequestPdu( + 0x3A25, + new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) })), + privacy, + null); + Assert.Equal(Levels.Authentication | Levels.Privacy | Levels.Reportable, request.Header.SecurityLevel); + Assert.Equal(ByteTool.Convert(bytes), request.ToBytes()); + } +#endif + [Fact] + public void TestConstructorV3AuthMd5() + { + const string bytes = "30 73" + + "02 01 03 " + + "30 0F " + + "02 02 35 41 " + + "02 03 00 FF E3" + + "04 01 05" + + "02 01 03" + + "04 2E " + + "30 2C" + + "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " + + "02 01 0D " + + "02 01 57 " + + "04 05 6C 65 78 6C 69 " + + "04 0C 1C 6D 67 BF B2 38 ED 63 DF 0A 05 24 " + + "04 00 " + + "30 2D " + + "04 0D 80 00 1F 88 80 E9 63 00 00 D6 1F F4 49 " + + "04 00 " + + "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"; + IPrivacyProvider pair = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass"))); + GetRequestMessage request = new GetRequestMessage( + VersionCode.V3, + new Header( + new Integer32(13633), + new Integer32(0xFFE3), + Levels.Authentication | Levels.Reportable), + new SecurityParameters( + new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), + new Integer32(0x0d), + new Integer32(0x57), + new OctetString("lexli"), + new OctetString(ByteTool.Convert("1C 6D 67 BF B2 38 ED 63 DF 0A 05 24")), + OctetString.Empty), + new Scope( + new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), + OctetString.Empty, + new GetRequestPdu( + 0x01AF, + new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null()) })), + pair, + null); + Assert.Equal(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel); + Assert.Equal(ByteTool.Convert(bytes), request.ToBytes()); + } + + [Fact] + public void TestConstructorV3AuthSha() + { + const string bytes = "30 77 02 01 03 30 0F 02 02 47 21 02 03 00 FF E3" + + "04 01 05 02 01 03 04 32 30 30 04 0D 80 00 1F 88" + + "80 E9 63 00 00 D6 1F F4 49 02 01 15 02 02 01 5B" + + "04 08 6C 65 78 74 75 64 69 6F 04 0C 7B 62 65 AE" + + "D3 8F E3 7D 58 45 5C 6C 04 00 30 2D 04 0D 80 00" + + "1F 88 80 E9 63 00 00 D6 1F F4 49 04 00 A0 1A 02" + + "02 56 FF 02 01 00 02 01 00 30 0E 30 0C 06 08 2B" + + "06 01 02 01 01 03 00 05 00"; + IPrivacyProvider pair = new DefaultPrivacyProvider(new SHA1AuthenticationProvider(new OctetString("password"))); + GetRequestMessage request = new GetRequestMessage( + VersionCode.V3, + new Header( + new Integer32(0x4721), + new Integer32(0xFFE3), + Levels.Authentication | Levels.Reportable), + new SecurityParameters( + new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), + new Integer32(0x15), + new Integer32(0x015B), + new OctetString("lextudio"), + new OctetString(ByteTool.Convert("7B 62 65 AE D3 8F E3 7D 58 45 5C 6C")), + OctetString.Empty), + new Scope( + new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00 00 D6 1F F4 49")), + OctetString.Empty, + new GetRequestPdu( + 0x56FF, + new List(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null()) })), + pair, + null); + Assert.Equal(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel); + Assert.Equal(ByteTool.Convert(bytes), request.ToBytes()); + } + + [Fact] + public void TestDiscoveryV3() + { + const string bytes = "30 3A 02 01 03 30 0F 02 02 6A 09 02 03 00 FF E3" + + " 04 01 04 02 01 03 04 10 30 0E 04 00 02 01 00 02" + + " 01 00 04 00 04 00 04 00 30 12 04 00 04 00 A0 0C" + + " 02 02 2C 6B 02 01 00 02 01 00 30 00"; + GetRequestMessage request = new GetRequestMessage( + VersionCode.V3, + new Header( + new Integer32(0x6A09), + new Integer32(0xFFE3), + Levels.Reportable), + new SecurityParameters( + OctetString.Empty, + Integer32.Zero, + Integer32.Zero, + OctetString.Empty, + OctetString.Empty, + OctetString.Empty), + new Scope( + OctetString.Empty, + OctetString.Empty, + new GetRequestPdu(0x2C6B, new List())), + DefaultPrivacyProvider.DefaultPair, + null + ); + string test = ByteTool.Convert(request.ToBytes()); + Assert.Equal(bytes, test); + } + + [Fact] + public void TestToBytes() + { + const string s = "30 27 02 01 01 04 06 70 75 62 6C 69 63 A0 1A 02" + + "02 4B ED 02 01 00 02 01 00 30 0E 30 0C 06 08 2B" + + "06 01 02 01 01 01 00 05 00 "; + byte[] expected = ByteTool.Convert(s); + GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) }); + Assert.Equal(expected, message.ToBytes()); + } + } +} +#pragma warning restore 1591 diff --git a/Tests/Messaging/Tests/ListenerBindingTestFixture.cs b/Tests/Unit/Messaging/ListenerBindingTestFixture.cs similarity index 100% rename from Tests/Messaging/Tests/ListenerBindingTestFixture.cs rename to Tests/Unit/Messaging/ListenerBindingTestFixture.cs diff --git a/Tests/Messaging/Tests/MessageFactoryTestFixture.cs b/Tests/Unit/Messaging/MessageFactoryTestFixture.cs similarity index 100% rename from Tests/Messaging/Tests/MessageFactoryTestFixture.cs rename to Tests/Unit/Messaging/MessageFactoryTestFixture.cs diff --git a/Tests/Messaging/Tests/NumberGeneratorTestFixture.cs b/Tests/Unit/Messaging/NumberGeneratorTestFixture.cs similarity index 100% rename from Tests/Messaging/Tests/NumberGeneratorTestFixture.cs rename to Tests/Unit/Messaging/NumberGeneratorTestFixture.cs diff --git a/Tests/Messaging/Tests/ResponseMessageTestFixture.cs b/Tests/Unit/Messaging/ResponseMessageTestFixture.cs similarity index 100% rename from Tests/Messaging/Tests/ResponseMessageTestFixture.cs rename to Tests/Unit/Messaging/ResponseMessageTestFixture.cs diff --git a/Tests/Messaging/Tests/TrapV1MessageTestFixture.cs b/Tests/Unit/Messaging/TrapV1MessageTestFixture.cs similarity index 100% rename from Tests/Messaging/Tests/TrapV1MessageTestFixture.cs rename to Tests/Unit/Messaging/TrapV1MessageTestFixture.cs diff --git a/Tests/Messaging/Tests/TrapV2MessageTestFixture.cs b/Tests/Unit/Messaging/TrapV2MessageTestFixture.cs similarity index 98% rename from Tests/Messaging/Tests/TrapV2MessageTestFixture.cs rename to Tests/Unit/Messaging/TrapV2MessageTestFixture.cs index 7c3c64ac..147edf2c 100644 --- a/Tests/Messaging/Tests/TrapV2MessageTestFixture.cs +++ b/Tests/Unit/Messaging/TrapV2MessageTestFixture.cs @@ -63,7 +63,7 @@ public void TestToBytes3() Integer32.Zero, Integer32.Zero, new OctetString("lextm"), - new OctetString(ByteTool.Convert("61A9A486AF4A861BD5C0BB1F")), + new OctetString(ByteTool.Convert("61A9A486AF4A861BD5C0BB1F")), new OctetString(ByteTool.Convert("0000000069D39B2A"))), new Scope(OctetString.Empty, OctetString.Empty, new TrapV2Pdu( @@ -71,8 +71,8 @@ public void TestToBytes3() new ObjectIdentifier("1.3.6"), 0, new List())), - privacy, - null); + privacy, + null); byte[] bytes = trap.ToBytes(); UserRegistry registry = new UserRegistry(); registry.Add(new OctetString("lextm"), privacy); diff --git a/Tests/Tests/NoSuchInstanceTestFixture.cs b/Tests/Unit/NoSuchInstanceTestFixture.cs similarity index 100% rename from Tests/Tests/NoSuchInstanceTestFixture.cs rename to Tests/Unit/NoSuchInstanceTestFixture.cs diff --git a/Tests/Tests/NoSuchObjectTestFixture.cs b/Tests/Unit/NoSuchObjectTestFixture.cs similarity index 100% rename from Tests/Tests/NoSuchObjectTestFixture.cs rename to Tests/Unit/NoSuchObjectTestFixture.cs diff --git a/Tests/Tests/NullTestFixture.cs b/Tests/Unit/NullTestFixture.cs similarity index 100% rename from Tests/Tests/NullTestFixture.cs rename to Tests/Unit/NullTestFixture.cs diff --git a/Tests/Tests/ObjectIdentifierTestFixture.cs b/Tests/Unit/ObjectIdentifierTestFixture.cs similarity index 100% rename from Tests/Tests/ObjectIdentifierTestFixture.cs rename to Tests/Unit/ObjectIdentifierTestFixture.cs diff --git a/Tests/Objects/Tests/SysContactTestFixture.cs b/Tests/Unit/Objects/SysContactTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysContactTestFixture.cs rename to Tests/Unit/Objects/SysContactTestFixture.cs diff --git a/Tests/Objects/Tests/SysDescrTestFixture.cs b/Tests/Unit/Objects/SysDescrTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysDescrTestFixture.cs rename to Tests/Unit/Objects/SysDescrTestFixture.cs diff --git a/Tests/Objects/Tests/SysLocationTestFixture.cs b/Tests/Unit/Objects/SysLocationTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysLocationTestFixture.cs rename to Tests/Unit/Objects/SysLocationTestFixture.cs diff --git a/Tests/Objects/Tests/SysNameTestFixture.cs b/Tests/Unit/Objects/SysNameTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysNameTestFixture.cs rename to Tests/Unit/Objects/SysNameTestFixture.cs diff --git a/Tests/Objects/Tests/SysORDescrTestFixture.cs b/Tests/Unit/Objects/SysORDescrTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysORDescrTestFixture.cs rename to Tests/Unit/Objects/SysORDescrTestFixture.cs diff --git a/Tests/Objects/Tests/SysORIDTestFixture.cs b/Tests/Unit/Objects/SysORIDTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysORIDTestFixture.cs rename to Tests/Unit/Objects/SysORIDTestFixture.cs diff --git a/Tests/Objects/Tests/SysORIndexTestFixture.cs b/Tests/Unit/Objects/SysORIndexTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysORIndexTestFixture.cs rename to Tests/Unit/Objects/SysORIndexTestFixture.cs diff --git a/Tests/Objects/Tests/SysORLastChangeTestFixture.cs b/Tests/Unit/Objects/SysORLastChangeTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysORLastChangeTestFixture.cs rename to Tests/Unit/Objects/SysORLastChangeTestFixture.cs diff --git a/Tests/Objects/Tests/SysORTableTestFixture.cs b/Tests/Unit/Objects/SysORTableTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysORTableTestFixture.cs rename to Tests/Unit/Objects/SysORTableTestFixture.cs diff --git a/Tests/Objects/Tests/SysORUpTimeTestFixture.cs b/Tests/Unit/Objects/SysORUpTimeTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysORUpTimeTestFixture.cs rename to Tests/Unit/Objects/SysORUpTimeTestFixture.cs diff --git a/Tests/Objects/Tests/SysObjectIdTestFixture.cs b/Tests/Unit/Objects/SysObjectIdTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysObjectIdTestFixture.cs rename to Tests/Unit/Objects/SysObjectIdTestFixture.cs diff --git a/Tests/Objects/Tests/SysServicesTestFixture.cs b/Tests/Unit/Objects/SysServicesTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysServicesTestFixture.cs rename to Tests/Unit/Objects/SysServicesTestFixture.cs diff --git a/Tests/Objects/Tests/SysUpTimeTestFixture.cs b/Tests/Unit/Objects/SysUpTimeTestFixture.cs similarity index 100% rename from Tests/Objects/Tests/SysUpTimeTestFixture.cs rename to Tests/Unit/Objects/SysUpTimeTestFixture.cs diff --git a/Tests/Tests/OctetStringTestFixture.cs b/Tests/Unit/OctetStringTestFixture.cs similarity index 100% rename from Tests/Tests/OctetStringTestFixture.cs rename to Tests/Unit/OctetStringTestFixture.cs diff --git a/Tests/Tests/OpaqueTestFixture.cs b/Tests/Unit/OpaqueTestFixture.cs similarity index 100% rename from Tests/Tests/OpaqueTestFixture.cs rename to Tests/Unit/OpaqueTestFixture.cs diff --git a/Tests/Pipeline/Tests/EngineGroupTestFixture.cs b/Tests/Unit/Pipeline/EngineGroupTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/EngineGroupTestFixture.cs rename to Tests/Unit/Pipeline/EngineGroupTestFixture.cs diff --git a/Tests/Pipeline/Tests/GetBulkMessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/GetBulkMessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/GetBulkMessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/GetBulkMessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/GetMessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/GetMessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/GetMessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/GetMessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/GetNextMessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/GetNextMessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/GetNextMessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/GetNextMessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/GetNextV1MessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/GetNextV1MessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/GetNextV1MessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/GetNextV1MessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/GetV1MessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/GetV1MessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/GetV1MessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/GetV1MessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/InformRequestMessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/InformRequestMessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/InformRequestMessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/InformRequestMessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/InformRequestMessageReceivedEventArgsTestFixture.cs b/Tests/Unit/Pipeline/InformRequestMessageReceivedEventArgsTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/InformRequestMessageReceivedEventArgsTestFixture.cs rename to Tests/Unit/Pipeline/InformRequestMessageReceivedEventArgsTestFixture.cs diff --git a/Tests/Pipeline/Tests/NormalSnmpContextTestFixture.cs b/Tests/Unit/Pipeline/NormalSnmpContextTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/NormalSnmpContextTestFixture.cs rename to Tests/Unit/Pipeline/NormalSnmpContextTestFixture.cs diff --git a/Tests/Pipeline/Tests/SetMessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/SetMessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/SetMessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/SetMessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/SetV1MessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/SetV1MessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/SetV1MessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/SetV1MessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/SnmpContextFactoryTestFixture.cs b/Tests/Unit/Pipeline/SnmpContextFactoryTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/SnmpContextFactoryTestFixture.cs rename to Tests/Unit/Pipeline/SnmpContextFactoryTestFixture.cs diff --git a/Tests/Pipeline/Tests/TrapV1MessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/TrapV1MessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/TrapV1MessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/TrapV1MessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/TrapV1MessageReceivedEventArgsTestFixture.cs b/Tests/Unit/Pipeline/TrapV1MessageReceivedEventArgsTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/TrapV1MessageReceivedEventArgsTestFixture.cs rename to Tests/Unit/Pipeline/TrapV1MessageReceivedEventArgsTestFixture.cs diff --git a/Tests/Pipeline/Tests/TrapV2MessageHandlerTestFixture.cs b/Tests/Unit/Pipeline/TrapV2MessageHandlerTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/TrapV2MessageHandlerTestFixture.cs rename to Tests/Unit/Pipeline/TrapV2MessageHandlerTestFixture.cs diff --git a/Tests/Pipeline/Tests/TrapV2MessageReceivedEventArgsTestFixture.cs b/Tests/Unit/Pipeline/TrapV2MessageReceivedEventArgsTestFixture.cs similarity index 100% rename from Tests/Pipeline/Tests/TrapV2MessageReceivedEventArgsTestFixture.cs rename to Tests/Unit/Pipeline/TrapV2MessageReceivedEventArgsTestFixture.cs diff --git a/Tests/Tests/ReportPduTestFixture.cs b/Tests/Unit/ReportPduTestFixture.cs similarity index 100% rename from Tests/Tests/ReportPduTestFixture.cs rename to Tests/Unit/ReportPduTestFixture.cs diff --git a/Tests/Tests/ResponsePduTestFixture.cs b/Tests/Unit/ResponsePduTestFixture.cs similarity index 100% rename from Tests/Tests/ResponsePduTestFixture.cs rename to Tests/Unit/ResponsePduTestFixture.cs diff --git a/Tests/Tests/ScopeTestFixture.cs b/Tests/Unit/ScopeTestFixture.cs similarity index 100% rename from Tests/Tests/ScopeTestFixture.cs rename to Tests/Unit/ScopeTestFixture.cs diff --git a/Tests/Security/Tests/AESPrivacyProviderTestFixture.cs b/Tests/Unit/Security/AESPrivacyProviderTestFixture.cs similarity index 100% rename from Tests/Security/Tests/AESPrivacyProviderTestFixture.cs rename to Tests/Unit/Security/AESPrivacyProviderTestFixture.cs diff --git a/Tests/Security/Tests/DESPrivacyProviderTestFixture.cs b/Tests/Unit/Security/DESPrivacyProviderTestFixture.cs similarity index 100% rename from Tests/Security/Tests/DESPrivacyProviderTestFixture.cs rename to Tests/Unit/Security/DESPrivacyProviderTestFixture.cs diff --git a/Tests/Unit/Security/DefaultAuthenticationProviderTestFixture.cs b/Tests/Unit/Security/DefaultAuthenticationProviderTestFixture.cs new file mode 100644 index 00000000..6fd1db9d --- /dev/null +++ b/Tests/Unit/Security/DefaultAuthenticationProviderTestFixture.cs @@ -0,0 +1,26 @@ +/* + * Created by SharpDevelop. + * User: lextm + * Date: 2010/12/5 + * Time: 15:23 + * + * To change this template use Tools | Options | Coding | Edit Standard Headers. + */ +using System; +using Xunit; + +namespace Lextm.SharpSnmpLib.Security.Tests +{ + public class DefaultAuthenticationProviderTestFixture + { + [Fact] + public void Test() + { + var provider = DefaultAuthenticationProvider.Instance; + Assert.Equal("Default authentication provider", provider.ToString()); + Assert.Throws(() => provider.PasswordToKey(null, null)); + Assert.Throws(() => provider.PasswordToKey(new byte[0], null)); + Assert.Equal(new byte[0], provider.PasswordToKey(new byte[0], new byte[0])); + } + } +} diff --git a/Tests/Security/Tests/DefaultPrivacyProviderTestFixture.cs b/Tests/Unit/Security/DefaultPrivacyProviderTestFixture.cs similarity index 100% rename from Tests/Security/Tests/DefaultPrivacyProviderTestFixture.cs rename to Tests/Unit/Security/DefaultPrivacyProviderTestFixture.cs diff --git a/Tests/Security/Tests/LevelsTestFixture.cs b/Tests/Unit/Security/LevelsTestFixture.cs similarity index 100% rename from Tests/Security/Tests/LevelsTestFixture.cs rename to Tests/Unit/Security/LevelsTestFixture.cs diff --git a/Tests/Security/Tests/MD5AuthenticationProviderTestFixture.cs b/Tests/Unit/Security/MD5AuthenticationProviderTestFixture.cs similarity index 70% rename from Tests/Security/Tests/MD5AuthenticationProviderTestFixture.cs rename to Tests/Unit/Security/MD5AuthenticationProviderTestFixture.cs index 1eb2acd1..8f390287 100644 --- a/Tests/Security/Tests/MD5AuthenticationProviderTestFixture.cs +++ b/Tests/Unit/Security/MD5AuthenticationProviderTestFixture.cs @@ -23,11 +23,6 @@ public void TestException() Assert.Throws(() => provider.PasswordToKey(null, null)); Assert.Throws(() => provider.PasswordToKey(new byte[0], null)); Assert.Throws(() => provider.PasswordToKey(new byte[0], new byte[0])); - - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, null, null, null, null)); - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, null, null, null)); - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), null, null)); - // Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), OctetString.Empty, null)); } [Fact] diff --git a/Tests/Security/Tests/PrivacyProviderExtensionTestFixture.cs b/Tests/Unit/Security/PrivacyProviderExtensionTestFixture.cs similarity index 100% rename from Tests/Security/Tests/PrivacyProviderExtensionTestFixture.cs rename to Tests/Unit/Security/PrivacyProviderExtensionTestFixture.cs diff --git a/Tests/Security/Tests/SHA1AuthenticationProviderTestFixture.cs b/Tests/Unit/Security/SHA1AuthenticationProviderTestFixture.cs similarity index 60% rename from Tests/Security/Tests/SHA1AuthenticationProviderTestFixture.cs rename to Tests/Unit/Security/SHA1AuthenticationProviderTestFixture.cs index 621764c1..16bfa53c 100644 --- a/Tests/Security/Tests/SHA1AuthenticationProviderTestFixture.cs +++ b/Tests/Unit/Security/SHA1AuthenticationProviderTestFixture.cs @@ -22,11 +22,6 @@ public void Test() Assert.Throws(() => provider.PasswordToKey(null, null)); Assert.Throws(() => provider.PasswordToKey(new byte[0], null)); Assert.Throws(() => provider.PasswordToKey(new byte[0], new byte[0])); - - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, null, null, null, null)); - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, null, null, null)); - Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), null, null)); - // Assert.Throws(() => provider.ComputeHash(VersionCode.V1, Header.Empty, SecurityParameters.Create(new OctetString("test")), OctetString.Empty, null)); } } } diff --git a/Tests/Security/Tests/SaltGeneratorTestFixture.cs b/Tests/Unit/Security/SaltGeneratorTestFixture.cs similarity index 100% rename from Tests/Security/Tests/SaltGeneratorTestFixture.cs rename to Tests/Unit/Security/SaltGeneratorTestFixture.cs diff --git a/Tests/Security/Tests/UserRegistryTestFixture.cs b/Tests/Unit/Security/UserRegistryTestFixture.cs similarity index 100% rename from Tests/Security/Tests/UserRegistryTestFixture.cs rename to Tests/Unit/Security/UserRegistryTestFixture.cs diff --git a/Tests/Security/Tests/UserTestFixture.cs b/Tests/Unit/Security/UserTestFixture.cs similarity index 100% rename from Tests/Security/Tests/UserTestFixture.cs rename to Tests/Unit/Security/UserTestFixture.cs diff --git a/Tests/Tests/SecurityParametersTestFixture.cs b/Tests/Unit/SecurityParametersTestFixture.cs similarity index 100% rename from Tests/Tests/SecurityParametersTestFixture.cs rename to Tests/Unit/SecurityParametersTestFixture.cs diff --git a/Tests/Tests/SequenceTestFixture.cs b/Tests/Unit/SequenceTestFixture.cs similarity index 100% rename from Tests/Tests/SequenceTestFixture.cs rename to Tests/Unit/SequenceTestFixture.cs diff --git a/Tests/Tests/SetRequestPduTestFixture.cs b/Tests/Unit/SetRequestPduTestFixture.cs similarity index 100% rename from Tests/Tests/SetRequestPduTestFixture.cs rename to Tests/Unit/SetRequestPduTestFixture.cs diff --git a/Tests/Tests/SnmpDataExtensionTestFixture.cs b/Tests/Unit/SnmpDataExtensionTestFixture.cs similarity index 100% rename from Tests/Tests/SnmpDataExtensionTestFixture.cs rename to Tests/Unit/SnmpDataExtensionTestFixture.cs diff --git a/Tests/Tests/StreamExtensionTestFixture.cs b/Tests/Unit/StreamExtensionTestFixture.cs similarity index 100% rename from Tests/Tests/StreamExtensionTestFixture.cs rename to Tests/Unit/StreamExtensionTestFixture.cs diff --git a/Tests/Tests/TimeticksTestFixture.cs b/Tests/Unit/TimeticksTestFixture.cs similarity index 100% rename from Tests/Tests/TimeticksTestFixture.cs rename to Tests/Unit/TimeticksTestFixture.cs diff --git a/Tests/Tests/TrapV1PduTestFixture.cs b/Tests/Unit/TrapV1PduTestFixture.cs similarity index 100% rename from Tests/Tests/TrapV1PduTestFixture.cs rename to Tests/Unit/TrapV1PduTestFixture.cs diff --git a/Tests/Tests/TrapV2PduTestFixture.cs b/Tests/Unit/TrapV2PduTestFixture.cs similarity index 100% rename from Tests/Tests/TrapV2PduTestFixture.cs rename to Tests/Unit/TrapV2PduTestFixture.cs diff --git a/Tests/Tests/VariableTestFixture.cs b/Tests/Unit/VariableTestFixture.cs similarity index 100% rename from Tests/Tests/VariableTestFixture.cs rename to Tests/Unit/VariableTestFixture.cs diff --git a/Tests/app.config b/Tests/app.config index 5e697b66..b46d57f7 100644 --- a/Tests/app.config +++ b/Tests/app.config @@ -1,15 +1,15 @@ - + - - + + - - + + - + diff --git a/Tests/packages.config b/Tests/packages.config deleted file mode 100644 index f4bb33c2..00000000 --- a/Tests/packages.config +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/lib/SharedAssemblyInfo.cs b/lib/SharedAssemblyInfo.cs index 5fe8358b..c12fb54d 100644 --- a/lib/SharedAssemblyInfo.cs +++ b/lib/SharedAssemblyInfo.cs @@ -7,5 +7,5 @@ // You can specify all the values or you can use the default the Revision and // Build Numbers by using the '*' as shown below: -[assembly: AssemblyVersion("9.1.000401.01")] -[assembly: AssemblyFileVersion("9.1.000401.01")] +[assembly: AssemblyVersion("9.1.000423.32")] +[assembly: AssemblyFileVersion("9.1.000423.32")] diff --git a/lib/SharedAssemblyInfo.vb b/lib/SharedAssemblyInfo.vb index 94c5b26b..e43e74f3 100644 --- a/lib/SharedAssemblyInfo.vb +++ b/lib/SharedAssemblyInfo.vb @@ -7,5 +7,5 @@ Imports System.Reflection ' You can specify all values by your own or you can build default build and revision ' numbers with the '*' character (the default): - - + +