From 921c972ddd7efbc7e062beddda2f2fc85d02a93e Mon Sep 17 00:00:00 2001 From: JohnMcPMS Date: Mon, 17 Apr 2023 10:11:51 -0700 Subject: [PATCH] Add tests for path replacement and ensure that the user profile path isn't in display --- .../AppInstallerCLITests.vcxproj.filters | 12 +++++----- src/AppInstallerCLITests/Filesystem.cpp | 16 +++++++++++++- src/AppInstallerCLITests/Runtime.cpp | 22 ++++++++++++++++++- src/AppInstallerCLITests/TestCommon.cpp | 13 +++++++++++ src/AppInstallerCLITests/TestCommon.h | 3 +++ src/AppInstallerCLITests/main.cpp | 9 +------- src/AppInstallerCLITests/pch.h | 1 + .../Public/AppInstallerRuntime.h | 2 ++ 8 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/AppInstallerCLITests/AppInstallerCLITests.vcxproj.filters b/src/AppInstallerCLITests/AppInstallerCLITests.vcxproj.filters index f9afd0fbb5..6e581b1893 100644 --- a/src/AppInstallerCLITests/AppInstallerCLITests.vcxproj.filters +++ b/src/AppInstallerCLITests/AppInstallerCLITests.vcxproj.filters @@ -227,9 +227,6 @@ Source Files\Common - - Source Files - Source Files\Common @@ -242,9 +239,6 @@ Source Files\Repository - - Source Files - Source Files\Common @@ -293,6 +287,12 @@ Source Files\CLI + + Source Files\Common + + + Source Files\Repository + diff --git a/src/AppInstallerCLITests/Filesystem.cpp b/src/AppInstallerCLITests/Filesystem.cpp index f3bff14163..c1d0db7f1e 100644 --- a/src/AppInstallerCLITests/Filesystem.cpp +++ b/src/AppInstallerCLITests/Filesystem.cpp @@ -86,4 +86,18 @@ TEST_CASE("VerifyIsSameVolume", "[filesystem]") REQUIRE_FALSE(IsSameVolume(path5, path6)); REQUIRE_FALSE(IsSameVolume(path4, path6)); REQUIRE_FALSE(IsSameVolume(path6, path8)); -} \ No newline at end of file +} + +TEST_CASE("ReplaceCommonPathPrefix", "[filesystem]") +{ + std::filesystem::path prefix = "C:\\test1\\test2"; + std::string replacement = "%TEST%"; + + std::filesystem::path shouldReplace = "C:\\test1\\test2\\subdir1\\subdir2"; + REQUIRE(ReplaceCommonPathPrefix(shouldReplace, prefix, replacement)); + REQUIRE(shouldReplace.u8string() == (replacement + "\\subdir1\\subdir2")); + + std::filesystem::path shouldNotReplace = "C:\\test1\\test3\\subdir1\\subdir2"; + REQUIRE(!ReplaceCommonPathPrefix(shouldNotReplace, prefix, replacement)); + REQUIRE(shouldNotReplace.u8string() == "C:\\test1\\test3\\subdir1\\subdir2"); +} diff --git a/src/AppInstallerCLITests/Runtime.cpp b/src/AppInstallerCLITests/Runtime.cpp index 4be693db71..65dc959a57 100644 --- a/src/AppInstallerCLITests/Runtime.cpp +++ b/src/AppInstallerCLITests/Runtime.cpp @@ -2,7 +2,9 @@ // Licensed under the MIT License. #include "pch.h" #include "TestCommon.h" +#include "TestHooks.h" #include +#include using namespace AppInstaller; using namespace AppInstaller::Runtime; @@ -113,4 +115,22 @@ TEST_CASE("VerifyDevModeEnabledCheck", "[runtime]") REQUIRE(modifiedState != initialState); REQUIRE(revertedState == initialState); -} \ No newline at end of file +} + +TEST_CASE("EnsureUserProfileNotPresentInDisplayPaths", "[runtime]") +{ + // Clear the overrides that we use when testing as they don't consider display purposes + Runtime::TestHook_ClearPathOverrides(); + auto restorePaths = wil::scope_exit([]() { TestCommon::SetTestPathOverrides(); }); + + std::filesystem::path userProfilePath = Filesystem::GetKnownFolderPath(FOLDERID_Profile); + std::string userProfileString = userProfilePath.u8string(); + + for (auto i = ToIntegral(ToEnum(0)); i < ToIntegral(PathName::Max); ++i) + { + std::filesystem::path displayPath = GetPathTo(ToEnum(i), true); + std::string displayPathString = displayPath.u8string(); + INFO(i << " = " << displayPathString); + REQUIRE(displayPathString.find(userProfileString) == std::string::npos); + } +} diff --git a/src/AppInstallerCLITests/TestCommon.cpp b/src/AppInstallerCLITests/TestCommon.cpp index 9abea67321..bb8d759637 100644 --- a/src/AppInstallerCLITests/TestCommon.cpp +++ b/src/AppInstallerCLITests/TestCommon.cpp @@ -8,6 +8,8 @@ #include #include +using namespace AppInstaller; + namespace TestCommon { namespace @@ -363,4 +365,15 @@ namespace TestCommon return root; } + + void SetTestPathOverrides() + { + // Force all tests to run against settings inside this container. + // This prevents test runs from trashing the users actual settings. + Runtime::TestHook_SetPathOverride(Runtime::PathName::LocalState, Runtime::GetPathTo(Runtime::PathName::LocalState) / "Tests"); + Runtime::TestHook_SetPathOverride(Runtime::PathName::UserFileSettings, Runtime::GetPathTo(Runtime::PathName::UserFileSettings) / "Tests"); + Runtime::TestHook_SetPathOverride(Runtime::PathName::StandardSettings, Runtime::GetPathTo(Runtime::PathName::StandardSettings) / "Tests"); + Runtime::TestHook_SetPathOverride(Runtime::PathName::SecureSettingsForRead, Runtime::GetPathTo(Runtime::PathName::StandardSettings) / "WinGet_SecureSettings_Tests"); + Runtime::TestHook_SetPathOverride(Runtime::PathName::SecureSettingsForWrite, Runtime::GetPathDetailsFor(Runtime::PathName::SecureSettingsForRead)); + } } diff --git a/src/AppInstallerCLITests/TestCommon.h b/src/AppInstallerCLITests/TestCommon.h index c6e719362b..5866068eed 100644 --- a/src/AppInstallerCLITests/TestCommon.h +++ b/src/AppInstallerCLITests/TestCommon.h @@ -153,4 +153,7 @@ namespace TestCommon // Convert to Json::Value Json::Value ConvertToJson(const std::string& content); + + // Sets up the test path overrides. + void SetTestPathOverrides(); } diff --git a/src/AppInstallerCLITests/main.cpp b/src/AppInstallerCLITests/main.cpp index 2ae3396f67..d38da19342 100644 --- a/src/AppInstallerCLITests/main.cpp +++ b/src/AppInstallerCLITests/main.cpp @@ -13,7 +13,6 @@ #include #include "TestCommon.h" -#include "TestHooks.h" #include "TestSettings.h" using namespace winrt; @@ -151,13 +150,7 @@ int main(int argc, char** argv) // Forcibly enable event writing to catch bugs in that code g_IsTelemetryProviderEnabled = true; - // Force all tests to run against settings inside this container. - // This prevents test runs from trashing the users actual settings. - Runtime::TestHook_SetPathOverride(Runtime::PathName::LocalState, Runtime::GetPathTo(Runtime::PathName::LocalState) / "Tests"); - Runtime::TestHook_SetPathOverride(Runtime::PathName::UserFileSettings, Runtime::GetPathTo(Runtime::PathName::UserFileSettings) / "Tests"); - Runtime::TestHook_SetPathOverride(Runtime::PathName::StandardSettings, Runtime::GetPathTo(Runtime::PathName::StandardSettings) / "Tests"); - Runtime::TestHook_SetPathOverride(Runtime::PathName::SecureSettingsForRead, Runtime::GetPathTo(Runtime::PathName::StandardSettings) / "WinGet_SecureSettings_Tests"); - Runtime::TestHook_SetPathOverride(Runtime::PathName::SecureSettingsForWrite, Runtime::GetPathDetailsFor(Runtime::PathName::SecureSettingsForRead)); + TestCommon::SetTestPathOverrides(); // Remove any existing settings files in the new tests path TestCommon::UserSettingsFileGuard settingsGuard; diff --git a/src/AppInstallerCLITests/pch.h b/src/AppInstallerCLITests/pch.h index 328fa5c20f..fdceff3fea 100644 --- a/src/AppInstallerCLITests/pch.h +++ b/src/AppInstallerCLITests/pch.h @@ -9,6 +9,7 @@ #include #include #include +#include #include diff --git a/src/AppInstallerCommonCore/Public/AppInstallerRuntime.h b/src/AppInstallerCommonCore/Public/AppInstallerRuntime.h index b7b2d8d10e..23eb8b567f 100644 --- a/src/AppInstallerCommonCore/Public/AppInstallerRuntime.h +++ b/src/AppInstallerCommonCore/Public/AppInstallerRuntime.h @@ -62,6 +62,8 @@ namespace AppInstaller::Runtime PortableLinksMachineLocation, // The root location for the package containing the winget application. SelfPackageRoot, + // Always one more than the last path; for being able to iterate paths in tests. + Max }; // The principal that an ACE applies to.