aboutsummaryrefslogtreecommitdiffstats
path: root/camel/tests/lib
diff options
context:
space:
mode:
authorNot Zed <NotZed@HelixCode.com>2000-11-24 11:18:20 +0800
committerMichael Zucci <zucchi@src.gnome.org>2000-11-24 11:18:20 +0800
commit99e80d6ecf06cc60f2734f87bc974bd9479ba139 (patch)
tree84b8922548128a99dd89e2ecb96fba5f21d94f78 /camel/tests/lib
parenta7e18523ff09dd48a0aae229e1416c6d021dbb29 (diff)
downloadgsoc2013-evolution-99e80d6ecf06cc60f2734f87bc974bd9479ba139.tar.gz
gsoc2013-evolution-99e80d6ecf06cc60f2734f87bc974bd9479ba139.tar.zst
gsoc2013-evolution-99e80d6ecf06cc60f2734f87bc974bd9479ba139.zip
Add tests.
2000-11-24 Not Zed <NotZed@HelixCode.com> * Makefile.am (SUBDIRS): Add tests. * camel-mime-filter-basic.c (filter): Well, I'll add the extra bytes here too, lathough not strictly needed, might save a re-malloc when we get to complete(). * camel-mime-filter-charset.c (filter): Make sure we have room if we only convert very short data. (complete): and here too. * tests/Makefile.am: Initial test harness & tests. Requires gcc for this. * camel-internet-address.c (d): Turn off debug. * camel-charset-map.c (camel_charset_step): Oops, & masks for set intersection, not | them. Dunno how this got even close to working. 2000-11-23 Not Zed <NotZed@HelixCode.com> * camel-mime-filter-basic.c (filter): For base64 encoding, the output size for 0, 1, or 2 bytes of input can exceed input*2, so make sure we account for that as well. (complete): And here. (complete): Similarly for qp encoding, if we have a trailing space, we need some extra bytes (not needed for 'filter()', as any such bytes are stored in state/save). * camel-mime-utils.c (quoted_decode_step): Removed fixme not required. (quoted_encode_close): Dont append a trailing afterall. Otherwise a pass through the encode/decode will grow the message each time. svn path=/trunk/; revision=6656
Diffstat (limited to 'camel/tests/lib')
-rw-r--r--camel/tests/lib/Makefile.am10
-rw-r--r--camel/tests/lib/addresses.c51
-rw-r--r--camel/tests/lib/addresses.h5
-rw-r--r--camel/tests/lib/camel-test.c240
-rw-r--r--camel/tests/lib/camel-test.h52
-rw-r--r--camel/tests/lib/messages.c145
-rw-r--r--camel/tests/lib/messages.h12
7 files changed, 515 insertions, 0 deletions
diff --git a/camel/tests/lib/Makefile.am b/camel/tests/lib/Makefile.am
new file mode 100644
index 0000000000..1fae79671d
--- /dev/null
+++ b/camel/tests/lib/Makefile.am
@@ -0,0 +1,10 @@
+
+check_LIBRARIES = libcameltest.a
+
+libcameltest_a_SOURCES = \
+ camel-test.c camel-test.h \
+ messages.c messages.h \
+ addresses.c addresses.h
+
+
+
diff --git a/camel/tests/lib/addresses.c b/camel/tests/lib/addresses.c
new file mode 100644
index 0000000000..a97ac5e4f9
--- /dev/null
+++ b/camel/tests/lib/addresses.c
@@ -0,0 +1,51 @@
+
+#include "addresses.h"
+#include "camel-test.h"
+
+void
+test_address_compare(CamelInternetAddress *addr, CamelInternetAddress *addr2)
+{
+ const char *r1, *r2, *a1, *a2;
+ char *e1, *e2, *f1, *f2;
+ int j;
+
+ check(camel_address_length(CAMEL_ADDRESS(addr)) == camel_address_length(CAMEL_ADDRESS(addr2)));
+ for (j=0;j<camel_address_length(CAMEL_ADDRESS(addr));j++) {
+
+ check(camel_internet_address_get(addr, j, &r1, &a1) == TRUE);
+ check(camel_internet_address_get(addr2, j, &r2, &a2) == TRUE);
+
+ check(string_equal(r1, r2));
+ check(strcmp(a1, a2) == 0);
+ }
+ check(camel_internet_address_get(addr, j, &r1, &a1) == FALSE);
+ check(camel_internet_address_get(addr2, j, &r2, &a2) == FALSE);
+
+ e1 = camel_address_encode(CAMEL_ADDRESS(addr));
+ e2 = camel_address_encode(CAMEL_ADDRESS(addr2));
+
+ if (camel_address_length(CAMEL_ADDRESS(addr)) == 0)
+ check(e1 == NULL && e2 == NULL);
+ else
+ check(e1 != NULL && e2 != NULL);
+
+ if (e1 != NULL) {
+ check_msg(string_equal(e1, e2), "e1 = '%s' e2 = '%s'", e1, e2);
+ test_free(e1);
+ test_free(e2);
+ }
+
+ f1 = camel_address_format(CAMEL_ADDRESS(addr));
+ f2 = camel_address_format(CAMEL_ADDRESS(addr2));
+
+ if (camel_address_length(CAMEL_ADDRESS(addr)) == 0)
+ check(f1 == NULL && f2 == NULL);
+ else
+ check(f1 != NULL && f2 != NULL);
+
+ if (f1 != NULL) {
+ check_msg(string_equal(f1, f2), "f1 = '%s' f2 = '%s'", f1, f2);
+ test_free(f1);
+ test_free(f2);
+ }
+}
diff --git a/camel/tests/lib/addresses.h b/camel/tests/lib/addresses.h
new file mode 100644
index 0000000000..dc35a19f05
--- /dev/null
+++ b/camel/tests/lib/addresses.h
@@ -0,0 +1,5 @@
+
+#include <camel/camel-internet-address.h>
+
+/* addresses.c */
+void test_address_compare(CamelInternetAddress *addr, CamelInternetAddress *addr2);
diff --git a/camel/tests/lib/camel-test.c b/camel/tests/lib/camel-test.c
new file mode 100644
index 0000000000..b60ba1fb34
--- /dev/null
+++ b/camel/tests/lib/camel-test.c
@@ -0,0 +1,240 @@
+
+#include "camel-test.h"
+
+#include <stdio.h>
+#include <signal.h>
+
+struct _stack {
+ struct _stack *next;
+ char *what;
+};
+
+static int setup;
+static struct _stack *state;
+static struct _stack *nonfatal;
+static int ok;
+
+int camel_test_verbose;
+
+static void die(int sig)
+{
+ static indie = 0;
+ struct _stack *node;
+
+ if (!indie) {
+ indie = 1;
+ printf("\n\nReceived fatal signal %d\n", sig);
+ node = state;
+ if (node) {
+ printf("Current action:\n");
+ while (node) {
+ printf("\t%s\n", node->what);
+ node = node->next;
+ }
+ }
+ }
+
+ _exit(1);
+}
+
+void camel_test_init(int argc, char **argv)
+{
+ void camel_init(void);
+ int i;
+
+ setup = 1;
+
+ camel_init();
+
+ /* yeah, we do need ot thread init, even though camel isn't compiled with enable threads */
+ g_thread_init(NULL);
+
+ signal(SIGSEGV, die);
+ signal(SIGABRT, die);
+
+ /* default, just say what, how well we did, unless fail, then abort */
+ camel_test_verbose = 1;
+
+ for (i=0;i<argc;i++) {
+ if (argv[i][0] == '-') {
+ switch (argv[i][1]) {
+ case 'v':
+ camel_test_verbose = strlen(argv[i]);
+ break;
+ case 'q':
+ camel_test_verbose = 0;
+ break;
+ }
+ }
+ }
+}
+
+void camel_test_start(const char *what)
+{
+ if (!setup)
+ camel_test_init(0, 0);
+
+ ok = 1;
+
+ if (camel_test_verbose > 0) {
+ printf("Test: %s ... ", what);
+ fflush(stdout);
+ }
+}
+
+void camel_test_push(const char *what, ...)
+{
+ struct _stack *node;
+ va_list ap;
+ char *text;
+
+ va_start(ap, what);
+ text = g_strdup_vprintf(what, ap);
+ va_end(ap);
+
+ if (camel_test_verbose > 3)
+ printf("Start step: %s\n", text);
+
+ node = g_malloc(sizeof(*node));
+ node->what = text;
+ node->next = state;
+ state = node;
+}
+
+void camel_test_pull(void)
+{
+ struct _stack *node;
+
+ g_assert(state);
+
+ if (camel_test_verbose > 3)
+ printf("Finish step: %s\n", state->what);
+
+ node = state;
+ state = node->next;
+ g_free(node->what);
+ g_free(node);
+}
+
+void camel_test_fail(const char *why, ...)
+{
+ struct _stack *node;
+ va_list ap;
+ char *text;
+
+ va_start(ap, why);
+ text = g_strdup_vprintf(why, ap);
+ va_end(ap);
+
+ if ((nonfatal == NULL && camel_test_verbose > 0)
+ || (nonfatal && camel_test_verbose > 1)) {
+ printf("Failed: %s\n", text);
+ }
+
+ g_free(text);
+
+ if ((nonfatal == NULL && camel_test_verbose > 0)
+ || (nonfatal && camel_test_verbose > 2)) {
+ node = state;
+ if (node) {
+ printf("Current action:\n");
+ while (node) {
+ printf("\t%s\n", node->what);
+ node = node->next;
+ }
+ }
+ }
+
+ if (nonfatal == NULL) {
+ exit(1);
+ } else {
+ ok=0;
+ if (camel_test_verbose > 1) {
+ printf("Known problem (ignored): %s\n", nonfatal->what);
+ }
+ }
+}
+
+void camel_test_nonfatal(const char *why, ...)
+{
+ struct _stack *node;
+ va_list ap;
+ char *text;
+
+ va_start(ap, why);
+ text = g_strdup_vprintf(why, ap);
+ va_end(ap);
+
+ if (camel_test_verbose>3)
+ printf("Start nonfatal: %s\n", text);
+
+ node = g_malloc(sizeof(*node));
+ node->what = text;
+ node->next = nonfatal;
+ nonfatal = node;
+}
+
+/* dont ask me why but the compiler just can't seem to find the prototypes for this */
+void camel_test_fatal()
+{
+ struct _stack *node;
+
+ g_assert(nonfatal);
+
+ if (camel_test_verbose>3)
+ printf("Finish nonfatal: %s\n", nonfatal->what);
+
+ node = nonfatal;
+ nonfatal = node->next;
+ g_free(node->what);
+ g_free(node);
+}
+
+void camel_test_end(void)
+{
+ if (camel_test_verbose > 0) {
+ if (ok)
+ printf("Ok\n");
+ else
+ printf("Partial success\n");
+ }
+
+ fflush(stdout);
+}
+
+
+
+
+/* compare strings, ignore whitespace though */
+int string_equal(const char *a, const char *b)
+{
+ const char *ap, *bp;
+ int cmp;
+
+ ap = a;
+ bp = b;
+
+ while (*ap && *bp) {
+ while (*ap == ' ' || *ap == '\n' || *ap == '\t')
+ ap++;
+ while (*bp == ' ' || *bp == '\n' || *bp == '\t')
+ bp++;
+
+ a = ap;
+ b = bp;
+
+ while (*ap && *ap != ' ' && *ap != '\n' && *ap != '\t')
+ ap++;
+ while (*bp && *bp != ' ' && *bp != '\n' && *bp != '\t')
+ bp++;
+
+ if (ap - a != bp - a
+ && ap - 1 > 0
+ && memcmp(a, b, ap-a) != 0) {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
diff --git a/camel/tests/lib/camel-test.h b/camel/tests/lib/camel-test.h
new file mode 100644
index 0000000000..8d3c94bd00
--- /dev/null
+++ b/camel/tests/lib/camel-test.h
@@ -0,0 +1,52 @@
+
+/* some utilities for testing */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <glib.h>
+
+/* perform a check assertion */
+#define check(x) do {if (!(x)) { camel_test_fail("%s", #x); } } while (0)
+#define check_msg(x, y, z...) do {if (!(x)) { camel_test_fail("%s\n\t" #y, #x, ##z); } } while (0)
+
+#define check_count(object, expected) do { \
+ if (CAMEL_OBJECT(object)->ref_count != expected) { \
+ camel_test_fail("%s->ref_count != %s\n\tref_count = %d", #object, #expected, CAMEL_OBJECT(object)->ref_count); \
+ } \
+} while (0)
+
+#define check_unref(object, expected) do { \
+ check_count(object, expected); \
+ camel_object_unref(CAMEL_OBJECT(object)); \
+ if (expected == 1) { \
+ object = NULL; \
+ } \
+} while (0)
+
+#define test_free(mem) (g_free(mem), mem=NULL)
+
+#define push camel_test_push
+#define pull camel_test_pull
+
+void camel_test_init(int argc, char **argv);
+
+/* start/finish a new test */
+void camel_test_start(const char *what);
+void camel_test_end(void);
+
+/* start/finish a new test part */
+void camel_test_push(const char *what, ...);
+void camel_test_pull(void);
+
+/* fail a test, with a reason why */
+void camel_test_fail(const char *why, ...);
+
+/* Set whether a failed test quits. May be nested, but must be called in nonfatal/fatal pairs */
+void camel_test_nonfatal(const char *why, ...);
+void camel_test_fatal();
+
+
+/* utility functions */
+/* compare strings, ignore whitespace though */
+int string_equal(const char *a, const char *b);
diff --git a/camel/tests/lib/messages.c b/camel/tests/lib/messages.c
new file mode 100644
index 0000000000..e2e447d97c
--- /dev/null
+++ b/camel/tests/lib/messages.c
@@ -0,0 +1,145 @@
+
+
+#include "messages.h"
+#include "camel-test.h"
+
+#include <camel/camel-mime-message.h>
+#include <camel/camel-stream-fs.h>
+#include <camel/camel-stream-mem.h>
+
+CamelMimeMessage *
+test_message_create_simple(void)
+{
+ CamelMimeMessage *msg;
+ CamelInternetAddress *addr;
+
+ msg = camel_mime_message_new();
+
+ addr = camel_internet_address_new();
+ camel_internet_address_add(addr, "Michael Zucchi", "zed@nowhere.com");
+ camel_mime_message_set_from(msg, addr);
+ camel_address_remove((CamelAddress *)addr, -1);
+ camel_internet_address_add(addr, "POSTMASTER", "POSTMASTER@somewhere.net");
+ camel_mime_message_set_recipients(msg, CAMEL_RECIPIENT_TYPE_TO, addr);
+ camel_address_remove((CamelAddress *)addr, -1);
+ camel_internet_address_add(addr, "Michael Zucchi", "zed@nowhere.com");
+ camel_mime_message_set_recipients(msg, CAMEL_RECIPIENT_TYPE_CC, addr);
+
+ check_unref(addr, 1);
+
+ camel_mime_message_set_subject(msg, "Simple message subject");
+ camel_mime_message_set_date(msg, time(0), 930);
+
+ return msg;
+}
+
+void
+test_message_set_content_simple(CamelMimePart *part, int how, const char *type, const char *text, int len)
+{
+ CamelStreamMem *content = NULL;
+ CamelDataWrapper *dw;
+ static GByteArray *ba;
+
+ switch (how) {
+ case 0:
+ camel_mime_part_set_content(part, text, len, type);
+ break;
+ case 1:
+ content = (CamelStreamMem *)camel_stream_mem_new_with_buffer(text, len);
+ break;
+ case 2:
+ content = (CamelStreamMem *)camel_stream_mem_new();
+ camel_stream_mem_set_buffer(content, text, len);
+ break;
+ case 3:
+ ba = g_byte_array_new();
+ g_byte_array_append(ba, text, len);
+
+ content = (CamelStreamMem *)camel_stream_mem_new_with_byte_array(ba);
+ ba = NULL;
+ break;
+ case 4:
+ ba = g_byte_array_new();
+ g_byte_array_append(ba, text, len);
+
+ content = (CamelStreamMem *)camel_stream_mem_new();
+ camel_stream_mem_set_byte_array(content, ba);
+
+ /* ba gets leaked here */
+ break;
+ }
+
+ if (content != 0) {
+ dw = camel_data_wrapper_new();
+ camel_data_wrapper_set_mime_type (dw, type);
+
+ camel_data_wrapper_construct_from_stream(dw, (CamelStream *)content);
+ camel_medium_set_content_object((CamelMedium *)part, dw);
+
+ check_unref(content, 2);
+ check_unref(dw, 2);
+ }
+}
+
+int
+test_message_write_file(CamelMimeMessage *msg, const char *name)
+{
+ CamelStreamFs *file;
+ int ret;
+
+ file = (CamelStreamFs *)camel_stream_fs_new_with_name(name, O_CREAT|O_WRONLY, 0600);
+ camel_data_wrapper_write_to_stream((CamelDataWrapper *)msg, (CamelStream *)file);
+ ret = camel_stream_close((CamelStream *)file);
+
+ check(((CamelObject *)file)->ref_count == 1);
+ camel_object_unref((CamelObject *)file);
+
+ return ret;
+}
+
+CamelMimeMessage *
+test_message_read_file(const char *name)
+{
+ CamelStreamFs *file;
+ CamelMimeMessage *msg2;
+
+ file = (CamelStreamFs *)camel_stream_fs_new_with_name(name, O_RDONLY, 0);
+ msg2 = camel_mime_message_new();
+
+ camel_data_wrapper_construct_from_stream((CamelDataWrapper *)msg2, (CamelStream *)file);
+ /* file's refcount may be > 1 if the message is real big */
+ check(CAMEL_OBJECT(file)->ref_count >=1);
+
+ return msg2;
+}
+
+int
+test_message_compare_content(CamelDataWrapper *dw, const char *text, int len)
+{
+ CamelStreamMem *content;
+
+ /* sigh, ok, so i len == 0, dw will probably be 0 too
+ camel_mime_part_set_content is weird like that */
+ if (dw == 0 && len == 0)
+ return 0;
+
+ content = (CamelStreamMem *)camel_stream_mem_new();
+ camel_data_wrapper_write_to_stream(dw, (CamelStream *)content);
+
+ check_msg(content->buffer->len == len, "buffer->len = %d, len = %d", content->buffer->len, len);
+ check_msg(memcmp(content->buffer->data, text, content->buffer->len) == 0, "len = %d", len);
+
+ check_unref(content, 1);
+
+ return 0;
+}
+
+int
+test_message_compare_header(CamelMimeMessage *m1, CamelMimeMessage *m2)
+{
+}
+
+int
+test_message_compare_messages(CamelMimeMessage *m1, CamelMimeMessage *m2)
+{
+}
diff --git a/camel/tests/lib/messages.h b/camel/tests/lib/messages.h
new file mode 100644
index 0000000000..9cb5758826
--- /dev/null
+++ b/camel/tests/lib/messages.h
@@ -0,0 +1,12 @@
+
+#include <camel/camel-mime-message.h>
+
+/* how many ways to set the content contents */
+#define SET_CONTENT_WAYS (5)
+
+/* messages.c */
+CamelMimeMessage *test_message_create_simple(void);
+void test_message_set_content_simple(CamelMimePart *part, int how, const char *type, const char *text, int len);
+int test_message_write_file(CamelMimeMessage *msg, const char *name);
+CamelMimeMessage *test_message_read_file(const char *name);
+int test_message_compare_content(CamelDataWrapper *dw, const char *text, int len);