From 6194d1bb9edbe3a02651718a74d8e913d4f58390 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Wed, 20 Apr 2022 17:13:40 +0200 Subject: [PATCH] Added MachineSystem Tests --- FabAccessAPI_Test/API_TestEnv_Test.cs | 259 ++++++++++++++++++++++++++ FabAccessAPI_Test/Connection_Test.cs | 44 ++++- 2 files changed, 301 insertions(+), 2 deletions(-) create mode 100644 FabAccessAPI_Test/API_TestEnv_Test.cs diff --git a/FabAccessAPI_Test/API_TestEnv_Test.cs b/FabAccessAPI_Test/API_TestEnv_Test.cs new file mode 100644 index 0000000..cf2f567 --- /dev/null +++ b/FabAccessAPI_Test/API_TestEnv_Test.cs @@ -0,0 +1,259 @@ +using Capnp.Rpc; +using FabAccessAPI; +using FabAccessAPI.Schema; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Net.Security; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; +using System.Threading.Tasks; +using static FabAccessAPI.Schema.MachineSystem; + +namespace FabAccessAPI_Test +{ + public static class API_TestEnv_Test + { + public const string TESTSERVER = "bffh.lab.bln.kjknet.de"; + public const int TESTSERVER_PORT = 59666; + public const string PASSWORD = "secret"; + + public static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) + { + return true; + } + + public static async Task Connect(string username) + { + TcpRpcClient tcpRpcClient = new TcpRpcClient(); + tcpRpcClient.InjectMidlayer((tcpstream) => + { + var sslStream = new SslStream(tcpstream, false, new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback)); + try + { + sslStream.AuthenticateAsClient("bffhd"); + return sslStream; + } + catch (AuthenticationException) + { + sslStream.Close(); + throw; + } + }); + + tcpRpcClient.Connect(TESTSERVER, TESTSERVER_PORT); + await tcpRpcClient.WhenConnected; + + Connection connection = new Connection(tcpRpcClient); + await connection.Auth("PLAIN", new Dictionary(StringComparer.Ordinal) { { "Username", username }, { "Password", PASSWORD } }); + + return connection; + } + + public static void Disconnect(Connection connection) + { + connection.RpcClient.Dispose(); + } + } + + [TestFixture, Parallelizable(ParallelScope.Children)] + public class MachineSystem + { + [TestCase("Admin1", true)] + [TestCase("Admin2", true)] + [TestCase("ManagerA1", true)] + [TestCase("ManagerA2", true)] + [TestCase("ManagerB1", true)] + [TestCase("ManagerB2", true)] + [TestCase("ManagerC1", true)] + [TestCase("ManagerC2", true)] + [TestCase("ManagerABC1", true)] + [TestCase("ManagerABC2", true)] + [TestCase("MakerA1", true)] + [TestCase("MakerA2", true)] + [TestCase("MakerB1", true)] + [TestCase("MakerB2", true)] + [TestCase("MakerC1", true)] + [TestCase("MakerC2", true)] + [TestCase("MakerABC1", true)] + [TestCase("MakerABC2", true)] + [TestCase("GuestA1", true)] + [TestCase("GuestA2", true)] + [TestCase("GuestB1", true)] + [TestCase("GuestB2", true)] + [TestCase("GuestC1", true)] + [TestCase("GuestC2", true)] + [TestCase("GuestABC1", true)] + [TestCase("GuestABC2", true)] + [TestCase("MakerQRA", true)] + [TestCase("MakerQRB", true)] + [TestCase("MakerQRC", true)] + + [Order(1)] + public async Task AccessMachineSystem(string username, bool expectAllow) + { + Connection connection = await API_TestEnv_Test.Connect(username); + Session session = connection.Session; + + bool result = !((MachineSystem_Proxy)session.MachineSystem).IsNull; + + API_TestEnv_Test.Disconnect(connection); + + Assert.AreEqual(expectAllow, result); + } + + //[TestCase("", true)] + //[Order(2)] + //public async Task AccessMachineSystem_Info(string username, bool expectAllow) + //{ + // Session session = await API_TestEnv_Test.Connect(username); + + // Assert.AreEqual(expectAllow, session.MachineSystem.Info); + //} + + [TestCase("Admin1", 15)] + [TestCase("ManagerA1", 5)] + [TestCase("ManagerB1", 5)] + [TestCase("ManagerC1", 5)] + [TestCase("ManagerABC1", 15)] + [TestCase("MakerA1", 5)] + [TestCase("MakerB1", 5)] + [TestCase("MakerC1", 5)] + [TestCase("MakerABC1", 15)] + [TestCase("GuestA1", 5)] + [TestCase("GuestB1", 5)] + [TestCase("GuestC1", 5)] + [TestCase("GuestABC1", 15)] + [TestCase("MakerQRA", 0)] + [TestCase("MakerQRB", 0)] + [TestCase("MakerQRC", 0)] + [Order(3)] + public async Task ListMachines(string username, int expectedMachineCount) + { + Connection connection = await API_TestEnv_Test.Connect(username); + Session session = connection.Session; + + IInfoInterface infoInterface = await session.MachineSystem.Info().ConfigureAwait(false); + IReadOnlyList machine_list = await infoInterface.GetMachineList().ConfigureAwait(false); + + int result = machine_list.Count; + + API_TestEnv_Test.Disconnect(connection); + + Assert.AreEqual(expectedMachineCount, result); + } + + [TestCase("Admin1", "MachineA1", true)] + [TestCase("Admin1", "MachineB1", true)] + [TestCase("Admin1", "MachineC1", true)] + [TestCase("ManagerA1", "MachineA1", true)] + [TestCase("ManagerA1", "MachineB1", false)] + [TestCase("ManagerA1", "MachineC1", false)] + [TestCase("ManagerB1", "MachineA1", false)] + [TestCase("ManagerB1", "MachineB1", true)] + [TestCase("ManagerB1", "MachineC1", false)] + [TestCase("ManagerC1", "MachineA1", false)] + [TestCase("ManagerC1", "MachineB1", false)] + [TestCase("ManagerC1", "MachineC1", true)] + [TestCase("ManagerABC1", "MachineA1", true)] + [TestCase("ManagerABC1", "MachineB1", true)] + [TestCase("ManagerABC1", "MachineC1", true)] + [TestCase("MakerA1", "MachineA1", true)] + [TestCase("MakerB1", "MachineB1", true)] + [TestCase("MakerC1", "MachineC1", true)] + [TestCase("GuestA1", "MachineA1", true)] + [TestCase("GuestB1", "MachineB1", true)] + [TestCase("GuestC1", "MachineC1", true)] + [TestCase("MakerQRA", "MachineA1", true)] + [TestCase("MakerQRB", "MachineB1", true)] + [TestCase("MakerQRC", "MachineC1", true)] + + [Order(4)] + public async Task GetMachineByName(string username, string machineName, bool expectedAllow) + { + Connection connection = await API_TestEnv_Test.Connect(username); + Session session = connection.Session; + + IInfoInterface infoInterface = await session.MachineSystem.Info().ConfigureAwait(false); + Machine machine = await infoInterface.GetMachine(machineName).ConfigureAwait(false); + bool result = machine != null; + + API_TestEnv_Test.Disconnect(connection); + + Assert.AreEqual(expectedAllow, result); + } + + [TestCase("Admin1", "MachineX")] + [TestCase("Admin1", "urn:fabaccess:resource:MachineA1")] + [Order(5)] + public async Task GetMachineByName_WrongName(string username, string machineName) + { + Connection connection = await API_TestEnv_Test.Connect(username); + Session session = connection.Session; + + IInfoInterface infoInterface = await session.MachineSystem.Info().ConfigureAwait(false); + Machine machine = await infoInterface.GetMachine(machineName).ConfigureAwait(false); + + API_TestEnv_Test.Disconnect(connection); + + Assert.IsNull(machine); + } + + [TestCase("Admin1", "urn:fabaccess:resource:MachineA1", true)] + [TestCase("Admin1", "urn:fabaccess:resource:MachineB1", true)] + [TestCase("Admin1", "urn:fabaccess:resource:MachineC1", true)] + [TestCase("ManagerA1", "urn:fabaccess:resource:MachineA1", true)] + [TestCase("ManagerA1", "urn:fabaccess:resource:MachineB1", false)] + [TestCase("ManagerA1", "urn:fabaccess:resource:MachineC1", false)] + [TestCase("ManagerB1", "urn:fabaccess:resource:MachineA1", false)] + [TestCase("ManagerB1", "urn:fabaccess:resource:MachineB1", true)] + [TestCase("ManagerB1", "urn:fabaccess:resource:MachineC1", false)] + [TestCase("ManagerC1", "urn:fabaccess:resource:MachineA1", false)] + [TestCase("ManagerC1", "urn:fabaccess:resource:MachineB1", false)] + [TestCase("ManagerC1", "urn:fabaccess:resource:MachineC1", true)] + [TestCase("ManagerABC1", "urn:fabaccess:resource:MachineA1", true)] + [TestCase("ManagerABC1", "urn:fabaccess:resource:MachineB1", true)] + [TestCase("ManagerABC1", "urn:fabaccess:resource:MachineC1", true)] + [TestCase("MakerA1", "urn:fabaccess:resource:MachineA1", true)] + [TestCase("MakerB1", "urn:fabaccess:resource:MachineB1", true)] + [TestCase("MakerC1", "urn:fabaccess:resource:MachineC1", true)] + [TestCase("GuestA1", "urn:fabaccess:resource:MachineA1", true)] + [TestCase("GuestB1", "urn:fabaccess:resource:MachineB1", true)] + [TestCase("GuestC1", "urn:fabaccess:resource:MachineC1", true)] + [TestCase("MakerQRA", "urn:fabaccess:resource:MachineA1", true)] + [TestCase("MakerQRB", "urn:fabaccess:resource:MachineB1", true)] + [TestCase("MakerQRC", "urn:fabaccess:resource:MachineC1", true)] + + [Order(6)] + public async Task GetMachineByURN(string username, string urn, bool expectedAllow) + { + Connection connection = await API_TestEnv_Test.Connect(username); + Session session = connection.Session; + + IInfoInterface infoInterface = await session.MachineSystem.Info().ConfigureAwait(false); + Machine machine = await infoInterface.GetMachine(urn).ConfigureAwait(false); + bool result = machine != null; + + API_TestEnv_Test.Disconnect(connection); + + Assert.AreEqual(expectedAllow, result); + } + + [TestCase("Admin1", "urn:fabaccess:resource:MachineX")] + [TestCase("Admin1", "MachineA1")] + [TestCase("Admin1", "something")] + [Order(7)] + public async Task GetMachineByURN_WrongURN(string username, string urn) + { + Connection connection = await API_TestEnv_Test.Connect(username); + Session session = connection.Session; + + IInfoInterface infoInterface = await session.MachineSystem.Info().ConfigureAwait(false); + Machine machine = await infoInterface.GetMachine(urn).ConfigureAwait(false); + + API_TestEnv_Test.Disconnect(connection); + + Assert.IsNull(machine); + } + } +} diff --git a/FabAccessAPI_Test/Connection_Test.cs b/FabAccessAPI_Test/Connection_Test.cs index a63149a..49a436b 100644 --- a/FabAccessAPI_Test/Connection_Test.cs +++ b/FabAccessAPI_Test/Connection_Test.cs @@ -3,26 +3,66 @@ using FabAccessAPI; using NUnit.Framework; using System; using System.Collections.Generic; +using System.Net.Security; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; namespace FabAccessAPI_Test { + public class Connection_Test { - [TestCase("test.fab-access.org", 59661)] + const string TESTSERVER = "bffh.lab.bln.kjknet.de"; + const int TESTSERVER_PORT = 59666; + + private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) + { + return true; + } + + [TestCase(TESTSERVER, TESTSERVER_PORT)] public async Task Connect(string host, int port) { TcpRpcClient tcpRpcClient = new TcpRpcClient(); + tcpRpcClient.InjectMidlayer((tcpstream) => + { + var sslStream = new SslStream(tcpstream, false, new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback)); + try + { + sslStream.AuthenticateAsClient("bffhd"); + return sslStream; + } + catch (AuthenticationException) + { + sslStream.Close(); + throw; + } + }); tcpRpcClient.Connect(host, port); await tcpRpcClient.WhenConnected; Connection connection = new Connection(tcpRpcClient); } - [TestCase("test.fab-access.org", 59661, "Testuser", "secret")] + [TestCase(TESTSERVER, TESTSERVER_PORT, "Admin1", "secret")] public async Task Authenticate_PLAIN(string host, int port, string username, string password) { TcpRpcClient tcpRpcClient = new TcpRpcClient(); + tcpRpcClient.InjectMidlayer((tcpstream) => + { + var sslStream = new SslStream(tcpstream, false, new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback)); + try + { + sslStream.AuthenticateAsClient("bffhd"); + return sslStream; + } + catch (AuthenticationException) + { + sslStream.Close(); + throw; + } + }); tcpRpcClient.Connect(host, port); await tcpRpcClient.WhenConnected;