/ src / common / UnitTests-CommonUtils / ExcludedApps.Tests.cpp
ExcludedApps.Tests.cpp
  1  #include "pch.h"
  2  #include "TestHelpers.h"
  3  #include <excluded_apps.h>
  4  
  5  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  6  
  7  namespace UnitTestsCommonUtils
  8  {
  9      TEST_CLASS(ExcludedAppsTests)
 10      {
 11      public:
 12          // find_app_name_in_path tests
 13          TEST_METHOD(FindAppNameInPath_ExactMatch_ReturnsTrue)
 14          {
 15              std::wstring path = L"C:\\Program Files\\App\\notepad.exe";
 16              std::vector<std::wstring> apps = { L"notepad.exe" };
 17              Assert::IsTrue(find_app_name_in_path(path, apps));
 18          }
 19  
 20          TEST_METHOD(FindAppNameInPath_NoMatch_ReturnsFalse)
 21          {
 22              std::wstring path = L"C:\\Program Files\\App\\notepad.exe";
 23              std::vector<std::wstring> apps = { L"calc.exe" };
 24              Assert::IsFalse(find_app_name_in_path(path, apps));
 25          }
 26  
 27          TEST_METHOD(FindAppNameInPath_MultipleApps_FindsMatch)
 28          {
 29              std::wstring path = L"C:\\Program Files\\App\\notepad.exe";
 30              std::vector<std::wstring> apps = { L"calc.exe", L"notepad.exe", L"word.exe" };
 31              Assert::IsTrue(find_app_name_in_path(path, apps));
 32          }
 33  
 34          TEST_METHOD(FindAppNameInPath_EmptyPath_ReturnsFalse)
 35          {
 36              std::wstring path = L"";
 37              std::vector<std::wstring> apps = { L"notepad.exe" };
 38              Assert::IsFalse(find_app_name_in_path(path, apps));
 39          }
 40  
 41          TEST_METHOD(FindAppNameInPath_EmptyApps_ReturnsFalse)
 42          {
 43              std::wstring path = L"C:\\Program Files\\App\\notepad.exe";
 44              std::vector<std::wstring> apps = {};
 45              Assert::IsFalse(find_app_name_in_path(path, apps));
 46          }
 47  
 48          TEST_METHOD(FindAppNameInPath_PartialMatchInFolder_ReturnsFalse)
 49          {
 50              // "notepad" appears in folder name but not as the exe name
 51              std::wstring path = L"C:\\notepad\\other.exe";
 52              std::vector<std::wstring> apps = { L"notepad.exe" };
 53              Assert::IsFalse(find_app_name_in_path(path, apps));
 54          }
 55  
 56          TEST_METHOD(FindAppNameInPath_CaseSensitive_ReturnsFalse)
 57          {
 58              std::wstring path = L"C:\\Program Files\\App\\NOTEPAD.EXE";
 59              std::vector<std::wstring> apps = { L"notepad.exe" };
 60              // The function does rfind which is case-sensitive
 61              Assert::IsFalse(find_app_name_in_path(path, apps));
 62          }
 63  
 64          TEST_METHOD(FindAppNameInPath_MatchWithDifferentExtension_ReturnsFalse)
 65          {
 66              std::wstring path = L"C:\\Program Files\\App\\notepad.com";
 67              std::vector<std::wstring> apps = { L"notepad.exe" };
 68              Assert::IsFalse(find_app_name_in_path(path, apps));
 69          }
 70  
 71          TEST_METHOD(FindAppNameInPath_MatchAtEndOfPath_ReturnsTrue)
 72          {
 73              std::wstring path = L"C:\\Windows\\System32\\notepad.exe";
 74              std::vector<std::wstring> apps = { L"notepad.exe" };
 75              Assert::IsTrue(find_app_name_in_path(path, apps));
 76          }
 77  
 78          TEST_METHOD(FindAppNameInPath_UNCPath_Works)
 79          {
 80              std::wstring path = L"\\\\server\\share\\folder\\app.exe";
 81              std::vector<std::wstring> apps = { L"app.exe" };
 82              Assert::IsTrue(find_app_name_in_path(path, apps));
 83          }
 84  
 85          // find_folder_in_path tests
 86          TEST_METHOD(FindFolderInPath_FolderExists_ReturnsTrue)
 87          {
 88              std::wstring path = L"C:\\Program Files\\MyApp\\app.exe";
 89              std::vector<std::wstring> folders = { L"Program Files" };
 90              Assert::IsTrue(find_folder_in_path(path, folders));
 91          }
 92  
 93          TEST_METHOD(FindFolderInPath_FolderNotExists_ReturnsFalse)
 94          {
 95              std::wstring path = L"C:\\Windows\\System32\\app.exe";
 96              std::vector<std::wstring> folders = { L"Program Files" };
 97              Assert::IsFalse(find_folder_in_path(path, folders));
 98          }
 99  
100          TEST_METHOD(FindFolderInPath_MultipleFolders_FindsMatch)
101          {
102              std::wstring path = L"C:\\Windows\\System32\\app.exe";
103              std::vector<std::wstring> folders = { L"Program Files", L"System32", L"Users" };
104              Assert::IsTrue(find_folder_in_path(path, folders));
105          }
106  
107          TEST_METHOD(FindFolderInPath_EmptyPath_ReturnsFalse)
108          {
109              std::wstring path = L"";
110              std::vector<std::wstring> folders = { L"Windows" };
111              Assert::IsFalse(find_folder_in_path(path, folders));
112          }
113  
114          TEST_METHOD(FindFolderInPath_EmptyFolders_ReturnsFalse)
115          {
116              std::wstring path = L"C:\\Windows\\app.exe";
117              std::vector<std::wstring> folders = {};
118              Assert::IsFalse(find_folder_in_path(path, folders));
119          }
120  
121          TEST_METHOD(FindFolderInPath_PartialMatch_ReturnsTrue)
122          {
123              // find_folder_in_path uses rfind which finds substrings
124              std::wstring path = L"C:\\Windows\\System32\\app.exe";
125              std::vector<std::wstring> folders = { L"System" };
126              Assert::IsTrue(find_folder_in_path(path, folders));
127          }
128  
129          TEST_METHOD(FindFolderInPath_NestedFolder_ReturnsTrue)
130          {
131              std::wstring path = L"C:\\Program Files\\Company\\Product\\bin\\app.exe";
132              std::vector<std::wstring> folders = { L"Product" };
133              Assert::IsTrue(find_folder_in_path(path, folders));
134          }
135  
136          TEST_METHOD(FindFolderInPath_RootDrive_ReturnsTrue)
137          {
138              std::wstring path = L"C:\\folder\\app.exe";
139              std::vector<std::wstring> folders = { L"C:\\" };
140              Assert::IsTrue(find_folder_in_path(path, folders));
141          }
142  
143          TEST_METHOD(FindFolderInPath_UNCPath_Works)
144          {
145              std::wstring path = L"\\\\server\\share\\folder\\app.exe";
146              std::vector<std::wstring> folders = { L"share" };
147              Assert::IsTrue(find_folder_in_path(path, folders));
148          }
149  
150          TEST_METHOD(FindFolderInPath_CaseSensitive_ReturnsFalse)
151          {
152              std::wstring path = L"C:\\WINDOWS\\app.exe";
153              std::vector<std::wstring> folders = { L"windows" };
154              // rfind is case-sensitive
155              Assert::IsFalse(find_folder_in_path(path, folders));
156          }
157  
158          // Edge case tests
159          TEST_METHOD(FindAppNameInPath_AppNameInMiddleOfPath_HandlesCorrectly)
160          {
161              // The app name appears both in folder and as filename
162              std::wstring path = L"C:\\notepad\\bin\\notepad.exe";
163              std::vector<std::wstring> apps = { L"notepad.exe" };
164              Assert::IsTrue(find_app_name_in_path(path, apps));
165          }
166  
167          TEST_METHOD(FindAppNameInPath_JustFilename_ReturnsFalse)
168          {
169              std::wstring path = L"notepad.exe";
170              std::vector<std::wstring> apps = { L"notepad.exe" };
171              // find_app_name_in_path expects a path separator to validate the executable segment
172              Assert::IsFalse(find_app_name_in_path(path, apps));
173          }
174  
175          TEST_METHOD(FindFolderInPath_JustFilename_ReturnsFalse)
176          {
177              std::wstring path = L"app.exe";
178              std::vector<std::wstring> folders = { L"Windows" };
179              Assert::IsFalse(find_folder_in_path(path, folders));
180          }
181      };
182  }