From d7f8a64b200130ecb2fa1a0a48fc4f16cfd143f4 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 26 Aug 2023 13:20:51 +0300 Subject: [PATCH] mu: modernize command-line unit tests --- mu/meson.build | 29 ++ mu/mu-cmd-cfind.cc | 228 +++++++++ mu/mu-cmd-extract.cc | 136 ++++++ mu/mu-cmd-find.cc | 224 +++++++++ mu/mu-cmd-verify.cc | 76 +++ mu/mu-cmd-view.cc | 82 +++- mu/tests/meson.build | 18 +- mu/tests/test-mu-cmd-cfind.cc | 382 --------------- mu/tests/test-mu-cmd.cc | 850 ---------------------------------- 9 files changed, 777 insertions(+), 1248 deletions(-) delete mode 100644 mu/tests/test-mu-cmd-cfind.cc delete mode 100644 mu/tests/test-mu-cmd.cc diff --git a/mu/meson.build b/mu/meson.build index 7bc3956a..215a591b 100644 --- a/mu/meson.build +++ b/mu/meson.build @@ -48,6 +48,27 @@ test('test-cmd-add', cpp_args: ['-DBUILD_TESTS'], dependencies: [glib_dep, lib_mu_dep])) +test('test-cmd-cfind', + executable('test-cmd-cfind', + 'mu-cmd-cfind.cc', + install: false, + cpp_args: ['-DBUILD_TESTS'], + dependencies: [glib_dep, lib_mu_dep])) + +test('test-cmd-extract', + executable('test-cmd-extract', + 'mu-cmd-extract.cc', + install: false, + cpp_args: ['-DBUILD_TESTS'], + dependencies: [glib_dep, lib_mu_dep])) + +test('test-cmd-find', + executable('test-cmd-find', + 'mu-cmd-find.cc', + install: false, + cpp_args: ['-DBUILD_TESTS'], + dependencies: [glib_dep, lib_mu_dep])) + test('test-cmd-mkdir', executable('test-cmd-mkdir', 'mu-cmd-mkdir.cc', @@ -62,6 +83,14 @@ test('test-cmd-remove', cpp_args: ['-DBUILD_TESTS'], dependencies: [glib_dep, lib_mu_dep])) +test('test-cmd-verify', + executable('test-cmd-verify', + 'mu-cmd-verify.cc', + install: false, + cpp_args: ['-DBUILD_TESTS'], + dependencies: [glib_dep, lib_mu_dep])) + + test('test-cmd-view', executable('test-cmd-view', 'mu-cmd-view.cc', diff --git a/mu/mu-cmd-cfind.cc b/mu/mu-cmd-cfind.cc index 044458fd..5385c9c6 100644 --- a/mu/mu-cmd-cfind.cc +++ b/mu/mu-cmd-cfind.cc @@ -305,3 +305,231 @@ Mu::mu_cmd_cfind(const Mu::Store& store, const Mu::Options& opts) output(ItemType::Footer, Nothing, opts); return Ok(); } + + + + +#ifdef BUILD_TESTS +/* + * Tests. + * + */ + +#include "utils/mu-test-utils.hh" + + +static std::string test_mu_home; + +static void +test_mu_cfind_plain(void) +{ + auto res{run_command({MU_PROGRAM, "--nocolor", "cfind", "--muhome", test_mu_home, + "--format", "plain", "testmu\\.xxx?"})}; + assert_valid_result(res); + + /* note, output order is unspecified */ + if (res->standard_out[0] == 'H') + assert_equal(res->standard_out, + "Helmut Kröger hk@testmu.xxx\n" + "Mü testmu@testmu.xx\n"); + else + assert_equal(res->standard_out, + "Mü testmu@testmu.xx\n" + "Helmut Kröger hk@testmu.xxx\n"); +} + +static void +test_mu_cfind_bbdb(void) +{ + const auto old_tz{set_tz("Europe/Helsinki")}; + auto res{run_command({MU_PROGRAM, "--nocolor", "cfind", "--muhome", test_mu_home, + "--format", "bbdb", "testmu\\.xxx?"})}; + assert_valid_result(res); + g_assert_cmpuint(res->standard_out.size(), >, 52); + +#define frm1 \ + ";; -*-coding: utf-8-emacs;-*-\n" \ + ";;; file-version: 6\n" \ + "[\"Helmut\" \"Kröger\" nil nil nil nil (\"hk@testmu.xxx\") " \ + "((creation-date . \"{}\") " \ + "(time-stamp . \"1970-01-01\")) nil]\n" \ + "[\"Mü\" \"\" nil nil nil nil (\"testmu@testmu.xx\") " \ + "((creation-date . \"{}\") " \ + "(time-stamp . \"1970-01-01\")) nil]\n" + +#define frm2 \ + ";; -*-coding: utf-8-emacs;-*-\n" \ + ";;; file-version: 6\n" \ + "[\"Mü\" \"\" nil nil nil nil (\"testmu@testmu.xx\") " \ + "((creation-date . \"{}\") " \ + "(time-stamp . \"1970-01-01\")) nil]\n" \ + "[\"Helmut\" \"Kröger\" nil nil nil nil (\"hk@testmu.xxx\") " \ + "((creation-date . \"{}\") " \ + "(time-stamp . \"1970-01-01\")) nil]\n" + + auto&& today{mu_format("{:%F}", mu_time(::time({})))}; + std::string expected; + if (res->standard_out.at(52) == 'H') + expected = mu_format(frm1, today, today); + else + expected = mu_format(frm2, today, today); + + assert_equal(res->standard_out, expected); + set_tz(old_tz); +} + +static void +test_mu_cfind_wl(void) +{ + auto res{run_command({MU_PROGRAM, "--nocolor", "cfind", "--muhome", test_mu_home, + "--format", "wl", "testmu\\.xxx?"})}; + assert_valid_result(res); + + if (res->standard_out.at(0) == 'h') + assert_equal(res->standard_out, + "hk@testmu.xxx \"HelmutK\" \"Helmut Kröger\"\n" + "testmu@testmu.xx \"Mü\" \"Mü\"\n"); + else + assert_equal(res->standard_out, + "testmu@testmu.xx \"Mü\" \"Mü\"\n" + "hk@testmu.xxx \"HelmutK\" \"Helmut Kröger\"\n"); +} + +static void +test_mu_cfind_mutt_alias(void) +{ + auto res{run_command({MU_PROGRAM, "--nocolor", "cfind", "--muhome", test_mu_home, + "--format", "mutt-alias", "testmu\\.xxx?"})}; + assert_valid_result(res); + + if (res->standard_out.at(6) == 'H') + assert_equal(res->standard_out, + "alias HelmutK Helmut Kröger \n" + "alias Mü Mü \n"); + else + assert_equal(res->standard_out, + "alias Mü Mü \n" + "alias HelmutK Helmut Kröger \n"); +} + +static void +test_mu_cfind_mutt_ab(void) +{ + auto res{run_command({MU_PROGRAM, "--nocolor", "cfind", "--muhome", test_mu_home, + "--format", "mutt-ab", "testmu\\.xxx?"})}; + assert_valid_result(res); + + if (res->standard_out.at(39) == 'h') + assert_equal(res->standard_out, + "Matching addresses in the mu database:\n" + "hk@testmu.xxx\tHelmut Kröger\t\n" + "testmu@testmu.xx\tMü\t\n"); + else + assert_equal(res->standard_out, + "Matching addresses in the mu database:\n" + "testmu@testmu.xx\tMü\t\n" + "hk@testmu.xxx\tHelmut Kröger\t\n"); +} + +static void +test_mu_cfind_org_contact(void) +{ + auto res{run_command({MU_PROGRAM, "--nocolor", "cfind", "--muhome", test_mu_home, + "--format", "org-contact", "testmu\\.xxx?"})}; + assert_valid_result(res); + + if (res->standard_out.at(2) == 'H') + assert_equal(res->standard_out, + "* Helmut Kröger\n" + ":PROPERTIES:\n" + ":EMAIL: hk@testmu.xxx\n" + ":END:\n\n" + "* Mü\n" + ":PROPERTIES:\n" + ":EMAIL: testmu@testmu.xx\n" + ":END:\n\n"); + else + assert_equal(res->standard_out, + "* Mü\n" + ":PROPERTIES:\n" + ":EMAIL: testmu@testmu.xx\n" + ":END:\n\n" + "* Helmut Kröger\n" + ":PROPERTIES:\n" + ":EMAIL: hk@testmu.xxx\n" + ":END:\n\n"); +} + +static void +test_mu_cfind_csv(void) +{ + auto res{run_command({MU_PROGRAM, "--nocolor", "cfind", "--muhome", test_mu_home, + "--format", "csv", "testmu\\.xxx?"})}; + assert_valid_result(res); + + if (res->standard_out.at(1) == 'H') + assert_equal(res->standard_out, + "\"Helmut Kröger\",\"hk@testmu.xxx\"\n" + "\"Mü\",\"testmu@testmu.xx\"\n"); + else + assert_equal(res->standard_out, + "\"Mü\",\"testmu@testmu.xx\"\n" + "\"Helmut Kröger\",\"hk@testmu.xxx\"\n"); +} + + +static void +test_mu_cfind_json() +{ + auto res{run_command({MU_PROGRAM, "--nocolor", "cfind", "--muhome", test_mu_home, + "--format", "json", "^a@example\\.com"})}; + assert_valid_result(res); + + const auto expected = R"([ + { + "email" : "a@example.com", + "name" : null, + "display" : "a@example.com", + "last-seen" : 1463331445, + "last-seen-iso" : "2016-05-15T16:57:25Z", + "personal" : false, + "frequency" : 1 + } +] +)"; + assert_equal(res->standard_out, expected); +} + +int +main(int argc, char* argv[]) +{ + mu_test_init(&argc, &argv); + + if (!set_en_us_utf8_locale()) + return 0; /* don't error out... */ + + TempDir temp_dir{}; + { + test_mu_home = temp_dir.path(); + + auto res1 = run_command({MU_PROGRAM, "--quiet", "init", + "--muhome", test_mu_home, "--maildir" , MU_TESTMAILDIR}); + assert_valid_result(res1); + + auto res2 = run_command({MU_PROGRAM, "--quiet", "index", + "--muhome", test_mu_home}); + assert_valid_result(res2); + } + + g_test_add_func("/cmd/find/plain", test_mu_cfind_plain); + g_test_add_func("/cmd/find/bbdb", test_mu_cfind_bbdb); + g_test_add_func("/cmd/find/wl", test_mu_cfind_wl); + g_test_add_func("/cmd/find/mutt-alias", test_mu_cfind_mutt_alias); + g_test_add_func("/cmd/find/mutt-ab", test_mu_cfind_mutt_ab); + g_test_add_func("/cmd/find/org-contact", test_mu_cfind_org_contact); + g_test_add_func("/cmd/find/csv", test_mu_cfind_csv); + g_test_add_func("/cmd/find/json", test_mu_cfind_json); + + return g_test_run(); +} +#endif /*BUILD_TESTS*/ diff --git a/mu/mu-cmd-extract.cc b/mu/mu-cmd-extract.cc index f33e74ba..28793b3a 100644 --- a/mu/mu-cmd-extract.cc +++ b/mu/mu-cmd-extract.cc @@ -168,3 +168,139 @@ Mu::mu_cmd_extract(const Options& opts) return save_parts(*message, opts.extract.filename_rx, opts); } + + + + +#ifdef BUILD_TESTS +/* + * Tests. + * + */ + +#include +#include +#include +#include +#include + +#include +#include "utils/mu-test-utils.hh" + + +static gint64 +get_file_size(const std::string& path) +{ + int rv; + struct stat statbuf; + + mu_info("ppatj {}", path); + + rv = stat(path.c_str(), &statbuf); + if (rv != 0) { + mu_debug ("error: {}", g_strerror (errno)); + return -1; + } + + mu_debug("{} -> {} bytes", path, statbuf.st_size); + + return statbuf.st_size; +} + +static void +test_mu_extract_02(void) +{ + TempDir temp_dir{}; + auto res= run_command({ + MU_PROGRAM, "extract", "--save-attachments", + mu_format("--target-dir='{}'", temp_dir.path()), + join_paths(MU_TESTMAILDIR2, "Foo", "cur", "mail5")}); + assert_valid_result(res); + g_assert_true(res->standard_err.empty()); + + g_assert_cmpuint(get_file_size(join_paths(temp_dir.path(), "custer.jpg")), >=, 15955); + g_assert_cmpuint(get_file_size(join_paths(temp_dir.path(), "custer.jpg")), <=, 15960); + g_assert_cmpuint(get_file_size(join_paths(temp_dir.path(), "sittingbull.jpg")), ==, 17674); +} + +static void +test_mu_extract_03(void) +{ + TempDir temp_dir{}; + auto res= run_command({ + MU_PROGRAM, "extract", "--parts=3", + mu_format("--target-dir='{}'", temp_dir.path()), + join_paths(MU_TESTMAILDIR2, "Foo", "cur", "mail5")}); + assert_valid_result(res); + g_assert_true(res->standard_err.empty()); + + g_assert_true(g_access(join_paths(temp_dir.path(), "custer.jpg").c_str(), F_OK) == 0); + g_assert_false(g_access(join_paths(temp_dir.path(), "sittingbull.jpg").c_str(), F_OK) == 0); +} + +static void +test_mu_extract_overwrite(void) +{ + TempDir temp_dir{}; + auto res= run_command({ + MU_PROGRAM, "extract", "-a", + mu_format("--target-dir='{}'", temp_dir.path()), + join_paths(MU_TESTMAILDIR2, "Foo", "cur", "mail5")}); + assert_valid_result(res); + g_assert_true(res->standard_err.empty()); + + g_assert_true(g_access(join_paths(temp_dir.path(), "custer.jpg").c_str(), F_OK) == 0); + g_assert_true(g_access(join_paths(temp_dir.path(), "sittingbull.jpg").c_str(), F_OK) == 0); + + + /* now, it should fail, because we don't allow overwrites + * without --overwrite */ + auto res2 = run_command({ + MU_PROGRAM, "extract", "-a", + mu_format("--target-dir='{}'", temp_dir.path()), + join_paths(MU_TESTMAILDIR2, "Foo", "cur", "mail5")}); + + assert_valid_result(res2); + g_assert_false(res2->standard_err.empty()); + + + auto res3 = run_command({ + MU_PROGRAM, "extract", "-a", "--overwrite", + mu_format("--target-dir='{}'", temp_dir.path()), + join_paths(MU_TESTMAILDIR2, "Foo", "cur", "mail5")}); + + assert_valid_result(res3); + g_assert_true(res3->standard_err.empty()); +} + +static void +test_mu_extract_by_name(void) +{ + TempDir temp_dir{}; + auto res= run_command({ + MU_PROGRAM, "extract", + mu_format("--target-dir='{}'", temp_dir.path()), + join_paths(MU_TESTMAILDIR2, "Foo", "cur", "mail5"), + "sittingbull.jpg"}); + assert_valid_result(res); + g_assert_true(res->standard_err.empty()); + + g_assert_true(g_access(join_paths(temp_dir.path(), "sittingbull.jpg").c_str(), F_OK) == 0); + g_assert_false(g_access(join_paths(temp_dir.path(), "custer.jpg").c_str(), F_OK) == 0); +} + + +int +main(int argc, char* argv[]) +{ + mu_test_init(&argc, &argv); + + g_test_add_func("/cmd/extract/02", test_mu_extract_02); + g_test_add_func("/cmd/extract/03", test_mu_extract_03); + g_test_add_func("/cmd/extract/overwrite", test_mu_extract_overwrite); + g_test_add_func("/cmd/extract/by-name", test_mu_extract_by_name); + + return g_test_run(); +} + +#endif diff --git a/mu/mu-cmd-find.cc b/mu/mu-cmd-find.cc index ec5a9283..12765140 100644 --- a/mu/mu-cmd-find.cc +++ b/mu/mu-cmd-find.cc @@ -499,3 +499,227 @@ Mu::mu_cmd_find(const Store& store, const Options& opts) else return process_query(store, *expr, opts); } + + + + + +#ifdef BUILD_TESTS +/* + * Tests. + * + */ + +#include "utils/mu-test-utils.hh" + + +/* tests for the command line interface, uses testdir2 */ + +static std::string test_mu_home; + +auto count_nl(const std::string& s)->size_t { + size_t n{}; + for (auto&& c: s) + if (c == '\n') + ++n; + return n; +} + +static size_t +search_func(const std::string& expr, size_t expected) +{ + auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home, expr}); + assert_valid_result(res); + + /* we expect zero lines of error output if there is a match; otherwise + * there should be one line 'No matches found' */ + if (res->exit_code != 0) { + g_assert_cmpuint(res->exit_code, ==, 2); // no match + g_assert_true(res->standard_out.empty()); + g_assert_cmpuint(count_nl(res->standard_err), ==, 1); + return 0; + } + + return count_nl(res->standard_out); +} + +#define search(Q,EXP) do { \ + g_assert_cmpuint(search_func(Q, EXP), ==, EXP); \ +} while(0) + + +static void +test_mu_find_empty_query(void) +{ + search("\"\"", 13); +} + +static void +test_mu_find_01(void) +{ + search("f:john fruit", 1); + search("f:soc@example.com", 1); + search("t:alki@example.com", 1); + search("t:alcibiades", 1); + search("http emacs", 1); + search("f:soc@example.com OR f:john", 2); + search("f:soc@example.com OR f:john OR t:edmond", 3); + search("t:julius", 1); + search("s:dude", 1); + search("t:dantès", 1); +} + +/* index testdir2, and make sure it adds two documents */ +static void +test_mu_find_02(void) +{ + search("bull", 1); + search("g:x", 0); + search("flag:encrypted", 0); + search("flag:attach", 1); + + search("i:3BE9E6535E0D852173@emss35m06.us.lmco.com", 1); +} + +static void +test_mu_find_file(void) +{ + search("file:sittingbull.jpg", 1); + search("file:custer.jpg", 1); + search("file:custer.*", 1); + search("j:sit*", 1); +} + +static void +test_mu_find_mime(void) +{ + search("mime:image/jpeg", 1); + search("mime:text/plain", 13); + search("y:text*", 13); + search("y:image*", 1); + search("mime:message/rfc822", 2); +} + +static void +test_mu_find_text_in_rfc822(void) +{ + search("embed:dancing", 1); + search("e:curious", 1); + search("embed:with", 2); + search("e:karjala", 0); + search("embed:navigation", 1); +} + +static void +test_mu_find_maildir_special(void) +{ + search("\"maildir:/wOm_bàT\"", 3); + search("\"maildir:/wOm*\"", 3); + search("\"maildir:/wOm_*\"", 3); + search("\"maildir:wom_bat\"", 0); + search("\"maildir:/wombat\"", 0); + search("subject:atoms", 1); + search("\"maildir:/wom_bat\" subject:atoms", 1); +} + + +/* some more tests */ + +static void +test_mu_find_wrong_muhome() +{ + auto res = run_command({MU_PROGRAM, "find", "--muhome", + join_paths("/foo", "bar", "nonexistent"), "f:socrates"}); + assert_valid_result(res); + g_assert_cmpuint(res->exit_code,==,1); // general error + g_assert_cmpuint(count_nl(res->standard_err), >, 1); +} + +static void +test_mu_find_links(void) +{ + TempDir temp_dir; + + { + auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home, + "--format", "links", "--linksdir", temp_dir.path(), + "mime:message/rfc822"}); + assert_valid_result(res); + g_assert_cmpuint(res->exit_code,==,0); + g_assert_cmpuint(count_nl(res->standard_out),==,0); + g_assert_cmpuint(count_nl(res->standard_err),==,0); + } + + + /* furthermore, two symlinks should be there */ + const auto f1{mu_format("{}/cur/rfc822.1", temp_dir)}; + const auto f2{mu_format("{}/cur/rfc822.2", temp_dir)}; + + g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK); + g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK); + + /* now we try again, we should get a line of error output, + * when we find the first target file already exists */ + { + auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home, + "--format", "links", "--linksdir", temp_dir.path(), + "mime:message/rfc822"}); + assert_valid_result(res); + g_assert_cmpuint(res->exit_code,==,1); + g_assert_cmpuint(count_nl(res->standard_out),==,0); + g_assert_cmpuint(count_nl(res->standard_err),==,1); + } + + /* now we try again with --clearlinks, and the we should be + * back to 0 errors */ + { + auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home, + "--format", "links", "--clearlinks", "--linksdir", temp_dir.path(), + "mime:message/rfc822"}); + assert_valid_result(res); + g_assert_cmpuint(res->exit_code,==,0); + g_assert_cmpuint(count_nl(res->standard_out),==,0); + g_assert_cmpuint(count_nl(res->standard_err),==,0); + } + + g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK); + g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK); +} + +/* some more tests */ + +int +main(int argc, char* argv[]) +{ + mu_test_init(&argc, &argv); + + if (!set_en_us_utf8_locale()) + return 0; /* don't error out... */ + + TempDir temp_dir{}; + { + test_mu_home = temp_dir.path(); + + auto res1 = run_command({MU_PROGRAM, "--quiet", "init", + "--muhome", test_mu_home, "--maildir" , MU_TESTMAILDIR2}); + assert_valid_result(res1); + + auto res2 = run_command({MU_PROGRAM, "--quiet", "index", + "--muhome", test_mu_home}); + assert_valid_result(res2); + } + + g_test_add_func("/cmd/find/empty-query", test_mu_find_empty_query); + g_test_add_func("/cmd/find/01", test_mu_find_01); + g_test_add_func("/cmd/find/02", test_mu_find_02); + g_test_add_func("/cmd/find/file", test_mu_find_file); + g_test_add_func("/cmd/find/mime", test_mu_find_mime); + g_test_add_func("/cmd/find/links", test_mu_find_links); + g_test_add_func("/cmd/find/text-in-rfc822", test_mu_find_text_in_rfc822); + g_test_add_func("/cmd/find/wrong-muhome", test_mu_find_wrong_muhome); + g_test_add_func("/cmd/find/maildir-special", test_mu_find_maildir_special); + + return g_test_run(); +} + +#endif /*BUILD_TESTS*/ diff --git a/mu/mu-cmd-verify.cc b/mu/mu-cmd-verify.cc index 62edeeca..7fbb4b9f 100644 --- a/mu/mu-cmd-verify.cc +++ b/mu/mu-cmd-verify.cc @@ -177,3 +177,79 @@ Mu::mu_cmd_verify(const Options& opts) return Err(Error::Code::UnverifiedSignature, "failed to verify one or more signatures"); } + + + +#ifdef BUILD_TESTS +/* + * Tests. + * + */ + +#include "utils/mu-test-utils.hh" + +/* we can only test 'verify' if gpg is installed, and has djcb@djcbsoftware's key in the keyring */ +static bool +verify_is_testable(void) +{ + auto gpg{program_in_path("gpg2")}; + if (!gpg) { + mu_message("cannot find gpg2 in path"); + return false; + } + + auto res{run_command({*gpg, "--list-keys", "DCC4A036"})}; /* djcb@djcbsoftware.nl's key */ + if (!res || res->exit_code != 0) { + mu_message("key DCC4A036 not found"); + return false; + } + + return true; +} + +static void +test_mu_verify_good(void) +{ + if (!verify_is_testable()) { + g_test_skip("cannot test verify"); + return; + } + + auto res = run_command({MU_PROGRAM, "verify", + join_paths(MU_TESTMAILDIR4, "signed!2,S")}); + assert_valid_result(res); + g_assert_cmpuint(res->exit_code ,==, 0); +} + +static void +test_mu_verify_bad(void) +{ + if (!verify_is_testable()) { + g_test_skip("cannot test verify"); + return; + } + + auto res = run_command({MU_PROGRAM, "verify", + join_paths(MU_TESTMAILDIR4, "signed-bad!2,S")}); + assert_valid_result(res); + g_assert_cmpuint(res->exit_code,==, 1); +} + +int +main(int argc, char* argv[]) try { + + mu_test_init(&argc, &argv); + + g_test_add_func("/cmd/verify/good", test_mu_verify_good); + g_test_add_func("/cmd/verify/bad", test_mu_verify_bad); + + return g_test_run(); + +} catch (const Error& e) { + mu_printerrln("{}", e.what()); + return 1; +} catch (...) { + mu_printerrln("caught exception"); + return 1; +} +#endif /*BUILD_TESTS*/ diff --git a/mu/mu-cmd-view.cc b/mu/mu-cmd-view.cc index 69589857..c80e46f0 100644 --- a/mu/mu-cmd-view.cc +++ b/mu/mu-cmd-view.cc @@ -316,10 +316,86 @@ test_view_sexp() assert_equal(output.standard_out, expected); } +static void +test_mu_view_01(void) +{ + TempDir temp_dir{}; + + if (!set_en_us_utf8_locale()) { + g_test_skip("failed to switch to en_US/utf8"); + return; + } + + auto res = run_command({MU_PROGRAM, "view", + join_paths(MU_TESTMAILDIR2, "bar", "cur", "mail4")}); + assert_valid_result(res); + g_assert_true(res->standard_err.empty()); + + g_assert_cmpuint(res->standard_out.size(), ==, 364); +} + +static void +test_mu_view_multi(void) +{ + TempDir temp_dir{}; + + if (!set_en_us_utf8_locale()) { + g_test_skip("failed to switch to en_US/utf8"); + return; + } + + auto res = run_command({MU_PROGRAM, "view", + join_paths(MU_TESTMAILDIR2, "bar", "cur", "mail5"), + join_paths(MU_TESTMAILDIR2, "bar", "cur", "mail5")}); + assert_valid_result(res); + g_assert_true(res->standard_err.empty()); + + g_assert_cmpuint(res->standard_out.size(), ==, 162); +} + +static void +test_mu_view_multi_separate(void) +{ + TempDir temp_dir{}; + + if (!set_en_us_utf8_locale()) { + g_test_skip("failed to switch to en_US/utf8"); + return; + } + + auto res = run_command({MU_PROGRAM, "view", "--terminate", + join_paths(MU_TESTMAILDIR2, "bar", "cur", "mail5"), + join_paths(MU_TESTMAILDIR2, "bar", "cur", "mail5")}); + assert_valid_result(res); + g_assert_true(res->standard_err.empty()); + + g_assert_cmpuint(res->standard_out.size(), ==, 164); +} + +static void +test_mu_view_attach(void) +{ + TempDir temp_dir{}; + + if (!set_en_us_utf8_locale()) { + g_test_skip("failed to switch to en_US/utf8"); + return; + } + + auto res = run_command({MU_PROGRAM, "view", "--terminate", + join_paths(MU_TESTMAILDIR2, "Foo", "cur", "mail5")}); + assert_valid_result(res); + g_assert_true(res->standard_err.empty()); + + g_assert_cmpuint(res->standard_out.size(), ==, 164); +} + + + int main(int argc, char* argv[]) try { - TempDir tmpdir{false}; + TempDir tmpdir{}; msgpath = join_paths(tmpdir.path(), "test-message.txt"); std::ofstream strm{msgpath}; strm.write(test_msg.data(), test_msg.size()); @@ -328,6 +404,10 @@ main(int argc, char* argv[]) try { mu_test_init(&argc, &argv); + g_test_add_func("/cmd/view/01", test_mu_view_01); + g_test_add_func("/cmd/view/multi", test_mu_view_multi); + g_test_add_func("/cmd/view/multi-separate", test_mu_view_multi_separate); + g_test_add_func("/cmd/view/attach", test_mu_view_attach); g_test_add_func("/cmd/view/plain", test_view_plain); g_test_add_func("/cmd/view/html", test_view_html); g_test_add_func("/cmd/view/sexp", test_view_sexp); diff --git a/mu/tests/meson.build b/mu/tests/meson.build index 8aeaec53..b1025b77 100644 --- a/mu/tests/meson.build +++ b/mu/tests/meson.build @@ -18,23 +18,11 @@ # tests # -# BROKEN on ubuntu CI, but works elsewhere. Investigate -test('test-cmd', - executable('test-cmd', - 'test-mu-cmd.cc', - install: false, - dependencies: [glib_dep, config_h_dep, lib_mu_dep])) - -test('test-cmd-cfind', - executable('test-cmd-cfind', - 'test-mu-cmd-cfind.cc', - install: false, - dependencies: [glib_dep, config_h_dep, lib_mu_dep])) test('test-cmd-query', executable('test-cmd-query', - 'test-mu-query.cc', - install: false, - dependencies: [glib_dep, config_h_dep, lib_mu_dep])) + 'test-mu-query.cc', + install: false, + dependencies: [glib_dep, config_h_dep, lib_mu_dep])) gmime_test = executable( 'gmime-test', [ diff --git a/mu/tests/test-mu-cmd-cfind.cc b/mu/tests/test-mu-cmd-cfind.cc deleted file mode 100644 index def90354..00000000 --- a/mu/tests/test-mu-cmd-cfind.cc +++ /dev/null @@ -1,382 +0,0 @@ -/* -** -** Copyright (C) 2008-2020 Dirk-Jan C. Binnema -** -** This program is free software; you can redistribute it and/or modify it -** under the terms of the GNU General Public License as published by the -** Free Software Foundation; either version 3, or (at your option) any -** later version. -** -** This program 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 General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software Foundation, -** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -** -*/ - -#include "config.h" - -#include -#include - -#include -#include -#include - -#include "utils/mu-test-utils.hh" -#include "mu-store.hh" -#include "mu-query.hh" -#include "utils/mu-utils.hh" - -static std::string CONTACTS_CACHE; - -using namespace Mu; - -static std::string -fill_contacts_cache(const std::string& path) -{ - auto cmdline = mu_format("/bin/sh -c '" - "{} --quiet init --muhome={} --maildir={} ; " - "{} --quiet index --muhome={} '", - MU_PROGRAM, - path, - MU_TESTMAILDIR, - MU_PROGRAM, - path); - - if (g_test_verbose()) - mu_println("{}", cmdline); - - GError *err{}; - if (!g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, &err)) { - mu_printerrln("Error: {}", err ? err->message : "?"); - g_clear_error(&err); - g_assert(0); - } - - return path; -} - -static void -test_mu_cfind_plain(void) -{ - gchar *cmdline, *output, *erroutput; - - cmdline = g_strdup_printf("%s --nocolor cfind --muhome=%s --format=plain " - "'testmu\\.xxx?'", - MU_PROGRAM, - CONTACTS_CACHE.c_str()); - if (g_test_verbose()) - g_print("%s\n", cmdline); - - output = erroutput = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - - /* note, output order is unspecified */ - g_assert(output); - if (output[0] == 'H') - g_assert_cmpstr(output, - ==, - "Helmut Kröger hk@testmu.xxx\n" - "Mü testmu@testmu.xx\n"); - else - g_assert_cmpstr(output, - ==, - "Mü testmu@testmu.xx\n" - "Helmut Kröger hk@testmu.xxx\n"); - g_free(cmdline); - g_free(output); - g_free(erroutput); -} - -static void -test_mu_cfind_bbdb(void) -{ - gchar * cmdline, *output, *erroutput, *expected; - gchar today[12]; - struct tm* tmtoday; - time_t now; - const char* old_tz; - - old_tz = set_tz("Europe/Helsinki"); - - cmdline = g_strdup_printf("%s --nocolor cfind --muhome=%s --format=bbdb " - "'testmu\\.xxx?'", - MU_PROGRAM, - CONTACTS_CACHE.c_str()); - - output = erroutput = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - -#define frm1 \ - ";; -*-coding: utf-8-emacs;-*-\n" \ - ";;; file-version: 6\n" \ - "[\"Helmut\" \"Kröger\" nil nil nil nil (\"hk@testmu.xxx\") " \ - "((creation-date . \"%s\") " \ - "(time-stamp . \"1970-01-01\")) nil]\n" \ - "[\"Mü\" \"\" nil nil nil nil (\"testmu@testmu.xx\") " \ - "((creation-date . \"%s\") " \ - "(time-stamp . \"1970-01-01\")) nil]\n" - -#define frm2 \ - ";; -*-coding: utf-8-emacs;-*-\n" \ - ";;; file-version: 6\n" \ - "[\"Mü\" \"\" nil nil nil nil (\"testmu@testmu.xx\") " \ - "((creation-date . \"%s\") " \ - "(time-stamp . \"1970-01-01\")) nil]\n" \ - "[\"Helmut\" \"Kröger\" nil nil nil nil (\"hk@testmu.xxx\") " \ - "((creation-date . \"%s\") " \ - "(time-stamp . \"1970-01-01\")) nil]\n" - - g_assert(output); - - now = time(NULL); - tmtoday = localtime(&now); - strftime(today, sizeof(today), "%Y-%m-%d", tmtoday); - - expected = g_strdup_printf(output[52] == 'H' ? frm1 : frm2, today, today); - - /* g_print ("\n%s\n", output); */ - - g_assert_cmpstr(output, ==, expected); - - g_free(cmdline); - g_free(output); - g_free(erroutput); - g_free(expected); - - set_tz(old_tz); -} - -static void -test_mu_cfind_wl(void) -{ - gchar *cmdline, *output, *erroutput; - - cmdline = g_strdup_printf("%s cfind --muhome=%s --format=wl " - "'testmu\\.xxx?'", - MU_PROGRAM, - CONTACTS_CACHE.c_str()); - - output = erroutput = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - - g_assert(output); - if (output[0] == 'h') - g_assert_cmpstr(output, - ==, - "hk@testmu.xxx \"HelmutK\" \"Helmut Kröger\"\n" - "testmu@testmu.xx \"Mü\" \"Mü\"\n"); - else - g_assert_cmpstr(output, - ==, - "testmu@testmu.xx \"Mü\" \"Mü\"\n" - "hk@testmu.xxx \"HelmutK\" \"Helmut Kröger\"\n"); - - g_free(cmdline); - g_free(output); - g_free(erroutput); -} - -static void -test_mu_cfind_mutt_alias(void) -{ - gchar *cmdline, *output, *erroutput; - - cmdline = g_strdup_printf("%s cfind --muhome=%s --format=mutt-alias " - "'testmu\\.xxx?'", - MU_PROGRAM, - CONTACTS_CACHE.c_str()); - - output = erroutput = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - - /* both orders are possible... */ - g_assert(output); - - if (output[6] == 'H') - g_assert_cmpstr(output, - ==, - "alias HelmutK Helmut Kröger \n" - "alias Mü Mü \n"); - else - g_assert_cmpstr(output, - ==, - "alias Mü Mü \n" - "alias HelmutK Helmut Kröger \n"); - - g_free(cmdline); - g_free(output); - g_free(erroutput); -} - -static void -test_mu_cfind_mutt_ab(void) -{ - gchar *cmdline, *output, *erroutput; - - cmdline = g_strdup_printf("%s cfind --muhome=%s --format=mutt-ab " - "'testmu\\.xxx?'", - MU_PROGRAM, - CONTACTS_CACHE.c_str()); - - if (g_test_verbose()) - g_print("%s\n", cmdline); - - output = erroutput = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - g_assert(output); - - if (output[39] == 'h') - g_assert_cmpstr(output, - ==, - "Matching addresses in the mu database:\n" - "hk@testmu.xxx\tHelmut Kröger\t\n" - "testmu@testmu.xx\tMü\t\n"); - else - g_assert_cmpstr(output, - ==, - "Matching addresses in the mu database:\n" - "testmu@testmu.xx\tMü\t\n" - "hk@testmu.xxx\tHelmut Kröger\t\n"); - - g_free(cmdline); - g_free(output); - g_free(erroutput); -} - -static void -test_mu_cfind_org_contact(void) -{ - gchar *cmdline, *output, *erroutput; - - cmdline = g_strdup_printf("%s cfind --muhome=%s --format=org-contact " - "'testmu\\.xxx?'", - MU_PROGRAM, - CONTACTS_CACHE.c_str()); - - output = erroutput = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - - g_assert(output); - - if (output[2] == 'H') - g_assert_cmpstr(output, - ==, - "* Helmut Kröger\n" - ":PROPERTIES:\n" - ":EMAIL: hk@testmu.xxx\n" - ":END:\n\n" - "* Mü\n" - ":PROPERTIES:\n" - ":EMAIL: testmu@testmu.xx\n" - ":END:\n\n"); - else - g_assert_cmpstr(output, - ==, - "* Mü\n" - ":PROPERTIES:\n" - ":EMAIL: testmu@testmu.xx\n" - ":END:\n\n" - "* Helmut Kröger\n" - ":PROPERTIES:\n" - ":EMAIL: hk@testmu.xxx\n" - ":END:\n\n"); - - g_free(cmdline); - g_free(output); - g_free(erroutput); -} - -static void -test_mu_cfind_csv(void) -{ - gchar *cmdline, *output, *erroutput; - - cmdline = g_strdup_printf("%s --nocolor cfind --muhome=%s --format=csv " - "'testmu\\.xxx?'", - MU_PROGRAM, - CONTACTS_CACHE.c_str()); - - if (g_test_verbose()) - g_print("%s\n", cmdline); - - output = erroutput = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - g_assert(output); - if (output[1] == 'H') - g_assert_cmpstr(output, - ==, - "\"Helmut Kröger\",\"hk@testmu.xxx\"\n" - "\"Mü\",\"testmu@testmu.xx\"\n"); - else - g_assert_cmpstr(output, - ==, - "\"Mü\",\"testmu@testmu.xx\"\n" - "\"Helmut Kröger\",\"hk@testmu.xxx\"\n"); - g_free(cmdline); - g_free(output); - g_free(erroutput); -} - - -static void -test_mu_cfind_json() -{ - gchar *cmdline, *output, *erroutput; - - cmdline = g_strdup_printf("%s --nocolor cfind --muhome=%s --format=json ^a@example\\.com", - MU_PROGRAM, - CONTACTS_CACHE.c_str()); - - if (g_test_verbose()) - g_print("%s\n", cmdline); - - output = erroutput = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - g_assert(output); - - const auto expected = R"([ - { - "email" : "a@example.com", - "name" : null, - "display" : "a@example.com", - "last-seen" : 1463331445, - "last-seen-iso" : "2016-05-15T16:57:25Z", - "personal" : false, - "frequency" : 1 - } -] -)"; - g_assert_cmpstr(output, ==, expected); - g_free(cmdline); - g_free(output); - g_free(erroutput); -} - -int -main(int argc, char* argv[]) -{ - mu_test_init(&argc, &argv); - - if (!set_en_us_utf8_locale()) - return 0; /* don't error out... */ - - TempDir tmpdir{}; - CONTACTS_CACHE = fill_contacts_cache(tmpdir.path()); - - g_test_add_func("/mu-cmd-cfind/test-mu-cfind-plain", test_mu_cfind_plain); - g_test_add_func("/mu-cmd-cfind/test-mu-cfind-bbdb", test_mu_cfind_bbdb); - g_test_add_func("/mu-cmd-cfind/test-mu-cfind-wl", test_mu_cfind_wl); - g_test_add_func("/mu-cmd-cfind/test-mu-cfind-mutt-alias", test_mu_cfind_mutt_alias); - g_test_add_func("/mu-cmd-cfind/test-mu-cfind-mutt-ab", test_mu_cfind_mutt_ab); - g_test_add_func("/mu-cmd-cfind/test-mu-cfind-org-contact", test_mu_cfind_org_contact); - g_test_add_func("/mu-cmd-cfind/test-mu-cfind-csv", test_mu_cfind_csv); - g_test_add_func("/mu-cmd-cfind/test-mu-cfind-json", test_mu_cfind_json); - - return g_test_run(); -} diff --git a/mu/tests/test-mu-cmd.cc b/mu/tests/test-mu-cmd.cc deleted file mode 100644 index 34d4d719..00000000 --- a/mu/tests/test-mu-cmd.cc +++ /dev/null @@ -1,850 +0,0 @@ -/* -** Copyright (C) 2008-2023 Dirk-Jan C. Binnema -** -** This program is free software; you can redistribute it and/or modify it -** under the terms of the GNU General Public License as published by the -** Free Software Foundation; either version 3, or (at your option) any -** later version. -** -** This program 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 General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software Foundation, -** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -** -*/ - -#include "config.h" - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "utils/mu-test-utils.hh" -#include "mu-store.hh" -#include "mu-query.hh" -#include "utils/mu-result.hh" -#include "utils/mu-utils.hh" -#include "utils/mu-utils-file.hh" - -using namespace Mu; - -/* tests for the command line interface, uses testdir2 */ - -static std::string DBPATH; /* global */ - -static void -fill_database(void) -{ - GError* err{}; - auto&& cmdline = mu_format("/bin/sh -c '" - "{} --quiet init --muhome={} --maildir={} ; " - "{} --quiet index --muhome={} '", - MU_PROGRAM, - DBPATH, - MU_TESTMAILDIR2, - MU_PROGRAM, - DBPATH); - if (g_test_verbose()) - mu_println("{}", cmdline); - - err = NULL; - if (!g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, &err)) { - mu_printerrln("Error: {}", err ? err->message : "?"); - g_clear_error(&err); - g_assert(0); - } -} - -static unsigned -newlines_in_output(const char* str) -{ - int count; - - count = 0; - - while (str && *str) { - if (*str == '\n') - ++count; - ++str; - } - - return count; -} - -static size_t -search_func(const char* query, unsigned expected) -{ - size_t lines; - gchar *cmdline, *output, *erroutput; - - cmdline = g_strdup_printf("%s find --muhome=%s %s", MU_PROGRAM, - DBPATH.c_str(), query); - - g_debug("%s", cmdline); - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - if (g_test_verbose()) - g_print("\nOutput:\n%s", output); - - lines = newlines_in_output(output); - - /* we expect zero lines of error output if there is a match; - * otherwise there should be one line 'No matches found' */ - /* g_assert_cmpuint (newlines_in_output(erroutput),==, */ - /* expected == 0 ? 1 : 0); */ - - g_free(output); - g_free(erroutput); - g_free(cmdline); - - return lines; -} - -#define search(Q,EXP) do { \ - unsigned lines = search_func(Q, EXP); \ - g_assert_cmpuint(lines, ==, EXP); \ -} while(0) - - -/* index testdir2, and make sure it adds two documents */ -static void -test_mu_index(void) -{ - auto store = Store::make(DBPATH + "/xapian"); - assert_valid_result(store); - g_assert_cmpuint(store->size(), ==, 13); -} - -static void -test_mu_find_empty_query(void) -{ - search("\"\"", 13); -} - -static void -test_mu_find_01(void) -{ - search("f:john fruit", 1); - search("f:soc@example.com", 1); - search("t:alki@example.com", 1); - search("t:alcibiades", 1); - search("http emacs", 1); - search("f:soc@example.com OR f:john", 2); - search("f:soc@example.com OR f:john OR t:edmond", 3); - search("t:julius", 1); - search("s:dude", 1); - search("t:dantès", 1); -} - -/* index testdir2, and make sure it adds two documents */ -static void -test_mu_find_02(void) -{ - /* when matching html as if it were text, - * 'bull' is also matched in arto.eml, • however, - * we don't do that anymore! */ - - search("bull", 1); - - search("g:x", 0); - search("flag:encrypted", 0); - search("flag:attach", 1); - - search("i:3BE9E6535E0D852173@emss35m06.us.lmco.com", 1); -} - -static void -test_mu_find_file(void) -{ - search("file:sittingbull.jpg", 1); - search("file:custer.jpg", 1); - search("file:custer.*", 1); - search("j:sit*", 1); -} - -static void -test_mu_find_mime(void) -{ - search("mime:image/jpeg", 1); - search("mime:text/plain", 13); - search("y:text*", 13); - search("y:image*", 1); - search("mime:message/rfc822", 2); -} - -static void -test_mu_find_text_in_rfc822(void) -{ - search("embed:dancing", 1); - search("e:curious", 1); - search("embed:with", 2); - search("e:karjala", 0); - search("embed:navigation", 1); -} - -/* some more tests */ - -static void /* error cases */ -test_mu_find_03() -{ - gchar *cmdline, *erroutput; - - cmdline = g_strdup_printf("find %s --muhome=%cfoo%cbar%cnonexistent " - "f:socrates", - MU_PROGRAM, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - - g_assert(g_spawn_command_line_sync(cmdline, NULL, &erroutput, NULL, NULL)); - - /* we expect multiple lines of error output */ - g_assert_cmpuint(newlines_in_output(erroutput), >=, 1); - - g_free(erroutput); - g_free(cmdline); -} - -static void -test_mu_find_links(void) -{ - char *output, *erroutput; - const auto tmpdir{test_random_tmpdir()}; - auto cmdline = mu_format("{} find --muhome={} --format=links --linksdir={} " - "mime:message/rfc822", MU_PROGRAM, DBPATH, tmpdir); - if (g_test_verbose()) - mu_println("cmdline: %s", cmdline); - - g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL)); - /* there should be no errors */ - g_assert_cmpuint(newlines_in_output(output), ==, 0); - g_assert_cmpuint(newlines_in_output(erroutput), ==, 0); - g_free(output); - g_free(erroutput); - output = erroutput = NULL; - - /* furthermore, two symlinks should be there */ - const auto f1{mu_format("{}/cur/rfc822.1", tmpdir)}; - const auto f2{mu_format("{}/cur/rfc822.2", tmpdir)}; - - g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK); - g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK); - - /* now we try again, we should get a line of error output, - * when we find the first target file already exists */ - - if (g_test_verbose()) - mu_println("cmdline: {}", cmdline); - - g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL)); - g_assert_cmpuint(newlines_in_output(output), ==, 0); - g_assert_cmpuint(newlines_in_output(erroutput), ==, 1); - g_free(output); - g_free(erroutput); - output = erroutput = NULL; - - /* now we try again with --clearlinks, and the we should be - * back to 0 errors */ - cmdline = mu_format("{} find --muhome={} --format=links --linksdir={} --clearlinks " - "mime:message/rfc822", MU_PROGRAM, DBPATH, tmpdir); - - g_assert(g_spawn_command_line_sync(cmdline.c_str(), &output, &erroutput, NULL, NULL)); - if (g_test_verbose()) - mu_println("cmdline: {}", cmdline); - g_assert_cmpuint(newlines_in_output(output), ==, 0); - g_assert_cmpuint(newlines_in_output(erroutput), ==, 0); - g_free(output); - g_free(erroutput); - - g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK); - g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK); -} - -/* some more tests */ -static void -test_mu_find_maildir_special(void) -{ - search("\"maildir:/wOm_bàT\"", 3); - search("\"maildir:/wOm*\"", 3); - search("\"maildir:/wOm_*\"", 3); - search("\"maildir:wom_bat\"", 0); - search("\"maildir:/wombat\"", 0); - search("subject:atoms", 1); - search("\"maildir:/wom_bat\" subject:atoms", 1); -} - -/* static void */ -/* test_mu_find_mime_types (void) */ -/* { */ -/* /\* ensure that maldirs with spaces in their names work... *\/ */ -/* search ("\"maildir:/wom bat\" subject:atoms", 1); */ -/* search ("\"maildir:/wOm_bàT\"", 3); */ -/* search ("subject:atoms", 1); */ -/* } */ - -// static void -// test_mu_extract_01(void) -// { -// gchar *cmdline, *output, *erroutput, *tmpdir; - -// tmpdir = test_mu_common_get_random_tmpdir(); -// g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - -// cmdline = g_strdup_printf("%s extract %s%cFoo%ccur%cmail5", -// MU_PROGRAM, -// MU_TESTMAILDIR2, -// G_DIR_SEPARATOR, -// G_DIR_SEPARATOR, -// G_DIR_SEPARATOR); - -// if (g_test_verbose()) -// g_print("cmd: %s\n", cmdline); - -// output = erroutput = NULL; -// g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - -// // ERROR:test-mu-cmd.cc:347:void test_mu_extract_01(): assertion failed (output == -// // "MIME-parts in this message:\n" " 1 text/plain [] (27 bytes)\n" " 2 -// // sittingbull.jpg image/jpeg [inline] (23.9\302\240kB)\n" " 3 custer.jpg image/jpeg -// // [inline] (21.6\302\240kB)\n"): -// // ("MIME-parts in this message:\n 1 text/plain [] (27 bytes)\n 2 -// // sittingbull.jpg image/jpeg [inline] (23.9 kB)\n 3 custer.jpg image/jpeg [inline] (21.6 -// // kB)\n" == "MIME-parts in this message:\n 1 text/plain [] (27 bytes)\n 2 -// // sittingbull.jpg image/jpeg [inline] (23.9\302\240kB)\n 3 custer.jpg image/jpeg [inline] -// // (21.6\302\240kB)\n") -// /* g_assert_cmpstr (output, */ -// /* ==, */ -// /* "MIME-parts in this message:\n" */ -// /* " 1 text/plain [] (27 bytes)\n" */ -// /* " 2 sittingbull.jpg image/jpeg [inline] (23.9\302\240kB)\n" */ -// /* " 3 custer.jpg image/jpeg [inline] (21.6\302\240kB)\n"); */ - -// /* we expect zero lines of error output */ -// g_assert_cmpuint(newlines_in_output(erroutput), ==, 0); - -// g_free(output); -// g_free(erroutput); -// g_free(cmdline); -// g_free(tmpdir); -// } - -static gint64 -get_file_size(const char* path) -{ - int rv; - struct stat statbuf; - - rv = stat(path, &statbuf); - if (rv != 0) { - g_debug ("error: %s", g_strerror (errno)); - return -1; - } - - return (gint64)statbuf.st_size; -} - -static void -test_mu_extract_02(void) -{ - gchar *cmdline, *output, *tmpdir; - gchar *att1, *att2; - size_t size; - - tmpdir = test_mu_common_get_random_tmpdir(); - - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s extract -a " - "--target-dir=%s %s%cFoo%ccur%cmail5", - MU_PROGRAM, - tmpdir, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - output = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, NULL, NULL)); - g_assert_cmpstr(output, ==, ""); - - att1 = g_strdup_printf("%s%ccuster.jpg", tmpdir, G_DIR_SEPARATOR); - att2 = g_strdup_printf("%s%csittingbull.jpg", tmpdir, G_DIR_SEPARATOR); - - size = get_file_size(att1); - g_assert_cmpuint(size, >=, 15955); - g_assert_cmpuint(size, <=, 15960); - - size = get_file_size(att2); - g_assert_cmpint(size, ==, 17674); - - g_free(output); - g_free(tmpdir); - g_free(cmdline); - g_free(att1); - g_free(att2); -} - -static void -test_mu_extract_03(void) -{ - gchar *cmdline, *output, *tmpdir; - gchar *att1, *att2; - size_t size; - - tmpdir = test_mu_common_get_random_tmpdir(); - - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s extract --parts 3 " - "--target-dir=%s %s%cFoo%ccur%cmail5", - MU_PROGRAM, - tmpdir, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - output = NULL; - - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, NULL, NULL)); - g_assert_cmpstr(output, ==, ""); - - att1 = g_strdup_printf("%s%ccuster.jpg", tmpdir, G_DIR_SEPARATOR); - att2 = g_strdup_printf("%s%csittingbull.jpg", tmpdir, G_DIR_SEPARATOR); - - size = get_file_size(att1); - g_assert_cmpuint(size, >=, 15955); - g_assert_cmpuint(size, <=, 15960); - g_assert_cmpint(get_file_size(att2), ==, -1); - - g_free(output); - g_free(tmpdir); - g_free(cmdline); - g_free(att1); - g_free(att2); -} - -static void -test_mu_extract_overwrite(void) -{ - gchar *cmdline, *output, *erroutput, *tmpdir; - - tmpdir = test_mu_common_get_random_tmpdir(); - - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s extract -a " - "--target-dir=%s %s%cFoo%ccur%cmail5", - MU_PROGRAM, - tmpdir, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - g_assert_cmpstr(output, ==, ""); - g_assert_cmpstr(erroutput, ==, ""); - g_free(erroutput); - g_free(output); - - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - /* now, it should fail, because we don't allow overwrites - * without --overwrite */ - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - g_assert_cmpstr(output, ==, ""); - g_assert_cmpstr(erroutput, !=, ""); - g_free(erroutput); - g_free(output); - - g_free(cmdline); - /* this should work now, because we have specified --overwrite */ - cmdline = g_strdup_printf("%s extract -a --overwrite " - "--target-dir=%s %s%cFoo%ccur%cmail5", - MU_PROGRAM, - tmpdir, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - g_assert_cmpstr(output, ==, ""); - g_assert_cmpstr(erroutput, ==, ""); - g_free(erroutput); - g_free(output); - - g_free(tmpdir); - g_free(cmdline); -} - -static void -test_mu_extract_by_name(void) -{ - gchar *cmdline, *output, *erroutput, *tmpdir, *path; - - tmpdir = test_mu_common_get_random_tmpdir(); - - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s extract " - "--target-dir=%s %s%cFoo%ccur%cmail5 " - "sittingbull.jpg", - MU_PROGRAM, - tmpdir, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - g_assert(g_spawn_command_line_sync(cmdline, &output, &erroutput, NULL, NULL)); - g_assert_cmpstr(output, ==, ""); - g_assert_cmpstr(erroutput, ==, ""); - path = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "sittingbull.jpg"); - g_assert(access(path, F_OK) == 0); - g_free(path); - - g_free(erroutput); - g_free(output); - - g_free(tmpdir); - g_free(cmdline); -} - -static void -test_mu_view_01(void) -{ - gchar *cmdline, *output, *tmpdir; - int len; - - tmpdir = test_mu_common_get_random_tmpdir(); - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s view %s%cbar%ccur%cmail4", - MU_PROGRAM, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - output = NULL; - - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, NULL, NULL)); - g_assert_cmpstr(output, !=, NULL); - - /* - * note: there are two possibilities here; older versions of - * GMime will produce: - * - * From: "=?iso-8859-1?Q? =F6tzi ?=" - * - * while newer ones return something like: - * - * From: ?tzi - * - * or even - * - * From: \xc3\xb6tzi - * - * both are 'okay' from mu's perspective; it'd be even better - * to have some #ifdefs for the GMime versions, but this - * should work for now - * - * Added 350 as 'okay', which comes with gmime 2.4.24 (ubuntu 10.04) - */ - len = strlen(output); - if (len < 339) { - g_print ("\n[%s] (%d)\n", output, len); - } - g_assert_cmpuint(len, >=, 339); - g_free(output); - g_free(cmdline); - g_free(tmpdir); -} - -static void -test_mu_view_multi(void) -{ - gchar *cmdline, *output, *tmpdir; - int len; - - tmpdir = test_mu_common_get_random_tmpdir(); - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s view " - "%s%cbar%ccur%cmail5 " - "%s%cbar%ccur%cmail5", - MU_PROGRAM, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - output = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, NULL, NULL)); - g_assert_cmpstr(output, !=, NULL); - - len = strlen(output); - if (g_test_verbose()) - g_print ("\n[%s](%u)\n", output, len); - g_assert_cmpuint(len, >=, 112); - - g_free(output); - g_free(cmdline); - g_free(tmpdir); -} - -static void -test_mu_view_multi_separate(void) -{ - gchar *cmdline, *output, *tmpdir; - int len; - - tmpdir = test_mu_common_get_random_tmpdir(); - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s view --terminate " - "%s%cbar%ccur%cmail5 " - "%s%cbar%ccur%cmail5", - MU_PROGRAM, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - output = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, NULL, NULL)); - g_assert_cmpstr(output, !=, NULL); - - len = strlen(output); - if (g_test_verbose()) - g_print ("\n[%s](%u)\n", output, len); - g_print ("\n[%s](%u)\n", output, len); - //g_assert_cmpuint(len, >=, 112); - - g_free(output); - g_free(cmdline); - g_free(tmpdir); -} - -static void -test_mu_view_attach(void) -{ - gchar *cmdline, *output, *tmpdir; - int len; - - tmpdir = test_mu_common_get_random_tmpdir(); - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s view --muhome=%s %s%cFoo%ccur%cmail5", - MU_PROGRAM, - tmpdir, - MU_TESTMAILDIR2, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR, - G_DIR_SEPARATOR); - output = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, NULL, NULL)); - g_assert_cmpstr(output, !=, NULL); - - len = strlen(output); - if (g_test_verbose()) - g_print ("\n[%s](%u)\n", output, len); - //g_assert(len == 168 || len == 166); - - g_free(output); - g_free(cmdline); - g_free(tmpdir); -} - -static void -test_mu_mkdir_01(void) -{ - gchar *cmdline, *output, *tmpdir; - gchar* dir; - - tmpdir = test_mu_common_get_random_tmpdir(); - g_assert(g_mkdir_with_parents(tmpdir, 0700) == 0); - - cmdline = g_strdup_printf("%s mkdir %s%ctest1 %s%ctest2", - MU_PROGRAM, - tmpdir, - G_DIR_SEPARATOR, - tmpdir, - G_DIR_SEPARATOR); - - output = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, NULL, NULL)); - g_assert_cmpstr(output, ==, ""); - - dir = g_strdup_printf("%s%ctest1%ccur", tmpdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR); - g_assert(access(dir, F_OK) == 0); - g_free(dir); - - dir = g_strdup_printf("%s%ctest2%ctmp", tmpdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR); - g_assert(access(dir, F_OK) == 0); - g_free(dir); - - dir = g_strdup_printf("%s%ctest1%cnew", tmpdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR); - g_assert(access(dir, F_OK) == 0); - g_free(dir); - - g_free(output); - g_free(tmpdir); - g_free(cmdline); -} - -/* we can only test 'verify' if gpg is installed, and has - * djcb@djcbsoftware's key in the keyring */ -G_GNUC_UNUSED static gboolean -verify_is_testable(void) -{ - gchar * gpg, *cmdline; - gchar * output, *erroutput; - int retval; - gboolean rv; - - /* find GPG or return FALSE */ - if ((gpg = (char*)g_getenv("MU_GPG_PATH"))) { - if (access(gpg, X_OK) != 0) - return FALSE; - else - gpg = g_strdup(gpg); - - } else if (!(gpg = g_find_program_in_path("gpg2"))) - return FALSE; - - cmdline = g_strdup_printf("%s --list-keys DCC4A036", gpg); - g_free(gpg); - - output = erroutput = NULL; - rv = g_spawn_command_line_sync(cmdline, &output, &erroutput, &retval, NULL); - g_free(output); - g_free(erroutput); - g_free(cmdline); - - return (rv && retval == 0) ? TRUE : FALSE; -} - -G_GNUC_UNUSED static void -test_mu_verify_good(void) -{ - gchar *cmdline, *output; - int retval; - - if (!verify_is_testable()) - return; - - cmdline = g_strdup_printf("%s verify '%s/signed!2,S'", MU_PROGRAM, MU_TESTMAILDIR4); - - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - output = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, &retval, NULL)); - g_free(output); - g_assert_cmpuint(retval, ==, 0); - g_free(cmdline); -} - -G_GNUC_UNUSED static void -test_mu_verify_bad(void) -{ - gchar *cmdline, *output; - int retval; - - if (!verify_is_testable()) - return; - - cmdline = g_strdup_printf("%s verify '%s/signed-bad!2,S'", MU_PROGRAM, MU_TESTMAILDIR4); - - if (g_test_verbose()) - g_print("$ %s\n", cmdline); - - output = NULL; - g_assert(g_spawn_command_line_sync(cmdline, &output, NULL, &retval, NULL)); - g_free(output); - g_assert_cmpuint(retval, !=, 0); - g_free(cmdline); -} - -int -main(int argc, char* argv[]) -{ - int rv; - - mu_test_init(&argc, &argv); - - if (!set_en_us_utf8_locale()) - return 0; /* don't error out... */ - - g_test_add_func("/mu-cmd/test-mu-index", test_mu_index); - - g_test_add_func("/mu-cmd/test-mu-find-empty-query", test_mu_find_empty_query); - g_test_add_func("/mu-cmd/test-mu-find-01", test_mu_find_01); - g_test_add_func("/mu-cmd/test-mu-find-02", test_mu_find_02); - - g_test_add_func("/mu-cmd/test-mu-find-file", test_mu_find_file); - g_test_add_func("/mu-cmd/test-mu-find-mime", test_mu_find_mime); - - g_test_add_func ("/mu-cmd/test-mu-find-links", test_mu_find_links); - - g_test_add_func("/mu-cmd/test-mu-find-text-in-rfc822", test_mu_find_text_in_rfc822); - - g_test_add_func("/mu-cmd/test-mu-find-03", test_mu_find_03); - g_test_add_func("/mu-cmd/test-mu-find-maildir-special", test_mu_find_maildir_special); - // g_test_add_func("/mu-cmd/test-mu-extract-01", test_mu_extract_01); - g_test_add_func("/mu-cmd/test-mu-extract-02", test_mu_extract_02); - g_test_add_func("/mu-cmd/test-mu-extract-03", test_mu_extract_03); - g_test_add_func("/mu-cmd/test-mu-extract-overwrite", test_mu_extract_overwrite); - g_test_add_func("/mu-cmd/test-mu-extract-by-name", test_mu_extract_by_name); - - g_test_add_func("/mu-cmd/test-mu-view-01", test_mu_view_01); - g_test_add_func("/mu-cmd/test-mu-view-multi", test_mu_view_multi); - g_test_add_func("/mu-cmd/test-mu-view-multi-separate", test_mu_view_multi_separate); - g_test_add_func("/mu-cmd/test-mu-view-attach", test_mu_view_attach); - g_test_add_func("/mu-cmd/test-mu-mkdir-01", test_mu_mkdir_01); - - g_test_add_func("/mu-cmd/test-mu-verify-good", test_mu_verify_good); - g_test_add_func("/mu-cmd/test-mu-verify-bad", test_mu_verify_bad); - - TempDir tempdir; - DBPATH = tempdir.path(); - fill_database(); - - rv = g_test_run(); - - return rv; -}