From bdd57493dace182196306e35a604891fb83c0126 Mon Sep 17 00:00:00 2001 From: Sik Date: Fri, 2 Aug 2024 13:55:20 +0200 Subject: [PATCH] feat(test): added tests for wol class --- System.Test/WakeOnLanTest.cs | 57 ++++++++++++++++++++++++++++++++++++ System/Net/WakeOnLan.cs | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 System.Test/WakeOnLanTest.cs diff --git a/System.Test/WakeOnLanTest.cs b/System.Test/WakeOnLanTest.cs new file mode 100644 index 0000000..950aabd --- /dev/null +++ b/System.Test/WakeOnLanTest.cs @@ -0,0 +1,57 @@ +using FrApp42.System.Net; + +namespace System.Test +{ + [TestClass] + public class WakeOnLanTest + { + private readonly string _macAddressColon = "FF:FF:FF:FF:FF:FF"; + private readonly string _macAddressDash = "FF-FF-FF-FF-FF-FF"; + private readonly string _expectedFormattedMacAddress = "FFFFFFFFFFFF"; + + [TestMethod] + public void BuildMagicPacketWithColon() + { + BuildMagicPacketTest(_macAddressColon); + } + + [TestMethod] + public void BuildMagicPacketWithDash() + { + BuildMagicPacketTest(_macAddressDash); + } + + [TestMethod] + public void MacFormatterWithColon() + { + string actualFormattedMac = WakeOnLan.MacFormatter().Replace(_macAddressColon, ""); + + Assert.AreEqual(_expectedFormattedMacAddress, actualFormattedMac, "The MAC address was not formatted correctly."); + } + + [TestMethod] + public void MacFormatterWithDash() + { + string actualFormattedMac = WakeOnLan.MacFormatter().Replace(_macAddressDash, ""); + + Assert.AreEqual(_expectedFormattedMacAddress, actualFormattedMac, "The MAC address was not formatted correctly."); + } + + private void BuildMagicPacketTest(string macAddress) + { + byte[] expectedMagicPacket = new byte[102]; + + for (int i = 0; i < 6; i++) + expectedMagicPacket[i] = 0xFF; + + byte[] macBytes = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + + for (int i = 0; i < 16; i++) + Array.Copy(macBytes, 0, expectedMagicPacket, 6 + i * 6, 6); + + byte[] actualMagicPacket = WakeOnLan.BuildMagicPacket(macAddress); + + CollectionAssert.AreEqual(expectedMagicPacket, actualMagicPacket, "The magic packet is not built correctly."); + } + } +} diff --git a/System/Net/WakeOnLan.cs b/System/Net/WakeOnLan.cs index af1ed4b..b8fa4cf 100644 --- a/System/Net/WakeOnLan.cs +++ b/System/Net/WakeOnLan.cs @@ -90,6 +90,6 @@ public static async Task SendWakeOnLan(IPAddress localIpAddress, IPAddress multi /// /// A Regex object for formatting MAC addresses. [GeneratedRegex("[: -]")] - private static partial Regex MacFormatter(); + public static partial Regex MacFormatter(); } }