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
|
/*
* 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)
{
GInputStream *in, *in2;
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;
in = libmsi_record_get_stream (h, 0);
ok (!in, "read non-stream type\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");
unlink (filename); /* Windows 98 doesn't like this at all, so don't check return. */
in = libmsi_record_get_stream (h, 1);
ok (in, "failed to get stream\n");
in2 = libmsi_record_get_stream (h, 1);
ok (in2, "failed to get stream\n");
sz = g_input_stream_read (in, buf, sizeof(buf), NULL, NULL);
ok (sz == 10, "failed to read stream\n");
ok (!strncmp (buf, "abcdefghij", 10), "read the wrong thing\n");
sz = g_input_stream_read (in2, buf, sizeof(buf), NULL, NULL);
ok (sz == 10, "failed to read stream\n");
ok (!strncmp (buf, "abcdefghij", 10), "read the wrong thing\n");
sz = g_input_stream_read (in, buf, sizeof(buf), NULL, NULL);
ok (sz == 10, "failed to read stream\n");
ok (!strncmp (buf, "klmnopqrst", 10), "read the wrong thing\n");
sz = g_input_stream_read (in, buf, sizeof(buf), NULL, NULL);
ok (sz == 6, "failed to read stream\n");
ok (!strncmp (buf, "uvwxyz", 6), "read the wrong thing\n");
g_object_unref (in);
sz = g_input_stream_read (in2, buf, sizeof(buf), NULL, NULL);
ok (sz == 10, "failed to read stream\n");
ok (!strncmp (buf, "klmnopqrst", 10), "read the wrong thing\n");
sz = g_seekable_tell (G_SEEKABLE (in2));
ok (sz == 20, "failed to get current position\n");
r = g_seekable_seek (G_SEEKABLE (in2), 0, G_SEEK_END, NULL, NULL);
ok (r, "failed to seek\n");
sz = g_seekable_tell (G_SEEKABLE (in2));
ok (sz == 26, "failed to get current position\n");
r = g_seekable_seek (G_SEEKABLE (in2), 0, G_SEEK_SET, NULL, NULL);
ok (r, "failed to seek\n");
sz = g_seekable_tell (G_SEEKABLE (in2));
ok (sz == 0, "failed to get current position\n");
sz = g_input_stream_read (in2, buf, sizeof(buf), NULL, NULL);
ok (sz == 10, "failed to read stream\n");
ok (!strncmp (buf, "abcdefghij", 10), "read the wrong thing\n");
g_object_unref (in2);
in = g_memory_input_stream_new_from_data ("12345", 5, NULL);
r = libmsi_record_set_stream (h, 1, in, 5, NULL, NULL);
ok (r, "failed to set stream to record\n");
g_object_unref(in);
in = libmsi_record_get_stream (h, 1);
ok (in, "failed to get stream\n");
sz = g_input_stream_read (in, buf, sizeof(buf), NULL, NULL);
ok (sz == 5, "failed to read stream\n");
ok (!strncmp (buf, "12345", 5), "read the wrong thing\n");
g_object_unref(in);
/* 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)
{
GError *error = NULL;
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);
hdb = libmsi_database_new(msifile, LIBMSI_DB_FLAGS_CREATE, NULL, NULL);
ok (hdb, "libmsi_database_open failed\n");
query = "CREATE TABLE `drone` ( "
"`id` INT, `name` CHAR (32), `number` CHAR (32) "
"PRIMARY KEY `id`)";
hview = libmsi_query_new (hdb, query, NULL);
ok (hview, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
r = libmsi_query_execute (hview, 0, NULL);
ok (r, "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')";
hview = libmsi_query_new (hdb, query, NULL);
ok (hview, "libmsi_query failed\n");
r = libmsi_query_execute (hview, 0, NULL);
ok (r, "libmsi_query_execute failed\n");
r = libmsi_query_close (hview, NULL);
ok (r, "libmsi_query_close failed\n");
g_object_unref (hview);
rec = libmsi_database_get_primary_keys (hdb, "drone", &error);
ok (rec, "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);
rec = libmsi_database_get_primary_keys (hdb, "nosuchtable", &error);
g_error_matches(error, LIBMSI_RESULT_ERROR, LIBMSI_RESULT_INVALID_TABLE);
query = "SELECT * FROM `drone` WHERE `id` = 1";
hview = libmsi_query_new (hdb, query, NULL);
ok (hview, "Expected LIBMSI_RESULT_SUCCESS\n");
r = libmsi_query_execute (hview, 0, NULL);
ok (r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r);
rec = libmsi_query_fetch (hview, NULL);
ok (r, "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);
}
int main ()
{
#if !GLIB_CHECK_VERSION(2,35,1)
g_type_init ();
#endif
test_msirecord ();
test_MsiRecordGetString ();
test_MsiRecordGetInteger ();
test_fieldzero ();
}
|