2018-01-18 15:14:05 +01:00
|
|
|
|
/*
|
2019-01-09 11:25:21 +01:00
|
|
|
|
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
|
2018-01-18 15:14:05 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-06-21 11:07:46 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using Moq;
|
2018-06-28 13:40:30 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Data;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2019-02-14 12:06:35 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Cryptography;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
2018-09-04 10:58:56 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2018-08-31 10:06:27 +02:00
|
|
|
|
using SafeExamBrowser.Runtime.Operations;
|
2018-10-03 14:35:27 +02:00
|
|
|
|
using SafeExamBrowser.Runtime.Operations.Events;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Runtime.UnitTests.Operations
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class ConfigurationOperationTests
|
|
|
|
|
{
|
2018-06-29 09:50:20 +02:00
|
|
|
|
private AppConfig appConfig;
|
2019-02-14 12:06:35 +01:00
|
|
|
|
private Mock<IHashAlgorithm> hashAlgorithm;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
private Mock<ILogger> logger;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private Mock<IConfigurationRepository> repository;
|
2019-02-14 12:06:35 +01:00
|
|
|
|
private Mock<ISessionConfiguration> currentSession;
|
|
|
|
|
private Mock<ISessionConfiguration> nextSession;
|
2018-10-12 15:23:43 +02:00
|
|
|
|
private SessionContext sessionContext;
|
|
|
|
|
|
2018-01-18 15:14:05 +01:00
|
|
|
|
[TestInitialize]
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig = new AppConfig();
|
2019-02-14 12:06:35 +01:00
|
|
|
|
hashAlgorithm = new Mock<IHashAlgorithm>();
|
2018-03-14 12:07:20 +01:00
|
|
|
|
logger = new Mock<ILogger>();
|
2018-02-06 15:12:11 +01:00
|
|
|
|
repository = new Mock<IConfigurationRepository>();
|
2019-02-14 12:06:35 +01:00
|
|
|
|
currentSession = new Mock<ISessionConfiguration>();
|
|
|
|
|
nextSession = new Mock<ISessionConfiguration>();
|
2018-10-12 15:23:43 +02:00
|
|
|
|
sessionContext = new SessionContext();
|
2018-01-19 09:23:09 +01:00
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.AppDataFolder = @"C:\Not\Really\AppData";
|
|
|
|
|
appConfig.DefaultSettingsFileName = "SettingsDummy.txt";
|
|
|
|
|
appConfig.ProgramDataFolder = @"C:\Not\Really\ProgramData";
|
2019-02-14 12:06:35 +01:00
|
|
|
|
currentSession.SetupGet(s => s.AppConfig).Returns(appConfig);
|
|
|
|
|
nextSession.SetupGet(s => s.AppConfig).Returns(appConfig);
|
|
|
|
|
sessionContext.Current = currentSession.Object;
|
|
|
|
|
sessionContext.Next = nextSession.Object;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2019-02-14 12:06:35 +01:00
|
|
|
|
public void MustUseCommandLineArgumentAs1stPrio()
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var settings = new Settings { ConfigurationMode = ConfigurationMode.Exam };
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
|
|
|
|
var location = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations));
|
|
|
|
|
|
|
|
|
|
appConfig.AppDataFolder = location;
|
|
|
|
|
appConfig.ProgramDataFolder = location;
|
|
|
|
|
appConfig.DefaultSettingsFileName = "SettingsDummy.txt";
|
|
|
|
|
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
var resource = new Uri(url);
|
|
|
|
|
|
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>()), Times.Once);
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustUseProgramDataAs2ndPrio()
|
|
|
|
|
{
|
|
|
|
|
var location = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations));
|
|
|
|
|
var settings = default(Settings);
|
|
|
|
|
|
|
|
|
|
appConfig.ProgramDataFolder = location;
|
|
|
|
|
appConfig.AppDataFolder = $@"{location}\WRONG";
|
|
|
|
|
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
var resource = new Uri(Path.Combine(location, "SettingsDummy.txt"));
|
|
|
|
|
|
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>()), Times.Once);
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustUseAppDataAs3rdPrio()
|
|
|
|
|
{
|
|
|
|
|
var location = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations));
|
|
|
|
|
var settings = default(Settings);
|
|
|
|
|
|
|
|
|
|
appConfig.AppDataFolder = location;
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
var resource = new Uri(Path.Combine(location, "SettingsDummy.txt"));
|
|
|
|
|
|
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>()), Times.Once);
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFallbackToDefaultsAsLastPrio()
|
|
|
|
|
{
|
|
|
|
|
var actualSettings = default(Settings);
|
|
|
|
|
var defaultSettings = new Settings();
|
|
|
|
|
|
|
|
|
|
repository.Setup(r => r.LoadDefaultSettings()).Returns(defaultSettings);
|
|
|
|
|
nextSession.SetupSet<Settings>(s => s.Settings = It.IsAny<Settings>()).Callback(s => actualSettings = s);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
|
|
|
|
|
repository.Verify(r => r.LoadDefaultSettings(), Times.Once);
|
|
|
|
|
nextSession.VerifySet(s => s.Settings = defaultSettings);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
Assert.AreSame(defaultSettings, actualSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustAbortIfWishedByUser()
|
|
|
|
|
{
|
|
|
|
|
var settings = new Settings();
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
|
|
|
|
|
|
|
|
|
sessionContext.Current = null;
|
|
|
|
|
settings.ConfigurationMode = ConfigurationMode.ConfigureClient;
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
repository.Setup(r => r.ConfigureClientWith(It.IsAny<Uri>(), It.IsAny<PasswordParameters>())).Returns(SaveStatus.Success);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is ConfigurationCompletedEventArgs c)
|
|
|
|
|
{
|
|
|
|
|
c.AbortStartup = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(OperationResult.Aborted, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotAbortIfNotWishedByUser()
|
|
|
|
|
{
|
|
|
|
|
var settings = new Settings();
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
|
|
|
|
|
|
|
|
|
settings.ConfigurationMode = ConfigurationMode.ConfigureClient;
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
repository.Setup(r => r.ConfigureClientWith(It.IsAny<Uri>(), It.IsAny<PasswordParameters>())).Returns(SaveStatus.Success);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is ConfigurationCompletedEventArgs c)
|
|
|
|
|
{
|
|
|
|
|
c.AbortStartup = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotAllowToAbortIfNotInConfigureClientMode()
|
|
|
|
|
{
|
|
|
|
|
var settings = new Settings();
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
settings.ConfigurationMode = ConfigurationMode.Exam;
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is ConfigurationCompletedEventArgs c)
|
|
|
|
|
{
|
|
|
|
|
Assert.Fail();
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailWithoutCommandLineArgs()
|
|
|
|
|
{
|
|
|
|
|
var actualSettings = default(Settings);
|
|
|
|
|
var defaultSettings = new Settings();
|
|
|
|
|
var result = OperationResult.Failed;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Setup(r => r.LoadDefaultSettings()).Returns(defaultSettings);
|
|
|
|
|
nextSession.SetupSet<Settings>(s => s.Settings = It.IsAny<Settings>()).Callback(s => actualSettings = s);
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
result = sut.Perform();
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Verify(r => r.LoadDefaultSettings(), Times.Once);
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
Assert.AreSame(defaultSettings, actualSettings);
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut = new ConfigurationOperation(new string[] { }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
|
|
|
|
result = sut.Perform();
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Verify(r => r.LoadDefaultSettings(), Times.Exactly(2));
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
Assert.AreSame(defaultSettings, actualSettings);
|
|
|
|
|
}
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailWithInvalidUri()
|
|
|
|
|
{
|
|
|
|
|
var uri = @"an/invalid\uri.'*%yolo/()你好";
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", uri }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustOnlyAllowToEnterAdminPasswordFiveTimes()
|
|
|
|
|
{
|
|
|
|
|
var count = 0;
|
|
|
|
|
var localSettings = new Settings { AdminPasswordHash = "1234" };
|
|
|
|
|
var settings = new Settings { AdminPasswordHash = "9876", ConfigurationMode = ConfigurationMode.ConfigureClient };
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
|
|
|
|
|
|
|
|
|
appConfig.AppDataFolder = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations));
|
|
|
|
|
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.LocalPath.Contains("SettingsDummy")), out localSettings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
nextSession.SetupGet(s => s.Settings).Returns(settings);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is PasswordRequiredEventArgs p && p.Purpose == PasswordRequestPurpose.LocalAdministrator)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
p.Success = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(5, count);
|
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
|
|
|
|
}
|
2018-10-03 14:35:27 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustOnlyAllowToEnterSettingsPasswordFiveTimes()
|
|
|
|
|
{
|
|
|
|
|
var count = 0;
|
|
|
|
|
var settings = default(Settings);
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.PasswordNeeded);
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is PasswordRequiredEventArgs p && p.Purpose == PasswordRequestPurpose.Settings)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
p.Success = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>()), Times.Exactly(6));
|
2018-10-03 14:35:27 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
Assert.AreEqual(5, count);
|
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
|
|
|
|
}
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustSucceedIfAdminPasswordCorrect()
|
|
|
|
|
{
|
|
|
|
|
var password = "test";
|
|
|
|
|
var currentSettings = new Settings { AdminPasswordHash = "1234", ConfigurationMode = ConfigurationMode.ConfigureClient };
|
|
|
|
|
var nextSettings = new Settings { AdminPasswordHash = "9876", ConfigurationMode = ConfigurationMode.ConfigureClient };
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
|
|
|
|
|
|
|
|
|
appConfig.AppDataFolder = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations));
|
|
|
|
|
nextSession.SetupGet(s => s.Settings).Returns(nextSettings);
|
|
|
|
|
|
|
|
|
|
hashAlgorithm.Setup(h => h.GenerateHashFor(It.Is<string>(p => p == password))).Returns(currentSettings.AdminPasswordHash);
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out currentSettings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.AbsoluteUri == url), out nextSettings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
repository.Setup(r => r.ConfigureClientWith(It.IsAny<Uri>(), It.IsAny<PasswordParameters>())).Returns(SaveStatus.Success);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is PasswordRequiredEventArgs p && p.Purpose == PasswordRequestPurpose.LocalAdministrator)
|
|
|
|
|
{
|
|
|
|
|
p.Password = password;
|
|
|
|
|
p.Success = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
|
|
|
|
|
repository.Verify(r => r.ConfigureClientWith(It.IsAny<Uri>(), It.IsAny<PasswordParameters>()), Times.Once);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustSucceedIfSettingsPasswordCorrect()
|
|
|
|
|
{
|
|
|
|
|
var password = "test";
|
|
|
|
|
var settings = new Settings { ConfigurationMode = ConfigurationMode.Exam };
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.PasswordNeeded);
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.Is<PasswordParameters>(p => p.Password == password))).Returns(LoadStatus.Success);
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is PasswordRequiredEventArgs p)
|
|
|
|
|
{
|
|
|
|
|
p.Password = password;
|
|
|
|
|
p.Success = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.Is<PasswordParameters>(p => p.Password == password)), Times.AtLeastOnce);
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustAbortAskingForAdminPasswordIfDecidedByUser()
|
|
|
|
|
{
|
|
|
|
|
var settings = default(Settings);
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.PasswordNeeded);
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is PasswordRequiredEventArgs p)
|
|
|
|
|
{
|
|
|
|
|
p.Success = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Perform();
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Aborted, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustAbortAskingForSettingsPasswordIfDecidedByUser()
|
|
|
|
|
{
|
|
|
|
|
var settings = default(Settings);
|
|
|
|
|
var url = @"http://www.safeexambrowser.org/whatever.seb";
|
|
|
|
|
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.PasswordNeeded);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(new[] { "blubb.exe", url }, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sut.ActionRequired += args =>
|
|
|
|
|
{
|
|
|
|
|
if (args is PasswordRequiredEventArgs p)
|
|
|
|
|
{
|
|
|
|
|
p.Success = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = sut.Perform();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(OperationResult.Aborted, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustReconfigureForExamWithCorrectUri()
|
|
|
|
|
{
|
|
|
|
|
var currentSettings = new Settings();
|
|
|
|
|
var location = Path.GetDirectoryName(GetType().Assembly.Location);
|
|
|
|
|
var resource = new Uri(Path.Combine(location, nameof(Operations), "SettingsDummy.txt"));
|
|
|
|
|
var settings = new Settings { ConfigurationMode = ConfigurationMode.Exam };
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
currentSession.SetupGet(s => s.Settings).Returns(currentSettings);
|
|
|
|
|
sessionContext.ReconfigurationFilePath = resource.LocalPath;
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Repeat();
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
nextSession.VerifySet(s => s.Settings = settings, Times.Once);
|
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>()), Times.AtLeastOnce);
|
|
|
|
|
repository.Verify(r => r.ConfigureClientWith(It.Is<Uri>(u => u.Equals(resource)), It.IsAny<PasswordParameters>()), Times.Never);
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustReconfigureForClientConfigurationWithCorrectUri()
|
|
|
|
|
{
|
|
|
|
|
var currentSettings = new Settings();
|
|
|
|
|
var location = Path.GetDirectoryName(GetType().Assembly.Location);
|
|
|
|
|
var resource = new Uri(Path.Combine(location, nameof(Operations), "SettingsDummy.txt"));
|
|
|
|
|
var settings = new Settings { ConfigurationMode = ConfigurationMode.ConfigureClient };
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
currentSession.SetupGet(s => s.Settings).Returns(currentSettings);
|
|
|
|
|
sessionContext.ReconfigurationFilePath = resource.LocalPath;
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
repository.Setup(r => r.ConfigureClientWith(It.Is<Uri>(u => u.Equals(resource)), It.IsAny<PasswordParameters>())).Returns(SaveStatus.Success);
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Repeat();
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
nextSession.VerifySet(s => s.Settings = settings, Times.Once);
|
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>()), Times.AtLeastOnce);
|
|
|
|
|
repository.Verify(r => r.ConfigureClientWith(It.Is<Uri>(u => u.Equals(resource)), It.IsAny<PasswordParameters>()), Times.Once);
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustFailToReconfigureWithInvalidUri()
|
|
|
|
|
{
|
|
|
|
|
var resource = new Uri("file:///C:/does/not/exist.txt");
|
|
|
|
|
var settings = default(Settings);
|
|
|
|
|
|
|
|
|
|
sessionContext.ReconfigurationFilePath = null;
|
|
|
|
|
repository.Setup(r => r.TryLoadSettings(It.IsAny<Uri>(), out settings, It.IsAny<PasswordParameters>())).Returns(LoadStatus.Success);
|
|
|
|
|
|
2019-02-22 14:50:46 +01:00
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
2019-02-14 12:06:35 +01:00
|
|
|
|
var result = sut.Repeat();
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>()), Times.Never);
|
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
2018-06-28 13:40:30 +02:00
|
|
|
|
|
2019-02-14 12:06:35 +01:00
|
|
|
|
sessionContext.ReconfigurationFilePath = resource.LocalPath;
|
|
|
|
|
result = sut.Repeat();
|
|
|
|
|
|
|
|
|
|
repository.Verify(r => r.TryLoadSettings(It.Is<Uri>(u => u.Equals(resource)), out settings, It.IsAny<PasswordParameters>()), Times.Never);
|
|
|
|
|
Assert.AreEqual(OperationResult.Failed, result);
|
|
|
|
|
}
|
2019-02-22 14:50:46 +01:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustDoNothingOnRevert()
|
|
|
|
|
{
|
|
|
|
|
var sut = new ConfigurationOperation(null, repository.Object, hashAlgorithm.Object, logger.Object, sessionContext);
|
|
|
|
|
var result = sut.Revert();
|
|
|
|
|
|
|
|
|
|
currentSession.VerifyNoOtherCalls();
|
|
|
|
|
hashAlgorithm.VerifyNoOtherCalls();
|
|
|
|
|
nextSession.VerifyNoOtherCalls();
|
|
|
|
|
repository.VerifyNoOtherCalls();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(OperationResult.Success, result);
|
|
|
|
|
}
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|