static void test_streams (GDir *dir, const char *datadir, const char *filename) { char inpath[256], outpath[256], *p, *q, *o; const char *dent, *d; gint64 start, end; size_t n; guint i; p = g_stpcpy (inpath, datadir); *p++ = G_DIR_SEPARATOR; p = g_stpcpy (p, "input"); *p++ = G_DIR_SEPARATOR; strcpy (p, filename); q = g_stpcpy (outpath, datadir); *q++ = G_DIR_SEPARATOR; q = g_stpcpy (q, "output"); *q++ = G_DIR_SEPARATOR; *q = '\0'; n = strlen (filename); while ((dent = g_dir_read_name (dir))) { if (strncmp (dent, filename, n) != 0 || dent[n] != '_') continue; d = dent + n + 1; if ((start = strtol (d, &o, 10)) < 0 || *o != ',') continue; d = o + 1; if ((((end = strtol (d, &o, 10)) < start) && end != -1) || *o != '\0') continue; strcpy (q, dent); for (i = 0; i < G_N_ELEMENTS (checks); i++) { testsuite_check ("%s on `%s'", checks[i].what, dent); try { if (!checks[i].check (inpath, outpath, dent, start, end)) { testsuite_check_warn ("%s could not open `%s'", checks[i].what, dent); } else { testsuite_check_passed (); } } catch (ex) { testsuite_check_failed ("%s on `%s' failed: %s", checks[i].what, dent, ex->message); } finally; } } g_dir_rewind (dir); }
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 (); }