public static void CreateTestProduct(Database db)
        {
            Guid productGuid = Guid.NewGuid();

            string[] properties = new string[]
            {
                "ProductCode", productGuid.ToString("B").ToUpper(),
                "UpgradeCode", UpgradeCode,
                "ProductName", "Windows Installer Test Product " + productGuid.ToString("P").ToUpper(),
                "ProductVersion", "1.0.0.0000",
            };

            using (View view = db.OpenView("INSERT INTO `Property` (`Property`, `Value`) VALUES (?, ?)"))
            {
                using (Record rec = new Record(2))
                {
                    for (int i = 0; i < properties.Length; i += 2)
                    {
                        rec[1] = properties[i];
                        rec[2] = properties[i + 1];
                        view.Execute(rec);
                    }
                }
            }

            int randomId = new Random().Next(10000);
            string productDir = "TestDir" + randomId;
            db.Execute(
                "INSERT INTO `Directory` (`Directory`, `Directory_Parent`, `DefaultDir`) " +
                "VALUES ('TestDir', 'ProgramFilesFolder', 'TestDir|{0}:.')", productDir);

            string compId = Guid.NewGuid().ToString("B").ToUpper();
            db.Execute(
                "INSERT INTO `Component` " +
                    "(`Component`, `ComponentId`, `Directory_`, `Attributes`, `KeyPath`) " +
                    "VALUES ('{0}', '{1}', '{2}', {3}, '{4}')",
                "TestRegComp1",
                compId,
                "TestDir",
                (int) ComponentAttributes.RegistryKeyPath,
                "TestReg1");

            string productReg = "TestReg" + randomId;
            db.Execute(
                "INSERT INTO `Registry` (`Registry`, `Root`, `Key`, `Component_`) VALUES ('{0}', {1}, '{2}', '{3}')",
                "TestReg1",
                -1,
                @"Software\Microsoft\Windows Installer Test\" + productReg,
                "TestRegComp1");

            db.Execute(
                "INSERT INTO `Feature` (`Feature`, `Title`, `Level`, `Attributes`) VALUES ('{0}', '{1}', {2}, {3})",
                "TestFeature1",
                "Test Feature 1",
                1,
                (int) FeatureAttributes.None);

            db.Execute(
                "INSERT INTO `FeatureComponents` (`Feature_`, `Component_`) VALUES ('{0}', '{1}')",
                "TestFeature1",
                "TestRegComp1");
        }
        public static void InitializeProductDatabase(Database db, bool sixtyFourBit)
        {
            db.SummaryInfo.CodePage = (short) Encoding.Default.CodePage;
            db.SummaryInfo.Title = "Windows Installer Test";
            db.SummaryInfo.Subject = db.SummaryInfo.Title;
            db.SummaryInfo.Author = typeof(WindowsInstallerUtils).Assembly.FullName;
            db.SummaryInfo.CreatingApp = db.SummaryInfo.Author;
            db.SummaryInfo.Comments = typeof(WindowsInstallerUtils).FullName + ".CreateBasicDatabase()";
            db.SummaryInfo.Keywords = "Installer,MSI,Database";
            db.SummaryInfo.PageCount = 300;
            db.SummaryInfo.WordCount = 0;
            db.SummaryInfo.RevisionNumber = Guid.NewGuid().ToString("B").ToUpper();
            db.SummaryInfo.Template = (sixtyFourBit ? "x64" : "Intel") + ";0";

            foreach (TableInfo tableInfo in Schema.Tables)
            {
                db.Execute(tableInfo.SqlCreateString);
            }

            db.Execute("INSERT INTO `Directory` (`Directory`, `DefaultDir`) VALUES ('TARGETDIR', 'SourceDir')");
            db.Execute("INSERT INTO `Directory` (`Directory`, `Directory_Parent`, `DefaultDir`) VALUES ('ProgramFilesFolder', 'TARGETDIR', '.')");

            foreach (Action action in Sequence.InstallExecute)
            {
                db.Execute("INSERT INTO `InstallExecuteSequence` (`Action`, `Sequence`) VALUES ('{0}', {1})",
                    action.Name, action.Sequence);
            }
        }
Beispiel #3
0
 public static void AddRegistryComponent(Database db,
     string featureName, string compName, string compId,
     string keyName, string keyValueName, string value)
 {
     db.Execute(
         "INSERT INTO `Component` " +
             "(`Component`, `ComponentId`, `Directory_`, `Attributes`, `KeyPath`) " +
             "VALUES ('{0}', '{1}', '{2}', {3}, '{4}')",
         compName,
         compId,
         "TestDir",
         (int) ComponentAttributes.RegistryKeyPath,
         compName + "Reg1");
     db.Execute(
         "INSERT INTO `Registry` (`Registry`, `Root`, `Key`, `Name`, `Value`, `Component_`) VALUES ('{0}', {1}, '{2}', '{3}', '{4}', '{5}')",
         compName + "Reg1",
         -1,
         @"Software\Microsoft\Windows Installer Test\" + keyName,
         keyValueName,
         value,
         compName);
     db.Execute(
         "INSERT INTO `FeatureComponents` (`Feature_`, `Component_`) VALUES ('{0}', '{1}')",
         featureName,
         compName);
 }
Beispiel #4
0
 public static void AddFileComponent(Database db,
     string featureName, string compName, string compId,
     string fileKey, string fileName)
 {
     db.Execute(
         "INSERT INTO `Component` " +
             "(`Component`, `ComponentId`, `Directory_`, `Attributes`, `KeyPath`) " +
             "VALUES ('{0}', '{1}', '{2}', {3}, '{4}')",
         compName,
         compId,
         "TestDir",
         (int) ComponentAttributes.None,
         fileKey);
     db.Execute(
         "INSERT INTO `File` " +
             "(`File`, `Component_`, `FileName`, `FileSize`, `Attributes`, `Sequence`) " +
             "VALUES ('{0}', '{1}', '{2}', 1, 0, 1)",
         fileKey,
         compName,
         fileName);
     db.Execute(
         "INSERT INTO `FeatureComponents` (`Feature_`, `Component_`) VALUES ('{0}', '{1}')",
         featureName,
         compName);
 }
Beispiel #5
0
 public static void AddFeature(Database db, string featureName)
 {
     db.Execute(
         "INSERT INTO `Feature` (`Feature`, `Title`, `Level`, `Attributes`) VALUES ('{0}', '{1}', {2}, {3})",
         featureName,
         featureName,
         1,
         (int) FeatureAttributes.None);
 }
Beispiel #6
0
        private void CreateCustomActionProduct(
            string msiFile, string customActionFile, IList<string> customActions, bool sixtyFourBit)
        {
            using (Database db = new Database(msiFile, DatabaseOpenMode.CreateDirect))
            {
                WindowsInstallerUtils.InitializeProductDatabase(db, sixtyFourBit);
                WindowsInstallerUtils.CreateTestProduct(db);

                if (!File.Exists(customActionFile))
                    throw new FileNotFoundException(customActionFile);

                using (Record binRec = new Record(2))
                {
                    binRec[1] = Path.GetFileName(customActionFile);
                    binRec.SetStream(2, customActionFile);

                    db.Execute("INSERT INTO `Binary` (`Name`, `Data`) VALUES (?, ?)", binRec);
                }

                using (Record binRec2 = new Record(2))
                {
                    binRec2[1] = "TestData";
                    binRec2.SetStream(2, new MemoryStream(Encoding.UTF8.GetBytes("This is a test data stream.")));

                    db.Execute("INSERT INTO `Binary` (`Name`, `Data`) VALUES (?, ?)", binRec2);
                }

                for (int i = 0; i < customActions.Count; i++)
                {
                    db.Execute(
                        "INSERT INTO `CustomAction` (`Action`, `Type`, `Source`, `Target`) VALUES ('{0}', 1, '{1}', '{2}')",
                        customActions[i],
                        Path.GetFileName(customActionFile),
                        customActions[i]);
                    db.Execute(
                        "INSERT INTO `InstallExecuteSequence` (`Action`, `Condition`, `Sequence`) VALUES ('{0}', '', {1})",
                        customActions[i],
                        101 + i);
                }

                db.Execute("INSERT INTO `Property` (`Property`, `Value`) VALUES ('SampleCATest', 'TestValue')");

                db.Commit();
            }
        }
Beispiel #7
0
        /// <summary>
        /// adds or updates a property in an msi file.
        /// </summary>
        /// <param name="msiFile">full path to the msi file to update</param>
        /// <param name="propertyName">name of property to add or update</param>
        /// <param name="propertyValue">value of property</param>
        public static void SetPropertyInMsi(string msiFile, string propertyName, string propertyValue)
        {
            Database db = new Database(msiFile, DatabaseOpenMode.Direct);

            string sql;
            try
            {
                // GetProperty will throw if the propertyName does not exist in the property table
                GetProperty(db, propertyName);
                sql = string.Format("UPDATE `Property` SET `Value` = '{0}' WHERE `Property` = '{1}'", propertyValue, propertyName);
            }
            catch
            {
                sql = string.Format("INSERT INTO `Property` (`Property`, `Value`) VALUES ('{0}', '{1}')", propertyName, propertyValue);
            }
            db.Execute(sql);
            db.Commit();
            db.Close();
        }
Beispiel #8
0
        // This test does not pass if run normally.
        // It only passes when a failure is injected into the EmbeddedUI launcher.
        ////[TestMethod]
        public void EmbeddedUIInitializeFails()
        {
            string dbFile = "EmbeddedUIInitializeFails.msi";
            string productCode;

            string uiDir = Path.GetFullPath(EmbeddedExternalUI.EmbeddedUISampleBinDir);
            string uiFile = "Microsoft.Deployment.Samples.EmbeddedUI.dll";

            // A number that will be used to check whether a type 19 CA runs.
            const string magicNumber = "3.14159265358979323846264338327950";

            using (Database db = new Database(dbFile, DatabaseOpenMode.CreateDirect))
            {
                WindowsInstallerUtils.InitializeProductDatabase(db);
                WindowsInstallerUtils.CreateTestProduct(db);

                const string failureActionName = "EmbeddedUIInitializeFails";
                db.Execute("INSERT INTO `CustomAction` (`Action`, `Type`, `Source`, `Target`) " +
                    "VALUES ('{0}', 19, '', 'Logging magic number: {1}')", failureActionName, magicNumber);

                // This type 19 CA (launch condition) is given a condition of 'UILevel = 3' so that it only runs if the
                // installation is running in BASIC UI mode, which is what we expect if the EmbeddedUI fails to initialize.
                db.Execute("INSERT INTO `InstallExecuteSequence` (`Action`, `Condition`, `Sequence`) " +
                    "VALUES ('{0}', 'UILevel = 3', 1)", failureActionName);

                productCode = db.ExecuteStringQuery("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductCode'")[0];

                using (Record uiRec = new Record(5))
                {
                    uiRec[1] = "TestEmbeddedUI";
                    uiRec[2] = Path.GetFileNameWithoutExtension(uiFile) + ".Wrapper.dll";
                    uiRec[3] = 1;
                    uiRec[4] = (int)(
                        EmbeddedExternalUI.TestLogModes |
                        InstallLogModes.Progress |
                        InstallLogModes.Initialize |
                        InstallLogModes.Terminate |
                        InstallLogModes.ShowDialog);
                    uiRec.SetStream(5, Path.Combine(uiDir, uiFile));
                    db.Execute(db.Tables["MsiEmbeddedUI"].SqlInsertString, uiRec);
                }

                db.Commit();
            }

            Installer.SetInternalUI(InstallUIOptions.Full);

            ProductInstallation installation = new ProductInstallation(productCode);
            Assert.IsFalse(installation.IsInstalled, "Checking that product is not installed before starting.");

            string logFile = "install.log";
            Exception caughtEx = null;
            try
            {
                Installer.EnableLog(EmbeddedExternalUI.TestLogModes, logFile);
                Installer.InstallProduct(dbFile, String.Empty);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(InstallerException),
                "Excpected InstallerException installing product; caught: " + caughtEx);

            Assert.IsFalse(installation.IsInstalled, "Checking that product is not installed.");

            string logText = File.ReadAllText(logFile);
            Assert.IsTrue(logText.Contains(magicNumber), "Checking that the type 19 custom action ran.");
        }
Beispiel #9
0
        public void EmbeddedUISingleInstall()
        {
            string dbFile = "EmbeddedUISingleInstall.msi";
            string productCode;

            string uiDir = Path.GetFullPath(EmbeddedExternalUI.EmbeddedUISampleBinDir);
            string uiFile = "Microsoft.Deployment.Samples.EmbeddedUI.dll";

            using (Database db = new Database(dbFile, DatabaseOpenMode.CreateDirect))
            {
                WindowsInstallerUtils.InitializeProductDatabase(db);
                WindowsInstallerUtils.CreateTestProduct(db);

                productCode = db.ExecuteStringQuery("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductCode'")[0];

                using (Record uiRec = new Record(5))
                {
                    uiRec[1] = "TestEmbeddedUI";
                    uiRec[2] = Path.GetFileNameWithoutExtension(uiFile) + ".Wrapper.dll";
                    uiRec[3] = 1;
                    uiRec[4] = (int) (
                        EmbeddedExternalUI.TestLogModes |
                        InstallLogModes.Progress |
                        InstallLogModes.Initialize |
                        InstallLogModes.Terminate |
                        InstallLogModes.ShowDialog);
                    uiRec.SetStream(5, Path.Combine(uiDir, uiFile));
                    db.Execute(db.Tables["MsiEmbeddedUI"].SqlInsertString, uiRec);
                }

                db.Commit();
            }

            Installer.SetInternalUI(InstallUIOptions.Full);

            ProductInstallation installation = new ProductInstallation(productCode);
            Assert.IsFalse(installation.IsInstalled, "Checking that product is not installed before starting.");

            Exception caughtEx = null;
            try
            {
                Installer.EnableLog(EmbeddedExternalUI.TestLogModes, "install.log");
                Installer.InstallProduct(dbFile, String.Empty);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsNull(caughtEx, "Exception thrown while installing product: " + caughtEx);

            Assert.IsTrue(installation.IsInstalled, "Checking that product is installed.");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("===================================================================");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            try
            {
                Installer.EnableLog(EmbeddedExternalUI.TestLogModes, "uninstall.log");
                Installer.InstallProduct(dbFile, "REMOVE=All");
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsNull(caughtEx, "Exception thrown while uninstalling product: " + caughtEx);
        }