2018-03-15 09:55:04 +01:00
|
|
|
|
/*
|
|
|
|
|
* 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.Messages;
|
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Responses;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2018-03-15 10:06:15 +01:00
|
|
|
|
using SafeExamBrowser.Core.Communication;
|
2018-03-15 09:55:04 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Core.UnitTests.Communication
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class BaseProxyTests
|
|
|
|
|
{
|
|
|
|
|
private Mock<IProxyObjectFactory> proxyObjectFactory;
|
|
|
|
|
private Mock<ILogger> logger;
|
2018-03-15 10:06:15 +01:00
|
|
|
|
private BaseProxy sut;
|
2018-03-15 09:55:04 +01:00
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
proxyObjectFactory = new Mock<IProxyObjectFactory>();
|
|
|
|
|
logger = new Mock<ILogger>();
|
|
|
|
|
|
|
|
|
|
sut = new BaseProxyImpl("net.pipe://some/address/here", proxyObjectFactory.Object, logger.Object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustConnectCorrectly()
|
|
|
|
|
{
|
|
|
|
|
var proxy = new Mock<ICommunication>();
|
|
|
|
|
var response = new ConnectionResponse
|
|
|
|
|
{
|
|
|
|
|
CommunicationToken = Guid.NewGuid(),
|
|
|
|
|
ConnectionEstablished = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(response);
|
|
|
|
|
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
|
|
|
|
|
|
|
|
|
|
var token = Guid.NewGuid();
|
|
|
|
|
var connected = sut.Connect(token);
|
|
|
|
|
|
|
|
|
|
proxy.Verify(p => p.Connect(token), Times.Once);
|
|
|
|
|
proxyObjectFactory.Verify(f => f.CreateObject(It.IsAny<string>()), Times.Once);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(connected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustDisconnectCorrectly()
|
|
|
|
|
{
|
|
|
|
|
var proxy = new Mock<ICommunication>();
|
|
|
|
|
var connectionResponse = new ConnectionResponse
|
|
|
|
|
{
|
|
|
|
|
CommunicationToken = Guid.NewGuid(),
|
|
|
|
|
ConnectionEstablished = true
|
|
|
|
|
};
|
|
|
|
|
var disconnectionResponse = new DisconnectionResponse
|
|
|
|
|
{
|
|
|
|
|
ConnectionTerminated = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(connectionResponse);
|
|
|
|
|
proxy.Setup(p => p.Disconnect(It.IsAny<DisconnectionMessage>())).Returns(disconnectionResponse);
|
|
|
|
|
proxy.As<ICommunicationObject>().Setup(o => o.State).Returns(CommunicationState.Opened);
|
|
|
|
|
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
|
|
|
|
|
|
|
|
|
|
var token = Guid.NewGuid();
|
|
|
|
|
var connected = sut.Connect(token);
|
|
|
|
|
var disconnected = sut.Disconnect();
|
|
|
|
|
|
|
|
|
|
proxy.Verify(p => p.Disconnect(It.Is<DisconnectionMessage>(m => m.CommunicationToken == connectionResponse.CommunicationToken)), Times.Once);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(connected);
|
|
|
|
|
Assert.IsTrue(disconnected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustHandleConnectionRefusalCorrectly()
|
|
|
|
|
{
|
|
|
|
|
var proxy = new Mock<ICommunication>();
|
|
|
|
|
var response = new ConnectionResponse
|
|
|
|
|
{
|
|
|
|
|
CommunicationToken = Guid.NewGuid(),
|
|
|
|
|
ConnectionEstablished = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(response);
|
|
|
|
|
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
|
|
|
|
|
|
|
|
|
|
var token = Guid.NewGuid();
|
|
|
|
|
var connected = sut.Connect(token);
|
|
|
|
|
|
|
|
|
|
proxy.Verify(p => p.Connect(token), Times.Once);
|
|
|
|
|
proxyObjectFactory.Verify(f => f.CreateObject(It.IsAny<string>()), Times.Once);
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(connected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
[ExpectedException(typeof(InvalidOperationException))]
|
|
|
|
|
public void MustFailToDisconnectIfNotConnected()
|
|
|
|
|
{
|
|
|
|
|
sut.Disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
[ExpectedException(typeof(CommunicationException))]
|
|
|
|
|
public void MustFailToDisconnectIfChannelNotOpen()
|
|
|
|
|
{
|
|
|
|
|
var proxy = new Mock<ICommunication>();
|
|
|
|
|
var response = new ConnectionResponse
|
|
|
|
|
{
|
|
|
|
|
CommunicationToken = Guid.NewGuid(),
|
|
|
|
|
ConnectionEstablished = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(response);
|
|
|
|
|
proxy.As<ICommunicationObject>().Setup(o => o.State).Returns(CommunicationState.Faulted);
|
|
|
|
|
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
|
|
|
|
|
|
|
|
|
|
var token = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
sut.Connect(token);
|
|
|
|
|
sut.Disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
[ExpectedException(typeof(InvalidOperationException))]
|
|
|
|
|
public void MustFailToSendIfNotConnected()
|
|
|
|
|
{
|
2018-03-15 10:06:15 +01:00
|
|
|
|
(sut as BaseProxyImpl).Send(new Mock<Message>().Object);
|
2018-03-15 09:55:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
[ExpectedException(typeof(CommunicationException))]
|
|
|
|
|
public void MustFailToSendIfChannelNotOpen()
|
|
|
|
|
{
|
|
|
|
|
var proxy = new Mock<ICommunication>();
|
|
|
|
|
var response = new ConnectionResponse
|
|
|
|
|
{
|
|
|
|
|
CommunicationToken = Guid.NewGuid(),
|
|
|
|
|
ConnectionEstablished = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(response);
|
|
|
|
|
proxy.As<ICommunicationObject>().Setup(o => o.State).Returns(CommunicationState.Faulted);
|
|
|
|
|
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
|
|
|
|
|
|
|
|
|
|
var token = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
sut.Connect(token);
|
2018-03-15 10:06:15 +01:00
|
|
|
|
(sut as BaseProxyImpl).Send(new Mock<Message>().Object);
|
2018-03-15 09:55:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
[ExpectedException(typeof(ArgumentNullException))]
|
|
|
|
|
public void MustNotAllowSendingNull()
|
|
|
|
|
{
|
2018-03-15 10:06:15 +01:00
|
|
|
|
(sut as BaseProxyImpl).Send(null);
|
2018-03-15 09:55:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustSendCorrectly()
|
|
|
|
|
{
|
|
|
|
|
var proxy = new Mock<ICommunication>();
|
|
|
|
|
var connectionResponse = new ConnectionResponse
|
|
|
|
|
{
|
|
|
|
|
CommunicationToken = Guid.NewGuid(),
|
|
|
|
|
ConnectionEstablished = true
|
|
|
|
|
};
|
|
|
|
|
var message = new SimpleMessage(SimpleMessagePurport.Authenticate);
|
|
|
|
|
var response = new Mock<Response>();
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(connectionResponse);
|
|
|
|
|
proxy.Setup(p => p.Send(message)).Returns(response.Object);
|
|
|
|
|
proxy.As<ICommunicationObject>().Setup(o => o.State).Returns(CommunicationState.Opened);
|
|
|
|
|
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
|
|
|
|
|
|
|
|
|
|
var token = Guid.NewGuid();
|
|
|
|
|
var connected = sut.Connect(token);
|
2018-03-15 10:06:15 +01:00
|
|
|
|
var received = (sut as BaseProxyImpl).Send(message);
|
2018-03-15 09:55:04 +01:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(response.Object, received);
|
|
|
|
|
Assert.AreEqual(connectionResponse.CommunicationToken, message.CommunicationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|