static gboolean check_stream_gio (const char *input, const char *output, const char *filename, gint64 start, gint64 end) { GMimeStream *streams[2]; Exception *ex = NULL; GFile *file; int fd; if (!(file = g_file_new_for_path (input))) return FALSE; if ((fd = open (output, O_RDONLY, 0)) == -1) { g_object_unref (file); return FALSE; } streams[0] = g_mime_stream_gio_new_with_bounds (file, start, end); streams[1] = g_mime_stream_fs_new (fd); if (!streams_match (streams, filename)) ex = exception_new ("GMimeStreamBuffer (Cache Mode) streams did not match for `%s'", filename); g_object_unref (streams[0]); g_object_unref (streams[1]); if (ex != NULL) throw (ex); return TRUE; }
static gboolean check_stream_buffer_cache (const char *input, const char *output, const char *filename, gint64 start, gint64 end) { GMimeStream *streams[2], *stream; Exception *ex = NULL; int fd[2]; if ((fd[0] = open (input, O_RDONLY, 0)) == -1) return FALSE; if ((fd[1] = open (output, O_RDONLY, 0)) == -1) { close (fd[0]); return FALSE; } streams[0] = g_mime_stream_fs_new (fd[0]); stream = g_mime_stream_buffer_new (streams[0], GMIME_STREAM_BUFFER_CACHE_READ); g_object_unref (streams[0]); streams[0] = g_mime_stream_substream (stream, start, end); g_object_unref (stream); streams[1] = g_mime_stream_fs_new (fd[1]); if (!streams_match (streams, filename)) ex = exception_new ("GMimeStreamBuffer (Cache Mode) streams did not match for `%s'", filename); g_object_unref (streams[0]); g_object_unref (streams[1]); if (ex != NULL) throw (ex); return TRUE; }
static gboolean check_stream_file (const char *input, const char *output, const char *filename, gint64 start, gint64 end) { GMimeStream *streams[2], *stream; Exception *ex = NULL; FILE *fp[2]; if (!(fp[0] = fopen (input, "r"))) return FALSE; if (!(fp[1] = fopen (output, "r"))) { fclose (fp[0]); return FALSE; } stream = g_mime_stream_file_new (fp[0]); streams[0] = g_mime_stream_substream (stream, start, end); g_object_unref (stream); streams[1] = g_mime_stream_file_new (fp[1]); if (!streams_match (streams, filename)) ex = exception_new ("GMimeStreamFile streams did not match for `%s'", filename); g_object_unref (streams[0]); g_object_unref (streams[1]); if (ex != NULL) throw (ex); return TRUE; }
static gboolean check_stream_mmap (const char *input, const char *output, const char *filename, gint64 start, gint64 end) { GMimeStream *streams[2], *stream; Exception *ex = NULL; int fd[2]; if ((fd[0] = open (input, O_RDONLY, 0)) == -1) return FALSE; if ((fd[1] = open (output, O_RDONLY, 0)) == -1) { close (fd[0]); return FALSE; } stream = g_mime_stream_mmap_new (fd[0], PROT_READ, MAP_PRIVATE); streams[0] = g_mime_stream_substream (stream, start, end); g_object_unref (stream); streams[1] = g_mime_stream_mmap_new (fd[1], PROT_READ, MAP_PRIVATE); if (!streams_match (streams, filename)) ex = exception_new ("streams did not match"); g_object_unref (streams[0]); g_object_unref (streams[1]); if (ex != NULL) throw (ex); return TRUE; }
int main (int argc, char **argv) { const char *datadir = "data/mbox"; char input[256], output[256], *tmp, *p, *q; GMimeStream *istream, *ostream, *mstream, *pstream; GMimeParser *parser; const char *dent; const char *path; struct stat st; GDir *dir; int i; #ifdef ENABLE_MBOX_MATCH int fd; if (mkdir ("./tmp", 0755) == -1 && errno != EEXIST) return 0; #endif g_mime_init (); testsuite_init (argc, argv); path = datadir; for (i = 1; i < argc; i++) { if (argv[i][0] != '-') { path = argv[i]; break; } } testsuite_start ("Mbox parser"); if (stat (path, &st) == -1) goto exit; if (S_ISDIR (st.st_mode)) { /* automated testsuite */ p = g_stpcpy (input, path); *p++ = G_DIR_SEPARATOR; p = g_stpcpy (p, "input"); if (!(dir = g_dir_open (input, 0, NULL))) goto exit; *p++ = G_DIR_SEPARATOR; *p = '\0'; q = g_stpcpy (output, path); *q++ = G_DIR_SEPARATOR; q = g_stpcpy (q, "output"); *q++ = G_DIR_SEPARATOR; *q = '\0'; while ((dent = g_dir_read_name (dir))) { if (!g_str_has_suffix (dent, ".mbox")) continue; strcpy (p, dent); strcpy (q, dent); tmp = NULL; parser = NULL; istream = NULL; ostream = NULL; mstream = NULL; pstream = NULL; testsuite_check ("%s", dent); try { if (!(istream = g_mime_stream_fs_open (input, O_RDONLY, 0, NULL))) { throw (exception_new ("could not open `%s': %s", input, g_strerror (errno))); } if (!(ostream = g_mime_stream_fs_open (output, O_RDONLY, 0, NULL))) { throw (exception_new ("could not open `%s': %s", output, g_strerror (errno))); } #ifdef ENABLE_MBOX_MATCH tmp = g_strdup_printf ("./tmp/%s", dent); if ((fd = open (tmp, O_CREAT | O_RDWR | O_TRUNC, 0644)) == -1) { throw (exception_new ("could not open `%s': %s", tmp, g_strerror (errno))); } mstream = g_mime_stream_fs_new (fd); #endif parser = g_mime_parser_new_with_stream (istream); g_mime_parser_set_persist_stream (parser, TRUE); g_mime_parser_set_format (parser, GMIME_FORMAT_MBOX); if (!g_mime_parser_get_persist_stream (parser)) throw (exception_new ("persist stream check failed")); if (g_mime_parser_get_format (parser) != GMIME_FORMAT_MBOX) throw (exception_new ("format check failed")); if (strstr (dent, "content-length") != NULL) { g_mime_parser_set_respect_content_length (parser, TRUE); if (!g_mime_parser_get_respect_content_length (parser)) throw (exception_new ("respect content-length check failed")); } else { g_mime_parser_set_respect_content_length (parser, FALSE); if (g_mime_parser_get_respect_content_length (parser)) throw (exception_new ("respect content-length check failed")); } g_mime_parser_set_header_regex (parser, "^X-Evolution", xevcb, NULL); pstream = g_mime_stream_mem_new (); test_parser (parser, mstream, pstream); #ifdef ENABLE_MBOX_MATCH g_mime_stream_flush (mstream); g_mime_stream_reset (istream); g_mime_stream_reset (mstream); if (!streams_match (istream, mstream)) throw (exception_new ("mboxes do not match for `%s'", dent)); #endif g_mime_stream_reset (ostream); g_mime_stream_reset (pstream); if (!streams_match (ostream, pstream)) throw (exception_new ("summaries do not match for `%s'", dent)); testsuite_check_passed (); #ifdef ENABLE_MBOX_MATCH unlink (tmp); #endif } catch (ex) { if (parser != NULL) testsuite_check_failed ("%s: %s", dent, ex->message); else testsuite_check_warn ("%s: %s", dent, ex->message); } finally; if (mstream != NULL) g_object_unref (mstream); if (pstream != NULL) g_object_unref (pstream); if (istream != NULL) g_object_unref (istream); if (ostream != NULL) g_object_unref (ostream); if (parser != NULL) g_object_unref (parser); g_free (tmp); } g_dir_close (dir); } else if (S_ISREG (st.st_mode)) { /* manually run test on a single file */ if (!(istream = g_mime_stream_fs_open (path, O_RDONLY, 0, NULL))) goto exit; parser = g_mime_parser_new_with_stream (istream); g_mime_parser_set_format (parser, GMIME_FORMAT_MBOX); #ifdef ENABLE_MBOX_MATCH tmp = g_strdup ("./tmp/mbox-test.XXXXXX"); if ((fd = g_mkstemp (tmp)) == -1) { g_object_unref (istream); g_object_unref (parser); g_free (tmp); goto exit; } mstream = g_mime_stream_fs_new (fd); #else mstream = NULL; #endif ostream = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner ((GMimeStreamFile *) ostream, FALSE); testsuite_check ("user-input mbox: `%s'", path); try { test_parser (parser, mstream, ostream); #ifdef ENABLE_MBOX_MATCH g_mime_stream_reset (istream); g_mime_stream_reset (mstream); if (!streams_match (istream, mstream)) throw (exception_new ("`%s' does not match `%s'", tmp, path)); unlink (tmp); #endif testsuite_check_passed (); } catch (ex) { testsuite_check_failed ("user-input mbox `%s': %s", path, ex->message); } finally; g_object_unref (istream); g_object_unref (ostream); g_object_unref (parser); #ifdef ENABLE_MBOX_MATCH g_object_unref (mstream); g_free (tmp); #endif } else { goto exit; } exit: #ifdef ENABLE_MBOX_MATCH //if ((dir = g_dir_open ("./tmp", 0, NULL))) { // p = g_stpcpy (input, "./tmp"); // *p++ = G_DIR_SEPARATOR; // // while ((dent = g_dir_read_name (dir))) { // strcpy (p, dent); // unlink (input); // } // // g_dir_close (dir); //} //rmdir ("./tmp"); #endif testsuite_end (); g_mime_shutdown (); return testsuite_exit (); }