DpapiNGXmlEncryptionTests.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233
  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.Xml.Linq;
  5. using Microsoft.AspNetCore.DataProtection.Test.Shared;
  6. using Microsoft.AspNetCore.Testing.xunit;
  7. using Xunit;
  8. namespace Microsoft.AspNetCore.DataProtection.XmlEncryption
  9. {
  10. public class DpapiNGXmlEncryptionTests
  11. {
  12. [ConditionalFact]
  13. [ConditionalRunTestOnlyOnWindows8OrLater]
  14. public void Encrypt_Decrypt_RoundTrips()
  15. {
  16. // Arrange
  17. var originalXml = XElement.Parse(@"<mySecret value='265ee4ea-ade2-43b1-b706-09b259e58b6b' />");
  18. var encryptor = new DpapiNGXmlEncryptor("LOCAL=user", DpapiNGProtectionDescriptorFlags.None);
  19. var decryptor = new DpapiNGXmlDecryptor();
  20. // Act & assert - run through encryptor and make sure we get back an obfuscated element
  21. var encryptedXmlInfo = encryptor.Encrypt(originalXml);
  22. Assert.Equal(typeof(DpapiNGXmlDecryptor), encryptedXmlInfo.DecryptorType);
  23. Assert.DoesNotContain("265ee4ea-ade2-43b1-b706-09b259e58b6b", encryptedXmlInfo.EncryptedElement.ToString(), StringComparison.OrdinalIgnoreCase);
  24. // Act & assert - run through decryptor and make sure we get back the original value
  25. var roundTrippedElement = decryptor.Decrypt(encryptedXmlInfo.EncryptedElement);
  26. XmlAssert.Equal(originalXml, roundTrippedElement);
  27. }
  28. }
  29. }