/ src / common / UnitTests-CommonUtils / ProcessPath.Tests.cpp
ProcessPath.Tests.cpp
  1  #include "pch.h"
  2  #include "TestHelpers.h"
  3  #include <process_path.h>
  4  
  5  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  6  
  7  namespace UnitTestsCommonUtils
  8  {
  9      TEST_CLASS(ProcessPathTests)
 10      {
 11      public:
 12          // get_process_path (by PID) tests
 13          TEST_METHOD(GetProcessPath_CurrentProcess_ReturnsPath)
 14          {
 15              DWORD pid = GetCurrentProcessId();
 16              auto path = get_process_path(pid);
 17  
 18              Assert::IsFalse(path.empty());
 19              Assert::IsTrue(path.find(L".exe") != std::wstring::npos ||
 20                            path.find(L".dll") != std::wstring::npos);
 21          }
 22  
 23          TEST_METHOD(GetProcessPath_InvalidPid_ReturnsEmpty)
 24          {
 25              DWORD invalidPid = 0xFFFFFFFF;
 26              auto path = get_process_path(invalidPid);
 27  
 28              // Should return empty for invalid PID
 29              Assert::IsTrue(path.empty());
 30          }
 31  
 32          TEST_METHOD(GetProcessPath_ZeroPid_ReturnsEmpty)
 33          {
 34              auto path = get_process_path(static_cast<DWORD>(0));
 35              // PID 0 is the System Idle Process, might return empty or a path
 36              // Just verify it doesn't crash
 37              Assert::IsTrue(true);
 38          }
 39  
 40          TEST_METHOD(GetProcessPath_SystemPid_DoesNotCrash)
 41          {
 42              // PID 4 is typically the System process
 43              auto path = get_process_path(static_cast<DWORD>(4));
 44              // May return empty due to access rights, but shouldn't crash
 45              Assert::IsTrue(true);
 46          }
 47  
 48          // get_module_filename tests
 49          TEST_METHOD(GetModuleFilename_NullModule_ReturnsExePath)
 50          {
 51              auto path = get_module_filename(nullptr);
 52  
 53              Assert::IsFalse(path.empty());
 54              Assert::IsTrue(path.find(L".exe") != std::wstring::npos ||
 55                            path.find(L".dll") != std::wstring::npos);
 56          }
 57  
 58          TEST_METHOD(GetModuleFilename_Kernel32_ReturnsPath)
 59          {
 60              HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
 61              Assert::IsNotNull(kernel32);
 62  
 63              auto path = get_module_filename(kernel32);
 64  
 65              Assert::IsFalse(path.empty());
 66              // Should contain kernel32 (case insensitive check)
 67              std::wstring lowerPath = path;
 68              std::transform(lowerPath.begin(), lowerPath.end(), lowerPath.begin(), ::towlower);
 69              Assert::IsTrue(lowerPath.find(L"kernel32") != std::wstring::npos);
 70          }
 71  
 72          TEST_METHOD(GetModuleFilename_InvalidModule_ReturnsEmpty)
 73          {
 74              auto path = get_module_filename(reinterpret_cast<HMODULE>(0x12345678));
 75              // Invalid module should return empty
 76              Assert::IsTrue(path.empty());
 77          }
 78  
 79          // get_module_folderpath tests
 80          TEST_METHOD(GetModuleFolderpath_NullModule_ReturnsFolder)
 81          {
 82              auto folder = get_module_folderpath(nullptr, true);
 83  
 84              Assert::IsFalse(folder.empty());
 85              // Should not end with .exe when removeFilename is true
 86              Assert::IsTrue(folder.find(L".exe") == std::wstring::npos);
 87              // Should end with backslash or be a valid folder path
 88              Assert::IsTrue(folder.back() == L'\\' || folder.find(L"\\") != std::wstring::npos);
 89          }
 90  
 91          TEST_METHOD(GetModuleFolderpath_KeepFilename_ReturnsFullPath)
 92          {
 93              auto fullPath = get_module_folderpath(nullptr, false);
 94  
 95              Assert::IsFalse(fullPath.empty());
 96              // Should contain .exe or .dll when not removing filename
 97              Assert::IsTrue(fullPath.find(L".exe") != std::wstring::npos ||
 98                            fullPath.find(L".dll") != std::wstring::npos);
 99          }
100  
101          TEST_METHOD(GetModuleFolderpath_Kernel32_ReturnsSystem32)
102          {
103              HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
104              Assert::IsNotNull(kernel32);
105  
106              auto folder = get_module_folderpath(kernel32, true);
107  
108              Assert::IsFalse(folder.empty());
109              // Should be in system32 folder
110              std::wstring lowerPath = folder;
111              std::transform(lowerPath.begin(), lowerPath.end(), lowerPath.begin(), ::towlower);
112              Assert::IsTrue(lowerPath.find(L"system32") != std::wstring::npos ||
113                            lowerPath.find(L"syswow64") != std::wstring::npos);
114          }
115  
116          // get_process_path (by HWND) tests
117          TEST_METHOD(GetProcessPath_DesktopWindow_ReturnsPath)
118          {
119              HWND desktop = GetDesktopWindow();
120              Assert::IsNotNull(desktop);
121  
122              auto path = get_process_path(desktop);
123              // Desktop window should return a path
124              // (could be explorer.exe or empty depending on system)
125              Assert::IsTrue(true); // Just verify it doesn't crash
126          }
127  
128          TEST_METHOD(GetProcessPath_InvalidHwnd_ReturnsEmpty)
129          {
130              auto path = get_process_path(reinterpret_cast<HWND>(0x12345678));
131              Assert::IsTrue(path.empty());
132          }
133  
134          TEST_METHOD(GetProcessPath_NullHwnd_ReturnsEmpty)
135          {
136              auto path = get_process_path(static_cast<HWND>(nullptr));
137              Assert::IsTrue(path.empty());
138          }
139  
140          // Consistency tests
141          TEST_METHOD(Consistency_ModuleFilenameAndFolderpath_AreRelated)
142          {
143              auto fullPath = get_module_filename(nullptr);
144              auto folder = get_module_folderpath(nullptr, true);
145  
146              Assert::IsFalse(fullPath.empty());
147              Assert::IsFalse(folder.empty());
148  
149              // Full path should start with the folder
150              Assert::IsTrue(fullPath.find(folder) == 0 || folder.find(fullPath.substr(0, folder.length())) == 0);
151          }
152      };
153  }