summaryrefslogtreecommitdiffstats
path: root/tests/testrecord.c
blob: 12989b29c27d8511cb6665660a4411edf79daaf5 (plain)
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
 * 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 r;
    unsigned char buffer[26], i;
    unsigned sz;
    int fd;
    
#ifdef _WIN32
    r = GetTempFileName(".", "msitest",0,name);
    if(!r)
        return r;
    fd = open(name, O_WRONLY);
#else
    strcpy(name, "msitext-XXXXXX.tmp");
    fd = mkstemp(name);
#endif

    if(fd == -1)
        return 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==-1, "field count for invalid record not -1\n");
    r = libmsi_record_is_null(0, 0);
    ok(r==0, "invalid handle not considered to be non-null...\n");
    r = libmsi_record_get_integer(0,0);
    ok(r == MSI_NULL_INTEGER, "got integer from invalid record\n");
    r = libmsi_record_set_int(0,0,0);
    ok(r == LIBMSI_RESULT_INVALID_HANDLE, "libmsi_record_set_int returned wrong error\n");
    r = libmsi_record_set_int(0,-1,0);
    ok(r == LIBMSI_RESULT_INVALID_HANDLE, "libmsi_record_set_int returned wrong error\n");
    h = libmsi_record_create(-1);
    ok(h==0, "created record with -1 elements\n");
    h = libmsi_record_create(0x10000);
    ok(h==0, "created record with 0x10000 elements\n");
    r = libmsi_record_clear_data(0);
    ok(r == LIBMSI_RESULT_INVALID_HANDLE, "libmsi_record_clear_data returned wrong error\n");
    r = libmsi_record_get_field_size(0,0);
    ok(r == 0, "libmsi_record_get_field_size returned wrong error\n");


    /* check behaviour of a record with 0 elements */
    h = libmsi_record_create(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");
    r = libmsi_record_get_field_size(h,0);
    ok(r==0, "size of null record is 0\n");
    sz = sizeof buf;
    strcpy(buf,"x");
    r = libmsi_record_get_string(h, 0, buf, &sz);
    ok(r==LIBMSI_RESULT_SUCCESS, "failed to get null string\n");
    ok(sz==0, "null string too long\n");
    ok(buf[0]==0, "null string not set\n");

    /* same record, but add an integer to it */
    r = libmsi_record_set_int(h, 0, 0);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to set integer at 0 to 0\n");
    r = libmsi_record_is_null(h,0);
    ok(r==0, "new record is null after setting an integer\n");
    r = libmsi_record_get_field_size(h,0);
    ok(r==sizeof(unsigned), "size of integer record is 4\n");
    r = libmsi_record_set_int(h, 0, 1);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to set integer at 0 to 1\n");
    r = libmsi_record_set_int(h, 1, 1);
    ok(r == LIBMSI_RESULT_INVALID_PARAMETER, "set integer at 1\n");
    r = libmsi_record_set_int(h, -1, 0);
    ok(r == LIBMSI_RESULT_INVALID_PARAMETER, "set integer at -1\n");
    r = libmsi_record_is_null(h,0);
    ok(r==0, "new record is null after setting an integer\n");
    r = libmsi_record_get_integer(h, 0);
    ok(r == 1, "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 == LIBMSI_RESULT_SUCCESS, "Failed to set null string at 0\n");
    r = libmsi_record_is_null(h, 0);
    ok(r == true, "null string not null field\n");
    r = libmsi_record_get_field_size(h, 0);
    ok(r == 0, "size of string record is strlen\n");
    buf[0] = 0;
    sz = sizeof buf;
    r = libmsi_record_get_string(h, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to get string at 0\n");
    ok(buf[0] == 0, "libmsi_record_get_string returned the wrong string\n");
    ok(sz == 0, "libmsi_record_get_string returned the wrong length\n");
    r = libmsi_record_set_string(h, 0, "");
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to set empty string at 0\n");
    r = libmsi_record_is_null(h, 0);
    ok(r == true, "null string not null field\n");
    r = libmsi_record_get_field_size(h, 0);
    ok(r == 0, "size of string record is strlen\n");
    buf[0] = 0;
    sz = sizeof buf;
    r = libmsi_record_get_string(h, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to get string at 0\n");
    ok(buf[0] == 0, "libmsi_record_get_string returned the wrong string\n");
    ok(sz == 0, "libmsi_record_get_string returned the wrong length\n");

    /* same record, but add a string to it */
    r = libmsi_record_set_string(h,0,str);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    r = libmsi_record_get_integer(h, 0);
    ok(r == MSI_NULL_INTEGER, "should get invalid integer\n");
    r = libmsi_record_get_field_size(h,0);
    ok(r==sizeof str-1, "size of string record is strlen\n");
    buf[0]=0;
    sz = sizeof buf;
    r = libmsi_record_get_string(h,0,buf,&sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to get string at 0\n");
    ok(0==strcmp(buf,str), "libmsi_record_get_string returned the wrong string\n");
    ok(sz == sizeof str-1, "libmsi_record_get_string returned the wrong length\n");
    buf[0]=0;
    sz = sizeof str - 2;
    r = libmsi_record_get_string(h,0,buf,&sz);
    ok(r == LIBMSI_RESULT_MORE_DATA, "small buffer should yield LIBMSI_RESULT_MORE_DATA\n");
    ok(sz == sizeof str-1, "libmsi_record_get_string returned the wrong length\n");
    ok(0==strncmp(buf,str,sizeof str-3), "libmsi_record_get_string returned the wrong string\n");
    ok(buf[sizeof str - 3]==0, "string wasn't nul terminated\n");

    buf[0]=0;
    sz = sizeof str;
    r = libmsi_record_get_string(h,0,buf,&sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "wrong error\n");
    ok(sz == sizeof str-1, "libmsi_record_get_string returned the wrong length\n");
    ok(0==strcmp(buf,str), "libmsi_record_get_string returned the wrong string\n");


    memset(buf, 0, sizeof buf);
    sz = 5;
    r = libmsi_record_get_string(h,0,buf,&sz);
    ok(r == LIBMSI_RESULT_MORE_DATA, "wrong error\n");
    ok(sz == 5, "libmsi_record_get_string returned the wrong length\n");
    ok(0==memcmp(buf,str,4), "libmsi_record_get_string returned the wrong string\n");

    sz = 0;
    buf[0] = 'x';
    r = libmsi_record_get_string(h,0,buf,&sz);
    ok(r == LIBMSI_RESULT_MORE_DATA, "wrong error\n");
    ok(sz == 5, "libmsi_record_get_string returned the wrong length\n");
    ok('x'==buf[0], "libmsi_record_get_string returned the wrong string\n");

    /* same record, check we can wipe all the data */
    r = libmsi_record_clear_data(h);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to clear record\n");
    r = libmsi_record_clear_data(h);
    ok(r == LIBMSI_RESULT_SUCCESS, "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 == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == 42, "should get invalid integer\n");
    i = libmsi_record_set_string(h,0,"-42");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == -42, "should get invalid integer\n");
    i = libmsi_record_set_string(h,0," 42");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
    i = libmsi_record_set_string(h,0,"42 ");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
    i = libmsi_record_set_string(h,0,"42.0");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
    i = libmsi_record_set_string(h,0,"0x42");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == MSI_NULL_INTEGER, "should get invalid integer\n");
    i = libmsi_record_set_string(h,0,"1000000000000000");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == -1530494976, "should get truncated integer\n");
    i = libmsi_record_set_string(h,0,"2147483647");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == 2147483647, "should get maxint\n");
    i = libmsi_record_set_string(h,0,"-2147483647");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == -2147483647, "should get -maxint-1\n");
    i = libmsi_record_set_string(h,0,"4294967297");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == 1, "should get one\n");
    i = libmsi_record_set_string(h,0,"foo");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == MSI_NULL_INTEGER, "should get zero\n");
    i = libmsi_record_set_string(h,0,"");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == MSI_NULL_INTEGER, "should get zero\n");
    i = libmsi_record_set_string(h,0,"+1");
    ok(i == LIBMSI_RESULT_SUCCESS, "Failed to set string at 0\n");
    i = libmsi_record_get_integer(h, 0);
    ok(i == MSI_NULL_INTEGER, "should get zero\n");

    /* same record, try converting integers to strings */
    r = libmsi_record_set_int(h, 0, 32);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to set integer at 0 to 32\n");
    sz = 1;
    r = libmsi_record_get_string(h, 0, NULL, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "failed to get string from integer\n");
    ok(sz == 2, "length wrong\n");
    buf[0]=0;
    sz = sizeof buf;
    r = libmsi_record_get_string(h, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "failed to get string from integer\n");
    ok(0==strcmp(buf,"32"), "failed to get string from integer\n");
    r = libmsi_record_set_int(h, 0, -32);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to set integer at 0 to 32\n");
    buf[0]=0;
    sz = 1;
    r = libmsi_record_get_string(h, 0, NULL, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "failed to get string from integer\n");
    ok(sz == 3, "length wrong\n");
    sz = sizeof buf;
    r = libmsi_record_get_string(h, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "failed to get string from integer\n");
    ok(0==strcmp(buf,"-32"), "failed to get string from integer\n");
    buf[0]=0;

    /* same record, now try streams */
    r = libmsi_record_load_stream(h, 0, NULL);
    ok(r == LIBMSI_RESULT_INVALID_PARAMETER, "set NULL stream\n");
    sz = sizeof buf;
    r = libmsi_record_save_stream(h, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_INVALID_DATATYPE, "read non-stream type\n");
    ok(sz == sizeof buf, "set sz\n");
    r = libmsi_record_get_field_size( h, -1);
    ok(r == 0,"libmsi_record_get_field_size returned wrong size\n");
    r = libmsi_record_get_field_size( h, 0);
    ok(r == 4,"libmsi_record_get_field_size returned wrong size\n");

    /* same record, now close it */
    r = libmsi_unref(h);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to close handle\n");

    /* now try streams in a new record - need to create a file to play with */
    r = create_temp_file(filename); 
    if(!r)
        return;

    /* streams can't be inserted in field 0 for some reason */
    h = libmsi_record_create(2);
    ok(h, "couldn't create a two field record\n");
    r = libmsi_record_load_stream(h, 0, filename);
    ok(r == LIBMSI_RESULT_INVALID_PARAMETER, "added stream to field 0\n");
    r = libmsi_record_load_stream(h, 1, filename);
    ok(r == LIBMSI_RESULT_SUCCESS, "failed to add stream to record\n");
    r = libmsi_record_save_stream(h, 1, buf, NULL);
    ok(r == LIBMSI_RESULT_INVALID_PARAMETER, "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 == LIBMSI_RESULT_INVALID_PARAMETER, "should return error\n");
    sz = sizeof buf;
    r = libmsi_record_save_stream(h, 1, NULL, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "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 == LIBMSI_RESULT_SUCCESS, "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 == LIBMSI_RESULT_SUCCESS, "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 == LIBMSI_RESULT_SUCCESS, "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 == LIBMSI_RESULT_SUCCESS, "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 == LIBMSI_RESULT_SUCCESS, "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 == LIBMSI_RESULT_SUCCESS, "failed to reset stream\n");
    sz = 0;
    r = libmsi_record_save_stream(h, 1, NULL, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "bytes left wrong after reset\n");
    ok(sz==26,"couldn't get size of stream\n");
    r = libmsi_record_get_field_size(h,1);
    ok(r == 26,"libmsi_record_get_field_size returned wrong size\n");

    /* now close the stream record */
    r = libmsi_unref(h);
    ok(r == LIBMSI_RESULT_SUCCESS, "Failed to close handle\n");
    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_create(2);
    ok(rec != 0, "Expected a valid handle\n");

    sz = sizeof(buf);
    r = libmsi_record_get_string(rec, 1, NULL, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n",r);
    ok(sz == 0, "Expected 0, got %d\n",sz);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 1, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, ""), "Expected \"\", got \"%s\"\n", buf);
    ok(sz == 0, "Expected 0, got %d\n", sz);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 10, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, ""), "Expected \"\", got \"%s\"\n", buf);
    ok(sz == 0, "Expected 0, got %d\n", sz);

    libmsi_unref(rec);

    rec = libmsi_record_create(1);
    ok(rec != 0, "Expected a valid handle\n");

    r = libmsi_record_set_int(rec, 1, 5);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);

    sz = sizeof(buf);
    r = libmsi_record_get_string(rec, 1, NULL, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n",r);
    ok(sz == 1, "Expected 1, got %d\n",sz);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 1, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, "5"), "Expected \"5\", got \"%s\"\n", buf);
    ok(sz == 1, "Expectd 1, got %d\n", sz);

    r = libmsi_record_set_int(rec, 1, -5);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 1, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, "-5"), "Expected \"-5\", got \"%s\"\n", buf);
    ok(sz == 2, "Expectd 2, got %d\n", sz);

    libmsi_unref(rec);
}

static void test_MsiRecordGetInteger(void)
{
    LibmsiRecord *rec;
    int val;
    unsigned r;

    rec = libmsi_record_create(1);
    ok(rec != 0, "Expected a valid handle\n");

    r = libmsi_record_set_string(rec, 1, "5");
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);

    val = libmsi_record_get_integer(rec, 1);
    ok(val == 5, "Expected 5, got %d\n", val);

    r = libmsi_record_set_string(rec, 1, "-5");
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);

    val = libmsi_record_get_integer(rec, 1);
    ok(val == -5, "Expected -5, got %d\n", val);

    r = libmsi_record_set_string(rec, 1, "5apple");
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);

    val = libmsi_record_get_integer(rec, 1);
    ok(val == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", val);

    libmsi_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_create(1);
    ok(rec != 0, "Expected a valid handle\n");

    r = libmsi_record_get_integer(rec, 0);
    ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, ""), "Expected \"\", got \"%s\"\n", buf);
    ok(sz == 0, "Expectd 0, got %d\n", sz);

    r = libmsi_record_is_null(rec, 0);
    ok(r == true, "Expected true, got %d\n", r);

    r = libmsi_record_get_integer(rec, 1);
    ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);

    r = libmsi_record_set_int(rec, 1, 42);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);

    r = libmsi_record_get_integer(rec, 0);
    ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, ""), "Expected \"\", got \"%s\"\n", buf);
    ok(sz == 0, "Expectd 0, got %d\n", sz);

    r = libmsi_record_is_null(rec, 0);
    ok(r == true, "Expected true, got %d\n", r);

    r = libmsi_record_get_integer(rec, 1);
    ok(r == 42, "Expected 42, got %d\n", r);

    r = libmsi_record_set_string(rec, 1, "bologna");
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);

    r = libmsi_record_get_integer(rec, 0);
    ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, ""), "Expected \"\", got \"%s\"\n", buf);
    ok(sz == 0, "Expectd 0, got %d\n", sz);

    r = libmsi_record_is_null(rec, 0);
    ok(r == true, "Expected true, got %d\n", r);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 1, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, "bologna"), "Expected \"bologna\", got \"%s\"\n", buf);
    ok(sz == 7, "Expectd 7, got %d\n", sz);

    libmsi_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);
    ok(r == LIBMSI_RESULT_SUCCESS, "libmsi_query_close failed\n");
    r = libmsi_unref(hview);
    ok(r == LIBMSI_RESULT_SUCCESS, "libmsi_unref failed\n");

    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);
    ok(r == LIBMSI_RESULT_SUCCESS, "libmsi_query_close failed\n");
    r = libmsi_unref(hview);
    ok(r == LIBMSI_RESULT_SUCCESS, "libmsi_unref failed\n");

    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_integer(rec, 0);
    ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r);

    sz = sizeof(buf);
    strcpy(buf, "apple");
    r = libmsi_record_get_string(rec, 0, buf, &sz);
    ok(r == LIBMSI_RESULT_SUCCESS, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
    ok(!strcmp(buf, "drone"), "Expected \"drone\", got \"%s\"\n", buf);
    ok(sz == 5, "Expectd 5, got %d\n", sz);

    r = libmsi_record_is_null(rec, 0);
    ok(r == false, "Expected false, got %d\n", r);

    libmsi_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_integer(rec, 0);
    ok(r != MSI_NULL_INTEGER && r != 0, "Expected non-NULL value, got %d\n", r);

    r = libmsi_record_is_null(rec, 0);
    ok(r == false, "Expected false, got %d\n", r);

    r = libmsi_unref(hview);
    ok(r == LIBMSI_RESULT_SUCCESS, "libmsi_unref failed\n");
    libmsi_unref(rec);
    libmsi_unref(hdb);
    unlink(msifile);
}

void main()
{
    g_type_init();
    test_msirecord();
    test_MsiRecordGetString();
    test_MsiRecordGetInteger();
    test_fieldzero();
}