MsixInfo.cpp (3755B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "TestCommon.h" 5 #include <AppInstallerMsixInfo.h> 6 #include <AppInstallerDownloader.h> 7 #include <AppInstallerRuntime.h> 8 9 using namespace std::string_literals; 10 using namespace std::string_view_literals; 11 using namespace TestCommon; 12 using namespace AppInstaller; 13 14 constexpr std::string_view s_MsixFile_1 = "index.1.0.0.0.msix"; 15 constexpr std::string_view s_MsixFile_2 = "index.2.0.0.0.msix"; 16 constexpr std::string_view s_MsixFileSigned_1 = "index.1.0.0.0.signed.msix"; 17 18 TEST_CASE("MsixInfo_GetPackageFullName", "[msixinfo]") 19 { 20 TestDataFile index(s_MsixFile_1); 21 Msix::MsixInfo msix(index.GetPath()); 22 23 std::string expectedFullName = "AppInstallerCLITestsFakeIndex_1.0.0.0_neutral__125rzkzqaqjwj"; 24 std::string actualFullName = msix.GetPackageFullName(); 25 26 REQUIRE(expectedFullName == actualFullName); 27 } 28 29 TEST_CASE("MsixInfo_CompareToSelf", "[msixinfo]") 30 { 31 TestDataFile index(s_MsixFile_1); 32 Msix::MsixInfo msix(index.GetPath()); 33 34 REQUIRE(!msix.IsNewerThan(index.GetPath().u8string())); 35 } 36 37 TEST_CASE("MsixInfo_CompareToOlder", "[msixinfo]") 38 { 39 TestDataFile index1(s_MsixFile_1); 40 TestDataFile index2(s_MsixFile_2); 41 Msix::MsixInfo msix2(index2.GetPath()); 42 43 REQUIRE(msix2.IsNewerThan(index1)); 44 } 45 46 TEST_CASE("MsixInfo_WriteFile", "[msixinfo]") 47 { 48 TestDataFile index(s_MsixFile_1); 49 Msix::MsixInfo msix(index.GetPath()); 50 51 TempFile file{ "msixtest_file"s, ".bin"s }; 52 ProgressCallback callback; 53 54 msix.WriteToFile("Public\\index.db", file, callback); 55 56 REQUIRE(1 == std::filesystem::file_size(file)); 57 } 58 59 TEST_CASE("MsixInfo_ValidateMsixTrustInfo", "[msixinfo]") 60 { 61 if (!Runtime::IsRunningAsAdmin()) 62 { 63 WARN("Test requires admin privilege. Skipped."); 64 return; 65 } 66 67 TestDataFile notSigned{ s_MsixFile_1 }; 68 Msix::WriteLockedMsixFile notSignedWriteLocked{ notSigned }; 69 REQUIRE_FALSE(notSignedWriteLocked.ValidateTrustInfo(false)); 70 71 TestDataFile testSigned{ s_MsixFileSigned_1 }; 72 Msix::WriteLockedMsixFile testSignedWriteLocked{ testSigned }; 73 74 // Remove the cert if already trusted 75 bool certExistsBeforeTest = UninstallCertFromSignedPackage(testSigned); 76 77 REQUIRE_FALSE(testSignedWriteLocked.ValidateTrustInfo(false)); 78 79 // Add the cert to trusted 80 InstallCertFromSignedPackage(testSigned); 81 82 REQUIRE(testSignedWriteLocked.ValidateTrustInfo(false)); 83 REQUIRE_FALSE(testSignedWriteLocked.ValidateTrustInfo(true)); 84 85 TestCommon::TempFile microsoftSigned{ "testIndex"s, ".msix"s }; 86 ProgressCallback callback; 87 Utility::Download("https://cdn.winget.microsoft.com/cache/source2.msix", microsoftSigned.GetPath(), Utility::DownloadType::Index, callback); 88 89 Msix::WriteLockedMsixFile microsoftSignedWriteLocked{ microsoftSigned }; 90 REQUIRE(microsoftSignedWriteLocked.ValidateTrustInfo(true)); 91 92 if (!certExistsBeforeTest) 93 { 94 UninstallCertFromSignedPackage(testSigned); 95 } 96 } 97 98 TEST_CASE("MsixInfo_GetPackageIdInfoFromFullName", "[msixinfo]") 99 { 100 auto testPackageIdInfo = Msix::GetPackageIdInfoFromFullName("Microsoft.NET.Native.Framework.2.2_2.2.29512.0_arm64__8wekyb3d8bbwe"); 101 REQUIRE(testPackageIdInfo.Name == "Microsoft.NET.Native.Framework.2.2"); 102 REQUIRE(testPackageIdInfo.Version == Utility::UInt64Version{ "2.2.29512.0" }); 103 104 auto testPackageIdInfo2 = Msix::GetPackageIdInfoFromFullName("Microsoft.DoesNotExist_1.2.3.4_neutral_~_8wekyb3d8bbwe"); 105 REQUIRE(testPackageIdInfo2.Name == "Microsoft.DoesNotExist"); 106 REQUIRE(testPackageIdInfo2.Version == Utility::UInt64Version{ "1.2.3.4" }); 107 }