Example #1
0
    public void GetAuthenticatedEncryptorByKeyId_DefersInstantiation_AndReturnsRevocationInfo()
    {
        // Arrange
        var expectedEncryptorInstance1 = new Mock <IAuthenticatedEncryptor>().Object;
        var expectedEncryptorInstance2 = new Mock <IAuthenticatedEncryptor>().Object;

        var key1 = new MyKey(expectedEncryptorInstance: expectedEncryptorInstance1, isRevoked: true);
        var key2 = new MyKey(expectedEncryptorInstance: expectedEncryptorInstance2);


        // Act
        var keyRing = new KeyRing(key2, new[] { key1, key2 });

        // Assert
        Assert.Equal(0, key1.NumTimesCreateEncryptorInstanceCalled);
        Assert.Same(expectedEncryptorInstance1, keyRing.GetAuthenticatedEncryptorByKeyId(key1.KeyId, out var isRevoked));
        Assert.True(isRevoked);
        Assert.Equal(1, key1.NumTimesCreateEncryptorInstanceCalled);
        Assert.Same(expectedEncryptorInstance1, keyRing.GetAuthenticatedEncryptorByKeyId(key1.KeyId, out isRevoked));
        Assert.True(isRevoked);
        Assert.Equal(1, key1.NumTimesCreateEncryptorInstanceCalled);
        Assert.Equal(0, key2.NumTimesCreateEncryptorInstanceCalled);
        Assert.Same(expectedEncryptorInstance2, keyRing.GetAuthenticatedEncryptorByKeyId(key2.KeyId, out isRevoked));
        Assert.False(isRevoked);
        Assert.Equal(1, key2.NumTimesCreateEncryptorInstanceCalled);
        Assert.Same(expectedEncryptorInstance2, keyRing.GetAuthenticatedEncryptorByKeyId(key2.KeyId, out isRevoked));
        Assert.False(isRevoked);
        Assert.Equal(1, key2.NumTimesCreateEncryptorInstanceCalled);
        Assert.Same(expectedEncryptorInstance2, keyRing.DefaultAuthenticatedEncryptor);
        Assert.Equal(1, key2.NumTimesCreateEncryptorInstanceCalled);
    }
Example #2
0
        public void GetAuthenticatedEncryptorByKeyId_should_return_encryptor()
        {
            var sut = new KeyRing(new Mock <IKey>().Object, new[] { new Mock <IKey>().Object }, new RsaEncryptorConfiguration());

            var result = sut.GetAuthenticatedEncryptorByKeyId(Guid.NewGuid(), out bool _);

            Assert.Null(result);
        }
Example #3
0
    public void DefaultKeyIdAndEncryptor_IfDefaultKeyNotPresentInAllKeys()
    {
        // Arrange
        var key1 = new MyKey();
        var key2 = new MyKey();
        var key3 = new MyKey(expectedEncryptorInstance: new Mock <IAuthenticatedEncryptor>().Object);

        // Act
        var keyRing = new KeyRing(key3, new[] { key1, key2 });

        // Assert
        Assert.Equal(key3.KeyId, keyRing.DefaultKeyId);
        Assert.Equal(key3.CreateEncryptor(), keyRing.GetAuthenticatedEncryptorByKeyId(key3.KeyId, out var _));
    }