winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

PackageCatalogInterop.cs (15951B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="PackageCatalogInterop.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 namespace AppInstallerCLIE2ETests.Interop
      7 {
      8     using System;
      9     using System.IO;
     10     using System.Threading;
     11     using System.Threading.Tasks;
     12     using AppInstallerCLIE2ETests.Helpers;
     13     using Microsoft.CodeAnalysis;
     14     using Microsoft.Management.Deployment;
     15     using Microsoft.Management.Deployment.Projection;
     16     using NUnit.Framework;
     17     using Windows.System;
     18 
     19     /// <summary>
     20     /// Package catalog interop class.
     21     /// </summary>
     22     [TestFixtureSource(typeof(InstanceInitializersSource), nameof(InstanceInitializersSource.InProcess), Category = nameof(InstanceInitializersSource.InProcess))]
     23     [TestFixtureSource(typeof(InstanceInitializersSource), nameof(InstanceInitializersSource.OutOfProcess), Category = nameof(InstanceInitializersSource.OutOfProcess))]
     24     public class PackageCatalogInterop : BaseInterop
     25     {
     26         private PackageManager packageManager;
     27 
     28         /// <summary>
     29         /// Initializes a new instance of the <see cref="PackageCatalogInterop"/> class.
     30         /// </summary>
     31         /// <param name="initializer">initializer.</param>
     32         public PackageCatalogInterop(IInstanceInitializer initializer)
     33             : base(initializer)
     34         {
     35         }
     36 
     37         /// <summary>
     38         /// Set up.
     39         /// </summary>
     40         [SetUp]
     41         public void Setup()
     42         {
     43             // Remove the tests source if it exists.
     44             TestCommon.RunAICLICommand("source remove", Constants.TestSourceName);
     45 
     46             this.packageManager = this.TestFactory.CreatePackageManager();
     47         }
     48 
     49         /// <summary>
     50         /// Add and remove package catalog.
     51         /// </summary>
     52         /// <returns> representing the asynchronous unit test.</returns>
     53         [Test]
     54         public async Task AddRemovePackageCatalog()
     55         {
     56             AddPackageCatalogOptions options = this.TestFactory.CreateAddPackageCatalogOptions();
     57             options.SourceUri = Constants.TestSourceUrl;
     58             options.Name = Constants.TestSourceName;
     59             options.TrustLevel = PackageCatalogTrustLevel.Trusted;
     60 
     61             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.Ok);
     62 
     63             RemovePackageCatalogOptions removePackageCatalogOptions = this.TestFactory.CreateRemovePackageCatalogOptions();
     64             removePackageCatalogOptions.Name = Constants.TestSourceName;
     65             var removeCatalogResult = await this.packageManager.RemovePackageCatalogAsync(removePackageCatalogOptions);
     66             Assert.IsNotNull(removeCatalogResult);
     67             Assert.AreEqual(RemovePackageCatalogStatus.Ok, removeCatalogResult.Status);
     68 
     69             var testSource = this.packageManager.GetPackageCatalogByName(Constants.TestSourceName);
     70             Assert.IsNull(testSource);
     71         }
     72 
     73         /// <summary>
     74         /// Add package catalog with invalid options.
     75         /// </summary>
     76         [Test]
     77         public void AddPackageCatalogWithInvalidOptions()
     78         {
     79             // Add package catalog with null options.
     80             Assert.ThrowsAsync<NullReferenceException>(async () => await this.packageManager.AddPackageCatalogAsync(null));
     81 
     82             // Add package catalog with empty options.
     83             Assert.ThrowsAsync<ArgumentException>(async () => await this.packageManager.AddPackageCatalogAsync(this.TestFactory.CreateAddPackageCatalogOptions()));
     84         }
     85 
     86         /// <summary>
     87         /// Add package catalog with a duplicate name and verify it returns SourceNameExists.
     88         /// </summary>
     89         /// <returns>representing the asynchronous unit test.</returns>
     90         [Test]
     91         public async Task AddPackageCatalog_DuplicateName_ReturnsSourceNameExists()
     92         {
     93             AddPackageCatalogOptions options = this.TestFactory.CreateAddPackageCatalogOptions();
     94             options.SourceUri = Constants.TestSourceUrl;
     95             options.Name = Constants.TestSourceName;
     96             options.TrustLevel = PackageCatalogTrustLevel.Trusted;
     97 
     98             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.Ok);
     99 
    100             // Add the same package catalog again.
    101             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.InvalidOptions, Constants.ErrorCode.ERROR_SOURCE_NAME_ALREADY_EXISTS);
    102 
    103             // Remove the tests source if it exists.
    104             RemovePackageCatalogOptions removePackageCatalogOptions = this.TestFactory.CreateRemovePackageCatalogOptions();
    105             removePackageCatalogOptions.Name = Constants.TestSourceName;
    106             await this.RemoveAndValidatePackageCatalogAsync(removePackageCatalogOptions, RemovePackageCatalogStatus.Ok);
    107         }
    108 
    109         /// <summary>
    110         /// Add package catalog with a duplicate source uri and verify it returns SourceArg AlreadyExists.
    111         /// </summary>
    112         /// <returns>representing the asynchronous unit test.</returns>
    113         [Test]
    114         public async Task AddPackageCatalog_DuplicateSourceUri_ReturnSourceArgAlreadyExists()
    115         {
    116             AddPackageCatalogOptions options = this.TestFactory.CreateAddPackageCatalogOptions();
    117             options.SourceUri = Constants.TestSourceUrl;
    118             options.Name = Constants.TestSourceName;
    119             options.TrustLevel = PackageCatalogTrustLevel.Trusted;
    120 
    121             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.Ok);
    122 
    123             // Add the same package catalog again.
    124             options.Name = "TestSource2";
    125             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.InvalidOptions, Constants.ErrorCode.ERROR_SOURCE_ARG_ALREADY_EXISTS);
    126 
    127             // Remove the tests source if it exists.
    128             RemovePackageCatalogOptions removePackageCatalogOptions = this.TestFactory.CreateRemovePackageCatalogOptions();
    129             removePackageCatalogOptions.Name = Constants.TestSourceName;
    130             await this.RemoveAndValidatePackageCatalogAsync(removePackageCatalogOptions, RemovePackageCatalogStatus.Ok);
    131         }
    132 
    133         /// <summary>
    134         /// Add package catalog with invalid source uri.
    135         /// </summary>
    136         /// <returns>representing the asynchronous unit test.</returns>
    137         [Test]
    138         public async Task AddPackageCatalogWithInvalidSourceUri()
    139         {
    140             AddPackageCatalogOptions options = this.TestFactory.CreateAddPackageCatalogOptions();
    141             options.SourceUri = "InvalidUri";
    142             options.Name = Constants.TestSourceName;
    143             options.TrustLevel = PackageCatalogTrustLevel.Trusted;
    144 
    145             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.InternalError);
    146         }
    147 
    148         /// <summary>
    149         /// Add package catalog with insecure source uri.
    150         /// </summary>
    151         /// <returns>representing the asynchronous unit test.</returns>
    152         [Test]
    153         public async Task AddPackageCatalogWithHttpSourceUri()
    154         {
    155             AddPackageCatalogOptions options = this.TestFactory.CreateAddPackageCatalogOptions();
    156             options.SourceUri = "http://microsoft.com";
    157             options.Name = "Insecure";
    158             options.TrustLevel = PackageCatalogTrustLevel.Trusted;
    159 
    160             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.InvalidOptions, Constants.ErrorCode.ERROR_SOURCE_NOT_SECURE);
    161         }
    162 
    163         /// <summary>
    164         /// Add package catalog with invalid type.
    165         /// </summary>
    166         /// <returns>representing the asynchronous unit test.</returns>
    167         [Test]
    168         public async Task AddPackageCatalogWithInvalidType()
    169         {
    170             AddPackageCatalogOptions options = this.TestFactory.CreateAddPackageCatalogOptions();
    171             options.SourceUri = Constants.TestSourceUrl;
    172             options.Name = Constants.TestSourceName;
    173             options.Type = "InvalidType";
    174 
    175             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.InvalidOptions, Constants.ErrorCode.ERROR_INVALID_SOURCE_TYPE);
    176         }
    177 
    178         /// <summary>
    179         /// Add, update and remove package catalog.
    180         /// </summary>
    181         /// <returns> representing the asynchronous unit test.</returns>
    182         [Test]
    183         public async Task AddUpdateRemovePackageCatalog()
    184         {
    185             AddPackageCatalogOptions options = this.TestFactory.CreateAddPackageCatalogOptions();
    186             options.SourceUri = Constants.TestSourceUrl;
    187             options.Name = Constants.TestSourceName;
    188             options.TrustLevel = PackageCatalogTrustLevel.Trusted;
    189 
    190             await this.AddCatalogAndVerifyStatusAsync(options, AddPackageCatalogStatus.Ok);
    191 
    192             var packageCatalog = this.GetAndValidatePackageCatalog(options);
    193             var lastUpdatedTime = packageCatalog.Info.LastUpdateTime;
    194 
    195             // Sleep for 30 seconds to make sure the last updated time is different after the refresh.
    196             Thread.Sleep(TimeSpan.FromSeconds(30));
    197 
    198             var updateResult = await packageCatalog.RefreshPackageCatalogAsync();
    199             Assert.IsNotNull(updateResult);
    200             Assert.AreEqual(RefreshPackageCatalogStatus.Ok, updateResult.Status);
    201 
    202             packageCatalog = this.GetAndValidatePackageCatalog(options);
    203             Assert.IsTrue(packageCatalog.Info.LastUpdateTime > lastUpdatedTime);
    204 
    205             RemovePackageCatalogOptions removePackageCatalogOptions = this.TestFactory.CreateRemovePackageCatalogOptions();
    206             removePackageCatalogOptions.Name = Constants.TestSourceName;
    207             await this.RemoveAndValidatePackageCatalogAsync(removePackageCatalogOptions, RemovePackageCatalogStatus.Ok);
    208         }
    209 
    210         /// <summary>
    211         /// Add, remove package catalog with PreserveData filed set..
    212         /// </summary>
    213         /// <returns> representing the asynchronous unit test.</returns>
    214         [Test]
    215         public async Task AddRemovePackageCatalogWithPreserveDataFiledSet()
    216         {
    217             AddPackageCatalogOptions options = this.TestFactory.CreateAddPackageCatalogOptions();
    218             options.SourceUri = Constants.TestSourceUrl;
    219             options.Name = Constants.TestSourceName;
    220             options.TrustLevel = PackageCatalogTrustLevel.Trusted;
    221 
    222             await this.AddAndValidatePackageCatalogAsync(options, AddPackageCatalogStatus.Ok);
    223 
    224             RemovePackageCatalogOptions removePackageCatalogOptions = this.TestFactory.CreateRemovePackageCatalogOptions();
    225             removePackageCatalogOptions.Name = Constants.TestSourceName;
    226             removePackageCatalogOptions.PreserveData = true;
    227 
    228             await this.RemoveAndValidatePackageCatalogAsync(removePackageCatalogOptions, RemovePackageCatalogStatus.Ok);
    229         }
    230 
    231         /// <summary>
    232         /// Remove package catalog with invalid options.
    233         /// </summary>
    234         [Test]
    235         public void RemovePackageCatalogWithInvalidOptions()
    236         {
    237             // Remove package catalog with null options.
    238             Assert.ThrowsAsync<NullReferenceException>(async () => await this.packageManager.RemovePackageCatalogAsync(null));
    239 
    240             // Remove package catalog with empty options.
    241             Assert.ThrowsAsync<ArgumentException>(async () => await this.packageManager.RemovePackageCatalogAsync(this.TestFactory.CreateRemovePackageCatalogOptions()));
    242         }
    243 
    244         /// <summary>
    245         /// Remove a package catalog that is not present.
    246         /// </summary>
    247         /// <returns>representing the asynchronous unit test.</returns>
    248         [Test]
    249         public async Task RemoveNonExistingPackageCatalog()
    250         {
    251             RemovePackageCatalogOptions removePackageCatalogOptions = this.TestFactory.CreateRemovePackageCatalogOptions();
    252             removePackageCatalogOptions.Name = Constants.TestSourceName;
    253 
    254             await this.RemoveAndValidatePackageCatalogAsync(removePackageCatalogOptions, RemovePackageCatalogStatus.InvalidOptions, Constants.ErrorCode.ERROR_SOURCE_NAME_DOES_NOT_EXIST);
    255         }
    256 
    257         /// <summary>
    258         /// Test class Tear down.
    259         /// </summary>
    260         [OneTimeTearDown]
    261         public void TestClassTearDown()
    262         {
    263             // Restore the tests source if it was removed as the affects subsequent tests.
    264             TestCommon.SetupTestSource();
    265         }
    266 
    267         private async Task AddCatalogAndVerifyStatusAsync(AddPackageCatalogOptions addPackageCatalogOptions, AddPackageCatalogStatus expectedStatus, int expectedErrorCode = 0)
    268         {
    269             var addCatalogResult = await this.packageManager.AddPackageCatalogAsync(addPackageCatalogOptions);
    270             Assert.IsNotNull(addCatalogResult);
    271             Assert.AreEqual(expectedStatus, addCatalogResult.Status);
    272 
    273             if (expectedStatus != AddPackageCatalogStatus.Ok && expectedErrorCode != 0)
    274             {
    275                 Assert.AreEqual(expectedErrorCode, addCatalogResult.ExtendedErrorCode.HResult);
    276             }
    277         }
    278 
    279         private PackageCatalogReference GetAndValidatePackageCatalog(AddPackageCatalogOptions addPackageCatalogOptions)
    280         {
    281             var packageCatalog = this.packageManager.GetPackageCatalogByName(addPackageCatalogOptions.Name);
    282 
    283             Assert.IsNotNull(packageCatalog);
    284             Assert.AreEqual(addPackageCatalogOptions.Name, packageCatalog.Info.Name);
    285             Assert.AreEqual(addPackageCatalogOptions.SourceUri, packageCatalog.Info.Argument);
    286 
    287             return packageCatalog;
    288         }
    289 
    290         private async Task AddAndValidatePackageCatalogAsync(AddPackageCatalogOptions addPackageCatalogOptions, AddPackageCatalogStatus expectedStatus, int expectedErrorCode = 0)
    291         {
    292             // Add the package catalog and verify the status
    293             var addCatalogResult = await this.packageManager.AddPackageCatalogAsync(addPackageCatalogOptions);
    294             Assert.IsNotNull(addCatalogResult);
    295             Assert.AreEqual(expectedStatus, addCatalogResult.Status);
    296 
    297             if (expectedStatus != AddPackageCatalogStatus.Ok)
    298             {
    299                 // Only validate the error code if the status is not Ok and the expected error code is not 0
    300                 if (expectedErrorCode != 0)
    301                 {
    302                     Assert.AreEqual(expectedErrorCode, addCatalogResult.ExtendedErrorCode.HResult);
    303                 }
    304 
    305                 return;
    306             }
    307 
    308             // Validate the added package catalog if the status is Ok
    309             this.GetAndValidatePackageCatalog(addPackageCatalogOptions);
    310         }
    311 
    312         private async Task RemoveAndValidatePackageCatalogAsync(RemovePackageCatalogOptions removePackageCatalogOptions, RemovePackageCatalogStatus expectedStatus, int expectedErrorCode = 0)
    313         {
    314             var removeCatalogResult = await this.packageManager.RemovePackageCatalogAsync(removePackageCatalogOptions);
    315             Assert.IsNotNull(removeCatalogResult);
    316             Assert.AreEqual(expectedStatus, removeCatalogResult.Status);
    317 
    318             if (expectedStatus != RemovePackageCatalogStatus.Ok && expectedErrorCode != 0)
    319             {
    320                 Assert.AreEqual(expectedErrorCode, removeCatalogResult.ExtendedErrorCode.HResult);
    321                 return;
    322             }
    323 
    324             var packageCatalog = this.packageManager.GetPackageCatalogByName(removePackageCatalogOptions.Name);
    325             Assert.IsNull(packageCatalog);
    326         }
    327     }
    328 }