From 99d7440134d0f33683d1150a770a2bc594be41de Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Mon, 8 Mar 2021 16:06:01 -0600 Subject: Port dependency tests from old repo. --- src/WixTestTools/BundleInstaller.cs | 3 ++ src/WixTestTools/BundleRegistration.cs | 14 ++++++-- src/WixTestTools/BundleVerifier.cs | 65 ++++++++++++++++++++++++++++------ src/WixTestTools/MsiUtilities.cs | 20 +++++++++++ src/WixTestTools/PackageVerifier.cs | 7 ++++ src/WixTestTools/WixTestTools.csproj | 2 +- 6 files changed, 98 insertions(+), 13 deletions(-) (limited to 'src/WixTestTools') diff --git a/src/WixTestTools/BundleInstaller.cs b/src/WixTestTools/BundleInstaller.cs index 044486fe..854c12f0 100644 --- a/src/WixTestTools/BundleInstaller.cs +++ b/src/WixTestTools/BundleInstaller.cs @@ -12,12 +12,15 @@ namespace WixTestTools { this.Bundle = Path.Combine(testContext.TestDataFolder, $"{name}.exe"); this.BundlePdb = Path.Combine(testContext.TestDataFolder, $"{name}.wixpdb"); + this.TestContext = testContext; this.TestGroupName = testContext.TestGroupName; this.TestName = testContext.TestName; } public string Bundle { get; } + private WixTestContext TestContext { get; } + public string TestGroupName { get; } public string TestName { get; } diff --git a/src/WixTestTools/BundleRegistration.cs b/src/WixTestTools/BundleRegistration.cs index d473dcdd..1a066232 100644 --- a/src/WixTestTools/BundleRegistration.cs +++ b/src/WixTestTools/BundleRegistration.cs @@ -7,7 +7,8 @@ namespace WixTestTools public class BundleRegistration { - public const string BURN_REGISTRATION_REGISTRY_UNINSTALL_KEY = "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; + public const string BURN_REGISTRATION_REGISTRY_UNINSTALL_KEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; + public const string BURN_REGISTRATION_REGISTRY_UNINSTALL_KEY_WOW6432NODE = "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; public const string BURN_REGISTRATION_REGISTRY_BUNDLE_CACHE_PATH = "BundleCachePath"; public const string BURN_REGISTRATION_REGISTRY_BUNDLE_ADDON_CODE = "BundleAddonCode"; public const string BURN_REGISTRATION_REGISTRY_BUNDLE_DETECT_CODE = "BundleDetectCode"; @@ -89,13 +90,22 @@ namespace WixTestTools public static bool TryGetPerMachineBundleRegistrationById(string bundleId, out BundleRegistration registration) { - var registrationKeyPath = $"{BURN_REGISTRATION_REGISTRY_UNINSTALL_KEY}\\{bundleId}"; + var registrationKeyPath = $"{BURN_REGISTRATION_REGISTRY_UNINSTALL_KEY_WOW6432NODE}\\{bundleId}"; using var registrationKey = Registry.LocalMachine.OpenSubKey(registrationKeyPath); var success = registrationKey != null; registration = success ? GetBundleRegistration(registrationKey) : null; return success; } + public static bool TryGetPerUserBundleRegistrationById(string bundleId, out BundleRegistration registration) + { + var registrationKeyPath = $"{BURN_REGISTRATION_REGISTRY_UNINSTALL_KEY}\\{bundleId}"; + using var registrationKey = Registry.CurrentUser.OpenSubKey(registrationKeyPath); + var success = registrationKey != null; + registration = success ? GetBundleRegistration(registrationKey) : null; + return success; + } + private static BundleRegistration GetBundleRegistration(RegistryKey idKey) { var registration = new BundleRegistration(); diff --git a/src/WixTestTools/BundleVerifier.cs b/src/WixTestTools/BundleVerifier.cs index 96c86fdf..433b6a0a 100644 --- a/src/WixTestTools/BundleVerifier.cs +++ b/src/WixTestTools/BundleVerifier.cs @@ -33,31 +33,46 @@ namespace WixTestTools return this.BundleSymbol; } - public string GetPackageCachePathForCacheId(string cacheId) + public string GetPackageCachePathForCacheId(string cacheId, bool perMachine) { - using var policyKey = Registry.LocalMachine.OpenSubKey(FULL_BURN_POLICY_REGISTRY_PATH); - var redirectedCachePath = policyKey?.GetValue("PackageCache") as string; - var cachePath = redirectedCachePath ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), PACKAGE_CACHE_FOLDER_NAME); + string cachePath; + if (perMachine) + { + using var policyKey = Registry.LocalMachine.OpenSubKey(FULL_BURN_POLICY_REGISTRY_PATH); + var redirectedCachePath = policyKey?.GetValue("PackageCache") as string; + cachePath = redirectedCachePath ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), PACKAGE_CACHE_FOLDER_NAME); + } + else + { + cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), PACKAGE_CACHE_FOLDER_NAME); + } return Path.Combine(cachePath, cacheId); } public string GetExpectedCachedBundlePath() { var bundleSymbol = this.GetBundleSymbol(); - var cachePath = this.GetPackageCachePathForCacheId(bundleSymbol.BundleId); + var cachePath = this.GetPackageCachePathForCacheId(bundleSymbol.BundleId, bundleSymbol.PerMachine); return Path.Combine(cachePath, Path.GetFileName(this.Bundle)); } - public bool TryGetPerMachineRegistration(out BundleRegistration registration) + public bool TryGetRegistration(out BundleRegistration registration) { var bundleSymbol = this.GetBundleSymbol(); var bundleId = bundleSymbol.BundleId; - return BundleRegistration.TryGetPerMachineBundleRegistrationById(bundleId, out registration); + if (bundleSymbol.PerMachine) + { + return BundleRegistration.TryGetPerMachineBundleRegistrationById(bundleId, out registration); + } + else + { + return BundleRegistration.TryGetPerUserBundleRegistrationById(bundleId, out registration); + } } public string VerifyRegisteredAndInPackageCache() { - Assert.True(this.TryGetPerMachineRegistration(out var registration)); + Assert.True(this.TryGetRegistration(out var registration)); Assert.NotNull(registration.CachePath); Assert.True(File.Exists(registration.CachePath)); @@ -76,7 +91,7 @@ namespace WixTestTools public void VerifyUnregisteredAndRemovedFromPackageCache(string cachedBundlePath) { - Assert.False(this.TryGetPerMachineRegistration(out _)); + Assert.False(this.TryGetRegistration(out _)); Assert.False(File.Exists(cachedBundlePath)); } @@ -86,11 +101,41 @@ namespace WixTestTools var intermediate = Intermediate.Load(wixOutput); var section = intermediate.Sections.Single(); var packageSymbol = section.Symbols.OfType().Single(p => p.Id.Id == packageId); - var cachePath = this.GetPackageCachePathForCacheId(packageSymbol.CacheId); + var cachePath = this.GetPackageCachePathForCacheId(packageSymbol.CacheId, packageSymbol.PerMachine == YesNoDefaultType.Yes); if (Directory.Exists(cachePath)) { Directory.Delete(cachePath, true); } } + + public void VerifyPackageIsCached(string packageId) + { + using var wixOutput = WixOutput.Read(this.BundlePdb); + var intermediate = Intermediate.Load(wixOutput); + var section = intermediate.Sections.Single(); + var packageSymbol = section.Symbols.OfType().Single(p => p.Id.Id == packageId); + var cachePath = this.GetPackageCachePathForCacheId(packageSymbol.CacheId, packageSymbol.PerMachine == YesNoDefaultType.Yes); + Assert.True(Directory.Exists(cachePath)); + } + + public void VerifyExeTestRegistryRootDeleted(string name) + { + using var testRegistryRoot = this.TestContext.GetTestRegistryRoot(name); + if (testRegistryRoot != null) + { + var actualValue = testRegistryRoot.GetValue("Version") as string; + Assert.Null(actualValue); + } + } + + public void VerifyExeTestRegistryValue(string name, string expectedValue) + { + using (var root = this.TestContext.GetTestRegistryRoot(name)) + { + Assert.NotNull(root); + var actualValue = root.GetValue("Version") as string; + Assert.Equal(expectedValue, actualValue); + } + } } } diff --git a/src/WixTestTools/MsiUtilities.cs b/src/WixTestTools/MsiUtilities.cs index 2a848938..4c7d1601 100644 --- a/src/WixTestTools/MsiUtilities.cs +++ b/src/WixTestTools/MsiUtilities.cs @@ -2,6 +2,7 @@ namespace WixTestTools { + using System; using WixToolset.Dtf.WindowsInstaller; public class MsiUtilities @@ -23,5 +24,24 @@ namespace WixTestTools } return false; } + + /// + /// Return true if it finds the given productcode in system with the specified version otherwise it returns false + /// + /// + /// + /// + public static bool IsProductInstalledWithVersion(string prodCode, Version prodVersion) + { + //look in all user's products (both per-machine and per-user) + foreach (ProductInstallation product in ProductInstallation.GetProducts(null, "s-1-1-0", UserContexts.All)) + { + if (product.ProductCode == prodCode && product.ProductVersion == prodVersion) + { + return true; + } + } + return false; + } } } diff --git a/src/WixTestTools/PackageVerifier.cs b/src/WixTestTools/PackageVerifier.cs index b4289032..073e83b0 100644 --- a/src/WixTestTools/PackageVerifier.cs +++ b/src/WixTestTools/PackageVerifier.cs @@ -58,6 +58,13 @@ namespace WixTestTools Assert.Equal(installed, MsiUtilities.IsProductInstalled(productCode)); } + public void VerifyInstalledWithVersion(bool installed) + { + var productCode = this.GetProperty("ProductCode"); + Version prodVersion = new Version(this.GetProperty("ProductVersion")); + Assert.Equal(installed, MsiUtilities.IsProductInstalledWithVersion(productCode, prodVersion)); + } + public void DeleteTestRegistryValue(string name) { using (var root = this.TestContext.GetTestRegistryRoot()) diff --git a/src/WixTestTools/WixTestTools.csproj b/src/WixTestTools/WixTestTools.csproj index f81c82d1..0c3c4c76 100644 --- a/src/WixTestTools/WixTestTools.csproj +++ b/src/WixTestTools/WixTestTools.csproj @@ -11,7 +11,7 @@ - + -- cgit v1.2.3-55-g6feb