DownloadInterop.cs (15983B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="DownloadInterop.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace AppInstallerCLIE2ETests.Interop 8 { 9 using System; 10 using System.IO; 11 using System.Threading.Tasks; 12 using AppInstallerCLIE2ETests.Helpers; 13 using Microsoft.Management.Deployment; 14 using Microsoft.Management.Deployment.Projection; 15 using NUnit.Framework; 16 using Windows.System; 17 18 /// <summary> 19 /// Download interop. 20 /// </summary> 21 [TestFixtureSource(typeof(InstanceInitializersSource), nameof(InstanceInitializersSource.InProcess), Category = nameof(InstanceInitializersSource.InProcess))] 22 [TestFixtureSource(typeof(InstanceInitializersSource), nameof(InstanceInitializersSource.OutOfProcess), Category = nameof(InstanceInitializersSource.OutOfProcess))] 23 public class DownloadInterop : BaseInterop 24 { 25 private PackageManager packageManager; 26 private PackageCatalogReference testSource; 27 28 /// <summary> 29 /// Initializes a new instance of the <see cref="DownloadInterop"/> class. 30 /// </summary> 31 /// <param name="initializer">Initializer.</param> 32 public DownloadInterop(IInstanceInitializer initializer) 33 : base(initializer) 34 { 35 } 36 37 /// <summary> 38 /// Set up. 39 /// </summary> 40 [SetUp] 41 public void SetUp() 42 { 43 this.packageManager = this.TestFactory.CreatePackageManager(); 44 this.testSource = this.packageManager.GetPackageCatalogByName(Constants.TestSourceName); 45 } 46 47 /// <summary> 48 /// Downloads the test installer and its package dependencies. 49 /// </summary> 50 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 51 [Test] 52 public async Task DownloadDependencies() 53 { 54 // Find package 55 var downloadDir = TestCommon.GetRandomTestDir(); 56 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.PackageDependency"); 57 58 // Configure installation 59 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 60 downloadOptions.AcceptPackageAgreements = true; 61 downloadOptions.DownloadDirectory = downloadDir; 62 63 // Download 64 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 65 66 // Assert 67 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 68 var dependenciesDir = Path.Combine(downloadDir, Constants.Dependencies); 69 TestCommon.AssertInstallerDownload(dependenciesDir, "TestPortableExe", "3.0.0.0", ProcessorArchitecture.X64, TestCommon.Scope.Unknown, PackageInstallerType.Portable, "en-US"); 70 TestCommon.AssertInstallerDownload(downloadDir, "TestPackageDependency", "1.0.0.0", ProcessorArchitecture.X64, TestCommon.Scope.Unknown, PackageInstallerType.Exe, "en-US"); 71 } 72 73 /// <summary> 74 /// Downloads the test installer and skips dependencies. 75 /// </summary> 76 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 77 [Test] 78 public async Task DownloadDependencies_Skip() 79 { 80 // Find package 81 var downloadDir = TestCommon.GetRandomTestDir(); 82 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.PackageDependency"); 83 84 // Configure installation 85 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 86 downloadOptions.AcceptPackageAgreements = true; 87 downloadOptions.DownloadDirectory = downloadDir; 88 downloadOptions.SkipDependencies = true; 89 90 // Download 91 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 92 93 // Assert 94 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 95 var dependenciesDir = Path.Combine(downloadDir, Constants.Dependencies); 96 Assert.IsFalse(Directory.Exists(dependenciesDir)); 97 TestCommon.AssertInstallerDownload(downloadDir, "TestPackageDependency", "1.0.0.0", ProcessorArchitecture.X64, TestCommon.Scope.Unknown, PackageInstallerType.Exe, "en-US"); 98 } 99 100 /// <summary> 101 /// Download the installer to the default directory. 102 /// </summary> 103 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 104 [Test] 105 public async Task DownloadToDefaultDirectory() 106 { 107 // Find package 108 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestExeInstaller"); 109 110 // Configure installation 111 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 112 downloadOptions.AcceptPackageAgreements = true; 113 114 // Download 115 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 116 117 // Assert 118 var packageVersion = "2.0.0.0"; 119 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 120 string downloadDir = Path.Combine(TestCommon.GetDefaultDownloadDirectory(), $"{Constants.ExeInstallerPackageId}_{packageVersion}"); 121 TestCommon.AssertInstallerDownload(downloadDir, "TestExeInstaller", packageVersion, ProcessorArchitecture.X86, TestCommon.Scope.User, PackageInstallerType.Exe); 122 } 123 124 /// <summary> 125 /// Download the installer to a specified directory. 126 /// </summary> 127 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 128 [Test] 129 public async Task DownloadToDirectory() 130 { 131 // Find package 132 var downloadDir = TestCommon.GetRandomTestDir(); 133 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestExeInstaller"); 134 135 // Configure installation 136 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 137 downloadOptions.AcceptPackageAgreements = true; 138 downloadOptions.DownloadDirectory = downloadDir; 139 140 // Download 141 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 142 143 // Assert 144 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 145 TestCommon.AssertInstallerDownload(downloadDir, "TestExeInstaller", "2.0.0.0", ProcessorArchitecture.X86, TestCommon.Scope.User, PackageInstallerType.Exe); 146 } 147 148 /// <summary> 149 /// Download the installer using the user scope argument. 150 /// </summary> 151 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 152 [Test] 153 public async Task DownloadWithUserScope() 154 { 155 // Find package 156 var downloadDir = TestCommon.GetRandomTestDir(); 157 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestMultipleInstallers"); 158 159 // Configure installation 160 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 161 downloadOptions.AcceptPackageAgreements = true; 162 downloadOptions.DownloadDirectory = downloadDir; 163 downloadOptions.Scope = PackageInstallScope.User; 164 165 // Download 166 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 167 168 // Assert 169 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 170 TestCommon.AssertInstallerDownload(downloadDir, "TestMultipleInstallers", "1.0.0.0", ProcessorArchitecture.X64, TestCommon.Scope.User, PackageInstallerType.Nullsoft, "en-US"); 171 } 172 173 /// <summary> 174 /// Download the installer using the machine scope argument. 175 /// </summary> 176 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 177 [Test] 178 public async Task DownloadWithMachineScope() 179 { 180 // Find package 181 var downloadDir = TestCommon.GetRandomTestDir(); 182 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestMultipleInstallers"); 183 184 // Configure installation 185 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 186 downloadOptions.AcceptPackageAgreements = true; 187 downloadOptions.DownloadDirectory = downloadDir; 188 downloadOptions.Scope = PackageInstallScope.System; 189 190 // Download 191 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 192 193 // Assert 194 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 195 TestCommon.AssertInstallerDownload(downloadDir, "TestMultipleInstallers", "1.0.0.0", ProcessorArchitecture.X86, TestCommon.Scope.Machine, PackageInstallerType.Msi, "en-US"); 196 } 197 198 /// <summary> 199 /// Download the test installer using the 'zip' installer type argument. 200 /// </summary> 201 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 202 [Test] 203 public async Task DownloadWithZipInstallerTypeArg() 204 { 205 // Find package 206 var downloadDir = TestCommon.GetRandomTestDir(); 207 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestMultipleInstallers"); 208 209 // Configure installation 210 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 211 downloadOptions.AcceptPackageAgreements = true; 212 downloadOptions.DownloadDirectory = downloadDir; 213 downloadOptions.InstallerType = PackageInstallerType.Zip; 214 215 // Download 216 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 217 218 // Assert 219 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 220 TestCommon.AssertInstallerDownload(downloadDir, "TestMultipleInstallers", "1.0.0.0", ProcessorArchitecture.X64, TestCommon.Scope.Unknown, PackageInstallerType.Exe, "zh-CN", true); 221 } 222 223 /// <summary> 224 /// Downloads the test installer using the installer type argument. 225 /// </summary> 226 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 227 [Test] 228 public async Task DownloadWithInstallerTypeArg() 229 { 230 // Find package 231 var downloadDir = TestCommon.GetRandomTestDir(); 232 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestMultipleInstallers"); 233 234 // Configure installation 235 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 236 downloadOptions.AcceptPackageAgreements = true; 237 downloadOptions.DownloadDirectory = downloadDir; 238 downloadOptions.InstallerType = PackageInstallerType.Msi; 239 240 // Download 241 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 242 243 // Assert 244 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 245 TestCommon.AssertInstallerDownload(downloadDir, "TestMultipleInstallers", "1.0.0.0", ProcessorArchitecture.X86, TestCommon.Scope.Machine, PackageInstallerType.Msi, "en-US"); 246 } 247 248 /// <summary> 249 /// Downloads the test installer using the architecture argument. 250 /// </summary> 251 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 252 [Test] 253 public async Task DownloadWithArchitectureArg() 254 { 255 // Find package 256 var downloadDir = TestCommon.GetRandomTestDir(); 257 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestMultipleInstallers"); 258 259 // Configure installation 260 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 261 downloadOptions.AcceptPackageAgreements = true; 262 downloadOptions.DownloadDirectory = downloadDir; 263 downloadOptions.Architecture = ProcessorArchitecture.X86; 264 265 // Download 266 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 267 268 // Assert 269 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 270 TestCommon.AssertInstallerDownload(downloadDir, "TestMultipleInstallers", "1.0.0.0", ProcessorArchitecture.X86, TestCommon.Scope.Machine, PackageInstallerType.Msi, "en-US"); 271 } 272 273 /// <summary> 274 /// Downloads the test installer using the locale argument. 275 /// </summary> 276 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 277 [Test] 278 public async Task DownloadWithLocaleArg() 279 { 280 // Find package 281 var downloadDir = TestCommon.GetRandomTestDir(); 282 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestMultipleInstallers"); 283 284 // Configure installation 285 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 286 downloadOptions.AcceptPackageAgreements = true; 287 downloadOptions.DownloadDirectory = downloadDir; 288 downloadOptions.Locale = "zh-CN"; 289 290 // Download 291 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 292 293 // Assert 294 Assert.AreEqual(DownloadResultStatus.Ok, downloadResult.Status); 295 TestCommon.AssertInstallerDownload(downloadDir, "TestMultipleInstallers", "1.0.0.0", ProcessorArchitecture.X64, TestCommon.Scope.Unknown, PackageInstallerType.Exe, "zh-CN", true); 296 } 297 298 /// <summary> 299 /// Downloads the test installer with a hash mismatch. 300 /// </summary> 301 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> 302 [Test] 303 public async Task DownloadWithHashMismatch() 304 { 305 // Find package 306 var downloadDir = TestCommon.GetRandomTestDir(); 307 var searchResult = this.FindOnePackage(this.testSource, PackageMatchField.Id, PackageFieldMatchOption.Equals, "AppInstallerTest.TestExeSha256Mismatch"); 308 309 // Configure installation 310 var downloadOptions = this.TestFactory.CreateDownloadOptions(); 311 downloadOptions.AcceptPackageAgreements = true; 312 downloadOptions.DownloadDirectory = downloadDir; 313 314 // Download 315 var downloadResult = await this.packageManager.DownloadPackageAsync(searchResult.CatalogPackage, downloadOptions); 316 317 // Assert 318 Assert.AreEqual(DownloadResultStatus.DownloadError, downloadResult.Status); 319 } 320 } 321 }