Use the SourceList functions to get the source path in
[wine] / dlls / msi / tests / record.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <windows.h>
22 #include <msi.h>
23 #include <msiquery.h>
24
25 #include "wine/test.h"
26
27 static BOOL create_temp_file(char *name)
28 {
29     UINT r;
30     unsigned char buffer[26], i;
31     DWORD sz;
32     HANDLE handle;
33     
34     r = GetTempFileName(".", "msitest",0,name);
35     if(!r)
36         return r;
37     handle = CreateFile(name, GENERIC_READ|GENERIC_WRITE, 
38         0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
39     if(handle==INVALID_HANDLE_VALUE)
40         return 0;
41     for(i=0; i<26; i++)
42         buffer[i]=i+'a';
43     r = WriteFile(handle,buffer,sizeof buffer,&sz,NULL);
44     CloseHandle(handle);
45     return r;
46 }
47
48 static void test_msirecord(void)
49 {
50     DWORD r, sz;
51     INT i;
52     MSIHANDLE h;
53     char buf[10];
54     const char str[] = "hello";
55     char filename[MAX_PATH];
56
57     /* check behaviour with an invalid record */
58     r = MsiRecordGetFieldCount(0);
59     ok(r==-1, "field count for invalid record not -1\n");
60     SetLastError(0);
61     r = MsiRecordIsNull(0, 0);
62     ok(r==0, "invalid handle not considered to be non-null...\n");
63     ok(GetLastError()==0, "MsiRecordIsNull set LastError\n");
64     r = MsiRecordGetInteger(0,0);
65     ok(r == MSI_NULL_INTEGER, "got integer from invalid record\n");
66     r = MsiRecordSetInteger(0,0,0);
67     ok(r == ERROR_INVALID_HANDLE, "MsiRecordSetInteger returned wrong error\n");
68     r = MsiRecordSetInteger(0,-1,0);
69     ok(r == ERROR_INVALID_HANDLE, "MsiRecordSetInteger returned wrong error\n");
70     SetLastError(0);
71     h = MsiCreateRecord(-1);
72     ok(h==0, "created record with -1 elements\n");
73     h = MsiCreateRecord(0x10000);
74     ok(h==0, "created record with 0x10000 elements\n");
75     /* doesn't set LastError */
76     ok(GetLastError()==0, "MsiCreateRecord set last error\n");
77     r = MsiRecordClearData(0);
78     ok(r == ERROR_INVALID_HANDLE, "MsiRecordClearData returned wrong error\n");
79     r = MsiRecordDataSize(0,0);
80     ok(r == 0, "MsiRecordDataSize returned wrong error\n");
81
82
83     /* check behaviour of a record with 0 elements */
84     h = MsiCreateRecord(0);
85     ok(h!=0, "couldn't create record with zero elements\n");
86     r = MsiRecordGetFieldCount(h);
87     ok(r==0, "field count should be zero\n");
88     r = MsiRecordIsNull(h,0);
89     ok(r, "new record wasn't null\n");
90     r = MsiRecordIsNull(h,1);
91     ok(r, "out of range record wasn't null\n");
92     r = MsiRecordIsNull(h,-1);
93     ok(r, "out of range record wasn't null\n");
94     r = MsiRecordDataSize(h,0);
95     ok(r==0, "size of null record is 0\n");
96     sz = sizeof buf;
97     strcpy(buf,"x");
98     r = MsiRecordGetString(h, 0, buf, &sz);
99     ok(r==ERROR_SUCCESS, "failed to get null string\n");
100     ok(sz==0, "null string too long\n");
101     ok(buf[0]==0, "null string not set\n");
102
103     /* same record, but add an integer to it */
104     r = MsiRecordSetInteger(h, 0, 0);
105     ok(r == ERROR_SUCCESS, "Failed to set integer at 0 to 0\n");
106     r = MsiRecordIsNull(h,0);
107     ok(r==0, "new record is null after setting an integer\n");
108     r = MsiRecordDataSize(h,0);
109     ok(r==sizeof(DWORD), "size of integer record is 4\n");
110     r = MsiRecordSetInteger(h, 0, 1);
111     ok(r == ERROR_SUCCESS, "Failed to set integer at 0 to 1\n");
112     r = MsiRecordSetInteger(h, 1, 1);
113     ok(r == ERROR_INVALID_PARAMETER, "set integer at 1\n");
114     r = MsiRecordSetInteger(h, -1, 0);
115     ok(r == ERROR_INVALID_PARAMETER, "set integer at -1\n");
116     r = MsiRecordIsNull(h,0);
117     ok(r==0, "new record is null after setting an integer\n");
118     r = MsiRecordGetInteger(h, 0);
119     ok(r == 1, "failed to get integer\n");
120
121     /* same record, but add a string to it */
122     r = MsiRecordSetString(h, 0, NULL);
123     ok(r == ERROR_SUCCESS, "Failed to set null string at 0\n");
124     r = MsiRecordIsNull(h, 0);
125     ok(r == TRUE, "null string not null field\n");
126     r = MsiRecordSetString(h, 0, "");
127     ok(r == ERROR_SUCCESS, "Failed to set empty string at 0\n");
128     r = MsiRecordIsNull(h, 0);
129     ok(r == TRUE, "null string not null field\n");
130     r = MsiRecordSetString(h,0,str);
131     ok(r == ERROR_SUCCESS, "Failed to set string at 0\n");
132     r = MsiRecordGetInteger(h, 0);
133     ok(r == MSI_NULL_INTEGER, "should get invalid integer\n");
134     r = MsiRecordDataSize(h,0);
135     ok(r==sizeof str-1, "size of string record is strlen\n");
136     buf[0]=0;
137     sz = sizeof buf;
138     r = MsiRecordGetString(h,0,buf,&sz);
139     ok(r == ERROR_SUCCESS, "Failed to get string at 0\n");
140     ok(0==strcmp(buf,str), "MsiRecordGetString returned the wrong string\n");
141     ok(sz == sizeof str-1, "MsiRecordGetString returned the wrong length\n");
142     buf[0]=0;
143     sz = sizeof str - 2;
144     r = MsiRecordGetString(h,0,buf,&sz);
145     ok(r == ERROR_MORE_DATA, "small buffer should yield ERROR_MORE_DATA\n");
146     ok(sz == sizeof str-1, "MsiRecordGetString returned the wrong length\n");
147     ok(0==strncmp(buf,str,sizeof str-3), "MsiRecordGetString returned the wrong string\n");
148     ok(buf[sizeof str - 3]==0, "string wasn't nul terminated\n");
149
150     /* same record, check we can wipe all the data */
151     r = MsiRecordClearData(h);
152     ok(r == ERROR_SUCCESS, "Failed to clear record\n");
153     r = MsiRecordClearData(h);
154     ok(r == ERROR_SUCCESS, "Failed to clear record again\n");
155     r = MsiRecordIsNull(h,0);
156     ok(r, "cleared record wasn't null\n");
157
158     /* same record, try converting strings to integers */
159     i = MsiRecordSetString(h,0,"42");
160     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
161     i = MsiRecordGetInteger(h, 0);
162     ok(i == 42, "should get invalid integer\n");
163     i = MsiRecordSetString(h,0,"-42");
164     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
165     i = MsiRecordGetInteger(h, 0);
166     ok(i == -42, "should get invalid integer\n");
167     i = MsiRecordSetString(h,0," 42");
168     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
169     i = MsiRecordGetInteger(h, 0);
170     ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
171     i = MsiRecordSetString(h,0,"42 ");
172     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
173     i = MsiRecordGetInteger(h, 0);
174     ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
175     i = MsiRecordSetString(h,0,"42.0");
176     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
177     i = MsiRecordGetInteger(h, 0);
178     ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
179     i = MsiRecordSetString(h,0,"0x42");
180     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
181     i = MsiRecordGetInteger(h, 0);
182     ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
183     i = MsiRecordSetString(h,0,"1000000000000000");
184     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
185     i = MsiRecordGetInteger(h, 0);
186     ok(i == -1530494976, "should get truncated integer\n");
187     i = MsiRecordSetString(h,0,"2147483647");
188     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
189     i = MsiRecordGetInteger(h, 0);
190     ok(i == 2147483647, "should get maxint\n");
191     i = MsiRecordSetString(h,0,"-2147483647");
192     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
193     i = MsiRecordGetInteger(h, 0);
194     ok(i == -2147483647, "should get -maxint-1\n");
195     i = MsiRecordSetString(h,0,"4294967297");
196     ok(i == ERROR_SUCCESS, "Failed to set string at 0\n");
197     i = MsiRecordGetInteger(h, 0);
198     ok(i == 1, "should get one\n");
199
200     /* same record, try converting integers to strings */
201     r = MsiRecordSetInteger(h, 0, 32);
202     ok(r == ERROR_SUCCESS, "Failed to set integer at 0 to 32\n");
203     buf[0]=0;
204     sz = sizeof buf;
205     r = MsiRecordGetString(h, 0, buf, &sz);
206     ok(r == ERROR_SUCCESS, "failed to get string from integer\n");
207     ok(0==strcmp(buf,"32"), "failed to get string from integer\n");
208     r = MsiRecordSetInteger(h, 0, -32);
209     ok(r == ERROR_SUCCESS, "Failed to set integer at 0 to 32\n");
210     buf[0]=0;
211     sz = sizeof buf;
212     r = MsiRecordGetString(h, 0, buf, &sz);
213     ok(r == ERROR_SUCCESS, "failed to get string from integer\n");
214     ok(0==strcmp(buf,"-32"), "failed to get string from integer\n");
215
216     /* same record, now try streams */
217     r = MsiRecordSetStream(h, 0, NULL);
218     ok(r == ERROR_INVALID_PARAMETER, "set NULL stream\n");
219     sz = sizeof buf;
220     r = MsiRecordReadStream(h, 0, buf, &sz);
221     ok(r == ERROR_INVALID_DATATYPE, "read non-stream type\n");
222     ok(sz == sizeof buf, "set sz\n");
223     r = MsiRecordDataSize( h, -1);
224     ok(r == 0,"MsiRecordDataSize returned wrong size\n");
225     r = MsiRecordDataSize( h, 0);
226     ok(r == 4,"MsiRecordDataSize returned wrong size\n");
227
228     /* same record, now close it */
229     r = MsiCloseHandle(h);
230     ok(r == ERROR_SUCCESS, "Failed to close handle\n");
231
232     /* now try streams in a new record - need to create a file to play with */
233     r = create_temp_file(filename); 
234     if(!r)
235         return;
236
237     /* streams can't be inserted in field 0 for some reason */
238     h = MsiCreateRecord(2);
239     ok(h, "couldn't create a two field record\n");
240     r = MsiRecordSetStream(h, 0, filename);
241     ok(r == ERROR_INVALID_PARAMETER, "added stream to field 0\n");
242     r = MsiRecordSetStream(h, 1, filename);
243     ok(r == ERROR_SUCCESS, "failed to add stream to record\n");
244     r = MsiRecordReadStream(h, 1, buf, NULL);
245     ok(r == ERROR_INVALID_PARAMETER, "should return error\n");
246     /* http://test.winehq.org/data/200503181000/98_jmelgarejo98casa/msi:record.txt */
247     DeleteFile(filename); /* Windows 98 doesn't like this at all, so don't check return. */
248     r = MsiRecordReadStream(h, 1, NULL, NULL);
249     ok(r == ERROR_INVALID_PARAMETER, "should return error\n");
250     sz = sizeof buf;
251     r = MsiRecordReadStream(h, 1, NULL, &sz);
252     ok(r == ERROR_SUCCESS, "failed to read stream\n");
253     ok(sz==26,"couldn't get size of stream\n");
254     sz = 0;
255     r = MsiRecordReadStream(h, 1, buf, &sz);
256     ok(r == ERROR_SUCCESS, "failed to read stream\n");
257     ok(sz==0,"short read\n");
258     sz = sizeof buf;
259     r = MsiRecordReadStream(h, 1, buf, &sz);
260     ok(r == ERROR_SUCCESS, "failed to read stream\n");
261     ok(sz==sizeof buf,"short read\n");
262     ok(!strncmp(buf,"abcdefghij",10), "read the wrong thing\n");
263     sz = sizeof buf;
264     r = MsiRecordReadStream(h, 1, buf, &sz);
265     ok(r == ERROR_SUCCESS, "failed to read stream\n");
266     ok(sz==sizeof buf,"short read\n");
267     ok(!strncmp(buf,"klmnopqrst",10), "read the wrong thing\n");
268     memset(buf,0,sizeof buf);
269     sz = sizeof buf;
270     r = MsiRecordReadStream(h, 1, buf, &sz);
271     ok(r == ERROR_SUCCESS, "failed to read stream\n");
272     ok(sz==6,"short read\n");
273     ok(!strcmp(buf,"uvwxyz"), "read the wrong thing\n");
274     memset(buf,0,sizeof buf);
275     sz = sizeof buf;
276     r = MsiRecordReadStream(h, 1, buf, &sz);
277     ok(r == ERROR_SUCCESS, "failed to read stream\n");
278     ok(sz==0,"size non-zero at end of stream\n");
279     ok(buf[0]==0, "read something at end of the stream\n");
280     r = MsiRecordSetStream(h, 1, NULL);
281     ok(r == ERROR_SUCCESS, "failed to reset stream\n");
282     sz = 0;
283     r = MsiRecordReadStream(h, 1, NULL, &sz);
284     ok(r == ERROR_SUCCESS, "bytes left wrong after reset\n");
285     ok(sz==26,"couldn't get size of stream\n");
286     r = MsiRecordDataSize(h,1);
287     ok(r == 26,"MsiRecordDataSize returned wrong size\n");
288
289     /* now close the stream record */
290     r = MsiCloseHandle(h);
291     ok(r == ERROR_SUCCESS, "Failed to close handle\n");
292     DeleteFile(filename); /* Delete it for sure, when everything else is closed. */
293 }
294
295 START_TEST(record)
296 {
297     test_msirecord();
298 }