aboutsummaryrefslogtreecommitdiff
path: root/src/test/burn/WixTestTools/BundleVerifier.cs
blob: da61d96e26193b577f36fc42f635a1e80e6ec8da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 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.

namespace WixTestTools
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Text;
    using Microsoft.Win32;
    using WixInternal.TestSupport;
    using WixToolset.Data;
    using WixToolset.Data.Symbols;
    using Xunit;

    public partial class BundleInstaller
    {
        public const string DependencyRegistryRoot = "Software\\Classes\\Installer\\Dependencies";
        public const string FULL_BURN_POLICY_REGISTRY_PATH = "SOFTWARE\\Policies\\WiX\\Burn";
        public const string FULL_BURN_POLICY_REGISTRY_PATH_WOW6432NODE = "SOFTWARE\\WOW6432Node\\Policies\\WiX\\Burn";
        public const string PACKAGE_CACHE_FOLDER_NAME = "Package Cache";

        public string BundlePdb { get; }

        private WixBundleSymbol BundleSymbol { get; set; }

        private WixBundleSymbol GetBundleSymbol()
        {
            if (this.BundleSymbol == null)
            {
                using var wixOutput = WixOutput.Read(this.BundlePdb);
                var intermediate = Intermediate.Load(wixOutput);
                var section = intermediate.Sections.Single();
                this.BundleSymbol = section.Symbols.OfType<WixBundleSymbol>().Single();
            }

            return this.BundleSymbol;
        }

        public string GetFullBurnPolicyRegistryPath()
        {
            var bundleSymbol = this.GetBundleSymbol();
            var x64 = bundleSymbol.Platform != Platform.X86;
            return x64 ? FULL_BURN_POLICY_REGISTRY_PATH : FULL_BURN_POLICY_REGISTRY_PATH_WOW6432NODE;
        }

        public string GetPackageCachePathForCacheId(string cacheId, bool perMachine)
        {
            string cachePath;
            if (perMachine)
            {
                using var policyKey = Registry.LocalMachine.OpenSubKey(this.GetFullBurnPolicyRegistryPath());
                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.BundleCode, bundleSymbol.PerMachine);
            return Path.Combine(cachePath, Path.GetFileName(this.Bundle));
        }

        public string ManuallyCache()
        {
            var expectedCachePath = this.GetExpectedCachedBundlePath();
            Directory.CreateDirectory(Path.GetDirectoryName(expectedCachePath));
            File.Copy(this.Bundle, expectedCachePath);
            return expectedCachePath;
        }

        public void ManuallyUncache()
        {
            var expectedCachePath = this.GetExpectedCachedBundlePath();
            File.Delete(expectedCachePath);
        }

        public bool TryGetArpEntryExePackageConfiguration(string packageId, out string arpId, out string arpVersion, out bool arpWin64, out bool perMachine)
        {
            using var wixOutput = WixOutput.Read(this.BundlePdb);
            var intermediate = Intermediate.Load(wixOutput);
            var section = intermediate.Sections.Single();
            var packageSymbol = section.Symbols.OfType<WixBundlePackageSymbol>().SingleOrDefault(p => p.Id.Id == packageId);
            var exePackageSymbol = section.Symbols.OfType<WixBundleExePackageSymbol>().SingleOrDefault(p => p.Id.Id == packageId);
            if (packageSymbol == null || exePackageSymbol == null || exePackageSymbol.DetectionType != WixBundleExePackageDetectionType.Arp)
            {
                arpId = null;
                arpVersion = null;
                arpWin64 = false;
                perMachine = false;
                return false;
            }

            arpId = exePackageSymbol.ArpId;
            arpVersion = exePackageSymbol.ArpDisplayVersion;
            arpWin64 = exePackageSymbol.ArpWin64;
            perMachine = packageSymbol.PerMachine == true;
            return true;
        }

        public bool TryGetRegistration(out BundleRegistration registration)
        {
            var bundleSymbol = this.GetBundleSymbol();
            var x64 = bundleSymbol.Platform != Platform.X86;
            var bundleCode = bundleSymbol.BundleCode;
            if (bundleSymbol.PerMachine)
            {
                return BundleRegistration.TryGetPerMachineBundleRegistrationById(bundleCode, x64, out registration);
            }
            else
            {
                return BundleRegistration.TryGetPerUserBundleRegistrationById(bundleCode, out registration);
            }
        }

        public BundleRegistration VerifyRegisteredAndInPackageCache(int? expectedSystemComponent = null)
        {
            Assert.True(this.TryGetRegistration(out var registration));

            Assert.Equal(expectedSystemComponent, registration.SystemComponent);

            Assert.NotNull(registration.CachePath);
            Assert.True(File.Exists(registration.CachePath));

            var expectedCachePath = this.GetExpectedCachedBundlePath();
            WixAssert.StringEqual(expectedCachePath, registration.CachePath, true);

            return registration;
        }

        public void VerifyUnregisteredAndRemovedFromPackageCache()
        {
            var cachedBundlePath = this.GetExpectedCachedBundlePath();
            this.VerifyUnregisteredAndRemovedFromPackageCache(cachedBundlePath);
        }

        public void VerifyUnregisteredAndRemovedFromPackageCache(string cachedBundlePath)
        {
            Assert.False(this.TryGetRegistration(out _), $"Bundle cached at '{cachedBundlePath}' should not still be registered.");
            Assert.False(File.Exists(cachedBundlePath), $"Cached bundle should have been removed from package cache at '{cachedBundlePath}'.");
        }

        public void RemovePackageFromCache(string packageId)
        {
            using var wixOutput = WixOutput.Read(this.BundlePdb);
            var intermediate = Intermediate.Load(wixOutput);
            var section = intermediate.Sections.Single();
            var packageSymbol = section.Symbols.OfType<WixBundlePackageSymbol>().Single(p => p.Id.Id == packageId);
            var cachePath = this.GetPackageCachePathForCacheId(packageSymbol.CacheId, packageSymbol.PerMachine == true);
            if (Directory.Exists(cachePath))
            {
                Directory.Delete(cachePath, true);
            }
        }

        public string GetPackageEntryPointCachePath(string packageId)
        {
            using var wixOutput = WixOutput.Read(this.BundlePdb);
            var intermediate = Intermediate.Load(wixOutput);
            var section = intermediate.Sections.Single();
            var packageSymbol = section.Symbols.OfType<WixBundlePackageSymbol>().Single(p => p.Id.Id == packageId);
            var packagePayloadSymbol = section.Symbols.OfType<WixBundlePayloadSymbol>().Single(p => p.Id.Id == packageSymbol.PayloadRef);
            var cachePath = this.GetPackageCachePathForCacheId(packageSymbol.CacheId, packageSymbol.PerMachine == true);
            return Path.Combine(cachePath, packagePayloadSymbol.Name);
        }

        public void VerifyPackageIsCached(string packageId, bool cached = true)
        {
            var entryPointCachePath = this.GetPackageEntryPointCachePath(packageId);
            Assert.Equal(cached, File.Exists(entryPointCachePath));
        }

        public void VerifyPackageProviderRemoved(string packageId)
        {
            using var wixOutput = WixOutput.Read(this.BundlePdb);
            var intermediate = Intermediate.Load(wixOutput);
            var section = intermediate.Sections.Single();
            var packageSymbol = section.Symbols.OfType<WixBundlePackageSymbol>().Single(p => p.Id.Id == packageId);
            var providerSymbol = section.Symbols.OfType<WixDependencyProviderSymbol>().Single(p => p.ParentRef == packageId);
            var registryRoot = packageSymbol.PerMachine == true ? Registry.LocalMachine : Registry.CurrentUser;
            var subkeyPath = Path.Combine(DependencyRegistryRoot, providerSymbol.ProviderKey);
            using var registryKey = registryRoot.OpenSubKey(subkeyPath);
            if (registryKey != null)
            {
                WixAssert.StringEqual(null, subkeyPath);
            }
        }

        public void VerifyExeTestRegistryRootDeleted(string name, bool x64 = false)
        {
            using var testRegistryRoot = this.TestContext.GetTestRegistryRoot(x64, name);
            if (testRegistryRoot != null)
            {
                var actualValue = testRegistryRoot.GetValue("Version") as string;
                Assert.Null(actualValue);
            }
        }

        public void VerifyExeTestRegistryValue(string name, string expectedValue, bool x64 = false)
        {
            using (var root = this.TestContext.GetTestRegistryRoot(x64, name))
            {
                Assert.NotNull(root);
                var actualValue = root.GetValue("Version") as string;
                Assert.Equal(expectedValue, actualValue);
            }
        }
    }
}