DataProtectionAdvancedExtensionsTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 System.Globalization;
  5. using System.Text;
  6. using Moq;
  7. using Xunit;
  8. namespace Microsoft.AspNetCore.DataProtection
  9. {
  10. public class DataProtectionAdvancedExtensionsTests
  11. {
  12. private const string SampleEncodedString = "AQI"; // = WebEncoders.Base64UrlEncode({ 0x01, 0x02 })
  13. [Fact]
  14. public void Protect_PayloadAsString_WithExplicitExpiration()
  15. {
  16. // Arrange
  17. var plaintextAsBytes = Encoding.UTF8.GetBytes("this is plaintext");
  18. var expiration = StringToDateTime("2015-01-01 00:00:00Z");
  19. var mockDataProtector = new Mock<ITimeLimitedDataProtector>();
  20. mockDataProtector.Setup(o => o.Protect(plaintextAsBytes, expiration)).Returns(new byte[] { 0x01, 0x02 });
  21. // Act
  22. string protectedPayload = mockDataProtector.Object.Protect("this is plaintext", expiration);
  23. // Assert
  24. Assert.Equal(SampleEncodedString, protectedPayload);
  25. }
  26. [Fact]
  27. public void Protect_PayloadAsString_WithLifetimeAsTimeSpan()
  28. {
  29. // Arrange
  30. var plaintextAsBytes = Encoding.UTF8.GetBytes("this is plaintext");
  31. DateTimeOffset actualExpiration = default(DateTimeOffset);
  32. var mockDataProtector = new Mock<ITimeLimitedDataProtector>();
  33. mockDataProtector.Setup(o => o.Protect(plaintextAsBytes, It.IsAny<DateTimeOffset>()))
  34. .Returns<byte[], DateTimeOffset>((_, exp) =>
  35. {
  36. actualExpiration = exp;
  37. return new byte[] { 0x01, 0x02 };
  38. });
  39. // Act
  40. DateTimeOffset lowerBound = DateTimeOffset.UtcNow.AddHours(48);
  41. string protectedPayload = mockDataProtector.Object.Protect("this is plaintext", TimeSpan.FromHours(48));
  42. DateTimeOffset upperBound = DateTimeOffset.UtcNow.AddHours(48);
  43. // Assert
  44. Assert.Equal(SampleEncodedString, protectedPayload);
  45. Assert.InRange(actualExpiration, lowerBound, upperBound);
  46. }
  47. [Fact]
  48. public void Protect_PayloadAsBytes_WithLifetimeAsTimeSpan()
  49. {
  50. // Arrange
  51. DateTimeOffset actualExpiration = default(DateTimeOffset);
  52. var mockDataProtector = new Mock<ITimeLimitedDataProtector>();
  53. mockDataProtector.Setup(o => o.Protect(new byte[] { 0x11, 0x22, 0x33 }, It.IsAny<DateTimeOffset>()))
  54. .Returns<byte[], DateTimeOffset>((_, exp) =>
  55. {
  56. actualExpiration = exp;
  57. return new byte[] { 0x01, 0x02 };
  58. });
  59. // Act
  60. DateTimeOffset lowerBound = DateTimeOffset.UtcNow.AddHours(48);
  61. byte[] protectedPayload = mockDataProtector.Object.Protect(new byte[] { 0x11, 0x22, 0x33 }, TimeSpan.FromHours(48));
  62. DateTimeOffset upperBound = DateTimeOffset.UtcNow.AddHours(48);
  63. // Assert
  64. Assert.Equal(new byte[] { 0x01, 0x02 }, protectedPayload);
  65. Assert.InRange(actualExpiration, lowerBound, upperBound);
  66. }
  67. [Fact]
  68. public void Unprotect_PayloadAsString()
  69. {
  70. // Arrange
  71. var futureDate = DateTimeOffset.UtcNow.AddYears(1);
  72. var controlExpiration = futureDate;
  73. var mockDataProtector = new Mock<ITimeLimitedDataProtector>();
  74. mockDataProtector.Setup(o => o.Unprotect(new byte[] { 0x01, 0x02 }, out controlExpiration)).Returns(Encoding.UTF8.GetBytes("this is plaintext"));
  75. // Act
  76. DateTimeOffset testExpiration;
  77. string unprotectedPayload = mockDataProtector.Object.Unprotect(SampleEncodedString, out testExpiration);
  78. // Assert
  79. Assert.Equal("this is plaintext", unprotectedPayload);
  80. Assert.Equal(futureDate, testExpiration);
  81. }
  82. private static DateTime StringToDateTime(string input)
  83. {
  84. return DateTimeOffset.ParseExact(input, "u", CultureInfo.InvariantCulture).UtcDateTime;
  85. }
  86. }
  87. }