diff --git a/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs b/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs index 477810d5..bafa5a09 100644 --- a/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/ResourceHandler.cs @@ -107,7 +107,7 @@ namespace SafeExamBrowser.Browser.Handlers var headers = new NameValueCollection(request.Headers); var urlWithoutFragment = request.Url.Split('#')[0]; var hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(urlWithoutFragment + settings.HashValue)); - var configurationKey = BitConverter.ToString(hash).Replace("-", string.Empty); + var configurationKey = BitConverter.ToString(hash).ToLower().Replace("-", string.Empty); // TODO: Implement Browser Exam Key calculation. // headers["X-SafeExamBrowser-RequestHash"] = ...; diff --git a/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/DataProcessorTests.cs b/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/DataProcessorTests.cs new file mode 100644 index 00000000..e06ca6ae --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/DataProcessorTests.cs @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2020 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.Collections.Generic; +using System.Reflection; +using System.Runtime.Serialization.Formatters.Binary; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SafeExamBrowser.Configuration.ConfigurationData; +using SafeExamBrowser.Settings; + +namespace SafeExamBrowser.Configuration.UnitTests.ConfigurationData +{ + [TestClass] + public class DataProcessorTests + { + private DataProcessor sut; + + [TestInitialize] + public void Initialize() + { + sut = new DataProcessor(); + } + + [TestMethod] + public void MustCalculateCorrectHashValue() + { + var formatter = new BinaryFormatter(); + var path1 = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(ConfigurationData)}.TestDictionary1.bin"; + var path2 = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(ConfigurationData)}.TestDictionary2.bin"; + var path3 = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(ConfigurationData)}.TestDictionary3.bin"; + var stream1 = Assembly.GetAssembly(GetType()).GetManifestResourceStream(path1); + var stream2 = Assembly.GetAssembly(GetType()).GetManifestResourceStream(path2); + var stream3 = Assembly.GetAssembly(GetType()).GetManifestResourceStream(path3); + var data1 = formatter.Deserialize(stream1) as IDictionary; + var data2 = formatter.Deserialize(stream2) as IDictionary; + var data3 = formatter.Deserialize(stream3) as IDictionary; + var settings1 = new AppSettings(); + var settings2 = new AppSettings(); + var settings3 = new AppSettings(); + + sut.Process(data1, settings1); + sut.Process(data2, settings2); + sut.Process(data3, settings3); + + Assert.AreEqual("6063c3351ed1ac878c05072598d5079e30ca763c957d8e04bd45131c08f88d1a", settings1.Browser.HashValue); + Assert.AreEqual("4fc002d2ae4faf994a14bede54d95ac58a1a2cb9b59bc5b4277ff29559b46e3d", settings2.Browser.HashValue); + Assert.AreEqual("ab426e25b795c917f1fb40f7ef8e5757ef97d7c7ad6792e655c4421d47329d7a", settings3.Browser.HashValue); + } + } +} diff --git a/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary1.bin b/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary1.bin new file mode 100644 index 00000000..a95d7e5a Binary files /dev/null and b/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary1.bin differ diff --git a/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary2.bin b/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary2.bin new file mode 100644 index 00000000..5c4d3a23 Binary files /dev/null and b/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary2.bin differ diff --git a/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary3.bin b/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary3.bin new file mode 100644 index 00000000..d65333fb Binary files /dev/null and b/SafeExamBrowser.Configuration.UnitTests/ConfigurationData/TestDictionary3.bin differ diff --git a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj index 72b9a974..ad3a996a 100644 --- a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj +++ b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj @@ -87,6 +87,7 @@ + @@ -106,6 +107,9 @@ + + + Designer diff --git a/SafeExamBrowser.Configuration/ConfigurationData/DataProcessor.cs b/SafeExamBrowser.Configuration/ConfigurationData/DataProcessor.cs index b76c3b2d..1a4c0a6d 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/DataProcessor.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/DataProcessor.cs @@ -12,7 +12,6 @@ using System.Globalization; using System.IO; using System.Linq; using System.Security.Cryptography; -using System.Text; using SafeExamBrowser.Settings; namespace SafeExamBrowser.Configuration.ConfigurationData @@ -28,22 +27,23 @@ namespace SafeExamBrowser.Configuration.ConfigurationData { using (var algorithm = new SHA256Managed()) using (var stream = new MemoryStream()) - using (var writer = new StreamWriter(stream, Encoding.UTF8)) + using (var writer = new StreamWriter(stream)) { Serialize(rawData, writer); + writer.Flush(); stream.Seek(0, SeekOrigin.Begin); - var hash = algorithm.ComputeHash(stream); - var hashString = BitConverter.ToString(hash).Replace("-", string.Empty); + var hashBytes = algorithm.ComputeHash(stream); + var hashValue = BitConverter.ToString(hashBytes).ToLower().Replace("-", string.Empty); - settings.Browser.HashValue = hashString; + settings.Browser.HashValue = hashValue; } } private void Serialize(IDictionary dictionary, StreamWriter stream) { - var orderedByKey = dictionary.OrderBy(d => d.Key, StringComparer.OrdinalIgnoreCase).ToList(); + var orderedByKey = dictionary.OrderBy(d => d.Key, StringComparer.InvariantCulture).ToList(); stream.Write('{');