/ src / common / UnitTests-CommonUtils / MsiUtils.Tests.cpp
MsiUtils.Tests.cpp
  1  #include "pch.h"
  2  #include "TestHelpers.h"
  3  #include <MsiUtils.h>
  4  
  5  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  6  
  7  namespace UnitTestsCommonUtils
  8  {
  9      TEST_CLASS(MsiUtilsTests)
 10      {
 11      public:
 12          // GetMsiPackageInstalledPath tests
 13          TEST_METHOD(GetMsiPackageInstalledPath_PerUser_DoesNotCrash)
 14          {
 15              auto result = GetMsiPackageInstalledPath(true);
 16              // Result depends on installation state, but should not crash
 17              Assert::IsTrue(true);
 18          }
 19  
 20          TEST_METHOD(GetMsiPackageInstalledPath_PerMachine_DoesNotCrash)
 21          {
 22              auto result = GetMsiPackageInstalledPath(false);
 23              // Result depends on installation state, but should not crash
 24              Assert::IsTrue(true);
 25          }
 26  
 27          TEST_METHOD(GetMsiPackageInstalledPath_ConsistentResults)
 28          {
 29              auto result1 = GetMsiPackageInstalledPath(true);
 30              auto result2 = GetMsiPackageInstalledPath(true);
 31  
 32              // Results should be consistent
 33              Assert::AreEqual(result1.has_value(), result2.has_value());
 34              if (result1.has_value() && result2.has_value())
 35              {
 36                  Assert::AreEqual(*result1, *result2);
 37              }
 38          }
 39  
 40          TEST_METHOD(GetMsiPackageInstalledPath_PerUserVsPerMachine_MayDiffer)
 41          {
 42              auto perUser = GetMsiPackageInstalledPath(true);
 43              auto perMachine = GetMsiPackageInstalledPath(false);
 44  
 45              // These may or may not be equal depending on installation
 46              // Just verify they don't crash
 47              Assert::IsTrue(true);
 48          }
 49  
 50          // GetMsiPackagePath tests
 51          TEST_METHOD(GetMsiPackagePath_DoesNotCrash)
 52          {
 53              auto result = GetMsiPackagePath();
 54              // Result depends on installation state, but should not crash
 55              Assert::IsTrue(true);
 56          }
 57  
 58          TEST_METHOD(GetMsiPackagePath_ConsistentResults)
 59          {
 60              auto result1 = GetMsiPackagePath();
 61              auto result2 = GetMsiPackagePath();
 62  
 63              // Results should be consistent
 64              Assert::AreEqual(result1, result2);
 65          }
 66  
 67          // Thread safety tests
 68          TEST_METHOD(GetMsiPackageInstalledPath_ThreadSafe)
 69          {
 70              std::vector<std::thread> threads;
 71              std::atomic<int> successCount{ 0 };
 72  
 73              for (int i = 0; i < 5; ++i)
 74              {
 75                  threads.emplace_back([&successCount]() {
 76                      for (int j = 0; j < 5; ++j)
 77                      {
 78                          GetMsiPackageInstalledPath(j % 2 == 0);
 79                          successCount++;
 80                      }
 81                  });
 82              }
 83  
 84              for (auto& t : threads)
 85              {
 86                  t.join();
 87              }
 88  
 89              Assert::AreEqual(25, successCount.load());
 90          }
 91  
 92          TEST_METHOD(GetMsiPackagePath_ThreadSafe)
 93          {
 94              std::vector<std::thread> threads;
 95              std::atomic<int> successCount{ 0 };
 96  
 97              for (int i = 0; i < 5; ++i)
 98              {
 99                  threads.emplace_back([&successCount]() {
100                      for (int j = 0; j < 5; ++j)
101                      {
102                          GetMsiPackagePath();
103                          successCount++;
104                      }
105                  });
106              }
107  
108              for (auto& t : threads)
109              {
110                  t.join();
111              }
112  
113              Assert::AreEqual(25, successCount.load());
114          }
115  
116          // Return value format tests
117          TEST_METHOD(GetMsiPackageInstalledPath_ReturnsValidPathOrEmpty)
118          {
119              auto path = GetMsiPackageInstalledPath(true);
120  
121              if (path.has_value() && !path->empty())
122              {
123                  // If a path is returned, it should contain backslash or be a valid path format
124                  Assert::IsTrue(path->find(L'\\') != std::wstring::npos ||
125                                path->find(L'/') != std::wstring::npos ||
126                                path->length() >= 2); // At minimum drive letter + colon
127              }
128              // No value or empty is also valid (not installed)
129              Assert::IsTrue(true);
130          }
131  
132          TEST_METHOD(GetMsiPackagePath_ReturnsValidPathOrEmpty)
133          {
134              auto path = GetMsiPackagePath();
135  
136              if (!path.empty())
137              {
138                  // If a path is returned, it should be a valid path format
139                  Assert::IsTrue(path.find(L'\\') != std::wstring::npos ||
140                                path.find(L'/') != std::wstring::npos ||
141                                path.length() >= 2);
142              }
143              Assert::IsTrue(true);
144          }
145      };
146  }