From 0a97c9f43718d34aacc834c60f3ced60e9ce956f Mon Sep 17 00:00:00 2001 From: Marc-André Lureau Date: Tue, 18 Dec 2012 14:35:14 +0100 Subject: record: add set_stream() and get_stream() using GIO Use GInputStream for record stream manipulation --- tests/testdatabase.c | 63 ++++++++++++++++++++++--------------- tests/testrecord.c | 89 ++++++++++++++++++++++++++++------------------------ 2 files changed, 86 insertions(+), 66 deletions(-) (limited to 'tests') diff --git a/tests/testdatabase.c b/tests/testdatabase.c index 58bce0a..381e178 100644 --- a/tests/testdatabase.c +++ b/tests/testdatabase.c @@ -1088,6 +1088,7 @@ static void create_file_data(const char *name, const char *data, unsigned size) static void test_streamtable(void) { GError *error = NULL; + GInputStream *in; LibmsiDatabase *hdb = 0; LibmsiRecord *rec; LibmsiQuery *query; @@ -1233,9 +1234,11 @@ static void test_streamtable(void) size = sizeof(buf); memset(buf, 0, sizeof(buf)); - r = libmsi_record_save_stream( rec, 2, buf, &size ); - ok(r, "Failed to get stream: %d\n", r); + in = libmsi_record_get_stream(rec, 2); + ok(in, "Failed to get stream\n"); + size = g_input_stream_read(in, buf, sizeof(buf), NULL, NULL); ok( g_str_equal(buf, "test.txt\n"), "Expected 'test.txt\\n', got %s\n", buf); + g_object_unref(in); g_object_unref( rec ); libmsi_query_close(query, NULL); @@ -1255,9 +1258,11 @@ static void test_streamtable(void) size = sizeof(buf); memset(buf, 0, sizeof(buf)); - r = libmsi_record_save_stream( rec, 2, buf, &size ); - ok( r, "Failed to get stream: %d\n", r); + in = libmsi_record_get_stream(rec, 2); + ok(in, "Failed to get stream\n"); + size = g_input_stream_read(in, buf, sizeof(buf), NULL, NULL); ok( g_str_equal(buf, "test1.txt\n"), "Expected 'test1.txt\\n', got %s\n", buf); + g_object_unref(in); g_object_unref( rec ); libmsi_query_close(query, NULL); @@ -1297,9 +1302,11 @@ static void test_streamtable(void) size = sizeof(buf); memset(buf, 0, sizeof(buf)); - r = libmsi_record_save_stream( rec, 2, buf, &size ); - ok(r, "Failed to get stream: %d\n", r); + in = libmsi_record_get_stream(rec, 2); + ok(in, "Failed to get stream\n"); + size = g_input_stream_read(in, buf, sizeof(buf), NULL, NULL); todo_wine ok( g_str_equal(buf, "test2.txt\n"), "Expected 'test2.txt\\n', got %s\n", buf); + g_object_unref(in); g_object_unref( rec ); libmsi_query_close(query, NULL); @@ -1325,6 +1332,7 @@ static void test_streamtable(void) static void test_binary(void) { + GInputStream *in; LibmsiDatabase *hdb = 0; LibmsiRecord *rec; char file[256]; @@ -1370,9 +1378,11 @@ static void test_binary(void) size = sizeof(buf); memset( buf, 0, sizeof(buf) ); - r = libmsi_record_save_stream( rec, 2, buf, &size ); - ok(r, "Failed to get stream: %d\n", r ); + in = libmsi_record_get_stream(rec, 2); + ok(in, "Failed to get stream\n"); + size = g_input_stream_read(in, buf, sizeof(buf), NULL, NULL); ok( g_str_equal(buf, "test.txt\n"), "Expected 'test.txt\\n', got %s\n", buf ); + g_object_unref(in); g_object_unref( rec ); @@ -1385,9 +1395,11 @@ static void test_binary(void) size = sizeof(buf); memset( buf, 0, sizeof(buf) ); - r = libmsi_record_save_stream( rec, 3, buf, &size ); - ok(r, "Failed to get stream: %d\n", r ); + in = libmsi_record_get_stream(rec, 3); + ok(in, "Failed to get stream\n"); + size = g_input_stream_read(in, buf, sizeof(buf), NULL, NULL); ok( g_str_equal(buf, "test.txt\n"), "Expected 'test.txt\\n', got %s\n", buf ); + g_object_unref(in); g_object_unref( rec ); @@ -2010,6 +2022,7 @@ static const char bin_import_dat[] = "Name\tData\r\n" static void test_binary_import(void) { + GInputStream *in; LibmsiDatabase *hdb = 0; LibmsiRecord *rec; char file[256]; @@ -2040,11 +2053,12 @@ static void test_binary_import(void) size = sizeof(buf); memset(buf, 0, size); - r = libmsi_record_save_stream(rec, 2, buf, &size); - ok(r, "Failed to get stream: %d\n", r); + in = libmsi_record_get_stream(rec, 2); + ok(in, "Failed to get stream\n"); + size = g_input_stream_read(in, buf, sizeof(buf), NULL, NULL); ok(g_str_equal(buf, "just some words"), "Expected 'just some words', got %s\n", buf); - + g_object_unref(in); g_object_unref(rec); g_object_unref(hdb); @@ -2573,12 +2587,13 @@ static void test_try_transform(void) ok(r == LIBMSI_RESULT_SUCCESS, "select query failed\n"); /* check the contents of the stream */ - sz = sizeof buffer; - r = libmsi_record_save_stream( hrec, 1, buffer, &sz ); - ok(r, "read stream failed\n"); + in = libmsi_record_get_stream(rec, 1); + ok(in, "Failed to get stream\n"); + sz = g_input_stream_read(in, buffer, sizeof(buffer), NULL, NULL); ok(!memcmp(buffer, "naengmyon", 9), "stream data was wrong\n"); ok(sz == 9, "stream data was wrong size\n"); if (hrec) g_object_unref(hrec); + g_object_unref(in); /* check the validity of the table with a deleted row */ hrec = 0; @@ -5819,12 +5834,8 @@ static void test_storages_table(void) check_record_string(hrec, 1, "stgname"); - size = sizeof(buf); - strcpy(buf, "apple"); - r = libmsi_record_save_stream(hrec, 2, buf, &size); - ok(!r, "Expected ERROR_INVALID_DATA, got %d\n", r); - ok(g_str_equal(buf, "apple"), "Expected buf to be unchanged, got %s\n", buf); - ok(size == 0, "Expected 0, got %d\n", size); + in = libmsi_record_get_stream(hrec, 2); + ok(!in, "Expected ERROR_INVALID_DATA\n"); g_object_unref(hrec); @@ -6059,6 +6070,7 @@ static void test_droptable(void) static void test_dbmerge(void) { GError *error = NULL; + GInputStream *in; LibmsiDatabase *hdb; LibmsiDatabase *href; LibmsiQuery *hquery; @@ -6554,11 +6566,12 @@ static void test_dbmerge(void) size = sizeof(buf); memset(buf, 0, sizeof(buf)); - r = libmsi_record_save_stream(hrec, 2, buf, &size); - ok(r, "Expected LIBMSI_RESULT_SUCCESS, got %d\n", r); + in = libmsi_record_get_stream(hrec, 2); + ok(in, "Failed to get stream\n"); + size = g_input_stream_read(in, buf, sizeof(buf), NULL, NULL); ok(g_str_equal(buf, "binary.dat\n"), "Expected \"binary.dat\\n\", got \"%s\"\n", buf); - + g_object_unref(in); g_object_unref(hrec); /* nothing in MergeErrors */ diff --git a/tests/testrecord.c b/tests/testrecord.c index ef9d26d..286c791 100644 --- a/tests/testrecord.c +++ b/tests/testrecord.c @@ -50,6 +50,7 @@ static bool create_temp_file (char *name) static void test_msirecord (void) { + GInputStream *in, *in2; unsigned r, sz; int i; LibmsiRecord *h; @@ -193,9 +194,8 @@ static void test_msirecord (void) 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"); + in = libmsi_record_get_stream (h, 0); + ok (!in, "read non-stream type\n"); /* same record, now close it */ g_object_unref (h); @@ -210,47 +210,54 @@ static void test_msirecord (void) 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"); + + 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 = 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"); + 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"); - 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"); + 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); -- cgit