1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/*
* Copyright (C) 2005 Mike McCormack for CodeWeavers
*
* A test program for MSI records
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <libmsi.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include "test.h"
static const char *msifile = "winetest-record.msi";
static bool create_temp_file (char *name)
{
unsigned char buffer[26], i;
int fd;
strcpy (name, "msitext-XXXXXX.tmp");
fd = g_mkstemp (name);
g_return_val_if_fail (fd != -1, FALSE);
for (i = 0; i < 26; i++)
buffer[i] = i + 'a';
write (fd, buffer, sizeof (buffer));
close (fd);
return TRUE;
}
static void test_msirecord (void)
{
unsigned r, sz;
int i;
LibmsiRecord *h;
char buf[10];
const char str[] = "hello";
char filename[100];
/* check behaviour with an invalid record */
r = libmsi_record_get_field_count (0);
ok (r == 0, "field count for invalid record not -1\n");
r = libmsi_record_is_null (0, 0);
ok (r == true, "invalid handle not considered to be null...\n");
r = libmsi_record_get_int (0, 0);
ok (r == LIBMSI_NULL_INT, "got integer from invalid record\n");
r = libmsi_record_set_int (0, 0, 0);
ok (!r, "libmsi_record_set_int returned wrong error\n");
r = libmsi_record_set_int (0, -1, 0);
ok (!r, "libmsi_record_set_int returned wrong error\n");
r = libmsi_record_clear (0);
ok (!r, "libmsi_record_clear returned wrong error\n");
/* check behaviour of a record with 0 elements */
h = libmsi_record_new (0);
ok (h!=0, "couldn't create record with zero elements\n");
r = libmsi_record_get_field_count (h);
ok (r == 0, "field count should be zero\n");
r = libmsi_record_is_null (h, 0);
ok (r, "new record wasn't null\n");
r = libmsi_record_is_null (h, 1);
ok (r, "out of range record wasn't null\n");
r = libmsi_record_is_null (h, -1);
ok (r, "out of range record wasn't null\n");
check_record_string (h, 0, "");
/* same record, but add an integer to it */
r = libmsi_record_set_int (h, 0, 0);
ok (r, "Failed to set integer at 0 to 0\n");
r = libmsi_record_is_null (h, 0);
ok (r == false, "new record is null after setting an integer\n");
r = libmsi_record_set_int (h, 0, 1);
ok (r, "Failed to set integer at 0 to 1\n");
r = libmsi_record_set_int (h, 1, 1);
ok (!r, "set integer at 1\n");
r = libmsi_record_set_int (h, -1, 0);
ok (!r, "set integer at -1\n");
r = libmsi_record_is_null (h, 0);
ok (r == false, "new record is null after setting an integer\n");
r = libmsi_record_get_int (h, 0);
ok (r, "failed to get integer\n");
/* same record, but add a null or empty string to it */
r = libmsi_record_set_string (h, 0, NULL);
ok (r, "Failed to set null string at 0\n");
r = libmsi_record_is_null (h, 0);
ok (r == true, "null string not null field\n");
check_record_string (h, 0, "");
r = libmsi_record_set_string (h, 0, "");
ok (r, "Failed to set empty string at 0\n");
r = libmsi_record_is_null (h, 0);
ok (r == true, "null string not null field\n");
check_record_string (h, 0, "");
/* same record, but add a string to it */
r = libmsi_record_set_string (h, 0, str);
ok (r, "Failed to set string at 0\n");
r = libmsi_record_get_int (h, 0);
ok (r == LIBMSI_NULL_INT, "should get invalid integer\n");
check_record_string (h, 0, str);
/* same record, check we can wipe all the data */
r = libmsi_record_clear (h);
ok (r, "Failed to clear record\n");
r = libmsi_record_clear (h);
ok (r, "Failed to clear record again\n");
r = libmsi_record_is_null (h, 0);
ok (r, "cleared record wasn't null\n");
/* same record, try converting strings to integers */
i = libmsi_record_set_string (h, 0, "42");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == 42, "should get invalid integer\n");
i = libmsi_record_set_string (h, 0, "-42");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == -42, "should get invalid integer\n");
i = libmsi_record_set_string (h, 0, " 42");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == LIBMSI_NULL_INT, "should get invalid integer\n");
i = libmsi_record_set_string (h, 0, "42 ");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == LIBMSI_NULL_INT, "should get invalid integer\n");
i = libmsi_record_set_string (h, 0, "42.0");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == LIBMSI_NULL_INT, "should get invalid integer\n");
i = libmsi_record_set_string (h, 0, "0x42");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == LIBMSI_NULL_INT, "should get invalid integer\n");
i = libmsi_record_set_string (h, 0, "1000000000000000");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == -1530494976, "should get truncated integer\n");
i = libmsi_record_set_string (h, 0, "2147483647");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == 2147483647, "should get maxint\n");
i = libmsi_record_set_string (h, 0, "-2147483647");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == -2147483647, "should get -maxint-1\n");
i = libmsi_record_set_string (h, 0, "4294967297");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == 1, "should get one\n");
i = libmsi_record_set_string (h, 0, "foo");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == LIBMSI_NULL_INT, "should get zero\n");
i = libmsi_record_set_string (h, 0, "");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == LIBMSI_NULL_INT, "should get zero\n");
i = libmsi_record_set_string (h, 0, "+1");
ok (i, "Failed to set string at 0\n");
i = libmsi_record_get_int (h, 0);
ok (i == LIBMSI_NULL_INT, "should get zero\n");
/* same record, try converting integers to strings */
r = libmsi_record_set_int (h, 0, 32);
ok (r, "Failed to set integer at 0 to 32\n");
check_record_string (h, 0, "32");
r = libmsi_record_set_int (h, 0, -32);
ok (r, "Failed to set integer at 0 to 32\n");
check_record_string (h, 0, "-32");
/* same record, now try streams */
r = libmsi_record_load_stream (h, 0, NULL);
ok (!r, "set NULL stream\n");
sz = sizeof buf;
r = libmsi_record_save_stream (h, 0, buf, &sz);
ok (!r, "read non-stream type\n");
ok (sz == sizeof buf, "set sz\n");
/* same record, now close it */
g_object_unref (h);
/* now try streams in a new record - need to create a file to play with */
g_assert (create_temp_file (filename));
/* streams can't be inserted in field 0 for some reason */
h = libmsi_record_new (2);
ok (h, "couldn't create a two field record\n");
r = libmsi_record_load_stream (h, 0, filename);
ok (!r, "added stream to field 0\n");
r = libmsi_record_load_stream (h, 1, filename);
ok (r, "failed to add stream to record\n");
r = libmsi_record_save_stream (h, 1, buf, NULL);
ok (!r, "should return error\n");
unlink (filename); /* Windows 98 doesn't like this at all, so don't check return. */
r = libmsi_record_save_stream (h, 1, NULL, NULL);
ok (!r, "should return error\n");
sz = sizeof buf;
r = libmsi_record_save_stream (h, 1, NULL, &sz);
ok (r, "failed to read stream\n");
ok (sz == 26, "couldn't get size of stream\n");
sz = 0;
r = libmsi_record_save_stream (h, 1, buf, &sz);
ok (r, "failed to read stream\n");
ok (sz == 0, "short read\n");
sz = sizeof buf;
r = libmsi_record_save_stream (h, 1, buf, &sz);
ok (r, "failed to read stream\n");
ok (sz == sizeof buf, "short read\n");
ok (!strncmp (buf, "abcdefghij", 10), "read the wrong thing\n");
sz = sizeof buf;
r = libmsi_record_save_stream (h, 1, buf, &sz);
ok (r, "failed to read stream\n");
ok (sz == sizeof buf, "short read\n");
ok (!strncmp (buf, "klmnopqrst", 10), "read the wrong thing\n");
memset (buf, 0, sizeof buf);
sz = sizeof buf;
r = libmsi_record_save_stream (h, 1, buf, &sz);
ok (r, "failed to read stream\n");
ok (sz == 6, "short read\n");
ok (!strcmp (buf, "uvwxyz"), "read the wrong thing\n");
memset (buf, 0, sizeof buf);
sz = sizeof buf;
r = libmsi_record_save_stream (h, 1, buf, &sz);
ok (r, "failed to read stream\n");
ok (sz == 0, "size non-zero at end of stream\n");
ok (buf[0] == 0, "read something at end of the stream\n");
r = libmsi_record_load_stream (h, 1, NULL);
ok (r, "failed to reset stream\n");
sz = 0;
r = libmsi_record_save_stream (h, 1, NULL, &sz);
ok (r, "bytes left wrong after reset\n");
ok (sz == 26, "couldn't get size of stream\n");
/* now close the stream record */
g_object_unref (h);
unlink (filename); /* Delete it for sure, when everything else is closed. */
}
static void test_MsiRecordGetString (void)
{
LibmsiRecord *rec;
char buf[100];
unsigned sz;
unsigned r;
rec = libmsi_record_new (2);
ok (rec != 0, "Expected a valid handle\n");
check_record_string(rec, 1, "");
g_object_unref (rec);
rec = libmsi_record_new (1);
ok (rec != 0, "Expected a valid handle\n");
r = libmsi_record_set_int (rec, 1, 5);
ok (r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
check_record_string (rec, 1, "5");
r = libmsi_record_set_int (rec, 1, -5);
ok (r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
check_record_string (rec, 1, "-5");
g_object_unref (rec);
}
static void test_MsiRecordGetInteger (void)
{
LibmsiRecord *rec;
int val;
unsigned r;
rec = libmsi_record_new (1);
ok (rec != 0, "Expected a valid handle\n");
r = libmsi_record_set_string (rec, 1, "5");
ok (r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
val = libmsi_record_get_int (rec, 1);
ok (val == 5, "Expected 5, got %d\n", val);
r = libmsi_record_set_string (rec, 1, "-5");
ok (r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
val = libmsi_record_get_int (rec, 1);
ok (val == -5, "Expected -5, got %d\n", val);
r = libmsi_record_set_string (rec, 1, "5apple");
ok (r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
val = libmsi_record_get_int (rec, 1);
ok (val == LIBMSI_NULL_INT, "Expected LIBMSI_NULL_INT, got %d\n", val);
g_object_unref (rec);
}
static void test_fieldzero (void)
{
LibmsiDatabase *hdb;
LibmsiQuery *hview;
LibmsiRecord *rec;
char buf[100];
const char *query;
unsigned sz;
unsigned r;
rec = libmsi_record_new (1);
ok (rec != 0, "Expected a valid handle\n");
r = libmsi_record_get_int (rec, 0);
ok (r == LIBMSI_NULL_INT, "Expected LIBMSI_NULL_INT, got %d\n", r);
check_record_string (rec, 0, "");
r = libmsi_record_is_null (rec, 0);
ok (r == true, "Expected true, got %d\n", r);
r = libmsi_record_get_int (rec, 1);
ok (r == LIBMSI_NULL_INT, "Expected LIBMSI_NULL_INT, got %d\n", r);
r = libmsi_record_set_int (rec, 1, 42);
ok (r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_record_get_int (rec, 0);
ok (r == LIBMSI_NULL_INT, "Expected LIBMSI_NULL_INT, got %d\n", r);
check_record_string (rec, 0, "");
r = libmsi_record_is_null (rec, 0);
ok (r == true, "Expected true, got %d\n", r);
r = libmsi_record_get_int (rec, 1);
ok (r == 42, "Expected 42, got %d\n", r);
r = libmsi_record_set_string (rec, 1, "bologna");
ok (r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_record_get_int (rec, 0);
ok (r == LIBMSI_NULL_INT, "Expected LIBMSI_NULL_INT, got %d\n", r);
check_record_string (rec, 0, "");
r = libmsi_record_is_null (rec, 0);
ok (r == true, "Expected true, got %d\n", r);
check_record_string (rec, 1, "bologna");
g_object_unref (rec);
r = libmsi_database_open (msifile, LIBMSI_DB_OPEN_CREATE, &hdb);
ok (r == LIBMSI_RESULT_SUCCESS, "libmsi_database_open failed\n");
query = "CREATE TABLE `drone` ( "
"`id` INT, `name` CHAR (32), `number` CHAR (32) "
"PRIMARY KEY `id`)";
r = libmsi_database_open_query (hdb, query, &hview);
ok (r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_query_execute (hview, 0);
ok (r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_query_close (hview, NULL);
ok (r, "libmsi_query_close failed\n");
g_object_unref (hview);
query = "INSERT INTO `drone` ( `id`, `name`, `number` )"
"VALUES ('1', 'Abe', '8675309')";
r = libmsi_database_open_query (hdb, query, &hview);
ok (r == LIBMSI_RESULT_SUCCESS, "libmsi_database_open_query failed\n");
r = libmsi_query_execute (hview, 0);
ok (r == LIBMSI_RESULT_SUCCESS, "libmsi_query_execute failed\n");
r = libmsi_query_close (hview, NULL);
ok (r, "libmsi_query_close failed\n");
g_object_unref (hview);
rec = NULL;
r = libmsi_database_get_primary_keys (hdb, "drone", &rec);
ok (r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_record_get_int (rec, 0);
ok (r == LIBMSI_NULL_INT, "Expected LIBMSI_NULL_INT, got %d\n", r);
check_record_string (rec, 0, "drone");
r = libmsi_record_is_null (rec, 0);
ok (r == false, "Expected false, got %d\n", r);
g_object_unref (rec);
r = libmsi_database_get_primary_keys (hdb, "nosuchtable", &rec);
ok (r == LIBMSI_RESULT_INVALID_TABLE, "Expected LIBMSI_RESULT_INVALID_TABLE, got %d\n", r);
query = "SELECT * FROM `drone` WHERE `id` = 1";
r = libmsi_database_open_query (hdb, query, &hview);
ok (r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_query_execute (hview, 0);
ok (r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_query_fetch (hview, &rec);
ok (r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_record_get_int (rec, 0);
ok (r == LIBMSI_NULL_INT, "Expected NULL value, got %d\n", r);
r = libmsi_record_is_null (rec, 0);
ok (r == true, "Expected true, got %d\n", r);
g_object_unref (hview);
g_object_unref (rec);
g_object_unref (hdb);
unlink (msifile);
}
void main ()
{
g_type_init ();
test_msirecord ();
test_MsiRecordGetString ();
test_MsiRecordGetInteger ();
test_fieldzero ();
}
|