mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
8d5614cd7b
* Expand Hkdf crypto functions * Add tests for hkdf crypto functions Took the testing infrastructure from bitwarden/server * Move Hkdf to cryptoFunctionService * Port changes from bitwarden/jslib#192 * Port changes from bitwarden/jslib#205 * Make Send Expiration Optional implement changes from bitwarden/jslib#242 * Bug fixes found by testing * Test helpers * Test conversion between model types * Test SendService These are mostly happy-path tests to ensure a reasonably correct implementation * Add run tests step to GitHub Actions * Test send decryption * Test Request generation from Send * Constructor dependencies on separate lines * Remove unused testing infrastructure * Rename to match class name * Move fat arrows to previous lines * Handle exceptions in App layer * PR review cleanups * Throw when attempting to save an unkown Send Type I think it's best to only throw on unknown send types here. I don't think we want to throw whenever we encounter one since that would do bad things like lock up Sync if clients get out of date relative to servers. Instead, keep the client from ruining saved data by complaining last minute that it doesn't know what it's doing.
78 lines
3.5 KiB
C#
78 lines
3.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Bit.Core.Models.Data;
|
|
using Bit.Core.Models.Domain;
|
|
using Bit.Test.Common;
|
|
using System.Text;
|
|
using Bit.Core.Abstractions;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Utilities;
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
using Bit.Core.Test.AutoFixture;
|
|
using AutoFixture.AutoNSubstitute;
|
|
|
|
namespace Bit.Core.Test.Models.Domain
|
|
{
|
|
public class SendTests
|
|
{
|
|
[Theory]
|
|
[InlineCustomAutoData(new[] { typeof(FileSendCustomization) })]
|
|
[InlineCustomAutoData(new[] { typeof(TextSendCustomization) })]
|
|
public void Send_FromSendData_Success(SendData data)
|
|
{
|
|
var send = new Send(data);
|
|
|
|
TestHelper.AssertPropertyEqual(data, send, "Name", "Notes", "Key", "SendFileData.FileName", "SendFileData.Key", "SendTextData.Text");
|
|
Assert.Equal(data.Name, send.Name?.EncryptedString);
|
|
Assert.Equal(data.Notes, send.Notes?.EncryptedString);
|
|
Assert.Equal(data.Key, send.Key?.EncryptedString);
|
|
Assert.Equal(data.Text?.Text, send.Text?.Text?.EncryptedString);
|
|
Assert.Equal(data.File?.FileName, send.File?.FileName?.EncryptedString);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineCustomAutoData(new[] { typeof(AutoNSubstituteCustomization), typeof(TextSendCustomization) })]
|
|
[InlineCustomAutoData(new[] { typeof(AutoNSubstituteCustomization), typeof(FileSendCustomization) })]
|
|
public async void DecryptAsync_Success(ICryptoService cryptoService, Send send)
|
|
{
|
|
var prefix = "decrypted_";
|
|
var prefixBytes = Encoding.UTF8.GetBytes(prefix);
|
|
|
|
cryptoService.DecryptToBytesAsync(Arg.Any<CipherString>(), Arg.Any<SymmetricCryptoKey>())
|
|
.Returns(info => prefixBytes.Concat(Encoding.UTF8.GetBytes(((CipherString)info[0]).EncryptedString)).ToArray());
|
|
cryptoService.DecryptFromBytesAsync(Arg.Any<byte[]>(), Arg.Any<SymmetricCryptoKey>())
|
|
.Returns(info => prefixBytes.Concat((byte[])info[0]).ToArray());
|
|
cryptoService.DecryptToUtf8Async(Arg.Any<CipherString>(), Arg.Any<SymmetricCryptoKey>())
|
|
.Returns(info => $"{prefix}{((CipherString)info[0]).EncryptedString}");
|
|
ServiceContainer.Register("cryptoService", cryptoService);
|
|
|
|
var view = await send.DecryptAsync();
|
|
|
|
string expectedDecryptionString(CipherString encryptedString) =>
|
|
encryptedString?.EncryptedString == null ? null : $"{prefix}{encryptedString.EncryptedString}";
|
|
|
|
TestHelper.AssertPropertyEqual(send, view, "Name", "Notes", "File", "Text", "Key", "UserId");
|
|
Assert.Equal(expectedDecryptionString(send.Name), view.Name);
|
|
Assert.Equal(expectedDecryptionString(send.Notes), view.Notes);
|
|
Assert.Equal(Encoding.UTF8.GetBytes(expectedDecryptionString(send.Key)), view.Key);
|
|
|
|
switch (send.Type)
|
|
{
|
|
case SendType.File:
|
|
TestHelper.AssertPropertyEqual(send.File, view.File, "FileName");
|
|
Assert.Equal(expectedDecryptionString(send.File.FileName), view.File.FileName);
|
|
break;
|
|
case SendType.Text:
|
|
TestHelper.AssertPropertyEqual(send.Text, view?.Text, "Text");
|
|
Assert.Equal(expectedDecryptionString(send.Text.Text), view.Text.Text);
|
|
break;
|
|
default:
|
|
throw new Exception("Untested Send type");
|
|
}
|
|
|
|
ServiceContainer.Reset();
|
|
}
|
|
}
|
|
}
|