2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Services;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
2017-04-25 23:05:13 +03:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Test
|
|
|
|
|
{
|
|
|
|
|
public class CryptoServiceTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void EncryptDecrypt()
|
|
|
|
|
{
|
|
|
|
|
var value = "hi";
|
|
|
|
|
Assert.Equal(EncryptDecryptValue(value), value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void EncryptDecryptLongValue()
|
|
|
|
|
{
|
|
|
|
|
var value = "This is a really long value that should encrypt and decrypt just fine too.";
|
|
|
|
|
Assert.Equal(EncryptDecryptValue(value), value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string EncryptDecryptValue(string value)
|
|
|
|
|
{
|
2016-08-02 03:23:46 +03:00
|
|
|
|
var storageService = Substitute.For<ISecureStorageService>();
|
|
|
|
|
var keyService = Substitute.For<IKeyDerivationService>();
|
2017-04-25 23:05:13 +03:00
|
|
|
|
var settingsService = Substitute.For<ISettings>();
|
2017-04-20 22:53:17 +03:00
|
|
|
|
storageService.Contains("key").Returns(true);
|
2016-08-02 03:23:46 +03:00
|
|
|
|
storageService.Retrieve("key").Returns(
|
|
|
|
|
Convert.FromBase64String("QpSYI5k0bLQXEygUEHn4wMII3ERatuWDFBszk7JAhbQ="));
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2017-04-25 23:05:13 +03:00
|
|
|
|
var service = new CryptoService(settingsService, storageService, keyService);
|
2017-04-20 22:53:17 +03:00
|
|
|
|
var encryptedValue = service.Encrypt(value);
|
|
|
|
|
return service.Decrypt(encryptedValue);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|