From 68360a603ddfcd018cb6d258b810968714698e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Thu, 27 Aug 2020 20:10:15 +0200 Subject: [PATCH] SEBWIN-420: Ensured the temporary directory is always created for every new session. --- .../SessionInitializationOperationTests.cs | 16 ++++++++++++---- SafeExamBrowser.Runtime/CompositionRoot.cs | 2 +- .../Operations/SessionInitializationOperation.cs | 6 ++++++ .../IFileSystem.cs | 5 +++++ SafeExamBrowser.SystemComponents/FileSystem.cs | 5 +++++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/SafeExamBrowser.Runtime.UnitTests/Operations/SessionInitializationOperationTests.cs b/SafeExamBrowser.Runtime.UnitTests/Operations/SessionInitializationOperationTests.cs index 68d6b940..5ef8df5b 100644 --- a/SafeExamBrowser.Runtime.UnitTests/Operations/SessionInitializationOperationTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/Operations/SessionInitializationOperationTests.cs @@ -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 configuration; + private Mock fileSystem; private Mock logger; private Mock runtimeHost; private SessionConfiguration session; @@ -34,6 +36,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations { appConfig = new AppConfig(); configuration = new Mock(); + fileSystem = new Mock(); logger = new Mock(); runtimeHost = new Mock(); 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(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(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(); diff --git a/SafeExamBrowser.Runtime/CompositionRoot.cs b/SafeExamBrowser.Runtime/CompositionRoot.cs index 6ca5f035..ab4c5009 100644 --- a/SafeExamBrowser.Runtime/CompositionRoot.cs +++ b/SafeExamBrowser.Runtime/CompositionRoot.cs @@ -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)); diff --git a/SafeExamBrowser.Runtime/Operations/SessionInitializationOperation.cs b/SafeExamBrowser.Runtime/Operations/SessionInitializationOperation.cs index 19b86d66..f39227c0 100644 --- a/SafeExamBrowser.Runtime/Operations/SessionInitializationOperation.cs +++ b/SafeExamBrowser.Runtime/Operations/SessionInitializationOperation.cs @@ -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() diff --git a/SafeExamBrowser.SystemComponents.Contracts/IFileSystem.cs b/SafeExamBrowser.SystemComponents.Contracts/IFileSystem.cs index beeaab39..f7159799 100644 --- a/SafeExamBrowser.SystemComponents.Contracts/IFileSystem.cs +++ b/SafeExamBrowser.SystemComponents.Contracts/IFileSystem.cs @@ -13,6 +13,11 @@ namespace SafeExamBrowser.SystemComponents.Contracts /// public interface IFileSystem { + /// + /// Creates all directories and subdirectories defined by the given path. + /// + void CreateDirectory(string path); + /// /// Deletes the item at the given path, if it exists. Directories will be completely deleted, including all subdirectories and files. /// diff --git a/SafeExamBrowser.SystemComponents/FileSystem.cs b/SafeExamBrowser.SystemComponents/FileSystem.cs index 8b00021d..104ed83b 100644 --- a/SafeExamBrowser.SystemComponents/FileSystem.cs +++ b/SafeExamBrowser.SystemComponents/FileSystem.cs @@ -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))