KeyRingTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
  5. using Moq;
  6. using Xunit;
  7. namespace Microsoft.AspNetCore.DataProtection.KeyManagement
  8. {
  9. public class KeyRingTests
  10. {
  11. [Fact]
  12. public void DefaultAuthenticatedEncryptor_Prop_InstantiationIsDeferred()
  13. {
  14. // Arrange
  15. var expectedEncryptorInstance = new Mock<IAuthenticatedEncryptor>().Object;
  16. var key1 = new MyKey(expectedEncryptorInstance: expectedEncryptorInstance);
  17. var key2 = new MyKey();
  18. // Act
  19. var keyRing = new KeyRing(key1, new[] { key1, key2 });
  20. // Assert
  21. Assert.Equal(0, key1.NumTimesCreateEncryptorInstanceCalled);
  22. Assert.Same(expectedEncryptorInstance, keyRing.DefaultAuthenticatedEncryptor);
  23. Assert.Equal(1, key1.NumTimesCreateEncryptorInstanceCalled);
  24. Assert.Same(expectedEncryptorInstance, keyRing.DefaultAuthenticatedEncryptor);
  25. Assert.Equal(1, key1.NumTimesCreateEncryptorInstanceCalled); // should've been cached
  26. }
  27. [Fact]
  28. public void DefaultKeyId_Prop()
  29. {
  30. // Arrange
  31. var key1 = new MyKey();
  32. var key2 = new MyKey();
  33. // Act
  34. var keyRing = new KeyRing(key2, new[] { key1, key2 });
  35. // Assert
  36. Assert.Equal(key2.KeyId, keyRing.DefaultKeyId);
  37. }
  38. [Fact]
  39. public void DefaultKeyIdAndEncryptor_IfDefaultKeyNotPresentInAllKeys()
  40. {
  41. // Arrange
  42. var key1 = new MyKey();
  43. var key2 = new MyKey();
  44. var key3 = new MyKey(expectedEncryptorInstance: new Mock<IAuthenticatedEncryptor>().Object);
  45. // Act
  46. var keyRing = new KeyRing(key3, new[] { key1, key2 });
  47. // Assert
  48. bool unused;
  49. Assert.Equal(key3.KeyId, keyRing.DefaultKeyId);
  50. Assert.Equal(key3.CreateEncryptorInstance(), keyRing.GetAuthenticatedEncryptorByKeyId(key3.KeyId, out unused));
  51. }
  52. [Fact]
  53. public void GetAuthenticatedEncryptorByKeyId_DefersInstantiation_AndReturnsRevocationInfo()
  54. {
  55. // Arrange
  56. var expectedEncryptorInstance1 = new Mock<IAuthenticatedEncryptor>().Object;
  57. var expectedEncryptorInstance2 = new Mock<IAuthenticatedEncryptor>().Object;
  58. var key1 = new MyKey(expectedEncryptorInstance: expectedEncryptorInstance1, isRevoked: true);
  59. var key2 = new MyKey(expectedEncryptorInstance: expectedEncryptorInstance2);
  60. // Act
  61. var keyRing = new KeyRing(key2, new[] { key1, key2 });
  62. // Assert
  63. bool isRevoked;
  64. Assert.Equal(0, key1.NumTimesCreateEncryptorInstanceCalled);
  65. Assert.Same(expectedEncryptorInstance1, keyRing.GetAuthenticatedEncryptorByKeyId(key1.KeyId, out isRevoked));
  66. Assert.True(isRevoked);
  67. Assert.Equal(1, key1.NumTimesCreateEncryptorInstanceCalled);
  68. Assert.Same(expectedEncryptorInstance1, keyRing.GetAuthenticatedEncryptorByKeyId(key1.KeyId, out isRevoked));
  69. Assert.True(isRevoked);
  70. Assert.Equal(1, key1.NumTimesCreateEncryptorInstanceCalled);
  71. Assert.Equal(0, key2.NumTimesCreateEncryptorInstanceCalled);
  72. Assert.Same(expectedEncryptorInstance2, keyRing.GetAuthenticatedEncryptorByKeyId(key2.KeyId, out isRevoked));
  73. Assert.False(isRevoked);
  74. Assert.Equal(1, key2.NumTimesCreateEncryptorInstanceCalled);
  75. Assert.Same(expectedEncryptorInstance2, keyRing.GetAuthenticatedEncryptorByKeyId(key2.KeyId, out isRevoked));
  76. Assert.False(isRevoked);
  77. Assert.Equal(1, key2.NumTimesCreateEncryptorInstanceCalled);
  78. Assert.Same(expectedEncryptorInstance2, keyRing.DefaultAuthenticatedEncryptor);
  79. Assert.Equal(1, key2.NumTimesCreateEncryptorInstanceCalled);
  80. }
  81. private sealed class MyKey : IKey
  82. {
  83. public int NumTimesCreateEncryptorInstanceCalled;
  84. private readonly Func<IAuthenticatedEncryptor> _encryptorFactory;
  85. public MyKey(bool isRevoked = false, IAuthenticatedEncryptor expectedEncryptorInstance = null)
  86. {
  87. CreationDate = DateTimeOffset.Now;
  88. ActivationDate = CreationDate + TimeSpan.FromHours(1);
  89. ExpirationDate = CreationDate + TimeSpan.FromDays(30);
  90. IsRevoked = isRevoked;
  91. KeyId = Guid.NewGuid();
  92. _encryptorFactory = () => expectedEncryptorInstance ?? new Mock<IAuthenticatedEncryptor>().Object;
  93. }
  94. public DateTimeOffset ActivationDate { get; }
  95. public DateTimeOffset CreationDate { get; }
  96. public DateTimeOffset ExpirationDate { get; }
  97. public bool IsRevoked { get; }
  98. public Guid KeyId { get; }
  99. public IAuthenticatedEncryptor CreateEncryptorInstance()
  100. {
  101. NumTimesCreateEncryptorInstanceCalled++;
  102. return _encryptorFactory();
  103. }
  104. }
  105. }
  106. }