1 /*
2  * Copyright (C) 2005 Mike McCormack for CodeWeavers
3  *
4  * A test program for MSI records
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include <windows.h>
22 #include <msi.h>
23 #include <msiquery.h>
24 
25 #include "wine/test.h"
26 
27 static const char *msifile = "winetest-record.msi";
28 static const WCHAR msifileW[] =
29     {'w','i','n','e','t','e','s','t','-','r','e','c','o','r','d','.','m','s','i',0};
30 
31 static BOOL create_temp_file(char *name)
32 {
33     UINT r;
34     unsigned char buffer[26], i;
35     DWORD sz;
36     HANDLE handle;
37 
38     r = GetTempFileNameA(".", "msitest",0,name);
39     if(!r)
40         return r;
41     handle = CreateFileA(name, GENERIC_READ|GENERIC_WRITE,
42         0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
43     if(handle==INVALID_HANDLE_VALUE)
44         return FALSE;
45     for(i=0; i<26; i++)
46         buffer[i]=i+'a';
47     r = WriteFile(handle,buffer,sizeof buffer,&sz,NULL);
48     CloseHandle(handle);
49     return r;
50 }
51 
52 static void test_msirecord(void)
53 {
54     DWORD r, sz;
55     INT i;
56     MSIHANDLE h;
57     char buf[10];
58     WCHAR bufW[10];
59     const char str[] = "hello";
60     const WCHAR strW[] = { 'h','e','l','l','o',0};
61     char filename[MAX_PATH];
62 
63     /* check behaviour with an invalid record */
64     r = MsiRecordGetFieldCount(0);
65     ok(r==-1, "field count for invalid record not -1\n");
66     SetLastError(0);
67     r = MsiRecordIsNull(0, 0);
68     ok(r==0, "invalid handle not considered to be non-null...\n");
69     ok(GetLastError()==0, "MsiRecordIsNull set LastError\n");
70     r = MsiRecordGetInteger(0,0);
71     ok(r == MSI_NULL_INTEGER, "got integer from invalid record\n");
72     r = MsiRecordSetInteger(0,0,0);
73     ok(r == ERROR_INVALID_HANDLE, "MsiRecordSetInteger returned wrong error\n");
74     r = MsiRecordSetInteger(0,-1,0);
75     ok(r == ERROR_INVALID_HANDLE, "MsiRecordSetInteger returned wrong error\n");
76     SetLastError(0);
77     h = MsiCreateRecord(-1);
78     ok(h==0, "created record with -1 elements\n");
79     h = MsiCreateRecord(0x10000);
80     ok(h==0, "created record with 0x10000 elements\n");
81     /* doesn't set LastError */
82     ok(GetLastError()==0, "MsiCreateRecord set last error\n");
83     r = MsiRecordClearData(0);
84     ok(r == ERROR_INVALID_HANDLE, "MsiRecordClearData returned wrong error\n");
85     r = MsiRecordDataSize(0,0);
86     ok(r == 0, "MsiRecordDataSize returned wrong error\n");
87 
88 
89     /* check behaviour of a record with 0 elements */
90     h = MsiCreateRecord(0);
91     ok(h!=0, "couldn't create record with zero elements\n");
92     r = MsiRecordGetFieldCount(h);
93     ok(r==0, "field count should be zero\n");
94     r = MsiRecordIsNull(h,0);
95     ok(r, "new record wasn't null\n");
96     r = MsiRecordIsNull(h,1);
97     ok(r, "out of range record wasn't null\n");
98     r = MsiRecordIsNull(h,-1);
99     ok(r, "out of range record wasn't null\n");
100     r = MsiRecordDataSize(h,0);
101     ok(r==0, "size of null record is 0\n");
102     sz = sizeof buf;
103     strcpy(buf,"x");
104     r = MsiRecordGetStringA(h, 0, buf, &sz);
105     ok(r==ERROR_SUCCESS, "failed to get null string\n");
106     ok(sz==0, "null string too long\n");
107     ok(buf[0]==0, "null string not set\n");
108 
109     /* same record, but add an integer to it */
110     r = MsiRecordSetInteger(h, 0, 0);
111     ok(r == ERROR_SUCCESS, "Failed to set integer at 0 to 0\n");
112     r = MsiRecordIsNull(h,0);
113     ok(r==0, "new record is null after setting an integer\n");
114     r = MsiRecordDataSize(h,0);
115     ok(r==sizeof(DWORD), "size of integer record is 4\n");
116     r = MsiRecordSetInteger(h, 0, 1);
117     ok(r == ERROR_SUCCESS, "Failed to set integer at 0 to 1\n");
118     r = MsiRecordSetInteger(h, 1, 1);
119     ok(r == ERROR_INVALID_PARAMETER, "set integer at 1\n");
120     r = MsiRecordSetInteger(h, -1, 0);
121     ok(r == ERROR_INVALID_PARAMETER, "set integer at -1\n");
122     r = MsiRecordIsNull(h,0);
123     ok(r==0, "new record is null after setting an integer\n");
124     r = MsiRecordGetInteger(h, 0);
125     ok(r == 1, "failed to get integer\n");
126 
127     /* same record, but add a null or empty string to it */
128     r = MsiRecordSetStringA(h, 0, NULL);
129     ok(r == ERROR_SUCCESS, "Failed to set null string at 0\n");
130     r = MsiRecordIsNull(h, 0);
131     ok(r == TRUE, "null string not null field\n");
132     r = MsiRecordDataSize(h, 0);
133     ok(r == 0, "size of string record is strlen\n");
134     buf[0] = 0;
135     sz = sizeof buf;
136     r = MsiRecordGetStringA(h, 0, buf, &sz);
137     ok(r == ERROR_SUCCESS, "Failed to get string at 0\n");
138     ok(buf[0] == 0, "MsiRecordGetStringA returned the wrong string\n");
139     ok(sz == 0, "MsiRecordGetStringA returned the wrong length\n");
140     bufW[0] = 0;
141     sz = sizeof bufW / sizeof bufW[0];
142     r = MsiRecordGetStringW(h, 0, bufW, &sz);
143     ok(r == ERROR_SUCCESS, "Failed to get string at 0\n");
144     ok(bufW[0] == 0, "MsiRecordGetStringW returned the wrong string\n");
145     ok(sz == 0, "MsiRecordGetStringW returned the wrong length\n");
146     r = MsiRecordSetStringA(h, 0, "");
147     ok(r == ERROR_SUCCESS, "Failed to set empty string at 0\n");
148     r = MsiRecordIsNull(h, 0);
149     ok(r == TRUE, "null string not null field\n");
150     r = MsiRecordDataSize(h, 0);
151     ok(r == 0, "size of string record is strlen\n");
152     buf[0] = 0;
153     sz = sizeof buf;
154     r = MsiRecordGetStringA(h, 0, buf, &sz);
155     ok(r == ERROR_SUCCESS, "Failed to get string at 0\n");
156     ok(buf[0] == 0, "MsiRecordGetStringA returned the wrong string\n");
157     ok(sz == 0, "MsiRecordGetStringA returned the wrong length\n");
158     bufW[0] = 0;
159     sz = sizeof bufW / sizeof bufW[0];
160     r = MsiRecordGetStringW(h, 0, bufW, &sz);
161     ok(r == ERROR_SUCCESS, "Failed to get string at 0\n");
162     ok(bufW[0] == 0, "MsiRecordGetStringW returned the wrong string\n");
163     ok(sz == 0, "MsiRecordGetStringW returned the wrong length\n");
164 
165     /* same record, but add a string to it */
166     r = MsiRecordSetStringA(h,0,str);
167     ok(r == ERROR_SUCCESS, "Failed to set string at 0\n");
168     r = MsiRecordGetInteger(h, 0);
169     ok(r == MSI_NULL_INTEGER, "should get invalid integer\n");
170     r = MsiRecordDataSize(h,0);
171     ok(r==sizeof str-1, "size of string record is strlen\n");
172     buf[0]=0;
173     sz = sizeof buf;
174     r = MsiRecordGetStringA(h,0,buf,&sz);
175     ok(r == ERROR_SUCCESS, "Failed to get string at 0\n");
176     ok(0==strcmp(buf,str), "MsiRecordGetStringA returned the wrong string\n");
177     ok(sz == sizeof str-1, "MsiRecordGetStringA returned the wrong length\n");
178     buf[0]=0;
179     sz = sizeof str - 2;
180     r = MsiRecordGetStringA(h,0,buf,&sz);
181     ok(r == ERROR_MORE_DATA, "small buffer should yield ERROR_MORE_DATA\n");
182     ok(sz == sizeof str-1, "MsiRecordGetStringA returned the wrong length\n");
183     ok(0==strncmp(buf,str,sizeof str-3), "MsiRecordGetStringA returned the wrong string\n");
184     ok(buf[sizeof str - 3]==0, "string wasn't nul terminated\n");
185 
186     buf[0]=0;
187     sz = sizeof str;
188     r = MsiRecordGetStringA(h,0,buf,&sz);
189     ok(r == ERROR_SUCCESS, "wrong error\n");
190     ok(sz == sizeof str-1, "MsiRecordGetStringA returned the wrong length\n");
191     ok(0==strcmp(buf,str), "MsiRecordGetStringA returned the wrong string\n");
192 
193 
194     memset(bufW, 0, sizeof bufW);
195     sz = 5;
196     r = MsiRecordGetStringW(h,0,bufW,&sz);
197     ok(r == ERROR_MORE_DATA, "wrong error\n");
198     ok(sz == 5, "MsiRecordGetStringA returned the wrong length\n");
199     ok(0==memcmp(bufW,strW,8), "MsiRecordGetStringA returned the wrong string\n");
200 
201     sz = 0;
202     bufW[0] = 'x';
203     r = MsiRecordGetStringW(h,0,bufW,&sz);
204     ok(r == ERROR_MORE_DATA, "wrong error\n");
205     ok(sz == 5, "MsiRecordGetStringA returned the wrong length\n");
206     ok('x'==bufW[0], "MsiRecordGetStringA returned the wrong string\n");
207 
208     memset(buf, 0, sizeof buf);
209     sz = 5;
210     r = MsiRecordGetStringA(h,0,buf,&sz);
211     ok(r == ERROR_MORE_DATA, "wrong error\n");
212     ok(sz == 5, "MsiRecordGetStringA returned the wrong length\n");
213     ok(0==memcmp(buf,str,4), "MsiRecordGetStringA returned the wrong string\n");
214 
215     sz = 0;
216     buf[0] = 'x';
217     r = MsiRecordGetStringA(h,0,buf,&sz);
218     ok(r == ERROR_MORE_DATA, "wrong error\n");
219     ok(sz == 5, "MsiRecordGetStringA returned the wrong length\n");
220     ok('x'==buf[0], "MsiRecordGetStringA returned the wrong string\n");
221 
222     /* same record, check we can wipe all the data */
223     r = MsiRecordClearData(h);
224     ok(r == ERROR_SUCCESS, "Failed to clear record\n");
225     r = MsiRecordClearData(h);
226     ok(r == ERROR_SUCCESS, "Failed to clear record again\n");
227     r = MsiRecordIsNull(h,0);
228     ok(r, "cleared record wasn't null\n");
229 
230     /* same record, try converting strings to integers */
231     i = MsiRecordSetStringA(h,0,"42");
232     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
233     i = MsiRecordGetInteger(h, 0);
234     ok(i == 42, "should get invalid integer\n");
235     i = MsiRecordSetStringA(h,0,"-42");
236     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
237     i = MsiRecordGetInteger(h, 0);
238     ok(i == -42, "should get invalid integer\n");
239     i = MsiRecordSetStringA(h,0," 42");
240     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
241     i = MsiRecordGetInteger(h, 0);
242     ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
243     i = MsiRecordSetStringA(h,0,"42 ");
244     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
245     i = MsiRecordGetInteger(h, 0);
246     ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
247     i = MsiRecordSetStringA(h,0,"42.0");
248     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
249     i = MsiRecordGetInteger(h, 0);
250     ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
251     i = MsiRecordSetStringA(h,0,"0x42");
252     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
253     i = MsiRecordGetInteger(h, 0);
254     ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
255     i = MsiRecordSetStringA(h,0,"1000000000000000");
256     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
257     i = MsiRecordGetInteger(h, 0);
258     ok(i == -1530494976, "should get truncated integer\n");
259     i = MsiRecordSetStringA(h,0,"2147483647");
260     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
261     i = MsiRecordGetInteger(h, 0);
262     ok(i == 2147483647, "should get maxint\n");
263     i = MsiRecordSetStringA(h,0,"-2147483647");
264     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
265     i = MsiRecordGetInteger(h, 0);
266     ok(i == -2147483647, "should get -maxint-1\n");
267     i = MsiRecordSetStringA(h,0,"4294967297");
268     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
269     i = MsiRecordGetInteger(h, 0);
270     ok(i == 1, "should get one\n");
271     i = MsiRecordSetStringA(h,0,"foo");
272     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
273     i = MsiRecordGetInteger(h, 0);
274     ok(i == MSI_NULL_INTEGER, "should get zero\n");
275     i = MsiRecordSetStringA(h,0,"");
276     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
277     i = MsiRecordGetInteger(h, 0);
278     ok(i == MSI_NULL_INTEGER, "should get zero\n");
279     i = MsiRecordSetStringA(h,0,"+1");
280     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
281     i = MsiRecordGetInteger(h, 0);
282     ok(i == MSI_NULL_INTEGER, "should get zero\n");
283 
284     /* same record, try converting integers to strings */
285     r = MsiRecordSetInteger(h, 0, 32);
286     ok(r == ERROR_SUCCESS, "Failed to set integer at 0 to 32\n");
287     sz = 1;
288     r = MsiRecordGetStringA(h, 0, NULL, &sz);
289     ok(r == ERROR_SUCCESS, "failed to get string from integer\n");
290     ok(sz == 2, "length wrong\n");
291     buf[0]=0;
292     sz = sizeof buf;
293     r = MsiRecordGetStringA(h, 0, buf, &sz);
294     ok(r == ERROR_SUCCESS, "failed to get string from integer\n");
295     ok(0==strcmp(buf,"32"), "failed to get string from integer\n");
296     r = MsiRecordSetInteger(h, 0, -32);
297     ok(r == ERROR_SUCCESS, "Failed to set integer at 0 to 32\n");
298     buf[0]=0;
299     sz = 1;
300     r = MsiRecordGetStringA(h, 0, NULL, &sz);
301     ok(r == ERROR_SUCCESS, "failed to get string from integer\n");
302     ok(sz == 3, "length wrong\n");
303     sz = sizeof buf;
304     r = MsiRecordGetStringA(h, 0, buf, &sz);
305     ok(r == ERROR_SUCCESS, "failed to get string from integer\n");
306     ok(0==strcmp(buf,"-32"), "failed to get string from integer\n");
307     buf[0]=0;
308 
309     /* same record, now try streams */
310     r = MsiRecordSetStreamA(h, 0, NULL);
311     ok(r == ERROR_INVALID_PARAMETER, "set NULL stream\n");
312     sz = sizeof buf;
313     r = MsiRecordReadStream(h, 0, buf, &sz);
314     ok(r == ERROR_INVALID_DATATYPE, "read non-stream type\n");
315     ok(sz == sizeof buf, "set sz\n");
316     r = MsiRecordDataSize( h, -1);
317     ok(r == 0,"MsiRecordDataSize returned wrong size\n");
318     r = MsiRecordDataSize( h, 0);
319     ok(r == 4,"MsiRecordDataSize returned wrong size\n");
320 
321     /* same record, now close it */
322     r = MsiCloseHandle(h);
323     ok(r == ERROR_SUCCESS, "Failed to close handle\n");
324 
325     /* now try streams in a new record - need to create a file to play with */
326     r = create_temp_file(filename);
327     if(!r)
328         return;
329 
330     /* streams can't be inserted in field 0 for some reason */
331     h = MsiCreateRecord(2);
332     ok(h, "couldn't create a two field record\n");
333     r = MsiRecordSetStreamA(h, 0, filename);
334     ok(r == ERROR_INVALID_PARAMETER, "added stream to field 0\n");
335     r = MsiRecordSetStreamA(h, 1, filename);
336     ok(r == ERROR_SUCCESS, "failed to add stream to record\n");
337     r = MsiRecordReadStream(h, 1, buf, NULL);
338     ok(r == ERROR_INVALID_PARAMETER, "should return error\n");
339     DeleteFileA(filename); /* Windows 98 doesn't like this at all, so don't check return. */
340     r = MsiRecordReadStream(h, 1, NULL, NULL);
341     ok(r == ERROR_INVALID_PARAMETER, "should return error\n");
342     sz = sizeof buf;
343     r = MsiRecordReadStream(h, 1, NULL, &sz);
344     ok(r == ERROR_SUCCESS, "failed to read stream\n");
345     ok(sz==26,"couldn't get size of stream\n");
346     sz = 0;
347     r = MsiRecordReadStream(h, 1, buf, &sz);
348     ok(r == ERROR_SUCCESS, "failed to read stream\n");
349     ok(sz==0,"short read\n");
350     sz = sizeof buf;
351     r = MsiRecordReadStream(h, 1, buf, &sz);
352     ok(r == ERROR_SUCCESS, "failed to read stream\n");
353     ok(sz==sizeof buf,"short read\n");
354     ok(!strncmp(buf,"abcdefghij",10), "read the wrong thing\n");
355     sz = sizeof buf;
356     r = MsiRecordReadStream(h, 1, buf, &sz);
357     ok(r == ERROR_SUCCESS, "failed to read stream\n");
358     ok(sz==sizeof buf,"short read\n");
359     ok(!strncmp(buf,"klmnopqrst",10), "read the wrong thing\n");
360     memset(buf,0,sizeof buf);
361     sz = sizeof buf;
362     r = MsiRecordReadStream(h, 1, buf, &sz);
363     ok(r == ERROR_SUCCESS, "failed to read stream\n");
364     ok(sz==6,"short read\n");
365     ok(!strcmp(buf,"uvwxyz"), "read the wrong thing\n");
366     memset(buf,0,sizeof buf);
367     sz = sizeof buf;
368     r = MsiRecordReadStream(h, 1, buf, &sz);
369     ok(r == ERROR_SUCCESS, "failed to read stream\n");
370     ok(sz==0,"size non-zero at end of stream\n");
371     ok(buf[0]==0, "read something at end of the stream\n");
372     r = MsiRecordSetStreamA(h, 1, NULL);
373     ok(r == ERROR_SUCCESS, "failed to reset stream\n");
374     sz = 0;
375     r = MsiRecordReadStream(h, 1, NULL, &sz);
376     ok(r == ERROR_SUCCESS, "bytes left wrong after reset\n");
377     ok(sz==26,"couldn't get size of stream\n");
378     r = MsiRecordDataSize(h,1);
379     ok(r == 26,"MsiRecordDataSize returned wrong size\n");
380 
381     /* now close the stream record */
382     r = MsiCloseHandle(h);
383     ok(r == ERROR_SUCCESS, "Failed to close handle\n");
384     DeleteFileA(filename); /* Delete it for sure, when everything else is closed. */
385 }
386 
387 static void test_MsiRecordGetString(void)
388 {
389     MSIHANDLE rec;
390     CHAR buf[MAX_PATH];
391     DWORD sz;
392     UINT r;
393 
394     rec = MsiCreateRecord(2);
395     ok(rec != 0, "Expected a valid handle\n");
396 
397     sz = MAX_PATH;
398     r = MsiRecordGetStringA(rec, 1, NULL, &sz);
399     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n",r);
400     ok(sz == 0, "Expected 0, got %d\n",sz);
401 
402     sz = MAX_PATH;
403     lstrcpyA(buf, "apple");
404     r = MsiRecordGetStringA(rec, 1, buf, &sz);
405     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
406     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
407     ok(sz == 0, "Expected 0, got %d\n", sz);
408 
409     sz = MAX_PATH;
410     lstrcpyA(buf, "apple");
411     r = MsiRecordGetStringA(rec, 10, buf, &sz);
412     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
413     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
414     ok(sz == 0, "Expected 0, got %d\n", sz);
415 
416     MsiCloseHandle(rec);
417 
418     rec = MsiCreateRecord(1);
419     ok(rec != 0, "Expected a valid handle\n");
420 
421     r = MsiRecordSetInteger(rec, 1, 5);
422     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
423 
424     sz = MAX_PATH;
425     r = MsiRecordGetStringA(rec, 1, NULL, &sz);
426     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n",r);
427     ok(sz == 1, "Expected 1, got %d\n",sz);
428 
429     sz = MAX_PATH;
430     lstrcpyA(buf, "apple");
431     r = MsiRecordGetStringA(rec, 1, buf, &sz);
432     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
433     ok(!lstrcmpA(buf, "5"), "Expected \"5\", got \"%s\"\n", buf);
434     ok(sz == 1, "Expectd 1, got %d\n", sz);
435 
436     r = MsiRecordSetInteger(rec, 1, -5);
437     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
438 
439     sz = MAX_PATH;
440     lstrcpyA(buf, "apple");
441     r = MsiRecordGetStringA(rec, 1, buf, &sz);
442     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
443     ok(!lstrcmpA(buf, "-5"), "Expected \"-5\", got \"%s\"\n", buf);
444     ok(sz == 2, "Expectd 2, got %d\n", sz);
445 
446     MsiCloseHandle(rec);
447 }
448 
449 static void test_MsiRecordGetInteger(void)
450 {
451     MSIHANDLE rec;
452     INT val;
453     UINT r;
454 
455     rec = MsiCreateRecord(1);
456     ok(rec != 0, "Expected a valid handle\n");
457 
458     r = MsiRecordSetStringA(rec, 1, "5");
459     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
460 
461     val = MsiRecordGetInteger(rec, 1);
462     ok(val == 5, "Expected 5, got %d\n", val);
463 
464     r = MsiRecordSetStringA(rec, 1, "-5");
465     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
466 
467     val = MsiRecordGetInteger(rec, 1);
468     ok(val == -5, "Expected -5, got %d\n", val);
469 
470     r = MsiRecordSetStringA(rec, 1, "5apple");
471     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
472 
473     val = MsiRecordGetInteger(rec, 1);
474     ok(val == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", val);
475 
476     MsiCloseHandle(rec);
477 }
478 
479 static void test_fieldzero(void)
480 {
481     MSIHANDLE hdb, hview, rec;
482     CHAR buf[MAX_PATH];
483     LPCSTR query;
484     DWORD sz;
485     UINT r;
486 
487     rec = MsiCreateRecord(1);
488     ok(rec != 0, "Expected a valid handle\n");
489 
490     r = MsiRecordGetInteger(rec, 0);
491     ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);
492 
493     sz = MAX_PATH;
494     lstrcpyA(buf, "apple");
495     r = MsiRecordGetStringA(rec, 0, buf, &sz);
496     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
497     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
498     ok(sz == 0, "Expectd 0, got %d\n", sz);
499 
500     r = MsiRecordIsNull(rec, 0);
501     ok(r == TRUE, "Expected TRUE, got %d\n", r);
502 
503     r = MsiRecordGetInteger(rec, 1);
504     ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);
505 
506     r = MsiRecordSetInteger(rec, 1, 42);
507     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
508 
509     r = MsiRecordGetInteger(rec, 0);
510     ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);
511 
512     sz = MAX_PATH;
513     lstrcpyA(buf, "apple");
514     r = MsiRecordGetStringA(rec, 0, buf, &sz);
515     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
516     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
517     ok(sz == 0, "Expectd 0, got %d\n", sz);
518 
519     r = MsiRecordIsNull(rec, 0);
520     ok(r == TRUE, "Expected TRUE, got %d\n", r);
521 
522     r = MsiRecordGetInteger(rec, 1);
523     ok(r == 42, "Expected 42, got %d\n", r);
524 
525     r = MsiRecordSetStringA(rec, 1, "bologna");
526     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
527 
528     r = MsiRecordGetInteger(rec, 0);
529     ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);
530 
531     sz = MAX_PATH;
532     lstrcpyA(buf, "apple");
533     r = MsiRecordGetStringA(rec, 0, buf, &sz);
534     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
535     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
536     ok(sz == 0, "Expectd 0, got %d\n", sz);
537 
538     r = MsiRecordIsNull(rec, 0);
539     ok(r == TRUE, "Expected TRUE, got %d\n", r);
540 
541     sz = MAX_PATH;
542     lstrcpyA(buf, "apple");
543     r = MsiRecordGetStringA(rec, 1, buf, &sz);
544     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
545     ok(!lstrcmpA(buf, "bologna"), "Expected \"bologna\", got \"%s\"\n", buf);
546     ok(sz == 7, "Expectd 7, got %d\n", sz);
547 
548     MsiCloseHandle(rec);
549 
550     r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
551     ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
552 
553     query = "CREATE TABLE `drone` ( "
554            "`id` INT, `name` CHAR(32), `number` CHAR(32) "
555            "PRIMARY KEY `id`)";
556     r = MsiDatabaseOpenViewA(hdb, query, &hview);
557     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
558     r = MsiViewExecute(hview, 0);
559     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
560     r = MsiViewClose(hview);
561     ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
562     r = MsiCloseHandle(hview);
563     ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
564 
565     query = "INSERT INTO `drone` ( `id`, `name`, `number` )"
566            "VALUES('1', 'Abe', '8675309')";
567     r = MsiDatabaseOpenViewA(hdb, query, &hview);
568     ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
569     r = MsiViewExecute(hview, 0);
570     ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
571     r = MsiViewClose(hview);
572     ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
573     r = MsiCloseHandle(hview);
574     ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
575 
576     r = MsiDatabaseGetPrimaryKeysA(hdb, "drone", &rec);
577     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
578 
579     r = MsiRecordGetInteger(rec, 0);
580     ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);
581 
582     sz = MAX_PATH;
583     lstrcpyA(buf, "apple");
584     r = MsiRecordGetStringA(rec, 0, buf, &sz);
585     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
586     ok(!lstrcmpA(buf, "drone"), "Expected \"drone\", got \"%s\"\n", buf);
587     ok(sz == 5, "Expectd 5, got %d\n", sz);
588 
589     r = MsiRecordIsNull(rec, 0);
590     ok(r == FALSE, "Expected FALSE, got %d\n", r);
591 
592     MsiCloseHandle(rec);
593 
594     r = MsiDatabaseGetPrimaryKeysA(hdb, "nosuchtable", &rec);
595     ok(r == ERROR_INVALID_TABLE, "Expected ERROR_INVALID_TABLE, got %d\n", r);
596 
597     query = "SELECT * FROM `drone` WHERE `id` = 1";
598     r = MsiDatabaseOpenViewA(hdb, query, &hview);
599     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
600     r = MsiViewExecute(hview, 0);
601     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
602     r = MsiViewFetch(hview, &rec);
603     ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
604 
605     r = MsiRecordGetInteger(rec, 0);
606     ok(r != MSI_NULL_INTEGER && r != 0, "Expected non-NULL value, got %d\n", r);
607 
608     r = MsiRecordIsNull(rec, 0);
609     ok(r == FALSE, "Expected FALSE, got %d\n", r);
610 
611     r = MsiCloseHandle(hview);
612     ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
613     MsiCloseHandle(rec);
614     MsiCloseHandle(hdb);
615     DeleteFileA(msifile);
616 }
617 
618 START_TEST(record)
619 {
620     test_msirecord();
621     test_MsiRecordGetString();
622     test_MsiRecordGetInteger();
623     test_fieldzero();
624 }
625