FileSystemXmlRepositoryTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.IO;
  5. using System.Linq;
  6. using System.Xml.Linq;
  7. using Microsoft.AspNetCore.Testing.xunit;
  8. using Xunit;
  9. namespace Microsoft.AspNetCore.DataProtection.Repositories
  10. {
  11. public class FileSystemXmlRepositoryTests
  12. {
  13. [ConditionalFact]
  14. [ConditionalRunTestOnlyIfLocalAppDataAvailable]
  15. public void DefaultKeyStorageDirectory_Property()
  16. {
  17. // Act
  18. var defaultDirInfo = FileSystemXmlRepository.DefaultKeyStorageDirectory;
  19. // Assert
  20. Assert.Equal(defaultDirInfo.FullName,
  21. new DirectoryInfo(Path.Combine(GetLocalApplicationData(), "ASP.NET", "DataProtection-Keys")).FullName);
  22. }
  23. [Fact]
  24. public void Directory_Property()
  25. {
  26. WithUniqueTempDirectory(dirInfo =>
  27. {
  28. // Arrange
  29. var repository = new FileSystemXmlRepository(dirInfo);
  30. // Act
  31. var retVal = repository.Directory;
  32. // Assert
  33. Assert.Equal(dirInfo, retVal);
  34. });
  35. }
  36. [Fact]
  37. public void GetAllElements_EmptyOrNonexistentDirectory_ReturnsEmptyCollection()
  38. {
  39. WithUniqueTempDirectory(dirInfo =>
  40. {
  41. // Arrange
  42. var repository = new FileSystemXmlRepository(dirInfo);
  43. // Act
  44. var allElements = repository.GetAllElements();
  45. // Assert
  46. Assert.Equal(0, allElements.Count);
  47. });
  48. }
  49. [Fact]
  50. public void StoreElement_WithValidFriendlyName_UsesFriendlyName()
  51. {
  52. WithUniqueTempDirectory(dirInfo =>
  53. {
  54. // Arrange
  55. var element = XElement.Parse("<element1 />");
  56. var repository = new FileSystemXmlRepository(dirInfo);
  57. // Act
  58. repository.StoreElement(element, "valid-friendly-name");
  59. // Assert
  60. var fileInfos = dirInfo.GetFiles();
  61. var fileInfo = fileInfos.Single(); // only one file should've been created
  62. // filename should be "valid-friendly-name.xml"
  63. Assert.Equal("valid-friendly-name.xml", fileInfo.Name, StringComparer.OrdinalIgnoreCase);
  64. // file contents should be "<element1 />"
  65. var parsedElement = XElement.Parse(File.ReadAllText(fileInfo.FullName));
  66. XmlAssert.Equal("<element1 />", parsedElement);
  67. });
  68. }
  69. [Theory]
  70. [InlineData(null)]
  71. [InlineData("")]
  72. [InlineData(" ")]
  73. [InlineData("..")]
  74. [InlineData("not*friendly")]
  75. public void StoreElement_WithInvalidFriendlyName_CreatesNewGuidAsName(string friendlyName)
  76. {
  77. WithUniqueTempDirectory(dirInfo =>
  78. {
  79. // Arrange
  80. var element = XElement.Parse("<element1 />");
  81. var repository = new FileSystemXmlRepository(dirInfo);
  82. // Act
  83. repository.StoreElement(element, friendlyName);
  84. // Assert
  85. var fileInfos = dirInfo.GetFiles();
  86. var fileInfo = fileInfos.Single(); // only one file should've been created
  87. // filename should be "{GUID}.xml"
  88. var filename = fileInfo.Name;
  89. Assert.EndsWith(".xml", filename, StringComparison.OrdinalIgnoreCase);
  90. var filenameNoSuffix = filename.Substring(0, filename.Length - ".xml".Length);
  91. Guid parsedGuid = Guid.Parse(filenameNoSuffix);
  92. Assert.NotEqual(Guid.Empty, parsedGuid);
  93. // file contents should be "<element1 />"
  94. var parsedElement = XElement.Parse(File.ReadAllText(fileInfo.FullName));
  95. XmlAssert.Equal("<element1 />", parsedElement);
  96. });
  97. }
  98. [Fact]
  99. public void StoreElements_ThenRetrieve_SeesAllElements()
  100. {
  101. WithUniqueTempDirectory(dirInfo =>
  102. {
  103. // Arrange
  104. var repository = new FileSystemXmlRepository(dirInfo);
  105. // Act
  106. repository.StoreElement(new XElement("element1"), friendlyName: null);
  107. repository.StoreElement(new XElement("element2"), friendlyName: null);
  108. repository.StoreElement(new XElement("element3"), friendlyName: null);
  109. var allElements = repository.GetAllElements();
  110. // Assert
  111. var orderedNames = allElements.Select(el => el.Name.LocalName).OrderBy(name => name);
  112. Assert.Equal(new[] { "element1", "element2", "element3" }, orderedNames);
  113. });
  114. }
  115. /// <summary>
  116. /// Runs a test and cleans up the temp directory afterward.
  117. /// </summary>
  118. private static void WithUniqueTempDirectory(Action<DirectoryInfo> testCode)
  119. {
  120. string uniqueTempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
  121. var dirInfo = new DirectoryInfo(uniqueTempPath);
  122. try
  123. {
  124. testCode(dirInfo);
  125. }
  126. finally
  127. {
  128. // clean up when test is done
  129. if (dirInfo.Exists)
  130. {
  131. dirInfo.Delete(recursive: true);
  132. }
  133. }
  134. }
  135. private static string GetLocalApplicationData()
  136. {
  137. #if NETCOREAPP1_0
  138. return Environment.GetEnvironmentVariable("LOCALAPPDATA");
  139. #else
  140. return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  141. #endif
  142. }
  143. private class ConditionalRunTestOnlyIfLocalAppDataAvailable : Attribute, ITestCondition
  144. {
  145. public bool IsMet => GetLocalApplicationData() != null;
  146. public string SkipReason { get; } = "%LOCALAPPDATA% couldn't be located.";
  147. }
  148. }
  149. }