KeyTests.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
  6. using Moq;
  7. using Xunit;
  8. namespace Microsoft.AspNetCore.DataProtection.KeyManagement
  9. {
  10. public class KeyTests
  11. {
  12. [Fact]
  13. public void Ctor_Properties()
  14. {
  15. // Arrange
  16. var keyId = Guid.NewGuid();
  17. var creationDate = DateTimeOffset.Now;
  18. var activationDate = creationDate.AddDays(2);
  19. var expirationDate = creationDate.AddDays(90);
  20. // Act
  21. var key = new Key(keyId, creationDate, activationDate, expirationDate, new Mock<IAuthenticatedEncryptorDescriptor>().Object);
  22. // Assert
  23. Assert.Equal(keyId, key.KeyId);
  24. Assert.Equal(creationDate, key.CreationDate);
  25. Assert.Equal(activationDate, key.ActivationDate);
  26. Assert.Equal(expirationDate, key.ExpirationDate);
  27. }
  28. [Fact]
  29. public void SetRevoked_Respected()
  30. {
  31. // Arrange
  32. var now = DateTimeOffset.UtcNow;
  33. var key = new Key(Guid.Empty, now, now, now, new Mock<IAuthenticatedEncryptorDescriptor>().Object);
  34. // Act & assert
  35. Assert.False(key.IsRevoked);
  36. key.SetRevoked();
  37. Assert.True(key.IsRevoked);
  38. }
  39. [Fact]
  40. public void CreateEncryptorInstance()
  41. {
  42. // Arrange
  43. var expected = new Mock<IAuthenticatedEncryptor>().Object;
  44. var mockDescriptor = new Mock<IAuthenticatedEncryptorDescriptor>();
  45. mockDescriptor.Setup(o => o.CreateEncryptorInstance()).Returns(expected);
  46. var now = DateTimeOffset.UtcNow;
  47. var key = new Key(Guid.Empty, now, now, now, mockDescriptor.Object);
  48. // Act
  49. var actual = key.CreateEncryptorInstance();
  50. // Assert
  51. Assert.Same(expected, actual);
  52. }
  53. }
  54. }