Exemplo n.º 1
0
    public void Without_Type_Null_Input_Byte_Key_Returns_Empty(string input)
    {
        // Arrange
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = input.Decrypt(key);

        // Assert
        Assert.Equal(string.Empty, result);
    }
Exemplo n.º 2
0
    public void Incorrect_Byte_Key_Returns_None()
    {
        // Arrange
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = defaultInputStringEncryptedWithByteKey.Decrypt <string>(key);

        // Assert
        result.AssertNone().AssertType <IncorrectKeyOrNonceExceptionMsg>();
    }
Exemplo n.º 3
0
    public void Null_Input_Byte_Key_Returns_None(string input)
    {
        // Arrange
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = input.Decrypt <int>(key);

        // Assert
        result.AssertNone().AssertType <DeserialisingNullOrEmptyStringMsg>();
    }
Exemplo n.º 4
0
    public void Without_Type_Incorrect_Byte_Key_Returns_Empty()
    {
        // Arrange
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = defaultInputStringEncryptedWithByteKey.Decrypt(key);

        // Assert
        Assert.Equal(string.Empty, result);
    }
Exemplo n.º 5
0
    public void Incorrect_Json_Input_Byte_Key_Returns_None()
    {
        // Arrange
        var          key  = CryptoF.GenerateKey().UnsafeUnwrap();
        const string json = /*lang=json,strict*/ "{\"foo\":\"bar\"}";

        // Act
        var result = json.Decrypt <int>(key);

        // Assert
        result.AssertNone().AssertType <UnlockWhenEncryptedContentsIsNoneMsg>();
    }
Exemplo n.º 6
0
    public void Invalid_Json_Input_Byte_Key_Returns_None()
    {
        // Arrange
        var key  = CryptoF.GenerateKey().UnsafeUnwrap();
        var json = Rnd.Str;

        // Act
        var result = json.Decrypt <int>(key);

        // Assert
        result.AssertNone().AssertType <DeserialiseExceptionMsg>();
    }
Exemplo n.º 7
0
    public void GenerateKey_Returns32ByteArray()
    {
        // Arrange

        // Act
        var result = CryptoF.GenerateKey();

        // Assert
        var some = result.AssertSome();

        Assert.Equal(32, some.Length);
    }
Exemplo n.º 8
0
    public void Without_Type_Incorrect_Json_Input_Byte_Key_Returns_Empty()
    {
        // Arrange
        var          key  = CryptoF.GenerateKey().UnsafeUnwrap();
        const string json = /*lang=json,strict*/ "{\"foo\":\"bar\"}";

        // Act
        var result = json.Decrypt(key);

        // Assert
        Assert.Equal(string.Empty, result);
    }
Exemplo n.º 9
0
    public void Without_Type_Invalid_Json_Input_Byte_Key_Returns_Empty()
    {
        // Arrange
        var key  = CryptoF.GenerateKey().UnsafeUnwrap();
        var json = Rnd.Str;

        // Act
        var result = json.Decrypt(key);

        // Assert
        Assert.Equal(string.Empty, result);
    }
Exemplo n.º 10
0
    public void No_EncryptedContents_Returns_None_With_UnlockWhenEncryptedContentsIsNullMsg()
    {
        // Arrange
        var box = new Locked <int>();
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = box.Unlock(key);

        // Assert
        result.AssertNone().AssertType <UnlockWhenEncryptedContentsIsNoneMsg>();
    }
Exemplo n.º 11
0
    public void Invalid_Key_Returns_None_With_InvalidKeyExceptionMsg()
    {
        // Arrange
        var value = Rnd.Str;
        var key   = CryptoF.GenerateKey().UnsafeUnwrap();
        var box   = new Locked <string>(value, key);

        // Act
        var result = box.Unlock(Rnd.ByteF.Get(16));

        // Assert
        result.AssertNone().AssertType <InvalidKeyExceptionMsg>();
    }
Exemplo n.º 12
0
    public void GenerateKey_ReturnsUniqueValues()
    {
        // Arrange

        // Act
        var r0 = CryptoF.GenerateKey();
        var r1 = CryptoF.GenerateKey();

        // Assert
        var s0 = r0.AssertSome();
        var s1 = r1.AssertSome();

        Assert.NotEqual(s0, s1);
    }
Exemplo n.º 13
0
    public void Incorrect_Key_Returns_None_With_IncorrectKeyOrNonceMsg()
    {
        // Arrange
        var value        = Rnd.Str;
        var key          = CryptoF.GenerateKey().UnsafeUnwrap();
        var incorrectKey = CryptoF.GenerateKey().UnsafeUnwrap();
        var box          = new Locked <string>(value, key);

        // Act
        var result = box.Unlock(incorrectKey);

        // Assert
        result.AssertNone().AssertType <IncorrectKeyOrNonceExceptionMsg>();
    }
Exemplo n.º 14
0
    public void Byte_Key_Returns_Lockable()
    {
        // Arrange
        var value = Rnd.Str;
        var key   = CryptoF.GenerateKey().UnsafeUnwrap();
        var box   = new Locked <string>(value, key);

        // Act
        var result = box.Unlock(key);

        // Assert
        var some = result.AssertSome();

        Assert.Equal(value, some.Contents);
    }
Exemplo n.º 15
0
    public void Byte_Key_Fills_With_Encrypted_Contents()
    {
        // Arrange
        var value = Rnd.Str;
        var key   = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = new Locked <string>(value, key);

        // Assert
        Assert.NotEmpty(result.Salt);
        Assert.NotEmpty(result.Nonce);
        var some = result.EncryptedContents.AssertSome();

        Assert.NotEmpty(some);
    }
Exemplo n.º 16
0
    public void Returns_Json()
    {
        // Arrange
        var value = Rnd.Str;
        var key   = CryptoF.GenerateKey().UnsafeUnwrap();
        var box   = new Locked <string>(value, key);
        var json  = string.Format("{{\"encryptedContents\":\"{0}\",\"salt\":\"{1}\",\"nonce\":\"{2}\"}}",
                                  Convert.ToBase64String(box.EncryptedContents.UnsafeUnwrap()),
                                  Convert.ToBase64String(box.Salt),
                                  Convert.ToBase64String(box.Nonce)
                                  );

        // Act
        var result = box.Serialise();

        // Assert
        var some = result.AssertSome();

        Assert.Equal(json, some);
    }