-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathRepairInterop.cs
More file actions
356 lines (293 loc) · 17.6 KB
/
Copy pathRepairInterop.cs
File metadata and controls
356 lines (293 loc) · 17.6 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// -----------------------------------------------------------------------------
// <copyright file="RepairInterop.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace AppInstallerCLIE2ETests.Interop
{
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AppInstallerCLIE2ETests.Helpers;
using Microsoft.Management.Deployment;
using Microsoft.Management.Deployment.Projection;
using NUnit.Framework;
/// <summary>
/// Repair Interop Tests for COM/WinRT Interop classes.
/// </summary>
[TestFixtureSource(typeof(InstanceInitializersSource), nameof(InstanceInitializersSource.InProcess), Category = nameof(InstanceInitializersSource.InProcess))]
[TestFixtureSource(typeof(InstanceInitializersSource), nameof(InstanceInitializersSource.OutOfProcess), Category = nameof(InstanceInitializersSource.OutOfProcess))]
public class RepairInterop : BaseInterop
{
private string installDir;
private PackageManager packageManager;
private PackageCatalogReference compositeSource;
/// <summary>
/// Initializes a new instance of the <see cref="RepairInterop"/> class.
/// </summary>
/// <param name="initializer">Initializer.</param>
public RepairInterop(IInstanceInitializer initializer)
: base(initializer)
{
}
/// <summary>
/// Test setup.
/// </summary>
[SetUp]
public void Init()
{
this.packageManager = this.TestFactory.CreatePackageManager();
this.installDir = TestCommon.GetRandomTestDir();
// Create a composite source
var options = this.TestFactory.CreateCreateCompositePackageCatalogOptions();
var testSource = this.packageManager.GetPackageCatalogByName(Constants.TestSourceName);
Assert.NotNull(testSource, $"{Constants.TestSourceName} cannot be null");
options.Catalogs.Add(testSource);
options.CompositeSearchBehavior = CompositeSearchBehavior.AllCatalogs;
this.compositeSource = this.packageManager.CreateCompositePackageCatalog(options);
}
/// <summary>
/// Test Repair MSI Installer.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test. </returns>
[Test]
public async Task RepairMSIInstaller()
{
string msiInstallerPackageId = "AppInstallerTest.TestMsiRepair";
var searchResult = await this.FindAndInstallPackage(msiInstallerPackageId, this.installDir, null);
// Note: The 'msiexec repair' command requires the original installer file to be present at the location registered in the ARP (Add/Remove Programs).
// In our test scenario, the MSI installer file is initially placed in a temporary location and then deleted, which can cause the repair operation to fail.
// To work around this, we copy the installer file to the ARP source directory before running the repair command.
// A more permanent solution would be to modify the MSI installer to cache the installer file in a known location and register that location as the installer source.
// This would allow the 'msiexec repair' command to function as expected.
string installerSourceDir = TestCommon.CopyInstallerFileToARPInstallSourceDirectory(TestCommon.GetTestDataFile("AppInstallerTestMsiInstallerV2.msi"), Constants.MsiInstallerProductCode, true);
// Repair the package
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, this.TestFactory.CreateRepairOptions(), RepairResultStatus.Ok);
// Cleanup
Assert.True(TestCommon.VerifyTestMsiInstalledAndCleanup(this.installDir));
if (installerSourceDir != null && Directory.Exists(installerSourceDir))
{
Directory.Delete(installerSourceDir, true);
}
}
/// <summary>
/// Test Repair MSIX Installer.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairNonStoreMSIXPackage()
{
string msixPackageId = "AppInstallerTest.TestMsixInstaller";
var searchResult = await this.FindAndInstallPackage(msixPackageId, this.installDir, null);
// Repair the package
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, this.TestFactory.CreateRepairOptions(), RepairResultStatus.Ok);
// Cleanup
Assert.True(TestCommon.VerifyTestMsixInstalledAndCleanup());
}
/// <summary>
/// Test MSIX non-store package repair with machine scope.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairNonStoreMsixPackageWithMachineScope()
{
var findPackages = this.FindAllPackages(this.compositeSource, PackageMatchField.Id, PackageFieldMatchOption.ContainsCaseInsensitive, "Microsoft.Paint");
if (findPackages == null || findPackages.Count == 0)
{
Assert.Ignore("Test skipped as Microsoft.Paint_8wekyb3d8bbwe can't be found.");
}
var searchResult = findPackages.First();
if (searchResult == null || searchResult.CatalogPackage == null || searchResult.CatalogPackage.InstalledVersion == null)
{
Assert.Ignore("Test skipped as Microsoft.Paint_8wekyb3d8bbwe is not installed.");
}
// Repair the package
var repairOptions = this.TestFactory.CreateRepairOptions();
repairOptions.PackageRepairScope = PackageRepairScope.System;
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, repairOptions, RepairResultStatus.RepairError, Constants.ErrorCode.ERROR_INSTALL_SYSTEM_NOT_SUPPORTED);
}
/// <summary>
/// Test repair of a Burn installer that has a "modify" repair behavior specified in the manifest.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairBurnInstallerWithModifyBehavior()
{
var replaceInstallerArguments = this.GetReplacementArguments(this.installDir, "2.0.0.0", "TestModifyRepair", useHKLM: true);
var searchResult = await this.FindAndInstallPackage(Constants.ModifyRepairInstaller, this.installDir, replaceInstallerArguments.ToString());
// Repair the package
var repairOptions = this.TestFactory.CreateRepairOptions();
repairOptions.PackageRepairMode = PackageRepairMode.Silent;
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, repairOptions, RepairResultStatus.Ok);
// Cleanup
Assert.True(TestCommon.VerifyTestExeRepairCompletedAndCleanup(this.installDir, "Modify Repair operation"));
}
/// <summary>
/// Tests the repair operation of a Burn installer that was installed in user scope but is being repaired in an admin context.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairBurnInstallerInAdminContextWithUserScopeInstall()
{
if (this.TestFactory.Context != ClsidContext.InProc)
{
Assert.Ignore("Test is only applicable for InProc context.");
}
string replaceInstallerArguments = this.GetReplacementArguments(this.installDir, "2.0.0.0", "TestUserScopeInstallRepairInAdminContext");
var searchResult = await this.FindAndInstallPackage("AppInstallerTest.TestUserScopeInstallRepairInAdminContext", this.installDir, replaceInstallerArguments);
// Repair the package
var repairOptions = this.TestFactory.CreateRepairOptions();
repairOptions.PackageRepairMode = PackageRepairMode.Silent;
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, repairOptions, RepairResultStatus.RepairError, Constants.ErrorCode.ERROR_ADMIN_CONTEXT_ACTION_PROHIBITED);
// Cleanup
TestCommon.CleanupTestExeAndDirectory(this.installDir);
}
/// <summary>
/// Test repair of a Exe installer that has a "uninstaller" repair behavior specified in the manifest and NoModify ARP flag set.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairBurnInstallerWithModifyBehaviorAndNoModifyFlag()
{
string replaceInstallerArguments = this.GetReplacementArguments(this.installDir, "2.0.0.0", "TestModifyRepairWithNoModify", useHKLM: true, noModify: true);
var searchResult = await this.FindAndInstallPackage("AppInstallerTest.TestModifyRepairWithNoModify", this.installDir, replaceInstallerArguments);
// Repair the package
var repairOptions = this.TestFactory.CreateRepairOptions();
repairOptions.PackageRepairMode = PackageRepairMode.Silent;
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, repairOptions, RepairResultStatus.RepairError, Constants.ErrorCode.ERROR_REPAIR_NOT_SUPPORTED);
// Cleanup
TestCommon.CleanupTestExeAndDirectory(this.installDir);
}
/// <summary>
/// Tests the scenario where the repair operation is not supported for Portable Installer type.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairOperationNotSupportedForPortableInstaller()
{
string installDir = TestCommon.GetPortablePackagesDirectory();
string packageId, commandAlias, fileName, packageDirName, productCode;
packageId = "AppInstallerTest.TestPortableExe";
packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier;
commandAlias = fileName = "AppInstallerTestExeInstaller.exe";
var searchResult = await this.FindAndInstallPackage(packageId, null, null);
// Repair the package
var repairOptions = this.TestFactory.CreateRepairOptions();
repairOptions.PackageRepairMode = PackageRepairMode.Silent;
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, repairOptions, RepairResultStatus.RepairError, Constants.ErrorCode.ERROR_REPAIR_NOT_SUPPORTED);
// If no location specified, default behavior is to create a package directory with the name "{packageId}_{sourceId}"
TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, true);
}
/// <summary>
/// Test repair of a Exe installer that has a "uninstaller" repair behavior specified in the manifest.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairExeInstallerWithUninstallerBehavior()
{
string replaceInstallerArguments = this.GetReplacementArguments(this.installDir, "2.0.0.0", "UninstallerRepair", useHKLM: true);
var searchResult = await this.FindAndInstallPackage("AppInstallerTest.UninstallerRepair", this.installDir, replaceInstallerArguments);
// Repair the package
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, this.TestFactory.CreateRepairOptions(), RepairResultStatus.Ok);
// Cleanup
Assert.True(TestCommon.VerifyTestExeRepairCompletedAndCleanup(this.installDir, "Uninstaller Repair operation"));
}
/// <summary>
/// Test repair of a Exe installer that has a "uninstaller" repair behavior specified in the manifest and NoRepair ARP flag set.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairExeInstallerWithUninstallerBehaviorAndNoRepairFlag()
{
string replaceInstallerArguments = this.GetReplacementArguments(this.installDir, "2.0.0.0", "UninstallerRepairWithNoRepair", useHKLM: true, noRepair: true);
var searchResult = await this.FindAndInstallPackage("AppInstallerTest.UninstallerRepairWithNoRepair", this.installDir, replaceInstallerArguments);
// Repair the package
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, this.TestFactory.CreateRepairOptions(), RepairResultStatus.RepairError, Constants.ErrorCode.ERROR_REPAIR_NOT_SUPPORTED);
// Cleanup
TestCommon.CleanupTestExeAndDirectory(this.installDir);
}
/// <summary>
/// Test repair of a Nullsoft installer that has a "uninstaller" repair behavior specified in the manifest.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairNullsoftInstallerWithUninstallerBehavior()
{
string replaceInstallerArguments = this.GetReplacementArguments(this.installDir, "2.0.0.0", "NullsoftUninstallerRepair", useHKLM: true);
var searchResult = await this.FindAndInstallPackage("AppInstallerTest.NullsoftUninstallerRepair", this.installDir, replaceInstallerArguments.ToString());
// Repair the package
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, this.TestFactory.CreateRepairOptions(), RepairResultStatus.Ok);
// Cleanup
Assert.True(TestCommon.VerifyTestExeRepairCompletedAndCleanup(this.installDir, "Uninstaller Repair operation"));
}
/// <summary>
/// Test repair of a Inno installer that has a "installer" repair behavior specified in the manifest.
/// </summary>
/// <returns>representing the asynchronous unit test.</returns>
[Test]
public async Task RepairInnoInstallerWithInstallerRepairBehavior()
{
string replaceInstallerArguments = this.GetReplacementArguments(this.installDir, "2.0.0.0", "TestInstallerRepair", useHKLM: true);
var searchResult = await this.FindAndInstallPackage("AppInstallerTest.TestInstallerRepair", this.installDir, replaceInstallerArguments);
// Repair the package
await this.RepairPackageAndValidateStatus(searchResult.CatalogPackage, this.TestFactory.CreateRepairOptions(), RepairResultStatus.Ok);
// Cleanup
Assert.True(TestCommon.VerifyTestExeRepairCompletedAndCleanup(this.installDir, "Installer Repair operation"));
}
private async Task<MatchResult> FindAndInstallPackage(string packageId, string installDir, string replacementInstallerArguments)
{
// Find a package
var searchResult = this.FindOnePackage(this.compositeSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, packageId);
// Configure installation
var installOptions = this.TestFactory.CreateInstallOptions();
installOptions.PackageInstallMode = PackageInstallMode.Silent;
if (!string.IsNullOrEmpty(installDir))
{
installOptions.PreferredInstallLocation = installDir;
}
if (!string.IsNullOrEmpty(replacementInstallerArguments))
{
installOptions.ReplacementInstallerArguments = replacementInstallerArguments;
}
// Install the package
var installResult = await this.packageManager.InstallPackageAsync(searchResult.CatalogPackage, installOptions);
Assert.AreEqual(InstallResultStatus.Ok, installResult.Status);
// Find package again, but this time it should be detected as installed
searchResult = this.FindOnePackage(this.compositeSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, packageId);
Assert.NotNull(searchResult.CatalogPackage.InstalledVersion);
// Return the search result
return searchResult;
}
private async Task RepairPackageAndValidateStatus(CatalogPackage package, RepairOptions repairOptions, RepairResultStatus expectedStatus, int expectedErrorCode = 0)
{
var repairResult = await this.packageManager.RepairPackageAsync(package, repairOptions);
Assert.AreEqual(expectedStatus, repairResult.Status);
if (expectedStatus != RepairResultStatus.Ok)
{
Assert.AreEqual(expectedErrorCode, repairResult.ExtendedErrorCode.HResult);
}
}
private string GetReplacementArguments(string installDir, string version, string displayName, bool useHKLM = false, bool noModify = false, bool noRepair = false)
{
var replacementArguments = new StringBuilder($"/InstallDir {installDir} /Version {version} /DisplayName {displayName}");
// Machine scope install.
if (useHKLM)
{
replacementArguments.Append($" /UseHKLM");
}
// Instructs test installer to set NoModify ARP flag.
if (noModify)
{
replacementArguments.Append($" /NoModify");
}
// Instructs test installer to set NoRepair ARP flag.
if (noRepair)
{
replacementArguments.Append($" /NoRepair");
}
return replacementArguments.ToString();
}
}
}