diff --git a/UnitTests/Parse_policy_Tests.cs b/UnitTests/Parse_policy_Tests.cs index ee440a2e..ea295a8e 100644 --- a/UnitTests/Parse_policy_Tests.cs +++ b/UnitTests/Parse_policy_Tests.cs @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2022 Frans van Dorsselaer +// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer // // SPDX-License-Identifier: GPL-3.0-only @@ -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(), + It.IsNotNull(), It.IsAny())).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(), + It.IsNotNull(), It.IsAny())).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(), @@ -78,7 +98,13 @@ 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(), @@ -86,4 +112,25 @@ public void remove_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(), It.IsAny())).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()); + } } diff --git a/UnitTests/Policy_Tests.cs b/UnitTests/Policy_Tests.cs new file mode 100644 index 00000000..84f61fbf --- /dev/null +++ b/UnitTests/Policy_Tests.cs @@ -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); + } +}