Skip to content

Commit

Permalink
Add streamed file decryption tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Sep 1, 2018
1 parent 1779256 commit 1935b30
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// ReSharper disable CheckNamespace

using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Telegram.Bot.Passport;
using Telegram.Bot.Types.Passport;
using Xunit;
using File = Telegram.Bot.Types.File;

namespace UnitTests
{
Expand Down Expand Up @@ -69,7 +71,7 @@ public void Should_decrypt_credentials()
}

[Fact(DisplayName = "Should decrypt docuemnt data in 'identity_card' element")]
public void Should_decreypt_element_document()
public void Should_decrypt_element_document()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
Expand All @@ -96,7 +98,7 @@ public void Should_decreypt_element_document()
}

[Fact(DisplayName = "Should decrypt front side photo in 'identity_card' element")]
public async Task Should_decreypt_identity_card_element_frontside()
public async Task Should_decrypt_identity_card_element_frontside()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
Expand All @@ -119,10 +121,22 @@ public async Task Should_decreypt_identity_card_element_frontside()
Assert.NotEmpty(content);

await System.IO.File.WriteAllBytesAsync("Files/identity_card-front_side.jpg", content);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
credentials.SecureData.IdentityCard.FrontSide,
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

[Fact(DisplayName = "Should decrypt reverse side photo in 'identity_card' element")]
public async Task Should_decreypt_identity_card_element_reverseside()
public async Task Should_decrypt_identity_card_element_reverseside()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
Expand All @@ -145,11 +159,23 @@ public async Task Should_decreypt_identity_card_element_reverseside()

Assert.NotEmpty(content);

await System.IO.File.WriteAllBytesAsync("Files/identity_card-reverse_side.jpg", content);
await System.IO.File.WriteAllBytesAsync("Files/identity_card-reverse_side.jpg", encryptedContent);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
credentials.SecureData.IdentityCard.ReverseSide,
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

[Fact(DisplayName = "Should decrypt selfie photo in 'identity_card' element")]
public async Task Should_decreypt_identity_card_element_selfie()
public async Task Should_decrypt_identity_card_element_selfie()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
Expand All @@ -172,10 +198,22 @@ public async Task Should_decreypt_identity_card_element_selfie()
Assert.NotEmpty(content);

await System.IO.File.WriteAllBytesAsync("Files/identity_card-selfie.jpg", content);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
credentials.SecureData.IdentityCard.Selfie,
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

[Fact(DisplayName = "Should decrypt the single file in 'utility_bill' element")]
public async Task Should_decreypt_utility_bill_element_file()
public async Task Should_decrypt_utility_bill_element_file()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
Expand All @@ -202,10 +240,22 @@ public async Task Should_decreypt_utility_bill_element_file()
Assert.NotEmpty(content);

await System.IO.File.WriteAllBytesAsync("Files/utility_bill.jpg", content);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
billFileCreds,
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

[Fact(DisplayName = "Should decrypt the single translation file in 'utility_bill' element")]
public async Task Should_decreypt_utility_bill_element_translation()
public async Task Should_decrypt_utility_bill_element_translation()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
Expand All @@ -232,6 +282,18 @@ public async Task Should_decreypt_utility_bill_element_translation()
Assert.NotEmpty(content);

await System.IO.File.WriteAllBytesAsync("Files/utility_bill-translation.jpg", content);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
billTranslationFileCreds,
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

static PassportData GetPassportData() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
// ReSharper disable CheckNamespace

using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Telegram.Bot.Passport;
using Telegram.Bot.Types.Passport;
using Xunit;
using File = Telegram.Bot.Types.File;

namespace UnitTests
{
Expand Down Expand Up @@ -59,7 +61,7 @@ public void Should_decrypt_credentials()
}

[Fact(DisplayName = "Should decrypt document data of 'driver_license' element")]
public void Should_decreypt_document_data()
public void Should_decrypt_document_data()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();
Expand Down Expand Up @@ -87,75 +89,123 @@ public void Should_decreypt_document_data()
}

[Fact(DisplayName = "Should decrypt front side photo file of 'driver_license' element")]
public async Task Should_decreypt_front_side_file()
public async Task Should_decrypt_front_side_file()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);

byte[] encrypted = await System.IO.File.ReadAllBytesAsync("Files/driver_license-front_side.jpg.enc");
byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/driver_license-front_side.jpg.enc");
byte[] content = decrypter.DecryptFile(
encrypted,
encryptedContent,
credentials.SecureData.DriverLicense.FrontSide
);
Assert.NotEmpty(content);
await System.IO.File.WriteAllBytesAsync("Files/driver_license-front_side.jpg", content);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
credentials.SecureData.DriverLicense.FrontSide,
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

[Fact(DisplayName = "Should decrypt reverse side photo file of 'driver_license' element")]
public async Task Should_decreypt_reverse_side_file()
public async Task Should_decrypt_reverse_side_file()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);

byte[] encrypted = await System.IO.File.ReadAllBytesAsync("Files/driver_license-reverse_side.jpg.enc");
byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/driver_license-reverse_side.jpg.enc");
byte[] content = decrypter.DecryptFile(
encrypted,
encryptedContent,
credentials.SecureData.DriverLicense.ReverseSide
);
Assert.NotEmpty(content);
await System.IO.File.WriteAllBytesAsync("Files/driver_license-reverse_side.jpg", content);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
credentials.SecureData.DriverLicense.ReverseSide,
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

[Fact(DisplayName = "Should decrypt selfie photo file of 'driver_license' element")]
public async Task Should_decreypt_selfie_file()
public async Task Should_decrypt_selfie_file()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);

byte[] encrypted = await System.IO.File.ReadAllBytesAsync("Files/driver_license-selfie.jpg.enc");
byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/driver_license-selfie.jpg.enc");
byte[] content = decrypter.DecryptFile(
encrypted,
encryptedContent,
credentials.SecureData.DriverLicense.Selfie
);
Assert.NotEmpty(content);
await System.IO.File.WriteAllBytesAsync("Files/driver_license-selfie.jpg", content);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
credentials.SecureData.DriverLicense.Selfie,
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

[Fact(DisplayName = "Should decrypt translation photo file of 'driver_license' element")]
public async Task Should_decreypt_translation_file()
public async Task Should_decrypt_translation_file()
{
RSA key = EncryptionKey.GetRsaPrivateKey();
PassportData passportData = GetPassportData();

IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);

byte[] encrypted = await System.IO.File.ReadAllBytesAsync("Files/driver_license-translation0.jpg.enc");
byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/driver_license-translation0.jpg.enc");
byte[] content = decrypter.DecryptFile(
encrypted,
encryptedContent,
credentials.SecureData.DriverLicense.Translation.Single()
);
Assert.NotEmpty(content);
await System.IO.File.WriteAllBytesAsync("Files/driver_license-translation0.jpg", content);

using (var encryptedFileStream = new MemoryStream(encryptedContent))
using (var decryptedFileStream = new MemoryStream())
{
await decrypter.DecryptFileAsync(
encryptedFileStream,
credentials.SecureData.DriverLicense.Translation.Single(),
decryptedFileStream
);

Assert.Equal(content, decryptedFileStream.ToArray());
}
}

static PassportData GetPassportData() =>
Expand Down

0 comments on commit 1935b30

Please sign in to comment.