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
|
// 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 WixBuildTools.TestSupport;
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 GetPackageCachePathForCacheId(string cacheId, bool perMachine)
{
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, 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 TryGetRegistration(out BundleRegistration registration)
{
var bundleSymbol = this.GetBundleSymbol();
var x64 = bundleSymbol.Platform != Platform.X86;
var bundleId = bundleSymbol.BundleId;
if (bundleSymbol.PerMachine)
{
return BundleRegistration.TryGetPerMachineBundleRegistrationById(bundleId, x64, out registration);
}
else
{
return BundleRegistration.TryGetPerUserBundleRegistrationById(bundleId, out registration);
}
}
public string VerifyRegisteredAndInPackageCache()
{
Assert.True(this.TryGetRegistration(out var registration));
Assert.NotNull(registration.CachePath);
Assert.True(File.Exists(registration.CachePath));
var expectedCachePath = this.GetExpectedCachedBundlePath();
WixAssert.StringEqual(expectedCachePath, registration.CachePath, true);
return registration.CachePath;
}
public void VerifyUnregisteredAndRemovedFromPackageCache()
{
var cachedBundlePath = this.GetExpectedCachedBundlePath();
this.VerifyUnregisteredAndRemovedFromPackageCache(cachedBundlePath);
}
public void VerifyUnregisteredAndRemovedFromPackageCache(string cachedBundlePath)
{
Assert.False(this.TryGetRegistration(out _));
Assert.False(File.Exists(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 == YesNoDefaultType.Yes);
if (Directory.Exists(cachePath))
{
Directory.Delete(cachePath, true);
}
}
public void VerifyPackageIsCached(string packageId, bool cached = true)
{
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 == YesNoDefaultType.Yes);
Assert.Equal(cached, Directory.Exists(cachePath));
}
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);
}
}
}
}
|