diff --git a/FabAccessAPI/Auth.cs b/FabAccessAPI/Auth.cs index be9ae9c..ab83078 100644 --- a/FabAccessAPI/Auth.cs +++ b/FabAccessAPI/Auth.cs @@ -98,6 +98,7 @@ namespace FabAccessAPI { } public class UnauthorizedException : Exception{} + public class UnsupportedMechanismException : Exception{} /// /// THIS IS VERY INCOMPLETE! diff --git a/FabAccessAPI/Connection.cs b/FabAccessAPI/Connection.cs index 1f31546..84386ab 100644 --- a/FabAccessAPI/Connection.cs +++ b/FabAccessAPI/Connection.cs @@ -2,6 +2,7 @@ using FabAccessAPI.Schema; using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; namespace FabAccessAPI { @@ -14,6 +15,8 @@ namespace FabAccessAPI { private Machines? _machines = null; #endregion + public TcpRpcClient? RpcClient => _rpcClient; + #region Log private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #endregion @@ -40,9 +43,11 @@ namespace FabAccessAPI { var authCap = await _bootstrapCap.Auth(); _auth = new Auth(authCap); var mechs = await _auth.GetMechanisms().ConfigureAwait(false); - _Log.Debug($"The Server supports the following auth mechs: {mechs}"); + _Log.Debug($"The Server supports the following auth mechs: {string.Join(", ", mechs)}"); - // TODO: Check that the requested auth mech is actually available. + if (!mechs.Contains(mech)) { + throw new UnsupportedMechanismException(); + } await _auth.Authenticate(mech, kvs).ConfigureAwait(false); } diff --git a/FabAccessAPI/Machines.cs b/FabAccessAPI/Machines.cs index 0265003..1a3212c 100644 --- a/FabAccessAPI/Machines.cs +++ b/FabAccessAPI/Machines.cs @@ -5,6 +5,9 @@ using System.Linq; using System.Threading.Tasks; namespace FabAccessAPI { + + public class MachineException : Exception { } + /// /// Wraps a capability for accessing the Machines subsystem of BFFH /// @@ -43,7 +46,7 @@ namespace FabAccessAPI { var mach = (await _machinesCap.GetMachine(name).ConfigureAwait(false)).Item1; if (mach == null) { //TODO: Throw a more specific exception! - throw new Exception(); + throw new MachineException(); } return new Machine(mach); } diff --git a/FabAccessAPI_Test/FabAccessAPITests.cs b/FabAccessAPI_Test/FabAccessAPITests.cs index ca6e9ab..a20f35a 100644 --- a/FabAccessAPI_Test/FabAccessAPITests.cs +++ b/FabAccessAPI_Test/FabAccessAPITests.cs @@ -10,9 +10,15 @@ using Microsoft.Extensions.Logging; namespace FabAccessAPI_Test { public class Tests { + #region Log + private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + #endregion + private static ILoggerFactory _loggerFactory; + private Connection _connection; + [OneTimeSetUp] - public void Setup() { + public void InitialSetup() { XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config")); _loggerFactory = LoggerFactory.Create(builder => { builder @@ -23,27 +29,39 @@ namespace FabAccessAPI_Test { Logging.LoggerFactory = _loggerFactory; } + [SetUp] + public void Setup() { + var rpcClient = new TcpRpcClient(); + rpcClient.Connect("127.0.0.1", 59661); + _connection = new Connection(rpcClient); + } + + [TearDown] + public void Teardown() { + _connection.RpcClient?.Dispose(); + _connection = null; + } + [Test] public void Connect() { - var rpcClient = new TcpRpcClient(); - rpcClient.Connect("::1", 59661); - var con = new Connection(rpcClient); - Assert.AreEqual(ConnectionState.Active, rpcClient.State); + Assert.AreEqual(ConnectionState.Active, _connection.RpcClient.State); + } + + [Test] + public async Task Authenticate() { + await _connection.Auth("PLAIN", new Dictionary{{"Username", "Testuser"}, {"Password", "secret"}}); } [Test] public async Task Machines() { - var rpcClient = new TcpRpcClient(); - rpcClient.Connect("::1", 59661); - var con = new Connection(rpcClient); + await _connection.Auth("PLAIN", new Dictionary{{"Username", "Testuser"}, {"Password", "secret"}}); + var machines = await _connection.AccessMachines(); - var machines = await con.AccessMachines(); - - var testmachine = await machines.GetMachine("Testmachine"); - Assert.NotNull(testmachine); - var minfo = await testmachine.GetMInfo(); - Assert.NotNull(minfo); - Console.WriteLine($"Name: {minfo.Name}, Description: {minfo.Description}, State: {minfo.State.ToString()}"); + var testmachine = await machines.GetMachine("Testmachine"); + Assert.NotNull(testmachine); + var minfo = await testmachine.GetMInfo(); + Assert.NotNull(minfo); + _Log.Info($"Name: {minfo.Name}, Description: {minfo.Description}, State: {minfo.State.ToString()}"); } } } \ No newline at end of file