Ejemplo n.º 1
0
    public void Unprotect_KeyNotFound_RefreshOnce_ThrowsKeyNotFound()
    {
        // Arrange
        Guid notFoundKeyId = new Guid("654057ab-2491-4471-a72a-b3b114afda38");

        byte[] protectedData = BuildProtectedDataFromCiphertext(
            keyId: notFoundKeyId,
            ciphertext: new byte[0]);

        var mockDescriptor       = new Mock <IAuthenticatedEncryptorDescriptor>();
        var mockEncryptorFactory = new Mock <IAuthenticatedEncryptorFactory>();

        mockEncryptorFactory.Setup(o => o.CreateEncryptorInstance(It.IsAny <IKey>())).Returns(new Mock <IAuthenticatedEncryptor>().Object);
        var encryptorFactory = new AuthenticatedEncryptorFactory(NullLoggerFactory.Instance);

        // the keyring has only one key
        Key key     = new Key(Guid.Empty, DateTimeOffset.Now, DateTimeOffset.Now, DateTimeOffset.Now, mockDescriptor.Object, new[] { mockEncryptorFactory.Object });
        var keyRing = new CacheableKeyRing(CancellationToken.None, DateTimeOffset.MaxValue, key, new[] { key });

        var keyRingProvider = CreateKeyRingProvider(new TestKeyRingProvider(keyRing));

        IDataProtector protector = new KeyRingBasedDataProtector(
            keyRingProvider: keyRingProvider,
            logger: GetLogger(),
            originalPurposes: null,
            newPurpose: "purpose");

        // Act & assert
        var ex = ExceptionAssert2.ThrowsCryptographicException(() => protector.Unprotect(protectedData));

        Assert.Equal(Error.Common_KeyNotFound(notFoundKeyId).Message, ex.Message);
    }
Ejemplo n.º 2
0
    public void Unprotect_NullProtectedData_Throws()
    {
        // Arrange
        IDataProtector protector = new KeyRingBasedDataProtector(
            keyRingProvider: new Mock <IKeyRingProvider>().Object,
            logger: GetLogger(),
            originalPurposes: null,
            newPurpose: "purpose");

        // Act & assert
        ExceptionAssert.ThrowsArgumentNull(() => protector.Unprotect(protectedData: null), "protectedData");
    }
Ejemplo n.º 3
0
    public void Unprotect_PayloadHasIncorrectVersionMarker_ThrowsNewerVersion()
    {
        // Arrange
        IDataProtector protector = new KeyRingBasedDataProtector(
            keyRingProvider: new Mock <IKeyRingProvider>().Object,
            logger: GetLogger(),
            originalPurposes: null,
            newPurpose: "purpose");

        byte[] badProtectedPayload = BuildProtectedDataFromCiphertext(Guid.NewGuid(), new byte[0]);
        badProtectedPayload[3]++; // bump the version payload

        // Act & assert
        var ex = ExceptionAssert2.ThrowsCryptographicException(() => protector.Unprotect(badProtectedPayload));

        Assert.Equal(Resources.ProtectionProvider_BadVersion, ex.Message);
    }
Ejemplo n.º 4
0
    public void Unprotect_PayloadTooShort_ThrowsBadMagicHeader()
    {
        // Arrange
        IDataProtector protector = new KeyRingBasedDataProtector(
            keyRingProvider: new Mock <IKeyRingProvider>().Object,
            logger: GetLogger(),
            originalPurposes: null,
            newPurpose: "purpose");

        byte[] badProtectedPayload = BuildProtectedDataFromCiphertext(Guid.NewGuid(), new byte[0]);
        badProtectedPayload = badProtectedPayload.Take(badProtectedPayload.Length - 1).ToArray(); // chop off the last byte

        // Act & assert
        var ex = ExceptionAssert2.ThrowsCryptographicException(() => protector.Unprotect(badProtectedPayload));

        Assert.Equal(Resources.ProtectionProvider_BadMagicHeader, ex.Message);
    }