CertificateXmlEncryptionTests.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.Security.Cryptography;
  5. using System.Security.Cryptography.Xml;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.Extensions.Logging.Abstractions;
  11. using Moq;
  12. using Xunit;
  13. namespace Microsoft.AspNetCore.DataProtection.XmlEncryption
  14. {
  15. public class CertificateXmlEncryptorTests
  16. {
  17. [Fact]
  18. public void Encrypt_Decrypt_RoundTrips()
  19. {
  20. // Arrange
  21. var symmetricAlgorithm = new TripleDESCryptoServiceProvider();
  22. symmetricAlgorithm.GenerateKey();
  23. var mockInternalEncryptor = new Mock<IInternalCertificateXmlEncryptor>();
  24. mockInternalEncryptor.Setup(o => o.PerformEncryption(It.IsAny<EncryptedXml>(), It.IsAny<XmlElement>()))
  25. .Returns<EncryptedXml, XmlElement>((encryptedXml, element) =>
  26. {
  27. encryptedXml.AddKeyNameMapping("theKey", symmetricAlgorithm); // use symmetric encryption
  28. return encryptedXml.Encrypt(element, "theKey");
  29. });
  30. var mockInternalDecryptor = new Mock<IInternalEncryptedXmlDecryptor>();
  31. mockInternalDecryptor.Setup(o => o.PerformPreDecryptionSetup(It.IsAny<EncryptedXml>()))
  32. .Callback<EncryptedXml>(encryptedXml =>
  33. {
  34. encryptedXml.AddKeyNameMapping("theKey", symmetricAlgorithm); // use symmetric encryption
  35. });
  36. var serviceCollection = new ServiceCollection();
  37. serviceCollection.AddSingleton<IInternalEncryptedXmlDecryptor>(mockInternalDecryptor.Object);
  38. var services = serviceCollection.BuildServiceProvider();
  39. var encryptor = new CertificateXmlEncryptor(NullLoggerFactory.Instance, mockInternalEncryptor.Object);
  40. var decryptor = new EncryptedXmlDecryptor(services);
  41. var originalXml = XElement.Parse(@"<mySecret value='265ee4ea-ade2-43b1-b706-09b259e58b6b' />");
  42. // Act & assert - run through encryptor and make sure we get back <EncryptedData> element
  43. var encryptedXmlInfo = encryptor.Encrypt(originalXml);
  44. Assert.Equal(typeof(EncryptedXmlDecryptor), encryptedXmlInfo.DecryptorType);
  45. Assert.Equal(XName.Get("EncryptedData", "http://www.w3.org/2001/04/xmlenc#"), encryptedXmlInfo.EncryptedElement.Name);
  46. Assert.Equal("http://www.w3.org/2001/04/xmlenc#Element", (string)encryptedXmlInfo.EncryptedElement.Attribute("Type"));
  47. Assert.DoesNotContain("265ee4ea-ade2-43b1-b706-09b259e58b6b", encryptedXmlInfo.EncryptedElement.ToString(), StringComparison.OrdinalIgnoreCase);
  48. // Act & assert - run through decryptor and make sure we get back the original value
  49. var roundTrippedElement = decryptor.Decrypt(encryptedXmlInfo.EncryptedElement);
  50. XmlAssert.Equal(originalXml, roundTrippedElement);
  51. }
  52. }
  53. }