aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolsetTest.BurnE2E/BundleVerifier.cs
blob: 94d51890e1b0ac20caf765b8d4af4b4524a78309 (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
// 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 WixToolsetTest.BurnE2E
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Text;
    using Microsoft.Win32;
    using WixToolset.Data;
    using WixToolset.Data.Symbols;
    using Xunit;

    public partial class BundleInstaller
    {
        public const string FULL_BURN_POLICY_REGISTRY_PATH = "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 GetExpectedCachedBundlePath()
        {
            var bundleSymbol = this.GetBundleSymbol();

            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);
            return Path.Combine(cachePath, bundleSymbol.BundleId, Path.GetFileName(this.Bundle));
        }

        public bool TryGetPerMachineRegistration(out BundleRegistration registration)
        {
            var bundleSymbol = this.GetBundleSymbol();
            var bundleId = bundleSymbol.BundleId;
            return BundleRegistration.TryGetPerMachineBundleRegistrationById(bundleId, out registration);
        }

        public string VerifyRegisteredAndInPackageCache()
        {
            Assert.True(this.TryGetPerMachineRegistration(out var registration));

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

            var expectedCachePath = this.GetExpectedCachedBundlePath();
            Assert.Equal(expectedCachePath, registration.CachePath, StringComparer.OrdinalIgnoreCase);

            return registration.CachePath;
        }

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

        public void VerifyUnregisteredAndRemovedFromPackageCache(string cachedBundlePath)
        {
            Assert.False(this.TryGetPerMachineRegistration(out _));
            Assert.False(File.Exists(cachedBundlePath));
        }
    }
}