winget-cli

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

WinGetUtilSQLiteIndex.cs (6133B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="WinGetUtilSQLiteIndex.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace AppInstallerCLIE2ETests.WinGetUtil
      8 {
      9     using System;
     10     using System.IO;
     11     using System.Runtime.InteropServices;
     12     using AppInstallerCLIE2ETests.Helpers;
     13     using NUnit.Framework;
     14 
     15     /// <summary>
     16     /// WinGetUtil sql index tests.
     17     /// </summary>
     18     public class WinGetUtilSQLiteIndex
     19     {
     20         private readonly uint majorVersion = 1;
     21         private readonly uint minorVersion = 2;
     22 
     23         // Manifest example 1
     24         private readonly string addManifestsFile = TestCommon.GetTestDataFile(@"WinGetUtil\Manifests\Merged\WinGetUtilTest.Add.yaml");
     25         private readonly string updateManifestsFile = TestCommon.GetTestDataFile(@"WinGetUtil\Manifests\Merged\WinGetUtilTest.Update.yaml");
     26         private readonly string relativePath = @"manifests\a\AppInstallerTest\WinGetUtilTest\1.0.0.0\WinGetTest.yaml";
     27 
     28         private string sqlitePath;
     29 
     30         /// <summary>
     31         /// Set up.
     32         /// </summary>
     33         [SetUp]
     34         public void SetUp()
     35         {
     36              this.sqlitePath = TestCommon.GetRandomTestFile(".sql");
     37         }
     38 
     39         /// <summary>
     40         /// Test add manifest.
     41         /// </summary>
     42         [Test]
     43         public void WinGetUtil_SQLiteIndex_AddManifest()
     44         {
     45             this.SQLiteIndex((indexHandle) =>
     46             {
     47                 // Add manifest
     48                 WinGetUtilWrapper.WinGetSQLiteIndexAddManifest(indexHandle, this.addManifestsFile, this.relativePath);
     49             });
     50         }
     51 
     52         /// <summary>
     53         /// Test update manifest.
     54         /// </summary>
     55         [Test]
     56         public void WinGetUtil_SQLiteIndex_UpdateManifest_Success()
     57         {
     58             this.SQLiteIndex((indexHandle) =>
     59             {
     60                 // Add manifest
     61                 WinGetUtilWrapper.WinGetSQLiteIndexAddManifest(indexHandle, this.addManifestsFile, this.relativePath);
     62 
     63                 // Update manifest
     64                 WinGetUtilWrapper.WinGetSQLiteIndexUpdateManifest(indexHandle, this.updateManifestsFile, this.relativePath, out bool indexModified);
     65                 Assert.True(indexModified);
     66             });
     67         }
     68 
     69         /// <summary>
     70         /// Test update manifest file not found.
     71         /// </summary>
     72         [Test]
     73         public void WinGetUtil_SQLiteIndex_UpdateManifest_Fail_NotFound()
     74         {
     75             this.SQLiteIndex((indexHandle) =>
     76             {
     77                 // Update non-existing manifest
     78                 Assert.Throws<COMException>(() =>
     79                 {
     80                     WinGetUtilWrapper.WinGetSQLiteIndexUpdateManifest(indexHandle, this.updateManifestsFile, this.relativePath, out bool indexModified);
     81                 });
     82             });
     83         }
     84 
     85         /// <summary>
     86         /// Test remove manifest.
     87         /// </summary>
     88         [Test]
     89         public void WinGetUtil_SQLiteIndex_RemoveManifest_Success()
     90         {
     91             this.SQLiteIndex((indexHandle) =>
     92             {
     93                 // Add manifest
     94                 WinGetUtilWrapper.WinGetSQLiteIndexAddManifest(indexHandle, this.addManifestsFile, this.relativePath);
     95 
     96                 // Remove manifest
     97                 WinGetUtilWrapper.WinGetSQLiteIndexRemoveManifest(indexHandle, this.addManifestsFile, this.relativePath);
     98             });
     99         }
    100 
    101         /// <summary>
    102         /// Test remove manifest file not found.
    103         /// </summary>
    104         [Test]
    105         public void WinGetUtil_SQLiteIndex_RemoveManifest_Fail_NotFound()
    106         {
    107             this.SQLiteIndex((indexHandle) =>
    108             {
    109                 // Remove non-existing manifest
    110                 Assert.Throws<COMException>(() =>
    111                 {
    112                     WinGetUtilWrapper.WinGetSQLiteIndexRemoveManifest(indexHandle, this.addManifestsFile, this.relativePath);
    113                 });
    114             });
    115         }
    116 
    117         /// <summary>
    118         /// Test open and closing index.
    119         /// </summary>
    120         [Test]
    121         public void WinGetUtil_SQLiteIndex_OpenClose()
    122         {
    123             this.SQLiteIndex((_) =>
    124             {
    125                 // Open
    126                 WinGetUtilWrapper.WinGetSQLiteIndexOpen(this.sqlitePath, out IntPtr indexHandle);
    127 
    128                 // Add manifest
    129                 WinGetUtilWrapper.WinGetSQLiteIndexAddManifest(indexHandle, this.addManifestsFile, this.relativePath);
    130 
    131                 // Close
    132                 WinGetUtilWrapper.WinGetSQLiteIndexClose(indexHandle);
    133             });
    134         }
    135 
    136         /// <summary>
    137         /// Test check consistency.
    138         /// </summary>
    139         [Test]
    140         public void WinGetUtil_SQLiteIndex_CheckConsistency()
    141         {
    142             this.SQLiteIndex((indexHandle) =>
    143             {
    144                 // Add manifest
    145                 WinGetUtilWrapper.WinGetSQLiteIndexAddManifest(indexHandle, this.addManifestsFile, this.relativePath);
    146 
    147                 // Prepare for packaging
    148                 WinGetUtilWrapper.WinGetSQLiteIndexPrepareForPackaging(indexHandle);
    149 
    150                 // Check consistency
    151                 WinGetUtilWrapper.WinGetSQLiteIndexCheckConsistency(indexHandle, out bool succeeded);
    152                 Assert.True(succeeded);
    153             });
    154         }
    155 
    156         /// <summary>
    157         /// Create and close an sqlite index file.
    158         /// </summary>
    159         /// <param name="execute">Function to execute.</param>
    160         private void SQLiteIndex(Action<IntPtr> execute)
    161         {
    162             // Create
    163             WinGetUtilWrapper.WinGetSQLiteIndexCreate(this.sqlitePath, this.majorVersion, this.minorVersion, out IntPtr indexHandle);
    164             Assert.True(File.Exists(this.sqlitePath));
    165             Assert.AreNotEqual(IntPtr.Zero, indexHandle);
    166 
    167             // Execute provided function
    168             execute(indexHandle);
    169 
    170             // Close
    171             WinGetUtilWrapper.WinGetSQLiteIndexClose(indexHandle);
    172         }
    173     }
    174 }