diff --git a/src/mime/backend/Makefile b/src/mime/backend/Makefile index b1e61a0c0..eb9300cc5 100644 --- a/src/mime/backend/Makefile +++ b/src/mime/backend/Makefile @@ -6,4 +6,26 @@ OBJS-$(CONFIG_MIMETYPES) += mimetypes.o OBJS = common.o default.o +TEST_PROGS = \ + mailcap-cache + +# The dependencies are a bit funny here! I don't know why. Just remember to +# make clean before making the test. --jonas +mailcap-cache.o: mailcap.c + $(call cmd,compile,-DTEST_MAILCAP) + +TESTDEPS = \ + common.o \ + $(top_builddir)/src/osdep/osdep.o \ + $(top_builddir)/src/osdep/stub.o \ + $(top_builddir)/src/util/conv.o \ + $(top_builddir)/src/util/error.o \ + $(top_builddir)/src/util/file.o \ + $(top_builddir)/src/util/hash.o \ + $(top_builddir)/src/util/memory.o \ + $(top_builddir)/src/util/string.o \ + $(top_builddir)/src/util/time.o + +TESTDEPS-$(CONFIG_DEBUG) += $(top_builddir)/src/util/memdebug.o + include $(top_srcdir)/Makefile.lib diff --git a/src/mime/backend/mailcap.c b/src/mime/backend/mailcap.c index bf9daec38..e3eccec55 100644 --- a/src/mime/backend/mailcap.c +++ b/src/mime/backend/mailcap.c @@ -443,6 +443,8 @@ done_mailcap(struct module *module) mailcap_map_size = 0; } +#ifndef TEST_MAILCAP + static int change_hook_mailcap(struct session *ses, struct option *current, struct option *changed) { @@ -469,6 +471,10 @@ init_mailcap(struct module *module) get_mailcap_enable() = 0; } +#else +#define init_mailcap NULL +#endif /* TEST_MAILCAP */ + /* The command semantics include the following: * * %s is the filename that contains the mail body data @@ -673,3 +679,108 @@ struct module mailcap_mime_module = struct_module( /* init: */ init_mailcap, /* done: */ done_mailcap ); + +#ifdef TEST_MAILCAP +/* Some ugly shortcuts for getting defined symbols to work. */ +int default_mime_backend, + install_signal_handler, + mimetypes_mime_backend; +struct list_head terminals; + +void die(const char *msg, ...) +{ + va_list args; + + if (msg) { + va_start(args, msg); + vfprintf(stderr, msg, args); + fputs("\n", stderr); + va_end(args); + } + + exit(1); +} + +int +main(int argc, char *argv[]) +{ + unsigned char *format = "description,ask,block,program"; + int has_gotten = 0; + int i; + + for (i = 1; i < argc; i++) { + char *arg = argv[i]; + + if (strncmp(arg, "--", 2)) + break; + + arg += 2; + + if (!strncmp(arg, "path", 4)) { + arg += 4; + if (*arg == '=') { + arg++; + get_mailcap_path() = arg; + } else { + i++; + if (i >= argc) + die("--path expects a parameter"); + get_mailcap_path() = argv[i]; + } + done_mailcap(NULL); + + } else if (!strncmp(arg, "format", 6)) { + arg += 6; + if (*arg == '=') { + arg++; + format = arg; + } else { + i++; + if (i >= argc) + die("--format expects a parameter"); + format = argv[i]; + } + + } else if (!strncmp(arg, "get", 3)) { + struct mime_handler *handler; + + arg += 3; + if (*arg == '=') { + arg++; + } else { + i++; + if (i >= argc) + die("--get expects a parameter"); + arg = argv[i]; + } + + if (has_gotten) + printf("\n"); + has_gotten = 1; + printf("type: %s\n", arg); + handler = get_mime_handler_mailcap(arg, 0); + if (!handler) continue; + + if (strstr(format, "description")) + printf("description: %s\n", handler->description); + + if (strstr(format, "ask")) + printf("ask: %d\n", handler->ask); + + if (strstr(format, "block")) + printf("block: %d\n", handler->block); + + if (strstr(format, "program")) + printf("program: %s\n", handler->program); + + } else { + die("Unknown argument '%s'", arg - 2); + } + } + + done_mailcap(NULL); + + return 0; +} + +#endif /* TEST_MAILCAP */ diff --git a/src/mime/backend/test-mailcap-cache b/src/mime/backend/test-mailcap-cache new file mode 100755 index 000000000..6bd9bbf07 --- /dev/null +++ b/src/mime/backend/test-mailcap-cache @@ -0,0 +1,95 @@ +#!/bin/sh +# +# Copyright (c) 2005 Jonas Fonseca +# + +test_description='Test mailcap parsing and querying + +This tests the parsing of various mailcap files, if they are +"prioritised" correctly, if the test are run correctly and +if querying returns the expected mailcap entry. +' + +. "$TEST_LIB" + +# Set PAGER to something recognisable since it gets appended as +# "|copiousoutput_handler" to entries with copiousoutput. +export PAGER=copiousoutput_handler + +################################################################ +# Parse a simple mailcap file + +cat > mailcap-basic <&1 ; copiousoutput +EOF + +mailcap-cache \ + --path "mailcap-basic" \ + --format "block,program" \ + --get "text/html" \ + --get "text/x-csh" \ + --get "application/postscript" \ + --get "application/foo" \ + > output + +cat > expected < mailcap-simple-with-test < output + +cat > expected <