Skip to content

Commit

Permalink
Merge pull request #1070 from dorssel/test_discovery
Browse files Browse the repository at this point in the history
Fix unit test discovery
  • Loading branch information
dorssel authored Nov 24, 2024
2 parents 2c154e9 + 1eb999e commit cf18433
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*.csproj text
*.cs text eol=crlf
*.props text
*.targets text

*.wixproj text
*.wxs text
Expand Down
1 change: 1 addition & 0 deletions .mega-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ XML_XMLLINT_FILE_EXTENSIONS:
- .csproj
- .props
- .pubxml
- .targets
- .wixproj
- .wxs
# Exclude verbatim copies of external sources that are not under our control.
Expand Down
2 changes: 0 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ SPDX-License-Identifier: GPL-3.0-only
<SelfContained>false</SelfContained>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
<DebugType Condition=" '$(Configuration)' == 'Release' ">None</DebugType>
<CopyOutputSymbolsToPublishDirectory>false</CopyOutputSymbolsToPublishDirectory>
<SatelliteResourceLanguages>en-US</SatelliteResourceLanguages>
<GenerateDependencyFile>false</GenerateDependencyFile>
<!--
Expand Down
16 changes: 16 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
SPDX-License-Identifier: GPL-3.0-only
-->
<Project>

<Target Name="RemoveDebug" AfterTargets="Publish">
<ItemGroup>
<DebugFiles Include="$(PublishDir)\**\*.pdb" />
</ItemGroup>
<Delete Files="@(DebugFiles)" />
</Target>

</Project>
8 changes: 6 additions & 2 deletions UnitTests/GlobalFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace UnitTests;

[TestClass]
sealed class GlobalFixture
static class GlobalFixture
{
public static string TemporaryDirectory { get; } = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

Expand All @@ -19,6 +19,10 @@ public static void AssemblyInitialize(TestContext context)
[AssemblyCleanup]
public static void AssemblyCleanup()
{
Directory.Delete(TemporaryDirectory, true);
try
{
Directory.Delete(TemporaryDirectory, true);
}
catch (DirectoryNotFoundException) { }
}
}
33 changes: 23 additions & 10 deletions UnitTests/Policy_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: GPL-3.0-only

using System.Data;
using Microsoft.Win32;
using Usbipd.Automation;

Expand All @@ -25,7 +26,7 @@ public static void ClassInitialize(TestContext testContext)
BaseTempRegistryKey = new TempRegistry(Registry.CurrentUser);
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfClass)]
public static void ClassCleanup()
{
BaseTempRegistryKey.Dispose();
Expand All @@ -44,6 +45,8 @@ public void Constructor_Success()

sealed class AutoBindData
{
// NOTE: MSTest cannot serialize PolicyRuleAutoBind (yet), so we have to deconstruct/reconstruct.

static readonly PolicyRuleAutoBind[] _Valid = [
new(PolicyRuleEffect.Allow, TestBusId, null),
new(PolicyRuleEffect.Allow, null, TestHardwareId),
Expand All @@ -56,17 +59,19 @@ sealed class AutoBindData
new(PolicyRuleEffect.Allow, BusId.IncompatibleHub, TestHardwareId),
];

public static IEnumerable<PolicyRuleAutoBind[]> Valid
=> from value in _Valid select new PolicyRuleAutoBind[] { value };
public static IEnumerable<object?[]> Valid
=> from rule in _Valid select new object?[] { rule.Effect, rule.BusId, rule.HardwareId };

public static IEnumerable<PolicyRuleAutoBind[]> Invalid
=> from value in _Invalid select new PolicyRuleAutoBind[] { value };
public static IEnumerable<object?[]> Invalid
=> from rule in _Invalid select new object?[] { rule.Effect, rule.BusId, rule.HardwareId };
}

[TestMethod]
[DynamicData(nameof(AutoBindData.Valid), typeof(AutoBindData))]
public void Persist_AutoBind(PolicyRuleAutoBind rule)
public void Persist_AutoBind(PolicyRuleEffect effect, BusId? busId, VidPid? hardwareId)
{
var rule = new PolicyRuleAutoBind(effect, busId, hardwareId);

using var tempRegistry = CreateTempRegistry();
rule.Save(tempRegistry.Key);

Expand All @@ -78,15 +83,19 @@ public void Persist_AutoBind(PolicyRuleAutoBind rule)

[TestMethod]
[DynamicData(nameof(AutoBindData.Valid), typeof(AutoBindData))]
public void Valid_AutoBind(PolicyRuleAutoBind rule)
public void Valid_AutoBind(PolicyRuleEffect effect, BusId? busId, VidPid? hardwareId)
{
var rule = new PolicyRuleAutoBind(effect, busId, hardwareId);

Assert.IsTrue(rule.IsValid());
}

[TestMethod]
[DynamicData(nameof(AutoBindData.Invalid), typeof(AutoBindData))]
public void Invalid_AutoBind(PolicyRuleAutoBind rule)
public void Invalid_AutoBind(PolicyRuleEffect effect, BusId? busId, VidPid? hardwareId)
{
var rule = new PolicyRuleAutoBind(effect, busId, hardwareId);

Assert.IsFalse(rule.IsValid());
}

Expand All @@ -97,8 +106,10 @@ static UsbDevice CreateTestUsbDevice(BusId busId, VidPid hardwareId)

[TestMethod]
[DynamicData(nameof(AutoBindData.Invalid), typeof(AutoBindData))]
public void Match_Invalid_AutoBind(PolicyRuleAutoBind rule)
public void Match_Invalid_AutoBind(PolicyRuleEffect effect, BusId? busId, VidPid? hardwareId)
{
var rule = new PolicyRuleAutoBind(effect, busId, hardwareId);

_ = Assert.ThrowsException<InvalidOperationException>(() =>
{
_ = rule.Matches(CreateTestUsbDevice(TestBusId, TestHardwareId));
Expand All @@ -107,8 +118,10 @@ public void Match_Invalid_AutoBind(PolicyRuleAutoBind rule)

[TestMethod]
[DynamicData(nameof(AutoBindData.Valid), typeof(AutoBindData))]
public void Match_Valid_AutoBind(PolicyRuleAutoBind rule)
public void Match_Valid_AutoBind(PolicyRuleEffect effect, BusId? busId, VidPid? hardwareId)
{
var rule = new PolicyRuleAutoBind(effect, busId, hardwareId);

Assert.IsTrue(rule.Matches(CreateTestUsbDevice(TestBusId, TestHardwareId)));
Assert.IsFalse(rule.Matches(CreateTestUsbDevice(OtherBusId, OtherHardwareId)));
if (rule.BusId is null)
Expand Down
23 changes: 12 additions & 11 deletions UnitTests/Wsl_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,36 @@ static readonly (string host, string client)[] DifferentNetworkData = [
("0::1.2.3.4/24", "0::1.2.3.4"),
];

static (IPAddress address, IPAddress mask) FromCIDR(string cidr)
{
var cidrParts = cidr.Split('/');
return (IPAddress.Parse(cidrParts[0]), new IPAddress(BinaryPrimitives.ReverseEndianness(unchecked((uint)(-1L << (32 - int.Parse(cidrParts[1])))))));
}

public static IEnumerable<object[]> TestData
{
get
{
foreach (var (host, client) in SameNetworkData)
{
var (hostAddress, hostMask) = FromCIDR(host);
yield return new object[] { hostAddress, hostMask, IPAddress.Parse(client), true };
yield return new object[] { host, client, true };
}
foreach (var (host, client) in DifferentNetworkData)
{
var (hostAddress, hostMask) = FromCIDR(host);
yield return new object[] { hostAddress, hostMask, IPAddress.Parse(client), false };
yield return new object[] { host, client, false };
}
yield break;
}
}
}

static (IPAddress address, IPAddress mask) FromCIDR(string cidr)
{
var cidrParts = cidr.Split('/');
return (IPAddress.Parse(cidrParts[0]), new IPAddress(BinaryPrimitives.ReverseEndianness(unchecked((uint)(-1L << (32 - int.Parse(cidrParts[1])))))));
}

[TestMethod]
[DynamicData(nameof(NetworkData.TestData), typeof(NetworkData))]
public void IsOnSameIPv4Network(IPAddress hostAddress, IPAddress hostMask, IPAddress clientAddress, bool expected)
public void IsOnSameIPv4Network(string host, string client, bool expected)
{
var (hostAddress, hostMask) = FromCIDR(host);
var clientAddress = IPAddress.Parse(client);

var result = Wsl.IsOnSameIPv4Network(hostAddress, hostMask, clientAddress);
Assert.AreEqual(expected, result);
}
Expand Down
22 changes: 18 additions & 4 deletions Usbipd.Automation/BusId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,26 @@ public BusId(ushort bus, ushort port)
{
throw new ArgumentOutOfRangeException(nameof(port));
}
Bus = bus;
Port = port;
(Bus, Port) = (bus, port);
}

public ushort Bus { get; }
public ushort Port { get; }
public ushort Bus
{
get;
#if !NETSTANDARD
// Required for MSTest DynamicData (de)serialization.
init;
#endif
}

public ushort Port
{
get;
#if !NETSTANDARD
// Required for MSTest DynamicData (de)serialization.
init;
#endif
}

/// <summary>
/// The special value 0-0 for hubs that do not expose a compatible busid.
Expand Down
1 change: 1 addition & 0 deletions usbipd-win.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
codecov.yml = codecov.yml
COPYING.md = COPYING.md
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
Directory.Packages.props = Directory.Packages.props
Fake_GitVersionInformation.cs = Fake_GitVersionInformation.cs
GitVersion.yml = GitVersion.yml
Expand Down

0 comments on commit cf18433

Please sign in to comment.