Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dorssel committed Apr 17, 2024
1 parent eddebaf commit 69a8b68
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
53 changes: 50 additions & 3 deletions UnitTests/Parse_policy_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 Frans van Dorsselaer
// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -67,7 +67,27 @@ public void list_StrayArgument()
}

[TestMethod]
public void add_Success()
public void add_BusIdSuccess()
{
var mock = CreateMock();
mock.Setup(m => m.PolicyAdd(It.IsAny<PolicyRule>(),
It.IsNotNull<IConsole>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(ExitCode.Success));

Test(ExitCode.Success, mock, "policy", "add", "--effect", "Allow", "--operation", "AutoBind", "--busid", TestBusId.ToString());
}

[TestMethod]
public void add_HardwareIdSuccess()
{
var mock = CreateMock();
mock.Setup(m => m.PolicyAdd(It.IsAny<PolicyRule>(),
It.IsNotNull<IConsole>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(ExitCode.Success));

Test(ExitCode.Success, mock, "policy", "add", "--effect", "Allow", "--operation", "AutoBind", "--hardware-id", TestHardwareId.ToString());
}

[TestMethod]
public void add_BothSuccess()
{
var mock = CreateMock();
mock.Setup(m => m.PolicyAdd(It.IsAny<PolicyRule>(),
Expand All @@ -78,12 +98,39 @@ public void add_Success()
}

[TestMethod]
public void remove_Success()
public void add_MissingCondition()
{
Test(ExitCode.ParseError, "policy", "add", "--effect", "Allow", "--operation", "AutoBind", TestHardwareId.ToString());
}

[TestMethod]
public void remove_GuidSuccess()
{
var mock = CreateMock();
mock.Setup(m => m.PolicyRemove(It.IsAny<Guid>(),
It.IsNotNull<IConsole>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(ExitCode.Success));

Test(ExitCode.Success, mock, "policy", "remove", "--guid", TestGuid.ToString());
}

[TestMethod]
public void remove_AllSuccess()
{
var mock = CreateMock();
mock.Setup(m => m.PolicyRemoveAll(It.IsNotNull<IConsole>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(ExitCode.Success));

Test(ExitCode.Success, mock, "policy", "remove", "--all");
}

[TestMethod]
public void remove_MissingOption()
{
Test(ExitCode.ParseError, "policy", "remove");
}

[TestMethod]
public void remove_TooManyOptions()
{
Test(ExitCode.ParseError, "policy", "remove", "--all", "--guid", TestGuid.ToString());
}
}
20 changes: 20 additions & 0 deletions UnitTests/Policy_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
//
// SPDX-License-Identifier: GPL-3.0-only

using Usbipd.Automation;

namespace UnitTests;

[TestClass]
sealed class Policy_Tests
{
static readonly BusId TestBusId = BusId.Parse("3-42");
static readonly VidPid TestHardwareId = VidPid.Parse("0123:cdef");

[TestMethod]
public void Constructor_Success()
{
_ = new PolicyRuleAutoBind(PolicyRuleEffect.Allow, TestBusId, TestHardwareId);
}
}

0 comments on commit 69a8b68

Please sign in to comment.