msi: Run the install tests from a temporary directory.
[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                                    "INSTALLLEVEL\t3\n"
119                                    "InstallMode\tTypical\n"
120                                    "Manufacturer\tWine\n"
121                                    "PIDTemplate\t12345<###-%%%%%%%>@@@@@\n"
122                                    "ProductCode\t{F1C3AF50-8B56-4A69-A00C-00773FE42F30}\n"
123                                    "ProductID\tnone\n"
124                                    "ProductLanguage\t1033\n"
125                                    "ProductName\tMSITEST\n"
126                                    "ProductVersion\t1.1.1\n"
127                                    "PROMPTROLLBACKCOST\tP\n"
128                                    "Setup\tSetup\n"
129                                    "UpgradeCode\t{CE067E8D-2E1A-4367-B734-4EB2BDAD6565}";
130
131 static const CHAR registry_dat[] = "Registry\tRoot\tKey\tName\tValue\tComponent_\n"
132                                    "s72\ti2\tl255\tL255\tL0\ts72\n"
133                                    "Registry\tRegistry\n"
134                                    "Apples\t2\tSOFTWARE\\Wine\\msitest\tName\timaname\tOne\n"
135                                    "Oranges\t2\tSOFTWARE\\Wine\\msitest\tnumber\t#314\tTwo\n"
136                                    "regdata\t2\tSOFTWARE\\Wine\\msitest\tblah\tbad\tdangler\n"
137                                    "OrderTest\t2\tSOFTWARE\\Wine\\msitest\tOrderTestName\tOrderTestValue\tcomponent";
138
139 static const CHAR service_install_dat[] = "ServiceInstall\tName\tDisplayName\tServiceType\tStartType\tErrorControl\t"
140                                           "LoadOrderGroup\tDependencies\tStartName\tPassword\tArguments\tComponent_\tDescription\n"
141                                           "s72\ts255\tL255\ti4\ti4\ti4\tS255\tS255\tS255\tS255\tS255\ts72\tL255\n"
142                                           "ServiceInstall\tServiceInstall\n"
143                                           "TestService\tTestService\tTestService\t2\t3\t0\t\t\tTestService\t\t\tservice_comp\t\t";
144
145 static const CHAR service_control_dat[] = "ServiceControl\tName\tEvent\tArguments\tWait\tComponent_\n"
146                                           "s72\tl255\ti2\tL255\tI2\ts72\n"
147                                           "ServiceControl\tServiceControl\n"
148                                           "ServiceControl\tTestService\t8\t\t0\tservice_comp";
149
150 /* tables for test_continuouscabs */
151 static const CHAR cc_component_dat[] = "Component\tComponentId\tDirectory_\tAttributes\tCondition\tKeyPath\n"
152                                        "s72\tS38\ts72\ti2\tS255\tS72\n"
153                                        "Component\tComponent\n"
154                                        "maximus\t\tMSITESTDIR\t0\t1\tmaximus\n"
155                                        "augustus\t\tMSITESTDIR\t0\t1\taugustus\n"
156                                        "caesar\t\tMSITESTDIR\t0\t1\tcaesar\n";
157
158 static const CHAR cc_feature_dat[] = "Feature\tFeature_Parent\tTitle\tDescription\tDisplay\tLevel\tDirectory_\tAttributes\n"
159                                      "s38\tS38\tL64\tL255\tI2\ti2\tS72\ti2\n"
160                                      "Feature\tFeature\n"
161                                      "feature\t\t\t\t2\t1\tTARGETDIR\t0";
162
163 static const CHAR cc_feature_comp_dat[] = "Feature_\tComponent_\n"
164                                           "s38\ts72\n"
165                                           "FeatureComponents\tFeature_\tComponent_\n"
166                                           "feature\tmaximus\n"
167                                           "feature\taugustus\n"
168                                           "feature\tcaesar";
169
170 static const CHAR cc_file_dat[] = "File\tComponent_\tFileName\tFileSize\tVersion\tLanguage\tAttributes\tSequence\n"
171                                   "s72\ts72\tl255\ti4\tS72\tS20\tI2\ti2\n"
172                                   "File\tFile\n"
173                                   "maximus\tmaximus\tmaximus\t500\t\t\t16384\t1\n"
174                                   "augustus\taugustus\taugustus\t50000\t\t\t16384\t2\n"
175                                   "caesar\tcaesar\tcaesar\t500\t\t\t16384\t12";
176
177 static const CHAR cc_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
178                                    "i2\ti4\tL64\tS255\tS32\tS72\n"
179                                    "Media\tDiskId\n"
180                                    "1\t10\t\ttest1.cab\tDISK1\t\n"
181                                    "2\t2\t\ttest2.cab\tDISK2\t\n"
182                                    "3\t12\t\ttest3.cab\tDISK3\t\n";
183
184 static const CHAR co_file_dat[] = "File\tComponent_\tFileName\tFileSize\tVersion\tLanguage\tAttributes\tSequence\n"
185                                   "s72\ts72\tl255\ti4\tS72\tS20\tI2\ti2\n"
186                                   "File\tFile\n"
187                                   "maximus\tmaximus\tmaximus\t500\t\t\t16384\t1\n"
188                                   "augustus\taugustus\taugustus\t50000\t\t\t16384\t2\n"
189                                   "caesar\tcaesar\tcaesar\t500\t\t\t16384\t3";
190
191 static const CHAR co_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
192                                    "i2\ti4\tL64\tS255\tS32\tS72\n"
193                                    "Media\tDiskId\n"
194                                    "1\t10\t\ttest1.cab\tDISK1\t\n"
195                                    "2\t2\t\ttest2.cab\tDISK2\t\n"
196                                    "3\t3\t\ttest3.cab\tDISK3\t\n";
197
198 static const CHAR co2_media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
199                                     "i2\ti4\tL64\tS255\tS32\tS72\n"
200                                     "Media\tDiskId\n"
201                                     "1\t10\t\ttest1.cab\tDISK1\t\n"
202                                     "2\t12\t\ttest3.cab\tDISK3\t\n"
203                                     "3\t2\t\ttest2.cab\tDISK2\t\n";
204
205 typedef struct _msi_table
206 {
207     const CHAR *filename;
208     const CHAR *data;
209     int size;
210 } msi_table;
211
212 #define ADD_TABLE(x) {#x".idt", x##_dat, sizeof(x##_dat)}
213
214 static const msi_table tables[] =
215 {
216     ADD_TABLE(component),
217     ADD_TABLE(directory),
218     ADD_TABLE(feature),
219     ADD_TABLE(feature_comp),
220     ADD_TABLE(file),
221     ADD_TABLE(install_exec_seq),
222     ADD_TABLE(media),
223     ADD_TABLE(property),
224     ADD_TABLE(registry),
225     ADD_TABLE(service_install),
226     ADD_TABLE(service_control)
227 };
228
229 static const msi_table cc_tables[] =
230 {
231     ADD_TABLE(cc_component),
232     ADD_TABLE(directory),
233     ADD_TABLE(cc_feature),
234     ADD_TABLE(cc_feature_comp),
235     ADD_TABLE(cc_file),
236     ADD_TABLE(install_exec_seq),
237     ADD_TABLE(cc_media),
238     ADD_TABLE(property),
239 };
240
241 static const msi_table co_tables[] =
242 {
243     ADD_TABLE(cc_component),
244     ADD_TABLE(directory),
245     ADD_TABLE(cc_feature),
246     ADD_TABLE(cc_feature_comp),
247     ADD_TABLE(co_file),
248     ADD_TABLE(install_exec_seq),
249     ADD_TABLE(co_media),
250     ADD_TABLE(property),
251 };
252
253 static const msi_table co2_tables[] =
254 {
255     ADD_TABLE(cc_component),
256     ADD_TABLE(directory),
257     ADD_TABLE(cc_feature),
258     ADD_TABLE(cc_feature_comp),
259     ADD_TABLE(cc_file),
260     ADD_TABLE(install_exec_seq),
261     ADD_TABLE(co2_media),
262     ADD_TABLE(property),
263 };
264
265 /* cabinet definitions */
266
267 /* make the max size large so there is only one cab file */
268 #define MEDIA_SIZE          0x7FFFFFFF
269 #define FOLDER_THRESHOLD    900000
270
271 /* the FCI callbacks */
272
273 static void *mem_alloc(ULONG cb)
274 {
275     return HeapAlloc(GetProcessHeap(), 0, cb);
276 }
277
278 static void mem_free(void *memory)
279 {
280     HeapFree(GetProcessHeap(), 0, memory);
281 }
282
283 static BOOL get_next_cabinet(PCCAB pccab, ULONG  cbPrevCab, void *pv)
284 {
285     sprintf(pccab->szCab, pv, pccab->iCab);
286     return TRUE;
287 }
288
289 static long progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv)
290 {
291     return 0;
292 }
293
294 static int file_placed(PCCAB pccab, char *pszFile, long cbFile,
295                        BOOL fContinuation, void *pv)
296 {
297     return 0;
298 }
299
300 static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv)
301 {
302     HANDLE handle;
303     DWORD dwAccess = 0;
304     DWORD dwShareMode = 0;
305     DWORD dwCreateDisposition = OPEN_EXISTING;
306     
307     dwAccess = GENERIC_READ | GENERIC_WRITE;
308     /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */
309     dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
310
311     if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES)
312         dwCreateDisposition = OPEN_EXISTING;
313     else
314         dwCreateDisposition = CREATE_NEW;
315
316     handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
317                          dwCreateDisposition, 0, NULL);
318
319     ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile);
320
321     return (INT_PTR)handle;
322 }
323
324 static UINT fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
325 {
326     HANDLE handle = (HANDLE)hf;
327     DWORD dwRead;
328     BOOL res;
329     
330     res = ReadFile(handle, memory, cb, &dwRead, NULL);
331     ok(res, "Failed to ReadFile\n");
332
333     return dwRead;
334 }
335
336 static UINT fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
337 {
338     HANDLE handle = (HANDLE)hf;
339     DWORD dwWritten;
340     BOOL res;
341
342     res = WriteFile(handle, memory, cb, &dwWritten, NULL);
343     ok(res, "Failed to WriteFile\n");
344
345     return dwWritten;
346 }
347
348 static int fci_close(INT_PTR hf, int *err, void *pv)
349 {
350     HANDLE handle = (HANDLE)hf;
351     ok(CloseHandle(handle), "Failed to CloseHandle\n");
352
353     return 0;
354 }
355
356 static long fci_seek(INT_PTR hf, long dist, int seektype, int *err, void *pv)
357 {
358     HANDLE handle = (HANDLE)hf;
359     DWORD ret;
360     
361     ret = SetFilePointer(handle, dist, NULL, seektype);
362     ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n");
363
364     return ret;
365 }
366
367 static int fci_delete(char *pszFile, int *err, void *pv)
368 {
369     BOOL ret = DeleteFileA(pszFile);
370     ok(ret, "Failed to DeleteFile %s\n", pszFile);
371
372     return 0;
373 }
374
375 static BOOL check_record(MSIHANDLE rec, UINT field, LPCSTR val)
376 {
377     CHAR buffer[0x20];
378     UINT r;
379     DWORD sz;
380
381     sz = sizeof buffer;
382     r = MsiRecordGetString(rec, field, buffer, &sz);
383     return (r == ERROR_SUCCESS ) && !strcmp(val, buffer);
384 }
385
386 static BOOL get_temp_file(char *pszTempName, int cbTempName, void *pv)
387 {
388     LPSTR tempname;
389
390     tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
391     GetTempFileNameA(".", "xx", 0, tempname);
392
393     if (tempname && (strlen(tempname) < (unsigned)cbTempName))
394     {
395         lstrcpyA(pszTempName, tempname);
396         HeapFree(GetProcessHeap(), 0, tempname);
397         return TRUE;
398     }
399
400     HeapFree(GetProcessHeap(), 0, tempname);
401
402     return FALSE;
403 }
404
405 static INT_PTR get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
406                              USHORT *pattribs, int *err, void *pv)
407 {
408     BY_HANDLE_FILE_INFORMATION finfo;
409     FILETIME filetime;
410     HANDLE handle;
411     DWORD attrs;
412     BOOL res;
413
414     handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
415                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
416
417     ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);
418
419     res = GetFileInformationByHandle(handle, &finfo);
420     ok(res, "Expected GetFileInformationByHandle to succeed\n");
421    
422     FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
423     FileTimeToDosDateTime(&filetime, pdate, ptime);
424
425     attrs = GetFileAttributes(pszName);
426     ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");
427
428     return (INT_PTR)handle;
429 }
430
431 static BOOL add_file(HFCI hfci, const char *file, TCOMP compress)
432 {
433     char path[MAX_PATH];
434     char filename[MAX_PATH];
435
436     lstrcpyA(path, CURR_DIR);
437     lstrcatA(path, "\\");
438     lstrcatA(path, file);
439
440     lstrcpyA(filename, file);
441
442     return FCIAddFile(hfci, path, filename, FALSE, get_next_cabinet,
443                       progress, get_open_info, compress);
444 }
445
446 static void set_cab_parameters(PCCAB pCabParams, const CHAR *name, DWORD max_size)
447 {
448     ZeroMemory(pCabParams, sizeof(CCAB));
449
450     pCabParams->cb = max_size;
451     pCabParams->cbFolderThresh = FOLDER_THRESHOLD;
452     pCabParams->setID = 0xbeef;
453     pCabParams->iCab = 1;
454     lstrcpyA(pCabParams->szCabPath, CURR_DIR);
455     lstrcatA(pCabParams->szCabPath, "\\");
456     lstrcpyA(pCabParams->szCab, name);
457 }
458
459 static void create_cab_file(const CHAR *name, DWORD max_size, const CHAR *files)
460 {
461     CCAB cabParams;
462     LPCSTR ptr;
463     HFCI hfci;
464     ERF erf;
465     BOOL res;
466
467     set_cab_parameters(&cabParams, name, max_size);
468
469     hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
470                       fci_read, fci_write, fci_close, fci_seek, fci_delete,
471                       get_temp_file, &cabParams, NULL);
472
473     ok(hfci != NULL, "Failed to create an FCI context\n");
474
475     ptr = files;
476     while (*ptr)
477     {
478         res = add_file(hfci, ptr, tcompTYPE_MSZIP);
479         ok(res, "Failed to add file: %s\n", ptr);
480         ptr += lstrlen(ptr) + 1;
481     }
482
483     res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
484     ok(res, "Failed to flush the cabinet\n");
485
486     res = FCIDestroy(hfci);
487     ok(res, "Failed to destroy the cabinet\n");
488 }
489
490 static BOOL get_program_files_dir(LPSTR buf)
491 {
492     HKEY hkey;
493     DWORD type = REG_EXPAND_SZ, size;
494
495     if (RegOpenKey(HKEY_LOCAL_MACHINE,
496                    "Software\\Microsoft\\Windows\\CurrentVersion", &hkey))
497         return FALSE;
498
499     size = MAX_PATH;
500     if (RegQueryValueEx(hkey, "ProgramFilesDir", 0, &type, (LPBYTE)buf, &size))
501         return FALSE;
502
503     RegCloseKey(hkey);
504     return TRUE;
505 }
506
507 static void create_file(const CHAR *name, DWORD size)
508 {
509     HANDLE file;
510     DWORD written, left;
511
512     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
513     ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
514     WriteFile(file, name, strlen(name), &written, NULL);
515     WriteFile(file, "\n", strlen("\n"), &written, NULL);
516
517     left = size - lstrlen(name) - 1;
518
519     SetFilePointer(file, left, NULL, FILE_CURRENT);
520     SetEndOfFile(file);
521     
522     CloseHandle(file);
523 }
524
525 static void create_test_files(void)
526 {
527     CreateDirectoryA("msitest", NULL);
528     create_file("msitest\\one.txt", 100);
529     CreateDirectoryA("msitest\\first", NULL);
530     create_file("msitest\\first\\two.txt", 100);
531     CreateDirectoryA("msitest\\second", NULL);
532     create_file("msitest\\second\\three.txt", 100);
533
534     create_file("four.txt", 100);
535     create_file("five.txt", 100);
536     create_cab_file("msitest.cab", MEDIA_SIZE, "four.txt\0five.txt\0");
537
538     create_file("msitest\\filename", 100);
539     create_file("msitest\\service.exe", 100);
540
541     DeleteFileA("four.txt");
542     DeleteFileA("five.txt");
543 }
544
545 static BOOL delete_pf(const CHAR *rel_path, BOOL is_file)
546 {
547     CHAR path[MAX_PATH];
548
549     lstrcpyA(path, PROG_FILES_DIR);
550     lstrcatA(path, "\\");
551     lstrcatA(path, rel_path);
552
553     if (is_file)
554         return DeleteFileA(path);
555     else
556         return RemoveDirectoryA(path);
557 }
558
559 static void delete_test_files(void)
560 {
561     DeleteFileA("msitest.msi");
562     DeleteFileA("msitest.cab");
563     DeleteFileA("msitest\\second\\three.txt");
564     DeleteFileA("msitest\\first\\two.txt");
565     DeleteFileA("msitest\\one.txt");
566     DeleteFileA("msitest\\service.exe");
567     DeleteFileA("msitest\\filename");
568     RemoveDirectoryA("msitest\\second");
569     RemoveDirectoryA("msitest\\first");
570     RemoveDirectoryA("msitest");
571 }
572
573 static void write_file(const CHAR *filename, const char *data, int data_size)
574 {
575     DWORD size;
576
577     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
578                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
579
580     WriteFile(hf, data, data_size, &size, NULL);
581     CloseHandle(hf);
582 }
583
584 static void write_msi_summary_info(MSIHANDLE db)
585 {
586     MSIHANDLE summary;
587     UINT r;
588
589     r = MsiGetSummaryInformationA(db, NULL, 4, &summary);
590     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
591
592     r = MsiSummaryInfoSetPropertyA(summary, PID_TEMPLATE, VT_LPSTR, 0, NULL, ";1033");
593     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
594
595     r = MsiSummaryInfoSetPropertyA(summary, PID_REVNUMBER, VT_LPSTR, 0, NULL,
596                                    "{004757CA-5092-49c2-AD20-28E1CE0DF5F2}");
597     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
598
599     r = MsiSummaryInfoSetPropertyA(summary, PID_PAGECOUNT, VT_I4, 100, NULL, NULL);
600     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
601
602     r = MsiSummaryInfoSetPropertyA(summary, PID_WORDCOUNT, VT_I4, 0, NULL, NULL);
603     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
604
605     /* write the summary changes back to the stream */
606     r = MsiSummaryInfoPersist(summary);
607     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
608
609     MsiCloseHandle(summary);
610 }
611
612 static void create_database(const CHAR *name, const msi_table *tables, int num_tables)
613 {
614     MSIHANDLE db;
615     UINT r;
616     int j;
617
618     r = MsiOpenDatabaseA(name, MSIDBOPEN_CREATE, &db);
619     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
620
621     /* import the tables into the database */
622     for (j = 0; j < num_tables; j++)
623     {
624         const msi_table *table = &tables[j];
625
626         write_file(table->filename, table->data, (table->size - 1) * sizeof(char));
627
628         r = MsiDatabaseImportA(db, CURR_DIR, table->filename);
629         ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
630
631         DeleteFileA(table->filename);
632     }
633
634     write_msi_summary_info(db);
635
636     r = MsiDatabaseCommit(db);
637     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
638
639     MsiCloseHandle(db);
640 }
641
642 static void check_service_is_installed(void)
643 {
644     SC_HANDLE scm, service;
645     BOOL res;
646
647     scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
648     ok(scm != NULL, "Failed to open the SC Manager\n");
649
650     service = OpenService(scm, "TestService", SC_MANAGER_ALL_ACCESS);
651     ok(service != NULL, "Failed to open TestService\n");
652
653     res = DeleteService(service);
654     ok(res, "Failed to delete TestService\n");
655 }
656
657 static void test_MsiInstallProduct(void)
658 {
659     UINT r;
660     CHAR path[MAX_PATH];
661     LONG res;
662     HKEY hkey;
663     DWORD num, size, type;
664
665     create_test_files();
666     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
667
668     r = MsiInstallProductA(msifile, NULL);
669     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
670
671     ok(delete_pf("msitest\\cabout\\new\\five.txt", TRUE), "File not installed\n");
672     ok(delete_pf("msitest\\cabout\\new", FALSE), "File not installed\n");
673     ok(delete_pf("msitest\\cabout\\four.txt", TRUE), "File not installed\n");
674     ok(delete_pf("msitest\\cabout", FALSE), "File not installed\n");
675     ok(delete_pf("msitest\\changed\\three.txt", TRUE), "File not installed\n");
676     ok(delete_pf("msitest\\changed", FALSE), "File not installed\n");
677     ok(delete_pf("msitest\\first\\two.txt", TRUE), "File not installed\n");
678     ok(delete_pf("msitest\\first", FALSE), "File not installed\n");
679     ok(delete_pf("msitest\\one.txt", TRUE), "File not installed\n");
680     ok(delete_pf("msitest\\filename", TRUE), "File not installed\n");
681     ok(delete_pf("msitest\\service.exe", TRUE), "File not installed\n");
682     ok(delete_pf("msitest", FALSE), "File not installed\n");
683
684     res = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wine\\msitest", &hkey);
685     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
686
687     size = MAX_PATH;
688     type = REG_SZ;
689     res = RegQueryValueExA(hkey, "Name", NULL, &type, (LPBYTE)path, &size);
690     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
691     ok(!lstrcmpA(path, "imaname"), "Expected imaname, got %s\n", path);
692
693     size = MAX_PATH;
694     type = REG_SZ;
695     res = RegQueryValueExA(hkey, "blah", NULL, &type, (LPBYTE)path, &size);
696     ok(res == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", res);
697
698     size = sizeof(num);
699     type = REG_DWORD;
700     res = RegQueryValueExA(hkey, "number", NULL, &type, (LPBYTE)&num, &size);
701     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
702     ok(num == 314, "Expected 314, got %d\n", num);
703
704     size = MAX_PATH;
705     type = REG_SZ;
706     res = RegQueryValueExA(hkey, "OrderTestName", NULL, &type, (LPBYTE)path, &size);
707     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
708     ok(!lstrcmpA(path, "OrderTestValue"), "Expected imaname, got %s\n", path);
709
710     check_service_is_installed();
711
712     RegDeleteKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wine\\msitest");
713
714     delete_test_files();
715 }
716
717 static void test_MsiSetComponentState(void)
718 {
719     INSTALLSTATE installed, action;
720     MSIHANDLE package;
721     char path[MAX_PATH];
722     UINT r;
723
724     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
725
726     CoInitialize(NULL);
727
728     lstrcpy(path, CURR_DIR);
729     lstrcat(path, "\\");
730     lstrcat(path, msifile);
731
732     r = MsiOpenPackage(path, &package);
733     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
734
735     r = MsiDoAction(package, "CostInitialize");
736     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
737
738     r = MsiDoAction(package, "FileCost");
739     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
740
741     r = MsiDoAction(package, "CostFinalize");
742     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
743
744     r = MsiGetComponentState(package, "dangler", &installed, &action);
745     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
746     ok(installed == INSTALLSTATE_ABSENT, "Expected INSTALLSTATE_ABSENT, got %d\n", installed);
747     ok(action == INSTALLSTATE_UNKNOWN, "Expected INSTALLSTATE_UNKNOWN, got %d\n", action);
748
749     r = MsiSetComponentState(package, "dangler", INSTALLSTATE_SOURCE);
750     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
751
752     MsiCloseHandle(package);
753     CoUninitialize();
754
755     DeleteFileA(msifile);
756 }
757
758 static void test_packagecoltypes(void)
759 {
760     MSIHANDLE hdb, view, rec;
761     char path[MAX_PATH];
762     LPCSTR query;
763     UINT r, count;
764
765     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
766
767     CoInitialize(NULL);
768
769     lstrcpy(path, CURR_DIR);
770     lstrcat(path, "\\");
771     lstrcat(path, msifile);
772
773     r = MsiOpenDatabase(path, MSIDBOPEN_READONLY, &hdb);
774     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
775
776     query = "SELECT * FROM `Media`";
777     r = MsiDatabaseOpenView( hdb, query, &view );
778     ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
779
780     r = MsiViewGetColumnInfo( view, MSICOLINFO_NAMES, &rec );
781     count = MsiRecordGetFieldCount( rec );
782     ok(r == ERROR_SUCCESS, "MsiViewGetColumnInfo failed\n");
783     ok(count == 6, "Expected 6, got %d\n", count);
784     ok(check_record(rec, 1, "DiskId"), "wrong column label\n");
785     ok(check_record(rec, 2, "LastSequence"), "wrong column label\n");
786     ok(check_record(rec, 3, "DiskPrompt"), "wrong column label\n");
787     ok(check_record(rec, 4, "Cabinet"), "wrong column label\n");
788     ok(check_record(rec, 5, "VolumeLabel"), "wrong column label\n");
789     ok(check_record(rec, 6, "Source"), "wrong column label\n");
790     MsiCloseHandle(rec);
791
792     r = MsiViewGetColumnInfo( view, MSICOLINFO_TYPES, &rec );
793     count = MsiRecordGetFieldCount( rec );
794     ok(r == ERROR_SUCCESS, "MsiViewGetColumnInfo failed\n");
795     ok(count == 6, "Expected 6, got %d\n", count);
796     ok(check_record(rec, 1, "i2"), "wrong column label\n");
797     ok(check_record(rec, 2, "i4"), "wrong column label\n");
798     ok(check_record(rec, 3, "L64"), "wrong column label\n");
799     ok(check_record(rec, 4, "S255"), "wrong column label\n");
800     ok(check_record(rec, 5, "S32"), "wrong column label\n");
801     ok(check_record(rec, 6, "S72"), "wrong column label\n");
802
803     MsiCloseHandle(rec);
804     MsiCloseHandle(view);
805     MsiCloseHandle(hdb);
806     DeleteFile(msifile);
807 }
808
809 static void create_cc_test_files(void)
810 {
811     CCAB cabParams;
812     HFCI hfci;
813     ERF erf;
814     static CHAR cab_context[] = "test%d.cab";
815     BOOL res;
816
817     create_file("maximus", 500);
818     create_file("augustus", 50000);
819     create_file("caesar", 500);
820
821     set_cab_parameters(&cabParams, "test1.cab", 200);
822
823     hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
824                       fci_read, fci_write, fci_close, fci_seek, fci_delete,
825                       get_temp_file, &cabParams, cab_context);
826     ok(hfci != NULL, "Failed to create an FCI context\n");
827
828     /* spews out hundreds of cab files.  re-enable when cabinet.dll is fixed */
829 #if 0
830     res = add_file(hfci, "maximus", tcompTYPE_MSZIP);
831     ok(res, "Failed to add file maximus\n");
832
833     res = add_file(hfci, "augustus", tcompTYPE_MSZIP);
834     todo_wine
835     {
836         ok(res, "Failed to add file augustus\n");
837     }
838 #endif
839
840     res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
841     ok(res, "Failed to flush the cabinet\n");
842
843     res = FCIDestroy(hfci);
844     ok(res, "Failed to destroy the cabinet\n");
845
846     create_cab_file("test3.cab", MEDIA_SIZE, "caesar\0");
847
848     DeleteFile("maximus");
849     DeleteFile("augustus");
850     DeleteFile("caesar");
851 }
852
853 static void test_continuouscabs(void)
854 {
855     UINT r;
856
857     create_cc_test_files();
858     create_database(msifile, cc_tables, sizeof(cc_tables) / sizeof(msi_table));
859
860     MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL);
861
862     r = MsiInstallProductA(msifile, NULL);
863     todo_wine
864     {
865         ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
866     }
867
868     todo_wine
869     {
870         ok(delete_pf("msitest\\maximus", TRUE), "File not installed\n");
871         ok(delete_pf("msitest\\augustus", TRUE), "File not installed\n");
872         ok(delete_pf("msitest\\caesar", TRUE), "File not installed\n");
873     }
874     ok(delete_pf("msitest", FALSE), "File not installed\n");
875
876     DeleteFile("test1.cab");
877     DeleteFile("test2.cab");
878     DeleteFile("test3.cab");
879     DeleteFile(msifile);
880 }
881
882 static void test_caborder(void)
883 {
884     UINT r;
885
886     create_file("imperator", 100);
887     create_file("maximus", 500);
888     create_file("augustus", 50000);
889     create_file("caesar", 500);
890
891     create_database(msifile, cc_tables, sizeof(cc_tables) / sizeof(msi_table));
892
893     MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL);
894
895     create_cab_file("test1.cab", MEDIA_SIZE, "maximus\0");
896     create_cab_file("test2.cab", MEDIA_SIZE, "augustus\0");
897     create_cab_file("test3.cab", MEDIA_SIZE, "caesar\0");
898
899     r = MsiInstallProductA(msifile, NULL);
900     ok(r == ERROR_INSTALL_FAILURE, "Expected ERROR_INSTALL_FAILURE, got %u\n", r);
901     ok(!delete_pf("msitest\\augustus", TRUE), "File is installed\n");
902     ok(!delete_pf("msitest\\caesar", TRUE), "File is installed\n");
903     todo_wine
904     {
905         ok(!delete_pf("msitest\\maximus", TRUE), "File is installed\n");
906         ok(!delete_pf("msitest", FALSE), "File is installed\n");
907     }
908
909     DeleteFile("test1.cab");
910     DeleteFile("test2.cab");
911     DeleteFile("test3.cab");
912
913     create_cab_file("test1.cab", MEDIA_SIZE, "imperator\0");
914     create_cab_file("test2.cab", MEDIA_SIZE, "maximus\0augustus\0");
915     create_cab_file("test3.cab", MEDIA_SIZE, "caesar\0");
916
917     r = MsiInstallProductA(msifile, NULL);
918     ok(r == ERROR_INSTALL_FAILURE, "Expected ERROR_INSTALL_FAILURE, got %u\n", r);
919     ok(!delete_pf("msitest\\maximus", TRUE), "File is installed\n");
920     ok(!delete_pf("msitest\\augustus", TRUE), "File is installed\n");
921     ok(!delete_pf("msitest\\caesar", TRUE), "File is installed\n");
922     todo_wine
923     {
924         ok(!delete_pf("msitest", FALSE), "File is installed\n");
925     }
926
927     DeleteFile("test1.cab");
928     DeleteFile("test2.cab");
929     DeleteFile("test3.cab");
930     DeleteFile(msifile);
931
932     create_cc_test_files();
933     create_database(msifile, co_tables, sizeof(co_tables) / sizeof(msi_table));
934
935     r = MsiInstallProductA(msifile, NULL);
936     ok(!delete_pf("msitest\\augustus", TRUE), "File is installed\n");
937     ok(!delete_pf("msitest\\maximus", TRUE), "File is installed\n");
938     ok(!delete_pf("msitest\\caesar", TRUE), "File is installed\n");
939     todo_wine
940     {
941         ok(r == ERROR_INSTALL_FAILURE, "Expected ERROR_INSTALL_FAILURE, got %u\n", r);
942         ok(!delete_pf("msitest", FALSE), "File is installed\n");
943     }
944
945     DeleteFile("test1.cab");
946     DeleteFile("test2.cab");
947     DeleteFile("test3.cab");
948     DeleteFile(msifile);
949
950     create_cc_test_files();
951     create_database(msifile, co2_tables, sizeof(co2_tables) / sizeof(msi_table));
952
953     r = MsiInstallProductA(msifile, NULL);
954     ok(!delete_pf("msitest\\augustus", TRUE), "File is installed\n");
955     ok(!delete_pf("msitest\\maximus", TRUE), "File is installed\n");
956     ok(!delete_pf("msitest\\caesar", TRUE), "File is installed\n");
957     todo_wine
958     {
959         ok(r == ERROR_INSTALL_FAILURE, "Expected ERROR_INSTALL_FAILURE, got %u\n", r);
960         ok(!delete_pf("msitest", FALSE), "File is installed\n");
961     }
962
963     DeleteFile("test1.cab");
964     DeleteFile("test2.cab");
965     DeleteFile("test3.cab");
966     DeleteFile("imperator");
967     DeleteFile("maximus");
968     DeleteFile("augustus");
969     DeleteFile("caesar");
970     DeleteFile(msifile);
971 }
972
973 START_TEST(install)
974 {
975     DWORD len;
976     char temp_path[MAX_PATH], prev_path[MAX_PATH];
977
978     GetCurrentDirectoryA(MAX_PATH, prev_path);
979     GetTempPath(MAX_PATH, temp_path);
980     SetCurrentDirectoryA(temp_path);
981
982     lstrcpyA(CURR_DIR, temp_path);
983     len = lstrlenA(CURR_DIR);
984
985     if(len && (CURR_DIR[len - 1] == '\\'))
986         CURR_DIR[len - 1] = 0;
987
988     get_program_files_dir(PROG_FILES_DIR);
989
990     test_MsiInstallProduct();
991     test_MsiSetComponentState();
992     test_packagecoltypes();
993     test_continuouscabs();
994     test_caborder();
995
996     SetCurrentDirectoryA(prev_path);
997 }