KeyEscrowServiceProviderExtensionsTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.Collections.Generic;
  5. using System.Globalization;
  6. using System.Xml.Linq;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Moq;
  9. using Xunit;
  10. namespace Microsoft.AspNetCore.DataProtection.KeyManagement
  11. {
  12. public class KeyEscrowServiceProviderExtensionsTests
  13. {
  14. [Fact]
  15. public void GetKeyEscrowSink_NullServiceProvider_ReturnsNull()
  16. {
  17. Assert.Null(((IServiceProvider)null).GetKeyEscrowSink());
  18. }
  19. [Fact]
  20. public void GetKeyEscrowSink_EmptyServiceProvider_ReturnsNull()
  21. {
  22. // Arrange
  23. var services = new ServiceCollection().BuildServiceProvider();
  24. // Act & assert
  25. Assert.Null(services.GetKeyEscrowSink());
  26. }
  27. [Fact]
  28. public void GetKeyEscrowSink_SingleKeyEscrowRegistration_ReturnsAggregateOverSingleSink()
  29. {
  30. // Arrange
  31. List<string> output = new List<string>();
  32. var mockKeyEscrowSink = new Mock<IKeyEscrowSink>();
  33. mockKeyEscrowSink.Setup(o => o.Store(It.IsAny<Guid>(), It.IsAny<XElement>()))
  34. .Callback<Guid, XElement>((keyId, element) =>
  35. {
  36. output.Add(String.Format(CultureInfo.InvariantCulture, "{0:D}: {1}", keyId, element.Name.LocalName));
  37. });
  38. var serviceCollection = new ServiceCollection();
  39. serviceCollection.AddSingleton<IKeyEscrowSink>(mockKeyEscrowSink.Object);
  40. var services = serviceCollection.BuildServiceProvider();
  41. // Act
  42. var sink = services.GetKeyEscrowSink();
  43. sink.Store(new Guid("39974d8e-3e53-4d78-b7e9-4ff64a2a5d7b"), XElement.Parse("<theElement />"));
  44. // Assert
  45. Assert.Equal(new[] { "39974d8e-3e53-4d78-b7e9-4ff64a2a5d7b: theElement" }, output);
  46. }
  47. [Fact]
  48. public void GetKeyEscrowSink_MultipleKeyEscrowRegistration_ReturnsAggregate()
  49. {
  50. // Arrange
  51. List<string> output = new List<string>();
  52. var mockKeyEscrowSink1 = new Mock<IKeyEscrowSink>();
  53. mockKeyEscrowSink1.Setup(o => o.Store(It.IsAny<Guid>(), It.IsAny<XElement>()))
  54. .Callback<Guid, XElement>((keyId, element) =>
  55. {
  56. output.Add(String.Format(CultureInfo.InvariantCulture, "[sink1] {0:D}: {1}", keyId, element.Name.LocalName));
  57. });
  58. var mockKeyEscrowSink2 = new Mock<IKeyEscrowSink>();
  59. mockKeyEscrowSink2.Setup(o => o.Store(It.IsAny<Guid>(), It.IsAny<XElement>()))
  60. .Callback<Guid, XElement>((keyId, element) =>
  61. {
  62. output.Add(String.Format(CultureInfo.InvariantCulture, "[sink2] {0:D}: {1}", keyId, element.Name.LocalName));
  63. });
  64. var serviceCollection = new ServiceCollection();
  65. serviceCollection.AddSingleton<IKeyEscrowSink>(mockKeyEscrowSink1.Object);
  66. serviceCollection.AddSingleton<IKeyEscrowSink>(mockKeyEscrowSink2.Object);
  67. var services = serviceCollection.BuildServiceProvider();
  68. // Act
  69. var sink = services.GetKeyEscrowSink();
  70. sink.Store(new Guid("39974d8e-3e53-4d78-b7e9-4ff64a2a5d7b"), XElement.Parse("<theElement />"));
  71. // Assert
  72. Assert.Equal(new[] { "[sink1] 39974d8e-3e53-4d78-b7e9-4ff64a2a5d7b: theElement", "[sink2] 39974d8e-3e53-4d78-b7e9-4ff64a2a5d7b: theElement" }, output);
  73. }
  74. }
  75. }