2018-03-16 09:28:33 +01:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2018-03-16 09:28:33 +01:00
|
|
|
|
*
|
|
|
|
|
* 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;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Communication.Contracts;
|
|
|
|
|
using SafeExamBrowser.Communication.Contracts.Data;
|
|
|
|
|
using SafeExamBrowser.Communication.Contracts.Proxies;
|
2019-02-15 10:01:52 +01:00
|
|
|
|
using SafeExamBrowser.Communication.Proxies;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Configuration.Contracts;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Communication.UnitTests.Proxies
|
2018-03-16 09:28:33 +01:00
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class RuntimeProxyTests
|
|
|
|
|
{
|
|
|
|
|
private Mock<ILogger> logger;
|
|
|
|
|
private Mock<IProxyObjectFactory> proxyObjectFactory;
|
2018-03-16 15:46:53 +01:00
|
|
|
|
private Mock<IProxyObject> proxy;
|
2018-03-16 09:28:33 +01:00
|
|
|
|
private RuntimeProxy sut;
|
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
var response = new ConnectionResponse
|
|
|
|
|
{
|
|
|
|
|
CommunicationToken = Guid.NewGuid(),
|
|
|
|
|
ConnectionEstablished = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
logger = new Mock<ILogger>();
|
|
|
|
|
proxyObjectFactory = new Mock<IProxyObjectFactory>();
|
2018-03-16 15:46:53 +01:00
|
|
|
|
proxy = new Mock<IProxyObject>();
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Connect(It.IsAny<Guid>())).Returns(response);
|
2018-03-16 15:46:53 +01:00
|
|
|
|
proxy.Setup(o => o.State).Returns(CommunicationState.Opened);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
proxyObjectFactory.Setup(f => f.CreateObject(It.IsAny<string>())).Returns(proxy.Object);
|
|
|
|
|
|
2019-06-18 10:18:56 +02:00
|
|
|
|
sut = new RuntimeProxy("net.pipe://random/address/here", proxyObjectFactory.Object, logger.Object, default(Interlocutor));
|
2018-03-16 09:28:33 +01:00
|
|
|
|
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);
|
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
var communication = sut.GetConfiguration();
|
|
|
|
|
var configuration = communication.Value.Configuration;
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
|
|
|
|
proxy.Verify(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ConfigurationNeeded)), Times.Once);
|
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
Assert.IsTrue(communication.Success);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
Assert.IsInstanceOfType(configuration, typeof(ClientConfiguration));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFailIfConfigurationNotRetrieved()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ConfigurationNeeded))).Returns<Response>(null);
|
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
var communication = sut.GetConfiguration();
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(communication.Success);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlyInformClientReady()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ClientIsReady))).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
|
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
var communication = sut.InformClientReady();
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
Assert.IsTrue(communication.Success);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
proxy.Verify(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ClientIsReady)), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFailIfClientReadyNotAcknowledged()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.ClientIsReady))).Returns<Response>(null);
|
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
var communication = sut.InformClientReady();
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(communication.Success);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlyRequestReconfiguration()
|
|
|
|
|
{
|
2020-09-24 12:55:20 +02:00
|
|
|
|
var path = "file:///C:/Some/file/url.seb";
|
|
|
|
|
var url = @"https://www.host.abc/someresource.seb";
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
2020-09-24 12:55:20 +02:00
|
|
|
|
proxy.Setup(p => p.Send(It.Is<ReconfigurationMessage>(m => m.ConfigurationPath == path))).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
2020-09-24 12:55:20 +02:00
|
|
|
|
var communication = sut.RequestReconfiguration(path, url);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
Assert.IsTrue(communication.Success);
|
2020-09-24 12:55:20 +02:00
|
|
|
|
proxy.Verify(p => p.Send(It.Is<ReconfigurationMessage>(m => m.ConfigurationPath == path && m.ResourceUrl == url)), Times.Once);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2018-07-04 09:53:33 +02:00
|
|
|
|
public void MustFailIfReconfigurationRequestNotAcknowledged()
|
2018-03-16 09:28:33 +01:00
|
|
|
|
{
|
2020-09-24 12:55:20 +02:00
|
|
|
|
var path = "file:///C:/Some/file/url.seb";
|
|
|
|
|
var url = @"https://www.host.abc/someresource.seb";
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
2020-09-24 12:55:20 +02:00
|
|
|
|
proxy.Setup(p => p.Send(It.Is<ReconfigurationMessage>(m => m.ConfigurationPath == path))).Returns<Response>(null);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
2020-09-24 12:55:20 +02:00
|
|
|
|
var communication = sut.RequestReconfiguration(path, url);
|
2018-08-10 13:23:24 +02:00
|
|
|
|
|
|
|
|
|
Assert.IsFalse(communication.Success);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlyRequestShutdown()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.RequestShutdown))).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
|
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
var communication = sut.RequestShutdown();
|
2018-03-16 09:28:33 +01:00
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
Assert.IsTrue(communication.Success);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
proxy.Verify(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.RequestShutdown)), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFailIfShutdownRequestNotAcknowledged()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.Is<SimpleMessage>(m => m.Purport == SimpleMessagePurport.RequestShutdown))).Returns<Response>(null);
|
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
var communication = sut.RequestShutdown();
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(communication.Success);
|
2018-03-16 09:28:33 +01:00
|
|
|
|
}
|
2018-08-10 15:16:33 +02:00
|
|
|
|
|
2021-07-26 11:31:49 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlySubmitExamSelection()
|
|
|
|
|
{
|
|
|
|
|
var examId = "abc123";
|
|
|
|
|
var requestId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<ExamSelectionReplyMessage>())).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
|
|
|
|
|
|
|
|
|
|
var communication = sut.SubmitExamSelectionResult(requestId, true, examId);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(communication.Success);
|
|
|
|
|
proxy.Verify(p => p.Send(It.Is<ExamSelectionReplyMessage>(m => m.SelectedExamId == examId && m.RequestId == requestId && m.Success)), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFailIfExamSelectionTransmissionNotAcknowledged()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<ExamSelectionReplyMessage>())).Returns<Response>(null);
|
|
|
|
|
|
|
|
|
|
var communication = sut.SubmitExamSelectionResult(default(Guid), false);
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(communication.Success);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-10 15:16:33 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlySubmitPassword()
|
|
|
|
|
{
|
|
|
|
|
var password = "blubb";
|
|
|
|
|
var requestId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<PasswordReplyMessage>())).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
|
|
|
|
|
|
|
|
|
|
var communication = sut.SubmitPassword(requestId, true, password);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(communication.Success);
|
|
|
|
|
proxy.Verify(p => p.Send(It.Is<PasswordReplyMessage>(m => m.Password == password && m.RequestId == requestId && m.Success)), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFailIfPasswordTransmissionNotAcknowledged()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<PasswordReplyMessage>())).Returns<Response>(null);
|
|
|
|
|
|
|
|
|
|
var communication = sut.SubmitPassword(default(Guid), false);
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(communication.Success);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 14:02:36 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlySubmitMessageBoxResult()
|
|
|
|
|
{
|
|
|
|
|
var result = 1234;
|
|
|
|
|
var requestId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<MessageBoxReplyMessage>())).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
|
|
|
|
|
|
|
|
|
|
var communication = sut.SubmitMessageBoxResult(requestId, result);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(communication.Success);
|
|
|
|
|
proxy.Verify(p => p.Send(It.Is<MessageBoxReplyMessage>(m => m.Result == result && m.RequestId == requestId)), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFailIfMessageBoxResultTransmissionNotAcknowledged()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<MessageBoxReplyMessage>())).Returns<Response>(null);
|
|
|
|
|
|
|
|
|
|
var communication = sut.SubmitMessageBoxResult(default(Guid), default(int));
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(communication.Success);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 11:31:49 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlySubmitServerFailureAction()
|
|
|
|
|
{
|
|
|
|
|
var requestId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<ServerFailureActionReplyMessage>())).Returns(new SimpleResponse(SimpleResponsePurport.Acknowledged));
|
|
|
|
|
|
|
|
|
|
var communication = sut.SubmitServerFailureActionResult(requestId, true, true, false);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(communication.Success);
|
|
|
|
|
proxy.Verify(p => p.Send(It.Is<ServerFailureActionReplyMessage>(m => m.RequestId == requestId && m.Abort && m.Fallback && !m.Retry)), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFailIfServerFailureActionTransmissionNotAcknowledged()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<ServerFailureActionReplyMessage>())).Returns<Response>(null);
|
|
|
|
|
|
|
|
|
|
var communication = sut.SubmitServerFailureActionResult(default(Guid), default(bool), default(bool), default(bool));
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(communication.Success);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 14:02:36 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustExecuteOperationsFailsafe()
|
|
|
|
|
{
|
|
|
|
|
proxy.Setup(p => p.Send(It.IsAny<Message>())).Throws<Exception>();
|
|
|
|
|
|
|
|
|
|
var client = sut.InformClientReady();
|
|
|
|
|
var configuration = sut.GetConfiguration();
|
2021-07-26 11:31:49 +02:00
|
|
|
|
var examSelection = sut.SubmitExamSelectionResult(default(Guid), default(bool));
|
2019-08-30 14:02:36 +02:00
|
|
|
|
var message = sut.SubmitMessageBoxResult(default(Guid), default(int));
|
|
|
|
|
var password = sut.SubmitPassword(default(Guid), false);
|
2020-09-24 12:55:20 +02:00
|
|
|
|
var reconfiguration = sut.RequestReconfiguration(null, null);
|
2021-07-26 11:31:49 +02:00
|
|
|
|
var serverFailure = sut.SubmitServerFailureActionResult(default(Guid), default(bool), default(bool), default(bool));
|
2019-08-30 14:02:36 +02:00
|
|
|
|
var shutdown = sut.RequestShutdown();
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(client.Success);
|
|
|
|
|
Assert.IsFalse(configuration.Success);
|
2021-07-26 11:31:49 +02:00
|
|
|
|
Assert.IsFalse(examSelection.Success);
|
2019-08-30 14:02:36 +02:00
|
|
|
|
Assert.IsFalse(message.Success);
|
|
|
|
|
Assert.IsFalse(password.Success);
|
|
|
|
|
Assert.IsFalse(reconfiguration.Success);
|
2021-07-26 11:31:49 +02:00
|
|
|
|
Assert.IsFalse(serverFailure.Success);
|
2019-08-30 14:02:36 +02:00
|
|
|
|
Assert.IsFalse(shutdown.Success);
|
|
|
|
|
}
|
2018-03-16 09:28:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|