From 693829f6416b6df39b39c30b1e7e4cf7b7d51f52 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Tue, 4 Sep 2018 10:58:56 +0200 Subject: [PATCH] SEBWIN-220: Extended unit test coverage for SafeExamBrowser.Configuration and SafeExamBrowser.Runtime. --- .../ConfigurationRepositoryTests.cs | 157 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 20 +++ ...ExamBrowser.Configuration.UnitTests.csproj | 113 +++++++++++++ .../app.config | 15 ++ .../packages.config | 10 ++ .../ConfigurationRepository.cs | 35 ++-- .../Operations/ClientOperationTests.cs | 10 +- .../ClientTerminationOperationTests.cs | 66 +++++++- .../Operations/ConfigurationOperationTests.cs | 12 +- .../{ => Operations}/SettingsDummy.txt | 0 .../{ => Operations}/WRONG/SettingsDummy.txt | 0 .../SafeExamBrowser.Runtime.UnitTests.csproj | 4 +- SafeExamBrowser.Runtime/CompositionRoot.cs | 13 +- .../Operations/ClientOperation.cs | 21 ++- .../SessionInitializationOperation.cs | 4 +- SafeExamBrowser.sln | 10 ++ appveyor.yml | 1 + 17 files changed, 451 insertions(+), 40 deletions(-) create mode 100644 SafeExamBrowser.Configuration.UnitTests/ConfigurationRepositoryTests.cs create mode 100644 SafeExamBrowser.Configuration.UnitTests/Properties/AssemblyInfo.cs create mode 100644 SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj create mode 100644 SafeExamBrowser.Configuration.UnitTests/app.config create mode 100644 SafeExamBrowser.Configuration.UnitTests/packages.config rename SafeExamBrowser.Runtime.UnitTests/{ => Operations}/SettingsDummy.txt (100%) rename SafeExamBrowser.Runtime.UnitTests/{ => Operations}/WRONG/SettingsDummy.txt (100%) diff --git a/SafeExamBrowser.Configuration.UnitTests/ConfigurationRepositoryTests.cs b/SafeExamBrowser.Configuration.UnitTests/ConfigurationRepositoryTests.cs new file mode 100644 index 00000000..1010b693 --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/ConfigurationRepositoryTests.cs @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2018 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.Reflection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Contracts.Communication.Proxies; +using SafeExamBrowser.Contracts.WindowsApi; + +namespace SafeExamBrowser.Configuration.UnitTests +{ + [TestClass] + public class ConfigurationRepositoryTests + { + private ConfigurationRepository sut; + + [TestInitialize] + public void Initialize() + { + var executablePath = Assembly.GetExecutingAssembly().Location; + + sut = new ConfigurationRepository(executablePath, string.Empty, string.Empty, string.Empty); + } + + [TestMethod] + public void AppConfigMustNeverBeNull() + { + Assert.IsNotNull(sut.AppConfig); + } + + [TestMethod] + public void CurrentSessionIsInitiallyNull() + { + Assert.IsNull(sut.CurrentSession); + } + + [TestMethod] + public void CurrentSettingsAreInitiallyNull() + { + Assert.IsNull(sut.CurrentSettings); + } + + [TestMethod] + public void MustCorrectlyBuildClientConfiguration() + { + sut.LoadDefaultSettings(); + sut.InitializeSessionConfiguration(); + + var appConfig = sut.AppConfig; + var clientConfig = sut.BuildClientConfiguration(); + var session = sut.CurrentSession; + var settings = sut.CurrentSettings; + + Assert.AreEqual(session.Id, clientConfig.SessionId); + Assert.AreSame(appConfig, clientConfig.AppConfig); + Assert.AreSame(settings, clientConfig.Settings); + } + + [TestMethod] + public void MustCorrectlyInitializeSessionConfiguration() + { + sut.InitializeSessionConfiguration(); + + Assert.IsNull(sut.CurrentSession.ClientProcess); + Assert.IsNull(sut.CurrentSession.ClientProxy); + Assert.IsInstanceOfType(sut.CurrentSession.Id, typeof(Guid)); + Assert.IsInstanceOfType(sut.CurrentSession.StartupToken, typeof(Guid)); + } + + [TestMethod] + public void MustCorrectlyUpdateAppConfig() + { + var clientAddress = sut.AppConfig.ClientAddress; + var clientId = sut.AppConfig.ClientId; + var clientLogFile = sut.AppConfig.ClientLogFile; + var runtimeAddress = sut.AppConfig.RuntimeAddress; + var runtimeId = sut.AppConfig.RuntimeId; + var runtimeLogFile = sut.AppConfig.RuntimeLogFile; + + sut.InitializeSessionConfiguration(); + + Assert.AreNotEqual(sut.AppConfig.ClientAddress, clientAddress); + Assert.AreNotEqual(sut.AppConfig.ClientId, clientId); + Assert.AreEqual(sut.AppConfig.ClientLogFile, clientLogFile); + Assert.AreEqual(sut.AppConfig.RuntimeAddress, runtimeAddress); + Assert.AreEqual(sut.AppConfig.RuntimeId, runtimeId); + Assert.AreEqual(sut.AppConfig.RuntimeLogFile, runtimeLogFile); + } + + [TestMethod] + public void MustCorrectlyUpdateSessionConfiguration() + { + var process = new Mock(); + var proxy = new Mock(); + + sut.InitializeSessionConfiguration(); + + var firstSession = sut.CurrentSession; + + sut.CurrentSession.ClientProcess = process.Object; + sut.CurrentSession.ClientProxy = proxy.Object; + sut.InitializeSessionConfiguration(); + + var secondSession = sut.CurrentSession; + + Assert.AreSame(firstSession.ClientProcess, secondSession.ClientProcess); + Assert.AreSame(firstSession.ClientProxy, secondSession.ClientProxy); + Assert.AreNotEqual(firstSession.Id, secondSession.Id); + Assert.AreNotEqual(firstSession.StartupToken, secondSession.StartupToken); + + sut.CurrentSession.ClientProcess = null; + sut.CurrentSession.ClientProxy = null; + sut.InitializeSessionConfiguration(); + + var thirdSession = sut.CurrentSession; + + Assert.IsNull(thirdSession.ClientProcess); + Assert.IsNull(thirdSession.ClientProxy); + Assert.AreNotEqual(secondSession.Id, thirdSession.Id); + Assert.AreNotEqual(secondSession.StartupToken, thirdSession.StartupToken); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void MustNotAllowNullForExecutablePath() + { + new ConfigurationRepository(null, null, null, null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void MustNotAllowNullForProgramCopyright() + { + new ConfigurationRepository(string.Empty, null, null, null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void MustNotAllowNullForProgramTitle() + { + new ConfigurationRepository(string.Empty, string.Empty, null, null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void MustNotAllowNullForProgramVersion() + { + new ConfigurationRepository(string.Empty, string.Empty, string.Empty, null); + } + } +} diff --git a/SafeExamBrowser.Configuration.UnitTests/Properties/AssemblyInfo.cs b/SafeExamBrowser.Configuration.UnitTests/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..367f3f81 --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("SafeExamBrowser.Configuration.UnitTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SafeExamBrowser.Configuration.UnitTests")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("9cdd03e7-ed65-409f-8c07-bd6f633a0170")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj new file mode 100644 index 00000000..9cc3511f --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj @@ -0,0 +1,113 @@ + + + + + + Debug + AnyCPU + {9CDD03E7-ED65-409F-8C07-BD6F633A0170} + Library + Properties + SafeExamBrowser.Configuration.UnitTests + SafeExamBrowser.Configuration.UnitTests + v4.5.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + bin\x86\Debug\ + DEBUG;TRACE + full + x86 + prompt + MinimumRecommendedRules.ruleset + + + bin\x86\Release\ + TRACE + true + pdbonly + x86 + prompt + MinimumRecommendedRules.ruleset + + + + ..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + ..\packages\Moq.4.9.0\lib\net45\Moq.dll + + + + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.1\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\netstandard1.0\System.ValueTuple.dll + + + + + + + + + + + + + {c388c4dd-a159-457d-af92-89f7ad185109} + SafeExamBrowser.Configuration + + + {47da5933-bef8-4729-94e6-abde2db12262} + SafeExamBrowser.Contracts + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/SafeExamBrowser.Configuration.UnitTests/app.config b/SafeExamBrowser.Configuration.UnitTests/app.config new file mode 100644 index 00000000..3fd21543 --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/app.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SafeExamBrowser.Configuration.UnitTests/packages.config b/SafeExamBrowser.Configuration.UnitTests/packages.config new file mode 100644 index 00000000..81653486 --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/packages.config @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/SafeExamBrowser.Configuration/ConfigurationRepository.cs b/SafeExamBrowser.Configuration/ConfigurationRepository.cs index 6c3bc618..af18ef18 100644 --- a/SafeExamBrowser.Configuration/ConfigurationRepository.cs +++ b/SafeExamBrowser.Configuration/ConfigurationRepository.cs @@ -8,7 +8,6 @@ using System; using System.IO; -using System.Reflection; using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.Logging; @@ -19,7 +18,11 @@ namespace SafeExamBrowser.Configuration { private const string BASE_ADDRESS = "net.pipe://localhost/safeexambrowser"; - private bool firstSession = true; + private readonly string executablePath; + private readonly string programCopyright; + private readonly string programTitle; + private readonly string programVersion; + private AppConfig appConfig; public ISessionData CurrentSession { get; private set; } @@ -39,6 +42,14 @@ namespace SafeExamBrowser.Configuration } } + public ConfigurationRepository(string executablePath, string programCopyright, string programTitle, string programVersion) + { + this.executablePath = executablePath ?? throw new ArgumentNullException(nameof(executablePath)); + this.programCopyright = programCopyright ?? throw new ArgumentNullException(nameof(programCopyright)); + this.programTitle = programTitle ?? throw new ArgumentNullException(nameof(programTitle)); + this.programVersion = programVersion ?? throw new ArgumentNullException(nameof(programVersion)); + } + public ClientConfiguration BuildClientConfiguration() { return new ClientConfiguration @@ -53,18 +64,13 @@ namespace SafeExamBrowser.Configuration { CurrentSession = new SessionData { + ClientProcess = CurrentSession?.ClientProcess, + ClientProxy = CurrentSession?.ClientProxy, Id = Guid.NewGuid(), StartupToken = Guid.NewGuid() }; - if (!firstSession) - { - UpdateAppConfig(); - } - else - { - firstSession = false; - } + UpdateAppConfig(); } public LoadStatus LoadSettings(Uri resource, string settingsPassword = null, string adminPassword = null) @@ -101,7 +107,6 @@ namespace SafeExamBrowser.Configuration private void InitializeAppConfig() { var appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(SafeExamBrowser)); - var executable = Assembly.GetEntryAssembly(); var startTime = DateTime.Now; var logFolder = Path.Combine(appDataFolder, "Logs"); var logFilePrefix = startTime.ToString("yyyy-MM-dd\\_HH\\hmm\\mss\\s"); @@ -113,16 +118,16 @@ namespace SafeExamBrowser.Configuration appConfig.BrowserLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Browser.log"); appConfig.ClientId = Guid.NewGuid(); appConfig.ClientAddress = $"{BASE_ADDRESS}/client/{Guid.NewGuid()}"; - appConfig.ClientExecutablePath = Path.Combine(Path.GetDirectoryName(executable.Location), $"{nameof(SafeExamBrowser)}.Client.exe"); + appConfig.ClientExecutablePath = Path.Combine(Path.GetDirectoryName(executablePath), $"{nameof(SafeExamBrowser)}.Client.exe"); appConfig.ClientLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Client.log"); appConfig.ConfigurationFileExtension = ".seb"; appConfig.DefaultSettingsFileName = "SebClientSettings.seb"; appConfig.DownloadDirectory = Path.Combine(appDataFolder, "Downloads"); appConfig.LogLevel = LogLevel.Debug; - appConfig.ProgramCopyright = executable.GetCustomAttribute().Copyright; + appConfig.ProgramCopyright = programCopyright; appConfig.ProgramDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), nameof(SafeExamBrowser)); - appConfig.ProgramTitle = executable.GetCustomAttribute().Title; - appConfig.ProgramVersion = executable.GetCustomAttribute().InformationalVersion; + appConfig.ProgramTitle = programTitle; + appConfig.ProgramVersion = programVersion; appConfig.RuntimeId = Guid.NewGuid(); appConfig.RuntimeAddress = $"{BASE_ADDRESS}/runtime/{Guid.NewGuid()}"; appConfig.RuntimeLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Runtime.log"); diff --git a/SafeExamBrowser.Runtime.UnitTests/Operations/ClientOperationTests.cs b/SafeExamBrowser.Runtime.UnitTests/Operations/ClientOperationTests.cs index 20968778..f2eb43b0 100644 --- a/SafeExamBrowser.Runtime.UnitTests/Operations/ClientOperationTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/Operations/ClientOperationTests.cs @@ -58,6 +58,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations configuration.SetupGet(c => c.CurrentSession).Returns(session.Object); configuration.SetupGet(c => c.AppConfig).Returns(appConfig); proxyFactory.Setup(f => f.CreateClientProxy(It.IsAny())).Returns(proxy.Object); + session.SetupGet(s => s.ClientProcess).Returns(process.Object); + session.SetupGet(s => s.ClientProxy).Returns(proxy.Object); sut = new ClientOperation(configuration.Object, logger.Object, processFactory.Object, proxyFactory.Object, runtimeHost.Object, 0); } @@ -125,8 +127,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations result = sut.Perform(); - session.VerifySet(s => s.ClientProcess = process.Object, Times.Never); - session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Never); + session.VerifySet(s => s.ClientProcess = process.Object, Times.Once); + session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Once); Assert.AreEqual(OperationResult.Failed, result); } @@ -145,8 +147,8 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations result = sut.Perform(); - session.VerifySet(s => s.ClientProcess = process.Object, Times.Never); - session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Never); + session.VerifySet(s => s.ClientProcess = process.Object, Times.Once); + session.VerifySet(s => s.ClientProxy = proxy.Object, Times.Once); Assert.AreEqual(OperationResult.Failed, result); } diff --git a/SafeExamBrowser.Runtime.UnitTests/Operations/ClientTerminationOperationTests.cs b/SafeExamBrowser.Runtime.UnitTests/Operations/ClientTerminationOperationTests.cs index 728c5abb..ba2dc4a9 100644 --- a/SafeExamBrowser.Runtime.UnitTests/Operations/ClientTerminationOperationTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/Operations/ClientTerminationOperationTests.cs @@ -12,6 +12,7 @@ using Moq; using SafeExamBrowser.Contracts.Communication.Hosts; using SafeExamBrowser.Contracts.Communication.Proxies; using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.WindowsApi; using SafeExamBrowser.Runtime.Operations; @@ -50,6 +51,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations configuration.SetupGet(c => c.CurrentSession).Returns(session.Object); configuration.SetupGet(c => c.AppConfig).Returns(appConfig); proxyFactory.Setup(f => f.CreateClientProxy(It.IsAny())).Returns(proxy.Object); + session.SetupGet(s => s.ClientProxy).Returns(proxy.Object); sut = new ClientTerminationOperation(configuration.Object, logger.Object, processFactory.Object, proxyFactory.Object, runtimeHost.Object, 0); } @@ -59,9 +61,11 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations { sut.Perform(); + process.VerifyNoOtherCalls(); processFactory.VerifyNoOtherCalls(); proxy.VerifyNoOtherCalls(); proxyFactory.VerifyNoOtherCalls(); + runtimeHost.VerifyNoOtherCalls(); } [TestMethod] @@ -70,15 +74,71 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations sut.Revert(); process.VerifyNoOtherCalls(); + processFactory.VerifyNoOtherCalls(); proxy.VerifyNoOtherCalls(); + proxyFactory.VerifyNoOtherCalls(); runtimeHost.VerifyNoOtherCalls(); } [TestMethod] - public void TODO() + public void MustTerminateClientOnRepeat() { - // TODO: MustStartClientOnRepeat -> Extract static fields from operation -> allows unit testing of this requirement etc.! - Assert.Fail(); + var terminated = new Action(() => + { + runtimeHost.Raise(h => h.ClientDisconnected += null); + process.Raise(p => p.Terminated += null, 0); + }); + + proxy.Setup(p => p.Disconnect()).Callback(terminated); + session.SetupGet(s => s.ClientProcess).Returns(process.Object); + + var result = sut.Repeat(); + + proxy.Verify(p => p.InitiateShutdown(), Times.Once); + proxy.Verify(p => p.Disconnect(), Times.Once); + process.Verify(p => p.Kill(), Times.Never); + session.VerifySet(s => s.ClientProcess = null, Times.Once); + session.VerifySet(s => s.ClientProxy = null, Times.Once); + + Assert.AreEqual(OperationResult.Success, result); + } + + [TestMethod] + public void MustDoNothingIfNoClientCreated() + { + session.SetupGet(s => s.ClientProcess).Returns(null as IProcess); + + var result = sut.Repeat(); + + session.VerifyGet(s => s.ClientProcess, Times.Once); + + process.VerifyNoOtherCalls(); + processFactory.VerifyNoOtherCalls(); + proxy.VerifyNoOtherCalls(); + proxyFactory.VerifyNoOtherCalls(); + runtimeHost.VerifyNoOtherCalls(); + + Assert.AreEqual(OperationResult.Success, result); + } + + [TestMethod] + public void MustDoNothingIfNoClientRunning() + { + process.SetupGet(p => p.HasTerminated).Returns(true); + session.SetupGet(s => s.ClientProcess).Returns(process.Object); + + var result = sut.Repeat(); + + process.VerifyGet(p => p.HasTerminated, Times.Once); + session.VerifyGet(s => s.ClientProcess, Times.Exactly(2)); + + process.VerifyNoOtherCalls(); + processFactory.VerifyNoOtherCalls(); + proxy.VerifyNoOtherCalls(); + proxyFactory.VerifyNoOtherCalls(); + runtimeHost.VerifyNoOtherCalls(); + + Assert.AreEqual(OperationResult.Success, result); } } } diff --git a/SafeExamBrowser.Runtime.UnitTests/Operations/ConfigurationOperationTests.cs b/SafeExamBrowser.Runtime.UnitTests/Operations/ConfigurationOperationTests.cs index a18bb79a..ca599c05 100644 --- a/SafeExamBrowser.Runtime.UnitTests/Operations/ConfigurationOperationTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/Operations/ConfigurationOperationTests.cs @@ -10,13 +10,13 @@ using System; using System.IO; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; -using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.Communication.Data; using SafeExamBrowser.Contracts.Communication.Events; using SafeExamBrowser.Contracts.Communication.Hosts; using SafeExamBrowser.Contracts.Communication.Proxies; using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.Configuration.Settings; +using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.UserInterface; @@ -68,7 +68,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations public void MustUseCommandLineArgumentAs1stPrio() { var url = @"http://www.safeexambrowser.org/whatever.seb"; - var location = Path.GetDirectoryName(GetType().Assembly.Location); + var location = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations)); appConfig.ProgramDataFolder = location; appConfig.AppDataFolder = location; @@ -86,7 +86,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations [TestMethod] public void MustUseProgramDataAs2ndPrio() { - var location = Path.GetDirectoryName(GetType().Assembly.Location); + var location = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations)); appConfig.ProgramDataFolder = location; appConfig.AppDataFolder = $@"{location}\WRONG"; @@ -104,7 +104,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations [TestMethod] public void MustUseAppDataAs3rdPrio() { - var location = Path.GetDirectoryName(GetType().Assembly.Location); + var location = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations)); appConfig.AppDataFolder = location; @@ -130,7 +130,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations [TestMethod] public void MustAbortIfWishedByUser() { - appConfig.ProgramDataFolder = Path.GetDirectoryName(GetType().Assembly.Location); + appConfig.ProgramDataFolder = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), nameof(Operations)); messageBox.Setup(m => m.Show(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(MessageBoxResult.Yes); repository.Setup(r => r.LoadSettings(It.IsAny(), null, null)).Returns(LoadStatus.Success); @@ -439,7 +439,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Operations public void MustReconfigureSuccessfullyWithCorrectUri() { var location = Path.GetDirectoryName(GetType().Assembly.Location); - var resource = new Uri(Path.Combine(location, "SettingsDummy.txt")); + var resource = new Uri(Path.Combine(location, nameof(Operations), "SettingsDummy.txt")); repository.SetupGet(r => r.ReconfigurationFilePath).Returns(resource.AbsolutePath); repository.Setup(r => r.LoadSettings(It.Is(u => u.Equals(resource)), null, null)).Returns(LoadStatus.Success); diff --git a/SafeExamBrowser.Runtime.UnitTests/SettingsDummy.txt b/SafeExamBrowser.Runtime.UnitTests/Operations/SettingsDummy.txt similarity index 100% rename from SafeExamBrowser.Runtime.UnitTests/SettingsDummy.txt rename to SafeExamBrowser.Runtime.UnitTests/Operations/SettingsDummy.txt diff --git a/SafeExamBrowser.Runtime.UnitTests/WRONG/SettingsDummy.txt b/SafeExamBrowser.Runtime.UnitTests/Operations/WRONG/SettingsDummy.txt similarity index 100% rename from SafeExamBrowser.Runtime.UnitTests/WRONG/SettingsDummy.txt rename to SafeExamBrowser.Runtime.UnitTests/Operations/WRONG/SettingsDummy.txt diff --git a/SafeExamBrowser.Runtime.UnitTests/SafeExamBrowser.Runtime.UnitTests.csproj b/SafeExamBrowser.Runtime.UnitTests/SafeExamBrowser.Runtime.UnitTests.csproj index ffb33672..bf1f637b 100644 --- a/SafeExamBrowser.Runtime.UnitTests/SafeExamBrowser.Runtime.UnitTests.csproj +++ b/SafeExamBrowser.Runtime.UnitTests/SafeExamBrowser.Runtime.UnitTests.csproj @@ -113,10 +113,10 @@ - + Always - + Always diff --git a/SafeExamBrowser.Runtime/CompositionRoot.cs b/SafeExamBrowser.Runtime/CompositionRoot.cs index 103fdd30..d69cd911 100644 --- a/SafeExamBrowser.Runtime/CompositionRoot.cs +++ b/SafeExamBrowser.Runtime/CompositionRoot.cs @@ -44,7 +44,7 @@ namespace SafeExamBrowser.Runtime const int TEN_SECONDS = 10000; var args = Environment.GetCommandLineArgs(); - var configuration = new ConfigurationRepository(); + var configuration = BuildConfigurationRepository(); var nativeMethods = new NativeMethods(); logger = new Logger(); @@ -101,6 +101,17 @@ namespace SafeExamBrowser.Runtime logger?.Log($"# Application terminated at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}"); } + private ConfigurationRepository BuildConfigurationRepository() + { + var executable = Assembly.GetExecutingAssembly(); + var programCopyright = executable.GetCustomAttribute().Copyright; + var programTitle = executable.GetCustomAttribute().Title; + var programVersion = executable.GetCustomAttribute().InformationalVersion; + var repository = new ConfigurationRepository(executable.Location, programCopyright, programTitle, programVersion); + + return repository; + } + private void InitializeLogging() { var logFileWriter = new LogFileWriter(new DefaultLogFormatter(), appConfig.RuntimeLogFile); diff --git a/SafeExamBrowser.Runtime/Operations/ClientOperation.cs b/SafeExamBrowser.Runtime/Operations/ClientOperation.cs index 939cb0b9..b7ad6154 100644 --- a/SafeExamBrowser.Runtime/Operations/ClientOperation.cs +++ b/SafeExamBrowser.Runtime/Operations/ClientOperation.cs @@ -30,10 +30,20 @@ namespace SafeExamBrowser.Runtime.Operations protected IProxyFactory proxyFactory; protected IRuntimeHost runtimeHost; - protected static IProcess ClientProcess { get; private set; } - protected static IClientProxy ClientProxy { get; private set; } public IProgressIndicator ProgressIndicator { protected get; set; } + protected IProcess ClientProcess + { + get { return configuration.CurrentSession.ClientProcess; } + set { configuration.CurrentSession.ClientProcess = value; } + } + + protected IClientProxy ClientProxy + { + get { return configuration.CurrentSession.ClientProxy; } + set { configuration.CurrentSession.ClientProxy = value; } + } + public ClientOperation( IConfigurationRepository configuration, ILogger logger, @@ -132,9 +142,6 @@ namespace SafeExamBrowser.Runtime.Operations logger.Info("Authentication of client has been successful, client is ready to operate."); - configuration.CurrentSession.ClientProcess = ClientProcess; - configuration.CurrentSession.ClientProxy = ClientProxy; - return true; } @@ -191,8 +198,8 @@ namespace SafeExamBrowser.Runtime.Operations if (success) { - configuration.CurrentSession.ClientProcess = null; - configuration.CurrentSession.ClientProxy = null; + ClientProcess = null; + ClientProxy = null; } return success; diff --git a/SafeExamBrowser.Runtime/Operations/SessionInitializationOperation.cs b/SafeExamBrowser.Runtime/Operations/SessionInitializationOperation.cs index 870a7812..c7525399 100644 --- a/SafeExamBrowser.Runtime/Operations/SessionInitializationOperation.cs +++ b/SafeExamBrowser.Runtime/Operations/SessionInitializationOperation.cs @@ -6,9 +6,9 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.Communication.Hosts; using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Core.OperationModel; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.UserInterface; @@ -58,7 +58,7 @@ namespace SafeExamBrowser.Runtime.Operations runtimeHost.StartupToken = configuration.CurrentSession.StartupToken; logger.Info($" -> Client-ID: {configuration.AppConfig.ClientId}"); - logger.Info($" -> Runtime-ID: {configuration.AppConfig.RuntimeId} (as reference, does not change)"); + logger.Info($" -> Runtime-ID: {configuration.AppConfig.RuntimeId}"); logger.Info($" -> Session-ID: {configuration.CurrentSession.Id}"); } } diff --git a/SafeExamBrowser.sln b/SafeExamBrowser.sln index c88c8876..fa17c343 100644 --- a/SafeExamBrowser.sln +++ b/SafeExamBrowser.sln @@ -54,6 +54,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SafeExamBrowser.Logging", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SafeExamBrowser.Logging.UnitTests", "SafeExamBrowser.Logging.UnitTests\SafeExamBrowser.Logging.UnitTests.csproj", "{7F012305-1125-47CE-9C2A-146C891C0924}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SafeExamBrowser.Configuration.UnitTests", "SafeExamBrowser.Configuration.UnitTests\SafeExamBrowser.Configuration.UnitTests.csproj", "{9CDD03E7-ED65-409F-8C07-BD6F633A0170}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -222,6 +224,14 @@ Global {7F012305-1125-47CE-9C2A-146C891C0924}.Release|Any CPU.Build.0 = Release|Any CPU {7F012305-1125-47CE-9C2A-146C891C0924}.Release|x86.ActiveCfg = Release|x86 {7F012305-1125-47CE-9C2A-146C891C0924}.Release|x86.Build.0 = Release|x86 + {9CDD03E7-ED65-409F-8C07-BD6F633A0170}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9CDD03E7-ED65-409F-8C07-BD6F633A0170}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9CDD03E7-ED65-409F-8C07-BD6F633A0170}.Debug|x86.ActiveCfg = Debug|x86 + {9CDD03E7-ED65-409F-8C07-BD6F633A0170}.Debug|x86.Build.0 = Debug|x86 + {9CDD03E7-ED65-409F-8C07-BD6F633A0170}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9CDD03E7-ED65-409F-8C07-BD6F633A0170}.Release|Any CPU.Build.0 = Release|Any CPU + {9CDD03E7-ED65-409F-8C07-BD6F633A0170}.Release|x86.ActiveCfg = Release|x86 + {9CDD03E7-ED65-409F-8C07-BD6F633A0170}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/appveyor.yml b/appveyor.yml index 1346b90b..729b2054 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -8,6 +8,7 @@ build_script: test_script: - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:user -target:"vstest.console.exe" -targetargs:"/logger:Appveyor SafeExamBrowser.Client.UnitTests\bin\%PLATFORM%\%CONFIGURATION%\SafeExamBrowser.Client.UnitTests.dll" -filter:"+[*]* -[*.UnitTests]*" -mergebyhash -output:"coverage.xml" - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:user -target:"vstest.console.exe" -targetargs:"/logger:Appveyor .\SafeExamBrowser.Communication.UnitTests\bin\%PLATFORM%\%CONFIGURATION%\SafeExamBrowser.Communication.UnitTests.dll" -filter:"+[*]* -[*.UnitTests]*" -mergebyhash -mergeoutput -output:"coverage.xml" + - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:user -target:"vstest.console.exe" -targetargs:"/logger:Appveyor .\SafeExamBrowser.Configuration.UnitTests\bin\%PLATFORM%\%CONFIGURATION%\SafeExamBrowser.Configuration.UnitTests.dll" -filter:"+[*]* -[*.UnitTests]*" -mergebyhash -mergeoutput -output:"coverage.xml" - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:user -target:"vstest.console.exe" -targetargs:"/logger:Appveyor .\SafeExamBrowser.Core.UnitTests\bin\%PLATFORM%\%CONFIGURATION%\SafeExamBrowser.Core.UnitTests.dll" -filter:"+[*]* -[*.UnitTests]*" -mergebyhash -mergeoutput -output:"coverage.xml" - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:user -target:"vstest.console.exe" -targetargs:"/logger:Appveyor .\SafeExamBrowser.I18n.UnitTests\bin\%PLATFORM%\%CONFIGURATION%\SafeExamBrowser.I18n.UnitTests.dll" -filter:"+[*]* -[*.UnitTests]*" -mergebyhash -mergeoutput -output:"coverage.xml" - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:user -target:"vstest.console.exe" -targetargs:"/logger:Appveyor .\SafeExamBrowser.Logging.UnitTests\bin\%PLATFORM%\%CONFIGURATION%\SafeExamBrowser.Logging.UnitTests.dll" -filter:"+[*]* -[*.UnitTests]*" -mergebyhash -mergeoutput -output:"coverage.xml"