diff --git a/SafeExamBrowser.Configuration.UnitTests/Cryptography/KeyGeneratorTests.cs b/SafeExamBrowser.Configuration.UnitTests/Cryptography/KeyGeneratorTests.cs new file mode 100644 index 00000000..1fb5c1f9 --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/Cryptography/KeyGeneratorTests.cs @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2024 ETH Zürich, IT Services + * + * 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.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Configuration.Contracts; +using SafeExamBrowser.Configuration.Contracts.Integrity; +using SafeExamBrowser.Configuration.Cryptography; +using SafeExamBrowser.Logging.Contracts; + +namespace SafeExamBrowser.Configuration.UnitTests.Cryptography +{ + [TestClass] + public class KeyGeneratorTests + { + private AppConfig appConfig; + private Mock integrityModule; + private Mock logger; + private KeyGenerator sut; + + [TestInitialize] + public void Initialize() + { + appConfig = new AppConfig(); + integrityModule = new Mock(); + logger = new Mock(); + + sut = new KeyGenerator(appConfig, integrityModule.Object, logger.Object); + } + + [TestMethod] + [ExpectedException(typeof(Exception), AllowDerivedTypes = true)] + public void CalculateBrowserExamKeyHash_MustFailWithoutUrl() + { + sut.CalculateBrowserExamKeyHash(default, default, default); + } + + [TestMethod] + [ExpectedException(typeof(Exception), AllowDerivedTypes = true)] + public void CalculateConfigurationKeyHash_MustFailWithoutUrl() + { + sut.CalculateConfigurationKeyHash(default, default); + } + + [TestMethod] + public void MustAllowForConcurrentKeyHashCalculation() + { + Parallel.For(0, 1000, (_) => + { + sut.CalculateBrowserExamKeyHash(default, default, "https://www.safeexambrowser.org"); + sut.CalculateConfigurationKeyHash(default, "https://www.safeexambrowser.org"); + }); + } + } +} diff --git a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj index 63c30d63..ae924d13 100644 --- a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj +++ b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj @@ -148,6 +148,7 @@ +