SEBWIN-219: Implemented unit tests for proxies in core library.

This commit is contained in:
dbuechel 2018-03-16 09:28:33 +01:00
parent ec588e50bc
commit e4940383fb
18 changed files with 361 additions and 22 deletions

View file

@ -11,7 +11,7 @@ using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Hosts;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication;
using SafeExamBrowser.Core.Communication.Hosts;
namespace SafeExamBrowser.Client.Communication
{

View file

@ -26,7 +26,7 @@ using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Contracts.UserInterface.MessageBox;
using SafeExamBrowser.Contracts.WindowsApi;
using SafeExamBrowser.Core.Behaviour.OperationModel;
using SafeExamBrowser.Core.Communication;
using SafeExamBrowser.Core.Communication.Proxies;
using SafeExamBrowser.Core.I18n;
using SafeExamBrowser.Core.Logging;
using SafeExamBrowser.Monitoring.Display;

View file

@ -10,9 +10,9 @@ using System;
using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication;
using SafeExamBrowser.Core.Communication.Proxies;
namespace SafeExamBrowser.Core.UnitTests.Communication
namespace SafeExamBrowser.Core.UnitTests.Communication.Proxies
{
internal class BaseProxyImpl : BaseProxy
{

View file

@ -15,7 +15,7 @@ using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Logging;
namespace SafeExamBrowser.Core.UnitTests.Communication
namespace SafeExamBrowser.Core.UnitTests.Communication.Proxies
{
[TestClass]
public class BaseProxyTests

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.ServiceModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication.Proxies;
namespace SafeExamBrowser.Core.UnitTests.Communication.Proxies
{
[TestClass]
public class ClientProxyTests
{
private Mock<ILogger> logger;
private Mock<IProxyObjectFactory> proxyObjectFactory;
private Mock<ICommunication> proxy;
private ClientProxy sut;
[TestInitialize]
public void Initialize()
{
var response = new ConnectionResponse
{
CommunicationToken = Guid.NewGuid(),
ConnectionEstablished = true
};
logger = new Mock<ILogger>();
proxyObjectFactory = new Mock<IProxyObjectFactory>();
proxy = new Mock<ICommunication>();
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(response);
proxy.As<ICommunicationObject>().Setup(o => o.State).Returns(CommunicationState.Opened);
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
sut = new ClientProxy("net.pipe://random/address/here", proxyObjectFactory.Object, logger.Object);
sut.Connect(Guid.NewGuid());
}
[TestMethod]
public void MustCorrectlyInitiateShutdown()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.Shutdown))).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
sut.InitiateShutdown();
proxy.Verify(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.Shutdown)), Times.Once);
}
[TestMethod]
[ExpectedException(typeof(CommunicationException))]
public void MustFailIfShutdownCommandNotAcknowledged()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.Shutdown))).Returns<Response>(null);
sut.InitiateShutdown();
}
[TestMethod]
public void MustCorrectlyRequestAuthentication()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.Authenticate))).Returns(new AuthenticationResponse());
var response = sut.RequestAuthentication();
proxy.Verify(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.Authenticate)), Times.Once);
Assert.IsInstanceOfType(response, typeof(AuthenticationResponse));
}
[TestMethod]
[ExpectedException(typeof(CommunicationException))]
public void MustFailIfAuthenticationCommandNotFollowed()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.Authenticate))).Returns<Response>(null);
sut.RequestAuthentication();
}
}
}

View file

@ -0,0 +1,161 @@
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.ServiceModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication.Proxies;
namespace SafeExamBrowser.Core.UnitTests.Communication.Proxies
{
[TestClass]
public class RuntimeProxyTests
{
private Mock<ILogger> logger;
private Mock<IProxyObjectFactory> proxyObjectFactory;
private Mock<ICommunication> proxy;
private RuntimeProxy sut;
[TestInitialize]
public void Initialize()
{
var response = new ConnectionResponse
{
CommunicationToken = Guid.NewGuid(),
ConnectionEstablished = true
};
logger = new Mock<ILogger>();
proxyObjectFactory = new Mock<IProxyObjectFactory>();
proxy = new Mock<ICommunication>();
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(response);
proxy.As<ICommunicationObject>().Setup(o => o.State).Returns(CommunicationState.Opened);
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
sut = new RuntimeProxy("net.pipe://random/address/here", proxyObjectFactory.Object, logger.Object);
sut.Connect(Guid.NewGuid());
}
[TestMethod]
public void MustCorrectlyRetrieveConfiguration()
{
var response = new ConfigurationResponse
{
Configuration = new ClientConfiguration()
};
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ConfigurationNeeded))).Returns(response);
var configuration = sut.GetConfiguration();
proxy.Verify(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ConfigurationNeeded)), Times.Once);
Assert.IsInstanceOfType(configuration, typeof(ClientConfiguration));
}
[TestMethod]
[ExpectedException(typeof(CommunicationException))]
public void MustFailIfConfigurationNotRetrieved()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ConfigurationNeeded))).Returns<Response>(null);
sut.GetConfiguration();
}
[TestMethod]
public void MustCorrectlyInformClientReady()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ClientIsReady))).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
sut.InformClientReady();
proxy.Verify(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ClientIsReady)), Times.Once);
}
[TestMethod]
[ExpectedException(typeof(CommunicationException))]
public void MustFailIfClientReadyNotAcknowledged()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ClientIsReady))).Returns<Response>(null);
sut.InformClientReady();
}
[TestMethod]
public void MustCorrectlyRequestReconfiguration()
{
var url = "sebs://some/url.seb";
var response = new ReconfigurationResponse
{
Accepted = true
};
proxy.Setup(p => p.Send(It.Is<ReconfigurationMessage>(m => m.ConfigurationUrl == url))).Returns(response);
var accepted = sut.RequestReconfiguration(url);
proxy.Verify(p => p.Send(It.Is<ReconfigurationMessage>(m => m.ConfigurationUrl == url)), Times.Once);
Assert.IsTrue(accepted);
}
[TestMethod]
public void MustCorrectlyHandleDeniedReconfigurationRequest()
{
var url = "sebs://some/url.seb";
var response = new ReconfigurationResponse
{
Accepted = false
};
proxy.Setup(p => p.Send(It.Is<ReconfigurationMessage>(m => m.ConfigurationUrl == url))).Returns(response);
var accepted = sut.RequestReconfiguration(url);
Assert.IsFalse(accepted);
}
[TestMethod]
public void MustNotFailIfIncorrectResponseToReconfigurationRequest()
{
var url = "sebs://some/url.seb";
proxy.Setup(p => p.Send(It.Is<ReconfigurationMessage>(m => m.ConfigurationUrl == url))).Returns<Response>(null);
var accepted = sut.RequestReconfiguration(url);
Assert.IsFalse(accepted);
}
[TestMethod]
public void MustCorrectlyRequestShutdown()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.RequestShutdown))).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
sut.RequestShutdown();
proxy.Verify(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.RequestShutdown)), Times.Once);
}
[TestMethod]
[ExpectedException(typeof(CommunicationException))]
public void MustFailIfShutdownRequestNotAcknowledged()
{
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.RequestShutdown))).Returns<Response>(null);
sut.RequestShutdown();
}
}
}

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.ServiceModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication.Proxies;
namespace SafeExamBrowser.Core.UnitTests.Communication.Proxies
{
[TestClass]
public class ServiceProxyTests
{
private Mock<ILogger> logger;
private Mock<IProxyObjectFactory> proxyObjectFactory;
private Mock<ICommunication> proxy;
private ServiceProxy sut;
[TestInitialize]
public void Initialize()
{
var response = new ConnectionResponse
{
CommunicationToken = Guid.NewGuid(),
ConnectionEstablished = true
};
logger = new Mock<ILogger>();
proxyObjectFactory = new Mock<IProxyObjectFactory>();
proxy = new Mock<ICommunication>();
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(response);
proxy.As<ICommunicationObject>().Setup(o => o.State).Returns(CommunicationState.Opened);
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
sut = new ServiceProxy("net.pipe://random/address/here", proxyObjectFactory.Object, logger.Object);
}
[TestMethod]
public void MustIgnoreConnectIfUnavailable()
{
sut.Ignore = true;
sut.Connect(Guid.NewGuid());
proxy.Verify(p => p.Connect(It.IsAny<Guid>()), Times.Never);
}
[TestMethod]
public void MustIgnoreDisconnectIfUnavailable()
{
sut.Ignore = true;
sut.Disconnect();
proxy.Verify(p => p.Disconnect(It.IsAny<DisconnectionMessage>()), Times.Never);
}
[TestMethod]
public void MustIgnoreStartSessionIfUnavaiable()
{
sut.Ignore = true;
sut.StartSession(Guid.Empty, null);
proxy.Verify(p => p.Send(It.IsAny<Message>()), Times.Never);
}
[TestMethod]
public void MustIgnoreStopSessionIfUnavaiable()
{
sut.Ignore = true;
sut.StopSession(Guid.Empty);
proxy.Verify(p => p.Send(It.IsAny<Message>()), Times.Never);
}
}
}

View file

@ -84,8 +84,11 @@
<Compile Include="Behaviour\OperationModel\I18nOperationTests.cs" />
<Compile Include="Behaviour\OperationModel\DelegateOperationTests.cs" />
<Compile Include="Behaviour\OperationModel\OperationSequenceTests.cs" />
<Compile Include="Communication\BaseProxyImpl.cs" />
<Compile Include="Communication\BaseProxyTests.cs" />
<Compile Include="Communication\Proxies\BaseProxyImpl.cs" />
<Compile Include="Communication\Proxies\BaseProxyTests.cs" />
<Compile Include="Communication\Proxies\ClientProxyTests.cs" />
<Compile Include="Communication\Proxies\RuntimeProxyTests.cs" />
<Compile Include="Communication\Proxies\ServiceProxyTests.cs" />
<Compile Include="I18n\TextTests.cs" />
<Compile Include="I18n\XmlTextResourceTests.cs" />
<Compile Include="Logging\DefaultLogFormatterTests.cs" />

View file

@ -13,7 +13,7 @@ using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Logging;
namespace SafeExamBrowser.Core.Communication
namespace SafeExamBrowser.Core.Communication.Hosts
{
/// <summary>
/// The base implementation of an <see cref="ICommunicationHost"/>. Runs the host on a new, separate thread.

View file

@ -14,7 +14,7 @@ using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Logging;
namespace SafeExamBrowser.Core.Communication
namespace SafeExamBrowser.Core.Communication.Proxies
{
/// <summary>
/// Base implementation of an <see cref="ICommunicationProxy"/>.

View file

@ -11,7 +11,7 @@ using SafeExamBrowser.Contracts.Communication.Data;
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Logging;
namespace SafeExamBrowser.Core.Communication
namespace SafeExamBrowser.Core.Communication.Proxies
{
/// <summary>
/// Default implementation of the <see cref="IClientProxy"/>, to be used for communication with the client application component.

View file

@ -10,7 +10,7 @@ using System.ServiceModel;
using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Communication.Proxies;
namespace SafeExamBrowser.Core.Communication
namespace SafeExamBrowser.Core.Communication.Proxies
{
/// <summary>
/// Default implementation of the <see cref="IProxyObjectFactory"/> utilizing WCF (<see cref="ChannelFactory"/>).

View file

@ -12,7 +12,7 @@ using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Logging;
namespace SafeExamBrowser.Core.Communication
namespace SafeExamBrowser.Core.Communication.Proxies
{
/// <summary>
/// Default implementation of the <see cref="IRuntimeProxy"/>, to be used for communication with the runtime application component.

View file

@ -11,7 +11,7 @@ using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.Logging;
namespace SafeExamBrowser.Core.Communication
namespace SafeExamBrowser.Core.Communication.Proxies
{
/// <summary>
/// Default implementation of the <see cref="IServiceProxy"/>, to be used for communication with the service application component.

View file

@ -60,12 +60,12 @@
<Compile Include="Behaviour\OperationModel\I18nOperation.cs" />
<Compile Include="Behaviour\OperationModel\DelegateOperation.cs" />
<Compile Include="Behaviour\OperationModel\OperationSequence.cs" />
<Compile Include="Communication\BaseProxy.cs" />
<Compile Include="Communication\BaseHost.cs" />
<Compile Include="Communication\ClientProxy.cs" />
<Compile Include="Communication\ProxyObjectFactory.cs" />
<Compile Include="Communication\RuntimeProxy.cs" />
<Compile Include="Communication\ServiceProxy.cs" />
<Compile Include="Communication\Proxies\BaseProxy.cs" />
<Compile Include="Communication\Hosts\BaseHost.cs" />
<Compile Include="Communication\Proxies\ClientProxy.cs" />
<Compile Include="Communication\Proxies\ProxyObjectFactory.cs" />
<Compile Include="Communication\Proxies\RuntimeProxy.cs" />
<Compile Include="Communication\Proxies\ServiceProxy.cs" />
<Compile Include="Logging\DefaultLogFormatter.cs" />
<Compile Include="Logging\LogFileWriter.cs" />
<Compile Include="Logging\LogMessage.cs" />

View file

@ -8,7 +8,7 @@
using SafeExamBrowser.Contracts.Communication.Proxies;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication;
using SafeExamBrowser.Core.Communication.Proxies;
using SafeExamBrowser.Core.Logging;
namespace SafeExamBrowser.Runtime.Communication

View file

@ -14,7 +14,7 @@ using SafeExamBrowser.Contracts.Communication.Hosts;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication;
using SafeExamBrowser.Core.Communication.Hosts;
namespace SafeExamBrowser.Runtime.Communication
{

View file

@ -14,7 +14,7 @@ using SafeExamBrowser.Contracts.Behaviour.OperationModel;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Behaviour.OperationModel;
using SafeExamBrowser.Core.Communication;
using SafeExamBrowser.Core.Communication.Proxies;
using SafeExamBrowser.Core.I18n;
using SafeExamBrowser.Core.Logging;
using SafeExamBrowser.Runtime.Behaviour;