From adacd8545fae60601257390f714f380cd2a49e8c Mon Sep 17 00:00:00 2001 From: Lex Li Date: Thu, 31 Oct 2024 01:41:59 -0400 Subject: [PATCH] Updated the packaging support files. --- LICENSE | 3 +- SharpSnmpLib/Messaging/Discoverer.cs | 2 +- SharpSnmpLib/Messaging/Net6Discoverer.cs | 4 +- SharpSnmpLib/OperationException.cs | 2 +- SharpSnmpLib/SharpSnmpLib.csproj | 6 ++- .../CSharpCore/Unit/DataFactoryTestFixture.cs | 52 +++++++++--------- .../Unit/MalformedPduTestFixture.cs | 2 +- .../Messaging/GetRequestMessageTestFixture.cs | 2 +- .../Messaging/MessageFactoryTestFixture.cs | 50 ++++++++--------- .../Messaging/NumberGeneratorTestFixture.cs | 2 +- .../Messaging/ResponseMessageTestFixture.cs | 2 +- .../Messaging/TrapV1MessageTestFixture.cs | 28 +++++----- .../Messaging/TrapV2MessageTestFixture.cs | 6 +-- .../Unit/OperationExceptionTestFixture.cs | 33 ------------ .../Unit/Security/SaltGeneratorTestFixture.cs | 4 +- Tests/CSharpCore/Unit/TrapV1PduTestFixture.cs | 48 ++++++++--------- global.json | 5 -- readme.md | 54 +++++++++++++++---- 18 files changed, 154 insertions(+), 151 deletions(-) delete mode 100644 global.json diff --git a/LICENSE b/LICENSE index 59225741d..066d81e4a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) -Copyright (c) 2008-2017 Malcolm Crowe, Lex Li, and other contributors. +Copyright (c) 2008-2024 Malcolm Crowe, Lex Li, and other contributors. +Copyright (c) 2018-2024 LeXtudio Inc. 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: diff --git a/SharpSnmpLib/Messaging/Discoverer.cs b/SharpSnmpLib/Messaging/Discoverer.cs index 666293b1a..a89052911 100644 --- a/SharpSnmpLib/Messaging/Discoverer.cs +++ b/SharpSnmpLib/Messaging/Discoverer.cs @@ -330,7 +330,7 @@ public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress } #if NET6_0_OR_GREATER - var source = new CancellationTokenSource(); + using var source = new CancellationTokenSource(); source.CancelAfter(interval); try { diff --git a/SharpSnmpLib/Messaging/Net6Discoverer.cs b/SharpSnmpLib/Messaging/Net6Discoverer.cs index cf6fb4423..6c16f3f6a 100644 --- a/SharpSnmpLib/Messaging/Net6Discoverer.cs +++ b/SharpSnmpLib/Messaging/Net6Discoverer.cs @@ -23,9 +23,9 @@ using System.Threading; using System.Threading.Tasks; +#if NET6_0_OR_GREATER namespace Lextm.SharpSnmpLib.Messaging { -#if NET6_0_OR_GREATER /// /// Discoverer class to discover SNMP agents in the same network. /// @@ -233,5 +233,5 @@ await Task.Factory.StartNew(() => HandleMessage(buffer, result.ReceivedBytes, (I } } } -#endif } +#endif diff --git a/SharpSnmpLib/OperationException.cs b/SharpSnmpLib/OperationException.cs index f4a7aac27..9f7debc7e 100644 --- a/SharpSnmpLib/OperationException.cs +++ b/SharpSnmpLib/OperationException.cs @@ -72,7 +72,7 @@ public OperationException(string message, Exception inner) : base(message, inner /// /// Info /// Context - internal protected OperationException(SerializationInfo info, StreamingContext context) + protected OperationException(SerializationInfo info, StreamingContext context) : base(info, context) { var content = info.GetString("Agent"); diff --git a/SharpSnmpLib/SharpSnmpLib.csproj b/SharpSnmpLib/SharpSnmpLib.csproj index 202047ab3..439f066a8 100644 --- a/SharpSnmpLib/SharpSnmpLib.csproj +++ b/SharpSnmpLib/SharpSnmpLib.csproj @@ -1,4 +1,4 @@ - + SharpSnmpLib Lextm.SharpSnmpLib @@ -31,6 +31,7 @@ File true MIT + readme.md False true latest @@ -59,6 +60,9 @@ all + + + diff --git a/Tests/CSharpCore/Unit/DataFactoryTestFixture.cs b/Tests/CSharpCore/Unit/DataFactoryTestFixture.cs index b21c09213..32b4dc3fe 100644 --- a/Tests/CSharpCore/Unit/DataFactoryTestFixture.cs +++ b/Tests/CSharpCore/Unit/DataFactoryTestFixture.cs @@ -23,66 +23,66 @@ public void TestException() Assert.Throws(() => DataFactory.CreateSnmpData((byte[])null)); Assert.Throws(() => DataFactory.CreateSnmpData(0, null)); } - + [Fact] public void TestCreateObjectIdentifier() { - byte[] expected = new byte[] {0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x90, 0x72, 0x87, 0x68, 0x02}; + byte[] expected = new byte[] { 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x90, 0x72, 0x87, 0x68, 0x02 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.ObjectIdentifier, data.TypeCode); ObjectIdentifier o = (ObjectIdentifier)data; Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, o.ToNumerical()); } - + [Fact] public void TestCreateObjectIdentifier2() { - byte[] expected = new Byte[] {0x06, 0x01, 0x00}; + byte[] expected = new Byte[] { 0x06, 0x01, 0x00 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.ObjectIdentifier, data.TypeCode); ObjectIdentifier o = (ObjectIdentifier)data; - Assert.Equal(new uint[] {0, 0}, o.ToNumerical()); + Assert.Equal(new uint[] { 0, 0 }, o.ToNumerical()); } - + [Fact] public void TestCreateNull() { - byte[] expected = new byte[] {0x05, 0x00}; + byte[] expected = new byte[] { 0x05, 0x00 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.Null, data.TypeCode); Null n = (Null)data; Assert.Equal(expected, n.ToBytes()); } - + [Fact] public void TestCreateInteger() { - byte[] expected = new byte[] {0x02, 0x01, 0x00}; + byte[] expected = new byte[] { 0x02, 0x01, 0x00 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.Integer32, data.TypeCode); Integer32 i = (Integer32)data; Assert.Equal(0, i.ToInt32()); } - + [Fact] public void TestCreateOctetString() { - byte[] expected = new byte[] {0x04, 0x06, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63}; + byte[] expected = new byte[] { 0x04, 0x06, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.OctetString, data.TypeCode); Assert.Equal("public", data.ToString()); } - + [Fact] public void TestCreateIP() { - byte[] expected = new byte[] { 0x40, 0x04, 0x7F, 0x00, 0x00, 0x01}; + byte[] expected = new byte[] { 0x40, 0x04, 0x7F, 0x00, 0x00, 0x01 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.IPAddress, data.TypeCode); IP a = (IP)data; Assert.Equal("127.0.0.1", a.ToString()); } - + [Fact] public void TestTimeticks() { @@ -92,7 +92,7 @@ public void TestTimeticks() TimeTicks t = (TimeTicks)data; Assert.Equal(16352U, t.ToUInt32()); } - + [Fact] public void TestVarbind() { @@ -103,17 +103,17 @@ public void TestVarbind() Assert.Equal(SnmpType.Sequence, data.TypeCode); Sequence a = (Sequence)data; Assert.Equal(2, a.Length); - + ISnmpData oid = a[0]; ISnmpData name = a[1]; Assert.Equal(SnmpType.ObjectIdentifier, oid.TypeCode); Assert.Equal(SnmpType.OctetString, name.TypeCode); ObjectIdentifier o = (ObjectIdentifier)oid; - Assert.Equal(new uint[] {1,3,6,1,4,1,2162,1001,21,0}, o.ToNumerical()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, o.ToNumerical()); OctetString s = (OctetString)name; Assert.Equal("TrapTest", s.ToString()); } - + [Fact] public void TestVarbindSection() { @@ -123,13 +123,13 @@ public void TestVarbindSection() 0x04, 0x08, 0x54, 0x72, 0x61, 0x70, 0x54, 0x65, 0x73, 0x74}; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.Sequence, data.TypeCode); - + Sequence a = (Sequence)data; Assert.Equal(1, a.Length); ISnmpData varbind = a[0]; Assert.Equal(SnmpType.Sequence, varbind.TypeCode); } - + [Fact] public void TestTrapv1Pdu() { @@ -145,16 +145,16 @@ public void TestTrapv1Pdu() 0x04, 0x08, 0x54, 0x72, 0x61, 0x70, 0x54, 0x65, 0x73, 0x74}; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.TrapV1Pdu, data.TypeCode); - + TrapV1Pdu t = (TrapV1Pdu)data; - Assert.Equal(new uint[] {1,3,6,1,4,1,2162,1000,2}, t.Enterprise.ToNumerical()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, t.Enterprise.ToNumerical()); Assert.Equal("127.0.0.1", t.AgentAddress.ToIPAddress().ToString()); Assert.Equal(GenericCode.EnterpriseSpecific, t.Generic); Assert.Equal(12, t.Specific); Assert.Equal(16352U, t.TimeStamp.ToUInt32()); - Assert.Equal(1, t.Variables.Count); + Assert.Single(t.Variables); } - + [Fact] public void TestTrapPacket() { @@ -174,7 +174,7 @@ public void TestTrapPacket() 0x04, 0x08, 0x54, 0x72, 0x61, 0x70, 0x54, 0x65, 0x73, 0x74}; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.Sequence, data.TypeCode); - + Sequence t = (Sequence)data; Assert.Equal(3, t.Length); ISnmpData version = t[0]; @@ -200,4 +200,4 @@ public void TestInformDiscovery() Assert.Equal(0U, newPdu.TimeStamp); } } -} \ No newline at end of file +} diff --git a/Tests/CSharpCore/Unit/MalformedPduTestFixture.cs b/Tests/CSharpCore/Unit/MalformedPduTestFixture.cs index f4b6ef4b3..a0c6481af 100644 --- a/Tests/CSharpCore/Unit/MalformedPduTestFixture.cs +++ b/Tests/CSharpCore/Unit/MalformedPduTestFixture.cs @@ -12,7 +12,7 @@ public void Test() Assert.Throws(() => { var test = pdu.ErrorIndex; }); Assert.Throws(() => { var test = pdu.ErrorStatus; }); Assert.Throws(() => pdu.AppendBytesTo(null)); - Assert.Equal(0, pdu.Variables.Count); + Assert.Empty(pdu.Variables); Assert.Equal(Integer32.Zero, pdu.RequestId); Assert.Equal(SnmpType.Unknown, pdu.TypeCode); Assert.Equal("Malformed PDU", pdu.ToString()); diff --git a/Tests/CSharpCore/Unit/Messaging/GetRequestMessageTestFixture.cs b/Tests/CSharpCore/Unit/Messaging/GetRequestMessageTestFixture.cs index 81248db37..4064bae49 100644 --- a/Tests/CSharpCore/Unit/Messaging/GetRequestMessageTestFixture.cs +++ b/Tests/CSharpCore/Unit/Messaging/GetRequestMessageTestFixture.cs @@ -29,7 +29,7 @@ public void Test() 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); + Assert.Single(pdu.Variables); 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()); diff --git a/Tests/CSharpCore/Unit/Messaging/MessageFactoryTestFixture.cs b/Tests/CSharpCore/Unit/Messaging/MessageFactoryTestFixture.cs index aabc8c744..170ed51b3 100644 --- a/Tests/CSharpCore/Unit/Messaging/MessageFactoryTestFixture.cs +++ b/Tests/CSharpCore/Unit/Messaging/MessageFactoryTestFixture.cs @@ -52,9 +52,9 @@ public void TestReportFailure() var users = new UserRegistry(); users.Add(new User(new OctetString(userName), priv)); var messages = MessageFactory.ParseMessages(bytes, users); - Assert.Equal(1, messages.Count); + Assert.Single(messages); var message = messages[0]; - Assert.Equal(1, message.Variables().Count); + Assert.Single(message.Variables()); } [Fact] @@ -68,9 +68,9 @@ public void TestReportFailure2() var users = new UserRegistry(); users.Add(new User(new OctetString(userName), priv)); var messages = MessageFactory.ParseMessages(bytes, users); - Assert.Equal(1, messages.Count); + Assert.Single(messages); var message = messages[0]; - Assert.Equal(1, message.Variables().Count); + Assert.Single(message.Variables()); Assert.Equal("not in time window", message.Variables()[0].Id.GetErrorMessage()); } @@ -93,7 +93,7 @@ public void TestString() string bytes = "30 29 02 01 00 04 06 70 75 62 6c 69 63 a0 1c 02 04 4f 89 fb dd" + Environment.NewLine + "02 01 00 02 01 00 30 0e 30 0c 06 08 2b 06 01 02 01 01 05 00 05 00"; IList messages = MessageFactory.ParseMessages(bytes, new UserRegistry()); - Assert.Equal(1, messages.Count); + Assert.Single(messages); GetRequestMessage m = (GetRequestMessage)messages[0]; Variable v = m.Variables()[0]; string i = v.Id.ToString(); @@ -108,7 +108,7 @@ public void TestBrokenString() { const string bytes = "30 39 02 01 01 04 06 70 75 62 6C 69 63 A7 2C 02 01 01 02 01 00 02 01 00 30 21 30 0D 06 08 2B 06 01 02 01 01"; IList messages = MessageFactory.ParseMessages(bytes, new UserRegistry()); - Assert.Equal(1, messages.Count); + Assert.Single(messages); } catch (Exception) { @@ -126,7 +126,7 @@ public void TestDiscovery() "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"; IList messages = MessageFactory.ParseMessages(bytes, new UserRegistry()); - Assert.Equal(1, messages.Count); + Assert.Single(messages); Assert.Equal(OctetString.Empty, messages[0].Parameters.UserName); } @@ -143,7 +143,7 @@ public void TestGetV3() UserRegistry registry = new UserRegistry(); registry.Add(new OctetString("lextm"), DefaultPrivacyProvider.DefaultPair); IList messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); GetRequestMessage get = (GetRequestMessage)messages[0]; Assert.Equal(27144, get.MessageId()); //Assert.Equal(SecurityLevel.None | SecurityLevel.Reportable, get.Level); @@ -171,7 +171,7 @@ public void TestGetRequestV3AuthPriv() var registry = new UserRegistry(); registry.Add(new OctetString("lexmark"), new DefaultPrivacyProvider(auth)); var messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); Assert.Equal(SnmpType.Unknown, messages[0].TypeCode()); //registry.Add(new OctetString("lexmark"), @@ -183,7 +183,7 @@ public void TestGetRequestV3AuthPriv() registry.Add(new OctetString("lexmark"), new DESPrivacyProvider(new OctetString("passtest"), auth)); messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); GetRequestMessage get = (GetRequestMessage)messages[0]; Assert.Equal(27801, get.MessageId()); //Assert.Equal(SecurityLevel.None | SecurityLevel.Reportable, get.Level); @@ -215,7 +215,7 @@ public void TestGetRequestV3AuthPrivAES() var registry = new UserRegistry(); registry.Add(new OctetString("usr-sha-aes"), privacy); var messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); GetRequestMessage get = (GetRequestMessage)messages[0]; Assert.Equal(1828028908, get.MessageId()); Assert.Equal("usr-sha-aes", get.Community().ToString()); @@ -305,7 +305,7 @@ public void TestGetRequestV3Auth() UserRegistry registry = new UserRegistry(); registry.Add(new OctetString("lexli"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass")))); IList messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); GetRequestMessage get = (GetRequestMessage)messages[0]; Assert.Equal(13633, get.MessageId()); //Assert.Equal(SecurityLevel.None | SecurityLevel.Reportable, get.Level); @@ -341,7 +341,7 @@ public void TestGetRequestV3AuthPriv_LocalizedKeys_Request() registry.Add(new OctetString("usr-sha-aes"), privacy); var messages = MessageFactory.ParseMessages(bytes, registry); var message = messages[0]; - Assert.Equal(1, messages.Count); + Assert.Single(messages); Assert.Equal(SnmpType.GetRequestPdu, message.TypeCode()); } @@ -371,7 +371,7 @@ public void TestGetRequestV3AuthPriv_LocalizedKeys_Response() registry.Add(new OctetString("usr-sha-aes"), privacy); var messages = MessageFactory.ParseMessages(bytes, registry); var message = messages[0]; - Assert.Equal(1, messages.Count); + Assert.Single(messages); Assert.Equal(SnmpType.ResponsePdu, message.TypeCode()); } @@ -386,7 +386,7 @@ public void TestResponseV1() ResponsePdu response = (ResponsePdu)pdu; Assert.Equal(Integer32.Zero, response.ErrorStatus); Assert.Equal(0, response.ErrorIndex.ToInt32()); - Assert.Equal(1, response.Variables.Count); + Assert.Single(response.Variables); Variable v = response.Variables[0]; Assert.Equal(new uint[] { 1, 3, 6, 1, 2, 1, 1, 6, 0 }, v.Id.ToNumerical()); Assert.Equal("Shanghai", v.Data.ToString()); @@ -407,7 +407,7 @@ public void TestGetResponseV3Error() UserRegistry registry = new UserRegistry(); registry.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair); IList messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); ISnmpMessage message = messages[0]; Assert.Equal("040D80001F8880E9630000D61FF449", message.Parameters.EngineId.ToHexString()); Assert.Equal(0, message.Parameters.EngineBoots.ToInt32()); @@ -444,12 +444,12 @@ public void TestGetResponseV3() "2B 06 01 02 01 01 03 00 43 03 05 E7 14"; UserRegistry registry = new UserRegistry(); var messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); Assert.Equal(SnmpType.Unknown, messages[0].TypeCode()); registry.Add(new OctetString("lextm"), DefaultPrivacyProvider.DefaultPair); messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); ISnmpMessage message = messages[0]; Assert.Equal("80001F8880E9630000D61FF449", message.Parameters.EngineId.ToHexString()); Assert.Equal(5, message.Parameters.EngineBoots.ToInt32()); @@ -472,12 +472,12 @@ public void TestDiscoveryResponse() "01 00 02 01 00 30 11 30 0F 06 0A 2B 06 01 06 03" + "0F 01 01 04 00 41 01 03"; IList messages = MessageFactory.ParseMessages(bytes, new UserRegistry()); - Assert.Equal(1, messages.Count); + Assert.Single(messages); Assert.Equal(5, messages[0].Parameters.EngineBoots.ToInt32()); Assert.Equal("80001F8880E9630000D61FF449", messages[0].Parameters.EngineId.ToHexString()); Assert.Equal(3867, messages[0].Parameters.EngineTime.ToInt32()); Assert.Equal(ErrorCode.NoError, messages[0].Pdu().ErrorStatus.ToErrorCode()); - Assert.Equal(1, messages[0].Pdu().Variables.Count); + Assert.Single(messages[0].Pdu().Variables); Variable v = messages[0].Pdu().Variables[0]; Assert.Equal("1.3.6.1.6.3.15.1.1.4.0", v.Id.ToString()); @@ -496,7 +496,7 @@ public void TestTrapV3() UserRegistry registry = new UserRegistry(); registry.Add(new OctetString("lextm"), DefaultPrivacyProvider.DefaultPair); IList messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); ISnmpMessage message = messages[0]; Assert.Equal("80001F8880E9630000D61FF449", message.Parameters.EngineId.ToHexString()); Assert.Equal(0, message.Parameters.EngineBoots.ToInt32()); @@ -518,7 +518,7 @@ public void TestTrapV3Auth() UserRegistry registry = new UserRegistry(); registry.Add(new OctetString("lextm"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication")))); IList messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); ISnmpMessage message = messages[0]; Assert.Equal("80001F8880E9630000D61FF449", message.Parameters.EngineId.ToHexString()); Assert.Equal(0, message.Parameters.EngineBoots.ToInt32()); @@ -540,7 +540,7 @@ public void TestTrapV3AuthBytes() SHA1AuthenticationProvider authen = new SHA1AuthenticationProvider(new OctetString("testpass")); registry.Add(new OctetString("test"), new DefaultPrivacyProvider(authen)); IList messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); ISnmpMessage message = messages[0]; Assert.Equal("80001299030005B706CF69", message.Parameters.EngineId.ToHexString()); Assert.Equal(41, message.Parameters.EngineBoots.ToInt32()); @@ -569,7 +569,7 @@ public void TestTrapV3AuthPriv() UserRegistry registry = new UserRegistry(); registry.Add(new OctetString("lextm"), new DESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication")))); IList messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); ISnmpMessage message = messages[0]; Assert.Equal("80001F8880E9630000D61FF449", message.Parameters.EngineId.ToHexString()); Assert.Equal(0, message.Parameters.EngineBoots.ToInt32()); @@ -579,7 +579,7 @@ public void TestTrapV3AuthPriv() Assert.Equal("0000000069D39B2A", message.Parameters.PrivacyParameters.ToHexString()); Assert.Equal("", message.Scope.ContextEngineId.ToHexString()); // SNMP#NET returns string.Empty here. Assert.Equal("", message.Scope.ContextName.ToHexString()); - Assert.Equal(0, message.Scope.Pdu.Variables.Count); + Assert.Empty(message.Scope.Pdu.Variables); Assert.Equal(1004947569, message.MessageId()); Assert.Equal(234419641, message.RequestId()); } diff --git a/Tests/CSharpCore/Unit/Messaging/NumberGeneratorTestFixture.cs b/Tests/CSharpCore/Unit/Messaging/NumberGeneratorTestFixture.cs index 50face0fc..e14103a47 100644 --- a/Tests/CSharpCore/Unit/Messaging/NumberGeneratorTestFixture.cs +++ b/Tests/CSharpCore/Unit/Messaging/NumberGeneratorTestFixture.cs @@ -16,7 +16,7 @@ public void Test(int count) ng.SetSalt(0); var list = Enumerable.Range(1, count).AsParallel().Select(x => ng.NextId); var dupes = list.GroupBy(x => x).Where(g => g.Count() > 1).Select(x => x.Key); - Assert.Equal(0, dupes.Count()); + Assert.Empty(dupes); } //[Theory] diff --git a/Tests/CSharpCore/Unit/Messaging/ResponseMessageTestFixture.cs b/Tests/CSharpCore/Unit/Messaging/ResponseMessageTestFixture.cs index aedecb41f..ce47c11ce 100644 --- a/Tests/CSharpCore/Unit/Messaging/ResponseMessageTestFixture.cs +++ b/Tests/CSharpCore/Unit/Messaging/ResponseMessageTestFixture.cs @@ -42,7 +42,7 @@ public void TestConstructor() var registry = new UserRegistry(); registry.Add(new OctetString("lextm"), DefaultPrivacyProvider.DefaultPair); var messages = MessageFactory.ParseMessages(response.ToBytes(), registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); } } } diff --git a/Tests/CSharpCore/Unit/Messaging/TrapV1MessageTestFixture.cs b/Tests/CSharpCore/Unit/Messaging/TrapV1MessageTestFixture.cs index 4f99e690b..737624f3e 100644 --- a/Tests/CSharpCore/Unit/Messaging/TrapV1MessageTestFixture.cs +++ b/Tests/CSharpCore/Unit/Messaging/TrapV1MessageTestFixture.cs @@ -12,12 +12,12 @@ public class TrapV1MessageTestFixture [Fact] public void TestSendTrap() { - TrapV1Message message = new TrapV1Message(VersionCode.V1, + TrapV1Message message = new TrapV1Message(VersionCode.V1, IPAddress.Parse("127.0.0.1"), new OctetString("public"), - new ObjectIdentifier(new uint[] {1,3,6}), + new ObjectIdentifier(new uint[] { 1, 3, 6 }), GenericCode.AuthenticationFailure, - 0, + 0, 0, new List()); byte[] bytes = message.ToBytes(); @@ -28,9 +28,9 @@ public void TestSendTrap() Assert.Equal(0, m.Specific); Assert.Equal("public", m.Community.ToString()); Assert.Equal(IPAddress.Parse("127.0.0.1"), m.AgentAddress); - Assert.Equal(new uint[] {1,3,6}, m.Enterprise.ToNumerical()); + Assert.Equal(new uint[] { 1, 3, 6 }, m.Enterprise.ToNumerical()); Assert.Equal(0U, m.TimeStamp); - Assert.Equal(0, m.Variables().Count); + Assert.Empty(m.Variables()); } #if !NETSTANDARD [Fact] @@ -43,22 +43,22 @@ public void TestParseNoVarbind() Assert.Equal(12, message.Specific); Assert.Equal("public", message.Community.ToString()); Assert.Equal(IPAddress.Parse("127.0.0.1"), message.AgentAddress); - Assert.Equal(new uint[] { 1,3 }, message.Enterprise.ToNumerical()); + Assert.Equal(new uint[] { 1, 3 }, message.Enterprise.ToNumerical()); Assert.Equal(16352U, message.TimeStamp); - Assert.Equal(0, message.Variables().Count); + Assert.Empty(message.Variables()); } - + [Fact] public void TestParseOneVarbind() { byte[] buffer = File.ReadAllBytes(Path.Combine("Resources", "onevarbind.dat")); TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(buffer, new UserRegistry())[0]; - Assert.Equal(1, message.Variables().Count); + Assert.Single(message.Variables()); Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical()); Assert.Equal("TrapTest", message.Variables()[0].Data.ToString()); - Assert.Equal(new uint[] {1,3,6,1,4,1,2162,1001,21,0}, message.Variables()[0].Id.ToNumerical()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical()); } - + [Fact] public void TestParseTwoVarbinds() { @@ -66,10 +66,10 @@ public void TestParseTwoVarbinds() TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(buffer, new UserRegistry())[0]; Assert.Equal(2, message.Variables().Count); Assert.Equal("TrapTest", message.Variables()[0].Data.ToString()); - Assert.Equal(new uint[] {1,3,6,1,4,1,2162,1001,21,0}, message.Variables()[0].Id.ToNumerical()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical()); Assert.Equal("TrapTest", message.Variables()[1].Data.ToString()); - Assert.Equal(new uint[] {1,3,6,1,4,1,2162,1001,22,0}, message.Variables()[1].Id.ToNumerical()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 22, 0 }, message.Variables()[1].Id.ToNumerical()); } #endif } -} \ No newline at end of file +} diff --git a/Tests/CSharpCore/Unit/Messaging/TrapV2MessageTestFixture.cs b/Tests/CSharpCore/Unit/Messaging/TrapV2MessageTestFixture.cs index 14c0b09bd..46a965226 100644 --- a/Tests/CSharpCore/Unit/Messaging/TrapV2MessageTestFixture.cs +++ b/Tests/CSharpCore/Unit/Messaging/TrapV2MessageTestFixture.cs @@ -28,7 +28,7 @@ public void TestToBytes() ); Assert.Equal(ByteTool.Convert(File.ReadAllBytes(Path.Combine("Resources", "trapv3"))), ByteTool.Convert(trap.ToBytes())); } - + [Fact] public void TestToBytes2() { @@ -85,7 +85,7 @@ public void TestToBytes3() UserRegistry registry = new UserRegistry(); registry.Add(new OctetString("lextm"), privacy); IList messages = MessageFactory.ParseMessages(bytes, registry); - Assert.Equal(1, messages.Count); + Assert.Single(messages); ISnmpMessage message = messages[0]; Assert.Equal("80001F8880E9630000D61FF449", message.Parameters.EngineId.ToHexString()); Assert.Equal(0, message.Parameters.EngineBoots.ToInt32()); @@ -95,7 +95,7 @@ public void TestToBytes3() Assert.Equal("0000000069D39B2A", message.Parameters.PrivacyParameters.ToHexString()); Assert.Equal("", message.Scope.ContextEngineId.ToHexString()); // SNMP#NET returns string.Empty here. Assert.Equal("", message.Scope.ContextName.ToHexString()); - Assert.Equal(0, message.Scope.Pdu.Variables.Count); + Assert.Empty(message.Scope.Pdu.Variables); Assert.Equal(1004947569, message.MessageId()); Assert.Equal(234419641, message.RequestId()); } diff --git a/Tests/CSharpCore/Unit/OperationExceptionTestFixture.cs b/Tests/CSharpCore/Unit/OperationExceptionTestFixture.cs index 38b042850..8ea54a69c 100644 --- a/Tests/CSharpCore/Unit/OperationExceptionTestFixture.cs +++ b/Tests/CSharpCore/Unit/OperationExceptionTestFixture.cs @@ -38,39 +38,6 @@ public void TestConstructorWithMessageAndInnerException() Assert.Null(ex.Agent); } - [Fact] - public void TestConstructorWithSerializationInfoAndStreamingContext() - { - var info = new SerializationInfo(typeof(OperationException), new FormatterConverter()); - info.AddValue("Message", "Operation failed."); - info.AddValue("InnerException", null); - info.AddValue("HelpURL", null); - info.AddValue("StackTraceString", null); - info.AddValue("RemoteStackTraceString", null); - info.AddValue("HResult", -2146233088); - info.AddValue("Source", null); - info.AddValue("Agent", null); - info.AddValue("ClassName", typeof(OperationException).FullName); - info.AddValue("RemoteStackIndex", 0); - info.AddValue("ExceptionMethod", null); - var context = new StreamingContext(); - var ex = new OperationException(info, context); - Assert.Equal("Operation failed.", ex.Message); - Assert.Null(ex.InnerException); - Assert.Null(ex.Agent); - } - - [Fact] - public void TestGetObjectData() - { - var info = new SerializationInfo(typeof(OperationException), new FormatterConverter()); - var context = new StreamingContext(); - var ex = new OperationException("Operation failed."); - ex.GetObjectData(info, context); - Assert.Equal("Operation failed.", info.GetString("Message")); - Assert.Null(info.GetString("Agent")); - } - [Fact] public void TestCreate() { diff --git a/Tests/CSharpCore/Unit/Security/SaltGeneratorTestFixture.cs b/Tests/CSharpCore/Unit/Security/SaltGeneratorTestFixture.cs index f6b218a36..e2756685c 100644 --- a/Tests/CSharpCore/Unit/Security/SaltGeneratorTestFixture.cs +++ b/Tests/CSharpCore/Unit/Security/SaltGeneratorTestFixture.cs @@ -25,7 +25,7 @@ public void Test() var second = gen.GetSaltBytes(); Assert.NotEqual(first, second); Assert.Equal("Salt generator", gen.ToString()); - + gen.SetSalt(long.MaxValue); Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, gen.GetSaltBytes()); } @@ -38,7 +38,7 @@ public void TestNormal(int count) ng.SetSalt(0); var list = Enumerable.Range(1, count).AsParallel().Select(x => ng.NextSalt); var dupes = list.GroupBy(x => x).Where(g => g.Count() > 1).Select(x => x.Key); - Assert.Equal(0, dupes.Count()); + Assert.Empty(dupes); } [Theory] diff --git a/Tests/CSharpCore/Unit/TrapV1PduTestFixture.cs b/Tests/CSharpCore/Unit/TrapV1PduTestFixture.cs index d93c8b912..96456fb23 100644 --- a/Tests/CSharpCore/Unit/TrapV1PduTestFixture.cs +++ b/Tests/CSharpCore/Unit/TrapV1PduTestFixture.cs @@ -22,51 +22,51 @@ public class TrapV1PduTestFixture public void TestException() { Assert.Throws(() => new TrapV1Pdu(null, null)); - Assert.Throws(() => new TrapV1Pdu(new Tuple(0, new byte[] { 0 }),null)); + Assert.Throws(() => new TrapV1Pdu(new Tuple(0, new byte[] { 0 }), null)); Assert.Throws(() => new TrapV1Pdu((ObjectIdentifier)null, null, null, null, null, null)); - Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}), null, null, null, null, null)); - Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}), + Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }), null, null, null, null, null)); + Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }), new IP("127.0.0.1"), null, null, null, null)); - Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}), + Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }), new IP("127.0.0.1"), new Integer32((int)GenericCode.EnterpriseSpecific), null, null, null)); - Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}), + Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }), new IP("127.0.0.1"), new Integer32((int)GenericCode.EnterpriseSpecific), new Integer32(12), null, null)); - Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}), + Assert.Throws(() => new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }), new IP("127.0.0.1"), new Integer32((int)GenericCode.EnterpriseSpecific), new Integer32(12), new TimeTicks(16352), null)); - Variable v = new Variable(new ObjectIdentifier(new uint[] {1,3,6,1,4,1,2162,1001,21,0}), + Variable v = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }), new OctetString("TrapTest")); - List vList = new List {v}; + List vList = new List { v }; - TrapV1Pdu pdu = new TrapV1Pdu(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}), + TrapV1Pdu pdu = new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }), new IP("127.0.0.1"), new Integer32((int)GenericCode.EnterpriseSpecific), new Integer32(12), new TimeTicks(16352), vList); Assert.Throws(() => pdu.AppendBytesTo(null)); - + Assert.Throws(() => { var test = pdu.RequestId; }); Assert.Throws(() => { var test = pdu.ErrorIndex; }); Assert.Throws(() => { var test = pdu.ErrorStatus; }); } - + [Fact] public void TestToTrapMessage() { - Variable v = new Variable(new ObjectIdentifier(new uint[] {1,3,6,1,4,1,2162,1001,21,0}), + Variable v = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }), new OctetString("TrapTest")); - List vList = new List {v}; + List vList = new List { v }; - TrapV1Pdu pdu = new TrapV1Pdu(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}, + TrapV1Pdu pdu = new TrapV1Pdu(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, new IP("127.0.0.1"), new Integer32((int)GenericCode.EnterpriseSpecific), new Integer32(12), @@ -78,21 +78,21 @@ public void TestToTrapMessage() Assert.Equal(GenericCode.EnterpriseSpecific, message.Generic); Assert.Equal(12, message.Specific); Assert.Equal(16352U, message.TimeStamp); - Assert.Equal(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}, message.Enterprise.ToNumerical()); - Assert.Equal(1, message.Variables().Count); - Assert.Equal(new uint[] {1,3,6,1,4,1,2162,1001,21,0}, message.Variables()[0].Id.ToNumerical()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical()); + Assert.Single(message.Variables()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical()); Assert.Equal("TrapTest", message.Variables()[0].Data.ToString()); Assert.Equal("SNMPv1 TRAP PDU: agent address: 127.0.0.1; time stamp: 00:02:43.5200000; enterprise: 1.3.6.1.4.1.2162.1000.2; generic: EnterpriseSpecific; specific: 12; varbind count: 1", pdu.ToString()); } - + [Fact] public void TestToTrapMessageChinese() { - Variable v = new Variable(new ObjectIdentifier(new uint[] {1,3,6,1,4,1,2162,1001,21,0}), + Variable v = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }), new OctetString("嶄忽", Encoding.Unicode)); - List vList = new List {v}; + List vList = new List { v }; - TrapV1Pdu pdu = new TrapV1Pdu(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}), + TrapV1Pdu pdu = new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }), new IP("127.0.0.1"), new Integer32((int)GenericCode.EnterpriseSpecific), new Integer32(12), @@ -104,9 +104,9 @@ public void TestToTrapMessageChinese() Assert.Equal(GenericCode.EnterpriseSpecific, message.Generic); Assert.Equal(12, message.Specific); Assert.Equal(16352U, message.TimeStamp); - Assert.Equal(new uint[] {1, 3, 6, 1, 4, 1, 2162, 1000, 2}, message.Enterprise.ToNumerical()); - Assert.Equal(1, message.Variables().Count); - Assert.Equal(new uint[] {1,3,6,1,4,1,2162,1001,21,0}, message.Variables()[0].Id.ToNumerical()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical()); + Assert.Single(message.Variables()); + Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical()); Assert.Equal("嶄忽", ((OctetString)message.Variables()[0].Data).ToString(Encoding.Unicode)); } } diff --git a/global.json b/global.json deleted file mode 100644 index 5cfa01d9c..000000000 --- a/global.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "msbuild-sdks": { - "MSBuild.Sdk.Extras": "3.0.44" - } -} diff --git a/readme.md b/readme.md index 9396066b0..f7ba4769c 100644 --- a/readme.md +++ b/readme.md @@ -1,17 +1,53 @@ -# C# SNMP Library (#SNMP) +# C# SNMP Library (aka #SNMP) [![NuGet Version](https://img.shields.io/nuget/v/Lextm.SharpSnmpLib.svg?style=flat-square)](https://www.nuget.org/packages/Lextm.SharpSnmpLib/) [![Nuget](https://img.shields.io/nuget/dt/Lextm.SharpSnmpLib?style=flat-square)](https://www.nuget.org/packages/Lextm.SharpSnmpLib/) [![Build](https://img.shields.io/github/actions/workflow/status/lextudio/sharpsnmplib/dotnetcore.yml?branch=master&style=flat-square)](https://github.com/lextudio/sharpsnmplib/actions/workflows/dotnetcore.yml) [![Coverage](https://img.shields.io/coveralls/github/lextudio/sharpsnmplib?style=flat-square)](https://coveralls.io/github/lextudio/sharpsnmplib) -This is the source code repository of C# SNMP Library (#SNMP). Visual Studio 2022 and .NET 8 SDK is required to compile it on Windows. +C# SNMP Library (#SNMP) is a comprehensive SNMP (Simple Network Management Protocol) library for C# and .NET, allowing you to easily integrate SNMP functionalities into your applications. -## Useful Links +## Features -* You can visit [C# SNMP Library homepage](https://sharpsnmp.com). -* [The documentation site is here](https://docs.sharpsnmp.com). -* [API references can be found here](https://help.sharpsnmp.com). -* Sample projects can be found in [their repo](https://github.com/lextudio/sharpsnmplib-samples). -* Issues can be created directly [at GitHub](https://github.com/lextudio/sharpsnmplib/issues/new). -* Priority support can be purchased separately from [LeXtudio Inc.](https://support.lextudio.com). +- **SNMP v1, v2c, and v3 Support:** Full support for different SNMP versions. +- **Agent and Manager:** Enable both SNMP agent and manager development. +- **Asynchronous Operations:** Utilize async/await for non-blocking operations with easy cancellation support. +- **Extensible:** Easily extend and customize to fit your needs. +- **Cross-Platform:** Compatible with latest platforms of .NET and .NET Framework. + +Enterprise MIB support is available in the [Pro Edition](https://pro.sharpsnmp.com). + +## Installation + +Install the package via the .NET CLI: + +```bash +dotnet add package Lextm.SharpSnmpLib +``` + +Or via NuGet Package Manager: + +```bash +Install-Package Lextm.SharpSnmpLib +``` + +## Documentation + +Comprehensive documentation is available at [C# SNMP Library Docs](https://docs.sharpsnmp.com). + +[API references can be found here](https://help.sharpsnmp.com). + +## Samples + +Sample projects can be found in the [samples repository](https://github.com/lextudio/sharpsnmplib-samples). + +## License + +C# SNMP Library is licensed under the MIT/X11 license. + +## Support + +For technical support, please visit [this page](https://docs.lextudio.com/sharpsnmplib/support/services), or contact [LeXtudio Inc.](https://www.lextudio.com). + +Copyright (c) 2008-2024 Malcolm Crowe, Lex Li, and other contributors. +Copyright (c) 2018-2024 LeXtudio Inc.