msi: Only initialize a component's state if it is linked with a feature.
[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 admin_exec_seq_dat[] = "Action\tCondition\tSequence\n"
38                                          "s72\tS255\tI2\n"
39                                          "AdminExecuteSequence\tAction\n"
40                                          "CostFinalize\t\t1000\n"
41                                          "CostInitialize\t\t800\n"
42                                          "FileCost\t\t900\n"
43                                          "InstallAdminPackage\t\t3900\n"
44                                          "InstallFiles\t\t4000\n"
45                                          "InstallFinalize\t\t6600\n"
46                                          "InstallInitialize\t\t1500\n"
47                                          "InstallValidate\t\t1400";
48
49 static const CHAR advt_exec_seq_dat[] = "Action\tCondition\tSequence\n"
50                                         "s72\tS255\tI2\n"
51                                         "AdvtExecuteSequence\tAction\n"
52                                         "CostFinalize\t\t1000\n"
53                                         "CostInitialize\t\t800\n"
54                                         "CreateShortcuts\t\t4500\n"
55                                         "InstallFinalize\t\t6600\n"
56                                         "InstallInitialize\t\t1500\n"
57                                         "InstallValidate\t\t1400\n"
58                                         "PublishComponents\t\t6200\n"
59                                         "PublishFeatures\t\t6300\n"
60                                         "PublishProduct\t\t6400\n"
61                                         "RegisterClassInfo\t\t4600\n"
62                                         "RegisterExtensionInfo\t\t4700\n"
63                                         "RegisterMIMEInfo\t\t4900\n"
64                                         "RegisterProgIdInfo\t\t4800";
65
66 static const CHAR component_dat[] = "Component\tComponentId\tDirectory_\tAttributes\tCondition\tKeyPath\n"
67                                     "s72\tS38\ts72\ti2\tS255\tS72\n"
68                                     "Component\tComponent\n"
69                                     "Five\t{8CC92E9D-14B2-4CA4-B2AA-B11D02078087}\tNEWDIR\t2\t\tfive.txt\n"
70                                     "Four\t{FD37B4EA-7209-45C0-8917-535F35A2F080}\tCABOUTDIR\t2\t\tfour.txt\n"
71                                     "One\t{783B242E-E185-4A56-AF86-C09815EC053C}\tMSITESTDIR\t2\t\tone.txt\n"
72                                     "Three\t{010B6ADD-B27D-4EDD-9B3D-34C4F7D61684}\tCHANGEDDIR\t2\t\tthree.txt\n"
73                                     "Two\t{BF03D1A6-20DA-4A65-82F3-6CAC995915CE}\tFIRSTDIR\t2\t\ttwo.txt\n"
74                                     "dangler\t{6091DF25-EF96-45F1-B8E9-A9B1420C7A3C}\tTARGETDIR\t4\t\tregdata\n"
75                                     "component\t\tMSITESTDIR\t0\t1\tfile\n"
76                                     "service_comp\t\tMSITESTDIR\t0\t1\tservice_file";
77
78 static const CHAR directory_dat[] = "Directory\tDirectory_Parent\tDefaultDir\n"
79                                     "s72\tS72\tl255\n"
80                                     "Directory\tDirectory\n"
81                                     "CABOUTDIR\tMSITESTDIR\tcabout\n"
82                                     "CHANGEDDIR\tMSITESTDIR\tchanged:second\n"
83                                     "FIRSTDIR\tMSITESTDIR\tfirst\n"
84                                     "MSITESTDIR\tProgramFilesFolder\tmsitest\n"
85                                     "NEWDIR\tCABOUTDIR\tnew\n"
86                                     "ProgramFilesFolder\tTARGETDIR\t.\n"
87                                     "TARGETDIR\t\tSourceDir";
88
89 static const CHAR feature_dat[] = "Feature\tFeature_Parent\tTitle\tDescription\tDisplay\tLevel\tDirectory_\tAttributes\n"
90                                   "s38\tS38\tL64\tL255\tI2\ti2\tS72\ti2\n"
91                                   "Feature\tFeature\n"
92                                   "Five\t\tFive\tThe Five Feature\t5\t3\tNEWDIR\t0\n"
93                                   "Four\t\tFour\tThe Four Feature\t4\t3\tCABOUTDIR\t0\n"
94                                   "One\t\tOne\tThe One Feature\t1\t3\tMSITESTDIR\t0\n"
95                                   "Three\t\tThree\tThe Three Feature\t3\t3\tCHANGEDDIR\t0\n"
96                                   "Two\t\tTwo\tThe Two Feature\t2\t3\tFIRSTDIR\t0\n"
97                                   "feature\t\t\t\t2\t1\tTARGETDIR\t0\n"
98                                   "service_feature\t\t\t\t2\t1\tTARGETDIR\t0";
99
100 static const CHAR feature_comp_dat[] = "Feature_\tComponent_\n"
101                                        "s38\ts72\n"
102                                        "FeatureComponents\tFeature_\tComponent_\n"
103                                        "Five\tFive\n"
104                                        "Four\tFour\n"
105                                        "One\tOne\n"
106                                        "Three\tThree\n"
107                                        "Two\tTwo\n"
108                                        "feature\tcomponent\n"
109                                        "service_feature\tservice_comp\n";
110
111 static const CHAR file_dat[] = "File\tComponent_\tFileName\tFileSize\tVersion\tLanguage\tAttributes\tSequence\n"
112                                "s72\ts72\tl255\ti4\tS72\tS20\tI2\ti2\n"
113                                "File\tFile\n"
114                                "five.txt\tFive\tfive.txt\t1000\t\t\t16384\t5\n"
115                                "four.txt\tFour\tfour.txt\t1000\t\t\t16384\t4\n"
116                                "one.txt\tOne\tone.txt\t1000\t\t\t0\t1\n"
117                                "three.txt\tThree\tthree.txt\t1000\t\t\t0\t3\n"
118                                "two.txt\tTwo\ttwo.txt\t1000\t\t\t0\t2\n"
119                                "file\tcomponent\tfilename\t100\t\t\t8192\t1\n"
120                                "service_file\tservice_comp\tservice.exe\t100\t\t\t8192\t1";
121
122 static const CHAR install_exec_seq_dat[] = "Action\tCondition\tSequence\n"
123                                            "s72\tS255\tI2\n"
124                                            "InstallExecuteSequence\tAction\n"
125                                            "AllocateRegistrySpace\tNOT Installed\t1550\n"
126                                            "CostFinalize\t\t1000\n"
127                                            "CostInitialize\t\t800\n"
128                                            "FileCost\t\t900\n"
129                                            "InstallFiles\t\t4000\n"
130                                            "InstallServices\t\t5000\n"
131                                            "InstallFinalize\t\t6600\n"
132                                            "InstallInitialize\t\t1500\n"
133                                            "InstallValidate\t\t1400\n"
134                                            "LaunchConditions\t\t100\n"
135                                            "WriteRegistryValues\tSourceDir And SOURCEDIR\t5000";
136
137 static const CHAR media_dat[] = "DiskId\tLastSequence\tDiskPrompt\tCabinet\tVolumeLabel\tSource\n"
138                                 "i2\ti4\tL64\tS255\tS32\tS72\n"
139                                 "Media\tDiskId\n"
140                                 "1\t3\t\t\tDISK1\t\n"
141                                 "2\t5\t\tmsitest.cab\tDISK2\t\n";
142
143 static const CHAR property_dat[] = "Property\tValue\n"
144                                    "s72\tl0\n"
145                                    "Property\tProperty\n"
146                                    "DefaultUIFont\tDlgFont8\n"
147                                    "INSTALLLEVEL\t3\n"
148                                    "InstallMode\tTypical\n"
149                                    "Manufacturer\tWine\n"
150                                    "PIDTemplate\t12345<###-%%%%%%%>@@@@@\n"
151                                    "ProductCode\t{F1C3AF50-8B56-4A69-A00C-00773FE42F30}\n"
152                                    "ProductID\tnone\n"
153                                    "ProductLanguage\t1033\n"
154                                    "ProductName\tMSITEST\n"
155                                    "ProductVersion\t1.1.1\n"
156                                    "PROMPTROLLBACKCOST\tP\n"
157                                    "Setup\tSetup\n"
158                                    "UpgradeCode\t{CE067E8D-2E1A-4367-B734-4EB2BDAD6565}";
159
160 static const CHAR registry_dat[] = "Registry\tRoot\tKey\tName\tValue\tComponent_\n"
161                                    "s72\ti2\tl255\tL255\tL0\ts72\n"
162                                    "Registry\tRegistry\n"
163                                    "Apples\t2\tSOFTWARE\\Wine\\msitest\tName\timaname\tOne\n"
164                                    "Oranges\t2\tSOFTWARE\\Wine\\msitest\tnumber\t#314\tTwo\n"
165                                    "regdata\t2\tSOFTWARE\\Wine\\msitest\tblah\tbad\tdangler\n"
166                                    "OrderTest\t2\tSOFTWARE\\Wine\\msitest\tOrderTestName\tOrderTestValue\tcomponent";
167
168 static const CHAR service_install_dat[] = "ServiceInstall\tName\tDisplayName\tServiceType\tStartType\tErrorControl\t"
169                                           "LoadOrderGroup\tDependencies\tStartName\tPassword\tArguments\tComponent_\tDescription\n"
170                                           "s72\ts255\tL255\ti4\ti4\ti4\tS255\tS255\tS255\tS255\tS255\ts72\tL255\n"
171                                           "ServiceInstall\tServiceInstall\n"
172                                           "TestService\tTestService\tTestService\t2\t3\t0\t\t\tTestService\t\t\tservice_comp\t\t";
173
174 static const CHAR service_control_dat[] = "ServiceControl\tName\tEvent\tArguments\tWait\tComponent_\n"
175                                           "s72\tl255\ti2\tL255\tI2\ts72\n"
176                                           "ServiceControl\tServiceControl\n"
177                                           "ServiceControl\tTestService\t8\t\t0\tservice_comp";
178
179 typedef struct _msi_table
180 {
181     const CHAR *filename;
182     const CHAR *data;
183     int size;
184 } msi_table;
185
186 #define ADD_TABLE(x) {#x".idt", x##_dat, sizeof(x##_dat)}
187
188 static const msi_table tables[] =
189 {
190     ADD_TABLE(admin_exec_seq),
191     ADD_TABLE(advt_exec_seq),
192     ADD_TABLE(component),
193     ADD_TABLE(directory),
194     ADD_TABLE(feature),
195     ADD_TABLE(feature_comp),
196     ADD_TABLE(file),
197     ADD_TABLE(install_exec_seq),
198     ADD_TABLE(media),
199     ADD_TABLE(property),
200     ADD_TABLE(registry),
201     ADD_TABLE(service_install),
202     ADD_TABLE(service_control)
203 };
204
205 /* cabinet definitions */
206
207 /* make the max size large so there is only one cab file */
208 #define MEDIA_SIZE          999999999
209 #define FOLDER_THRESHOLD    900000
210
211 /* The following defintions were copied from dlls/cabinet/cabinet.h
212  * because they are undocumented in windows.
213  */
214
215 /* EXTRACTdest flags */
216 #define EXTRACT_FILLFILELIST  0x00000001
217 #define EXTRACT_EXTRACTFILES  0x00000002
218
219 struct ExtractFileList {
220     LPSTR  filename;
221     struct ExtractFileList *next;
222     BOOL   unknown;  /* always 1L */
223 };
224
225 /* the first parameter of the function extract */
226 typedef struct {
227     long   result1;          /* 0x000 */
228     long   unknown1[3];      /* 0x004 */
229     struct ExtractFileList *filelist; /* 0x010 */
230     long   filecount;        /* 0x014 */
231     long   flags;            /* 0x018 */
232     char   directory[0x104]; /* 0x01c */
233     char   lastfile[0x20c];  /* 0x120 */
234 } EXTRACTDEST;
235
236 /* cabinet function pointers */
237 HMODULE hCabinet;
238 static HRESULT (WINAPI *pExtract)(EXTRACTDEST*, LPCSTR);
239
240 /* the FCI callbacks */
241
242 static void *mem_alloc(ULONG cb)
243 {
244     return HeapAlloc(GetProcessHeap(), 0, cb);
245 }
246
247 static void mem_free(void *memory)
248 {
249     HeapFree(GetProcessHeap(), 0, memory);
250 }
251
252 static BOOL get_next_cabinet(PCCAB pccab, ULONG  cbPrevCab, void *pv)
253 {
254     return TRUE;
255 }
256
257 static long progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv)
258 {
259     return 0;
260 }
261
262 static int file_placed(PCCAB pccab, char *pszFile, long cbFile,
263                        BOOL fContinuation, void *pv)
264 {
265     return 0;
266 }
267
268 static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv)
269 {
270     HANDLE handle;
271     DWORD dwAccess = 0;
272     DWORD dwShareMode = 0;
273     DWORD dwCreateDisposition = OPEN_EXISTING;
274     
275     dwAccess = GENERIC_READ | GENERIC_WRITE;
276     /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */
277     dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
278
279     if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES)
280         dwCreateDisposition = OPEN_EXISTING;
281     else
282         dwCreateDisposition = CREATE_NEW;
283
284     handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
285                          dwCreateDisposition, 0, NULL);
286
287     ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile);
288
289     return (INT_PTR)handle;
290 }
291
292 static UINT fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
293 {
294     HANDLE handle = (HANDLE)hf;
295     DWORD dwRead;
296     BOOL res;
297     
298     res = ReadFile(handle, memory, cb, &dwRead, NULL);
299     ok(res, "Failed to ReadFile\n");
300
301     return dwRead;
302 }
303
304 static UINT fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv)
305 {
306     HANDLE handle = (HANDLE)hf;
307     DWORD dwWritten;
308     BOOL res;
309
310     res = WriteFile(handle, memory, cb, &dwWritten, NULL);
311     ok(res, "Failed to WriteFile\n");
312
313     return dwWritten;
314 }
315
316 static int fci_close(INT_PTR hf, int *err, void *pv)
317 {
318     HANDLE handle = (HANDLE)hf;
319     ok(CloseHandle(handle), "Failed to CloseHandle\n");
320
321     return 0;
322 }
323
324 static long fci_seek(INT_PTR hf, long dist, int seektype, int *err, void *pv)
325 {
326     HANDLE handle = (HANDLE)hf;
327     DWORD ret;
328     
329     ret = SetFilePointer(handle, dist, NULL, seektype);
330     ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n");
331
332     return ret;
333 }
334
335 static int fci_delete(char *pszFile, int *err, void *pv)
336 {
337     BOOL ret = DeleteFileA(pszFile);
338     ok(ret, "Failed to DeleteFile %s\n", pszFile);
339
340     return 0;
341 }
342
343 static BOOL check_record(MSIHANDLE rec, UINT field, LPCSTR val)
344 {
345     CHAR buffer[0x20];
346     UINT r;
347     DWORD sz;
348
349     sz = sizeof buffer;
350     r = MsiRecordGetString(rec, field, buffer, &sz);
351     return (r == ERROR_SUCCESS ) && !strcmp(val, buffer);
352 }
353
354 static BOOL get_temp_file(char *pszTempName, int cbTempName, void *pv)
355 {
356     LPSTR tempname;
357
358     tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
359     GetTempFileNameA(".", "xx", 0, tempname);
360
361     if (tempname && (strlen(tempname) < (unsigned)cbTempName))
362     {
363         lstrcpyA(pszTempName, tempname);
364         HeapFree(GetProcessHeap(), 0, tempname);
365         return TRUE;
366     }
367
368     HeapFree(GetProcessHeap(), 0, tempname);
369
370     return FALSE;
371 }
372
373 static INT_PTR get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
374                              USHORT *pattribs, int *err, void *pv)
375 {
376     BY_HANDLE_FILE_INFORMATION finfo;
377     FILETIME filetime;
378     HANDLE handle;
379     DWORD attrs;
380     BOOL res;
381
382     handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
383                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
384
385     ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);
386
387     res = GetFileInformationByHandle(handle, &finfo);
388     ok(res, "Expected GetFileInformationByHandle to succeed\n");
389    
390     FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
391     FileTimeToDosDateTime(&filetime, pdate, ptime);
392
393     attrs = GetFileAttributes(pszName);
394     ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");
395
396     return (INT_PTR)handle;
397 }
398
399 static void add_file(HFCI hfci, char *file)
400 {
401     char path[MAX_PATH];
402     BOOL res;
403
404     lstrcpyA(path, CURR_DIR);
405     lstrcatA(path, "\\");
406     lstrcatA(path, file);
407
408     res = FCIAddFile(hfci, path, file, FALSE, get_next_cabinet, progress,
409                      get_open_info, tcompTYPE_MSZIP);
410     ok(res, "Expected FCIAddFile to succeed\n");
411 }
412
413 static void set_cab_parameters(PCCAB pCabParams, const CHAR *name)
414 {
415     ZeroMemory(pCabParams, sizeof(CCAB));
416
417     pCabParams->cb = MEDIA_SIZE;
418     pCabParams->cbFolderThresh = FOLDER_THRESHOLD;
419     pCabParams->setID = 0xbeef;
420     lstrcpyA(pCabParams->szCabPath, CURR_DIR);
421     lstrcatA(pCabParams->szCabPath, "\\");
422     lstrcpyA(pCabParams->szCab, name);
423 }
424
425 static void create_cab_file(const CHAR *name)
426 {
427     CCAB cabParams;
428     HFCI hfci;
429     ERF erf;
430     static CHAR four_txt[] = "four.txt",
431                 five_txt[] = "five.txt";
432     BOOL res;
433
434     set_cab_parameters(&cabParams, name);
435
436     hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open,
437                       fci_read, fci_write, fci_close, fci_seek, fci_delete,
438                       get_temp_file, &cabParams, NULL);
439
440     ok(hfci != NULL, "Failed to create an FCI context\n");
441
442     add_file(hfci, four_txt);
443     add_file(hfci, five_txt);
444
445     res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress);
446     ok(res, "Failed to flush the cabinet\n");
447
448     res = FCIDestroy(hfci);
449     ok(res, "Failed to destroy the cabinet\n");
450 }
451
452 static BOOL init_function_pointers(void)
453 {
454     hCabinet = LoadLibraryA("cabinet.dll");
455     if (!hCabinet)
456         return FALSE;
457
458     pExtract = (void *)GetProcAddress(hCabinet, "Extract");
459     if (!pExtract)
460         return FALSE;
461
462     return TRUE;
463 }
464
465 static BOOL get_program_files_dir(LPSTR buf)
466 {
467     HKEY hkey;
468     DWORD type = REG_EXPAND_SZ, size;
469
470     if (RegOpenKey(HKEY_LOCAL_MACHINE,
471                    "Software\\Microsoft\\Windows\\CurrentVersion", &hkey))
472         return FALSE;
473
474     size = MAX_PATH;
475     if (RegQueryValueEx(hkey, "ProgramFilesDir", 0, &type, (LPBYTE)buf, &size))
476         return FALSE;
477
478     RegCloseKey(hkey);
479     return TRUE;
480 }
481
482 static void create_file(const CHAR *name)
483 {
484     HANDLE file;
485     DWORD written;
486
487     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
488     ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
489     WriteFile(file, name, strlen(name), &written, NULL);
490     WriteFile(file, "\n", strlen("\n"), &written, NULL);
491     CloseHandle(file);
492 }
493
494 static void create_test_files(void)
495 {
496     get_program_files_dir(PROG_FILES_DIR);
497
498     CreateDirectoryA("msitest", NULL);
499     create_file("msitest\\one.txt");
500     CreateDirectoryA("msitest\\first", NULL);
501     create_file("msitest\\first\\two.txt");
502     CreateDirectoryA("msitest\\second", NULL);
503     create_file("msitest\\second\\three.txt");
504
505     create_file("four.txt");
506     create_file("five.txt");
507     create_cab_file("msitest.cab");
508
509     create_file("msitest\\filename");
510     create_file("msitest\\service.exe");
511
512     DeleteFileA("four.txt");
513     DeleteFileA("five.txt");
514 }
515
516 static BOOL delete_pf(const CHAR *rel_path, BOOL is_file)
517 {
518     CHAR path[MAX_PATH];
519
520     lstrcpyA(path, PROG_FILES_DIR);
521     lstrcatA(path, "\\");
522     lstrcatA(path, rel_path);
523
524     if (is_file)
525         return DeleteFileA(path);
526     else
527         return RemoveDirectoryA(path);
528 }
529
530 static void delete_test_files(void)
531 {
532     DeleteFileA("msitest.msi");
533     DeleteFileA("msitest.cab");
534     DeleteFileA("msitest\\second\\three.txt");
535     DeleteFileA("msitest\\first\\two.txt");
536     DeleteFileA("msitest\\one.txt");
537     DeleteFileA("msitest\\service.exe");
538     DeleteFileA("msitest\\filename");
539     RemoveDirectoryA("msitest\\second");
540     RemoveDirectoryA("msitest\\first");
541     RemoveDirectoryA("msitest");
542 }
543
544 static void write_file(const CHAR *filename, const char *data, int data_size)
545 {
546     DWORD size;
547
548     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
549                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
550
551     WriteFile(hf, data, data_size, &size, NULL);
552     CloseHandle(hf);
553 }
554
555 static void write_msi_summary_info(MSIHANDLE db)
556 {
557     MSIHANDLE summary;
558     UINT r;
559
560     r = MsiGetSummaryInformationA(db, NULL, 4, &summary);
561     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
562
563     r = MsiSummaryInfoSetPropertyA(summary, PID_TEMPLATE, VT_LPSTR, 0, NULL, ";1033");
564     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
565
566     r = MsiSummaryInfoSetPropertyA(summary, PID_REVNUMBER, VT_LPSTR, 0, NULL,
567                                    "{004757CA-5092-49c2-AD20-28E1CE0DF5F2}");
568     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
569
570     r = MsiSummaryInfoSetPropertyA(summary, PID_PAGECOUNT, VT_I4, 100, NULL, NULL);
571     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
572
573     r = MsiSummaryInfoSetPropertyA(summary, PID_WORDCOUNT, VT_I4, 0, NULL, NULL);
574     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
575
576     /* write the summary changes back to the stream */
577     r = MsiSummaryInfoPersist(summary);
578     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
579
580     MsiCloseHandle(summary);
581 }
582
583 static void create_database(const CHAR *name, const msi_table *tables, int num_tables)
584 {
585     MSIHANDLE db;
586     UINT r;
587     int j;
588
589     r = MsiOpenDatabaseA(name, MSIDBOPEN_CREATE, &db);
590     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
591
592     /* import the tables into the database */
593     for (j = 0; j < num_tables; j++)
594     {
595         const msi_table *table = &tables[j];
596
597         write_file(table->filename, table->data, (table->size - 1) * sizeof(char));
598
599         r = MsiDatabaseImportA(db, CURR_DIR, table->filename);
600         ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
601
602         DeleteFileA(table->filename);
603     }
604
605     write_msi_summary_info(db);
606
607     r = MsiDatabaseCommit(db);
608     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
609
610     MsiCloseHandle(db);
611 }
612
613 static void check_service_is_installed(void)
614 {
615     SC_HANDLE scm, service;
616     BOOL res;
617
618     scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
619     ok(scm != NULL, "Failed to open the SC Manager\n");
620
621     service = OpenService(scm, "TestService", SC_MANAGER_ALL_ACCESS);
622     ok(service != NULL, "Failed to open TestService\n");
623
624     res = DeleteService(service);
625     ok(res, "Failed to delete TestService\n");
626 }
627
628 static void test_MsiInstallProduct(void)
629 {
630     UINT r;
631     CHAR path[MAX_PATH];
632     LONG res;
633     HKEY hkey;
634     DWORD num, size, type;
635
636     create_test_files();
637     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
638
639     r = MsiInstallProductA(msifile, NULL);
640     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
641
642     ok(delete_pf("msitest\\cabout\\new\\five.txt", TRUE), "File not installed\n");
643     ok(delete_pf("msitest\\cabout\\new", FALSE), "File not installed\n");
644     ok(delete_pf("msitest\\cabout\\four.txt", TRUE), "File not installed\n");
645     ok(delete_pf("msitest\\cabout", FALSE), "File not installed\n");
646     ok(delete_pf("msitest\\changed\\three.txt", TRUE), "File not installed\n");
647     ok(delete_pf("msitest\\changed", FALSE), "File not installed\n");
648     ok(delete_pf("msitest\\first\\two.txt", TRUE), "File not installed\n");
649     ok(delete_pf("msitest\\first", FALSE), "File not installed\n");
650     ok(delete_pf("msitest\\one.txt", TRUE), "File not installed\n");
651     ok(delete_pf("msitest\\filename", TRUE), "File not installed\n");
652     ok(delete_pf("msitest\\service.exe", TRUE), "File not installed\n");
653     ok(delete_pf("msitest", FALSE), "File not installed\n");
654
655     res = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wine\\msitest", &hkey);
656     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
657
658     size = MAX_PATH;
659     type = REG_SZ;
660     res = RegQueryValueExA(hkey, "Name", NULL, &type, (LPBYTE)path, &size);
661     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
662     ok(!lstrcmpA(path, "imaname"), "Expected imaname, got %s\n", path);
663
664     size = MAX_PATH;
665     type = REG_SZ;
666     res = RegQueryValueExA(hkey, "blah", NULL, &type, (LPBYTE)path, &size);
667     ok(res == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", res);
668
669     size = sizeof(num);
670     type = REG_DWORD;
671     res = RegQueryValueExA(hkey, "number", NULL, &type, (LPBYTE)&num, &size);
672     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
673     ok(num == 314, "Expected 314, got %d\n", num);
674
675     size = MAX_PATH;
676     type = REG_SZ;
677     res = RegQueryValueExA(hkey, "OrderTestName", NULL, &type, (LPBYTE)path, &size);
678     ok(res == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", res);
679     ok(!lstrcmpA(path, "OrderTestValue"), "Expected imaname, got %s\n", path);
680
681     check_service_is_installed();
682
683     RegDeleteKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wine\\msitest");
684
685     delete_test_files();
686 }
687
688 static void test_MsiSetComponentState(void)
689 {
690     INSTALLSTATE installed, action;
691     MSIHANDLE package;
692     char path[MAX_PATH];
693     UINT r;
694
695     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
696
697     CoInitialize(NULL);
698
699     lstrcpy(path, CURR_DIR);
700     lstrcat(path, "\\");
701     lstrcat(path, msifile);
702
703     r = MsiOpenPackage(path, &package);
704     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
705
706     r = MsiDoAction(package, "CostInitialize");
707     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
708
709     r = MsiDoAction(package, "FileCost");
710     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
711
712     r = MsiDoAction(package, "CostFinalize");
713     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
714
715     r = MsiGetComponentState(package, "dangler", &installed, &action);
716     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
717     ok(installed == INSTALLSTATE_ABSENT, "Expected INSTALLSTATE_ABSENT, got %d\n", installed);
718     ok(action == INSTALLSTATE_UNKNOWN, "Expected INSTALLSTATE_UNKNOWN, got %d\n", action);
719
720     r = MsiSetComponentState(package, "dangler", INSTALLSTATE_SOURCE);
721     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
722
723     MsiCloseHandle(package);
724     CoUninitialize();
725
726     DeleteFileA(msifile);
727 }
728
729 static void test_packagecoltypes(void)
730 {
731     MSIHANDLE hdb, view, rec;
732     char path[MAX_PATH];
733     LPCSTR query;
734     UINT r, count;
735
736     create_database(msifile, tables, sizeof(tables) / sizeof(msi_table));
737
738     CoInitialize(NULL);
739
740     lstrcpy(path, CURR_DIR);
741     lstrcat(path, "\\");
742     lstrcat(path, msifile);
743
744     r = MsiOpenDatabase(path, MSIDBOPEN_READONLY, &hdb);
745     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
746
747     query = "SELECT * FROM `Media`";
748     r = MsiDatabaseOpenView( hdb, query, &view );
749     ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
750
751     r = MsiViewGetColumnInfo( view, MSICOLINFO_NAMES, &rec );
752     count = MsiRecordGetFieldCount( rec );
753     ok(r == ERROR_SUCCESS, "MsiViewGetColumnInfo failed\n");
754     ok(count == 6, "Expected 6, got %d\n", count);
755     ok(check_record(rec, 1, "DiskId"), "wrong column label\n");
756     ok(check_record(rec, 2, "LastSequence"), "wrong column label\n");
757     ok(check_record(rec, 3, "DiskPrompt"), "wrong column label\n");
758     ok(check_record(rec, 4, "Cabinet"), "wrong column label\n");
759     ok(check_record(rec, 5, "VolumeLabel"), "wrong column label\n");
760     ok(check_record(rec, 6, "Source"), "wrong column label\n");
761     MsiCloseHandle(rec);
762
763     r = MsiViewGetColumnInfo( view, MSICOLINFO_TYPES, &rec );
764     count = MsiRecordGetFieldCount( rec );
765     ok(r == ERROR_SUCCESS, "MsiViewGetColumnInfo failed\n");
766     ok(count == 6, "Expected 6, got %d\n", count);
767     ok(check_record(rec, 4, "S255"), "wrong column label\n");
768     ok(check_record(rec, 5, "S32"), "wrong column label\n");
769     ok(check_record(rec, 6, "S72"), "wrong column label\n");
770     todo_wine
771     {
772         ok(check_record(rec, 1, "i2"), "wrong column label\n");
773         ok(check_record(rec, 2, "i4"), "wrong column label\n");
774         ok(check_record(rec, 3, "L64"), "wrong column label\n");
775     }
776
777     MsiCloseHandle(rec);
778     MsiCloseHandle(view);
779     MsiCloseHandle(hdb);
780     DeleteFile(msifile);
781 }
782
783 START_TEST(install)
784 {
785     DWORD len;
786
787     if (!init_function_pointers())
788         return;
789
790     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
791     len = lstrlenA(CURR_DIR);
792
793     if(len && (CURR_DIR[len-1] == '\\'))
794         CURR_DIR[len - 1] = 0;
795
796     test_MsiInstallProduct();
797     test_MsiSetComponentState();
798     test_packagecoltypes();
799 }