Skip to content

Commit

Permalink
Fix file access errors in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
poulad committed Sep 3, 2018
1 parent 40dd003 commit 2639d80
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
18 changes: 14 additions & 4 deletions test/UnitTests/Decryption/File Bytes Decryption Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ namespace UnitTests
/// <summary>
/// Tests for decrypting file byte arrays using <see cref="IDecrypter.DecryptFile"/> method
/// </summary>
public class FileBytesDecryptionTests
public class FileBytesDecryptionTests : IClassFixture<FileBytesDecryptionTests.Fixture>
{
public class Fixture
{
public Fixture() =>
FileDuplicator.CopyTestFiles(
("driver_license-selfie.jpg.enc", "bytes_dec1.driver_license-selfie.jpg.enc"),
("driver_license-selfie.jpg", "bytes_dec2.driver_license-selfie.jpg"),
("driver_license-selfie.jpg.enc", "bytes_dec3.driver_license-selfie.jpg.enc")
);
}

[Fact(DisplayName = "Should decrypt from file bytes")]
public async Task Should_Decrypt_From_Bytes()
{
Expand All @@ -27,7 +37,7 @@ public async Task Should_Decrypt_From_Bytes()

IDecrypter decrypter = new Decrypter();

byte[] encContent = await File.ReadAllBytesAsync("Files/driver_license-selfie.jpg.enc");
byte[] encContent = await File.ReadAllBytesAsync("Files/bytes_dec1.driver_license-selfie.jpg.enc");

byte[] content = decrypter.DecryptFile(
encContent,
Expand All @@ -48,7 +58,7 @@ public async Task Should_Throw_Decrypting_Unencrypted_File_Invalid_Length()
};

IDecrypter decrypter = new Decrypter();
byte[] encContent = await File.ReadAllBytesAsync("Files/driver_license-selfie.jpg");
byte[] encContent = await File.ReadAllBytesAsync("Files/bytes_dec2.driver_license-selfie.jpg");

Exception exception = Assert.ThrowsAny<Exception>(() =>
decrypter.DecryptFile(
Expand Down Expand Up @@ -94,7 +104,7 @@ public async Task Should_Throw_Decrypting_Bytes_With_Wrong_FileCredentials()
};

IDecrypter decrypter = new Decrypter();
byte[] encContent = await File.ReadAllBytesAsync("Files/driver_license-selfie.jpg.enc");
byte[] encContent = await File.ReadAllBytesAsync("Files/bytes_dec3.driver_license-selfie.jpg.enc");

Exception exception = Assert.ThrowsAny<Exception>(() =>
decrypter.DecryptFile(
Expand Down
31 changes: 23 additions & 8 deletions test/UnitTests/Decryption/File Stream Decryption Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,23 @@ namespace UnitTests
/// <summary>
/// Tests for decrypting file streams using <see cref="IDecrypter.DecryptFileAsync"/> method
/// </summary>
public class FileStreamDecryptionTests
public class FileStreamDecryptionTests : IClassFixture<FileStreamDecryptionTests.Fixture>
{
public class Fixture
{
public Fixture() =>
FileDuplicator.CopyTestFiles(
("driver_license-selfie.jpg.enc", "s_dec1.driver_license-selfie.jpg.enc"),
("driver_license-selfie.jpg.enc", "s_dec2.driver_license-selfie.jpg.enc"),
("driver_license-selfie.jpg", "s_dec3.driver_license-selfie.jpg"),
("driver_license-selfie.jpg.enc", "s_dec3.driver_license-selfie.jpg.enc"),
("driver_license-selfie.jpg.enc", "s_dec4.driver_license-selfie.jpg.enc"),
("driver_license-selfie.jpg.enc", "s_dec5.driver_license-selfie.jpg.enc"),
("driver_license-selfie.jpg", "s_dec6.driver_license-selfie.jpg"),
("driver_license-selfie.jpg", "s_dec7.driver_license-selfie.jpg")
);
}

[Fact(DisplayName = "Should decrypt from a seekable stream")]
public async Task Should_Decrypt_From_Seekable_Stream()
{
Expand All @@ -30,7 +45,7 @@ public async Task Should_Decrypt_From_Seekable_Stream()
IDecrypter decrypter = new Decrypter();

Stream encContentStream = new MemoryStream();
using (Stream encFileStream = new NonSeekableFileReadStream("Files/driver_license-selfie.jpg.enc"))
using (Stream encFileStream = new NonSeekableFileReadStream("Files/s_dec1.driver_license-selfie.jpg.enc"))
{
await encFileStream.CopyToAsync(encContentStream);
}
Expand Down Expand Up @@ -60,7 +75,7 @@ public async Task Should_Decrypt_From_NonSeekable_Stream()
IDecrypter decrypter = new Decrypter();

using (Stream
encFileStream = new NonSeekableFileReadStream("Files/driver_license-selfie.jpg.enc"),
encFileStream = new NonSeekableFileReadStream("Files/s_dec2.driver_license-selfie.jpg.enc"),
fileStream = new MemoryStream()
)
{
Expand All @@ -85,7 +100,7 @@ public async Task Should_Throw_Decrypting_Unencrypted_File()

Exception exception;
using (Stream
encFileStream = new NonSeekableFileReadStream("Files/driver_license-selfie.jpg"),
encFileStream = new NonSeekableFileReadStream("Files/s_dec3.driver_license-selfie.jpg"),
fileStream = new MemoryStream()
)
{
Expand Down Expand Up @@ -114,7 +129,7 @@ public async Task Should_Throw_Decrypting_Mispositioned_Stream()
IDecrypter decrypter = new Decrypter();

Stream encContentStream = new MemoryStream();
using (Stream encFileStream = new NonSeekableFileReadStream("Files/driver_license-selfie.jpg.enc"))
using (Stream encFileStream = new NonSeekableFileReadStream("Files/s_dec4.driver_license-selfie.jpg.enc"))
{
await encFileStream.CopyToAsync(encContentStream);
}
Expand Down Expand Up @@ -149,7 +164,7 @@ public async Task Should_Throw_Decrypting_Stream_With_Wrong_FileCredentials()

Exception exception;
using (Stream
encFileStream = new NonSeekableFileReadStream("Files/driver_license-selfie.jpg.enc"),
encFileStream = new NonSeekableFileReadStream("Files/s_dec5.driver_license-selfie.jpg.enc"),
fileStream = new MemoryStream()
)
{
Expand Down Expand Up @@ -225,7 +240,7 @@ public async Task Should_Throw_If_NonReadable_Data_Stream()
FileCredentials fileCredentials = new FileCredentials {Secret = "", FileHash = ""};

Exception exception;
using (Stream encStream = File.OpenWrite("Files/driver_license-selfie.jpg"))
using (Stream encStream = File.OpenWrite("Files/s_dec6.driver_license-selfie.jpg"))
{
exception = await Assert.ThrowsAnyAsync<Exception>(() =>
decrypter.DecryptFileAsync(encStream, fileCredentials, new MemoryStream())
Expand Down Expand Up @@ -292,7 +307,7 @@ public async Task Should_Throw_If_NonWritable_Destination()
FileCredentials fileCredentials = new FileCredentials {Secret = "", FileHash = ""};

Exception exception;
using (Stream destStream = File.OpenRead("Files/driver_license-selfie.jpg"))
using (Stream destStream = File.OpenRead("Files/s_dec7.driver_license-selfie.jpg"))
{
exception = await Assert.ThrowsAnyAsync<Exception>(() =>
decrypter.DecryptFileAsync(new MemoryStream(new byte[16]), fileCredentials, destStream)
Expand Down
19 changes: 19 additions & 0 deletions test/UnitTests/FileDuplicatorFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.IO;

namespace UnitTests
{
public static class FileDuplicator
{
/// <summary>
/// Copies test files before unit tests parallel execution starts in order to avoid file access errors
/// </summary>
/// <param name="map">Mapping of source to destination files</param>
public static void CopyTestFiles(
params (string Src, string Dest)[] map
)
{
foreach (var m in map)
File.Copy($"Files/{m.Src}", $"Files/{m.Dest}", true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ReSharper disable InconsistentNaming
// ReSharper disable PossibleNullReferenceException
// ReSharper disable CheckNamespace
// ReSharper disable StringLiteralTypo

using Newtonsoft.Json;
using Telegram.Bot.Passport;
Expand Down Expand Up @@ -62,7 +63,6 @@ public void Should_decrypt_address_element()
}

static PassportData GetPassportData() =>
// ReSharper disable StringLiteralTypo
JsonConvert.DeserializeObject<PassportData>(@"
{
""data"": [
Expand All @@ -79,6 +79,5 @@ static PassportData GetPassportData() =>
}
}
");
// ReSharper restore StringLiteralTypo
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Telegram.Bot.Passport;
Expand Down
1 change: 1 addition & 0 deletions test/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="BouncyCastle" Version="1.8.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
Expand Down

0 comments on commit 2639d80

Please sign in to comment.