From 9d1ffa2d0603a5ae399bd019f426925c7baf1f21 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 22 Jun 2018 09:38:21 -0700 Subject: [PATCH] Use guid for temp directories and delete them after tests (#963) --- test/CommonLibTests/CommonLibTests.vcxproj | 8 +- .../CommonLibTests/FileOutputManagerTests.cpp | 28 ++---- test/CommonLibTests/GlobalVersionTests.cpp | 96 +++++++++---------- test/CommonLibTests/Helpers.cpp | 49 ++++------ test/CommonLibTests/Helpers.h | 29 +++--- test/CommonLibTests/main.cpp | 6 -- 6 files changed, 99 insertions(+), 117 deletions(-) diff --git a/test/CommonLibTests/CommonLibTests.vcxproj b/test/CommonLibTests/CommonLibTests.vcxproj index 6844054804..5117512c47 100644 --- a/test/CommonLibTests/CommonLibTests.vcxproj +++ b/test/CommonLibTests/CommonLibTests.vcxproj @@ -101,7 +101,7 @@ true Console ..\..\src\AspNetCoreModuleV2\InProcessRequestHandler\$(Configuration)\; - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;inprocessapplication.obj;inprocesshandler.obj;ahadmin.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;inprocessapplication.obj;inprocesshandler.obj;ahadmin.lib;Rpcrt4.lib;%(AdditionalDependencies) @@ -127,7 +127,7 @@ true Console ..\..\src\AspNetCoreModuleV2\InProcessRequestHandler\x64\$(Configuration)\; - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;inprocessapplication.obj;inprocesshandler.obj;ahadmin.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;inprocessapplication.obj;inprocesshandler.obj;ahadmin.lib;Rpcrt4.lib;%(AdditionalDependencies) @@ -153,7 +153,7 @@ true true ..\..\src\AspNetCoreModuleV2\InProcessRequestHandler\$(Configuration)\; - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;inprocessapplication.obj;inprocesshandler.obj;ahadmin.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;inprocessapplication.obj;inprocesshandler.obj;ahadmin.lib;Rpcrt4.lib;%(AdditionalDependencies) @@ -179,7 +179,7 @@ true true ..\..\src\AspNetCoreModuleV2\InProcessRequestHandler\x64\$(Configuration)\; - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;inprocessapplication.obj;inprocesshandler.obj;ahadmin.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;inprocessapplication.obj;inprocesshandler.obj;ahadmin.lib;Rpcrt4.lib;%(AdditionalDependencies) diff --git a/test/CommonLibTests/FileOutputManagerTests.cpp b/test/CommonLibTests/FileOutputManagerTests.cpp index ee3a446633..93557c73a3 100644 --- a/test/CommonLibTests/FileOutputManagerTests.cpp +++ b/test/CommonLibTests/FileOutputManagerTests.cpp @@ -31,16 +31,16 @@ namespace FileOutManagerStartupTests { PCWSTR expected = L"test"; - std::wstring tempDirectory = Helpers::CreateRandomTempDirectory(); + auto tempDirectory = TempDirectory(); FileOutputManager* pManager = new FileOutputManager; - pManager->Initialize(fileNamePrefix.c_str(), tempDirectory.c_str()); + pManager->Initialize(fileNamePrefix.c_str(), tempDirectory.path().c_str()); { FileManagerWrapper wrapper(pManager); wprintf(expected, out); } - - for (auto & p : std::experimental::filesystem::directory_iterator(tempDirectory)) + + for (auto & p : std::experimental::filesystem::directory_iterator(tempDirectory.path())) { std::wstring filename(p.path().filename()); ASSERT_EQ(filename.substr(0, fileNamePrefix.size()), fileNamePrefix); @@ -49,8 +49,6 @@ namespace FileOutManagerStartupTests ASSERT_EQ(content.length(), DWORD(4)); ASSERT_STREQ(content.c_str(), expected); } - - Helpers::DeleteDirectory(tempDirectory); } }; @@ -74,10 +72,10 @@ namespace FileOutManagerOutputTests { PCSTR expected = "test"; - std::wstring tempDirectory = Helpers::CreateRandomTempDirectory(); + auto tempDirectory = TempDirectory(); FileOutputManager* pManager = new FileOutputManager; - pManager->Initialize(L"", tempDirectory.c_str()); + pManager->Initialize(L"", tempDirectory.path().c_str()); { FileManagerWrapper wrapper(pManager); @@ -87,18 +85,16 @@ namespace FileOutManagerOutputTests ASSERT_STREQ(straContent.QueryStr(), expected); } - - Helpers::DeleteDirectory(tempDirectory); } TEST(FileOutManagerOutputTest, DISABLED_CheckFileOutput) { PCSTR expected = "test"; - std::wstring tempDirectory = Helpers::CreateRandomTempDirectory(); + auto tempDirectory = TempDirectory(); FileOutputManager* pManager = new FileOutputManager; - pManager->Initialize(L"", tempDirectory.c_str()); + pManager->Initialize(L"", tempDirectory.path().c_str()); { FileManagerWrapper wrapper(pManager); @@ -108,18 +104,16 @@ namespace FileOutManagerOutputTests ASSERT_STREQ(straContent.QueryStr(), expected); } - - Helpers::DeleteDirectory(tempDirectory); } TEST(FileOutManagerOutputTest, DISABLED_CapAt4KB) { PCSTR expected = "test"; - std::wstring tempDirectory = Helpers::CreateRandomTempDirectory(); + auto tempDirectory = TempDirectory(); FileOutputManager* pManager = new FileOutputManager; - pManager->Initialize(L"", tempDirectory.c_str()); + pManager->Initialize(L"", tempDirectory.path().c_str()); { FileManagerWrapper wrapper(pManager); @@ -133,8 +127,6 @@ namespace FileOutManagerOutputTests ASSERT_EQ(straContent.QueryCCH(), 4096); } - - Helpers::DeleteDirectory(tempDirectory); } } diff --git a/test/CommonLibTests/GlobalVersionTests.cpp b/test/CommonLibTests/GlobalVersionTests.cpp index 00ec23f479..9c74b6720e 100644 --- a/test/CommonLibTests/GlobalVersionTests.cpp +++ b/test/CommonLibTests/GlobalVersionTests.cpp @@ -30,32 +30,32 @@ namespace GlobalVersionTests TEST(GetRequestHandlerVersions, GetFolders) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / L"2.0.0")); - auto res = GlobalVersionUtility::GetRequestHandlerVersions(tempPath.c_str()); + auto res = GlobalVersionUtility::GetRequestHandlerVersions(tempPath.path().c_str()); EXPECT_EQ(res.size(), 1); EXPECT_EQ(res.at(0), fx_ver_t(2, 0, 0, std::wstring())); } TEST(GetRequestHandlerVersions, GetFolderPreview) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0-preview")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / L"2.0.0-preview")); - auto res = GlobalVersionUtility::GetRequestHandlerVersions(tempPath.c_str()); + auto res = GlobalVersionUtility::GetRequestHandlerVersions(tempPath.path().c_str()); EXPECT_EQ(res.size(), 1); EXPECT_EQ(res.at(0), fx_ver_t(2, 0, 0, std::wstring(L"-preview"))); } TEST(GetRequestHandlerVersions, GetFolderManyVersions) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\1.9.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.1.0")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / + L"2.0.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / + L"1.9.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / + L"2.1.0")); - auto res = GlobalVersionUtility::GetRequestHandlerVersions(tempPath.c_str()); + auto res = GlobalVersionUtility::GetRequestHandlerVersions(tempPath.path().c_str()); EXPECT_EQ(res.size(), 3); EXPECT_TRUE(std::find(res.begin(), res.end(), fx_ver_t(1, 9, 0, std::wstring())) != std::end(res)); EXPECT_TRUE(std::find(res.begin(), res.end(), fx_ver_t(2, 0, 0, std::wstring())) != std::end(res)); @@ -64,90 +64,90 @@ namespace GlobalVersionTests TEST(FindHighestGlobalVersion, HighestVersionWithSingleFolder) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.0.0")); - auto res = GlobalVersionUtility::FindHighestGlobalVersion(tempPath.c_str()); + auto res = GlobalVersionUtility::FindHighestGlobalVersion(tempPath.path().c_str()); EXPECT_STREQ(res.c_str(), L"2.0.0"); } TEST(FindHighestGlobalVersion, HighestVersionWithMultipleVersions) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.1.0")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.0.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.1.0")); - auto res = GlobalVersionUtility::FindHighestGlobalVersion(tempPath.c_str()); + auto res = GlobalVersionUtility::FindHighestGlobalVersion(tempPath.path().c_str()); EXPECT_STREQ(res.c_str(), L"2.1.0"); } TEST(FindHighestGlobalVersion, HighestVersionWithMultipleVersionsPreview) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.1.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.2.0-preview")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.0.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.1.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.2.0-preview")); - auto res = GlobalVersionUtility::FindHighestGlobalVersion(tempPath.c_str()); + auto res = GlobalVersionUtility::FindHighestGlobalVersion(tempPath.path().c_str()); EXPECT_STREQ(res.c_str(), L"2.2.0-preview"); } TEST(FindHighestGlobalVersion, HighestVersionWithMultipleVersionNoPreview) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.1.0-preview")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.1.0")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.0.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.1.0-preview")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.1.0")); - auto res = GlobalVersionUtility::FindHighestGlobalVersion(tempPath.c_str()); + auto res = GlobalVersionUtility::FindHighestGlobalVersion(tempPath.path().c_str()); EXPECT_STREQ(res.c_str(), L"2.1.0"); } TEST(GetGlobalRequestHandlerPath, FindHighestVersionNoHandlerName) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); - auto result = GlobalVersionUtility::GetGlobalRequestHandlerPath(tempPath.c_str(), L"", L"aspnetcorev2_outofprocess.dll"); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.0.0")); + auto result = GlobalVersionUtility::GetGlobalRequestHandlerPath(tempPath.path().c_str(), L"", L"aspnetcorev2_outofprocess.dll"); - EXPECT_STREQ(result.c_str(), (tempPath + L"2.0.0\\aspnetcorev2_outofprocess.dll").c_str()); + EXPECT_STREQ(result.c_str(), (tempPath.path() / L"2.0.0\\aspnetcorev2_outofprocess.dll").c_str()); } TEST(GetGlobalRequestHandlerPath, FindHighestVersionPreviewWins) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.1.0-preview")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.0.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.1.0-preview")); - auto result = GlobalVersionUtility::GetGlobalRequestHandlerPath(tempPath.c_str(), L"", L"aspnetcorev2_outofprocess.dll"); + auto result = GlobalVersionUtility::GetGlobalRequestHandlerPath(tempPath.path().c_str(), L"", L"aspnetcorev2_outofprocess.dll"); - EXPECT_STREQ(result.c_str(), (tempPath + L"2.1.0-preview\\aspnetcorev2_outofprocess.dll").c_str()); + EXPECT_STREQ(result.c_str(), (tempPath.path() / L"2.1.0-preview\\aspnetcorev2_outofprocess.dll").c_str()); } TEST(GetGlobalRequestHandlerPath, FindHighestVersionSpecificVersion) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.1.0-preview")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.0.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.1.0-preview")); - auto result = GlobalVersionUtility::GetGlobalRequestHandlerPath(tempPath.c_str(), L"2.0.0", L"aspnetcorev2_outofprocess.dll"); + auto result = GlobalVersionUtility::GetGlobalRequestHandlerPath(tempPath.path().c_str(), L"2.0.0", L"aspnetcorev2_outofprocess.dll"); - EXPECT_STREQ(result.c_str(), (tempPath + L"2.0.0\\aspnetcorev2_outofprocess.dll").c_str()); + EXPECT_STREQ(result.c_str(), (tempPath.path() / L"2.0.0\\aspnetcorev2_outofprocess.dll").c_str()); } TEST(GetGlobalRequestHandlerPath, FindHighestVersionSpecificPreview) { - std::wstring tempPath = Helpers::CreateRandomTempDirectory(); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.0.0")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.1.0-preview")); - EXPECT_TRUE(fs::create_directories(tempPath + L"\\2.2.0")); + auto tempPath = TempDirectory(); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.0.0")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.1.0-preview")); + EXPECT_TRUE(fs::create_directories(tempPath.path() / "2.2.0")); - auto result = GlobalVersionUtility::GetGlobalRequestHandlerPath(tempPath.c_str(), L"2.1.0-preview", L"aspnetcorev2_outofprocess.dll"); + auto result = GlobalVersionUtility::GetGlobalRequestHandlerPath(tempPath.path().c_str(), L"2.1.0-preview", L"aspnetcorev2_outofprocess.dll"); - EXPECT_STREQ(result.c_str(), (tempPath + L"2.1.0-preview\\aspnetcorev2_outofprocess.dll").c_str()); + EXPECT_STREQ(result.c_str(), (tempPath.path() / L"2.1.0-preview\\aspnetcorev2_outofprocess.dll").c_str()); } } diff --git a/test/CommonLibTests/Helpers.cpp b/test/CommonLibTests/Helpers.cpp index 9ad7560c94..3e429a6665 100644 --- a/test/CommonLibTests/Helpers.cpp +++ b/test/CommonLibTests/Helpers.cpp @@ -3,29 +3,6 @@ #include "stdafx.h" -std::wstring -Helpers::CreateRandomValue() -{ - int randomValue = rand(); - return std::to_wstring(randomValue); -} - -std::wstring -Helpers::CreateRandomTempDirectory() -{ - PWSTR tempPath = new WCHAR[256]; - GetTempPath(256, tempPath); - std::wstring wstringPath(tempPath); - - return wstringPath.append(Helpers::CreateRandomValue()).append(L"\\"); -} - -void -Helpers::DeleteDirectory(std::wstring directory) -{ - std::experimental::filesystem::remove_all(directory); -} - std::wstring Helpers::ReadFileContent(std::wstring file) { @@ -37,12 +14,28 @@ Helpers::ReadFileContent(std::wstring file) int nChars = MultiByteToWideChar(CP_ACP, 0, buffer.str().c_str(), -1, NULL, 0); - LPWSTR pwzName = new WCHAR[nChars]; - MultiByteToWideChar(CP_UTF8, 0, buffer.str().c_str(), -1, pwzName, nChars); + std::wstring retVal(nChars, '\0'); - std::wstring retVal(pwzName); - - delete pwzName; + MultiByteToWideChar(CP_UTF8, 0, buffer.str().c_str(), -1, retVal.data(), nChars); return retVal; } + +TempDirectory::TempDirectory() +{ + UUID uuid; + UuidCreate(&uuid); + RPC_CSTR szUuid = NULL; + if (UuidToStringA(&uuid, &szUuid) == RPC_S_OK) + { + m_path = std::experimental::filesystem::temp_directory_path() / szUuid; + RpcStringFreeA(&szUuid); + return; + } + throw std::exception("Cannot create temp directory"); +} + +TempDirectory::~TempDirectory() +{ + std::experimental::filesystem::remove_all(m_path); +} diff --git a/test/CommonLibTests/Helpers.h b/test/CommonLibTests/Helpers.h index f9e06bbca4..657766dd43 100644 --- a/test/CommonLibTests/Helpers.h +++ b/test/CommonLibTests/Helpers.h @@ -5,21 +5,24 @@ class Helpers { public: - - static - std::wstring - CreateRandomValue(); - - static - std::wstring - CreateRandomTempDirectory(); - - static - void - DeleteDirectory(std::wstring directory); - static std::wstring ReadFileContent(std::wstring file); }; +class TempDirectory +{ +public: + + TempDirectory(); + + ~TempDirectory(); + + std::experimental::filesystem::path path() const + { + return m_path; + } + +private: + std::experimental::filesystem::path m_path; +}; diff --git a/test/CommonLibTests/main.cpp b/test/CommonLibTests/main.cpp index f4a91c733a..1ad0a10ccd 100644 --- a/test/CommonLibTests/main.cpp +++ b/test/CommonLibTests/main.cpp @@ -5,14 +5,8 @@ DECLARE_DEBUG_PRINT_OBJECT2("tests", ASPNETCORE_DEBUG_FLAG_INFO | ASPNETCORE_DEBUG_FLAG_CONSOLE); -#if defined(_WIN32) int wmain(int argc, wchar_t* argv[]) -#else -int main(int argc, char* argv[]) -#endif { - std::srand((unsigned int)std::time(0)); ::testing::InitGoogleTest(&argc, argv); RUN_ALL_TESTS(); - return 0; }