| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Dtf.Test |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Text; |
| 8 | using System.Collections.Generic; |
| 9 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 10 | using WixToolset.Dtf.WindowsInstaller; |
| 11 | |
| 12 | [TestClass] |
| 13 | public class CustomActionTest |
| 14 | { |
| 15 | public CustomActionTest() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | [TestMethod] |
| 20 | [Ignore] // Currently fails. |
| 21 | public void CustomActionTest1() |
| 22 | { |
| 23 | InstallLogModes logEverything = |
| 24 | InstallLogModes.FatalExit | |
| 25 | InstallLogModes.Error | |
| 26 | InstallLogModes.Warning | |
| 27 | InstallLogModes.User | |
| 28 | InstallLogModes.Info | |
| 29 | InstallLogModes.ResolveSource | |
| 30 | InstallLogModes.OutOfDiskSpace | |
| 31 | InstallLogModes.ActionStart | |
| 32 | InstallLogModes.ActionData | |
| 33 | InstallLogModes.CommonData | |
| 34 | InstallLogModes.Progress | |
| 35 | InstallLogModes.Initialize | |
| 36 | InstallLogModes.Terminate | |
| 37 | InstallLogModes.ShowDialog; |
| 38 | |
| 39 | Installer.SetInternalUI(InstallUIOptions.Silent); |
| 40 | ExternalUIHandler prevHandler = Installer.SetExternalUI( |
| 41 | WindowsInstallerTest.ExternalUILogger, logEverything); |
| 42 | |
| 43 | try |
| 44 | { |
| 45 | string[] customActions = new string[] { "SampleCA1", "SampleCA2" }; |
| 46 | #if DEBUG |
| 47 | string caDir = @"..\..\..\..\..\build\debug\x86\"; |
| 48 | #else |
| 49 | string caDir = @"..\..\..\..\..\build\ship\x86\"; |
| 50 | #endif |
| 51 | caDir = Path.GetFullPath(caDir); |
| 52 | string caFile = "WixToolset.Dtf.Samples.ManagedCA.dll"; |
| 53 | string caProduct = "CustomActionTest.msi"; |
| 54 | |
| 55 | this.CreateCustomActionProduct(caProduct, caDir + caFile, customActions, false); |
| 56 | |
| 57 | Exception caughtEx = null; |
| 58 | try |
| 59 | { |
| 60 | Installer.InstallProduct(caProduct, String.Empty); |
| 61 | } |
| 62 | catch (Exception ex) { caughtEx = ex; } |
| 63 | Assert.IsInstanceOfType(caughtEx, typeof(InstallCanceledException), |
| 64 | "Exception thrown while installing product: " + caughtEx); |
| 65 | |
| 66 | string arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"); |
| 67 | string arch2 = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"); |
| 68 | if (arch == "AMD64" || arch2 == "AMD64") |
| 69 | { |
| 70 | caDir = caDir.Replace("x86", "x64"); |
| 71 | |
| 72 | this.CreateCustomActionProduct(caProduct, caDir + caFile, customActions, true); |
| 73 | |
| 74 | caughtEx = null; |
| 75 | try |
| 76 | { |
| 77 | Installer.InstallProduct(caProduct, String.Empty); |
| 78 | } |
| 79 | catch (Exception ex) { caughtEx = ex; } |
| 80 | Assert.IsInstanceOfType(caughtEx, typeof(InstallCanceledException), |
| 81 | "Exception thrown while installing 64bit product: " + caughtEx); |
| 82 | } |
| 83 | } |
| 84 | finally |
| 85 | { |
| 86 | Installer.SetExternalUI(prevHandler, InstallLogModes.None); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void CreateCustomActionProduct( |
| 91 | string msiFile, string customActionFile, IList<string> customActions, bool sixtyFourBit) |
| 92 | { |
| 93 | using (Database db = new Database(msiFile, DatabaseOpenMode.CreateDirect)) |
| 94 | { |
| 95 | WindowsInstallerUtils.InitializeProductDatabase(db, sixtyFourBit); |
| 96 | WindowsInstallerUtils.CreateTestProduct(db); |
| 97 | |
| 98 | if (!File.Exists(customActionFile)) |
| 99 | throw new FileNotFoundException(customActionFile); |
| 100 | |
| 101 | using (Record binRec = new Record(2)) |
| 102 | { |
| 103 | binRec[1] = Path.GetFileName(customActionFile); |
| 104 | binRec.SetStream(2, customActionFile); |
| 105 | |
| 106 | db.Execute("INSERT INTO `Binary` (`Name`, `Data`) VALUES (?, ?)", binRec); |
| 107 | } |
| 108 | |
| 109 | using (Record binRec2 = new Record(2)) |
| 110 | { |
| 111 | binRec2[1] = "TestData"; |
| 112 | binRec2.SetStream(2, new MemoryStream(Encoding.UTF8.GetBytes("This is a test data stream."))); |
| 113 | |
| 114 | db.Execute("INSERT INTO `Binary` (`Name`, `Data`) VALUES (?, ?)", binRec2); |
| 115 | } |
| 116 | |
| 117 | for (int i = 0; i < customActions.Count; i++) |
| 118 | { |
| 119 | db.Execute( |
| 120 | "INSERT INTO `CustomAction` (`Action`, `Type`, `Source`, `Target`) VALUES ('{0}', 1, '{1}', '{2}')", |
| 121 | customActions[i], |
| 122 | Path.GetFileName(customActionFile), |
| 123 | customActions[i]); |
| 124 | db.Execute( |
| 125 | "INSERT INTO `InstallExecuteSequence` (`Action`, `Condition`, `Sequence`) VALUES ('{0}', '', {1})", |
| 126 | customActions[i], |
| 127 | 101 + i); |
| 128 | } |
| 129 | |
| 130 | db.Execute("INSERT INTO `Property` (`Property`, `Value`) VALUES ('SampleCATest', 'TestValue')"); |
| 131 | |
| 132 | db.Commit(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | [TestMethod] |
| 137 | public void CustomActionData() |
| 138 | { |
| 139 | string dataString = "Key1=Value1;Key2=;Key3;Key4=Value=4;Key5"; |
| 140 | string dataString2 = "Key1=;Key2=Value2;Key3;Key4;Key6=Value;;6=6;Key7=Value7"; |
| 141 | |
| 142 | CustomActionData data = new CustomActionData(dataString); |
| 143 | Assert.AreEqual<string>(dataString, data.ToString()); |
| 144 | |
| 145 | data["Key1"] = String.Empty; |
| 146 | data["Key2"] = "Value2"; |
| 147 | data["Key4"] = null; |
| 148 | data.Remove("Key5"); |
| 149 | data["Key6"] = "Value;6=6"; |
| 150 | data["Key7"] = "Value7"; |
| 151 | |
| 152 | Assert.AreEqual<string>(dataString2, data.ToString()); |
| 153 | |
| 154 | MyDataClass myData = new MyDataClass(); |
| 155 | myData.Member1 = "test1"; |
| 156 | myData.Member2 = "test2"; |
| 157 | data.AddObject("MyData", myData); |
| 158 | |
| 159 | string myDataString = data.ToString(); |
| 160 | CustomActionData data2 = new CustomActionData(myDataString); |
| 161 | |
| 162 | MyDataClass myData2 = data2.GetObject<MyDataClass>("MyData"); |
| 163 | Assert.AreEqual<MyDataClass>(myData, myData2); |
| 164 | |
| 165 | List<string> myComplexDataObject = new List<string>(); |
| 166 | myComplexDataObject.Add("CValue1"); |
| 167 | myComplexDataObject.Add("CValue2"); |
| 168 | myComplexDataObject.Add("CValue3"); |
| 169 | |
| 170 | CustomActionData myComplexData = new CustomActionData(); |
| 171 | myComplexData.AddObject("MyComplexData", myComplexDataObject); |
| 172 | myComplexData.AddObject("NestedData", data); |
| 173 | string myComplexDataString = myComplexData.ToString(); |
| 174 | |
| 175 | CustomActionData myComplexData2 = new CustomActionData(myComplexDataString); |
| 176 | List<string> myComplexDataObject2 = myComplexData2.GetObject<List<string>>("MyComplexData"); |
| 177 | |
| 178 | Assert.AreEqual<int>(myComplexDataObject.Count, myComplexDataObject2.Count); |
| 179 | for (int i = 0; i < myComplexDataObject.Count; i++) |
| 180 | { |
| 181 | Assert.AreEqual<string>(myComplexDataObject[i], myComplexDataObject2[i]); |
| 182 | } |
| 183 | |
| 184 | data2 = myComplexData2.GetObject<CustomActionData>("NestedData"); |
| 185 | Assert.AreEqual<string>(data.ToString(), data2.ToString()); |
| 186 | } |
| 187 | |
| 188 | public class MyDataClass |
| 189 | { |
| 190 | public string Member1; |
| 191 | public string Member2; |
| 192 | |
| 193 | public override bool Equals(object obj) |
| 194 | { |
| 195 | MyDataClass other = obj as MyDataClass; |
| 196 | return other != null && this.Member1 == other.Member1 && this.Member2 == other.Member2; |
| 197 | } |
| 198 | |
| 199 | public override int GetHashCode() |
| 200 | { |
| 201 | return (this.Member1 != null ? this.Member1.GetHashCode() : 0) ^ |
| 202 | (this.Member2 != null ? this.Member2.GetHashCode() : 0); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |