2 * Copyright (C) 2005 Mike McCormack for CodeWeavers
4 * A test program for MSI database files.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/test.h"
28 MSIHANDLE helper_createpackage()
31 CHAR szName[] = "C:\\mytest.msi";
39 /* create an empty database */
40 res = MsiOpenDatabase(szName, MSIDBOPEN_CREATE, &hdb );
41 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
43 res = MsiDatabaseCommit( hdb );
44 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
46 /* build summmary info */
47 res = MsiGetSummaryInformation(hdb, NULL, 7, &suminfo);
48 ok( res == ERROR_SUCCESS , "Failed to open summaryinfo\n" );
50 res = MsiSummaryInfoSetProperty(suminfo,2, VT_LPSTR, 0,NULL,
51 "Installation Database");
52 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
54 res = MsiSummaryInfoSetProperty(suminfo,3, VT_LPSTR, 0,NULL,
55 "Installation Database");
56 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
58 res = MsiSummaryInfoSetProperty(suminfo,4, VT_LPSTR, 0,NULL,
60 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
62 res = MsiSummaryInfoSetProperty(suminfo,7, VT_LPSTR, 0,NULL,
64 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
66 res = MsiSummaryInfoSetProperty(suminfo,9, VT_LPSTR, 0,NULL,
67 "{913B8D18-FBB6-4CAC-A239-C74C11E3FA74}");
68 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
70 res = MsiSummaryInfoSetProperty(suminfo, 14, VT_I4, 100, NULL, NULL);
71 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
73 res = MsiSummaryInfoSetProperty(suminfo, 15, VT_I4, 0, NULL, NULL);
74 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
76 res = MsiSummaryInfoPersist(suminfo);
77 ok( res == ERROR_SUCCESS , "Failed to make summary info persist\n" );
79 res = MsiCloseHandle( suminfo);
80 ok( res == ERROR_SUCCESS , "Failed to close suminfo\n" );
82 sprintf(szPackage,"#%li",(DWORD)hdb);
83 res = MsiOpenPackage(szPackage,&hPackage);
84 ok( res == ERROR_SUCCESS , "Failed to open package\n" );
86 res = MsiCloseHandle( hdb );
87 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
92 static void test_createpackage(void)
94 MSIHANDLE hPackage = 0;
97 hPackage = helper_createpackage();
98 ok ( hPackage != 0, " Failed to create package\n");
100 res = MsiCloseHandle( hPackage);
101 ok( res == ERROR_SUCCESS , "Failed to close package\n" );
104 static void test_msidatabase(void)
107 CHAR szName[] = "C:\\mytest.msi";
112 /* create an empty database */
113 res = MsiOpenDatabase(szName, MSIDBOPEN_CREATE, &hdb );
114 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
116 res = MsiDatabaseCommit( hdb );
117 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
119 res = MsiCloseHandle( hdb );
120 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
122 res = DeleteFile( szName );
123 ok( res == TRUE, "Falled to delete database\n" );
126 void test_msiinsert(void)
128 const char *msifile = "winetest.msi";
129 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
131 char *query, buf[80];
136 /* just MsiOpenDatabase should not create a file */
137 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
138 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
141 query = "CREATE TABLE `phone` ( "
142 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
144 r = MsiDatabaseOpenView(hdb, query, &hview);
145 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
146 r = MsiViewExecute(hview, 0);
147 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
148 r = MsiViewClose(hview);
149 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
150 r = MsiCloseHandle(hview);
151 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
153 /* insert a value into it */
154 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
155 "VALUES('1', 'Abe', '8675309')";
156 r = MsiDatabaseOpenView(hdb, query, &hview);
157 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
158 r = MsiViewExecute(hview, 0);
159 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
160 r = MsiViewClose(hview);
161 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
162 r = MsiCloseHandle(hview);
163 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
165 query = "SELECT * FROM `phone`";
166 r = MsiDatabaseOpenView(hdb, query, &hview);
167 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
168 r = MsiViewExecute(hview, 0);
169 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
170 r = MsiViewFetch(hview, &hrec);
171 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
173 /* check the record contains what we put in it */
174 r = MsiRecordGetFieldCount(hrec);
175 ok(r == 3, "record count wrong\n");
177 r = MsiRecordGetInteger(hrec, 1);
178 ok(r == 1, "field 1 contents wrong\n");
180 r = MsiRecordGetString(hrec, 2, buf, &sz);
181 ok(r == ERROR_SUCCESS, "field 2 content fetch failed\n");
182 ok(!strcmp(buf,"Abe"), "field 2 content incorrect\n");
184 r = MsiRecordGetString(hrec, 3, buf, &sz);
185 ok(r == ERROR_SUCCESS, "field 3 content fetch failed\n");
186 ok(!strcmp(buf,"8675309"), "field 3 content incorrect\n");
188 r = MsiViewClose(hview);
189 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
190 r = MsiCloseHandle(hview);
191 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
192 r = MsiCloseHandle(hrec);
193 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
195 r = MsiDatabaseCommit(hdb);
196 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
198 r = MsiCloseHandle(hdb);
199 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
201 r = DeleteFile(msifile);
202 ok(r == TRUE, "file didn't exist after commit\n");
205 typedef UINT (WINAPI *fnMsiDecomposeDescriptorA)(LPCSTR, LPCSTR, LPSTR, LPSTR, DWORD *);
206 fnMsiDecomposeDescriptorA MsiDecomposeDescriptorA;
208 void test_msidecomposedesc(void)
210 char prod[MAX_FEATURE_CHARS+1], comp[MAX_FEATURE_CHARS+1], feature[MAX_FEATURE_CHARS+1];
216 hmod = GetModuleHandle("msi.dll");
219 MsiDecomposeDescriptorA = (fnMsiDecomposeDescriptorA)
220 GetProcAddress(hmod, "MsiDecomposeDescriptorA");
221 if (!MsiDecomposeDescriptorA)
224 /* test a valid feature descriptor */
225 desc = "']gAVn-}f(ZXfeAR6.jiFollowTheWhiteRabbit>3w2x^IGfe?CxI5heAvk.";
227 r = MsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
228 ok(r == ERROR_SUCCESS, "returned an error\n");
229 ok(len == strlen(desc), "length was wrong\n");
230 ok(strcmp(prod,"{90110409-6000-11D3-8CFE-0150048383C9}")==0, "product wrong\n");
231 ok(strcmp(feature,"FollowTheWhiteRabbit")==0, "feature wrong\n");
232 ok(strcmp(comp,"{A7CD68DB-EF74-49C8-FBB2-A7C463B2AC24}")==0,"component wrong\n");
234 /* test an invalid feature descriptor with too many characters */
235 desc = "']gAVn-}f(ZXfeAR6.ji"
236 "ThisWillFailIfTheresMoreThanAGuidsChars>"
237 "3w2x^IGfe?CxI5heAvk.";
239 r = MsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
240 ok(r == ERROR_INVALID_PARAMETER, "returned wrong error\n");
243 * Test a valid feature descriptor with the
244 * maximum number of characters and some trailing characters.
246 desc = "']gAVn-}f(ZXfeAR6.ji"
247 "ThisWillWorkIfTheresLTEThanAGuidsChars>"
248 "3w2x^IGfe?CxI5heAvk."
251 r = MsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
252 ok(r == ERROR_SUCCESS, "returned wrong error\n");
253 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
256 static UINT try_query_param( MSIHANDLE hdb, LPSTR szQuery, MSIHANDLE hrec )
261 res = MsiDatabaseOpenView( hdb, szQuery, &htab );
262 if(res == ERROR_SUCCESS )
266 r = MsiViewExecute( htab, hrec );
267 if(r != ERROR_SUCCESS )
270 r = MsiViewClose( htab );
271 if(r != ERROR_SUCCESS )
274 r = MsiCloseHandle( htab );
275 if(r != ERROR_SUCCESS )
281 static UINT try_query( MSIHANDLE hdb, LPSTR szQuery )
283 return try_query_param( hdb, szQuery, 0 );
286 static UINT try_insert_query( MSIHANDLE hdb, LPSTR szQuery )
291 hrec = MsiCreateRecord( 1 );
292 MsiRecordSetString( hrec, 1, "Hello");
294 r = try_query_param( hdb, szQuery, hrec );
296 MsiCloseHandle( hrec );
300 void test_msibadqueries()
302 const char *msifile = "winetest.msi";
308 /* just MsiOpenDatabase should not create a file */
309 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
310 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
312 r = MsiDatabaseCommit( hdb );
313 ok(r == ERROR_SUCCESS , "Failed to commit database\n");
315 r = MsiCloseHandle( hdb );
316 ok(r == ERROR_SUCCESS , "Failed to close database\n");
318 /* open it readonly */
319 r = MsiOpenDatabase(msifile, MSIDBOPEN_READONLY, &hdb );
320 ok(r == ERROR_SUCCESS , "Failed to open database r/o\n");
322 /* add a table to it */
323 r = try_query( hdb, "select * from _Tables");
324 ok(r == ERROR_SUCCESS , "query 1 failed\n");
326 r = MsiCloseHandle( hdb );
327 ok(r == ERROR_SUCCESS , "Failed to close database r/o\n");
329 /* open it read/write */
330 r = MsiOpenDatabase(msifile, MSIDBOPEN_TRANSACT, &hdb );
331 ok(r == ERROR_SUCCESS , "Failed to open database r/w\n");
333 /* a bunch of test queries that fail with the native MSI */
335 r = try_query( hdb, "CREATE TABLE");
336 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2a return code\n");
338 r = try_query( hdb, "CREATE TABLE `a`");
339 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2b return code\n");
341 r = try_query( hdb, "CREATE TABLE `a` ()");
342 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2c return code\n");
344 r = try_query( hdb, "CREATE TABLE `a` (`b`)");
345 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2d return code\n");
347 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) )");
348 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2e return code\n");
350 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL)");
351 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2f return code\n");
353 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY)");
354 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2g return code\n");
356 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
357 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2h return code\n");
359 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
360 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2i return code\n");
363 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY 'b')");
364 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2j return code\n");
367 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
368 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2k return code\n");
370 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
371 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2l return code\n");
373 r = try_query( hdb, "CREATE TABLE `a` (`b` CHA(72) NOT NULL PRIMARY KEY `b`)");
374 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2m return code\n");
376 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(-1) NOT NULL PRIMARY KEY `b`)");
377 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2n return code\n");
379 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(720) NOT NULL PRIMARY KEY `b`)");
380 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2o return code\n");
382 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL KEY `b`)");
383 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
385 r = try_query( hdb, "CREATE TABLE `a` (`` CHAR(72) NOT NULL PRIMARY KEY `b`)");
386 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
389 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
390 ok(r == ERROR_SUCCESS , "valid query 2z failed\n");
393 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
394 ok(r == ERROR_BAD_QUERY_SYNTAX , "created same table again\n");
396 r = try_query( hdb, "CREATE TABLE `aa` (`b` CHAR(72) NOT NULL, `c` "
397 "CHAR(72), `d` CHAR(255) NOT NULL LOCALIZABLE PRIMARY KEY `b`)");
398 ok(r == ERROR_SUCCESS , "query 4 failed\n");
400 r = MsiDatabaseCommit( hdb );
401 ok(r == ERROR_SUCCESS , "Failed to commit database after write\n");
403 r = try_query( hdb, "CREATE TABLE `blah` (`foo` CHAR(72) NOT NULL "
404 "PRIMARY KEY `foo`)");
405 ok(r == ERROR_SUCCESS , "query 4 failed\n");
407 r = try_insert_query( hdb, "insert into a ( `b` ) VALUES ( ? )");
408 ok(r == ERROR_SUCCESS , "failed to insert record in db\n");
410 r = MsiDatabaseCommit( hdb );
411 ok(r == ERROR_SUCCESS , "Failed to commit database after write\n");
413 r = try_query( hdb, "CREATE TABLE `boo` (`foo` CHAR(72) NOT NULL "
414 "PRIMARY KEY `ba`)");
415 ok(r != ERROR_SUCCESS , "query 5 succeeded\n");
417 r = try_query( hdb,"CREATE TABLE `bee` (`foo` CHAR(72) NOT NULL )");
418 ok(r != ERROR_SUCCESS , "query 6 succeeded\n");
420 r = try_query( hdb, "CREATE TABLE `temp` (`t` CHAR(72) NOT NULL "
422 ok(r == ERROR_SUCCESS , "query 7 failed\n");
424 r = try_query( hdb, "CREATE TABLE `c` (`b` CHAR NOT NULL PRIMARY KEY `b`)");
425 ok(r == ERROR_SUCCESS , "query 8 failed\n");
427 r = MsiCloseHandle( hdb );
428 ok(r == ERROR_SUCCESS , "Failed to close database transact\n");
430 r = DeleteFile( msifile );
431 ok(r == TRUE, "file didn't exist after commit\n");
438 test_msidecomposedesc();
439 test_msibadqueries();
440 test_createpackage();