diff options
| author | Sean Hall <r.sean.hall@gmail.com> | 2021-02-17 15:45:19 -0600 |
|---|---|---|
| committer | Sean Hall <r.sean.hall@gmail.com> | 2021-02-17 16:20:10 -0600 |
| commit | badde27bf66fcacef2d625af0bd7590ecdc5804f (patch) | |
| tree | c70f7da4642b2c9d2613a575fbb0169bc3034a60 /src/WixTestTools/BundleVerifier.cs | |
| parent | e950ce317301aea2e9c8cdab1bf8c453c40a4edd (diff) | |
| download | wix-badde27bf66fcacef2d625af0bd7590ecdc5804f.tar.gz wix-badde27bf66fcacef2d625af0bd7590ecdc5804f.tar.bz2 wix-badde27bf66fcacef2d625af0bd7590ecdc5804f.zip | |
Create WixTestTools project and reorganize files.
Diffstat (limited to 'src/WixTestTools/BundleVerifier.cs')
| -rw-r--r-- | src/WixTestTools/BundleVerifier.cs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/WixTestTools/BundleVerifier.cs b/src/WixTestTools/BundleVerifier.cs new file mode 100644 index 00000000..96c86fdf --- /dev/null +++ b/src/WixTestTools/BundleVerifier.cs | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | namespace WixTestTools | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.IO; | ||
| 7 | using System.Linq; | ||
| 8 | using System.Text; | ||
| 9 | using Microsoft.Win32; | ||
| 10 | using WixToolset.Data; | ||
| 11 | using WixToolset.Data.Symbols; | ||
| 12 | using Xunit; | ||
| 13 | |||
| 14 | public partial class BundleInstaller | ||
| 15 | { | ||
| 16 | public const string FULL_BURN_POLICY_REGISTRY_PATH = "SOFTWARE\\WOW6432Node\\Policies\\WiX\\Burn"; | ||
| 17 | public const string PACKAGE_CACHE_FOLDER_NAME = "Package Cache"; | ||
| 18 | |||
| 19 | public string BundlePdb { get; } | ||
| 20 | |||
| 21 | private WixBundleSymbol BundleSymbol { get; set; } | ||
| 22 | |||
| 23 | private WixBundleSymbol GetBundleSymbol() | ||
| 24 | { | ||
| 25 | if (this.BundleSymbol == null) | ||
| 26 | { | ||
| 27 | using var wixOutput = WixOutput.Read(this.BundlePdb); | ||
| 28 | var intermediate = Intermediate.Load(wixOutput); | ||
| 29 | var section = intermediate.Sections.Single(); | ||
| 30 | this.BundleSymbol = section.Symbols.OfType<WixBundleSymbol>().Single(); | ||
| 31 | } | ||
| 32 | |||
| 33 | return this.BundleSymbol; | ||
| 34 | } | ||
| 35 | |||
| 36 | public string GetPackageCachePathForCacheId(string cacheId) | ||
| 37 | { | ||
| 38 | using var policyKey = Registry.LocalMachine.OpenSubKey(FULL_BURN_POLICY_REGISTRY_PATH); | ||
| 39 | var redirectedCachePath = policyKey?.GetValue("PackageCache") as string; | ||
| 40 | var cachePath = redirectedCachePath ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), PACKAGE_CACHE_FOLDER_NAME); | ||
| 41 | return Path.Combine(cachePath, cacheId); | ||
| 42 | } | ||
| 43 | |||
| 44 | public string GetExpectedCachedBundlePath() | ||
| 45 | { | ||
| 46 | var bundleSymbol = this.GetBundleSymbol(); | ||
| 47 | var cachePath = this.GetPackageCachePathForCacheId(bundleSymbol.BundleId); | ||
| 48 | return Path.Combine(cachePath, Path.GetFileName(this.Bundle)); | ||
| 49 | } | ||
| 50 | |||
| 51 | public bool TryGetPerMachineRegistration(out BundleRegistration registration) | ||
| 52 | { | ||
| 53 | var bundleSymbol = this.GetBundleSymbol(); | ||
| 54 | var bundleId = bundleSymbol.BundleId; | ||
| 55 | return BundleRegistration.TryGetPerMachineBundleRegistrationById(bundleId, out registration); | ||
| 56 | } | ||
| 57 | |||
| 58 | public string VerifyRegisteredAndInPackageCache() | ||
| 59 | { | ||
| 60 | Assert.True(this.TryGetPerMachineRegistration(out var registration)); | ||
| 61 | |||
| 62 | Assert.NotNull(registration.CachePath); | ||
| 63 | Assert.True(File.Exists(registration.CachePath)); | ||
| 64 | |||
| 65 | var expectedCachePath = this.GetExpectedCachedBundlePath(); | ||
| 66 | Assert.Equal(expectedCachePath, registration.CachePath, StringComparer.OrdinalIgnoreCase); | ||
| 67 | |||
| 68 | return registration.CachePath; | ||
| 69 | } | ||
| 70 | |||
| 71 | public void VerifyUnregisteredAndRemovedFromPackageCache() | ||
| 72 | { | ||
| 73 | var cachedBundlePath = this.GetExpectedCachedBundlePath(); | ||
| 74 | this.VerifyUnregisteredAndRemovedFromPackageCache(cachedBundlePath); | ||
| 75 | } | ||
| 76 | |||
| 77 | public void VerifyUnregisteredAndRemovedFromPackageCache(string cachedBundlePath) | ||
| 78 | { | ||
| 79 | Assert.False(this.TryGetPerMachineRegistration(out _)); | ||
| 80 | Assert.False(File.Exists(cachedBundlePath)); | ||
| 81 | } | ||
| 82 | |||
| 83 | public void RemovePackageFromCache(string packageId) | ||
| 84 | { | ||
| 85 | using var wixOutput = WixOutput.Read(this.BundlePdb); | ||
| 86 | var intermediate = Intermediate.Load(wixOutput); | ||
| 87 | var section = intermediate.Sections.Single(); | ||
| 88 | var packageSymbol = section.Symbols.OfType<WixBundlePackageSymbol>().Single(p => p.Id.Id == packageId); | ||
| 89 | var cachePath = this.GetPackageCachePathForCacheId(packageSymbol.CacheId); | ||
| 90 | if (Directory.Exists(cachePath)) | ||
| 91 | { | ||
| 92 | Directory.Delete(cachePath, true); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
