msi: Fix an uninitialized variable causing random conformance test failures.
[wine] / dlls / msi / tests / suminfo.c
1 /*
2  * Copyright (C) 2005 Mike McCormack for CodeWeavers
3  *
4  * A test program for MSI database files.
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 #define COBJMACROS
22
23 #include <stdio.h>
24 #include <windows.h>
25 #include <msi.h>
26 #include <msiquery.h>
27 #include <objidl.h>
28
29 #include "wine/test.h"
30
31 /*
32  * The following are defined in Windows SDK's msidefs.h
33  * but that file doesn't exist in the msvc6 header set.
34  *
35  * Some are already defined in PropIdl.h - undefine them
36  */
37 #undef PID_DICTIONARY
38 #undef PID_CODEPAGE
39 #undef PID_SUBJECT
40 #undef PID_SECURITY
41
42 #define PID_DICTIONARY 0
43 #define PID_CODEPAGE 1
44 #define PID_TITLE 2
45 #define PID_SUBJECT 3
46 #define PID_AUTHOR 4
47 #define PID_KEYWORDS 5
48 #define PID_COMMENTS 6
49 #define PID_TEMPLATE 7
50 #define PID_LASTAUTHOR 8
51 #define PID_REVNUMBER 9
52 #define PID_EDITTINE 10
53 #define PID_LASTPRINTED 11
54 #define PID_CREATE_DTM 12
55 #define PID_LASTSAVE_DTM 13
56 #define PID_PAGECOUNT 14
57 #define PID_WORDCOUNT 15
58 #define PID_CHARCOUNT 16
59 #define PID_THUMBNAIL 17
60 #define PID_APPNAME 18
61 #define PID_SECURITY 19
62 #define PID_MSIVERSION PID_PAGECOUNT
63 #define PID_MSISOURCE PID_WORDCOUNT
64 #define PID_MSIRESTRICT PID_CHARCOUNT
65
66 static void test_suminfo(void)
67 {
68     const char *msifile = "winetest.msi";
69     MSIHANDLE hdb = 0, hsuminfo;
70     UINT r, count, type;
71     DWORD sz;
72     INT val;
73     FILETIME ft;
74     char buf[0x10];
75
76     DeleteFile(msifile);
77
78     /* just MsiOpenDatabase should not create a file */
79     r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
80     ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
81
82     r = MsiGetSummaryInformation(hdb, NULL, 0, NULL);
83     ok(r == ERROR_INVALID_PARAMETER, "MsiGetSummaryInformation wrong error\n");
84
85     r = MsiGetSummaryInformation(hdb, NULL, 0, &hsuminfo);
86     ok(r == ERROR_SUCCESS, "MsiGetSummaryInformation failed\n");
87
88     r = MsiSummaryInfoGetPropertyCount(0, NULL);
89     ok(r == ERROR_INVALID_HANDLE, "getpropcount failed\n");
90
91     r = MsiSummaryInfoGetPropertyCount(hsuminfo, NULL);
92     ok(r == ERROR_SUCCESS, "getpropcount failed\n");
93
94     count = -1;
95     r = MsiSummaryInfoGetPropertyCount(hsuminfo, &count);
96     ok(r == ERROR_SUCCESS, "getpropcount failed\n");
97     ok(count == 0, "count should be zero\n");
98
99     r = MsiSummaryInfoGetProperty(hsuminfo, 0, NULL, NULL, NULL, 0, NULL);
100     ok(r == ERROR_SUCCESS, "getpropcount failed\n");
101
102     type = -1;
103     r = MsiSummaryInfoGetProperty(hsuminfo, 0, &type, NULL, NULL, 0, NULL);
104     ok(r == ERROR_SUCCESS, "getpropcount failed\n");
105     ok(type == 0, "wrong type\n");
106
107     type = -1;
108     val = 1234;
109     r = MsiSummaryInfoGetProperty(hsuminfo, 0, &type, &val, NULL, 0, NULL);
110     ok(r == ERROR_SUCCESS, "getpropcount failed\n");
111     ok(type == 0, "wrong type\n");
112     ok(val == 1234, "wrong val\n");
113
114     buf[0]='x';
115     buf[1]=0;
116     sz = 0x10;
117     r = MsiSummaryInfoGetProperty(hsuminfo, PID_REVNUMBER, &type, &val, NULL, buf, &sz);
118     ok(r == ERROR_SUCCESS, "getpropcount failed\n");
119     ok(buf[0]=='x', "cleared buffer\n");
120     ok(sz == 0x10, "count wasn't zero\n");
121     ok(type == VT_EMPTY, "should be empty\n");
122
123     r = MsiSummaryInfoSetProperty(hsuminfo, PID_TITLE, VT_LPSTR, 0, NULL, "Mike");
124     ok(r == ERROR_FUNCTION_FAILED, "MsiSummaryInfoSetProperty wrong error\n");
125
126     r = MsiSummaryInfoSetProperty(hsuminfo, PID_TITLE, VT_LPSTR, 1, NULL, "JungAh");
127     ok(r == ERROR_FUNCTION_FAILED, "MsiSummaryInfoSetProperty wrong error\n");
128
129     r = MsiSummaryInfoSetProperty(hsuminfo, PID_TITLE, VT_LPSTR, 1, &ft, "Mike");
130     ok(r == ERROR_FUNCTION_FAILED, "MsiSummaryInfoSetProperty wrong error\n");
131
132     r = MsiSummaryInfoSetProperty(hsuminfo, PID_CODEPAGE, VT_I2, 1, &ft, "JungAh");
133     ok(r == ERROR_FUNCTION_FAILED, "MsiSummaryInfoSetProperty wrong error\n");
134
135     r = MsiCloseHandle(hsuminfo);
136     ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
137
138     /* try again with the update count set */
139     r = MsiGetSummaryInformation(hdb, NULL, 1, &hsuminfo);
140     ok(r == ERROR_SUCCESS, "MsiGetSummaryInformation failed\n");
141
142     r = MsiSummaryInfoSetProperty(hsuminfo, 0, VT_LPSTR, 1, NULL, NULL);
143     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
144
145     r = MsiSummaryInfoSetProperty(hsuminfo, PID_CODEPAGE, VT_LPSTR, 1, NULL, NULL);
146     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
147
148     r = MsiSummaryInfoSetProperty(hsuminfo, PID_TITLE, VT_I4, 0, NULL, "Mike");
149     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
150
151     r = MsiSummaryInfoSetProperty(hsuminfo, PID_AUTHOR, VT_I4, 0, NULL, "JungAh");
152     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
153
154     r = MsiSummaryInfoSetProperty(hsuminfo, PID_KEYWORDS, VT_I2, 0, NULL, "Mike");
155     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
156
157     r = MsiSummaryInfoSetProperty(hsuminfo, PID_COMMENTS, VT_FILETIME, 0, NULL, "JungAh");
158     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
159
160     r = MsiSummaryInfoSetProperty(hsuminfo, PID_TEMPLATE, VT_I2, 0, NULL, "Mike");
161     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
162
163     r = MsiSummaryInfoSetProperty(hsuminfo, PID_LASTAUTHOR, VT_LPSTR, 0, NULL, NULL);
164     ok(r == ERROR_INVALID_PARAMETER, "MsiSummaryInfoSetProperty wrong error\n");
165
166     r = MsiSummaryInfoSetProperty(hsuminfo, PID_LASTSAVE_DTM, VT_FILETIME, 0, NULL, NULL);
167     ok(r == ERROR_INVALID_PARAMETER, "MsiSummaryInfoSetProperty wrong error\n");
168
169     r = MsiSummaryInfoSetProperty(hsuminfo, PID_LASTAUTHOR, VT_LPWSTR, 0, NULL, "h\0i\0\0");
170     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
171
172     r = MsiSummaryInfoSetProperty(hsuminfo, PID_REVNUMBER, VT_I4, 0, NULL, "Jungah");
173     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
174
175     r = MsiSummaryInfoSetProperty(hsuminfo, PID_PAGECOUNT, VT_LPSTR, 1, NULL, NULL);
176     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
177
178     r = MsiSummaryInfoSetProperty(hsuminfo, PID_TITLE, VT_LPSTR, 0, NULL, "Mike");
179     ok(r == ERROR_SUCCESS, "MsiSummaryInfoSetProperty failed\n");
180
181     sz = 2;
182     strcpy(buf,"x");
183     r = MsiSummaryInfoGetProperty(hsuminfo, PID_TITLE, &type, NULL, NULL, buf, &sz );
184     ok(r == ERROR_MORE_DATA, "MsiSummaryInfoSetProperty failed\n");
185     ok(sz == 4, "count was wrong\n");
186     ok(type == VT_LPSTR, "type was wrong\n");
187     ok(!strcmp(buf,"M"), "buffer was wrong\n");
188
189     sz = 4;
190     strcpy(buf,"x");
191     r = MsiSummaryInfoGetProperty(hsuminfo, PID_TITLE, &type, NULL, NULL, buf, &sz );
192     ok(r == ERROR_MORE_DATA, "MsiSummaryInfoSetProperty failed\n");
193     ok(sz == 4, "count was wrong\n");
194     ok(type == VT_LPSTR, "type was wrong\n");
195     ok(!strcmp(buf,"Mik"), "buffer was wrong\n");
196
197     r = MsiSummaryInfoSetProperty(hsuminfo, PID_TITLE, VT_LPSTR, 0, NULL, "JungAh");
198     ok(r == ERROR_SUCCESS, "MsiSummaryInfoSetProperty failed\n");
199
200     r = MsiSummaryInfoSetProperty(hsuminfo, PID_CODEPAGE, VT_I2, 1, &ft, "Mike");
201     ok(r == ERROR_FUNCTION_FAILED, "MsiSummaryInfoSetProperty wrong error\n");
202
203     r = MsiCloseHandle(hsuminfo);
204     ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
205
206     /* try again with a higher update count */
207     r = MsiGetSummaryInformation(hdb, NULL, 10, &hsuminfo);
208     ok(r == ERROR_SUCCESS, "MsiGetSummaryInformation failed\n");
209
210     r = MsiSummaryInfoSetProperty(hsuminfo, PID_TITLE, VT_LPSTR, 0, NULL, "JungAh");
211     ok(r == ERROR_SUCCESS, "MsiSummaryInfoSetProperty failed\n");
212
213     r = MsiSummaryInfoSetProperty(hsuminfo, PID_CODEPAGE, VT_LPSTR, 1, NULL, NULL);
214     ok(r == ERROR_DATATYPE_MISMATCH, "MsiSummaryInfoSetProperty wrong error\n");
215
216     r = MsiSummaryInfoSetProperty(hsuminfo, PID_CODEPAGE, VT_I2, 1, NULL, NULL);
217     ok(r == ERROR_SUCCESS, "MsiSummaryInfoSetProperty wrong error\n");
218
219     r = MsiSummaryInfoSetProperty(hsuminfo, PID_CODEPAGE, VT_I2, 1, &ft, "Mike");
220     ok(r == ERROR_SUCCESS, "MsiSummaryInfoSetProperty wrong error\n");
221
222     r = MsiSummaryInfoSetProperty(hsuminfo, PID_AUTHOR, VT_LPSTR, 1, &ft, "Mike");
223     ok(r == ERROR_SUCCESS, "MsiSummaryInfoSetProperty wrong error\n");
224
225     r = MsiSummaryInfoPersist(hsuminfo);
226     ok(r == ERROR_SUCCESS, "MsiSummaryInfoPersist failed\n");
227
228     MsiDatabaseCommit(hdb);
229
230     r = MsiCloseHandle(hsuminfo);
231     ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
232
233     r = MsiCloseHandle(hdb);
234     ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
235
236     r = DeleteFile(msifile);
237     ok(r, "DeleteFile failed\n");
238 }
239
240 static const WCHAR tb[] = { 0x4840, 0x3f7f, 0x4164, 0x422f, 0x4836, 0 }; /* _Tables */
241 static const WCHAR sd[] = { 0x4840, 0x3f3f, 0x4577, 0x446c, 0x3b6a, 0x45e4, 0x4824, 0 }; /* _StringData */
242 static const WCHAR sp[] = { 0x4840, 0x3f3f, 0x4577, 0x446c, 0x3e6a, 0x44b2, 0x482f, 0 }; /* _StringPool */
243
244 #define LOSE_CONST(x) ((LPSTR)(UINT_PTR)(x))
245
246 static void test_create_database_binary(void)
247 {
248     static const CLSID CLSID_MsiDatabase =
249         { 0xc1084, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46 } };
250     static const CLSID IID_IPropertySetStorage =
251         { 0x13a, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46 } };
252     static const CLSID FMTID_SummaryInformation =
253         { 0xf29f85e0, 0x4ff9, 0x1068, {0xab, 0x91, 0x08, 0x00, 0x2b, 0x27, 0xb3, 0xd9}};
254     DWORD mode = STGM_CREATE | STGM_READWRITE | STGM_DIRECT | STGM_SHARE_EXCLUSIVE;
255     static const WCHAR msifile[] = {
256         'w','i','n','e','t','e','s','t','.','m','s','i',0 };
257     IPropertySetStorage *pss = NULL;
258     IPropertyStorage *ps = NULL;
259     IStorage *stg = NULL;
260     IStream *stm = NULL;
261     HRESULT r;
262     PROPSPEC propspec[10];
263     PROPVARIANT propvar[10];
264     USHORT data[2] = { 0, 0 };
265
266     r = StgCreateDocfile( msifile, mode, 0, &stg );
267     ok( r == S_OK, "failed to create database\n");
268
269     r = IStorage_SetClass( stg, &CLSID_MsiDatabase );
270     ok( r == S_OK, "failed to set clsid\n");
271
272     /* create the _StringData stream */
273     r = IStorage_CreateStream( stg, sd, STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
274     ok( r == S_OK, "failed to create stream\n");
275
276     IStream_Release( stm );
277
278     /* create the _StringPool stream */
279     r = IStorage_CreateStream( stg, sp, STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
280     ok( r == S_OK, "failed to create stream\n");
281
282     r = IStream_Write( stm, data, sizeof data, NULL );
283     ok( r == S_OK, "failed to write stream\n");
284
285     IStream_Release( stm );
286
287     /* create the _Tables stream */
288     r = IStorage_CreateStream( stg, tb, STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
289     ok( r == S_OK, "failed to create stream\n");
290
291     IStream_Release( stm );
292
293     r = IStorage_QueryInterface( stg, &IID_IPropertySetStorage, (void**) &pss );
294     ok( r == S_OK, "failed to set clsid\n");
295
296     r = IPropertySetStorage_Create( pss, &FMTID_SummaryInformation, NULL, 0, mode, &ps );
297     ok( r == S_OK, "failed to create property set\n");
298
299     r = IPropertyStorage_SetClass( ps, &FMTID_SummaryInformation );
300     ok( r == S_OK, "failed to set class\n");
301
302     PropVariantClear( &propvar[0] );
303     propspec[0].lpwstr = NULL;
304     propspec[0].ulKind = PRSPEC_PROPID;
305     propspec[0].propid = PID_TITLE;
306     propvar[0].vt = VT_LPSTR;
307     U(propvar[0]).pszVal = LOSE_CONST("test title");
308
309     PropVariantClear( &propvar[1] );
310     propspec[1].lpwstr = NULL;
311     propspec[1].ulKind = PRSPEC_PROPID;
312     propspec[1].propid = PID_SUBJECT;
313     propvar[1].vt = VT_LPSTR;
314     U(propvar[1]).pszVal = LOSE_CONST("msi suminfo / property storage test");
315
316     PropVariantClear( &propvar[2] );
317     propspec[2].lpwstr = NULL;
318     propspec[2].ulKind = PRSPEC_PROPID;
319     propspec[2].propid = PID_AUTHOR;
320     propvar[2].vt = VT_LPSTR;
321     U(propvar[2]).pszVal = LOSE_CONST("mike_m");
322
323     PropVariantClear( &propvar[3] );
324     propspec[3].lpwstr = NULL;
325     propspec[3].ulKind = PRSPEC_PROPID;
326     propspec[3].propid = PID_TEMPLATE;
327     propvar[3].vt = VT_LPSTR;
328     U(propvar[3]).pszVal = LOSE_CONST(";1033");  /* actually the string table's codepage */
329
330     PropVariantClear( &propvar[4] );
331     propspec[4].lpwstr = NULL;
332     propspec[4].ulKind = PRSPEC_PROPID;
333     propspec[4].propid = PID_REVNUMBER;
334     propvar[4].vt = VT_LPSTR;
335     U(propvar[4]).pszVal = LOSE_CONST("{913B8D18-FBB6-4CAC-A239-C74C11E3FA74}");
336
337     PropVariantClear( &propvar[5] );
338     propspec[5].lpwstr = NULL;
339     propspec[5].ulKind = PRSPEC_PROPID;
340     propspec[5].propid = PID_PAGECOUNT;
341     propvar[5].vt = VT_I4;
342     U(propvar[5]).lVal = 100;
343
344     PropVariantClear( &propvar[6] );
345     propspec[6].lpwstr = NULL;
346     propspec[6].ulKind = PRSPEC_PROPID;
347     propspec[6].propid = PID_WORDCOUNT;
348     propvar[6].vt = VT_I4;
349     U(propvar[6]).lVal = 0;
350
351     /* MSDN says that PID_LASTPRINTED should be a VT_FILETIME... */
352     PropVariantClear( &propvar[7] );
353     propspec[7].lpwstr = NULL;
354     propspec[7].ulKind = PRSPEC_PROPID;
355     propspec[7].propid = PID_LASTPRINTED;
356     propvar[7].vt = VT_LPSTR;
357     U(propvar[7]).pszVal = LOSE_CONST("7/1/1999 5:17");
358
359     r = IPropertyStorage_WriteMultiple( ps, 8, propspec, propvar, PID_FIRST_USABLE );
360     ok( r == S_OK, "failed to write properties\n");
361
362     IPropertyStorage_Commit( ps, STGC_DEFAULT );
363
364     IPropertyStorage_Release( ps );
365     IPropertySetStorage_Release( pss );
366
367     IStorage_Commit( stg, STGC_DEFAULT );
368     IStorage_Release( stg );
369 }
370
371 static void test_summary_binary(void)
372 {
373     const char *msifile = "winetest.msi";
374     MSIHANDLE hdb = 0, hsuminfo = 0;
375     UINT r, type, count;
376     INT ival;
377     DWORD sz;
378     char sval[20];
379
380     DeleteFile( msifile );
381
382     test_create_database_binary();
383
384     ok( INVALID_FILE_ATTRIBUTES != GetFileAttributes(msifile), "file doesn't exist!\n");
385
386     /* just MsiOpenDatabase should not create a file */
387     r = MsiOpenDatabase(msifile, MSIDBOPEN_READONLY, &hdb);
388     ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
389
390     r = MsiGetSummaryInformation(hdb, NULL, 0, &hsuminfo);
391     ok(r == ERROR_SUCCESS, "MsiGetSummaryInformation failed\n");
392
393     /*
394      * Check what reading PID_LASTPRINTED does...
395      * The string value is written to the msi file
396      * but it appears that we're not allowed to read it back again.
397      * We can still read its type though...?
398      */
399     sz = sizeof sval;
400     sval[0] = 0;
401     type = 0;
402     r = MsiSummaryInfoGetProperty(hsuminfo, PID_LASTPRINTED, &type, NULL, NULL, sval, &sz);
403     ok(r == ERROR_SUCCESS, "MsiSummaryInfoGetProperty failed\n");
404     ok( !strcmp(sval, ""), "value incorrect\n");
405     todo_wine {
406     ok( type == VT_LPSTR, "type wrong\n");
407     ok( sz == 0, "length wrong\n");
408     }
409
410     r = MsiSummaryInfoGetProperty(hsuminfo, PID_WORDCOUNT, &type, &ival, NULL, NULL, NULL);
411     ok(r == ERROR_SUCCESS, "MsiSummaryInfoGetProperty failed\n");
412     todo_wine ok( ival == 0, "value incorrect\n");
413
414     /* looks like msi adds some of its own values in here */
415     count = 0;
416     r = MsiSummaryInfoGetPropertyCount( hsuminfo, &count );
417     ok(r == ERROR_SUCCESS, "getpropcount failed\n");
418     todo_wine ok(count == 10, "prop count incorrect\n");
419
420     MsiCloseHandle( hsuminfo );
421     MsiCloseHandle( hdb );
422
423     DeleteFile( msifile );
424 }
425
426 START_TEST(suminfo)
427 {
428     test_suminfo();
429     test_summary_binary();
430 }