diff --git a/Services/AdminWebApp/src/routes/device/+page.svelte b/Services/AdminWebApp/src/routes/device/+page.svelte index 41c92d5..e4ecfd8 100644 --- a/Services/AdminWebApp/src/routes/device/+page.svelte +++ b/Services/AdminWebApp/src/routes/device/+page.svelte @@ -62,6 +62,6 @@ - + diff --git a/Services/Netmon.SNMPPolling/Program.cs b/Services/Netmon.SNMPPolling/Program.cs index f94a75e..e843a26 100644 --- a/Services/Netmon.SNMPPolling/Program.cs +++ b/Services/Netmon.SNMPPolling/Program.cs @@ -87,4 +87,7 @@ app.UseMiddleware(); } -app.Run(); \ No newline at end of file +app.Run(); + + +public partial class SNMPPollingProgram { } \ No newline at end of file diff --git a/Services/Netmon.SNMPPolling/SNMP/Converter/Component/MIBCpuConverter.cs b/Services/Netmon.SNMPPolling/SNMP/Converter/Component/MIBCpuConverter.cs index 97eb0dd..a3de98c 100644 --- a/Services/Netmon.SNMPPolling/SNMP/Converter/Component/MIBCpuConverter.cs +++ b/Services/Netmon.SNMPPolling/SNMP/Converter/Component/MIBCpuConverter.cs @@ -13,43 +13,40 @@ public class MIBCpuConverter : IMIBComponentConverter { public List ConvertMIBsToComponent(List mibs) { - Cpu? cpu = null; + Cpu? cpu = new() + { + Index = 1, + Metrics = new List(), + Cores = new List() + }; HostResourcesMIB? hostResourcesMIB = mibs.OfType().FirstOrDefault(); if (hostResourcesMIB != null && hostResourcesMIB.HrDevice.HrProcessorTable.HrProcessorEntries.Any()) { - cpu = new Cpu - { - Index = 1, - Cores = hostResourcesMIB.HrDevice.HrProcessorTable.HrProcessorEntries - .Select(e => new CpuCore + cpu.Cores = hostResourcesMIB.HrDevice.HrProcessorTable.HrProcessorEntries + .Select(e => new CpuCore + { + Index = e.HrProcessorIndex.ToInt32(), + Name = hostResourcesMIB.HrDevice.HrDeviceTable.HrDeviceEntries + .FirstOrDefault(e1 => e1.HrDeviceIndex.ToInt32() == e.HrProcessorIndex.ToInt32())?.HrDeviceDescr + .ToString() ?? "Unknown", + Metrics = new List { - Index = e.HrProcessorIndex.ToInt32(), - Name = hostResourcesMIB.HrDevice.HrDeviceTable.HrDeviceEntries - .FirstOrDefault(e1 => e1.HrDeviceIndex.ToInt32() == e.HrProcessorIndex.ToInt32())?.HrDeviceDescr - .ToString() ?? "Unknown", - Metrics = new List + new CpuCoreMetric { - new CpuCoreMetric - { - Load = e.HrProcessorLoad.ToInt32() - } + Load = e.HrProcessorLoad.ToInt32() } - }) - .Cast() - .ToList() - }; + } + }) + .Cast() + .ToList(); } UCDavisMIB? ucDavisMIB = mibs.OfType().FirstOrDefault(); if (ucDavisMIB != null && ucDavisMIB.LaLoadTable.LaLoadEntries.Any()) { - cpu ??= new Cpu - { - Index = 1 - }; List laLoadEntries = ucDavisMIB.LaLoadTable.LaLoadEntries; CpuMetric cpuMetric = new() { @@ -60,6 +57,6 @@ public List ConvertMIBsToComponent(List mibs) cpu.Metrics = new List { cpuMetric }; } - return cpu == null ? new List() : new List { cpu }; + return !cpu.Cores.Any() && !cpu.Metrics.Any() ? new List() : new List { cpu }; } } \ No newline at end of file diff --git a/Services/Netmon.SNMPPolling/SNMP/Converter/Device/MIBDeviceConverter.cs b/Services/Netmon.SNMPPolling/SNMP/Converter/Device/MIBDeviceConverter.cs index 89d0681..98fcea8 100644 --- a/Services/Netmon.SNMPPolling/SNMP/Converter/Device/MIBDeviceConverter.cs +++ b/Services/Netmon.SNMPPolling/SNMP/Converter/Device/MIBDeviceConverter.cs @@ -11,7 +11,6 @@ public class MIBDeviceConverter : IMIBDeviceConverter { public IDevice ConvertMIBsToDevice(SNMPConnectionInfo connectionInfo, List mibs) { - Console.WriteLine("Found {0} MIBs", mibs.Count); IDevice device = new Models.Device.Device { IpAddress = connectionInfo.IpAddress, diff --git a/Tests/Netmon.SNMPPolling.Tests/ExampleTest.cs b/Tests/Netmon.SNMPPolling.Tests/ExampleTest.cs deleted file mode 100644 index d8cc996..0000000 --- a/Tests/Netmon.SNMPPolling.Tests/ExampleTest.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Netmon.SNMPPolling.Tests; - -[TestFixture] -public class ExampleTest -{ - [Test] - public void Example() - { - Assert.Pass(); - } -} \ No newline at end of file diff --git a/Tests/Netmon.SNMPPolling.Tests/Integration/SNMPDiscoverIntegrationTests.cs b/Tests/Netmon.SNMPPolling.Tests/Integration/SNMPDiscoverIntegrationTests.cs new file mode 100644 index 0000000..0cab8d7 --- /dev/null +++ b/Tests/Netmon.SNMPPolling.Tests/Integration/SNMPDiscoverIntegrationTests.cs @@ -0,0 +1,36 @@ +using System.Net.Http.Json; +using Microsoft.AspNetCore.Mvc.Testing; +using Xunit; + +namespace Netmon.SNMPPolling.Tests.Integration; + +public class SNMPDiscoverIntegrationTests : IClassFixture> +{ + private readonly WebApplicationFactory _factory; + + public SNMPDiscoverIntegrationTests(WebApplicationFactory factory) + { + _factory = factory; + } + + [Theory] + [InlineData("/Discover/Details")] + public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url) + { + // Arrange + HttpClient client = _factory.CreateClient(); + + // Act + HttpResponseMessage response = await client.PostAsJsonAsync(url, new + { + version = "V2", + ipAddress = "192.168.178.7", + port = 161, + community = "public" + }); + + // Assert + response.EnsureSuccessStatusCode(); + Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType?.ToString()); + } +} \ No newline at end of file diff --git a/Tests/Netmon.SNMPPolling.Tests/Netmon.SNMPPolling.Tests.csproj b/Tests/Netmon.SNMPPolling.Tests/Netmon.SNMPPolling.Tests.csproj index 737fedc..4df2957 100644 --- a/Tests/Netmon.SNMPPolling.Tests/Netmon.SNMPPolling.Tests.csproj +++ b/Tests/Netmon.SNMPPolling.Tests/Netmon.SNMPPolling.Tests.csproj @@ -9,15 +9,21 @@ + + + + + + diff --git a/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBCpuConverterTests.cs b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBCpuConverterTests.cs new file mode 100644 index 0000000..cc51a82 --- /dev/null +++ b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBCpuConverterTests.cs @@ -0,0 +1,133 @@ +using Lextm.SharpSnmpLib; +using Netmon.Models.Component.Cpu; +using Netmon.Models.Component.Cpu.Core; +using Netmon.Models.Component.Cpu.Core.Metric; +using Netmon.Models.Component.Cpu.Metric; +using Netmon.SNMPPolling.SNMP.Converter.Component; +using Netmon.SNMPPolling.SNMP.MIB; +using Netmon.SNMPPolling.SNMP.MIB.HostResources; +using Netmon.SNMPPolling.SNMP.MIB.HostResources.Device; +using Netmon.SNMPPolling.SNMP.MIB.HostResources.Device.Device; +using Netmon.SNMPPolling.SNMP.MIB.HostResources.Device.Processor; +using Netmon.SNMPPolling.SNMP.MIB.UCDavis; +using Netmon.SNMPPolling.SNMP.MIB.UCDavis.CpuLoad; +using NUnit.Framework; + +namespace Netmon.SNMPPolling.Tests.SNMP.Converter.Component; + +[TestFixture] +public class MIBCpuConverterTests +{ + [Test] + public void ConvertMIBsToComponent_ShouldReturnEmptyList_WhenNoHostResourcesMIB() + { + // Arrange + List mibs = new(); + MIBCpuConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.IsEmpty(result); + } + + [Test] + public void ConvertMIBsToComponent_ShouldReturnCpuWithCoresAndMetrics_WhenHostResourcesMIBExists() + { + // Arrange + HostResourcesMIB hostResourcesMIB = new() + { + HrDevice = new HrDevice + { + HrProcessorTable = new HrProcessorTable + { + HrProcessorEntries = new List + { + new() + { + HrProcessorIndex = new Integer32(1), + HrProcessorLoad = new Integer32(50) + } + } + }, + HrDeviceTable = new HrDeviceTable + { + HrDeviceEntries = new List + { + new() + { + HrDeviceIndex = new Integer32(1), + HrDeviceDescr = new OctetString("Processor 1") + } + } + } + } + }; + + List mibs = new() { hostResourcesMIB }; + + MIBCpuConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.IsNotEmpty(result); + Assert.That(result, Has.Count.EqualTo(1)); + + ICpu cpu = result.First(); + Assert.That(cpu.Index, Is.EqualTo(1)); + Assert.IsNotEmpty(cpu.Cores); + Assert.That(cpu.Cores, Has.Count.EqualTo(1)); + + ICpuCore core = cpu.Cores.First(); + Assert.That(core.Index, Is.EqualTo(1)); + Assert.That(core.Name, Is.EqualTo("Processor 1")); + Assert.That(core.Metrics, Is.Not.Empty); + Assert.That(core.Metrics, Has.Count.EqualTo(1)); + + ICpuCoreMetric metric = core.Metrics.First(); + Assert.That(metric, Is.InstanceOf()); + Assert.That(((CpuCoreMetric)metric).Load, Is.EqualTo(50)); + } + + [Test] + public void ConvertMIBsToComponent_ShouldReturnCpuWithMetrics_WhenUCDavisMIBExists() + { + // Arrange + UCDavisMIB ucDavisMIB = new() + { + LaLoadTable = new LaLoadTable + { + LaLoadEntries = new List + { + new() { LaLoadInt = new Integer32(1) }, + new() { LaLoadInt = new Integer32(2) }, + new() { LaLoadInt = new Integer32(3) } + } + } + }; + + List mibs = new() { ucDavisMIB }; + + MIBCpuConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.IsNotEmpty(result); + Assert.That(result, Has.Count.EqualTo(1)); + + ICpu cpu = result.First(); + Assert.That(cpu.Index, Is.EqualTo(1)); + Assert.IsEmpty(cpu.Cores); + + ICpuMetric metric = cpu.Metrics.First(); + Assert.IsInstanceOf(metric); + Assert.That(((CpuMetric)metric).OneMinuteLoad, Is.EqualTo(1)); + Assert.That(((CpuMetric)metric).FiveMinuteLoad, Is.EqualTo(2)); + Assert.That(((CpuMetric)metric).FifteenMinuteLoad, Is.EqualTo(3)); + } +} \ No newline at end of file diff --git a/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBDiskConverterTests.cs b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBDiskConverterTests.cs new file mode 100644 index 0000000..569d509 --- /dev/null +++ b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBDiskConverterTests.cs @@ -0,0 +1,125 @@ +using Lextm.SharpSnmpLib; +using Netmon.Models.Component.Disk; +using Netmon.Models.Component.Disk.Metric; +using Netmon.SNMPPolling.SNMP.Converter.Component; +using Netmon.SNMPPolling.SNMP.MIB; +using Netmon.SNMPPolling.SNMP.MIB.HostResources; +using Netmon.SNMPPolling.SNMP.MIB.HostResources.Storage; +using NUnit.Framework; + +namespace Netmon.SNMPPolling.Tests.SNMP.Converter.Component; + +[TestFixture] +public class MIBDiskConverterTests +{ + [Test] + public void ConvertMIBsToComponent_ShouldReturnEmptyList_WhenNoHostResourcesMIB() + { + // Arrange + List mibs = new(); + + MIBDiskConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.IsEmpty(result); + } + + [Test] + public void ConvertMIBsToComponent_ShouldReturnDisksWithMetrics_WhenHostResourcesMIBExists() + { + // Arrange + HostResourcesMIB hostResourcesMIB = new() + { + HrStorage = new HrStorage + { + HrStorageTable = new HrStorageTable + { + HrStorageEntries = new List + { + new() + { + HrStorageIndex = new Integer32(1), + HrStorageType = HrStorageEntry.StorageType.FixedDisk, + HrStorageDescr = new OctetString("C:"), + HrStorageAllocationUnits = new Integer32(1024), + HrStorageSize = new Integer32(10240), + HrStorageUsed = new Integer32(5120) + }, + new() + { + HrStorageIndex = new Integer32(2), + HrStorageType = HrStorageEntry.StorageType.RemovableDisk, + HrStorageDescr = new OctetString("D:"), + HrStorageAllocationUnits = new Integer32(512), + HrStorageSize = new Integer32(5120), + HrStorageUsed = new Integer32(2560) + } + } + } + } + }; + + List mibs = new() { hostResourcesMIB }; + + MIBDiskConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.That(result, Is.Not.Empty); + Assert.That(result, Has.Count.EqualTo(1)); + + IDisk disk = result.First(); + Assert.That(disk.Index, Is.EqualTo(1)); + Assert.That(disk.MountingPoint, Is.EqualTo("C:")); + Assert.That(disk.Metrics, Is.Not.Empty); + Assert.That(disk.Metrics, Has.Count.EqualTo(1)); + + IDiskMetric metric = disk.Metrics.First(); + Assert.That(metric, Is.InstanceOf()); + Assert.That(((DiskMetric)metric).AllocationUnits, Is.EqualTo(1024)); + Assert.That(((DiskMetric)metric).TotalSpace, Is.EqualTo(10240)); + Assert.That(((DiskMetric)metric).UsedSpace, Is.EqualTo(5120)); + } + + [Test] + public void ConvertMIBsToComponent_ShouldSkipNonFixedDisks_WhenHostResourcesMIBExists() + { + // Arrange + HostResourcesMIB hostResourcesMIB = new() + { + HrStorage = new HrStorage + { + HrStorageTable = new HrStorageTable + { + HrStorageEntries = new List + { + new() + { + HrStorageIndex = new Integer32(1), + HrStorageType = HrStorageEntry.StorageType.Ram, + HrStorageDescr = new OctetString("RAM Disk"), + HrStorageAllocationUnits = new Integer32(1024), + HrStorageSize = new Integer32(10240), + HrStorageUsed = new Integer32(5120) + } + } + } + } + }; + + List mibs = new() { hostResourcesMIB }; + + MIBDiskConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.IsEmpty(result); + } +} \ No newline at end of file diff --git a/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBInterfaceConverterTests.cs b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBInterfaceConverterTests.cs new file mode 100644 index 0000000..0732595 --- /dev/null +++ b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBInterfaceConverterTests.cs @@ -0,0 +1,115 @@ +using System.Net.NetworkInformation; +using Lextm.SharpSnmpLib; +using Netmon.Models.Component.Interface; +using Netmon.Models.Component.Interface.Metric; +using Netmon.SNMPPolling.SNMP.Converter.Component; +using Netmon.SNMPPolling.SNMP.MIB; +using Netmon.SNMPPolling.SNMP.MIB.If; +using Netmon.SNMPPolling.SNMP.MIB.If.Interface; +using Netmon.SNMPPolling.SNMP.MIB.If.InterfaceX; +using NUnit.Framework; + +namespace Netmon.SNMPPolling.Tests.SNMP.Converter.Component; + +[TestFixture] +public class MIBInterfaceConverterTests +{ + [Test] + public void ConvertMIBsToComponent_ShouldReturnEmptyList_WhenNoIfMIB() + { + // Arrange + List mibs = new(); + + MIBInterfaceConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.IsEmpty(result); + } + + [Test] + public void ConvertMIBsToComponent_ShouldReturnInterfacesWithMetrics_WhenIfMIBExists() + { + // Arrange + IfMIB ifMIB = new() + { + IfTable = new IfTable + { + IfEntries = new List + { + new() + { + IfIndex = new Integer32(1), + IfDescr = new OctetString("eth0"), + IfType = InterfaceType.EthernetCsmacd, + IfPhysAddress = new OctetString(new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB }), + IfAdminStatus = InterfaceStatus.Up, + IfOperationalStatus = InterfaceStatus.Up, + IfSpeed = new Gauge32(1000000000), // 1 Gbps + IfMtu = new Integer32(1500), + IfInErrors = new Counter32(10), + IfOutErrors = new Counter32(5), + IfInDiscards = new Counter32(2), + IfOutDiscards = new Counter32(1) + } + } + }, + IfXTable = new IfXTable + { + IfXEntries = new List + { + new() + { + IfIndex = 1, + IfHCInOctets = new Counter64(1000), + IfHCOutOctets = new Counter64(500), + IfHCInBroadcastPkts = new Counter64(100), + IfHCOutBroadcastPkts = new Counter64(50), + IfHCInMulticastPkts = new Counter64(50), + IfHCOutMulticastPkts = new Counter64(25), + IfHCInUcastPkts = new Counter64(900), + IfHCOutUcastPkts = new Counter64(450) + } + } + } + }; + + List mibs = new() { ifMIB }; + + MIBInterfaceConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.IsNotEmpty(result); + Assert.That(result, Has.Count.EqualTo(1)); + + IInterface iface = result.First(); + Assert.That(iface.Index, Is.EqualTo(1)); + Assert.That(iface.Name, Is.EqualTo("eth0")); + Assert.That(iface.Type, Is.EqualTo(InterfaceType.EthernetCsmacd)); + Assert.That(iface.PhysAddress, Is.EqualTo(PhysicalAddress.Parse("01-23-45-67-89-AB"))); + Assert.That(iface.Metrics, Is.Not.Empty); + Assert.That(iface.Metrics, Has.Count.EqualTo(1)); + + IInterfaceMetric metric = iface.Metrics.First(); + Assert.That(metric, Is.InstanceOf()); + Assert.That(((InterfaceMetric)metric).AdminStatus, Is.EqualTo(InterfaceStatus.Up)); + Assert.That(((InterfaceMetric)metric).OperationStatus, Is.EqualTo(InterfaceStatus.Up)); + Assert.That(((InterfaceMetric)metric).Speed, Is.EqualTo(1000000000)); + Assert.That(((InterfaceMetric)metric).Mtu, Is.EqualTo(1500)); + Assert.That(((InterfaceMetric)metric).InOctets, Is.EqualTo(1000)); + Assert.That(((InterfaceMetric)metric).OutOctets, Is.EqualTo(500)); + Assert.That(((InterfaceMetric)metric).InErrors, Is.EqualTo(10)); + Assert.That(((InterfaceMetric)metric).OutErrors, Is.EqualTo(5)); + Assert.That(((InterfaceMetric)metric).InBroadcastPackets, Is.EqualTo(100)); + Assert.That(((InterfaceMetric)metric).OutBroadcastPackets, Is.EqualTo(50)); + Assert.That(((InterfaceMetric)metric).InMulticastPackets, Is.EqualTo(50)); + Assert.That(((InterfaceMetric)metric).OutMulticastPackets, Is.EqualTo(25)); + Assert.That(((InterfaceMetric)metric).InUnicastPackets, Is.EqualTo(900)); + Assert.That(((InterfaceMetric)metric).OutUnicastPackets, Is.EqualTo(450)); + } +} \ No newline at end of file diff --git a/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBMemoryConverterTests.cs b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBMemoryConverterTests.cs new file mode 100644 index 0000000..072fc73 --- /dev/null +++ b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Component/MIBMemoryConverterTests.cs @@ -0,0 +1,116 @@ +using Lextm.SharpSnmpLib; +using Netmon.Models.Component.Memory; +using Netmon.Models.Component.Memory.Metric; +using Netmon.SNMPPolling.SNMP.Converter.Component; +using Netmon.SNMPPolling.SNMP.MIB; +using Netmon.SNMPPolling.SNMP.MIB.HostResources; +using Netmon.SNMPPolling.SNMP.MIB.HostResources.Storage; +using NUnit.Framework; + +namespace Netmon.SNMPPolling.Tests.SNMP.Converter.Component; + +[TestFixture] +public class MIBMemoryConverterTests +{ + [Test] + public void ConvertMIBsToComponent_ShouldReturnEmptyList_WhenNoHostResourcesMIB() + { + // Arrange + List mibs = new(); + + MIBMemoryConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.That(result, Is.Empty); + } + + [Test] + public void ConvertMIBsToComponent_ShouldReturnMemoryWithMetrics_WhenHostResourcesMIBExists() + { + // Arrange + HostResourcesMIB hostResourcesMIB = new() + { + HrStorage = new HrStorage + { + HrStorageTable = new HrStorageTable + { + HrStorageEntries = new List + { + new() + { + HrStorageIndex = new Integer32(1), + HrStorageType = HrStorageEntry.StorageType.Ram, + HrStorageDescr = new OctetString("Physical Memory"), + HrStorageAllocationUnits = new Integer32(4096), + HrStorageSize = new Integer32(16384), + HrStorageUsed = new Integer32(8192) + } + } + } + } + }; + + List mibs = new() { hostResourcesMIB }; + + MIBMemoryConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.That(result, Is.Not.Empty); + Assert.That(result, Has.Count.EqualTo(1)); + + IMemory memory = result.First(); + Assert.That(memory.Index, Is.EqualTo(1)); + Assert.That(memory.Name, Is.EqualTo("Physical Memory")); + Assert.That(memory.Metrics, Is.Not.Empty); + Assert.That(memory.Metrics, Has.Count.EqualTo(1)); + + IMemoryMetric metric = memory.Metrics.First(); + Assert.That(metric, Is.InstanceOf()); + Assert.That(((MemoryMetric)metric).AllocationUnits, Is.EqualTo(4096)); + Assert.That(((MemoryMetric)metric).TotalSpace, Is.EqualTo(16384)); + Assert.That(((MemoryMetric)metric).UsedSpace, Is.EqualTo(8192)); + } + + [Test] + public void ConvertMIBsToComponent_ShouldSkipNonRamStorage_WhenHostResourcesMIBExists() + { + // Arrange + HostResourcesMIB hostResourcesMIB = new() + { + HrStorage = new HrStorage + { + HrStorageTable = new HrStorageTable + { + HrStorageEntries = new List + { + new() + { + HrStorageIndex = new Integer32(1), + HrStorageType = HrStorageEntry.StorageType.FixedDisk, + HrStorageDescr = new OctetString("C:"), + HrStorageAllocationUnits = new Integer32(1024), + HrStorageSize = new Integer32(10240), + HrStorageUsed = new Integer32(5120) + } + } + } + } + }; + + List mibs = new() { hostResourcesMIB }; + + MIBMemoryConverter converter = new(); + + // Act + List result = converter.ConvertMIBsToComponent(mibs); + + // Assert + Assert.That(result, Is.Empty); + } +} \ No newline at end of file diff --git a/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Device/MIBDeviceConverterTests.cs b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Device/MIBDeviceConverterTests.cs new file mode 100644 index 0000000..8daf146 --- /dev/null +++ b/Tests/Netmon.SNMPPolling.Tests/Unit/SNMP/Converter/Device/MIBDeviceConverterTests.cs @@ -0,0 +1,105 @@ +using Lextm.SharpSnmpLib; +using Netmon.Models.Device; +using Netmon.Models.Device.Connection; +using Netmon.Models.Device.Connection.Protocol; +using Netmon.SNMPPolling.SNMP.Converter.Device; +using Netmon.SNMPPolling.SNMP.MIB; +using Netmon.SNMPPolling.SNMP.MIB.System; +using Netmon.SNMPPolling.SNMP.Request; +using NUnit.Framework; + +namespace Netmon.SNMPPolling.Tests.SNMP.Converter.Device; + +[TestFixture] +public class MIBDeviceConverterTests +{ + [Test] + public void ConvertMIBsToDevice_ShouldReturnDeviceWithConnectionInfo_WhenSystemMIBExists() + { + // Arrange + SNMPConnectionInfo connectionInfo = new() + { + IpAddress = "192.168.1.1", + Port = 161, + Community = "public", + AuthPassword = "authPass", + PrivacyPassword = "privPass", + AuthProtocol = AuthProtocol.SHA512, + PrivacyProtocol = PrivacyProtocol.AES256, + ContextName = "contextName", + Version = VersionCode.V3 + }; + + List mibs = new() + { + new SystemMIB + { + SysName = new OctetString("Device1"), + SysLocation = new OctetString("Location1"), + SysContact = new OctetString("Contact1") + } + }; + + MIBDeviceConverter converter = new(); + + // Act + IDevice result = converter.ConvertMIBsToDevice(connectionInfo, mibs); + + // Assert + Assert.IsNotNull(result); + Assert.IsInstanceOf(result); + Assert.That(result.IpAddress, Is.EqualTo("192.168.1.1")); + + IDeviceConnection? deviceConnection = result.DeviceConnection; + Assert.IsNotNull(deviceConnection); + Assert.That(deviceConnection?.Port, Is.EqualTo(161)); + Assert.That(deviceConnection?.Community, Is.EqualTo("public")); + Assert.That(deviceConnection?.AuthPassword, Is.EqualTo("authPass")); + Assert.That(deviceConnection?.PrivacyPassword, Is.EqualTo("privPass")); + Assert.That(deviceConnection?.AuthProtocol, Is.EqualTo(AuthProtocol.SHA512)); + Assert.That(deviceConnection?.PrivacyProtocol, Is.EqualTo(PrivacyProtocol.AES256)); + Assert.That(deviceConnection?.ContextName, Is.EqualTo("contextName")); + Assert.That(deviceConnection?.SNMPVersion, Is.EqualTo(3)); + + Assert.That(result.Name, Is.EqualTo("Device1")); + Assert.That(result.Location, Is.EqualTo("Location1")); + Assert.That(result.Contact, Is.EqualTo("Contact1")); + } + + [Test] + public void ConvertMIBsToDevice_ShouldReturnDeviceWithDefaultConnectionInfo_WhenSystemMIBDoesNotExist() + { + // Arrange + SNMPConnectionInfo connectionInfo = new() + { + IpAddress = "192.168.1.1" + }; + + List mibs = new(); + + MIBDeviceConverter converter = new(); + + // Act + IDevice result = converter.ConvertMIBsToDevice(connectionInfo, mibs); + + // Assert + Assert.IsNotNull(result); + Assert.IsInstanceOf(result); + Assert.That(result.IpAddress, Is.EqualTo("192.168.1.1")); + + IDeviceConnection? deviceConnection = result.DeviceConnection; + Assert.IsNotNull(deviceConnection); + Assert.That(deviceConnection?.Port, Is.EqualTo(161)); // Default SNMP port + Assert.That(deviceConnection?.Community, Is.EqualTo("public")); // Default SNMP community + Assert.That(deviceConnection?.AuthPassword, Is.EqualTo(string.Empty)); + Assert.That(deviceConnection?.PrivacyPassword, Is.EqualTo(string.Empty)); + Assert.That(deviceConnection?.AuthProtocol, Is.EqualTo(AuthProtocol.SHA256)); + Assert.That(deviceConnection?.PrivacyProtocol, Is.EqualTo(PrivacyProtocol.AES)); + Assert.That(deviceConnection?.ContextName, Is.EqualTo(string.Empty)); + Assert.That(deviceConnection?.SNMPVersion, Is.EqualTo(3)); // Default to SNMPv3 + + Assert.That(result.Name, Is.EqualTo(null)); + Assert.That(result.Location, Is.EqualTo(null)); + Assert.That(result.Contact, Is.EqualTo(null)); + } +} \ No newline at end of file diff --git a/Tests/Netmon.SNMPPolling.Tests/Unit/Security/PrivacyProviderTests.cs b/Tests/Netmon.SNMPPolling.Tests/Unit/Security/PrivacyProviderTests.cs new file mode 100644 index 0000000..3eac698 --- /dev/null +++ b/Tests/Netmon.SNMPPolling.Tests/Unit/Security/PrivacyProviderTests.cs @@ -0,0 +1,75 @@ +using Lextm.SharpSnmpLib.Security; +using Netmon.Models.Device.Connection.Protocol; +using Netmon.SNMPPolling.Exception.SNMP; +using Netmon.SNMPPolling.SNMP.Security; +using NUnit.Framework; + +namespace Netmon.SNMPPolling.Tests; + +[TestFixture] +public class PrivacyProviderTests +{ + [Test] + public void GetPrivacyProvider_ShouldReturnAESPrivacyProvider_WhenAESProtocolIsSelected() + { + // Arrange + string authPassword = "test_auth_password"; + string privacyPassword = "test_privacy_password"; + AuthProtocol authProtocol = AuthProtocol.SHA256; + PrivacyProtocol privacyProtocol = PrivacyProtocol.AES; + + // Act + IPrivacyProvider result = PrivacyProvider.GetPrivacyProvider(authPassword, privacyPassword, authProtocol, privacyProtocol); + + // Assert + Assert.IsInstanceOf(result); + } + + [Test] + public void GetPrivacyProvider_ShouldReturnAES192PrivacyProvider_WhenAES192ProtocolIsSelected() + { + // Arrange + string authPassword = "test_auth_password"; + string privacyPassword = "test_privacy_password"; + AuthProtocol authProtocol = AuthProtocol.SHA256; + PrivacyProtocol privacyProtocol = PrivacyProtocol.AES192; + + // Act + IPrivacyProvider result = PrivacyProvider.GetPrivacyProvider(authPassword, privacyPassword, authProtocol, privacyProtocol); + + // Assert + Assert.IsInstanceOf(result); + } + + [Test] + public void GetPrivacyProvider_ShouldThrowUnknownAuthProtocolException_WhenUnknownAuthProtocolIsSelected() + { + // Arrange + string authPassword = "test_auth_password"; + string privacyPassword = "test_privacy_password"; + AuthProtocol authProtocol = (AuthProtocol)100; // An unknown protocol value + PrivacyProtocol privacyProtocol = PrivacyProtocol.AES; + + // Act and Assert + Assert.Throws(() => + { + PrivacyProvider.GetPrivacyProvider(authPassword, privacyPassword, authProtocol, privacyProtocol); + }); + } + + [Test] + public void GetPrivacyProvider_ShouldThrowUnknownPrivacyProtocolException_WhenUnknownPrivacyProtocolIsSelected() + { + // Arrange + string authPassword = "test_auth_password"; + string privacyPassword = "test_privacy_password"; + AuthProtocol authProtocol = AuthProtocol.SHA256; + PrivacyProtocol privacyProtocol = (PrivacyProtocol)100; // An unknown protocol value + + // Act and Assert + Assert.Throws(() => + { + PrivacyProvider.GetPrivacyProvider(authPassword, privacyPassword, authProtocol, privacyProtocol); + }); + } +} \ No newline at end of file diff --git a/Tests/Netmon.SNMPPolling.Tests/Usings.cs b/Tests/Netmon.SNMPPolling.Tests/Usings.cs deleted file mode 100644 index cefced4..0000000 --- a/Tests/Netmon.SNMPPolling.Tests/Usings.cs +++ /dev/null @@ -1 +0,0 @@ -global using NUnit.Framework; \ No newline at end of file