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" );
43 res = MsiDatabaseCommit( hdb );
44 ok( res == ERROR_SUCCESS , "Failed to commit database" );
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" );
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" );
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" );
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" );
116 res = MsiDatabaseCommit( hdb );
117 ok( res == ERROR_SUCCESS , "Failed to commit database" );
119 res = MsiCloseHandle( hdb );
120 ok( res == ERROR_SUCCESS , "Failed to close database" );
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");
193 r = MsiDatabaseCommit(hdb);
194 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
196 r = MsiCloseHandle(hdb);
197 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
199 r = DeleteFile(msifile);
200 ok(r == TRUE, "file didn't exist after commit\n");
203 typedef UINT (WINAPI *fnMsiDecomposeDescriptorA)(LPCSTR, LPCSTR, LPSTR, LPSTR, DWORD *);
204 fnMsiDecomposeDescriptorA MsiDecomposeDescriptorA;
206 void test_msidecomposedesc(void)
208 char prod[MAX_FEATURE_CHARS+1], comp[MAX_FEATURE_CHARS+1], feature[MAX_FEATURE_CHARS+1];
214 hmod = GetModuleHandle("msi.dll");
217 MsiDecomposeDescriptorA = (fnMsiDecomposeDescriptorA)
218 GetProcAddress(hmod, "MsiDecomposeDescriptorA");
219 if (!MsiDecomposeDescriptorA)
222 /* test a valid feature descriptor */
223 desc = "']gAVn-}f(ZXfeAR6.jiFollowTheWhiteRabbit>3w2x^IGfe?CxI5heAvk.";
225 r = MsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
226 ok(r == ERROR_SUCCESS, "returned an error\n");
227 ok(len == strlen(desc), "length was wrong\n");
228 ok(strcmp(prod,"{90110409-6000-11D3-8CFE-0150048383C9}")==0, "product wrong\n");
229 ok(strcmp(feature,"FollowTheWhiteRabbit")==0, "feature wrong\n");
230 ok(strcmp(comp,"{A7CD68DB-EF74-49C8-FBB2-A7C463B2AC24}")==0,"component wrong\n");
232 /* test an invalid feature descriptor with too many characters */
233 desc = "']gAVn-}f(ZXfeAR6.ji"
234 "ThisWillFailIfTheresMoreThanAGuidsChars>"
235 "3w2x^IGfe?CxI5heAvk.";
237 r = MsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
238 ok(r == ERROR_INVALID_PARAMETER, "returned wrong error\n");
241 * Test a valid feature descriptor with the
242 * maximum number of characters and some trailing characters.
244 desc = "']gAVn-}f(ZXfeAR6.ji"
245 "ThisWillWorkIfTheresLTEThanAGuidsChars>"
246 "3w2x^IGfe?CxI5heAvk."
249 r = MsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
250 ok(r == ERROR_SUCCESS, "returned wrong error\n");
251 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
254 static UINT try_query_param( MSIHANDLE hdb, LPSTR szQuery, MSIHANDLE hrec )
259 res = MsiDatabaseOpenView( hdb, szQuery, &htab );
260 if(res == ERROR_SUCCESS )
264 r = MsiViewExecute( htab, hrec );
265 if(r != ERROR_SUCCESS )
268 r = MsiViewClose( htab );
269 if(r != ERROR_SUCCESS )
272 r = MsiCloseHandle( htab );
273 if(r != ERROR_SUCCESS )
279 static UINT try_query( MSIHANDLE hdb, LPSTR szQuery )
281 return try_query_param( hdb, szQuery, 0 );
284 static UINT try_insert_query( MSIHANDLE hdb, LPSTR szQuery )
289 hrec = MsiCreateRecord( 1 );
290 MsiRecordSetString( hrec, 1, "Hello");
292 r = try_query_param( hdb, szQuery, hrec );
294 MsiCloseHandle( hrec );
298 void test_msibadqueries()
300 const char *msifile = "winetest.msi";
306 /* just MsiOpenDatabase should not create a file */
307 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
308 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
310 r = MsiDatabaseCommit( hdb );
311 ok(r == ERROR_SUCCESS , "Failed to commit database\n");
313 r = MsiCloseHandle( hdb );
314 ok(r == ERROR_SUCCESS , "Failed to close database\n");
316 /* open it readonly */
317 r = MsiOpenDatabase(msifile, MSIDBOPEN_READONLY, &hdb );
318 ok(r == ERROR_SUCCESS , "Failed to open database r/o\n");
320 /* add a table to it */
321 r = try_query( hdb, "select * from _Tables");
322 ok(r == ERROR_SUCCESS , "query 1 failed\n");
324 r = MsiCloseHandle( hdb );
325 ok(r == ERROR_SUCCESS , "Failed to close database r/o\n");
327 /* open it read/write */
328 r = MsiOpenDatabase(msifile, MSIDBOPEN_TRANSACT, &hdb );
329 ok(r == ERROR_SUCCESS , "Failed to open database r/w\n");
331 /* a bunch of test queries that fail with the native MSI */
333 r = try_query( hdb, "CREATE TABLE");
334 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2a return code\n");
336 r = try_query( hdb, "CREATE TABLE `a`");
337 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2b return code\n");
339 r = try_query( hdb, "CREATE TABLE `a` ()");
340 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2c return code\n");
342 r = try_query( hdb, "CREATE TABLE `a` (`b`)");
343 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2d return code\n");
345 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) )");
346 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2e return code\n");
348 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL)");
349 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2f return code\n");
351 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY)");
352 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2g return code\n");
354 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
355 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2h return code\n");
357 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
358 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2i return code\n");
361 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY 'b')");
362 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2j return code\n");
365 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
366 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2k return code\n");
368 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
369 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2l return code\n");
371 r = try_query( hdb, "CREATE TABLE `a` (`b` CHA(72) NOT NULL PRIMARY KEY `b`)");
372 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2m return code\n");
374 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(-1) NOT NULL PRIMARY KEY `b`)");
375 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2n return code\n");
377 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(720) NOT NULL PRIMARY KEY `b`)");
378 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2o return code\n");
380 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL KEY `b`)");
381 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
383 r = try_query( hdb, "CREATE TABLE `a` (`` CHAR(72) NOT NULL PRIMARY KEY `b`)");
384 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
387 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
388 ok(r == ERROR_SUCCESS , "valid query 2z failed\n");
391 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
392 ok(r == ERROR_BAD_QUERY_SYNTAX , "created same table again\n");
394 r = try_query( hdb, "CREATE TABLE `aa` (`b` CHAR(72) NOT NULL, `c` "
395 "CHAR(72), `d` CHAR(255) NOT NULL LOCALIZABLE PRIMARY KEY `b`)");
396 ok(r == ERROR_SUCCESS , "query 4 failed\n");
398 r = MsiDatabaseCommit( hdb );
399 ok(r == ERROR_SUCCESS , "Failed to commit database after write");
401 r = try_query( hdb, "CREATE TABLE `blah` (`foo` CHAR(72) NOT NULL "
402 "PRIMARY KEY `foo`)");
403 ok(r == ERROR_SUCCESS , "query 4 failed\n");
405 r = try_insert_query( hdb, "insert into a ( `b` ) VALUES ( ? )");
406 ok(r == ERROR_SUCCESS , "failed to insert record in db\n");
408 r = MsiDatabaseCommit( hdb );
409 ok(r == ERROR_SUCCESS , "Failed to commit database after write");
411 r = try_query( hdb, "CREATE TABLE `boo` (`foo` CHAR(72) NOT NULL "
412 "PRIMARY KEY `ba`)");
413 ok(r != ERROR_SUCCESS , "query 5 succeeded\n");
415 r = try_query( hdb,"CREATE TABLE `bee` (`foo` CHAR(72) NOT NULL )");
416 ok(r != ERROR_SUCCESS , "query 6 succeeded\n");
418 r = try_query( hdb, "CREATE TABLE `temp` (`t` CHAR(72) NOT NULL "
420 ok(r == ERROR_SUCCESS , "query 7 failed\n");
422 r = try_query( hdb, "CREATE TABLE `c` (`b` CHAR NOT NULL PRIMARY KEY `b`)");
423 ok(r == ERROR_SUCCESS , "query 8 failed\n");
425 r = MsiCloseHandle( hdb );
426 ok(r == ERROR_SUCCESS , "Failed to close database transact\n");
428 r = DeleteFile( msifile );
429 ok(r == TRUE, "file didn't exist after commit\n");
436 test_msidecomposedesc();
437 test_msibadqueries();
438 test_createpackage();