hhctrl.ocx: Added hhc parser.
[wine] / dlls / msi / tests / install.c
1 /*
2  * Copyright (C) 2006 James Hawkins
3  *
4  * A test program for installing MSI products.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22
23 #include <windows.h>
24 #include <msiquery.h>
25 #include <msidefs.h>
26 #include <msi.h>
27 #include <fci.h>
28
29 #include "wine/test.h"
30
31 static const char *msifile = "winetest.msi";
32 CHAR CURR_DIR[MAX_PATH];
33 CHAR PROG_FILES_DIR[MAX_PATH];
34
35 /* msi database data */
36
37 static const CHAR component_dat[] = "Component\tComponentId\tDirectory_\tAttributes\tCondition\tKeyPath\n"
38                                     "s72\tS38\ts72\ti2\tS255\tS72\n"
39                                     "Component\tComponent\n"
40                                     "Five\t{8CC92E9D-14B2-4CA4-B2AA-B11D02078087}\tNEWDIR\t2\t\tfive.txt\n"
41                                     "Four\t{FD37B4EA-7209-45C0-8917-535F35A2F080}\tCABOUTDIR\t2\t\tfour.txt\n"
42                                     "One\t{783B242E-E185-4A56-AF86-C09815EC053C}\tMSITESTDIR\t2\t\tone.txt\n"
43                                     "Three\t{010B6ADD-B27D-4EDD-9B3D-34C4F7D61684}\tCHANGEDDIR\t2\t\tthree.txt\n"
44                                     "Two\t{BF03D1A6-20DA-4A65-82F3-6CAC995915CE}\tFIRSTDIR\t2\t\ttwo.txt\n"
45                                     "dangler\t{6091DF25-EF96-45F1-B8E9-A9B1420C7A3C}\tTARGETDIR\t4\t\tregdata\n"
46                                     "component\t\tMSITESTDIR\t0\t1\tfile\n"
47                                     "service_comp\t\tMSITESTDIR\t0\t1\tservice_file";
48
49 static const CHAR directory_dat[] = "Directory\tDirectory_Parent\tDefaultDir\n"
50                                     "s72\tS72\tl255\n"
51                                     "Directory\tDirectory\n"
52                                     "CABOUTDIR\tMSITESTDIR\tcabout\n"
53                                     "CHANGEDDIR\tMSITESTDIR\tchanged:second\n"
54                                     "FIRSTDIR\tMSITESTDIR\tfirst\n"
55                                     "MSITESTDIR\tProgramFilesFolder\tmsitest\n"
56                                     "NEWDIR\tCABOUTDIR\tnew\n"
57                                     "ProgramFilesFolder\tTARGETDIR\t.\n"
58                                     "TARGETDIR\t\tSourceDir";
59
60 static const CHAR feature_dat[] = "Feature\tFeature_Parent\tTitle\tDescription\tDisplay\tLevel\tDirectory_\tAttributes\n"
61                                   "s38\tS38\tL64\tL255\tI2\ti2\tS72\ti2\n"
62                                   "Feature\tFeature\n"
63                                   "Five\t\tFive\tThe Five Feature\t5\t3\tNEWDIR\t0\n"
64                                   "Four\t\tFour\tThe Four Feature\t4\t3\tCABOUTDIR\t0\n"
65                                   "One\t\tOne\tThe One Feature\t1\t3\tMSITESTDIR\t0\n"
66                                   "Three\t\tThree\tThe Three Feature\t3\t3\tCHANGEDDIR\t0\n"
67                                   "Two\t\tTwo\tThe Two Feature\t2\t3\tFIRSTDIR\t0\n"
68                                   "feature\t\t\t\t2\t1\tTARGETDIR\t0\n"
69                                   "service_feature\t\t\t\t2\t1\tTARGETDIR\t0";
70
71 static const CHAR feature_comp_dat[] = "Feature_\tComponent_\n"
72                                        "s38\ts72\n"
73                                        "FeatureComponents\tFeature_\tComponent_\n"
74                                        "Five\tFive\n"
75                                        "Four\tFour\n"
76                                        "One\tOne\n"
77                                        "Three\tThree\n"
78                                        "Two\tTwo\n"
79                                        "feature\tcomponent\n"
80                                        "service_feature\tservice_comp\n";
81
82 static const CHAR file_dat[] = "File\tComponent_\tFileName\tFileSize\tVersion\tLanguage\tAttributes\tSequence\n"
83                                "s72\ts72\tl255\ti4\tS72\tS20\tI2\ti2\n"
84                                "File\tFile\n"
85                                "five.txt\tFive\tfive.txt\t1000\t\t\t16384\t5\n"
86                                "four.txt\tFour\tfour.txt\t1000\t\t\t16384\t4\n"
87                                "one.txt\tOne\tone.txt\t1000\t\t\t0\t1\n"
88                                "three.txt\tThree\tthree.txt\t1000\t\t\t0\t3\n"
89                                "two.txt\tTwo\ttwo.txt\t1000\t\t\t0\t2\n"
90                                "file\tcomponent\tfilename\t100\t\t\t8192\t1\n"
91                                "service_file\tservice_comp\tservice.exe\t100\t\t\t8192\t1";
92
93 static const CHAR install_exec_seq_dat[] = "Action\tCondition\tSequence\n"
94                                            "s72\tS255\tI2\n"
95                                            "InstallExecuteSequence\tAction\n"
96                                            "AllocateRegistrySpace\tNOT Installed\t1550\n"
97                                            "CostFinalize\t\t1000\n"
98                                            "CostInitialize\t\t800\n"
99                                            "FileCost\t\t900\n"
100                                            "InstallFiles\t\t4000\n"
101                                            "InstallServices\t\t5000\n"
102                                            "InstallFinalize\t\t6600\n"
103                                            "InstallInitialize\t\t1500\n"
104                                            "InstallValidate\t\t1400\n"
105                                            "LaunchConditions\t\t100\n"
106                                            "WriteRegistryValues\tSourceDir And SOURCEDIR\t5000";
107
108 static const CHAR media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
109                                 "i2\ti4\tL64\tS255\tS32\tS72\n"
110                                 "Media\tDiskId\n"
111                                 "1\t3\t\t\tDISK1\t\n"
112                                 "2\t5\t\tmsitest.cab\tDISK2\t\n";
113
114 static const CHAR property_dat[] = "Property\tValue\n"
115                                    "s72\tl0\n"
116                                    "Property\tProperty\n"
117                                    "DefaultUIFont\tDlgFont8\n"
118                                    "HASUIRUN\t0\n"
119                                    "INSTALLLEVEL\t3\n"
120                                    "InstallMode\tTypical\n"
121                                    "Manufacturer\tWine\n"
122                                    "PIDTemplate\t12345<###-%%%%%%%>@@@@@\n"
123                                    "ProductCode\t{F1C3AF50-8B56-4A69-A00C-00773FE42F30}\n"
124                                    "ProductID\tnone\n"
125                                    "ProductLanguage\t1033\n"
126                                    "ProductName\tMSITEST\n"
127                                    "ProductVersion\t1.1.1\n"
128                                    "PROMPTROLLBACKCOST\tP\n"
129                                    "Setup\tSetup\n"
130                                    "UpgradeCode\t{CE067E8D-2E1A-4367-B734-4EB2BDAD6565}";
131
132 static const CHAR registry_dat[] = "Registry\tRoot\tKey\tName\tValue\tComponent_\n"
133                                    "s72\ti2\tl255\tL255\tL0\ts72\n"
134                                    "Registry\tRegistry\n"
135                                    "Apples\t2\tSOFTWARE\\Wine\\msitest\tName\timaname\tOne\n"
136                                    "Oranges\t2\tSOFTWARE\\Wine\\msitest\tnumber\t#314\tTwo\n"
137                                    "regdata\t2\tSOFTWARE\\Wine\\msitest\tblah\tbad\tdangler\n"
138                                    "OrderTest\t2\tSOFTWARE\\Wine\\msitest\tOrderTestName\tOrderTestValue\tcomponent";
139
140 static const CHAR service_install_dat[] = "ServiceInstall\tName\tDisplayName\tServiceType\tStartType\tErrorControl\t"
141                                           "LoadOrderGroup\tDependencies\tStartName\tPassword\tArguments\tComponent_\tDescription\n"
142                                           "s72\ts255\tL255\ti4\ti4\ti4\tS255\tS255\tS255\tS255\tS255\ts72\tL255\n"
143                                           "ServiceInstall\tServiceInstall\n"
144                                           "TestService\tTestService\tTestService\t2\t3\t0\t\t\tTestService\t\t\tservice_comp\t\t";
145
146 static const CHAR service_control_dat[] = "ServiceControl\tName\tEvent\tArguments\tWait\tComponent_\n"
147                                           "s72\tl255\ti2\tL255\tI2\ts72\n"
148                                           "ServiceControl\tServiceControl\n"
149                                           "ServiceControl\tTestService\t8\t\t0\tservice_comp";
150
151 /* tables for test_continuouscabs */
152 static const CHAR cc_component_dat[] = "Component\tComponentId\tDirectory_\tAttributes\tCondition\tKeyPath\n"
153                                        "s72\tS38\ts72\ti2\tS255\tS72\n"
154                                        "Component\tComponent\n"
155                                        "maximus\t\tMSITESTDIR\t0\t1\tmaximus\n"
156                                        "augustus\t\tMSITESTDIR\t0\t1\taugustus\n"
157                                        "caesar\t\tMSITESTDIR\t0\t1\tcaesar\n";
158
159 static const CHAR cc_feature_dat[] = "Feature\tFeature_Parent\tTitle\tDescription\tDisplay\tLevel\tDirectory_\tAttributes\n"
160                                      "s38\tS38\tL64\tL255\tI2\ti2\tS72\ti2\n"
161                                      "Feature\tFeature\n"
162                                      "feature\t\t\t\t2\t1\tTARGETDIR\t0";
163
164 static const CHAR cc_feature_comp_dat[] = "Feature_\tComponent_\n"
165                                           "s38\ts72\n"
166                                           "FeatureComponents\tFeature_\tComponent_\n"
167                                           "feature\tmaximus\n"
168                                           "feature\taugustus\n"
169                                           "feature\tcaesar";
170
171 static const CHAR cc_file_dat[] = "File\tComponent_\tFileName\tFileSize\tVersion\tLanguage\tAttributes\tSequence\n"
172                                   "s72\ts72\tl255\ti4\tS72\tS20\tI2\ti2\n"
173                                   "File\tFile\n"
174                                   "maximus\tmaximus\tmaximus\t500\t\t\t16384\t1\n"
175                                   "augustus\taugustus\taugustus\t50000\t\t\t16384\t2\n"
176                                   "caesar\tcaesar\tcaesar\t500\t\t\t16384\t12";
177
178 static const CHAR cc_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
179                                    "i2\ti4\tL64\tS255\tS32\tS72\n"
180                                    "Media\tDiskId\n"
181                                    "1\t10\t\ttest1.cab\tDISK1\t\n"
182                                    "2\t2\t\ttest2.cab\tDISK2\t\n"
183                                    "3\t12\t\ttest3.cab\tDISK3\t\n";
184
185 static const CHAR co_file_dat[] = "File\tComponent_\tFileName\tFileSize\tVersion\tLanguage\tAttributes\tSequence\n"
186                                   "s72\ts72\tl255\ti4\tS72\tS20\tI2\ti2\n"
187                                   "File\tFile\n"
188                                   "maximus\tmaximus\tmaximus\t500\t\t\t16384\t1\n"
189                                   "augustus\taugustus\taugustus\t50000\t\t\t16384\t2\n"
190                                   "caesar\tcaesar\tcaesar\t500\t\t\t16384\t3";
191
192 static const CHAR co_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
193                                    "i2\ti4\tL64\tS255\tS32\tS72\n"
194                                    "Media\tDiskId\n"
195                                    "1\t10\t\ttest1.cab\tDISK1\t\n"
196                                    "2\t2\t\ttest2.cab\tDISK2\t\n"
197                                    "3\t3\t\ttest3.cab\tDISK3\t\n";
198
199 static const CHAR co2_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
200                                     "i2\ti4\tL64\tS255\tS32\tS72\n"
201                                     "Media\tDiskId\n"
202                                     "1\t10\t\ttest1.cab\tDISK1\t\n"
203                                     "2\t12\t\ttest3.cab\tDISK3\t\n"
204                                     "3\t2\t\ttest2.cab\tDISK2\t\n";
205
206 static const CHAR mm_file_dat[] = "File\tComponent_\tFileName\tFileSize\tVersion\tLanguage\tAttributes\tSequence\n"
207                                   "s72\ts72\tl255\ti4\tS72\tS20\tI2\ti2\n"
208                                   "File\tFile\n"
209                                   "maximus\tmaximus\tmaximus\t500\t\t\t512\t1\n"
210                                   "augustus\taugustus\taugustus\t500\t\t\t512\t2\n"
211                                   "caesar\tcaesar\tcaesar\t500\t\t\t16384\t3";
212
213 static const CHAR mm_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
214                                    "i2\ti4\tL64\tS255\tS32\tS72\n"
215                                    "Media\tDiskId\n"
216                                    "1\t3\t\ttest1.cab\tDISK1\t\n";
217
218 static const CHAR ss_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
219                                    "i2\ti4\tL64\tS255\tS32\tS72\n"
220                                    "Media\tDiskId\n"
221                                    "1\t2\t\ttest1.cab\tDISK1\t\n"
222                                    "2\t2\t\ttest2.cab\tDISK2\t\n"
223                                    "3\t12\t\ttest3.cab\tDISK3\t\n";
224
225 /* tables for test_uiLevelFlags */
226 static const CHAR ui_component_dat[] = "Component\tComponentId\tDirectory_\tAttributes\tCondition\tKeyPath\n"
227                                        "s72\tS38\ts72\ti2\tS255\tS72\n"
228                                        "Component\tComponent\n"
229                                        "maximus\t\tMSITESTDIR\t0\tHASUIRUN=1\tmaximus\n"
230                                        "augustus\t\tMSITESTDIR\t0\t1\taugustus\n"
231                                        "caesar\t\tMSITESTDIR\t0\t1\tcaesar\n";
232
233 static const CHAR ui_install_ui_seq_dat[] = "Action\tCondition\tSequence\n"
234                                            "s72\tS255\tI2\n"
235                                            "InstallUISequence\tAction\n"
236                                            "SetUIProperty\t\t5\n"
237                                            "ExecuteAction\t\t1100\n";
238
239 static const CHAR ui_custom_action_dat[] = "Action\tType\tSource\tTarget\tISComments\n"
240                                            "s72\ti2\tS64\tS0\tS255\n"
241                                            "CustomAction\tAction\n"
242                                            "SetUIProperty\t51\tHASUIRUN\t1\t\n";
243
244 static const CHAR rof_component_dat[] = "Component\tComponentId\tDirectory_\tAttributes\tCondition\tKeyPath\n"
245                                         "s72\tS38\ts72\ti2\tS255\tS72\n"
246                                         "Component\tComponent\n"
247                                         "maximus\t\tMSITESTDIR\t0\t1\tmaximus\n";
248
249 static const CHAR rof_feature_dat[] = "Feature\tFeature_Parent\tTitle\tDescription\tDisplay\tLevel\tDirectory_\tAttributes\n"
250                                       "s38\tS38\tL64\tL255\tI2\ti2\tS72\ti2\n"
251                                       "Feature\tFeature\n"
252                                       "feature\t\t\t\t2\t1\tTARGETDIR\t0";
253
254 static const CHAR rof_feature_comp_dat[] = "Feature_\tComponent_\n"
255                                            "s38\ts72\n"
256                                            "FeatureComponents\tFeature_\tComponent_\n"
257                                            "feature\tmaximus";
258
259 static const CHAR rof_file_dat[] = "File\tComponent_\tFileName\tFileSize\tVersion\tLanguage\tAttributes\tSequence\n"
260                                    "s72\ts72\tl255\ti4\tS72\tS20\tI2\ti2\n"
261                                    "File\tFile\n"
262                                    "maximus\tmaximus\tmaximus\t500\t\t\t8192\t1";
263
264 static const CHAR rof_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
265                                     "i2\ti4\tL64\tS255\tS32\tS72\n"
266                                     "Media\tDiskId\n"
267                                     "1\t1\t\t\tDISK1\t\n";
268
269 typedef struct _msi_table
270 {
271     const CHAR *filename;
272     const CHAR *data;
273     int size;
274 } msi_table;
275
276 #define ADD_TABLE(x) {#x".idt", x##_dat, sizeof(x##_dat)}
277
278 static const msi_table tables[] =
279 {
280     ADD_TABLE(component),
281     ADD_TABLE(directory),
282     ADD_TABLE(feature),
283     ADD_TABLE(feature_comp),
284     ADD_TABLE(file),
285     ADD_TABLE(install_exec_seq),
286     ADD_TABLE(media),
287     ADD_TABLE(property),
288     ADD_TABLE(registry),
289     ADD_TABLE(service_install),
290     ADD_TABLE(service_control)
291 };
292
293 static const msi_table cc_tables[] =
294 {
295     ADD_TABLE(cc_component),
296     ADD_TABLE(directory),
297     ADD_TABLE(cc_feature),
298     ADD_TABLE(cc_feature_comp),
299     ADD_TABLE(cc_file),
300     ADD_TABLE(install_exec_seq),
301     ADD_TABLE(cc_media),
302     ADD_TABLE(property),
303 };
304
305 static const msi_table co_tables[] =
306 {
307     ADD_TABLE(cc_component),
308     ADD_TABLE(directory),
309     ADD_TABLE(cc_feature),
310     ADD_TABLE(cc_feature_comp),
311     ADD_TABLE(co_file),
312     ADD_TABLE(install_exec_seq),
313     ADD_TABLE(co_media),
314     ADD_TABLE(property),
315 };
316
317 static const msi_table co2_tables[] =
318 {
319     ADD_TABLE(cc_component),
320     ADD_TABLE(directory),
321     ADD_TABLE(cc_feature),
322     ADD_TABLE(cc_feature_comp),
323     ADD_TABLE(cc_file),
324     ADD_TABLE(install_exec_seq),
325     ADD_TABLE(co2_media),
326     ADD_TABLE(property),
327 };
328
329 static const msi_table mm_tables[] =
330 {
331     ADD_TABLE(cc_component),
332     ADD_TABLE(directory),
333     ADD_TABLE(cc_feature),
334     ADD_TABLE(cc_feature_comp),
335     ADD_TABLE(mm_file),
336     ADD_TABLE(install_exec_seq),
337     ADD_TABLE(mm_media),
338     ADD_TABLE(property),
339 };
340
341 static const msi_table ss_tables[] =
342 {
343     ADD_TABLE(cc_component),
344     ADD_TABLE(directory),
345     ADD_TABLE(cc_feature),
346     ADD_TABLE(cc_feature_comp),
347     ADD_TABLE(cc_file),
348     ADD_TABLE(install_exec_seq),
349     ADD_TABLE(ss_media),
350     ADD_TABLE(property),
351 };
352
353 static const msi_table ui_tables[] =
354 {
355     ADD_TABLE(ui_component),
356     ADD_TABLE(directory),
357     ADD_TABLE(cc_feature),
358     ADD_TABLE(cc_feature_comp),
359     ADD_TABLE(cc_file),
360     ADD_TABLE(install_exec_seq),
361     ADD_TABLE(ui_install_ui_seq),
362     ADD_TABLE(ui_custom_action),
363     ADD_TABLE(cc_media),
364     ADD_TABLE(property),
365 };
366
367 static const msi_table rof_tables[] =
368 {
369     ADD_TABLE(rof_component),
370     ADD_TABLE(directory),
371     ADD_TABLE(rof_feature),
372     ADD_TABLE(rof_feature_comp),
373     ADD_TABLE(rof_file),
374     ADD_TABLE(install_exec_seq),
375     ADD_TABLE(rof_media),
376     ADD_TABLE(property),
377 };
378
379 /* cabinet definitions */
380
381 /* make the max size large so there is only one cab file */
382 #define MEDIA_SIZE          0x7FFFFFFF
383 #define FOLDER_THRESHOLD    900000
384
385 /* the FCI callbacks */
386
387 static void *mem_alloc(ULONG cb)
388 {
389     return HeapAlloc(GetProcessHeap(), 0, cb);
390 }
391
392 static void mem_free(void *memory)
393 {
394     HeapFree(GetProcessHeap(), 0, memory);
395 }
396
397 static BOOL get_next_cabinet(PCCAB pccab, ULONG  cbPrevCab, void *pv)
398 {
399     sprintf(pccab->szCab, pv, pccab->iCab);
400     return TRUE;
401 }
402
403 static long progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv)
404 {
405     return 0;
406 }
407
408 static int file_placed(PCCAB pccab, char *pszFile, long cbFile,
409                        BOOL fContinuation, void *pv)
410 {
411     return 0;
412 }
413
414 static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv)
415 {
416     HANDLE handle;
417     DWORD dwAccess = 0;
418     DWORD dwShareMode = 0;
419     DWORD dwCreateDisposition = OPEN_EXISTING;
420     
421     dwAccess = GENERIC_READ | GENERIC_WRITE;
422     /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */
423     dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
424
425     if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES)
426         dwCreateDisposition = OPEN_EXISTING;
427     else
428         dwCreateDisposition = CREATE_NEW;
429
430     handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
431                          dwCreateDisposition, 0, NULL);
432
433     ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile);
434
435     return (INT_PTR)handle;
436 }
437
438 static UINT fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
439 {
440     HANDLE handle = (HANDLE)hf;
441     DWORD dwRead;
442     BOOL res;
443     
444     res = ReadFile(handle, memory, cb, &dwRead, NULL);
445     ok(res, "Failed to ReadFile\n");
446
447     return dwRead;
448 }
449
450 static UINT fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
451 {
452     HANDLE handle = (HANDLE)hf;
453     DWORD dwWritten;
454     BOOL res;
455
456     res = WriteFile(handle, memory, cb, &dwWritten, NULL);
457     ok(res, "Failed to WriteFile\n");
458
459     return dwWritten;
460 }
461
462 static int fci_close(INT_PTR hf, int *err, void *pv)
463 {
464     HANDLE handle = (HANDLE)hf;
465     ok(CloseHandle(handle), "Failed to CloseHandle\n");
466
467     return 0;
468 }
469
470 static long fci_seek(INT_PTR hf, long dist, int seektype, int *err, void *pv)
471 {
472     HANDLE handle = (HANDLE)hf;
473     DWORD ret;
474     
475     ret = SetFilePointer(handle, dist, NULL, seektype);
476     ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n");
477
478     return ret;
479 }
480
481 static int fci_delete(char *pszFile, int *err, void *pv)
482 {
483     BOOL ret = DeleteFileA(pszFile);
484     ok(ret, "Failed to DeleteFile %s\n", pszFile);
485
486     return 0;
487 }
488
489 static BOOL check_record(MSIHANDLE rec, UINT field, LPCSTR val)
490 {
491     CHAR buffer[0x20];
492     UINT r;
493     DWORD sz;
494
495     sz = sizeof buffer;
496     r = MsiRecordGetString(rec, field, buffer, &sz);
497     return (r == ERROR_SUCCESS ) && !strcmp(val, buffer);
498 }
499
500 static BOOL get_temp_file(char *pszTempName, int cbTempName, void *pv)
501 {
502     LPSTR tempname;
503
504     tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
505     GetTempFileNameA(".", "xx", 0, tempname);
506
507     if (tempname && (strlen(tempname) < (unsigned)cbTempName))
508     {
509         lstrcpyA(pszTempName, tempname);
510         HeapFree(GetProcessHeap(), 0, tempname);
511         return TRUE;
512     }
513
514     HeapFree(GetProcessHeap(), 0, tempname);
515
516     return FALSE;
517 }
518
519 static INT_PTR get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
520                              USHORT *pattribs, int *err, void *pv)
521 {
522     BY_HANDLE_FILE_INFORMATION finfo;
523     FILETIME filetime;
524     HANDLE handle;
525     DWORD attrs;
526     BOOL res;
527
528     handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
529                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
530
531     ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);
532
533     res = GetFileInformationByHandle(handle, &finfo);
534     ok(res, "Expected GetFileInformationByHandle to succeed\n");
535    
536     FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
537     FileTimeToDosDateTime(&filetime, pdate, ptime);
538
539     attrs = GetFileAttributes(pszName);
540     ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");
541
542     return (INT_PTR)handle;
543 }
544
545 static BOOL add_file(HFCI hfci, const char *file, TCOMP compress)
546 {
547     char path[MAX_PATH];
548     char filename[MAX_PATH];
549
550     lstrcpyA(path, CURR_DIR);
551     lstrcatA(path, "\\");
552     lstrcatA(path, file);
553
554     lstrcpyA(filename, file);
555
556     return FCIAddFile(hfci, path, filename, FALSE, get_next_cabinet,
557                       progress, get_open_info, compress);
558 }
559
560 static void set_cab_parameters(PCCAB pCabParams, const CHAR *name, DWORD max_size)
561 {
562     ZeroMemory(pCabParams, sizeof(CCAB));
563
564     pCabParams->cb = max_size;
565     pCabParams->cbFolderThresh = FOLDER_THRESHOLD;
566     pCabParams->setID = 0xbeef;
567     pCabParams->iCab = 1;
568     lstrcpyA(pCabParams->szCabPath, CURR_DIR);
569     lstrcatA(pCabParams->szCabPath, "\\");
570     lstrcpyA(pCabParams->szCab, name);
571 }
572
573 static void create_cab_file(const CHAR *name, DWORD max_size, const CHAR *files)
574 {
575     CCAB cabParams;
576     LPCSTR ptr;
577     HFCI hfci;
578     ERF erf;
579     BOOL res;
580
581     set_cab_parameters(&cabParams, name, max_size);
582
583     hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
584                       fci_read, fci_write, fci_close, fci_seek, fci_delete,
585                       get_temp_file, &cabParams, NULL);
586
587     ok(hfci != NULL, "Failed to create an FCI context\n");
588
589     ptr = files;
590     while (*ptr)
591     {
592         res = add_file(hfci, ptr, tcompTYPE_MSZIP);
593         ok(res, "Failed to add file: %s\n", ptr);
594         ptr += lstrlen(ptr) + 1;
595     }
596
597     res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
598     ok(res, "Failed to flush the cabinet\n");
599
600     res = FCIDestroy(hfci);
601     ok(res, "Failed to destroy the cabinet\n");
602 }
603
604 static BOOL get_program_files_dir(LPSTR buf)
605 {
606     HKEY hkey;
607     DWORD type = REG_EXPAND_SZ, size;
608
609     if (RegOpenKey(HKEY_LOCAL_MACHINE,
610                    "Software\\Microsoft\\Windows\\CurrentVersion", &hkey))
611         return FALSE;
612
613     size = MAX_PATH;
614     if (RegQueryValueEx(hkey, "ProgramFilesDir", 0, &type, (LPBYTE)buf, &size))
615         return FALSE;
616
617     RegCloseKey(hkey);
618     return TRUE;
619 }
620
621 static void create_file(const CHAR *name, DWORD size)
622 {
623     HANDLE file;
624     DWORD written, left;
625
626     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
627     ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
628     WriteFile(file, name, strlen(name), &written, NULL);
629     WriteFile(file, "\n", strlen("\n"), &written, NULL);
630
631     left = size - lstrlen(name) - 1;
632
633     SetFilePointer(file, left, NULL, FILE_CURRENT);
634     SetEndOfFile(file);
635     
636     CloseHandle(file);
637 }
638
639 static void create_test_files(void)
640 {
641     CreateDirectoryA("msitest", NULL);
642     create_file("msitest\\one.txt", 100);
643     CreateDirectoryA("msitest\\first", NULL);
644     create_file("msitest\\first\\two.txt", 100);
645     CreateDirectoryA("msitest\\second", NULL);
646     create_file("msitest\\second\\three.txt", 100);
647
648     create_file("four.txt", 100);
649     create_file("five.txt", 100);
650     create_cab_file("msitest.cab", MEDIA_SIZE, "four.txt\0five.txt\0");
651
652     create_file("msitest\\filename", 100);
653     create_file("msitest\\service.exe", 100);
654
655     DeleteFileA("four.txt");
656     DeleteFileA("five.txt");
657 }
658
659 static BOOL delete_pf(const CHAR *rel_path, BOOL is_file)
660 {
661     CHAR path[MAX_PATH];
662
663     lstrcpyA(path, PROG_FILES_DIR);
664     lstrcatA(path, "\\");
665     lstrcatA(path, rel_path);
666
667     if (is_file)
668         return DeleteFileA(path);
669     else
670         return RemoveDirectoryA(path);
671 }
672
673 static void delete_test_files(void)
674 {
675     DeleteFileA("msitest.msi");
676     DeleteFileA("msitest.cab");
677     DeleteFileA("msitest\\second\\three.txt");
678     DeleteFileA("msitest\\first\\two.txt");
679     DeleteFileA("msitest\\one.txt");
680     DeleteFileA("msitest\\service.exe");
681     DeleteFileA("msitest\\filename");
682     RemoveDirectoryA("msitest\\second");
683     RemoveDirectoryA("msitest\\first");
684     RemoveDirectoryA("msitest");
685 }
686
687 static void write_file(const CHAR *filename, const char *data, int data_size)
688 {
689     DWORD size;
690
691     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
692                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
693
694     WriteFile(hf, data, data_size, &size, NULL);
695     CloseHandle(hf);
696 }
697
698 static void write_msi_summary_info(MSIHANDLE db)
699 {
700     MSIHANDLE summary;
701     UINT r;
702
703     r = MsiGetSummaryInformationA(db, NULL, 4, &summary);
704     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
705
706     r = MsiSummaryInfoSetPropertyA(summary, PID_TEMPLATE, VT_LPSTR, 0, NULL, ";1033");
707     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
708
709     r = MsiSummaryInfoSetPropertyA(summary, PID_REVNUMBER, VT_LPSTR, 0, NULL,
710                                    "{004757CA-5092-49c2-AD20-28E1CE0DF5F2}");
711     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
712
713     r = MsiSummaryInfoSetPropertyA(summary, PID_PAGECOUNT, VT_I4, 100, NULL, NULL);
714     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
715
716     r = MsiSummaryInfoSetPropertyA(summary, PID_WORDCOUNT, VT_I4, 0, NULL, NULL);
717     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
718
719     /* write the summary changes back to the stream */
720     r = MsiSummaryInfoPersist(summary);
721     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
722
723     MsiCloseHandle(summary);
724 }
725
726 static void create_database(const CHAR *name, const msi_table *tables, int num_tables)
727 {
728     MSIHANDLE db;
729     UINT r;
730     int j;
731
732     r = MsiOpenDatabaseA(name, MSIDBOPEN_CREATE, &db);
733     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
734
735     /* import the tables into the database */
736     for (j = 0; j < num_tables; j++)
737     {
738         const msi_table *table = &tables[j];
739
740         write_file(table->filename, table->data, (table->size - 1) * sizeof(char));
741
742         r = MsiDatabaseImportA(db, CURR_DIR, table->filename);
743         ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
744
745         DeleteFileA(table->filename);
746     }
747
748     write_msi_summary_info(db);
749
750     r = MsiDatabaseCommit(db);
751     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
752
753     MsiCloseHandle(db);
754 }
755
756 static void check_service_is_installed(void)
757 {
758     SC_HANDLE scm, service;
759     BOOL res;
760
761     scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
762     ok(scm != NULL, "Failed to open the SC Manager\n");
763
764     service = OpenService(scm, "TestService", SC_MANAGER_ALL_ACCESS);
765     ok(service != NULL, "Failed to open TestService\n");
766
767     res = DeleteService(service);
768     ok(res, "Failed to delete TestService\n");
769 }
770
771 static void test_MsiInstallProduct(void)
772 {
773     UINT r;
774     CHAR path[MAX_PATH];
775     LONG res;
776     HKEY hkey;
777     DWORD num, size, type;
778
779     create_test_files();
780     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
781
782     r = MsiInstallProductA(msifile, NULL);
783     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
784
785     ok(delete_pf("msitest\\cabout\\new\\five.txt", TRUE), "File not installed\n");
786     ok(delete_pf("msitest\\cabout\\new", FALSE), "File not installed\n");
787     ok(delete_pf("msitest\\cabout\\four.txt", TRUE), "File not installed\n");
788     ok(delete_pf("msitest\\cabout", FALSE), "File not installed\n");
789     ok(delete_pf("msitest\\changed\\three.txt", TRUE), "File not installed\n");
790     ok(delete_pf("msitest\\changed", FALSE), "File not installed\n");
791     ok(delete_pf("msitest\\first\\two.txt", TRUE), "File not installed\n");
792     ok(delete_pf("msitest\\first", FALSE), "File not installed\n");
793     ok(delete_pf("msitest\\one.txt", TRUE), "File not installed\n");
794     ok(delete_pf("msitest\\filename", TRUE), "File not installed\n");
795     ok(delete_pf("msitest\\service.exe", TRUE), "File not installed\n");
796     ok(delete_pf("msitest", FALSE), "File not installed\n");
797
798     res = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wine\\msitest", &hkey);
799     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
800
801     size = MAX_PATH;
802     type = REG_SZ;
803     res = RegQueryValueExA(hkey, "Name", NULL, &type, (LPBYTE)path, &size);
804     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
805     ok(!lstrcmpA(path, "imaname"), "Expected imaname, got %s\n", path);
806
807     size = MAX_PATH;
808     type = REG_SZ;
809     res = RegQueryValueExA(hkey, "blah", NULL, &type, (LPBYTE)path, &size);
810     ok(res == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", res);
811
812     size = sizeof(num);
813     type = REG_DWORD;
814     res = RegQueryValueExA(hkey, "number", NULL, &type, (LPBYTE)&num, &size);
815     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
816     ok(num == 314, "Expected 314, got %d\n", num);
817
818     size = MAX_PATH;
819     type = REG_SZ;
820     res = RegQueryValueExA(hkey, "OrderTestName", NULL, &type, (LPBYTE)path, &size);
821     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
822     ok(!lstrcmpA(path, "OrderTestValue"), "Expected imaname, got %s\n", path);
823
824     check_service_is_installed();
825
826     RegDeleteKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wine\\msitest");
827
828     delete_test_files();
829 }
830
831 static void test_MsiSetComponentState(void)
832 {
833     INSTALLSTATE installed, action;
834     MSIHANDLE package;
835     char path[MAX_PATH];
836     UINT r;
837
838     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
839
840     CoInitialize(NULL);
841
842     lstrcpy(path, CURR_DIR);
843     lstrcat(path, "\\");
844     lstrcat(path, msifile);
845
846     r = MsiOpenPackage(path, &package);
847     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
848
849     r = MsiDoAction(package, "CostInitialize");
850     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
851
852     r = MsiDoAction(package, "FileCost");
853     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
854
855     r = MsiDoAction(package, "CostFinalize");
856     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
857
858     r = MsiGetComponentState(package, "dangler", &installed, &action);
859     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
860     ok(installed == INSTALLSTATE_ABSENT, "Expected INSTALLSTATE_ABSENT, got %d\n", installed);
861     ok(action == INSTALLSTATE_UNKNOWN, "Expected INSTALLSTATE_UNKNOWN, got %d\n", action);
862
863     r = MsiSetComponentState(package, "dangler", INSTALLSTATE_SOURCE);
864     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
865
866     MsiCloseHandle(package);
867     CoUninitialize();
868
869     DeleteFileA(msifile);
870 }
871
872 static void test_packagecoltypes(void)
873 {
874     MSIHANDLE hdb, view, rec;
875     char path[MAX_PATH];
876     LPCSTR query;
877     UINT r, count;
878
879     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
880
881     CoInitialize(NULL);
882
883     lstrcpy(path, CURR_DIR);
884     lstrcat(path, "\\");
885     lstrcat(path, msifile);
886
887     r = MsiOpenDatabase(path, MSIDBOPEN_READONLY, &hdb);
888     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
889
890     query = "SELECT * FROM `Media`";
891     r = MsiDatabaseOpenView( hdb, query, &view );
892     ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
893
894     r = MsiViewGetColumnInfo( view, MSICOLINFO_NAMES, &rec );
895     count = MsiRecordGetFieldCount( rec );
896     ok(r == ERROR_SUCCESS, "MsiViewGetColumnInfo failed\n");
897     ok(count == 6, "Expected 6, got %d\n", count);
898     ok(check_record(rec, 1, "DiskId"), "wrong column label\n");
899     ok(check_record(rec, 2, "LastSequence"), "wrong column label\n");
900     ok(check_record(rec, 3, "DiskPrompt"), "wrong column label\n");
901     ok(check_record(rec, 4, "Cabinet"), "wrong column label\n");
902     ok(check_record(rec, 5, "VolumeLabel"), "wrong column label\n");
903     ok(check_record(rec, 6, "Source"), "wrong column label\n");
904     MsiCloseHandle(rec);
905
906     r = MsiViewGetColumnInfo( view, MSICOLINFO_TYPES, &rec );
907     count = MsiRecordGetFieldCount( rec );
908     ok(r == ERROR_SUCCESS, "MsiViewGetColumnInfo failed\n");
909     ok(count == 6, "Expected 6, got %d\n", count);
910     ok(check_record(rec, 1, "i2"), "wrong column label\n");
911     ok(check_record(rec, 2, "i4"), "wrong column label\n");
912     ok(check_record(rec, 3, "L64"), "wrong column label\n");
913     ok(check_record(rec, 4, "S255"), "wrong column label\n");
914     ok(check_record(rec, 5, "S32"), "wrong column label\n");
915     ok(check_record(rec, 6, "S72"), "wrong column label\n");
916
917     MsiCloseHandle(rec);
918     MsiCloseHandle(view);
919     MsiCloseHandle(hdb);
920     DeleteFile(msifile);
921 }
922
923 static void create_cc_test_files(void)
924 {
925     CCAB cabParams;
926     HFCI hfci;
927     ERF erf;
928     static CHAR cab_context[] = "test%d.cab";
929     BOOL res;
930
931     create_file("maximus", 500);
932     create_file("augustus", 50000);
933     create_file("caesar", 500);
934
935     set_cab_parameters(&cabParams, "test1.cab", 200);
936
937     hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
938                       fci_read, fci_write, fci_close, fci_seek, fci_delete,
939                       get_temp_file, &cabParams, cab_context);
940     ok(hfci != NULL, "Failed to create an FCI context\n");
941
942     res = add_file(hfci, "maximus", tcompTYPE_MSZIP);
943     ok(res, "Failed to add file maximus\n");
944
945     res = add_file(hfci, "augustus", tcompTYPE_MSZIP);
946     ok(res, "Failed to add file augustus\n");
947
948     res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
949     ok(res, "Failed to flush the cabinet\n");
950
951     res = FCIDestroy(hfci);
952     ok(res, "Failed to destroy the cabinet\n");
953
954     create_cab_file("test3.cab", MEDIA_SIZE, "caesar\0");
955
956     DeleteFile("maximus");
957     DeleteFile("augustus");
958     DeleteFile("caesar");
959 }
960
961 static void delete_cab_files(void)
962 {
963     SHFILEOPSTRUCT shfl;
964     CHAR path[MAX_PATH];
965
966     lstrcpyA(path, CURR_DIR);
967     lstrcatA(path, "\\*.cab\0");
968
969     shfl.hwnd = NULL;
970     shfl.wFunc = FO_DELETE;
971     shfl.pFrom = (LPCSTR)path;
972     shfl.pTo = NULL;
973     shfl.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_NORECURSION | FOF_SILENT;
974
975     SHFileOperation(&shfl);
976 }
977
978 static void test_continuouscabs(void)
979 {
980     UINT r;
981
982     create_cc_test_files();
983     create_database(msifile, cc_tables, sizeof(cc_tables) / sizeof(msi_table));
984
985     MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL);
986
987     r = MsiInstallProductA(msifile, NULL);
988     ok(delete_pf("msitest\\maximus", TRUE), "File not installed\n");
989     todo_wine
990     {
991         ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
992         ok(delete_pf("msitest\\augustus", TRUE), "File not installed\n");
993         ok(delete_pf("msitest\\caesar", TRUE), "File not installed\n");
994     }
995     ok(delete_pf("msitest", FALSE), "File not installed\n");
996
997     delete_cab_files();
998     DeleteFile(msifile);
999 }
1000
1001 static void test_caborder(void)
1002 {
1003     UINT r;
1004
1005     create_file("imperator", 100);
1006     create_file("maximus", 500);
1007     create_file("augustus", 50000);
1008     create_file("caesar", 500);
1009
1010     create_database(msifile, cc_tables, sizeof(cc_tables) / sizeof(msi_table));
1011
1012     MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL);
1013
1014     create_cab_file("test1.cab", MEDIA_SIZE, "maximus\0");
1015     create_cab_file("test2.cab", MEDIA_SIZE, "augustus\0");
1016     create_cab_file("test3.cab", MEDIA_SIZE, "caesar\0");
1017
1018     r = MsiInstallProductA(msifile, NULL);
1019     ok(r == ERROR_INSTALL_FAILURE, "Expected ERROR_INSTALL_FAILURE, got %u\n", r);
1020     ok(!delete_pf("msitest\\augustus", TRUE), "File is installed\n");
1021     ok(!delete_pf("msitest\\caesar", TRUE), "File is installed\n");
1022     todo_wine
1023     {
1024         ok(!delete_pf("msitest\\maximus", TRUE), "File is installed\n");
1025         ok(!delete_pf("msitest", FALSE), "File is installed\n");
1026     }
1027
1028     delete_cab_files();
1029
1030     create_cab_file("test1.cab", MEDIA_SIZE, "imperator\0");
1031     create_cab_file("test2.cab", MEDIA_SIZE, "maximus\0augustus\0");
1032     create_cab_file("test3.cab", MEDIA_SIZE, "caesar\0");
1033
1034     r = MsiInstallProductA(msifile, NULL);
1035     ok(r == ERROR_INSTALL_FAILURE, "Expected ERROR_INSTALL_FAILURE, got %u\n", r);
1036     ok(!delete_pf("msitest\\maximus", TRUE), "File is installed\n");
1037     ok(!delete_pf("msitest\\augustus", TRUE), "File is installed\n");
1038     ok(!delete_pf("msitest\\caesar", TRUE), "File is installed\n");
1039     todo_wine
1040     {
1041         ok(!delete_pf("msitest", FALSE), "File is installed\n");
1042     }
1043
1044     delete_cab_files();
1045     DeleteFile(msifile);
1046
1047     create_cc_test_files();
1048     create_database(msifile, co_tables, sizeof(co_tables) / sizeof(msi_table));
1049
1050     r = MsiInstallProductA(msifile, NULL);
1051     ok(!delete_pf("msitest\\augustus", TRUE), "File is installed\n");
1052     ok(!delete_pf("msitest\\caesar", TRUE), "File is installed\n");
1053     ok(!delete_pf("msitest", FALSE), "File is installed\n");
1054     todo_wine
1055     {
1056         ok(!delete_pf("msitest\\maximus", TRUE), "File is installed\n");
1057         ok(r == ERROR_INSTALL_FAILURE, "Expected ERROR_INSTALL_FAILURE, got %u\n", r);
1058     }
1059
1060     delete_cab_files();
1061     DeleteFile(msifile);
1062
1063     create_cc_test_files();
1064     create_database(msifile, co2_tables, sizeof(co2_tables) / sizeof(msi_table));
1065
1066     r = MsiInstallProductA(msifile, NULL);
1067     ok(!delete_pf("msitest\\augustus", TRUE), "File is installed\n");
1068     ok(!delete_pf("msitest\\caesar", TRUE), "File is installed\n");
1069     todo_wine
1070     {
1071         ok(r == ERROR_INSTALL_FAILURE, "Expected ERROR_INSTALL_FAILURE, got %u\n", r);
1072         ok(!delete_pf("msitest\\maximus", TRUE), "File is installed\n");
1073         ok(!delete_pf("msitest", FALSE), "File is installed\n");
1074     }
1075
1076     delete_cab_files();
1077     DeleteFile("imperator");
1078     DeleteFile("maximus");
1079     DeleteFile("augustus");
1080     DeleteFile("caesar");
1081     DeleteFile(msifile);
1082 }
1083
1084 static void test_mixedmedia(void)
1085 {
1086     UINT r;
1087
1088     CreateDirectoryA("msitest", NULL);
1089     create_file("msitest\\maximus", 500);
1090     create_file("msitest\\augustus", 500);
1091     create_file("caesar", 500);
1092
1093     create_database(msifile, mm_tables, sizeof(mm_tables) / sizeof(msi_table));
1094
1095     MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL);
1096
1097     create_cab_file("test1.cab", MEDIA_SIZE, "caesar\0");
1098
1099     r = MsiInstallProductA(msifile, NULL);
1100     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
1101     ok(delete_pf("msitest\\augustus", TRUE), "File not installed\n");
1102     ok(delete_pf("msitest\\caesar", TRUE), "File not installed\n");
1103     ok(delete_pf("msitest\\maximus", TRUE), "File not installed\n");
1104     ok(delete_pf("msitest", FALSE), "File not installed\n");
1105
1106     DeleteFile("maximus");
1107     DeleteFile("augustus");
1108     DeleteFile("caesar");
1109     RemoveDirectory("msitest");
1110     DeleteFile("test1.cab");
1111     DeleteFile(msifile);
1112 }
1113
1114 static void test_samesequence(void)
1115 {
1116     UINT r;
1117
1118     create_cc_test_files();
1119     create_database(msifile, ss_tables, sizeof(ss_tables) / sizeof(msi_table));
1120
1121     MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL);
1122
1123     r = MsiInstallProductA(msifile, NULL);
1124     todo_wine
1125     {
1126         ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
1127         ok(delete_pf("msitest\\augustus", TRUE), "File not installed\n");
1128         ok(delete_pf("msitest\\caesar", TRUE), "File not installed\n");
1129     }
1130     ok(delete_pf("msitest\\maximus", TRUE), "File not installed\n");
1131     ok(delete_pf("msitest", FALSE), "File not installed\n");
1132
1133     delete_cab_files();
1134     DeleteFile(msifile);
1135 }
1136
1137 static void test_uiLevelFlags(void)
1138 {
1139     UINT r;
1140
1141     create_cc_test_files();
1142     create_database(msifile, ui_tables, sizeof(ui_tables) / sizeof(msi_table));
1143
1144     MsiSetInternalUI(INSTALLUILEVEL_NONE | INSTALLUILEVEL_SOURCERESONLY, NULL);
1145
1146     r = MsiInstallProductA(msifile, NULL);
1147     ok(!delete_pf("msitest\\maximus", TRUE), "UI install occurred, but execute-only was requested.\n");
1148     todo_wine
1149     {
1150         ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
1151         ok(delete_pf("msitest\\caesar", TRUE), "File not installed\n");
1152     }
1153     ok(delete_pf("msitest\\augustus", TRUE), "File not installed\n");
1154     ok(delete_pf("msitest", FALSE), "File not installed\n");
1155
1156     delete_cab_files();
1157     DeleteFile(msifile);
1158 }
1159
1160 static BOOL file_matches(LPSTR path)
1161 {
1162     CHAR buf[MAX_PATH];
1163     HANDLE file;
1164     DWORD size;
1165
1166     file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
1167                       NULL, OPEN_EXISTING, 0, NULL);
1168
1169     ZeroMemory(buf, MAX_PATH);
1170     ReadFile(file, buf, 15, &size, NULL);
1171     CloseHandle(file);
1172
1173     return !lstrcmp(buf, "msitest\\maximus");
1174 }
1175
1176 static void test_readonlyfile(void)
1177 {
1178     UINT r;
1179     DWORD size;
1180     HANDLE file;
1181     CHAR path[MAX_PATH];
1182
1183     CreateDirectoryA("msitest", NULL);
1184     create_file("msitest\\maximus", 500);
1185     create_database(msifile, rof_tables, sizeof(rof_tables) / sizeof(msi_table));
1186
1187     MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL);
1188
1189     lstrcpy(path, PROG_FILES_DIR);
1190     lstrcat(path, "\\msitest");
1191     CreateDirectory(path, NULL);
1192
1193     lstrcat(path, "\\maximus");
1194     file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
1195                       NULL, CREATE_NEW, FILE_ATTRIBUTE_READONLY, NULL);
1196     if (file == INVALID_HANDLE_VALUE) printf("didnt work here: %d\n", GetLastError());
1197
1198     WriteFile(file, "readonlyfile", 20, &size, NULL);
1199     CloseHandle(file);
1200
1201     r = MsiInstallProductA(msifile, NULL);
1202     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
1203     ok(file_matches(path), "Expected file to be overwritten\n");
1204     ok(delete_pf("msitest\\maximus", TRUE), "File not installed\n");
1205     ok(delete_pf("msitest", FALSE), "File not installed\n");
1206
1207     DeleteFile(msifile);
1208 }
1209
1210 START_TEST(install)
1211 {
1212     DWORD len;
1213     char temp_path[MAX_PATH], prev_path[MAX_PATH];
1214
1215     GetCurrentDirectoryA(MAX_PATH, prev_path);
1216     GetTempPath(MAX_PATH, temp_path);
1217     SetCurrentDirectoryA(temp_path);
1218
1219     lstrcpyA(CURR_DIR, temp_path);
1220     len = lstrlenA(CURR_DIR);
1221
1222     if(len && (CURR_DIR[len - 1] == '\\'))
1223         CURR_DIR[len - 1] = 0;
1224
1225     get_program_files_dir(PROG_FILES_DIR);
1226
1227     test_MsiInstallProduct();
1228     test_MsiSetComponentState();
1229     test_packagecoltypes();
1230     test_continuouscabs();
1231     test_caborder();
1232     test_mixedmedia();
1233     test_samesequence();
1234     test_uiLevelFlags();
1235     test_readonlyfile();
1236
1237     SetCurrentDirectoryA(prev_path);
1238 }