diff options
Diffstat (limited to 'src/test/burn/WixTestTools/BundleVerifier.cs')
-rw-r--r-- | src/test/burn/WixTestTools/BundleVerifier.cs | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/test/burn/WixTestTools/BundleVerifier.cs b/src/test/burn/WixTestTools/BundleVerifier.cs new file mode 100644 index 00000000..984df169 --- /dev/null +++ b/src/test/burn/WixTestTools/BundleVerifier.cs | |||
@@ -0,0 +1,156 @@ | |||
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, bool perMachine) | ||
37 | { | ||
38 | string cachePath; | ||
39 | if (perMachine) | ||
40 | { | ||
41 | using var policyKey = Registry.LocalMachine.OpenSubKey(FULL_BURN_POLICY_REGISTRY_PATH); | ||
42 | var redirectedCachePath = policyKey?.GetValue("PackageCache") as string; | ||
43 | cachePath = redirectedCachePath ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), PACKAGE_CACHE_FOLDER_NAME); | ||
44 | } | ||
45 | else | ||
46 | { | ||
47 | cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), PACKAGE_CACHE_FOLDER_NAME); | ||
48 | } | ||
49 | return Path.Combine(cachePath, cacheId); | ||
50 | } | ||
51 | |||
52 | public string GetExpectedCachedBundlePath() | ||
53 | { | ||
54 | var bundleSymbol = this.GetBundleSymbol(); | ||
55 | var cachePath = this.GetPackageCachePathForCacheId(bundleSymbol.BundleId, bundleSymbol.PerMachine); | ||
56 | return Path.Combine(cachePath, Path.GetFileName(this.Bundle)); | ||
57 | } | ||
58 | |||
59 | public string ManuallyCache() | ||
60 | { | ||
61 | var expectedCachePath = this.GetExpectedCachedBundlePath(); | ||
62 | Directory.CreateDirectory(Path.GetDirectoryName(expectedCachePath)); | ||
63 | File.Copy(this.Bundle, expectedCachePath); | ||
64 | return expectedCachePath; | ||
65 | } | ||
66 | |||
67 | public void ManuallyUncache() | ||
68 | { | ||
69 | var expectedCachePath = this.GetExpectedCachedBundlePath(); | ||
70 | File.Delete(expectedCachePath); | ||
71 | } | ||
72 | |||
73 | public bool TryGetRegistration(out BundleRegistration registration) | ||
74 | { | ||
75 | var bundleSymbol = this.GetBundleSymbol(); | ||
76 | var x64 = bundleSymbol.Platform != Platform.X86; | ||
77 | var bundleId = bundleSymbol.BundleId; | ||
78 | if (bundleSymbol.PerMachine) | ||
79 | { | ||
80 | return BundleRegistration.TryGetPerMachineBundleRegistrationById(bundleId, x64, out registration); | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | return BundleRegistration.TryGetPerUserBundleRegistrationById(bundleId, out registration); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | public string VerifyRegisteredAndInPackageCache() | ||
89 | { | ||
90 | Assert.True(this.TryGetRegistration(out var registration)); | ||
91 | |||
92 | Assert.NotNull(registration.CachePath); | ||
93 | Assert.True(File.Exists(registration.CachePath)); | ||
94 | |||
95 | var expectedCachePath = this.GetExpectedCachedBundlePath(); | ||
96 | Assert.Equal(expectedCachePath, registration.CachePath, StringComparer.OrdinalIgnoreCase); | ||
97 | |||
98 | return registration.CachePath; | ||
99 | } | ||
100 | |||
101 | public void VerifyUnregisteredAndRemovedFromPackageCache() | ||
102 | { | ||
103 | var cachedBundlePath = this.GetExpectedCachedBundlePath(); | ||
104 | this.VerifyUnregisteredAndRemovedFromPackageCache(cachedBundlePath); | ||
105 | } | ||
106 | |||
107 | public void VerifyUnregisteredAndRemovedFromPackageCache(string cachedBundlePath) | ||
108 | { | ||
109 | Assert.False(this.TryGetRegistration(out _)); | ||
110 | Assert.False(File.Exists(cachedBundlePath)); | ||
111 | } | ||
112 | |||
113 | public void RemovePackageFromCache(string packageId) | ||
114 | { | ||
115 | using var wixOutput = WixOutput.Read(this.BundlePdb); | ||
116 | var intermediate = Intermediate.Load(wixOutput); | ||
117 | var section = intermediate.Sections.Single(); | ||
118 | var packageSymbol = section.Symbols.OfType<WixBundlePackageSymbol>().Single(p => p.Id.Id == packageId); | ||
119 | var cachePath = this.GetPackageCachePathForCacheId(packageSymbol.CacheId, packageSymbol.PerMachine == YesNoDefaultType.Yes); | ||
120 | if (Directory.Exists(cachePath)) | ||
121 | { | ||
122 | Directory.Delete(cachePath, true); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | public void VerifyPackageIsCached(string packageId) | ||
127 | { | ||
128 | using var wixOutput = WixOutput.Read(this.BundlePdb); | ||
129 | var intermediate = Intermediate.Load(wixOutput); | ||
130 | var section = intermediate.Sections.Single(); | ||
131 | var packageSymbol = section.Symbols.OfType<WixBundlePackageSymbol>().Single(p => p.Id.Id == packageId); | ||
132 | var cachePath = this.GetPackageCachePathForCacheId(packageSymbol.CacheId, packageSymbol.PerMachine == YesNoDefaultType.Yes); | ||
133 | Assert.True(Directory.Exists(cachePath)); | ||
134 | } | ||
135 | |||
136 | public void VerifyExeTestRegistryRootDeleted(string name, bool x64 = false) | ||
137 | { | ||
138 | using var testRegistryRoot = this.TestContext.GetTestRegistryRoot(x64, name); | ||
139 | if (testRegistryRoot != null) | ||
140 | { | ||
141 | var actualValue = testRegistryRoot.GetValue("Version") as string; | ||
142 | Assert.Null(actualValue); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | public void VerifyExeTestRegistryValue(string name, string expectedValue, bool x64 = false) | ||
147 | { | ||
148 | using (var root = this.TestContext.GetTestRegistryRoot(x64, name)) | ||
149 | { | ||
150 | Assert.NotNull(root); | ||
151 | var actualValue = root.GetValue("Version") as string; | ||
152 | Assert.Equal(expectedValue, actualValue); | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | } | ||