[evolution-data-server] Camel: Port mime-filter tests to use GIO streams.
- From: Matthew Barnes <mbarnes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server] Camel: Port mime-filter tests to use GIO streams.
- Date: Tue, 1 Oct 2013 20:06:44 +0000 (UTC)
commit e47406458b292161da2b93832e29f3a2b35b4d1e
Author: Matthew Barnes <mbarnes redhat com>
Date: Tue Oct 1 12:08:14 2013 -0400
Camel: Port mime-filter tests to use GIO streams.
camel/tests/mime-filter/test-charset.c | 244 ++++++++++++++++++------------
camel/tests/mime-filter/test-crlf.c | 254 +++++++++++++++++---------------
camel/tests/mime-filter/test-tohtml.c | 157 +++++++++++++-------
camel/tests/mime-filter/test1.c | 89 +++++++-----
4 files changed, 441 insertions(+), 303 deletions(-)
---
diff --git a/camel/tests/mime-filter/test-charset.c b/camel/tests/mime-filter/test-charset.c
index 9934138..27707b0 100644
--- a/camel/tests/mime-filter/test-charset.c
+++ b/camel/tests/mime-filter/test-charset.c
@@ -15,131 +15,181 @@
#define CHUNK_SIZE 4096
-gint
-main (gint argc,
- gchar **argv)
+static void
+test_case (const gchar *basename)
{
+ GFileInputStream *source_stream;
+ GFileInputStream *correct_stream;
+ GInputStream *filter_stream;
+ CamelMimeFilter *filter;
+ GFile *file;
gssize comp_progress, comp_correct_chunk, comp_filter_chunk;
gchar comp_correct[CHUNK_SIZE], comp_filter[CHUNK_SIZE];
- CamelStream *source;
- CamelStream *correct;
- CamelStream *stream;
- CamelMimeFilter *f;
- struct dirent *dent;
+ gchar *filename, *charset, *work;
+ const gchar *ext;
gint i, test = 0;
- DIR *dir;
-
- camel_test_init (argc, argv);
+ GError *local_error = NULL;
- dir = opendir (SOURCEDIR);
- if (!dir)
- return 1;
+ ext = strrchr (basename, '.');
+ if (ext == NULL)
+ return;
- while ((dent = readdir (dir))) {
- gchar *infile, *outfile, *charset, *work;
- const gchar *ext;
+ if (!g_str_has_prefix (basename, "charset-"))
+ return;
- ext = strrchr (dent->d_name, '.');
- if (!(!strncmp (dent->d_name, "charset-", 8) && ext && !strcmp (ext, ".in")))
- continue;
+ if (!g_str_has_suffix (basename, ".in"))
+ return;
- work = g_strdup_printf ("Charset filter, test case %d (%s)", test++, dent->d_name);
- camel_test_start (work);
- g_free (work);
+ work = g_strdup_printf (
+ "Charset filter, test case %d (%s)", test++, basename);
+ camel_test_start (work);
+ g_free (work);
- infile = g_strdup_printf ("%s/%s", SOURCEDIR, dent->d_name);
- if (!(source = camel_stream_fs_new_with_name (infile, 0, O_RDONLY, NULL))) {
- camel_test_fail ("Failed to open input case in \"%s\"", infile);
- g_free (outfile);
- continue;
- }
- g_free (infile);
+ filename = g_strdup_printf ("%s/%s", SOURCEDIR, basename);
- outfile = g_strdup_printf ("%s/%.*s.out", SOURCEDIR, ext - dent->d_name, dent->d_name);
+ file = g_file_new_for_path (filename);
+ source_stream = g_file_read (file, NULL, &local_error);
+ g_object_unref (file);
- if (!(correct = camel_stream_fs_new_with_name (outfile, 0, O_RDONLY, NULL))) {
- camel_test_fail ("Failed to open correct output in \"%s\"", outfile);
- g_free (outfile);
- continue;
- }
- g_free (outfile);
+ /* Sanity check. */
+ g_warn_if_fail (
+ ((source_stream != NULL) && (local_error == NULL)) ||
+ ((source_stream == NULL) && (local_error != NULL)));
- if (!(stream = camel_stream_filter_new (CAMEL_STREAM (source)))) {
- camel_test_fail ("Couldn't create CamelStreamFilter??");
- continue;
- }
+ if (local_error != NULL) {
+ camel_test_fail (
+ "Failed to open input case in \"%s\": %s",
+ filename, local_error->message);
+ g_error_free (local_error);
+ g_free (filename);
+ return;
+ }
+ g_free (filename);
+
+ filename = g_strdup_printf (
+ "%s/%.*s.out", SOURCEDIR, ext - basename, basename);
+
+ file = g_file_new_for_path (filename);
+ correct_stream = g_file_read (file, NULL, &local_error);
+ g_object_unref (file);
+
+ /* Sanity check. */
+ g_warn_if_fail (
+ ((correct_stream != NULL) && (local_error == NULL)) ||
+ ((correct_stream == NULL) && (local_error != NULL)));
+
+ if (local_error != NULL) {
+ camel_test_fail (
+ "Failed to open correct output in \"%s\": %s",
+ filename, local_error->message);
+ g_error_free (local_error);
+ g_free (filename);
+ return;
+ }
+ g_free (filename);
- charset = g_strdup (dent->d_name + 8);
- ext = strchr (charset, '.');
- *((gchar *) ext) = '\0';
+ charset = g_strdup (basename + 8);
+ ext = strchr (charset, '.');
+ *((gchar *) ext) = '\0';
- if (!(f = camel_mime_filter_charset_new (charset, "UTF-8"))) {
- camel_test_fail ("Couldn't create CamelMimeFilterCharset??");
- g_free (charset);
- continue;
- }
+ filter = camel_mime_filter_charset_new (charset, "UTF-8");
+ if (filter == NULL) {
+ camel_test_fail ("Couldn't create CamelMimeFilterCharset??");
g_free (charset);
+ return;
+ }
+ filter_stream = camel_filter_input_stream_new (
+ G_INPUT_STREAM (source_stream), filter);
+ g_clear_object (&filter);
+
+ g_free (charset);
+
+ camel_test_push ("Running filter and comparing to correct result");
+
+ comp_progress = 0;
+
+ while (1) {
+ comp_correct_chunk = g_input_stream_read (
+ G_INPUT_STREAM (correct_stream),
+ comp_correct, CHUNK_SIZE, NULL, NULL);
+ comp_filter_chunk = 0;
+
+ if (comp_correct_chunk == 0)
+ break;
+
+ while (comp_filter_chunk < comp_correct_chunk) {
+ gssize delta;
+
+ delta = g_input_stream_read (
+ filter_stream,
+ comp_filter + comp_filter_chunk,
+ CHUNK_SIZE - comp_filter_chunk,
+ NULL, NULL);
+
+ if (delta == 0) {
+ camel_test_fail (
+ "Chunks are different sizes: "
+ "correct is %d, "
+ "filter is %d, "
+ "%d bytes into stream",
+ comp_correct_chunk,
+ comp_filter_chunk,
+ comp_progress);
+ }
- camel_stream_filter_add (CAMEL_STREAM_FILTER (stream), f);
- g_object_unref (f);
-
- camel_test_push ("Running filter and comparing to correct result");
-
- comp_progress = 0;
-
- while (1) {
- comp_correct_chunk = camel_stream_read (
- correct, comp_correct,
- CHUNK_SIZE, NULL, NULL);
- comp_filter_chunk = 0;
+ comp_filter_chunk += delta;
+ }
- if (comp_correct_chunk == 0)
- break;
+ for (i = 0; i < comp_filter_chunk; i++) {
+ if (comp_correct[i] != comp_filter[i]) {
+ camel_test_fail ("Difference: correct is %c, filter is %c, "
+ "%d bytes into stream",
+ comp_correct[i],
+ comp_filter[i],
+ comp_progress + i);
+ }
+ }
- while (comp_filter_chunk < comp_correct_chunk) {
- gssize delta;
+ comp_progress += comp_filter_chunk;
+ }
- delta = camel_stream_read (
- stream,
- comp_filter + comp_filter_chunk,
- CHUNK_SIZE - comp_filter_chunk,
- NULL, NULL);
+ camel_test_pull ();
- if (delta == 0) {
- camel_test_fail ("Chunks are different sizes: correct is %d, "
- "filter is %d, %d bytes into stream",
- comp_correct_chunk, comp_filter_chunk, comp_progress);
- }
+ camel_test_push ("Cleaning up");
+ g_object_unref (correct_stream);
+ g_object_unref (source_stream);
+ g_object_unref (filter_stream);
+ camel_test_pull ();
- comp_filter_chunk += delta;
- }
+ camel_test_end ();
+}
- for (i = 0; i < comp_filter_chunk; i++) {
- if (comp_correct[i] != comp_filter[i]) {
- camel_test_fail ("Difference: correct is %c, filter is %c, "
- "%d bytes into stream",
- comp_correct[i],
- comp_filter[i],
- comp_progress + i);
- }
- }
+gint
+main (gint argc,
+ gchar **argv)
+{
+ GDir *dir;
+ const gchar *basename;
+ GError *local_error = NULL;
- comp_progress += comp_filter_chunk;
- }
+ camel_test_init (argc, argv);
- camel_test_pull ();
+ dir = g_dir_open (SOURCEDIR, 0, &local_error);
- /* inefficient */
- camel_test_push ("Cleaning up");
- g_object_unref (stream);
- g_object_unref (correct);
- g_object_unref (source);
- camel_test_pull ();
+ /* Sanity check. */
+ g_warn_if_fail (
+ ((dir != NULL) && (local_error == NULL)) ||
+ ((dir == NULL) && (local_error != NULL)));
- camel_test_end ();
+ if (local_error != NULL) {
+ g_error ("%s", local_error->message);
+ g_assert_not_reached ();
}
- closedir (dir);
+ while ((basename = g_dir_read_name (dir)) != NULL)
+ test_case (basename);
+
+ g_dir_close (dir);
return 0;
}
diff --git a/camel/tests/mime-filter/test-crlf.c b/camel/tests/mime-filter/test-crlf.c
index 5bc1b6d..17d0eef 100644
--- a/camel/tests/mime-filter/test-crlf.c
+++ b/camel/tests/mime-filter/test-crlf.c
@@ -11,7 +11,6 @@
#define d(x)
-#define NUM_CASES 1
#define CHUNK_SIZE 4096
enum {
@@ -20,142 +19,165 @@ enum {
CRLF_DONE
};
-gint
-main (gint argc,
- gchar **argv)
+static void
+test_case (gint test_num)
{
- CamelStream *source;
- CamelStream *correct;
- CamelStream *stream;
- CamelMimeFilter *sh;
- gchar *work;
- gint i;
+ GFileInputStream *source_stream;
+ GFileInputStream *correct_stream;
+ GInputStream *filter_stream;
+ CamelMimeFilter *filter;
+ CamelMimeFilterCRLFDirection direction;
+ GFile *file;
gssize comp_progress, comp_correct_chunk, comp_filter_chunk;
gint comp_i;
gchar comp_correct[CHUNK_SIZE], comp_filter[CHUNK_SIZE];
+ gchar *infile = NULL, *outfile = NULL;
+ GError *local_error = NULL;
+
+ switch (test_num) {
+ case CRLF_ENCODE:
+ camel_test_push ("Test of the encoder");
+ direction = CAMEL_MIME_FILTER_CRLF_ENCODE;
+ infile = g_strdup_printf ("%s/crlf-%d.in", SOURCEDIR, 1);
+ outfile = g_strdup_printf ("%s/crlf-%d.out", SOURCEDIR, 1);
+ break;
+ case CRLF_DECODE:
+ camel_test_push ("Test of the decoder");
+ direction = CAMEL_MIME_FILTER_CRLF_DECODE;
+ infile = g_strdup_printf ("%s/crlf-%d.out", SOURCEDIR, 1);
+ outfile = g_strdup_printf ("%s/crlf-%d.in", SOURCEDIR, 1);
+ break;
+ default:
+ break;
+ }
- camel_test_init (argc, argv);
+ camel_test_push ("Initializing objects");
- for (i = 0; i < NUM_CASES; i++) {
- gint j;
-
- work = g_strdup_printf ("CRLF/DOT filter, test case %d", i);
- camel_test_start (work);
- g_free (work);
-
- for (j = CRLF_ENCODE; j < CRLF_DONE; j++) {
- CamelMimeFilterCRLFDirection direction;
- gchar *infile = NULL, *outfile = NULL;
-
- switch (j) {
- case CRLF_ENCODE:
- camel_test_push ("Test of the encoder");
- direction = CAMEL_MIME_FILTER_CRLF_ENCODE;
- infile = g_strdup_printf ("%s/crlf-%d.in", SOURCEDIR, i + 1);
- outfile = g_strdup_printf ("%s/crlf-%d.out", SOURCEDIR, i + 1);
- break;
- case CRLF_DECODE:
- camel_test_push ("Test of the decoder");
- direction = CAMEL_MIME_FILTER_CRLF_DECODE;
- infile = g_strdup_printf ("%s/crlf-%d.out", SOURCEDIR, i + 1);
- outfile = g_strdup_printf ("%s/crlf-%d.in", SOURCEDIR, i + 1);
- break;
- default:
- break;
- }
+ file = g_file_new_for_path (infile);
+ source_stream = g_file_read (file, NULL, &local_error);
+ g_object_unref (file);
- camel_test_push ("Initializing objects");
- source = camel_stream_fs_new_with_name (infile, 0, O_RDONLY, NULL);
- if (!source) {
- camel_test_fail ("Failed to open input case in \"%s\"", infile);
- g_free (infile);
- continue;
- }
- g_free (infile);
+ /* Sanity check. */
+ g_warn_if_fail (
+ ((source_stream != NULL) && (local_error == NULL)) ||
+ ((source_stream == NULL) && (local_error != NULL)));
- correct = camel_stream_fs_new_with_name (outfile, 0, O_RDONLY, NULL);
- if (!correct) {
- camel_test_fail ("Failed to open correct output in \"%s\"", outfile);
- g_free (outfile);
- continue;
+ if (local_error != NULL) {
+ camel_test_fail (
+ "Failed to open input case in \"%s\": %s",
+ infile, local_error->message);
+ g_free (infile);
+ return;
+ }
+ g_free (infile);
+
+ file = g_file_new_for_path (outfile);
+ correct_stream = g_file_read (file, NULL, &local_error);
+ g_object_unref (file);
+
+ /* Sanity check. */
+ g_warn_if_fail (
+ ((correct_stream != NULL) && (local_error == NULL)) ||
+ ((correct_stream == NULL) && (local_error != NULL)));
+
+ if (local_error != NULL) {
+ camel_test_fail (
+ "Failed to open correct output in \"%s\": %s",
+ outfile, local_error->message);
+ g_free (outfile);
+ return;
+ }
+ g_free (outfile);
+
+ filter = camel_mime_filter_crlf_new (
+ direction, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS);
+ filter_stream = camel_filter_input_stream_new (
+ G_INPUT_STREAM (source_stream), filter);
+ g_object_unref (filter);
+
+ camel_test_pull ();
+
+ camel_test_push ("Running filter and comparing to correct result");
+
+ comp_progress = 0;
+
+ while (1) {
+ comp_correct_chunk = g_input_stream_read (
+ G_INPUT_STREAM (correct_stream),
+ comp_correct, CHUNK_SIZE, NULL, NULL);
+ comp_filter_chunk = 0;
+
+ if (comp_correct_chunk == 0)
+ break;
+
+ while (comp_filter_chunk < comp_correct_chunk) {
+ gssize delta;
+
+ delta = g_input_stream_read (
+ filter_stream,
+ comp_filter + comp_filter_chunk,
+ CHUNK_SIZE - comp_filter_chunk,
+ NULL, NULL);
+
+ if (delta == 0) {
+ camel_test_fail (
+ "Chunks are different sizes: "
+ "correct is %d, "
+ "filter is %d, "
+ "%d bytes into stream",
+ comp_correct_chunk,
+ comp_filter_chunk,
+ comp_progress);
}
- g_free (outfile);
- stream = camel_stream_filter_new (source);
- if (!stream) {
- camel_test_fail ("Couldn't create CamelStreamFilter??");
- continue;
- }
+ comp_filter_chunk += delta;
+ }
- sh = camel_mime_filter_crlf_new (direction, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS);
- if (!sh) {
- camel_test_fail ("Couldn't create CamelMimeFilterCrlf??");
- continue;
+ for (comp_i = 0; comp_i < comp_filter_chunk; comp_i++) {
+ if (comp_correct[comp_i] != comp_filter[comp_i]) {
+ camel_test_fail (
+ "Difference: "
+ "correct is %c, "
+ "filter is %c, "
+ "%d bytes into stream",
+ comp_correct[comp_i],
+ comp_filter[comp_i],
+ comp_progress + comp_i);
}
+ }
- camel_stream_filter_add (
- CAMEL_STREAM_FILTER (stream), sh);
- camel_test_pull ();
-
- camel_test_push ("Running filter and comparing to correct result");
-
- comp_progress = 0;
-
- while (1) {
- comp_correct_chunk = camel_stream_read (
- correct, comp_correct,
- CHUNK_SIZE, NULL, NULL);
- comp_filter_chunk = 0;
-
- if (comp_correct_chunk == 0)
- break;
-
- while (comp_filter_chunk < comp_correct_chunk) {
- gssize delta;
-
- delta = camel_stream_read (
- stream,
- comp_filter + comp_filter_chunk,
- CHUNK_SIZE - comp_filter_chunk,
- NULL, NULL);
+ comp_progress += comp_filter_chunk;
+ }
- if (delta == 0) {
- camel_test_fail ("Chunks are different sizes: correct is %d, "
- "filter is %d, %d bytes into stream",
- comp_correct_chunk, comp_filter_chunk, comp_progress);
- }
+ camel_test_pull ();
- comp_filter_chunk += delta;
- }
+ /* inefficient */
+ camel_test_push ("Cleaning up");
+ g_object_unref (correct_stream);
+ g_object_unref (source_stream);
+ g_object_unref (filter_stream);
+ camel_test_pull ();
- for (comp_i = 0; comp_i < comp_filter_chunk; comp_i++) {
- if (comp_correct[comp_i] != comp_filter[comp_i]) {
- camel_test_fail ("Difference: correct is %c, filter is %c, "
- "%d bytes into stream",
- comp_correct[comp_i],
- comp_filter[comp_i],
- comp_progress + comp_i);
- }
- }
+ camel_test_pull ();
+}
- comp_progress += comp_filter_chunk;
- }
+gint
+main (gint argc,
+ gchar **argv)
+{
+ gchar *work;
+ gint ii;
- camel_test_pull ();
+ camel_test_init (argc, argv);
- /* inefficient */
- camel_test_push ("Cleaning up");
- g_object_unref (stream);
- g_object_unref (correct);
- g_object_unref (source);
- g_object_unref (sh);
- camel_test_pull ();
+ work = g_strdup_printf ("CRLF/DOT filter, test case %d", 0);
+ camel_test_start (work);
+ g_free (work);
- camel_test_pull ();
- }
+ for (ii = CRLF_ENCODE; ii < CRLF_DONE; ii++)
+ test_case (ii);
- camel_test_end ();
- }
+ camel_test_end ();
return 0;
}
diff --git a/camel/tests/mime-filter/test-tohtml.c b/camel/tests/mime-filter/test-tohtml.c
index cb9b2f3..27b9fdd 100644
--- a/camel/tests/mime-filter/test-tohtml.c
+++ b/camel/tests/mime-filter/test-tohtml.c
@@ -19,56 +19,79 @@
#define CHUNK_SIZE 4096
static void
-test_filter (CamelMimeFilter *f,
- const gchar *inname,
- const gchar *outname)
+test_filter (CamelMimeFilter *filter,
+ GFile *infile,
+ GFile *outfile)
{
- CamelStream *in, *out;
- CamelStream *indisk, *outdisk, *filter;
- GByteArray *byte_array_in;
- GByteArray *byte_array_out;
- gint id;
-
- camel_test_push ("Data file '%s'", inname);
+ GFileInputStream *indisk;
+ GFileInputStream *outdisk;
+ GOutputStream *in;
+ GOutputStream *out;
+ GInputStream *in_filter_stream;
+ GOutputStream *out_filter_stream;
+ gchar *in_data;
+ gsize in_size;
+ gchar *out_data;
+ gsize out_size;
camel_test_push ("setup");
- indisk = camel_stream_fs_new_with_name (inname, O_RDONLY, 0, NULL);
- check (indisk);
- outdisk = camel_stream_fs_new_with_name (outname, O_RDONLY, 0, NULL);
- check (outdisk);
+ indisk = g_file_read (infile, NULL, NULL);
+ check (indisk != NULL);
+ outdisk = g_file_read (outfile, NULL, NULL);
+ check (outdisk != NULL);
- byte_array_out = g_byte_array_new ();
- out = camel_stream_mem_new_with_byte_array (byte_array_out);
- check (camel_stream_write_to_stream (outdisk, out, NULL, NULL) > 0);
+ out = g_memory_output_stream_new_resizable ();
+ check (g_output_stream_splice (
+ out, G_INPUT_STREAM (outdisk),
+ G_OUTPUT_STREAM_SPLICE_NONE, NULL, NULL) > 0);
camel_test_pull ();
camel_test_push ("reading through filter stream");
- byte_array_in = g_byte_array_new ();
- in = camel_stream_mem_new_with_byte_array (byte_array_in);
+ in = g_memory_output_stream_new_resizable ();
- filter = camel_stream_filter_new (indisk);
- check_count (indisk, 2);
- id = camel_stream_filter_add ((CamelStreamFilter *) filter, f);
- check_count (f, 2);
+ in_filter_stream = camel_filter_input_stream_new (
+ G_INPUT_STREAM (indisk), filter);
+
+ /* Leave the base stream open so we can re-read it. */
+ g_filter_input_stream_set_close_base_stream (
+ G_FILTER_INPUT_STREAM (in_filter_stream), FALSE);
- check (camel_stream_write_to_stream (filter, in, NULL, NULL) > 0);
- check_msg (byte_array_in->len == byte_array_out->len
- && memcmp (byte_array_in->data, byte_array_out->data, byte_array_in->len) == 0,
- "Buffer content mismatch, %d != %d, in = '%.*s' != out = '%.*s'", byte_array_in->len,
byte_array_out->len,
- byte_array_in->len, byte_array_in->data, byte_array_out->len, byte_array_out->data);
+ check_count (indisk, 2);
+ check_count (filter, 2);
+
+ check (g_output_stream_splice (
+ in, in_filter_stream,
+ G_OUTPUT_STREAM_SPLICE_NONE, NULL, NULL) > 0);
+
+ in_data = g_memory_output_stream_get_data (
+ G_MEMORY_OUTPUT_STREAM (in));
+ in_size = g_memory_output_stream_get_data_size (
+ G_MEMORY_OUTPUT_STREAM (in));
+
+ out_data = g_memory_output_stream_get_data (
+ G_MEMORY_OUTPUT_STREAM (out));
+ out_size = g_memory_output_stream_get_data_size (
+ G_MEMORY_OUTPUT_STREAM (out));
+
+ check_msg (
+ in_size == out_size &&
+ memcmp (in_data, out_data, in_size) == 0,
+ "Buffer content mismatch: "
+ "%d != %d, in = '%.*s' != out = '%.*s'",
+ in_size, out_size,
+ in_size, in_data,
+ out_size, out_data);
camel_test_pull ();
- camel_stream_filter_remove ((CamelStreamFilter *) filter, id);
- check_count (f, 1);
- camel_mime_filter_reset (f);
+ camel_mime_filter_reset (filter);
- check_unref (filter, 1);
+ check_unref (in_filter_stream, 1);
check_count (indisk, 1);
- check_count (f, 1);
+ check_count (filter, 1);
check_unref (in, 1);
check (g_seekable_seek (
@@ -76,30 +99,43 @@ test_filter (CamelMimeFilter *f,
camel_test_push ("writing through filter stream");
- byte_array_in = g_byte_array_new ();
- in = camel_stream_mem_new_with_byte_array (byte_array_in);
- filter = camel_stream_filter_new (in);
+ in = g_memory_output_stream_new_resizable ();
+
+ out_filter_stream = camel_filter_output_stream_new (in, filter);
check_count (in, 2);
- id = camel_stream_filter_add ((CamelStreamFilter *) filter, f);
- check_count (f, 2);
-
- check (camel_stream_write_to_stream (indisk, filter, NULL, NULL) > 0);
- check (camel_stream_flush (filter, NULL, NULL) == 0);
- check_msg (byte_array_in->len == byte_array_out->len
- && memcmp (byte_array_in->data, byte_array_out->data, byte_array_in->len) == 0,
- "Buffer content mismatch, %d != %d, in = '%.*s' != out = '%.*s'", byte_array_in->len,
byte_array_out->len,
- byte_array_in->len, byte_array_in->data, byte_array_out->len, byte_array_out->data);
-
- camel_stream_filter_remove ((CamelStreamFilter *) filter, id);
- check_unref (filter, 1);
+ check_count (filter, 2);
+
+ check (g_output_stream_splice (
+ out_filter_stream, G_INPUT_STREAM (indisk),
+ G_OUTPUT_STREAM_SPLICE_NONE, NULL, NULL) > 0);
+ check (g_output_stream_flush (out_filter_stream, NULL, NULL));
+
+ in_data = g_memory_output_stream_get_data (
+ G_MEMORY_OUTPUT_STREAM (in));
+ in_size = g_memory_output_stream_get_data_size (
+ G_MEMORY_OUTPUT_STREAM (in));
+
+ out_data = g_memory_output_stream_get_data (
+ G_MEMORY_OUTPUT_STREAM (out));
+ out_size = g_memory_output_stream_get_data_size (
+ G_MEMORY_OUTPUT_STREAM (out));
+
+ check_msg (
+ in_size == out_size &&
+ memcmp (in_data, out_data, in_size) == 0,
+ "Buffer content mismatch: "
+ "%d != %d, in = '%.*s' != out = '%.*s'",
+ in_size, out_size,
+ in_size, in_data,
+ out_size, out_data);
+
+ check_unref (out_filter_stream, 1);
check_unref (in, 1);
check_unref (indisk, 1);
check_unref (outdisk, 1);
check_unref (out, 1);
camel_test_pull ();
-
- camel_test_pull ();
}
gint
@@ -114,7 +150,9 @@ main (gint argc,
for (i = 0; i < 100; i++) {
gchar inname[32], outname[32];
- CamelMimeFilter *f;
+ CamelMimeFilter *filter;
+ GFile *infile;
+ GFile *outfile;
struct stat st;
sprintf (inname, "data/html.%d.in", i);
@@ -123,11 +161,22 @@ main (gint argc,
if (g_stat (inname, &st) == -1)
break;
- f = camel_mime_filter_tohtml_new (CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0);
+ filter = camel_mime_filter_tohtml_new (
+ CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0);
+
+ infile = g_file_new_for_path (inname);
+ outfile = g_file_new_for_path (outname);
+
+ camel_test_push ("Data file '%s'", inname);
+
+ test_filter (filter, infile, outfile);
+
+ camel_test_pull ();
- test_filter (f, inname, outname);
+ g_object_unref (infile);
+ g_object_unref (outfile);
- check_unref (f, 1);
+ check_unref (filter, 1);
}
camel_test_end ();
diff --git a/camel/tests/mime-filter/test1.c b/camel/tests/mime-filter/test1.c
index e9ac7c1..0613d63 100644
--- a/camel/tests/mime-filter/test1.c
+++ b/camel/tests/mime-filter/test1.c
@@ -48,54 +48,71 @@ struct {
"Tasmiania with fur\r\n=46rom here or there ?\r\n" },
};
+static void
+test_case (gint test_num,
+ gsize chunk_size)
+{
+ GOutputStream *memory_stream;
+ GOutputStream *filter_stream;
+ CamelMimeFilter *filter;
+ const gchar *p;
+ gchar *data;
+ gsize size;
+
+ filter = camel_mime_filter_canon_new (tests[test_num].flags);
+ memory_stream = g_memory_output_stream_new_resizable ();
+ filter_stream = camel_filter_output_stream_new (memory_stream, filter);
+ check_unref (filter, 2);
+
+ p = tests[test_num].in;
+ while (*p) {
+ gint w = MIN (strlen (p), chunk_size);
+
+ check (g_output_stream_write (
+ filter_stream, p, w, NULL, NULL) == w);
+ p += w;
+ }
+ g_output_stream_flush (filter_stream, NULL, NULL);
+
+ data = g_memory_output_stream_get_data (
+ G_MEMORY_OUTPUT_STREAM (memory_stream));
+ size = g_memory_output_stream_get_data_size (
+ G_MEMORY_OUTPUT_STREAM (memory_stream));
+
+ check_msg (
+ size == strlen (tests[test_num].out),
+ "Buffer length mismatch: "
+ "expected %d got %d\n or '%s' got '%.*s'",
+ strlen (tests[test_num].out), size,
+ tests[test_num].out, size, data);
+ check_msg (
+ memcmp (data, tests[test_num].out, size) == 0,
+ "Buffer mismatch: expected '%s' got '%.*s'",
+ tests[test_num].out, size, data);
+
+ check_unref (filter_stream, 1);
+ check_unref (memory_stream, 1);
+}
+
gint
main (gint argc,
gchar **argv)
{
- CamelStream *stream;
- CamelMimeFilter *sh;
- gint i;
+ gint ii;
camel_test_init (argc, argv);
camel_test_start ("canonicalisation filter tests");
- for (i = 0; i < G_N_ELEMENTS (tests); i++) {
- gint step;
+ for (ii = 0; ii < G_N_ELEMENTS (tests); ii++) {
+ gsize chunk_size;
- camel_test_push ("Data test %d '%s'\n", i, tests[i].in);
+ camel_test_push ("Data test %d '%s'\n", ii, tests[ii].in);
/* try all write sizes */
- for (step = 1; step < 20; step++) {
- GByteArray *byte_array;
- CamelStream *out;
- const gchar *p;
-
- camel_test_push ("Chunk size %d\n", step);
-
- byte_array = g_byte_array_new ();
- out = camel_stream_mem_new_with_byte_array (byte_array);
- stream = camel_stream_filter_new (out);
- sh = camel_mime_filter_canon_new (tests[i].flags);
- check (camel_stream_filter_add (
- CAMEL_STREAM_FILTER (stream), sh) != -1);
- check_unref (sh, 2);
-
- p = tests[i].in;
- while (*p) {
- gint w = MIN (strlen (p), step);
-
- check (camel_stream_write (
- stream, p, w, NULL, NULL) == w);
- p += w;
- }
- camel_stream_flush (stream, NULL, NULL);
-
- check_msg (byte_array->len == strlen (tests[i].out), "Buffer length mismatch:
expected %d got %d\n or '%s' got '%.*s'", strlen (tests[i].out), byte_array->len, tests[i].out,
byte_array->len, byte_array->data);
- check_msg (0 == memcmp (byte_array->data, tests[i].out, byte_array->len), "Buffer
mismatch: expected '%s' got '%.*s'", tests[i].out, byte_array->len, byte_array->data);
- check_unref (stream, 1);
- check_unref (out, 1);
-
+ for (chunk_size = 1; chunk_size < 20; chunk_size++) {
+ camel_test_push ("Chunk size %d\n", chunk_size);
+ test_case (ii, chunk_size);
camel_test_pull ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]