2019-06-07 15:26:03 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2019 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-06-19 15:40:21 +02:00
|
|
|
|
using System;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using Moq;
|
2019-06-19 15:40:21 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Events;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Hosts;
|
2019-06-11 09:53:33 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Service.UnitTests
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class ServiceControllerTests
|
|
|
|
|
{
|
2019-06-18 10:18:56 +02:00
|
|
|
|
private Mock<ILogger> logger;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
private Mock<IOperationSequence> bootstrapSequence;
|
|
|
|
|
private SessionContext sessionContext;
|
2019-06-19 15:40:21 +02:00
|
|
|
|
private Mock<IOperationSequence> sessionSequence;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
private Mock<IServiceHost> serviceHost;
|
|
|
|
|
private ServiceController sut;
|
|
|
|
|
|
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
2019-06-18 10:18:56 +02:00
|
|
|
|
logger = new Mock<ILogger>();
|
2019-06-07 15:26:03 +02:00
|
|
|
|
bootstrapSequence = new Mock<IOperationSequence>();
|
|
|
|
|
sessionContext = new SessionContext();
|
2019-06-19 15:40:21 +02:00
|
|
|
|
sessionSequence = new Mock<IOperationSequence>();
|
2019-06-07 15:26:03 +02:00
|
|
|
|
serviceHost = new Mock<IServiceHost>();
|
|
|
|
|
|
2019-06-18 10:18:56 +02:00
|
|
|
|
sut = new ServiceController(logger.Object, bootstrapSequence.Object, sessionSequence.Object, serviceHost.Object, sessionContext);
|
2019-06-07 15:26:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 15:40:21 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Communication_MustStartNewSessionUponRequest()
|
|
|
|
|
{
|
|
|
|
|
var args = new SessionStartEventArgs { Configuration = new ServiceConfiguration { SessionId = Guid.NewGuid() } };
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
|
|
|
|
|
sessionSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
|
|
|
|
|
|
|
|
|
|
sut.TryStart();
|
|
|
|
|
serviceHost.Raise(h => h.SessionStartRequested += null, args);
|
|
|
|
|
|
|
|
|
|
sessionSequence.Verify(s => s.TryPerform(), Times.Once);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(sessionContext.Configuration);
|
|
|
|
|
Assert.AreEqual(args.Configuration.SessionId, sessionContext.Configuration.SessionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Communication_MustNotAllowNewSessionDuringActiveSession()
|
|
|
|
|
{
|
|
|
|
|
var args = new SessionStartEventArgs { Configuration = new ServiceConfiguration { SessionId = Guid.NewGuid() } };
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
|
|
|
|
|
sessionContext.Configuration = new ServiceConfiguration { SessionId = Guid.NewGuid() };
|
2019-06-27 08:32:37 +02:00
|
|
|
|
sessionContext.IsRunning = true;
|
2019-06-19 15:40:21 +02:00
|
|
|
|
|
|
|
|
|
sut.TryStart();
|
|
|
|
|
serviceHost.Raise(h => h.SessionStartRequested += null, args);
|
|
|
|
|
|
|
|
|
|
sessionSequence.Verify(s => s.TryPerform(), Times.Never);
|
|
|
|
|
|
|
|
|
|
Assert.AreNotEqual(args.Configuration.SessionId, sessionContext.Configuration.SessionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Communication_MustStopActiveSessionUponRequest()
|
|
|
|
|
{
|
|
|
|
|
var args = new SessionStopEventArgs { SessionId = Guid.NewGuid() };
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
|
|
|
|
|
sessionContext.Configuration = new ServiceConfiguration { SessionId = args.SessionId };
|
2019-06-27 08:32:37 +02:00
|
|
|
|
sessionContext.IsRunning = true;
|
2019-06-19 15:40:21 +02:00
|
|
|
|
|
|
|
|
|
sut.TryStart();
|
|
|
|
|
serviceHost.Raise(h => h.SessionStopRequested += null, args);
|
|
|
|
|
|
|
|
|
|
sessionSequence.Verify(s => s.TryRevert(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Communication_MustNotStopSessionWithWrongId()
|
|
|
|
|
{
|
|
|
|
|
var args = new SessionStopEventArgs { SessionId = Guid.NewGuid() };
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
|
|
|
|
|
sessionContext.Configuration = new ServiceConfiguration { SessionId = Guid.NewGuid() };
|
|
|
|
|
|
|
|
|
|
sut.TryStart();
|
|
|
|
|
serviceHost.Raise(h => h.SessionStopRequested += null, args);
|
|
|
|
|
|
|
|
|
|
sessionSequence.Verify(s => s.TryRevert(), Times.Never);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Communication_MustNotStopSessionWithoutActiveSession()
|
|
|
|
|
{
|
|
|
|
|
var args = new SessionStopEventArgs { SessionId = Guid.NewGuid() };
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
|
2019-06-27 08:32:37 +02:00
|
|
|
|
sessionContext.IsRunning = false;
|
2019-06-19 15:40:21 +02:00
|
|
|
|
|
|
|
|
|
sut.TryStart();
|
|
|
|
|
serviceHost.Raise(h => h.SessionStopRequested += null, args);
|
|
|
|
|
|
|
|
|
|
sessionSequence.Verify(s => s.TryRevert(), Times.Never);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 15:26:03 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Start_MustOnlyPerformBootstrapSequence()
|
|
|
|
|
{
|
|
|
|
|
bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
|
|
|
|
|
sessionSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
|
2019-06-18 10:18:56 +02:00
|
|
|
|
sessionContext.Configuration = null;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
|
|
|
|
|
var success = sut.TryStart();
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Verify(b => b.TryPerform(), Times.Once);
|
|
|
|
|
bootstrapSequence.Verify(b => b.TryRevert(), Times.Never);
|
|
|
|
|
sessionSequence.Verify(b => b.TryPerform(), Times.Never);
|
|
|
|
|
sessionSequence.Verify(b => b.TryRevert(), Times.Never);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Stop_MustRevertSessionThenBootstrapSequence()
|
|
|
|
|
{
|
|
|
|
|
var order = 0;
|
|
|
|
|
var bootstrap = 0;
|
|
|
|
|
var session = 0;
|
|
|
|
|
|
|
|
|
|
sut.TryStart();
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Reset();
|
|
|
|
|
sessionSequence.Reset();
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Setup(b => b.TryRevert()).Returns(OperationResult.Success).Callback(() => bootstrap = ++order);
|
|
|
|
|
sessionSequence.Setup(b => b.TryRevert()).Returns(OperationResult.Success).Callback(() => session = ++order);
|
2019-06-18 10:18:56 +02:00
|
|
|
|
sessionContext.Configuration = new ServiceConfiguration();
|
2019-06-27 08:32:37 +02:00
|
|
|
|
sessionContext.IsRunning = true;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
|
|
|
|
|
sut.Terminate();
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Verify(b => b.TryPerform(), Times.Never);
|
|
|
|
|
bootstrapSequence.Verify(b => b.TryRevert(), Times.Once);
|
|
|
|
|
sessionSequence.Verify(b => b.TryPerform(), Times.Never);
|
|
|
|
|
sessionSequence.Verify(b => b.TryRevert(), Times.Once);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, session);
|
|
|
|
|
Assert.AreEqual(2, bootstrap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Stop_MustNotRevertSessionSequenceIfNoSessionRunning()
|
|
|
|
|
{
|
|
|
|
|
var order = 0;
|
|
|
|
|
var bootstrap = 0;
|
|
|
|
|
var session = 0;
|
|
|
|
|
|
|
|
|
|
sut.TryStart();
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Reset();
|
|
|
|
|
sessionSequence.Reset();
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Setup(b => b.TryRevert()).Returns(OperationResult.Success).Callback(() => bootstrap = ++order);
|
|
|
|
|
sessionSequence.Setup(b => b.TryRevert()).Returns(OperationResult.Success).Callback(() => session = ++order);
|
2019-06-18 10:18:56 +02:00
|
|
|
|
sessionContext.Configuration = null;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
|
|
|
|
|
sut.Terminate();
|
|
|
|
|
|
|
|
|
|
bootstrapSequence.Verify(b => b.TryPerform(), Times.Never);
|
|
|
|
|
bootstrapSequence.Verify(b => b.TryRevert(), Times.Once);
|
|
|
|
|
sessionSequence.Verify(b => b.TryPerform(), Times.Never);
|
|
|
|
|
sessionSequence.Verify(b => b.TryRevert(), Times.Never);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(0, session);
|
|
|
|
|
Assert.AreEqual(1, bootstrap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|