SEBWIN-296: Discovered and fixed epic bug in SubStream & PasswordEncryption by implementing unit tests for the latter.
This commit is contained in:
parent
273e404f60
commit
86e494e611
5 changed files with 58 additions and 2 deletions
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2019 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.IO;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using Moq;
|
||||||
|
using SafeExamBrowser.Configuration.Cryptography;
|
||||||
|
using SafeExamBrowser.Contracts.Configuration;
|
||||||
|
using SafeExamBrowser.Contracts.Logging;
|
||||||
|
|
||||||
|
namespace SafeExamBrowser.Configuration.UnitTests.Cryptography
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class PasswordEncryptionTests
|
||||||
|
{
|
||||||
|
private Mock<ILogger> logger;
|
||||||
|
private PasswordEncryption sut;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
logger = new Mock<ILogger>();
|
||||||
|
sut = new PasswordEncryption(logger.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MustPerformCorrectly()
|
||||||
|
{
|
||||||
|
var password = "test1234";
|
||||||
|
var message = Encoding.UTF8.GetBytes("A super secret message!");
|
||||||
|
var saveStatus = sut.Encrypt(new MemoryStream(message), password, out var encrypted);
|
||||||
|
var loadStatus = sut.Decrypt(encrypted, password, out var decrypted);
|
||||||
|
var original = new MemoryStream(message);
|
||||||
|
|
||||||
|
decrypted.Seek(0, SeekOrigin.Begin);
|
||||||
|
original.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
while (original.Position < original.Length)
|
||||||
|
{
|
||||||
|
Assert.AreEqual(original.ReadByte(), decrypted.ReadByte());
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.AreEqual(SaveStatus.Success, saveStatus);
|
||||||
|
Assert.AreEqual(LoadStatus.Success, loadStatus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -85,6 +85,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="ConfigurationRepositoryTests.cs" />
|
<Compile Include="ConfigurationRepositoryTests.cs" />
|
||||||
<Compile Include="Cryptography\HashAlgorithmTests.cs" />
|
<Compile Include="Cryptography\HashAlgorithmTests.cs" />
|
||||||
|
<Compile Include="Cryptography\PasswordEncryptionTests.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -127,7 +127,7 @@ namespace SafeExamBrowser.Configuration.Cryptography
|
||||||
var hashStream = new SubStream(data, 0, data.Length - originalHmac.Length);
|
var hashStream = new SubStream(data, 0, data.Length - originalHmac.Length);
|
||||||
var computedHmac = algorithm.ComputeHash(hashStream);
|
var computedHmac = algorithm.ComputeHash(hashStream);
|
||||||
|
|
||||||
data.Seek(originalHmac.Length, SeekOrigin.End);
|
data.Seek(-originalHmac.Length, SeekOrigin.End);
|
||||||
data.Read(originalHmac, 0, originalHmac.Length);
|
data.Read(originalHmac, 0, originalHmac.Length);
|
||||||
|
|
||||||
return (originalHmac, computedHmac);
|
return (originalHmac, computedHmac);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
// General Information about an assembly is controlled through the following
|
// General Information about an assembly is controlled through the following
|
||||||
|
@ -14,6 +15,7 @@ using System.Runtime.InteropServices;
|
||||||
// to COM components. If you need to access a type in this assembly from
|
// to COM components. If you need to access a type in this assembly from
|
||||||
// COM, set the ComVisible attribute to true on that type.
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
[assembly: ComVisible(false)]
|
[assembly: ComVisible(false)]
|
||||||
|
[assembly: InternalsVisibleTo("SafeExamBrowser.Configuration.UnitTests")]
|
||||||
|
|
||||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
[assembly: Guid("c388c4dd-a159-457d-af92-89f7ad185109")]
|
[assembly: Guid("c388c4dd-a159-457d-af92-89f7ad185109")]
|
||||||
|
|
|
@ -119,7 +119,7 @@ namespace SafeExamBrowser.Configuration
|
||||||
Position += offset;
|
Position += offset;
|
||||||
break;
|
break;
|
||||||
case SeekOrigin.End:
|
case SeekOrigin.End:
|
||||||
Position = length - offset;
|
Position = length + offset;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new NotImplementedException($"Seeking from position '{origin}' is not implemented!");
|
throw new NotImplementedException($"Seeking from position '{origin}' is not implemented!");
|
||||||
|
|
Loading…
Reference in a new issue