| 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 WixToolsetTest.BurnE2E |
| 4 | { |
| 5 | using System; |
| 6 | using Microsoft.Win32; |
| 7 | using WixTestTools; |
| 8 | using WixToolset.BootstrapperApplicationApi; |
| 9 | |
| 10 | public class TestBAController : IDisposable |
| 11 | { |
| 12 | public TestBAController(WixTestContext testContext, bool x64 = false) |
| 13 | { |
| 14 | this.TestGroupName = testContext.TestGroupName; |
| 15 | this.BaseRegKeyPath = x64 ? @"Software\WiX\Tests" : @"Software\WOW6432Node\WiX\Tests"; |
| 16 | this.TestBaseRegKeyPath = String.Format(@"{0}\TestBAControl\{1}", this.BaseRegKeyPath, this.TestGroupName); |
| 17 | } |
| 18 | |
| 19 | private string BaseRegKeyPath { get; } |
| 20 | |
| 21 | private string TestBaseRegKeyPath { get; } |
| 22 | |
| 23 | public string TestGroupName { get; } |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Sets a test value in the registry to communicate with the TestBA. |
| 27 | /// </summary> |
| 28 | /// <param name="name">Name of the value to set.</param> |
| 29 | /// <param name="value">Value to set. If this is null, the value is removed.</param> |
| 30 | public void SetBurnTestValue(string name, string value) |
| 31 | { |
| 32 | using (var testKey = Registry.LocalMachine.CreateSubKey(this.TestBaseRegKeyPath)) |
| 33 | { |
| 34 | if (String.IsNullOrEmpty(value)) |
| 35 | { |
| 36 | testKey.DeleteValue(name, false); |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | testKey.SetValue(name, value); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public void SetAllowAcquireAfterValidationFailure(string value = "true") |
| 46 | { |
| 47 | this.SetBurnTestValue("AllowAcquireAfterValidationFailure", value); |
| 48 | } |
| 49 | |
| 50 | public void SetExplicitlyElevateAndPlanFromOnElevateBegin(string value = "true") |
| 51 | { |
| 52 | this.SetBurnTestValue("ExplicitlyElevateAndPlanFromOnElevateBegin", value); |
| 53 | } |
| 54 | |
| 55 | public void SetForceKeepRegistration(string value = "true") |
| 56 | { |
| 57 | this.SetBurnTestValue("ForceKeepRegistration", value); |
| 58 | } |
| 59 | |
| 60 | public void SetImmediatelyQuit(string value = "true") |
| 61 | { |
| 62 | this.SetBurnTestValue("ImmediatelyQuit", value); |
| 63 | } |
| 64 | |
| 65 | public void SetQuitAfterDetect(string value = "true") |
| 66 | { |
| 67 | this.SetBurnTestValue("QuitAfterDetect", value); |
| 68 | } |
| 69 | |
| 70 | /// <summary> |
| 71 | /// Slows the cache progress of a package. |
| 72 | /// </summary> |
| 73 | /// <param name="packageId">Package identity.</param> |
| 74 | /// <param name="delay">Sets or removes the delay on a package being cached.</param> |
| 75 | public void SetPackageSlowCache(string packageId, int? delay) |
| 76 | { |
| 77 | this.SetPackageState(packageId, "SlowCache", delay.HasValue ? delay.ToString() : null); |
| 78 | } |
| 79 | |
| 80 | /// <summary> |
| 81 | /// Cancels the cache of a package at a particular progress point. |
| 82 | /// </summary> |
| 83 | /// <param name="packageId">Package identity.</param> |
| 84 | /// <param name="cancelPoint">Sets or removes the cancel progress on a package being cached.</param> |
| 85 | public void SetPackageCancelCacheAtProgress(string packageId, int? cancelPoint) |
| 86 | { |
| 87 | this.SetPackageState(packageId, "CancelCacheAtProgress", cancelPoint.HasValue ? cancelPoint.ToString() : null); |
| 88 | } |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Slows the execute progress of a package. |
| 92 | /// </summary> |
| 93 | /// <param name="packageId">Package identity.</param> |
| 94 | /// <param name="delay">Sets or removes the delay on a package being executed.</param> |
| 95 | public void SetPackageSlowExecute(string packageId, int? delay) |
| 96 | { |
| 97 | this.SetPackageState(packageId, "SlowExecute", delay.HasValue ? delay.ToString() : null); |
| 98 | } |
| 99 | |
| 100 | /// <summary> |
| 101 | /// Cancels the execute of a package at a particular progress point. |
| 102 | /// </summary> |
| 103 | /// <param name="packageId">Package identity.</param> |
| 104 | /// <param name="cancelPoint">Sets or removes the cancel progress on a package being executed.</param> |
| 105 | public void SetPackageCancelExecuteAtProgress(string packageId, int? cancelPoint) |
| 106 | { |
| 107 | this.SetPackageState(packageId, "CancelExecuteAtProgress", cancelPoint.HasValue ? cancelPoint.ToString() : null); |
| 108 | } |
| 109 | |
| 110 | /// <summary> |
| 111 | /// Cancels the execute of a package at the next progess after the specified MSI action start. |
| 112 | /// </summary> |
| 113 | /// <param name="packageId">Package identity.</param> |
| 114 | /// <param name="actionName">Sets or removes the cancel progress on a package being executed.</param> |
| 115 | public void SetPackageCancelExecuteAtActionStart(string packageId, string actionName) |
| 116 | { |
| 117 | this.SetPackageState(packageId, "CancelExecuteAtActionStart", actionName); |
| 118 | } |
| 119 | |
| 120 | /// <summary> |
| 121 | /// Cancels the execute of a package at a particular OnProgress point. |
| 122 | /// </summary> |
| 123 | /// <param name="packageId">Package identity.</param> |
| 124 | /// <param name="cancelPoint">Sets or removes the cancel OnProgress point on a package being executed.</param> |
| 125 | public void SetPackageCancelOnProgressAtProgress(string packageId, int? cancelPoint) |
| 126 | { |
| 127 | this.SetPackageState(packageId, "CancelOnProgressAtProgress", cancelPoint.HasValue ? cancelPoint.ToString() : null); |
| 128 | } |
| 129 | |
| 130 | /// <summary> |
| 131 | /// Retries the files in use one or more times before canceling. |
| 132 | /// </summary> |
| 133 | /// <param name="packageId">Package identity.</param> |
| 134 | /// <param name="cancelPoint">Sets or removes the retry count on a package's file in use message.</param> |
| 135 | public void SetPackageRetryExecuteFilesInUse(string packageId, int? retryCount) |
| 136 | { |
| 137 | this.SetPackageState(packageId, "RetryExecuteFilesInUse", retryCount.HasValue ? retryCount.ToString() : null); |
| 138 | } |
| 139 | |
| 140 | /// <summary> |
| 141 | /// Sets the requested cache type for a package that the TestBA will return to the engine during plan. |
| 142 | /// </summary> |
| 143 | /// <param name="packageId">Package identity.</param> |
| 144 | /// <param name="type">Cache type to request.</param> |
| 145 | public void SetPackageRequestedCacheType(string packageId, BOOTSTRAPPER_CACHE_TYPE type) |
| 146 | { |
| 147 | this.SetPackageState(packageId, "CacheRequested", type.ToString()); |
| 148 | } |
| 149 | |
| 150 | /// <summary> |
| 151 | /// Sets the requested state for a package that the TestBA will return to the engine during plan. |
| 152 | /// </summary> |
| 153 | /// <param name="packageId">Package identity.</param> |
| 154 | /// <param name="state">State to request.</param> |
| 155 | public void SetPackageRequestedState(string packageId, RequestState state) |
| 156 | { |
| 157 | this.SetPackageState(packageId, "Requested", state.ToString()); |
| 158 | } |
| 159 | |
| 160 | /// <summary> |
| 161 | /// Sets the requested state for a package that the TestBA will return to the engine during plan. |
| 162 | /// </summary> |
| 163 | /// <param name="packageId">Package identity.</param> |
| 164 | /// <param name="state">State to request.</param> |
| 165 | public void SetPackageFeatureState(string packageId, string featureId, FeatureState state) |
| 166 | { |
| 167 | this.SetPackageState(packageId, String.Concat(featureId, "Requested"), state.ToString()); |
| 168 | } |
| 169 | |
| 170 | /// <summary> |
| 171 | /// Requests the BA to log the test registry value for the specified package. |
| 172 | /// </summary> |
| 173 | /// <param name="packageId"></param> |
| 174 | /// <param name="value"></param> |
| 175 | public void SetPackageRecordTestRegistryValue(string packageId, string value = "true") |
| 176 | { |
| 177 | this.SetPackageState(packageId, "RecordTestRegistryValue", value); |
| 178 | } |
| 179 | |
| 180 | public void SetPackageProcessCancelAction(string packageId, BOOTSTRAPPER_EXECUTEPROCESSCANCEL_ACTION action) |
| 181 | { |
| 182 | this.SetPackageState(packageId, "ProcessCancelAction", action.ToString()); |
| 183 | } |
| 184 | |
| 185 | /// <summary> |
| 186 | /// At startup, set the payload's source path to the given path. |
| 187 | /// </summary> |
| 188 | /// <param name="payloadId"></param> |
| 189 | /// <param name="sourcePath"></param> |
| 190 | public void SetPayloadInitialLocalSource(string payloadId, string sourcePath) |
| 191 | { |
| 192 | this.SetPayloadState(payloadId, "InitialLocalSource", sourcePath); |
| 193 | } |
| 194 | |
| 195 | /// <summary> |
| 196 | /// At startup, set the container's source path to the given path. |
| 197 | /// </summary> |
| 198 | /// <param name="containerId"></param> |
| 199 | /// <param name="sourcePath"></param> |
| 200 | public void SetContainerInitialLocalSource(string containerId, string sourcePath) |
| 201 | { |
| 202 | this.SetContainerState(containerId, "InitialLocalSource", sourcePath); |
| 203 | } |
| 204 | |
| 205 | /// <summary> |
| 206 | /// Sets the number of times to re-run the Detect phase. |
| 207 | /// </summary> |
| 208 | /// <param name="state">Number of times to run Detect (after the first, normal, Detect).</param> |
| 209 | public void SetRedetectCount(int redetectCount) |
| 210 | { |
| 211 | this.SetPackageState(null, "RedetectCount", redetectCount.ToString()); |
| 212 | } |
| 213 | |
| 214 | /// <summary> |
| 215 | /// Resets the state for a package that the TestBA will return to the engine during plan. |
| 216 | /// </summary> |
| 217 | /// <param name="packageId">Package identity.</param> |
| 218 | public void ResetPackageStates(string packageId) |
| 219 | { |
| 220 | var key = String.Format(@"{0}\{1}", this.TestBaseRegKeyPath, packageId ?? String.Empty); |
| 221 | Registry.LocalMachine.DeleteSubKey(key); |
| 222 | } |
| 223 | |
| 224 | public void SetVerifyArguments(string verifyArguments) |
| 225 | { |
| 226 | this.SetBurnTestValue("VerifyArguments", verifyArguments); |
| 227 | } |
| 228 | |
| 229 | private void SetPackageState(string packageId, string name, string value) |
| 230 | { |
| 231 | var key = String.Format(@"{0}\{1}", this.TestBaseRegKeyPath, packageId ?? String.Empty); |
| 232 | using (var packageKey = Registry.LocalMachine.CreateSubKey(key)) |
| 233 | { |
| 234 | if (String.IsNullOrEmpty(value)) |
| 235 | { |
| 236 | packageKey.DeleteValue(name, false); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | packageKey.SetValue(name, value); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | private void SetContainerState(string containerId, string name, string value) |
| 246 | { |
| 247 | var key = String.Format(@"{0}\container\{1}", this.TestBaseRegKeyPath, containerId); |
| 248 | using (var containerKey = Registry.LocalMachine.CreateSubKey(key)) |
| 249 | { |
| 250 | if (String.IsNullOrEmpty(value)) |
| 251 | { |
| 252 | containerKey.DeleteValue(name, false); |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | containerKey.SetValue(name, value); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private void SetPayloadState(string payloadId, string name, string value) |
| 262 | { |
| 263 | var key = String.Format(@"{0}\payload\{1}", this.TestBaseRegKeyPath, payloadId ?? String.Empty); |
| 264 | using (var payloadKey = Registry.LocalMachine.CreateSubKey(key)) |
| 265 | { |
| 266 | if (String.IsNullOrEmpty(value)) |
| 267 | { |
| 268 | payloadKey.DeleteValue(name, false); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | payloadKey.SetValue(name, value); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | public void Dispose() |
| 278 | { |
| 279 | Registry.LocalMachine.DeleteSubKeyTree($@"{this.BaseRegKeyPath}\{this.TestGroupName}", false); |
| 280 | Registry.LocalMachine.DeleteSubKeyTree($@"{this.BaseRegKeyPath}\TestBAControl", false); |
| 281 | } |
| 282 | } |
| 283 | } |