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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/test.h"
28 static const char *msifile = "winetest.msi";
29 static const char *msifile2 = "winetst2.msi";
30 static const char *mstfile = "winetst.mst";
32 #ifndef ERROR_INSTALL_TRANSFORM_FAILURE
33 #define ERROR_INSTALL_TRANSFORM_FAILURE 1624
36 static void test_msidatabase(void)
43 /* create an empty database */
44 res = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb );
45 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
47 res = MsiDatabaseCommit( hdb );
48 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
50 res = MsiCloseHandle( hdb );
51 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
53 res = DeleteFile( msifile );
54 ok( res == TRUE, "Failed to delete database\n" );
57 static UINT do_query(MSIHANDLE hdb, const char *query, MSIHANDLE *phrec)
62 /* open a select query */
63 r = MsiDatabaseOpenView(hdb, query, &hview);
64 if (r != ERROR_SUCCESS)
66 r = MsiViewExecute(hview, 0);
67 if (r != ERROR_SUCCESS)
69 ret = MsiViewFetch(hview, phrec);
70 r = MsiViewClose(hview);
71 if (r != ERROR_SUCCESS)
73 r = MsiCloseHandle(hview);
74 if (r != ERROR_SUCCESS)
79 static void test_msiinsert(void)
81 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
89 /* just MsiOpenDatabase should not create a file */
90 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
91 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
94 query = "CREATE TABLE `phone` ( "
95 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
97 r = MsiDatabaseOpenView(hdb, query, &hview);
98 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
99 r = MsiViewExecute(hview, 0);
100 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
101 r = MsiViewClose(hview);
102 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
103 r = MsiCloseHandle(hview);
104 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
106 /* insert a value into it */
107 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
108 "VALUES('1', 'Abe', '8675309')";
109 r = MsiDatabaseOpenView(hdb, query, &hview);
110 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
111 r = MsiViewExecute(hview, 0);
112 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
113 r = MsiViewClose(hview);
114 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
115 r = MsiCloseHandle(hview);
116 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
118 query = "SELECT * FROM `phone` WHERE `id` = 1";
119 r = do_query(hdb, query, &hrec);
120 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
122 /* check the record contains what we put in it */
123 r = MsiRecordGetFieldCount(hrec);
124 ok(r == 3, "record count wrong\n");
127 r = MsiRecordIsNull(hrec, 0);
128 ok(r == FALSE, "field 0 not null\n");
131 r = MsiRecordGetInteger(hrec, 1);
132 ok(r == 1, "field 1 contents wrong\n");
134 r = MsiRecordGetString(hrec, 2, buf, &sz);
135 ok(r == ERROR_SUCCESS, "field 2 content fetch failed\n");
136 ok(!strcmp(buf,"Abe"), "field 2 content incorrect\n");
138 r = MsiRecordGetString(hrec, 3, buf, &sz);
139 ok(r == ERROR_SUCCESS, "field 3 content fetch failed\n");
140 ok(!strcmp(buf,"8675309"), "field 3 content incorrect\n");
142 r = MsiCloseHandle(hrec);
143 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
145 /* open a select query */
147 query = "SELECT * FROM `phone` WHERE `id` >= 10";
148 r = do_query(hdb, query, &hrec);
149 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
150 ok(hrec == 0, "hrec should be null\n");
152 r = MsiCloseHandle(hrec);
153 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
155 query = "SELECT * FROM `phone` WHERE `id` < 0";
156 r = do_query(hdb, query, &hrec);
157 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
159 query = "SELECT * FROM `phone` WHERE `id` <= 0";
160 r = do_query(hdb, query, &hrec);
161 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
163 query = "SELECT * FROM `phone` WHERE `id` <> 1";
164 r = do_query(hdb, query, &hrec);
165 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
167 query = "SELECT * FROM `phone` WHERE `id` > 10";
168 r = do_query(hdb, query, &hrec);
169 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
171 /* now try a few bad INSERT xqueries */
172 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
174 r = MsiDatabaseOpenView(hdb, query, &hview);
175 ok(r == ERROR_BAD_QUERY_SYNTAX, "MsiDatabaseOpenView failed\n");
177 /* construct a record to insert */
178 hrec = MsiCreateRecord(4);
179 r = MsiRecordSetInteger(hrec, 1, 2);
180 ok(r == ERROR_SUCCESS, "MsiRecordSetInteger failed\n");
181 r = MsiRecordSetString(hrec, 2, "Adam");
182 ok(r == ERROR_SUCCESS, "MsiRecordSetString failed\n");
183 r = MsiRecordSetString(hrec, 3, "96905305");
184 ok(r == ERROR_SUCCESS, "MsiRecordSetString failed\n");
186 /* insert another value, using a record and wildcards */
187 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
189 r = MsiDatabaseOpenView(hdb, query, &hview);
190 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
192 if (r == ERROR_SUCCESS)
194 r = MsiViewExecute(hview, hrec);
195 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
196 r = MsiViewClose(hview);
197 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
198 r = MsiCloseHandle(hview);
199 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
201 r = MsiCloseHandle(hrec);
202 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
204 r = MsiViewFetch(0, NULL);
205 ok(r == ERROR_INVALID_PARAMETER, "MsiViewFetch failed\n");
207 r = MsiDatabaseCommit(hdb);
208 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
210 r = MsiCloseHandle(hdb);
211 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
213 r = DeleteFile(msifile);
214 ok(r == TRUE, "file didn't exist after commit\n");
217 typedef UINT (WINAPI *fnMsiDecomposeDescriptorA)(LPCSTR, LPCSTR, LPSTR, LPSTR, DWORD *);
218 static fnMsiDecomposeDescriptorA pMsiDecomposeDescriptorA;
220 static void test_msidecomposedesc(void)
222 char prod[MAX_FEATURE_CHARS+1], comp[MAX_FEATURE_CHARS+1], feature[MAX_FEATURE_CHARS+1];
228 hmod = GetModuleHandle("msi.dll");
231 pMsiDecomposeDescriptorA = (fnMsiDecomposeDescriptorA)
232 GetProcAddress(hmod, "MsiDecomposeDescriptorA");
233 if (!pMsiDecomposeDescriptorA)
236 /* test a valid feature descriptor */
237 desc = "']gAVn-}f(ZXfeAR6.jiFollowTheWhiteRabbit>3w2x^IGfe?CxI5heAvk.";
239 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
240 ok(r == ERROR_SUCCESS, "returned an error\n");
241 ok(len == strlen(desc), "length was wrong\n");
242 ok(strcmp(prod,"{90110409-6000-11D3-8CFE-0150048383C9}")==0, "product wrong\n");
243 ok(strcmp(feature,"FollowTheWhiteRabbit")==0, "feature wrong\n");
244 ok(strcmp(comp,"{A7CD68DB-EF74-49C8-FBB2-A7C463B2AC24}")==0,"component wrong\n");
246 /* test an invalid feature descriptor with too many characters */
247 desc = "']gAVn-}f(ZXfeAR6.ji"
248 "ThisWillFailIfTheresMoreThanAGuidsChars>"
249 "3w2x^IGfe?CxI5heAvk.";
251 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
252 ok(r == ERROR_INVALID_PARAMETER, "returned wrong error\n");
255 * Test a valid feature descriptor with the
256 * maximum number of characters and some trailing characters.
258 desc = "']gAVn-}f(ZXfeAR6.ji"
259 "ThisWillWorkIfTheresLTEThanAGuidsChars>"
260 "3w2x^IGfe?CxI5heAvk."
263 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
264 ok(r == ERROR_SUCCESS, "returned wrong error\n");
265 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
268 r = pMsiDecomposeDescriptorA(desc, prod, feature, NULL, &len);
269 ok(r == ERROR_SUCCESS, "returned wrong error\n");
270 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
273 r = pMsiDecomposeDescriptorA(desc, prod, NULL, NULL, &len);
274 ok(r == ERROR_SUCCESS, "returned wrong error\n");
275 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
278 r = pMsiDecomposeDescriptorA(desc, NULL, NULL, NULL, &len);
279 ok(r == ERROR_SUCCESS, "returned wrong error\n");
280 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
283 r = pMsiDecomposeDescriptorA(NULL, NULL, NULL, NULL, &len);
284 ok(r == ERROR_INVALID_PARAMETER, "returned wrong error\n");
285 ok(len == 0, "length wrong\n");
288 static UINT try_query_param( MSIHANDLE hdb, LPCSTR szQuery, MSIHANDLE hrec )
293 res = MsiDatabaseOpenView( hdb, szQuery, &htab );
294 if(res == ERROR_SUCCESS )
298 r = MsiViewExecute( htab, hrec );
299 if(r != ERROR_SUCCESS )
302 r = MsiViewClose( htab );
303 if(r != ERROR_SUCCESS )
306 r = MsiCloseHandle( htab );
307 if(r != ERROR_SUCCESS )
313 static UINT try_query( MSIHANDLE hdb, LPCSTR szQuery )
315 return try_query_param( hdb, szQuery, 0 );
318 static UINT try_insert_query( MSIHANDLE hdb, LPCSTR szQuery )
323 hrec = MsiCreateRecord( 1 );
324 MsiRecordSetString( hrec, 1, "Hello");
326 r = try_query_param( hdb, szQuery, hrec );
328 MsiCloseHandle( hrec );
332 static void test_msibadqueries(void)
339 /* just MsiOpenDatabase should not create a file */
340 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
341 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
343 r = MsiDatabaseCommit( hdb );
344 ok(r == ERROR_SUCCESS , "Failed to commit database\n");
346 r = MsiCloseHandle( hdb );
347 ok(r == ERROR_SUCCESS , "Failed to close database\n");
349 /* open it readonly */
350 r = MsiOpenDatabase(msifile, MSIDBOPEN_READONLY, &hdb );
351 ok(r == ERROR_SUCCESS , "Failed to open database r/o\n");
353 /* add a table to it */
354 r = try_query( hdb, "select * from _Tables");
355 ok(r == ERROR_SUCCESS , "query 1 failed\n");
357 r = MsiCloseHandle( hdb );
358 ok(r == ERROR_SUCCESS , "Failed to close database r/o\n");
360 /* open it read/write */
361 r = MsiOpenDatabase(msifile, MSIDBOPEN_TRANSACT, &hdb );
362 ok(r == ERROR_SUCCESS , "Failed to open database r/w\n");
364 /* a bunch of test queries that fail with the native MSI */
366 r = try_query( hdb, "CREATE TABLE");
367 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2a return code\n");
369 r = try_query( hdb, "CREATE TABLE `a`");
370 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2b return code\n");
372 r = try_query( hdb, "CREATE TABLE `a` ()");
373 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2c return code\n");
375 r = try_query( hdb, "CREATE TABLE `a` (`b`)");
376 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2d return code\n");
378 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) )");
379 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2e return code\n");
381 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL)");
382 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2f return code\n");
384 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY)");
385 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2g return code\n");
387 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
388 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2h return code\n");
390 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
391 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2i return code\n");
393 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY 'b')");
394 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2j return code\n");
396 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
397 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2k return code\n");
399 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
400 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2l return code\n");
402 r = try_query( hdb, "CREATE TABLE `a` (`b` CHA(72) NOT NULL PRIMARY KEY `b`)");
403 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2m return code\n");
405 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(-1) NOT NULL PRIMARY KEY `b`)");
406 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2n return code\n");
408 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(720) NOT NULL PRIMARY KEY `b`)");
409 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2o return code\n");
411 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL KEY `b`)");
412 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
414 r = try_query( hdb, "CREATE TABLE `a` (`` CHAR(72) NOT NULL PRIMARY KEY `b`)");
415 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
417 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
418 ok(r == ERROR_SUCCESS , "valid query 2z failed\n");
420 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
421 ok(r == ERROR_BAD_QUERY_SYNTAX , "created same table again\n");
423 r = try_query( hdb, "CREATE TABLE `aa` (`b` CHAR(72) NOT NULL, `c` "
424 "CHAR(72), `d` CHAR(255) NOT NULL LOCALIZABLE PRIMARY KEY `b`)");
425 ok(r == ERROR_SUCCESS , "query 4 failed\n");
427 r = MsiDatabaseCommit( hdb );
428 ok(r == ERROR_SUCCESS , "Failed to commit database after write\n");
430 r = try_query( hdb, "CREATE TABLE `blah` (`foo` CHAR(72) NOT NULL "
431 "PRIMARY KEY `foo`)");
432 ok(r == ERROR_SUCCESS , "query 4 failed\n");
434 r = try_insert_query( hdb, "insert into a ( `b` ) VALUES ( ? )");
435 ok(r == ERROR_SUCCESS , "failed to insert record in db\n");
437 r = MsiDatabaseCommit( hdb );
438 ok(r == ERROR_SUCCESS , "Failed to commit database after write\n");
440 r = try_query( hdb, "CREATE TABLE `boo` (`foo` CHAR(72) NOT NULL "
441 "PRIMARY KEY `ba`)");
442 ok(r != ERROR_SUCCESS , "query 5 succeeded\n");
444 r = try_query( hdb,"CREATE TABLE `bee` (`foo` CHAR(72) NOT NULL )");
445 ok(r != ERROR_SUCCESS , "query 6 succeeded\n");
447 r = try_query( hdb, "CREATE TABLE `temp` (`t` CHAR(72) NOT NULL "
449 ok(r == ERROR_SUCCESS , "query 7 failed\n");
451 r = try_query( hdb, "CREATE TABLE `c` (`b` CHAR NOT NULL PRIMARY KEY `b`)");
452 ok(r == ERROR_SUCCESS , "query 8 failed\n");
454 r = MsiCloseHandle( hdb );
455 ok(r == ERROR_SUCCESS , "Failed to close database transact\n");
457 r = DeleteFile( msifile );
458 ok(r == TRUE, "file didn't exist after commit\n");
461 static UINT run_query( MSIHANDLE hdb, MSIHANDLE hrec, const char *query )
466 r = MsiDatabaseOpenView(hdb, query, &hview);
467 if( r != ERROR_SUCCESS )
470 r = MsiViewExecute(hview, hrec);
471 if( r == ERROR_SUCCESS )
472 r = MsiViewClose(hview);
473 MsiCloseHandle(hview);
477 static void test_viewmodify(void)
479 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
487 /* just MsiOpenDatabase should not create a file */
488 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
489 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
491 query = "CREATE TABLE `phone` ( "
492 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
494 r = run_query( hdb, 0, query );
495 ok(r == ERROR_SUCCESS, "query failed\n");
497 /* check what the error function reports without doing anything */
499 /* passing NULL as the 3rd param make function to crash on older platforms */
500 r = MsiViewGetError( 0, NULL, &sz );
501 ok(r == MSIDBERROR_INVALIDARG, "MsiViewGetError return\n");
504 query = "SELECT * FROM `phone`";
505 r = MsiDatabaseOpenView(hdb, query, &hview);
506 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
508 /* see what happens with a good hview and bad args */
509 r = MsiViewGetError( hview, NULL, NULL );
510 ok(r == MSIDBERROR_INVALIDARG || r == MSIDBERROR_NOERROR,
511 "MsiViewGetError returns %u (expected -3)\n", r);
512 r = MsiViewGetError( hview, buffer, NULL );
513 ok(r == MSIDBERROR_INVALIDARG, "MsiViewGetError return\n");
515 /* see what happens with a zero length buffer */
518 r = MsiViewGetError( hview, buffer, &sz );
519 ok(r == MSIDBERROR_MOREDATA, "MsiViewGetError return\n");
520 ok(buffer[0] == 'x', "buffer cleared\n");
521 ok(sz == 0, "size not zero\n");
523 /* ok this one is strange */
525 r = MsiViewGetError( hview, NULL, &sz );
526 ok(r == MSIDBERROR_NOERROR, "MsiViewGetError return\n");
527 ok(sz == 0, "size not zero\n");
529 /* see if it really has an error */
532 r = MsiViewGetError( hview, buffer, &sz );
533 ok(r == MSIDBERROR_NOERROR, "MsiViewGetError return\n");
534 ok(buffer[0] == 0, "buffer not cleared\n");
535 ok(sz == 0, "size not zero\n");
537 r = MsiViewExecute(hview, 0);
538 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
540 /* try some invalid records */
541 r = MsiViewModify(hview, MSIMODIFY_INSERT, 0 );
542 ok(r == ERROR_INVALID_HANDLE, "MsiViewModify failed\n");
543 r = MsiViewModify(hview, -1, 0 );
544 ok(r == ERROR_INVALID_HANDLE, "MsiViewModify failed\n");
546 /* try an small record */
547 hrec = MsiCreateRecord(1);
548 r = MsiViewModify(hview, -1, hrec );
549 ok(r == ERROR_INVALID_DATA, "MsiViewModify failed\n");
551 r = MsiCloseHandle(hrec);
552 ok(r == ERROR_SUCCESS, "failed to close record\n");
554 /* insert a valid record */
555 hrec = MsiCreateRecord(3);
557 r = MsiRecordSetInteger(hrec, 2, 1);
558 ok(r == ERROR_SUCCESS, "failed to set integer\n");
559 r = MsiRecordSetString(hrec, 2, "bob");
560 ok(r == ERROR_SUCCESS, "failed to set integer\n");
561 r = MsiRecordSetString(hrec, 3, "7654321");
562 ok(r == ERROR_SUCCESS, "failed to set integer\n");
564 r = MsiViewExecute(hview, 0);
565 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
566 r = MsiViewModify(hview, MSIMODIFY_INSERT_TEMPORARY, hrec );
567 ok(r == ERROR_SUCCESS, "MsiViewModify failed\n");
569 /* insert the same thing again */
570 r = MsiViewExecute(hview, 0);
571 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
573 /* should fail ... */
575 r = MsiViewModify(hview, MSIMODIFY_INSERT_TEMPORARY, hrec );
576 ok(r == ERROR_FUNCTION_FAILED, "MsiViewModify failed\n");
579 r = MsiCloseHandle(hrec);
580 ok(r == ERROR_SUCCESS, "failed to close record\n");
582 r = MsiViewClose(hview);
583 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
584 r = MsiCloseHandle(hview);
585 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
587 r = MsiCloseHandle( hdb );
588 ok(r == ERROR_SUCCESS, "MsiOpenDatabase close failed\n");
591 static MSIHANDLE create_db(void)
598 /* create an empty database */
599 res = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb );
600 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
601 if( res != ERROR_SUCCESS )
604 res = MsiDatabaseCommit( hdb );
605 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
610 static void test_getcolinfo(void)
612 MSIHANDLE hdb, hview = 0, rec = 0;
617 /* create an empty db */
619 ok( hdb, "failed to create db\n");
621 /* tables should be present */
622 r = MsiDatabaseOpenView(hdb, "select * from _Tables", &hview);
623 ok( r == ERROR_SUCCESS, "failed to open query\n");
625 r = MsiViewExecute(hview, 0);
626 ok( r == ERROR_SUCCESS, "failed to execute query\n");
628 /* check that NAMES works */
630 r = MsiViewGetColumnInfo( hview, MSICOLINFO_NAMES, &rec );
631 ok( r == ERROR_SUCCESS, "failed to get names\n");
633 r = MsiRecordGetString(rec, 1, buffer, &sz );
634 ok( r == ERROR_SUCCESS, "failed to get string\n");
635 ok( !strcmp(buffer,"Name"), "_Tables has wrong column name\n");
636 r = MsiCloseHandle( rec );
637 ok( r == ERROR_SUCCESS, "failed to close record handle\n");
639 /* check that TYPES works */
641 r = MsiViewGetColumnInfo( hview, MSICOLINFO_TYPES, &rec );
642 ok( r == ERROR_SUCCESS, "failed to get names\n");
644 r = MsiRecordGetString(rec, 1, buffer, &sz );
645 ok( r == ERROR_SUCCESS, "failed to get string\n");
646 ok( !strcmp(buffer,"s64"), "_Tables has wrong column type\n");
647 r = MsiCloseHandle( rec );
648 ok( r == ERROR_SUCCESS, "failed to close record handle\n");
650 /* check that invalid values fail */
652 r = MsiViewGetColumnInfo( hview, 100, &rec );
653 ok( r == ERROR_INVALID_PARAMETER, "wrong error code\n");
654 ok( rec == 0, "returned a record\n");
656 r = MsiViewGetColumnInfo( hview, MSICOLINFO_TYPES, NULL );
657 ok( r == ERROR_INVALID_PARAMETER, "wrong error code\n");
659 r = MsiViewGetColumnInfo( 0, MSICOLINFO_TYPES, &rec );
660 ok( r == ERROR_INVALID_HANDLE, "wrong error code\n");
662 r = MsiViewClose(hview);
663 ok( r == ERROR_SUCCESS, "failed to close view\n");
664 r = MsiCloseHandle(hview);
665 ok( r == ERROR_SUCCESS, "failed to close view handle\n");
666 r = MsiCloseHandle(hdb);
667 ok( r == ERROR_SUCCESS, "failed to close database\n");
670 static MSIHANDLE get_column_info(MSIHANDLE hdb, const char *query, MSICOLINFO type)
672 MSIHANDLE hview = 0, rec = 0;
675 r = MsiDatabaseOpenView(hdb, query, &hview);
676 if( r != ERROR_SUCCESS )
679 r = MsiViewExecute(hview, 0);
680 if( r == ERROR_SUCCESS )
682 MsiViewGetColumnInfo( hview, type, &rec );
685 MsiCloseHandle(hview);
689 static UINT get_columns_table_type(MSIHANDLE hdb, const char *table, UINT field)
691 MSIHANDLE hview = 0, rec = 0;
695 sprintf(query, "select * from `_Columns` where `Table` = '%s'", table );
697 r = MsiDatabaseOpenView(hdb, query, &hview);
698 if( r != ERROR_SUCCESS )
701 r = MsiViewExecute(hview, 0);
702 if( r == ERROR_SUCCESS )
706 r = MsiViewFetch( hview, &rec );
707 if( r != ERROR_SUCCESS)
709 r = MsiRecordGetInteger( rec, 2 );
711 type = MsiRecordGetInteger( rec, 4 );
712 MsiCloseHandle( rec );
717 MsiCloseHandle(hview);
721 static BOOL check_record( MSIHANDLE rec, UINT field, LPCSTR val )
728 r = MsiRecordGetString( rec, field, buffer, &sz );
729 return (r == ERROR_SUCCESS ) && !strcmp(val, buffer);
732 static void test_viewgetcolumninfo(void)
734 MSIHANDLE hdb = 0, rec;
738 ok( hdb, "failed to create db\n");
740 r = run_query( hdb, 0,
741 "CREATE TABLE `Properties` "
742 "( `Property` CHAR(255), `Value` CHAR(1) PRIMARY KEY `Property`)" );
743 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
745 /* check the column types */
746 rec = get_column_info( hdb, "select * from `Properties`", MSICOLINFO_TYPES );
747 ok( rec, "failed to get column info record\n" );
749 ok( check_record( rec, 1, "S255"), "wrong record type\n");
750 ok( check_record( rec, 2, "S1"), "wrong record type\n");
752 MsiCloseHandle( rec );
754 /* check the type in _Columns */
755 ok( 0x3dff == get_columns_table_type(hdb, "Properties", 1 ), "_columns table wrong\n");
756 ok( 0x1d01 == get_columns_table_type(hdb, "Properties", 2 ), "_columns table wrong\n");
758 /* now try the names */
759 rec = get_column_info( hdb, "select * from `Properties`", MSICOLINFO_NAMES );
760 ok( rec, "failed to get column info record\n" );
762 ok( check_record( rec, 1, "Property"), "wrong record type\n");
763 ok( check_record( rec, 2, "Value"), "wrong record type\n");
765 MsiCloseHandle( rec );
767 r = run_query( hdb, 0,
768 "CREATE TABLE `Binary` "
769 "( `Name` CHAR(255), `Data` OBJECT PRIMARY KEY `Name`)" );
770 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
772 /* check the column types */
773 rec = get_column_info( hdb, "select * from `Binary`", MSICOLINFO_TYPES );
774 ok( rec, "failed to get column info record\n" );
776 ok( check_record( rec, 1, "S255"), "wrong record type\n");
777 ok( check_record( rec, 2, "V0"), "wrong record type\n");
779 MsiCloseHandle( rec );
781 /* check the type in _Columns */
782 ok( 0x3dff == get_columns_table_type(hdb, "Binary", 1 ), "_columns table wrong\n");
783 ok( 0x1900 == get_columns_table_type(hdb, "Binary", 2 ), "_columns table wrong\n");
785 /* now try the names */
786 rec = get_column_info( hdb, "select * from `Binary`", MSICOLINFO_NAMES );
787 ok( rec, "failed to get column info record\n" );
789 ok( check_record( rec, 1, "Name"), "wrong record type\n");
790 ok( check_record( rec, 2, "Data"), "wrong record type\n");
791 MsiCloseHandle( rec );
793 r = run_query( hdb, 0,
794 "CREATE TABLE `UIText` "
795 "( `Key` CHAR(72) NOT NULL, `Text` CHAR(255) LOCALIZABLE PRIMARY KEY `Key`)" );
796 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
798 ok( 0x2d48 == get_columns_table_type(hdb, "UIText", 1 ), "_columns table wrong\n");
799 ok( 0x1fff == get_columns_table_type(hdb, "UIText", 2 ), "_columns table wrong\n");
801 rec = get_column_info( hdb, "select * from `UIText`", MSICOLINFO_NAMES );
802 ok( rec, "failed to get column info record\n" );
803 ok( check_record( rec, 1, "Key"), "wrong record type\n");
804 ok( check_record( rec, 2, "Text"), "wrong record type\n");
805 MsiCloseHandle( rec );
807 rec = get_column_info( hdb, "select * from `UIText`", MSICOLINFO_TYPES );
808 ok( rec, "failed to get column info record\n" );
809 ok( check_record( rec, 1, "s72"), "wrong record type\n");
810 ok( check_record( rec, 2, "L255"), "wrong record type\n");
811 MsiCloseHandle( rec );
813 MsiCloseHandle( hdb );
816 static void test_msiexport(void)
818 MSIHANDLE hdb = 0, hview = 0;
822 const char file[] = "phone.txt";
826 const char expected[] =
827 "id\tname\tnumber\r\n"
830 "1\tAbe\t8675309\r\n";
834 /* just MsiOpenDatabase should not create a file */
835 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
836 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
839 query = "CREATE TABLE `phone` ( "
840 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
842 r = MsiDatabaseOpenView(hdb, query, &hview);
843 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
844 r = MsiViewExecute(hview, 0);
845 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
846 r = MsiViewClose(hview);
847 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
848 r = MsiCloseHandle(hview);
849 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
851 /* insert a value into it */
852 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
853 "VALUES('1', 'Abe', '8675309')";
854 r = MsiDatabaseOpenView(hdb, query, &hview);
855 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
856 r = MsiViewExecute(hview, 0);
857 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
858 r = MsiViewClose(hview);
859 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
860 r = MsiCloseHandle(hview);
861 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
863 GetCurrentDirectory(MAX_PATH, path);
865 r = MsiDatabaseExport(hdb, "phone", path, file);
866 ok(r == ERROR_SUCCESS, "MsiDatabaseExport failed\n");
873 /* check the data that was written */
875 memset(buffer, 0, sizeof buffer);
876 handle = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
877 if (handle != INVALID_HANDLE_VALUE)
879 ReadFile(handle, buffer, sizeof buffer, &length, NULL);
884 ok(0, "failed to open file %s\n", path);
886 ok( length == strlen(expected), "length of data wrong\n");
887 ok( !lstrcmp(buffer, expected), "data doesn't match\n");
891 static void test_longstrings(void)
893 const char insert_query[] =
894 "INSERT INTO `strings` ( `id`, `val` ) VALUES('1', 'Z')";
896 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
899 const DWORD STRING_LENGTH = 0x10005;
902 /* just MsiOpenDatabase should not create a file */
903 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
904 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
908 "CREATE TABLE `strings` ( `id` INT, `val` CHAR(0) PRIMARY KEY `id`)");
909 ok(r == ERROR_SUCCESS, "query failed\n");
911 /* try a insert a very long string */
912 str = HeapAlloc(GetProcessHeap(), 0, STRING_LENGTH+sizeof insert_query);
913 len = strchr(insert_query, 'Z') - insert_query;
914 strcpy(str, insert_query);
915 memset(str+len, 'Z', STRING_LENGTH);
916 strcpy(str+len+STRING_LENGTH, insert_query+len+1);
917 r = try_query( hdb, str );
918 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
920 HeapFree(GetProcessHeap(), 0, str);
922 MsiDatabaseCommit(hdb);
923 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
926 r = MsiOpenDatabase(msifile, MSIDBOPEN_READONLY, &hdb);
927 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
929 r = MsiDatabaseOpenView(hdb, "select * from `strings` where `id` = 1", &hview);
930 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
932 r = MsiViewExecute(hview, 0);
933 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
935 r = MsiViewFetch(hview, &hrec);
936 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
938 MsiCloseHandle(hview);
940 r = MsiRecordGetString(hrec, 2, NULL, &len);
941 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
942 ok(len == STRING_LENGTH, "string length wrong\n");
944 MsiCloseHandle(hrec);
949 static void create_file(const CHAR *name)
954 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
955 ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
956 WriteFile(file, name, strlen(name), &written, NULL);
957 WriteFile(file, "\n", strlen("\n"), &written, NULL);
961 static void test_streamtable(void)
963 MSIHANDLE hdb = 0, rec, view;
970 ok( hdb, "failed to create db\n");
972 r = run_query( hdb, 0,
973 "CREATE TABLE `Properties` "
974 "( `Property` CHAR(255), `Value` CHAR(1) PRIMARY KEY `Property`)" );
975 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
977 /* check the column types */
978 rec = get_column_info( hdb, "select * from `_Streams`", MSICOLINFO_TYPES );
979 ok( rec, "failed to get column info record\n" );
982 ok( check_record( rec, 1, "s62"), "wrong record type\n");
983 ok( check_record( rec, 2, "V0"), "wrong record type\n");
986 MsiCloseHandle( rec );
988 /* now try the names */
989 rec = get_column_info( hdb, "select * from `_Streams`", MSICOLINFO_NAMES );
990 ok( rec, "failed to get column info record\n" );
993 ok( check_record( rec, 1, "Name"), "wrong record type\n");
994 ok( check_record( rec, 2, "Data"), "wrong record type\n");
997 MsiCloseHandle( rec );
999 /* insert a file into the _Streams table */
1000 create_file( "test.txt" );
1002 rec = MsiCreateRecord( 2 );
1003 MsiRecordSetString( rec, 1, "data" );
1005 r = MsiRecordSetStream( rec, 2, "test.txt" );
1006 ok( r == ERROR_SUCCESS, "Failed to add stream data to the record: %d\n", r);
1008 DeleteFile("test.txt");
1010 r = MsiDatabaseOpenView( hdb,
1011 "INSERT INTO `_Streams` ( `Name`, `Data` ) VALUES ( ?, ? )", &view );
1014 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1017 r = MsiViewExecute( view, rec );
1020 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1023 MsiCloseHandle( rec );
1024 MsiCloseHandle( view );
1026 r = MsiDatabaseOpenView( hdb,
1027 "SELECT `Name`, `Data` FROM `_Streams`", &view );
1030 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1033 r = MsiViewExecute( view, 0 );
1036 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1039 r = MsiViewFetch( view, &rec );
1042 ok( r == ERROR_SUCCESS, "Failed to fetch record: %d\n", r);
1046 r = MsiRecordGetString( rec, 1, file, &size );
1049 ok( r == ERROR_SUCCESS, "Failed to get string: %d\n", r);
1050 ok( !lstrcmp(file, "data"), "Expected 'data', got %s\n", file);
1054 memset(buf, 0, MAX_PATH);
1055 r = MsiRecordReadStream( rec, 2, buf, &size );
1058 ok( r == ERROR_SUCCESS, "Failed to get stream: %d\n", r);
1059 ok( !lstrcmp(buf, "test.txt\n"), "Expected 'test.txt\\n', got %s\n", buf);
1062 MsiCloseHandle( rec );
1064 r = MsiViewFetch( view, &rec );
1067 ok( r == ERROR_NO_MORE_ITEMS, "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
1070 MsiCloseHandle( view );
1071 MsiCloseHandle( hdb );
1072 DeleteFile(msifile);
1075 static void test_where(void)
1077 MSIHANDLE hdb = 0, rec;
1082 ok( hdb, "failed to create db\n");
1084 r = run_query( hdb, 0,
1085 "CREATE TABLE `Media` ("
1086 "`DiskId` SHORT NOT NULL, "
1087 "`LastSequence` LONG, "
1088 "`DiskPrompt` CHAR(64) LOCALIZABLE, "
1089 "`Cabinet` CHAR(255), "
1090 "`VolumeLabel` CHAR(32), "
1091 "`Source` CHAR(72) "
1092 "PRIMARY KEY `DiskId`)" );
1093 ok( r == S_OK, "cannot create Media table: %d\n", r );
1095 r = run_query( hdb, 0, "INSERT INTO `Media` "
1096 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
1097 "VALUES ( 1, 0, '', 'zero.cab', '', '' )" );
1098 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
1100 r = run_query( hdb, 0, "INSERT INTO `Media` "
1101 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
1102 "VALUES ( 2, 1, '', 'one.cab', '', '' )" );
1103 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
1105 r = run_query( hdb, 0, "INSERT INTO `Media` "
1106 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
1107 "VALUES ( 3, 2, '', 'two.cab', '', '' )" );
1108 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
1110 query = "SELECT * FROM `Media`";
1111 r = do_query(hdb, query, &rec);
1112 ok(r == ERROR_SUCCESS, "MsiViewFetch failed: %d\n", r);
1113 ok( check_record( rec, 4, "zero.cab"), "wrong cabinet\n");
1114 MsiCloseHandle( rec );
1116 query = "SELECT * FROM `Media` WHERE `LastSequence` >= 1";
1117 r = do_query(hdb, query, &rec);
1118 ok(r == ERROR_SUCCESS, "MsiViewFetch failed: %d\n", r);
1119 ok( check_record( rec, 4, "one.cab"), "wrong cabinet\n");
1121 r = MsiRecordGetInteger(rec, 1);
1122 ok( 2 == r, "field wrong\n");
1123 r = MsiRecordGetInteger(rec, 2);
1124 ok( 1 == r, "field wrong\n");
1126 MsiCloseHandle( rec );
1128 MsiCloseHandle( hdb );
1129 DeleteFile(msifile);
1132 static CHAR CURR_DIR[MAX_PATH];
1134 static const CHAR test_data[] = "FirstPrimaryColumn\tSecondPrimaryColumn\tShortInt\tShortIntNullable\tLongInt\tLongIntNullable\tString\tLocalizableString\tLocalizableStringNullable\n"
1135 "s255\ti2\ti2\tI2\ti4\tI4\tS255\tS0\ts0\n"
1136 "TestTable\tFirstPrimaryColumn\n"
1137 "stringage\t5\t2\t\t2147483640\t-2147483640\tanother string\tlocalizable\tduh\n";
1139 static void write_file(const CHAR *filename, const char *data, int data_size)
1143 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
1144 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1146 WriteFile(hf, data, data_size, &size, NULL);
1150 static UINT add_table_to_db(MSIHANDLE hdb, LPCSTR table_data)
1154 write_file("temp_file", table_data, (lstrlen(table_data) - 1) * sizeof(char));
1155 r = MsiDatabaseImportA(hdb, CURR_DIR, "temp_file");
1156 DeleteFileA("temp_file");
1161 static void test_msiimport(void)
1163 MSIHANDLE hdb, view, rec;
1168 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
1170 r = MsiOpenDatabaseA(msifile, MSIDBOPEN_CREATE, &hdb);
1171 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1173 r = add_table_to_db(hdb, test_data);
1176 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1179 query = "SELECT * FROM `TestTable`";
1180 r = MsiDatabaseOpenView(hdb, query, &view);
1183 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1186 r = MsiViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
1187 count = MsiRecordGetFieldCount(rec);
1190 ok(count == 9, "Expected 9, got %d\n", count);
1191 ok(check_record(rec, 1, "FirstPrimaryColumn"), "Expected FirstPrimaryColumn\n");
1192 ok(check_record(rec, 2, "SecondPrimaryColumn"), "Expected SecondPrimaryColumn\n");
1193 ok(check_record(rec, 3, "ShortInt"), "Expected ShortInt\n");
1194 ok(check_record(rec, 4, "ShortIntNullable"), "Expected ShortIntNullalble\n");
1195 ok(check_record(rec, 5, "LongInt"), "Expected LongInt\n");
1196 ok(check_record(rec, 6, "LongIntNullable"), "Expected LongIntNullalble\n");
1197 ok(check_record(rec, 7, "String"), "Expected String\n");
1198 ok(check_record(rec, 8, "LocalizableString"), "Expected LocalizableString\n");
1199 ok(check_record(rec, 9, "LocalizableStringNullable"), "Expected LocalizableStringNullable\n");
1202 r = MsiViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
1203 count = MsiRecordGetFieldCount(rec);
1206 ok(count == 9, "Expected 9, got %d\n", count);
1207 ok(check_record(rec, 1, "s255"), "Expected s255\n");
1208 ok(check_record(rec, 2, "i2"), "Expected i2\n");
1209 ok(check_record(rec, 3, "i2"), "Expected i2\n");
1210 ok(check_record(rec, 4, "I2"), "Expected I2\n");
1211 ok(check_record(rec, 5, "i4"), "Expected i4\n");
1212 ok(check_record(rec, 6, "I4"), "Expected I4\n");
1213 ok(check_record(rec, 7, "S255"), "Expected S255\n");
1214 ok(check_record(rec, 8, "S0"), "Expected S0\n");
1215 ok(check_record(rec, 9, "s0"), "Expected s0\n");
1218 query = "SELECT * FROM `TestTable`";
1219 r = do_query(hdb, query, &rec);
1222 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1227 ok(check_record(rec, 1, "stringage"), "Expected 'stringage'\n");
1228 ok(check_record(rec, 7, "another string"), "Expected 'another string'\n");
1229 ok(check_record(rec, 8, "localizable"), "Expected 'localizable'\n");
1230 ok(check_record(rec, 9, "duh"), "Expected 'duh'\n");
1233 i = MsiRecordGetInteger(rec, 2);
1236 ok(i == 5, "Expected 5, got %d\n", i);
1239 i = MsiRecordGetInteger(rec, 3);
1242 ok(i == 2, "Expected 2, got %d\n", i);
1245 i = MsiRecordGetInteger(rec, 4);
1246 ok(i == 0x80000000, "Expected 0x80000000, got %d\n", i);
1248 i = MsiRecordGetInteger(rec, 5);
1251 ok(i == 2147483640, "Expected 2147483640, got %d\n", i);
1254 i = MsiRecordGetInteger(rec, 6);
1257 ok(i == -2147483640, "Expected -2147483640, got %d\n", i);
1260 MsiCloseHandle(rec);
1261 MsiCloseHandle(view);
1262 MsiCloseHandle(hdb);
1263 DeleteFileA(msifile);
1266 static void test_markers(void)
1273 ok( hdb, "failed to create db\n");
1275 rec = MsiCreateRecord(3);
1276 MsiRecordSetString(rec, 1, "Table");
1277 MsiRecordSetString(rec, 2, "Apples");
1278 MsiRecordSetString(rec, 3, "Oranges");
1280 /* try a legit create */
1281 query = "CREATE TABLE `Table` ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
1282 r = run_query(hdb, 0, query);
1283 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1284 MsiCloseHandle(rec);
1286 /* try table name as marker */
1287 rec = MsiCreateRecord(1);
1288 MsiRecordSetString(rec, 1, "Fable");
1289 query = "CREATE TABLE `?` ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
1290 r = run_query(hdb, rec, query);
1291 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1293 /* verify that we just created a table called '?', not 'Fable' */
1294 r = try_query(hdb, "SELECT * from `Fable`");
1295 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1297 r = try_query(hdb, "SELECT * from `?`");
1298 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1300 /* try table name as marker without backticks */
1301 MsiRecordSetString(rec, 1, "Mable");
1302 query = "CREATE TABLE ? ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
1303 r = run_query(hdb, rec, query);
1304 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1306 /* try one column name as marker */
1307 MsiRecordSetString(rec, 1, "One");
1308 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
1309 r = run_query(hdb, rec, query);
1310 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1312 /* try column names as markers */
1313 MsiCloseHandle(rec);
1314 rec = MsiCreateRecord(2);
1315 MsiRecordSetString(rec, 1, "One");
1316 MsiRecordSetString(rec, 2, "Two");
1317 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `One`)";
1318 r = run_query(hdb, rec, query);
1319 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1321 /* try names with backticks */
1322 MsiCloseHandle(rec);
1323 rec = MsiCreateRecord(3);
1324 MsiRecordSetString(rec, 1, "One");
1325 MsiRecordSetString(rec, 2, "Two");
1326 MsiRecordSetString(rec, 3, "One");
1327 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `?`)";
1328 r = run_query(hdb, rec, query);
1329 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1331 /* try names with backticks, minus definitions */
1332 query = "CREATE TABLE `Mable` ( `?`, `?` PRIMARY KEY `?`)";
1333 r = run_query(hdb, rec, query);
1334 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1336 /* try names without backticks */
1337 query = "CREATE TABLE `Mable` ( ? SHORT NOT NULL, ? CHAR(255) PRIMARY KEY ?)";
1338 r = run_query(hdb, rec, query);
1339 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1341 /* try one long marker */
1342 MsiCloseHandle(rec);
1343 rec = MsiCreateRecord(1);
1344 MsiRecordSetString(rec, 1, "`One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`");
1345 query = "CREATE TABLE `Mable` ( ? )";
1346 r = run_query(hdb, rec, query);
1347 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1349 /* try all names as markers */
1350 MsiCloseHandle(rec);
1351 rec = MsiCreateRecord(4);
1352 MsiRecordSetString(rec, 1, "Mable");
1353 MsiRecordSetString(rec, 2, "One");
1354 MsiRecordSetString(rec, 3, "Two");
1355 MsiRecordSetString(rec, 4, "One");
1356 query = "CREATE TABLE `?` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `?`)";
1357 r = run_query(hdb, rec, query);
1358 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1360 /* try a legit insert */
1361 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( 5, 'hello' )";
1362 r = run_query(hdb, 0, query);
1363 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1365 r = try_query(hdb, "SELECT * from `Table`");
1366 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1368 /* try values as markers */
1369 MsiCloseHandle(rec);
1370 rec = MsiCreateRecord(2);
1371 MsiRecordSetInteger(rec, 1, 4);
1372 MsiRecordSetString(rec, 2, "hi");
1373 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, '?' )";
1374 r = run_query(hdb, rec, query);
1375 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1377 /* try column names and values as markers */
1378 MsiCloseHandle(rec);
1379 rec = MsiCreateRecord(4);
1380 MsiRecordSetString(rec, 1, "One");
1381 MsiRecordSetString(rec, 2, "Two");
1382 MsiRecordSetInteger(rec, 3, 5);
1383 MsiRecordSetString(rec, 4, "hi");
1384 query = "INSERT INTO `Table` ( `?`, `?` ) VALUES ( ?, '?' )";
1385 r = run_query(hdb, rec, query);
1386 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1388 /* try column names as markers */
1389 MsiCloseHandle(rec);
1390 rec = MsiCreateRecord(2);
1391 MsiRecordSetString(rec, 1, "One");
1392 MsiRecordSetString(rec, 2, "Two");
1393 query = "INSERT INTO `Table` ( `?`, `?` ) VALUES ( 3, 'yellow' )";
1394 r = run_query(hdb, rec, query);
1395 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1397 /* try table name as a marker */
1398 MsiCloseHandle(rec);
1399 rec = MsiCreateRecord(1);
1400 MsiRecordSetString(rec, 1, "Table");
1401 query = "INSERT INTO `?` ( `One`, `Two` ) VALUES ( 2, 'green' )";
1402 r = run_query(hdb, rec, query);
1403 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1405 /* try table name and values as markers */
1406 MsiCloseHandle(rec);
1407 rec = MsiCreateRecord(3);
1408 MsiRecordSetString(rec, 1, "Table");
1409 MsiRecordSetInteger(rec, 2, 10);
1410 MsiRecordSetString(rec, 3, "haha");
1411 query = "INSERT INTO `?` ( `One`, `Two` ) VALUES ( ?, '?' )";
1412 r = run_query(hdb, rec, query);
1413 ok(r == ERROR_FUNCTION_FAILED, "Expected ERROR_FUNCTION_FAILED, got %d\n", r);
1415 /* try all markers */
1416 MsiCloseHandle(rec);
1417 rec = MsiCreateRecord(5);
1418 MsiRecordSetString(rec, 1, "Table");
1419 MsiRecordSetString(rec, 1, "One");
1420 MsiRecordSetString(rec, 1, "Two");
1421 MsiRecordSetInteger(rec, 2, 10);
1422 MsiRecordSetString(rec, 3, "haha");
1423 query = "INSERT INTO `?` ( `?`, `?` ) VALUES ( ?, '?' )";
1424 r = run_query(hdb, rec, query);
1425 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1427 /* insert an integer as a string */
1428 MsiCloseHandle(rec);
1429 rec = MsiCreateRecord(2);
1430 MsiRecordSetString(rec, 1, "11");
1431 MsiRecordSetString(rec, 2, "hi");
1432 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, '?' )";
1433 r = run_query(hdb, rec, query);
1434 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1436 /* leave off the '' for the string */
1437 MsiCloseHandle(rec);
1438 rec = MsiCreateRecord(2);
1439 MsiRecordSetInteger(rec, 1, 12);
1440 MsiRecordSetString(rec, 2, "hi");
1441 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, ? )";
1442 r = run_query(hdb, rec, query);
1443 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1444 MsiCloseHandle(rec);
1446 MsiCloseHandle(hdb);
1447 DeleteFileA(msifile);
1450 #define MY_NVIEWS 4000 /* Largest installer I've seen uses < 2k */
1451 static void test_handle_limit(void)
1455 MSIHANDLE hviews[MY_NVIEWS];
1458 /* create an empty db */
1460 ok( hdb, "failed to create db\n");
1462 memset(hviews, 0, sizeof(hviews));
1464 for (i=0; i<MY_NVIEWS; i++) {
1465 static char szQueryBuf[256] = "SELECT * from `_Tables`";
1466 hviews[i] = 0xdeadbeeb;
1467 r = MsiDatabaseOpenView(hdb, szQueryBuf, &hviews[i]);
1468 if( r != ERROR_SUCCESS || hviews[i] == 0xdeadbeeb ||
1469 hviews[i] == 0 || (i && (hviews[i] == hviews[i-1])))
1473 ok( i == MY_NVIEWS, "problem opening views\n");
1475 for (i=0; i<MY_NVIEWS; i++) {
1476 if (hviews[i] != 0 && hviews[i] != 0xdeadbeeb) {
1477 r = MsiCloseHandle(hviews[i]);
1478 if (r != ERROR_SUCCESS)
1483 ok( i == MY_NVIEWS, "problem closing views\n");
1485 r = MsiCloseHandle(hdb);
1486 ok( r == ERROR_SUCCESS, "failed to close database\n");
1489 static void test_generate_transform(void)
1491 MSIHANDLE hdb1, hdb2;
1495 DeleteFile(msifile);
1496 DeleteFile(msifile2);
1497 DeleteFile(mstfile);
1499 /* create an empty database */
1500 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb1 );
1501 ok( r == ERROR_SUCCESS , "Failed to create database\n" );
1503 r = MsiDatabaseCommit( hdb1 );
1504 ok( r == ERROR_SUCCESS , "Failed to commit database\n" );
1506 /* create another empty database */
1507 r = MsiOpenDatabase(msifile2, MSIDBOPEN_CREATE, &hdb2 );
1508 ok( r == ERROR_SUCCESS , "Failed to create database\n" );
1510 r = MsiDatabaseCommit( hdb2 );
1511 ok( r == ERROR_SUCCESS , "Failed to commit database\n" );
1513 /* the transform between two empty database should be empty */
1514 r = MsiDatabaseGenerateTransform(hdb1, hdb2, NULL, 0, 0);
1516 ok( r == ERROR_NO_DATA, "return code %d, should be ERROR_NO_DATA\n", r );
1519 query = "CREATE TABLE `AAR` ( `BAR` SHORT NOT NULL, `CAR` CHAR(255) PRIMARY KEY `CAR`)";
1520 r = run_query(hdb1, 0, query);
1521 ok(r == ERROR_SUCCESS, "failed to add table\n");
1524 r = MsiDatabaseGenerateTransform(hdb1, hdb2, NULL, 0, 0);
1525 ok( r == ERROR_SUCCESS, "return code %d, should be ERROR_SUCCESS\n", r );
1527 r = MsiDatabaseGenerateTransform(hdb1, hdb2, mstfile, 0, 0);
1528 ok( r == ERROR_SUCCESS, "return code %d, should be ERROR_SUCCESS\n", r );
1530 MsiCloseHandle( hdb1 );
1532 r = MsiDatabaseApplyTransform( hdb2, mstfile, 0 );
1533 ok( r == ERROR_SUCCESS, "return code %d, should be ERROR_SUCCESS\n", r );
1535 /* apply the same transform again? */
1536 r = MsiDatabaseApplyTransform( hdb2, mstfile, 0 );
1537 ok( r == ERROR_INSTALL_TRANSFORM_FAILURE,
1538 "return code %d, should be ERROR_INSTALL_TRANSFORM_FAILURE\n", r );
1541 MsiCloseHandle( hdb2 );
1543 DeleteFile(msifile);
1544 DeleteFile(msifile2);
1545 DeleteFile(mstfile);
1552 test_msidecomposedesc();
1553 test_msibadqueries();
1555 test_viewgetcolumninfo();
1563 test_handle_limit();
1564 test_generate_transform();