SEBWIN-420: Ensured the temporary directory is always created for every new session.

This commit is contained in:
Damian Büchel 2020-08-27 20:10:15 +02:00
parent 867bcfe6a7
commit 68360a603d
5 changed files with 29 additions and 5 deletions

View file

@ -14,6 +14,7 @@ using SafeExamBrowser.Configuration.Contracts;
using SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Runtime.Operations;
using SafeExamBrowser.SystemComponents.Contracts;
namespace SafeExamBrowser.Runtime.UnitTests.Operations
{
@ -22,6 +23,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{
private AppConfig appConfig;
private Mock<IConfigurationRepository> configuration;
private Mock<IFileSystem> fileSystem;
private Mock<ILogger> logger;
private Mock<IRuntimeHost> runtimeHost;
private SessionConfiguration session;
@ -34,6 +36,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
{
appConfig = new AppConfig();
configuration = new Mock<IConfigurationRepository>();
fileSystem = new Mock<IFileSystem>();
logger = new Mock<ILogger>();
runtimeHost = new Mock<IRuntimeHost>();
session = new SessionConfiguration();
@ -43,47 +46,52 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations
session.AppConfig = appConfig;
sessionContext.Next = session;
sut = new SessionInitializationOperation(configuration.Object, logger.Object, runtimeHost.Object, sessionContext);
sut = new SessionInitializationOperation(configuration.Object, fileSystem.Object, logger.Object, runtimeHost.Object, sessionContext);
}
[TestMethod]
public void MustInitializeConfigurationOnPerform()
public void Perform_MustInitializeConfiguration()
{
var token = Guid.NewGuid();
appConfig.TemporaryDirectory = @"C:\Some\Random\Path";
session.ClientAuthenticationToken = token;
var result = sut.Perform();
configuration.Verify(c => c.InitializeSessionConfiguration(), Times.Once);
fileSystem.Verify(f => f.CreateDirectory(It.Is<string>(s => s == appConfig.TemporaryDirectory)), Times.Once);
Assert.AreEqual(OperationResult.Success, result);
Assert.IsNull(sessionContext.Current);
}
[TestMethod]
public void MustInitializeConfigurationOnRepeat()
public void Repeat_MustInitializeConfiguration()
{
var currentSession = new SessionConfiguration();
var token = Guid.NewGuid();
appConfig.TemporaryDirectory = @"C:\Some\Random\Path";
session.ClientAuthenticationToken = token;
sessionContext.Current = currentSession;
var result = sut.Repeat();
configuration.Verify(c => c.InitializeSessionConfiguration(), Times.Once);
fileSystem.Verify(f => f.CreateDirectory(It.Is<string>(s => s == appConfig.TemporaryDirectory)), Times.Once);
Assert.AreEqual(OperationResult.Success, result);
Assert.AreSame(currentSession,sessionContext.Current);
}
[TestMethod]
public void MustDoNothingOnRevert()
public void Revert_MustDoNothing()
{
var result = sut.Revert();
configuration.VerifyNoOtherCalls();
fileSystem.VerifyNoOtherCalls();
logger.VerifyNoOtherCalls();
runtimeHost.VerifyNoOtherCalls();

View file

@ -81,7 +81,7 @@ namespace SafeExamBrowser.Runtime
bootstrapOperations.Enqueue(new I18nOperation(logger, text));
bootstrapOperations.Enqueue(new CommunicationHostOperation(runtimeHost, logger));
sessionOperations.Enqueue(new SessionInitializationOperation(configuration, logger, runtimeHost, sessionContext));
sessionOperations.Enqueue(new SessionInitializationOperation(configuration, fileSystem, logger, runtimeHost, sessionContext));
sessionOperations.Enqueue(new ConfigurationOperation(args, configuration, new FileSystem(), new HashAlgorithm(), logger, sessionContext));
sessionOperations.Enqueue(new ServerOperation(args, configuration, fileSystem, logger, sessionContext, server));
sessionOperations.Enqueue(new VirtualMachineOperation(vmDetector, logger, sessionContext));

View file

@ -12,12 +12,14 @@ using SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.SystemComponents.Contracts;
namespace SafeExamBrowser.Runtime.Operations
{
internal class SessionInitializationOperation : SessionOperation
{
private IConfigurationRepository configuration;
private IFileSystem fileSystem;
private ILogger logger;
private IRuntimeHost runtimeHost;
@ -26,11 +28,13 @@ namespace SafeExamBrowser.Runtime.Operations
public SessionInitializationOperation(
IConfigurationRepository configuration,
IFileSystem fileSystem,
ILogger logger,
IRuntimeHost runtimeHost,
SessionContext sessionContext) : base(sessionContext)
{
this.configuration = configuration;
this.fileSystem = fileSystem;
this.logger = logger;
this.runtimeHost = runtimeHost;
}
@ -66,6 +70,8 @@ namespace SafeExamBrowser.Runtime.Operations
logger.Info($" -> Client-ID: {Context.Next.AppConfig.ClientId}");
logger.Info($" -> Runtime-ID: {Context.Next.AppConfig.RuntimeId}");
logger.Info($" -> Session-ID: {Context.Next.SessionId}");
fileSystem.CreateDirectory(Context.Next.AppConfig.TemporaryDirectory);
}
private void FinalizeSessionConfiguration()

View file

@ -13,6 +13,11 @@ namespace SafeExamBrowser.SystemComponents.Contracts
/// </summary>
public interface IFileSystem
{
/// <summary>
/// Creates all directories and subdirectories defined by the given path.
/// </summary>
void CreateDirectory(string path);
/// <summary>
/// Deletes the item at the given path, if it exists. Directories will be completely deleted, including all subdirectories and files.
/// </summary>

View file

@ -13,6 +13,11 @@ namespace SafeExamBrowser.SystemComponents
{
public class FileSystem : IFileSystem
{
public void CreateDirectory(string path)
{
Directory.CreateDirectory(path);
}
public void Delete(string path)
{
if (File.Exists(path))