SEBWIN-301: Extended unit tests for service and related functionality.

This commit is contained in:
dbuechel 2019-06-27 10:47:41 +02:00
parent dd78bc1fbc
commit 3338710949
6 changed files with 301 additions and 0 deletions

View file

@ -47,6 +47,28 @@ namespace SafeExamBrowser.Lockdown.UnitTests
Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, threadId);
}
[TestMethod]
public void MustNotStartMultipleTimes()
{
var counter = 0;
var list = new List<IFeatureConfiguration>();
var sync = new AutoResetEvent(false);
backup.Setup(b => b.GetAllConfigurations()).Returns(() => { counter++; Thread.Sleep(50); sync.Set(); return list; });
sut.Start();
sut.Start();
sut.Start();
sut.Start();
sut.Start();
sut.Start();
sut.Start();
sync.WaitOne();
sut.Stop();
Assert.AreEqual(1, counter);
}
[TestMethod]
public void MustNotTerminateUntilAllChangesReverted()
{

View file

@ -135,6 +135,16 @@ namespace SafeExamBrowser.Service.UnitTests.Communication
Assert.AreEqual(SimpleResponsePurport.UnknownMessage, (response as SimpleResponse)?.Purport);
}
[TestMethod]
public void MustNotFailIfNoEventHandlersSubscribed()
{
var token = sut.Connect(Guid.Empty).CommunicationToken.Value;
sut.Send(new SessionStartMessage(null) { CommunicationToken = token });
sut.Send(new SessionStopMessage(Guid.Empty) { CommunicationToken = token });
sut.Disconnect(new DisconnectionMessage { CommunicationToken = token, Interlocutor = Interlocutor.Runtime });
}
private class TestMessage : Message { };
}
}

View file

@ -0,0 +1,208 @@
/*
* 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/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.Core.OperationModel;
using SafeExamBrowser.Contracts.Lockdown;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Service.Operations;
namespace SafeExamBrowser.Service.UnitTests.Operations
{
[TestClass]
public class LockdownOperationTests
{
private Mock<IFeatureConfigurationBackup> backup;
private Mock<IFeatureConfigurationFactory> factory;
private Mock<ILogger> logger;
private Settings settings;
private SessionContext sessionContext;
private LockdownOperation sut;
[TestInitialize]
public void Initialize()
{
backup = new Mock<IFeatureConfigurationBackup>();
factory = new Mock<IFeatureConfigurationFactory>();
logger = new Mock<ILogger>();
settings = new Settings();
sessionContext = new SessionContext { Configuration = new ServiceConfiguration { Settings = settings } };
sut = new LockdownOperation(backup.Object, factory.Object, logger.Object, sessionContext);
}
[TestMethod]
public void Perform_MustSetConfigurationsCorrectly()
{
var configuration = new Mock<IFeatureConfiguration>();
var count = typeof(IFeatureConfigurationFactory).GetMethods().Where(m => m.Name.StartsWith("Create")).Count();
configuration.SetReturnsDefault(true);
factory.SetReturnsDefault(configuration.Object);
settings.Service.DisableChromeNotifications = true;
settings.Service.DisablePowerOptions = true;
settings.Service.DisableSignout = true;
var result = sut.Perform();
backup.Verify(b => b.Save(It.Is<IFeatureConfiguration>(c => c == configuration.Object)), Times.Exactly(count));
configuration.Verify(c => c.DisableFeature(), Times.Exactly(3));
configuration.Verify(c => c.EnableFeature(), Times.Exactly(count - 3));
configuration.Verify(c => c.Monitor(), Times.Exactly(count));
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void Perform_MustUseSameGroupForAllConfigurations()
{
var configuration = new Mock<IFeatureConfiguration>();
var groupId = default(Guid);
configuration.SetReturnsDefault(true);
factory.Setup(f => f.CreateChromeNotificationConfiguration(It.IsAny<Guid>())).Returns(configuration.Object).Callback<Guid>(id => groupId = id);
factory.SetReturnsDefault(configuration.Object);
sut.Perform();
factory.Verify(f => f.CreateChromeNotificationConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateEaseOfAccessConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateNetworkOptionsConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreatePasswordChangeConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreatePowerOptionsConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateRemoteConnectionConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateSignoutConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateTaskManagerConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateUserLockConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateUserSwitchConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateVmwareOverlayConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
factory.Verify(f => f.CreateWindowsUpdateConfiguration(It.Is<Guid>(id => id == groupId)), Times.Once);
}
[TestMethod]
public void Perform_MustImmediatelyAbortOnFailure()
{
var configuration = new Mock<IFeatureConfiguration>();
var count = typeof(IFeatureConfigurationFactory).GetMethods().Where(m => m.Name.StartsWith("Create")).Count();
var counter = 0;
var offset = 3;
configuration.Setup(c => c.EnableFeature()).Returns(() => ++counter < count - offset);
factory.SetReturnsDefault(configuration.Object);
var result = sut.Perform();
backup.Verify(b => b.Save(It.Is<IFeatureConfiguration>(c => c == configuration.Object)), Times.Exactly(count - offset));
configuration.Verify(c => c.DisableFeature(), Times.Never);
configuration.Verify(c => c.EnableFeature(), Times.Exactly(count - offset));
configuration.Verify(c => c.Monitor(), Times.Exactly(count - offset - 1));
Assert.AreEqual(OperationResult.Failed, result);
}
[TestMethod]
public void Revert_MustRestoreConfigurationsCorrectly()
{
var configuration1 = new Mock<IFeatureConfiguration>();
var configuration2 = new Mock<IFeatureConfiguration>();
var configuration3 = new Mock<IFeatureConfiguration>();
var configuration4 = new Mock<IFeatureConfiguration>();
var configurations = new List<IFeatureConfiguration>
{
configuration1.Object,
configuration2.Object,
configuration3.Object,
configuration4.Object
};
backup.Setup(b => b.GetBy(It.IsAny<Guid>())).Returns(configurations);
configuration1.Setup(c => c.Restore()).Returns(true);
configuration2.Setup(c => c.Restore()).Returns(true);
configuration3.Setup(c => c.Restore()).Returns(true);
configuration4.Setup(c => c.Restore()).Returns(true);
var result = sut.Revert();
backup.Verify(b => b.Delete(It.Is<IFeatureConfiguration>(c => c == configuration1.Object)), Times.Once);
backup.Verify(b => b.Delete(It.Is<IFeatureConfiguration>(c => c == configuration2.Object)), Times.Once);
backup.Verify(b => b.Delete(It.Is<IFeatureConfiguration>(c => c == configuration3.Object)), Times.Once);
backup.Verify(b => b.Delete(It.Is<IFeatureConfiguration>(c => c == configuration4.Object)), Times.Once);
configuration1.Verify(c => c.DisableFeature(), Times.Never);
configuration1.Verify(c => c.EnableFeature(), Times.Never);
configuration1.Verify(c => c.Restore(), Times.Once);
configuration2.Verify(c => c.DisableFeature(), Times.Never);
configuration2.Verify(c => c.EnableFeature(), Times.Never);
configuration2.Verify(c => c.Restore(), Times.Once);
configuration3.Verify(c => c.DisableFeature(), Times.Never);
configuration3.Verify(c => c.EnableFeature(), Times.Never);
configuration3.Verify(c => c.Restore(), Times.Once);
configuration4.Verify(c => c.DisableFeature(), Times.Never);
configuration4.Verify(c => c.EnableFeature(), Times.Never);
configuration4.Verify(c => c.Restore(), Times.Once);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void Revert_MustContinueToRevertOnFailure()
{
var configuration1 = new Mock<IFeatureConfiguration>();
var configuration2 = new Mock<IFeatureConfiguration>();
var configuration3 = new Mock<IFeatureConfiguration>();
var configuration4 = new Mock<IFeatureConfiguration>();
var configurations = new List<IFeatureConfiguration>
{
configuration1.Object,
configuration2.Object,
configuration3.Object,
configuration4.Object
};
backup.Setup(b => b.GetBy(It.IsAny<Guid>())).Returns(configurations);
configuration1.Setup(c => c.Restore()).Returns(true);
configuration2.Setup(c => c.Restore()).Returns(false);
configuration3.Setup(c => c.Restore()).Returns(false);
configuration4.Setup(c => c.Restore()).Returns(true);
var result = sut.Revert();
backup.Verify(b => b.Delete(It.Is<IFeatureConfiguration>(c => c == configuration1.Object)), Times.Once);
backup.Verify(b => b.Delete(It.Is<IFeatureConfiguration>(c => c == configuration2.Object)), Times.Never);
backup.Verify(b => b.Delete(It.Is<IFeatureConfiguration>(c => c == configuration3.Object)), Times.Never);
backup.Verify(b => b.Delete(It.Is<IFeatureConfiguration>(c => c == configuration4.Object)), Times.Once);
configuration1.Verify(c => c.DisableFeature(), Times.Never);
configuration1.Verify(c => c.EnableFeature(), Times.Never);
configuration1.Verify(c => c.Restore(), Times.Once);
configuration2.Verify(c => c.DisableFeature(), Times.Never);
configuration2.Verify(c => c.EnableFeature(), Times.Never);
configuration2.Verify(c => c.Restore(), Times.Once);
configuration3.Verify(c => c.DisableFeature(), Times.Never);
configuration3.Verify(c => c.EnableFeature(), Times.Never);
configuration3.Verify(c => c.Restore(), Times.Once);
configuration4.Verify(c => c.DisableFeature(), Times.Never);
configuration4.Verify(c => c.EnableFeature(), Times.Never);
configuration4.Verify(c => c.Restore(), Times.Once);
Assert.AreEqual(OperationResult.Failed, result);
}
}
}

View file

@ -0,0 +1,58 @@
/*
* 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/.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Core.OperationModel;
using SafeExamBrowser.Contracts.Lockdown;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Service.Operations;
namespace SafeExamBrowser.Service.UnitTests.Operations
{
[TestClass]
public class RestoreOperationTests
{
private SessionContext sessionContext;
private Mock<IAutoRestoreMechanism> autoRestoreMechanism;
private Mock<IFeatureConfigurationBackup> backup;
private Mock<ILogger> logger;
private RestoreOperation sut;
[TestInitialize]
public void Initialize()
{
autoRestoreMechanism = new Mock<IAutoRestoreMechanism>();
backup = new Mock<IFeatureConfigurationBackup>();
logger = new Mock<ILogger>();
sessionContext = new SessionContext { AutoRestoreMechanism = autoRestoreMechanism.Object };
sut = new RestoreOperation(backup.Object, logger.Object, sessionContext);
}
[TestMethod]
public void Perform_MustStartAutoRestore()
{
var result = sut.Perform();
autoRestoreMechanism.Verify(m => m.Start(), Times.Once);
autoRestoreMechanism.Verify(m => m.Stop(), Times.Never);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void Revert_MustStopAutoRestore()
{
var result = sut.Revert();
autoRestoreMechanism.Verify(m => m.Start(), Times.Never);
autoRestoreMechanism.Verify(m => m.Stop(), Times.Once);
Assert.AreEqual(OperationResult.Success, result);
}
}
}

View file

@ -82,6 +82,8 @@
<ItemGroup>
<Compile Include="Communication\ServiceHostTests.cs" />
<Compile Include="Operations\EventStub.cs" />
<Compile Include="Operations\LockdownOperationTests.cs" />
<Compile Include="Operations\RestoreOperationTests.cs" />
<Compile Include="Operations\ServiceEventCleanupOperationTests.cs" />
<Compile Include="Operations\SessionActivationOperationTests.cs" />
<Compile Include="Operations\SessionInitializationOperationTests.cs" />

View file

@ -95,6 +95,7 @@ namespace SafeExamBrowser.Service.UnitTests
bootstrapSequence.Setup(b => b.TryPerform()).Returns(OperationResult.Success);
sessionContext.Configuration = new ServiceConfiguration { SessionId = Guid.NewGuid() };
sessionContext.IsRunning = true;
sut.TryStart();
serviceHost.Raise(h => h.SessionStopRequested += null, args);