aboutsummaryrefslogtreecommitdiffstats
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
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
-rw-r--r--camel/ChangeLog35
-rw-r--r--camel/Makefile.am2
-rw-r--r--camel/camel-charset-map.c2
-rw-r--r--camel/camel-internet-address.c2
-rw-r--r--camel/camel-mime-filter-basic.c18
-rw-r--r--camel/camel-mime-filter-charset.c4
-rw-r--r--camel/camel-mime-part-utils.c4
-rw-r--r--camel/camel-mime-utils.c10
-rw-r--r--camel/tests/Makefile.am3
-rwxr-xr-xcamel/tests/data/gendoc.pl65
-rwxr-xr-xcamel/tests/data/genline.pl72
-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
-rw-r--r--camel/tests/message/Makefile.am22
-rw-r--r--camel/tests/message/test1.c189
-rw-r--r--camel/tests/message/test2.c337
21 files changed, 1264 insertions, 16 deletions
diff --git a/camel/ChangeLog b/camel/ChangeLog
index e80ff42226..6e8fb27fd8 100644
--- a/camel/ChangeLog
+++ b/camel/ChangeLog
@@ -1,3 +1,38 @@
+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.
+
2000-11-22 Radek Doulik <rodo@helixcode.com>
* camel-mime-utils.c (header_msgid_generate): check for return
diff --git a/camel/Makefile.am b/camel/Makefile.am
index a57f1c5fbb..cb2924e467 100644
--- a/camel/Makefile.am
+++ b/camel/Makefile.am
@@ -1,6 +1,6 @@
## Process this file with automake to produce Makefile.in
-SUBDIRS = providers
+SUBDIRS = providers tests
libcamelincludedir = $(includedir)/camel
providerdir = $(libdir)/evolution/camel-providers/$(VERSION)
diff --git a/camel/camel-charset-map.c b/camel/camel-charset-map.c
index fd18337aae..c8974af936 100644
--- a/camel/camel-charset-map.c
+++ b/camel/camel-charset-map.c
@@ -231,7 +231,7 @@ camel_charset_step(CamelCharset *c, const char *in, int len)
}
inptr = newinptr;
if (c<=0xffff) {
- mask |= charset_mask(c);
+ mask &= charset_mask(c);
if (c>=128 && c<256)
level = MAX(level, 1);
diff --git a/camel/camel-internet-address.c b/camel/camel-internet-address.c
index e93fd581a1..1bcd532625 100644
--- a/camel/camel-internet-address.c
+++ b/camel/camel-internet-address.c
@@ -23,7 +23,7 @@
#include <stdio.h>
-#define d(x) x
+#define d(x)
static int internet_decode (CamelAddress *, const char *raw);
static char * internet_encode (CamelAddress *);
diff --git a/camel/camel-mime-filter-basic.c b/camel/camel-mime-filter-basic.c
index 8dd94e1580..17e667c5ae 100644
--- a/camel/camel-mime-filter-basic.c
+++ b/camel/camel-mime-filter-basic.c
@@ -98,23 +98,27 @@ complete(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out,
switch(f->type) {
case CAMEL_MIME_FILTER_BASIC_BASE64_ENC:
/* wont go to more than 2x size (overly conservative) */
- camel_mime_filter_set_size(mf, len*2, FALSE);
+ camel_mime_filter_set_size(mf, len*2+6, FALSE);
newlen = base64_encode_close(in, len, TRUE, mf->outbuf, &f->state, &f->save);
+ g_assert(newlen <= len*2+6);
break;
case CAMEL_MIME_FILTER_BASIC_QP_ENC:
/* *4 is definetly more than needed ... */
- camel_mime_filter_set_size(mf, len*4, FALSE);
+ camel_mime_filter_set_size(mf, len*4+4, FALSE);
newlen = quoted_encode_close(in, len, mf->outbuf, &f->state, &f->save);
+ g_assert(newlen <= len*4+4);
break;
case CAMEL_MIME_FILTER_BASIC_BASE64_DEC:
/* output can't possibly exceed the input size */
- camel_mime_filter_set_size(mf, len, FALSE);
+ camel_mime_filter_set_size(mf, len, FALSE);
newlen = base64_decode_step(in, len, mf->outbuf, &f->state, &f->save);
+ g_assert(newlen <= len);
break;
case CAMEL_MIME_FILTER_BASIC_QP_DEC:
/* output can't possibly exceed the input size */
camel_mime_filter_set_size(mf, len, FALSE);
newlen = quoted_decode_step(in, len, mf->outbuf, &f->state, &f->save);
+ g_assert(newlen <= len);
break;
default:
g_warning("unknown type %d in CamelMimeFilterBasic", f->type);
@@ -142,23 +146,27 @@ filter(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, s
switch(f->type) {
case CAMEL_MIME_FILTER_BASIC_BASE64_ENC:
/* wont go to more than 2x size (overly conservative) */
- camel_mime_filter_set_size(mf, len*2, FALSE);
+ camel_mime_filter_set_size(mf, len*2+6, FALSE);
newlen = base64_encode_step(in, len, TRUE, mf->outbuf, &f->state, &f->save);
+ g_assert(newlen <= len*2+6);
break;
case CAMEL_MIME_FILTER_BASIC_QP_ENC:
/* *4 is overly conservative, but will do */
- camel_mime_filter_set_size(mf, len*4, FALSE);
+ camel_mime_filter_set_size(mf, len*4+4, FALSE);
newlen = quoted_encode_step(in, len, mf->outbuf, &f->state, &f->save);
+ g_assert(newlen <= len*4+4);
break;
case CAMEL_MIME_FILTER_BASIC_BASE64_DEC:
/* output can't possibly exceed the input size */
camel_mime_filter_set_size(mf, len, FALSE);
newlen = base64_decode_step(in, len, mf->outbuf, &f->state, &f->save);
+ g_assert(newlen <= len);
break;
case CAMEL_MIME_FILTER_BASIC_QP_DEC:
/* output can't possibly exceed the input size */
camel_mime_filter_set_size(mf, len, FALSE);
newlen = quoted_decode_step(in, len, mf->outbuf, &f->state, &f->save);
+ g_assert(newlen <= len);
break;
default:
g_warning("unknown type %d in CamelMimeFilterBasic", f->type);
diff --git a/camel/camel-mime-filter-charset.c b/camel/camel-mime-filter-charset.c
index 1fc10a1aff..40562dc88d 100644
--- a/camel/camel-mime-filter-charset.c
+++ b/camel/camel-mime-filter-charset.c
@@ -96,7 +96,7 @@ complete(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out,
/* FIXME: there's probably a safer way to size this ...? */
/* We could always resize if we run out of room in outbuf (but it'd be nice not
to have to) */
- camel_mime_filter_set_size(mf, len*5, FALSE);
+ camel_mime_filter_set_size(mf, len*5+16, FALSE);
inbuf = in;
inlen = len;
outbuf = mf->outbuf;
@@ -160,7 +160,7 @@ filter(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, s
}
/* FIXME: there's probably a safer way to size this ...? */
- camel_mime_filter_set_size(mf, len*5, FALSE);
+ camel_mime_filter_set_size(mf, len*5+16, FALSE);
inbuf = in;
inlen = len;
outbuf = mf->outbuf;
diff --git a/camel/camel-mime-part-utils.c b/camel/camel-mime-part-utils.c
index 778255393e..84cdf25055 100644
--- a/camel/camel-mime-part-utils.c
+++ b/camel/camel-mime-part-utils.c
@@ -114,7 +114,7 @@ simple_data_wrapper_construct_from_parser(CamelDataWrapper *dw, CamelMimeParser
start = camel_mime_parser_tell(mp) + seekable_source->bound_start;
}
while ( camel_mime_parser_step(mp, &buf, &len) != HSCAN_BODY_END ) {
- d(printf("appending o/p data: %.*s\n", len, buf));
+ d(printf("appending o/p data: %d: %.*s\n", len, len, buf));
if (buffer) {
if (buffer->len > 20480 && seekable_source) {
/* is this a 'big' message? Yes? We dont want to convert it all then.*/
@@ -132,7 +132,9 @@ simple_data_wrapper_construct_from_parser(CamelDataWrapper *dw, CamelMimeParser
if (buffer) {
CamelStream *mem;
+
d(printf("Small message part, kept in memory!\n"));
+
mem = camel_stream_mem_new_with_byte_array(buffer);
camel_data_wrapper_construct_from_stream (dw, mem);
camel_object_unref ((CamelObject *)mem);
diff --git a/camel/camel-mime-utils.c b/camel/camel-mime-utils.c
index 4fa150ae2a..238b34156e 100644
--- a/camel/camel-mime-utils.c
+++ b/camel/camel-mime-utils.c
@@ -285,6 +285,11 @@ base64_encode_close(unsigned char *in, int inlen, gboolean break_lines, unsigned
c1 = ((unsigned char *)save)[1];
c2 = ((unsigned char *)save)[2];
+
+ d(printf("mode = %d\nc1 = %c\nc2 = %c\n",
+ (int)((char *)save)[0],
+ (int)((char *)save)[1],
+ (int)((char *)save)[2]));
switch (((char *)save)[0]) {
case 2:
@@ -560,10 +565,6 @@ quoted_encode_close(unsigned char *in, int len, unsigned char *out, int *state,
}
}
- /* hmm, not sure if this should really be added here, we dont want
- to add it to the content, afterall ...? */
- *outptr++ = '\n';
-
*save = 0;
*state = -1;
@@ -677,7 +678,6 @@ quoted_decode_step(unsigned char *in, int len, unsigned char *out, int *savestat
case 0:
while (inptr<inend) {
c = *inptr++;
- /* FIXME: use a specials table to avoid 3 comparisons for the common case */
if (c=='=') {
state = 1;
break;
diff --git a/camel/tests/Makefile.am b/camel/tests/Makefile.am
new file mode 100644
index 0000000000..c1799b985f
--- /dev/null
+++ b/camel/tests/Makefile.am
@@ -0,0 +1,3 @@
+
+SUBDIRS = lib \
+ message
diff --git a/camel/tests/data/gendoc.pl b/camel/tests/data/gendoc.pl
new file mode 100755
index 0000000000..732f05a4ed
--- /dev/null
+++ b/camel/tests/data/gendoc.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl
+
+# Generate 'documents' in different encodings, from po files
+
+if ($#ARGV < 0) {
+ print "Usage: gendoc.pl pofile pofile ...\n";
+ exit 1;
+}
+
+$fmt = "| fmt -u ";
+
+sub read_msgstr()
+{
+ my $str = "";
+ while (<IN>) {
+ if (m/^msgstr \"(.*)\"/) {
+ $str = $1;
+ if ($str eq "") {
+ while (<IN>) {
+ if (m/\"(.*)\"/) {
+ $str .= $1;
+ } else {
+ last;
+ }
+ }
+ }
+ return $str;
+ }
+ }
+ return "";
+}
+
+$unknown = "x-unknown-1";
+
+foreach $name (@ARGV) {
+ if ($name =~ m@([^/]*).po$@) {
+ $poname = $1;
+
+ open IN,"<$name";
+
+ $header = read_msgstr;
+ if ($header =~ /Content-Type:.*charset=([-a-zA-Z0-9]*)/i) {
+ $charset = $1;
+ } else {
+ $charset = $unknown++;
+ }
+
+ print "Building $poname.$charset.txt from $name\n";
+
+ open OUT,"$fmt > $poname.$charset.txt";
+ while (!eof(IN)) {
+ $msg = read_msgstr;
+ # de-escape
+ $msg =~ s/\\n/\n/gso;
+ $msg =~ s/\\t/\t/gso;
+ $msg =~ s/\\(.)/$1/gso;
+ print OUT $msg." ";
+ }
+ close OUT;
+ close IN;
+ } else {
+ printf("ignoring $name, probably not intended\n");
+ }
+}
+
diff --git a/camel/tests/data/genline.pl b/camel/tests/data/genline.pl
new file mode 100755
index 0000000000..99ff43c882
--- /dev/null
+++ b/camel/tests/data/genline.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+# Generate 'documents' in different encodings, from po files
+
+if ($#ARGV < 0) {
+ print "Usage: genline.pl pofile pofile ...\n";
+ exit 1;
+}
+
+sub read_msgstr()
+{
+ my $str = "";
+ while (<IN>) {
+ if (m/^msgstr \"(.*)\"/) {
+ $str = $1;
+ if ($str eq "") {
+ while (<IN>) {
+ if (m/\"(.*)\"/) {
+ $str .= $1;
+ } else {
+ last;
+ }
+ }
+ }
+ return $str;
+ }
+ }
+ return "";
+}
+
+$unknown = "x-unknown-1";
+open OUT, ">test-lines.h";
+
+print OUT <<END;
+struct _l {
+ char *type;
+ char *line;
+} test_lines[] = {
+END
+
+foreach $name (@ARGV) {
+ if ($name =~ m@([^/]*).po$@) {
+ $poname = $1;
+
+ open IN,"<$name";
+
+ $header = read_msgstr;
+ if ($header =~ /Content-Type:.*charset=([-a-zA-Z0-9]*)/i) {
+ $charset = $1;
+ } else {
+ $charset = $unknown++;
+ }
+
+ while (!eof(IN)) {
+ $msg = read_msgstr;
+ if (length($msg) > 60 && length($msg) < 160) {
+ print OUT "\t{ \"$charset\", \"$msg\" },\n";
+ last;
+ }
+# $msg =~ s/\\n/\n/gso;
+# $msg =~ s/\\t/\t/gso;
+# $msg =~ s/\\(.)/$1/gso;
+# print OUT $msg." ";
+ }
+ close IN;
+ } else {
+ printf("ignoring $name, probably not intended\n");
+ }
+}
+
+print OUT "};\n";
+close OUT;
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);
diff --git a/camel/tests/message/Makefile.am b/camel/tests/message/Makefile.am
new file mode 100644
index 0000000000..2656a007d9
--- /dev/null
+++ b/camel/tests/message/Makefile.am
@@ -0,0 +1,22 @@
+
+INCLUDES = -I$(top_srcdir)/intl -I$(top_srcdir) -I$(top_srcdir)/camel \
+ -I$(includedir) \
+ -I$(top_srcdir)/camel/tests/lib \
+ -DG_LOG_DOMAIN=\"evolution-tests\"
+
+LDADD = \
+ $(top_builddir)/camel/libcamel.la \
+ $(top_builddir)/e-util/libeutil.la \
+ $(top_builddir)/libibex/libibex.la \
+ $(GNOME_LIBDIR) \
+ $(top_builddir)/camel/tests/lib/libcameltest.a \
+ $(GNOMEUI_LIBS) $(INTLLIBS) $(EXTRA_GNOME_LIBS)
+
+check_PROGRAMS = \
+ test1 \
+ test2
+
+TESTS = test1 test2
+
+
+
diff --git a/camel/tests/message/test1.c b/camel/tests/message/test1.c
new file mode 100644
index 0000000000..681537d269
--- /dev/null
+++ b/camel/tests/message/test1.c
@@ -0,0 +1,189 @@
+/*
+ test1.c
+
+ Create a message, save it.
+
+ Retrieve message, compare content.
+
+
+ Operations:
+ writing/loading from different types of streams
+ reading/writing different content
+ reading/writing different encodings
+ reading/writing different charsets
+
+ Just testing streams:
+ different stream types
+ different file ops
+ seek, eof, etc.
+*/
+
+#include "camel-test.h"
+#include "messages.h"
+
+/* for stat */
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <camel/camel-mime-message.h>
+#include <camel/camel-stream-fs.h>
+#include <camel/camel-stream-mem.h>
+
+struct _text {
+ char *text;
+ int len;
+};
+
+#define MAX_TEXTS (14)
+struct _text texts[MAX_TEXTS];
+
+static void
+setup(void)
+{
+ int i, j;
+ char *p;
+
+ /* setup various edge and other general cases */
+ texts[0].text = "";
+ texts[0].len = 0;
+ texts[1].text = "";
+ texts[1].len = 1;
+ texts[2].text = "\n";
+ texts[2].len = 1;
+ texts[3].text = "A";
+ texts[3].len = 1;
+ texts[4].text = "This is a test.\n.";
+ texts[4].len = strlen(texts[4].text);
+ texts[5].text = "This is a test.\n\n.\n";
+ texts[5].len = strlen(texts[5].text);
+ texts[6].text = g_malloc0(1024);
+ texts[6].len = 1024;
+ texts[7].text = g_malloc0(102400);
+ texts[7].len = 102400;
+ texts[8].text = g_malloc(1024);
+ memset(texts[8].text, '\n', 1024);
+ texts[8].len = 1024;
+ texts[9].text = g_malloc(102400);
+ memset(texts[9].text, '\n', 102400);
+ texts[9].len = 102400;
+ texts[10].text = g_malloc(1024);
+ memset(texts[10].text, ' ', 1024);
+ texts[10].len = 1024;
+ texts[11].text = g_malloc(102400);
+ memset(texts[11].text, ' ', 102400);
+ texts[11].len = 102400;
+
+ srand(42);
+ p = texts[12].text = g_malloc(1024);
+ for (i=0;i<1024;i++) {
+ j = rand();
+ if (j<RAND_MAX/120)
+ *p++ = '\n';
+ else
+ *p++ = (j % 95) + 32;
+ }
+ texts[12].len = 1024;
+ p = texts[13].text = g_malloc(102400);
+ for (i=0;i<102400;i++) {
+ j = rand();
+ if (j<RAND_MAX/120)
+ *p++ = '\n';
+ else
+ *p++ = (j % 95) + 32;
+ }
+ texts[13].len = 102400;
+}
+
+int main(int argc, char **argv)
+{
+ CamelMimeMessage *msg, *msg2;
+ int i, j;
+ char *text;
+ int len;
+
+ camel_test_init(argc, argv);
+
+ setup();
+
+ camel_test_start("Simple memory-based content creation");
+
+ /* test all ways of setting simple content for a message (i.e. memory based) */
+ for (j=0;j<MAX_TEXTS;j++) {
+ push("testing text number %d", j);
+ text = texts[j].text;
+ len = texts[j].len;
+ for (i=0;i<SET_CONTENT_WAYS;i++) {
+ push("create simple message %d", i);
+ msg = test_message_create_simple();
+
+ push("set simple content");
+ test_message_set_content_simple((CamelMimePart *)msg, i, "text/plain", text, len);
+ pull();
+
+ push("compare original content");
+ test_message_compare_content(camel_medium_get_content_object((CamelMedium *)msg), text, len);
+ pull();
+
+ push("save message to test1.msg");
+ unlink("test1.msg");
+ test_message_write_file(msg, "test1.msg");
+ check_unref(msg, 1);
+ pull();
+
+ push("read from test1.msg");
+ msg2 = test_message_read_file("test1.msg");
+ pull();
+
+ push("compare read with original content");
+ test_message_compare_content(camel_medium_get_content_object((CamelMedium *)msg2), text, len);
+ check_unref(msg2, 1);
+ pull();
+
+ unlink("test1.msg");
+ pull();
+ }
+ pull();
+ }
+
+ camel_test_end();
+
+ camel_test_start("Different encodings");
+ for (j=0;j<MAX_TEXTS;j++) {
+ push("testing text number %d", j);
+ text = texts[j].text;
+ len = texts[j].len;
+ for (i=0;i<CAMEL_MIME_PART_NUM_ENCODINGS;i++) {
+
+ push("test simple message, encoding %s", camel_mime_part_encoding_to_string(i));
+ msg = test_message_create_simple();
+
+ push("set simple content");
+ test_message_set_content_simple((CamelMimePart *)msg, 0, "text/plain", text, len);
+ pull();
+
+ camel_mime_part_set_encoding((CamelMimePart *)msg, i);
+
+ push("save message to test1.msg");
+ unlink("test1.msg");
+ test_message_write_file(msg, "test1.msg");
+ check_unref(msg, 1);
+ pull();
+
+ push("read from test1.msg");
+ msg2 = test_message_read_file("test1.msg");
+ pull();
+
+ push("compare read with original content");
+ test_message_compare_content(camel_medium_get_content_object((CamelMedium *)msg2), text, len);
+ check_unref(msg2, 1);
+ pull();
+
+ unlink("test1.msg");
+ pull();
+ }
+ pull();
+ }
+ camel_test_end();
+
+ return 0;
+}
diff --git a/camel/tests/message/test2.c b/camel/tests/message/test2.c
new file mode 100644
index 0000000000..8a3fe3c250
--- /dev/null
+++ b/camel/tests/message/test2.c
@@ -0,0 +1,337 @@
+#include "camel-test.h"
+#include "messages.h"
+#include "addresses.h"
+
+/* for stat */
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <unicode.h>
+
+#include <camel/camel-internet-address.h>
+#include <camel/camel-address.h>
+
+/* a bunch of test strings in different encodings, just taken from gnome-core po files */
+/* see data/genline.pl */
+struct _l {
+ char *type;
+ char *line;
+} test_lines[] = {
+ /* FIXME: for now, remove the types libunicode doesn't know about, this is tricky to fix */
+ /* And at best, all we could do is translate it to X-Unknown, or something */
+ /*{ "windows-1251", "Åäèí àïëåò íå îòãîâàðÿ íà çàÿâêà çà çàïèñ.\nÄà ãî ïðåìàõíà ëè èëè äà ïî÷àêàì?" },*/
+ { "iso-8859-1", "Omple les miniatures de la finestra amb contingut de la pantalla" },
+ { "ISO-8859-2", "Správce oken hýbe s okrajem okna (AfterStep, Enlightenment, FVWM, IceWM, Sawmill)" },
+ { "ISO-8859-1", "Vindueshåndtering flytter dekorationsvindue istedet (AfterStep, Enlightenment, FVWM, IceWM, Sawfish)" },
+ { "ISO-8859-1", "Vorschaubilder der Fenster mit dem Bildschirminhalt ausfüllen" },
+ { "iso-8859-7", "ÅìöÜíéóç åñãáóéþí ðïõ äå öáßíïíôáé óôç ëßóôá ðáñáèýñùí (ÐÁÑÁÊÁÌØÇ-ËÉÓÔÁÐÁÑÁÈÕÑÙÍ)" },
+ { "iso-8859-1", "You've chosen to disable the startup hint. To re-enable it, choose \"Startup Hint\" in the GNOME Control Centre" },
+ { "iso-8859-1", "El aplique de reloj muestra en su panel la fecha y la hora de forma simple y ligero " },
+ { "iso-8859-1", "Applet ei vasta salvestuskäsule. Kas peaks ta niisama sulgema, või veel ootama?" },
+ { "iso-8859-1", "Lehio kudeatzaileak lehioaren dekorazaioa mugiarazten (AfterStep, Enlightenment, FVWM, IceWM, Sawmill)" },
+ { "iso-8859-15", "Näytä sovellukset, joiden ikkunoista on näkyvillä vain otsikkopalkki" },
+ { "ISO-8859-1", "Afficher les tâches qui ne sont pas dans la liste des fenêtres" },
+ { "iso-8859-1", "Níl applet ag tabhair freagra ar iarratas sábháil. Bain amach an applet nó lean ar fánacht?" },
+ { "iso-8859-1", "Amosa-las tarefas agochadas da lista de fiestras (SKIP-WINLIST)" },
+ { "iso-8859-2", "Az ablakkezelõ a dekorációt mozgassa az ablak helyett (AfterStep, Enlightenment, FVWM, IceWM, SawMill)" },
+ { "iso-8859-1", "Riempi la finestra delle anteprime con il contenuto dello schermo" },
+ { "euc-jp", "¥¦¥¤¥ó¥É¥¦¥Þ¥Í¡¼¥¸¥ã¤Ï¾þ¤ê¥¦¥¤¥ó¥É¥¦¤òÆ°¤«¤¹ (AfterStep, Enlightenment, FVWM, IceWM, Sawfish)" },
+ { "euc-kr", "â °ü¸®ÀÚ°¡ ²Ù¹Î â ´ë½Å À̵¿ (AfterStep, Enlightenment, FVWM, IceWM, Sawmill)" },
+ { "iso-8859-13", "Priedas neatsakinëja á praðymà iðsisaugoti. Paðalinti priedà ar laukti toliau?" },
+ { "iso-8859-1", "Window manager verplaatst dekoratie (AfterStep, Enlightenment, FVWM, IceWM, Sawmill)" },
+ { "iso-8859-1", "Vindushåndtereren flytter dekorasjonsvinduet i stedet (AfterStep, Enlightenment, FVWM, IceWM, Sawfish)" },
+ { "iso-8859-2", "Przemieszczanie dekoracji zamiast okna (AfterStep, Enlightenment, FVWM, IceWM, Sawmill)" },
+ { "iso-8859-1", "Este programa é responsável por executar outras aplicações, embeber pequenos applets, a paz no mundo e crashes aleatórios do X." },
+ { "iso-8859-1", "Mostrar tarefas que se escondem da lista de janelas (SKIP-WINLIST)" },
+ { "koi8-r", "÷ÙÓÏÔÁ ÒÁÂÏÞÅÇÏ ÓÔÏÌÁ × ÐÅÒÅËÌÀÞÁÔÅÌÅ ÓÏ×ÐÁÄÁÅÔ Ó ×ÙÓÏÔÏÊ ÐÁÎÅÌÉ" },
+ { "iso-8859-2", "Správca okien presúva okraje okien (AfterStep, Enlightenment, FVWM, IceWM, Sawfish)" },
+ { "iso-8859-2", "Ka¾i posle, ki se skrivajo pred upravljalnik oken (SKIP-WINLIST)" },
+ { "iso-8859-5", "Window ÜÕÝÐÔ×ÕàØ ßÞÜÕàÐ ÔÕÚÞàÐæØÞÝØ ßàÞ×Þà ãÜÕáâÞ âÞÓa (AfterStep, Enlightenment, FVWM, IceWM, Sawmill)" },
+ { "iso-8859-2", "Window menadzeri pomera dekoracioni prozor umesto toga (AfterStep, Enlightenment, FVWM, IceWM, Sawmill)" },
+ { "iso-8859-1", "Fönsterhanteraren flyttar dekorationsfönstret istället (AfterStep, Enlightenment, FVWM, IceWM, Sawfish)" },
+ /*{ "TSCII", "À½¢ì¸¼î-ºð¼¸ò¾¢ø À¡÷ì¸ ÓÊ¡¾ À½¢ì¸¼í¸¨Ç ¸¡Á¢ (À½¢ì¸¼î-ºð¼¸õ-¾Å¢÷)" },*/
+ { "iso-8859-9", "Kaydetme isteðine bir uygulak cevap vermiyor . Uygulaðý sileyim mi , yoksa bekleyeyim mi ?" },
+ { "koi8-u", "ðÅÒÅͦÝÅÎÎÑ ÄÅËÏÒÁæ§ ÚÁͦÓÔØ ×¦ËÎÁ (AfterStep, Enlightenment, FVWM, IceWM, Sawfish)" },
+ { "iso-8859-1", "Cwand on scriftôr est bodjî foû, li scriftôr èt totes les apliketes å dvins sont pierdowes. Bodjî ci scriftôr chal?" },
+ { "gb2312", "ǨÒƵ½×°Êδ°¿Ú¹ÜÀí³ÌÐò(AfterStep, Enlightenment, FVWM, IceWM, SawMill)" },
+ { "big5", "µøµ¡ºÞ²zªÌ¥u²¾°Ê¸Ë¹¢µøµ¡ (AfterStep, Enlightenment, FVWM, IceWM, Sawmill)" },
+};
+
+static char *convert(const char *in, const char *from, const char *to)
+{
+ unicode_iconv_t ic = unicode_iconv_open(to, from);
+ char *out, *outp;
+ const char *inp;
+ int inlen, outlen;
+
+ if (ic == (unicode_iconv_t)-1)
+ return g_strdup(in);
+
+ inlen = strlen(in);
+ outlen = inlen*5 + 16;
+
+ outp = out = g_malloc(outlen);
+ inp = in;
+
+ if (unicode_iconv(ic, &inp, &inlen, &outp, &outlen) == -1) {
+ test_free(out);
+ unicode_iconv_close(ic);
+ return g_strdup(in);
+ }
+
+ if (unicode_iconv(ic, NULL, 0, &outp, &outlen) == -1) {
+ test_free(out);
+ unicode_iconv_close(ic);
+ return g_strdup(in);
+ }
+
+ unicode_iconv_close(ic);
+
+ *outp = 0;
+
+#if 0
+ /* lets see if we can convert back again? */
+ {
+ char *nout, *noutp;
+ unicode_iconv_t ic = unicode_iconv_open(from, to);
+
+ inp = out;
+ inlen = strlen(out);
+ outlen = inlen*5 + 16;
+ noutp = nout = g_malloc(outlen);
+ if (unicode_iconv(ic, &inp, &inlen, &noutp, &outlen) == -1
+ || unicode_iconv(ic, NULL, 0, &noutp, &outlen) == -1) {
+ g_warning("Cannot convert '%s' \n from %s to %s: %s\n", in, to, from, strerror(errno));
+ }
+ unicode_iconv_close(ic);
+ }
+
+ /* and lets see what camel thinks out optimal charset is */
+ {
+ printf("Camel thinks the best encoding of '%s' is %s, although we converted from %s\n",
+ in, camel_charset_best(out, strlen(out)), from);
+ }
+#endif
+
+ return out;
+}
+
+#define to_utf8(in, type) convert(in, type, "utf-8")
+#define from_utf8(in, type) convert(in, "utf-8", type)
+
+#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0]))
+
+int main(int argc, char **argv)
+{
+ int i;
+ CamelInternetAddress *addr, *addr2;
+ char *name;
+ char *charset;
+ const char *real, *where;
+ char *enc, *enc2, *format, *format2;
+
+ camel_test_init(argc, argv);
+
+ camel_test_start("CamelInternetAddress, basics");
+
+ addr = camel_internet_address_new();
+
+ push("Test blank address");
+ check(camel_address_length(CAMEL_ADDRESS(addr)) == 0);
+ check(camel_internet_address_get(addr, 0, &real, &where) == FALSE);
+ pull();
+
+ push("Test blank clone");
+ addr2 = CAMEL_INTERNET_ADDRESS(camel_address_new_clone(CAMEL_ADDRESS(addr)));
+ test_address_compare(addr, addr2);
+ check_unref(addr2, 1);
+ pull();
+
+ push("Test add 1");
+ camel_internet_address_add(addr, "Zed", "nowhere@here.com.au");
+ check(camel_address_length(CAMEL_ADDRESS(addr)) == 1);
+ check(camel_internet_address_get(addr, 0, &real, &where) == TRUE);
+ check_msg(string_equal("Zed", real), "real = '%s'", real);
+ check(strcmp(where, "nowhere@here.com.au") == 0);
+ pull();
+
+ push("Test clone 1");
+ addr2 = CAMEL_INTERNET_ADDRESS(camel_address_new_clone(CAMEL_ADDRESS(addr)));
+ test_address_compare(addr, addr2);
+ check_unref(addr2, 1);
+ pull();
+
+ push("Test add many");
+ for (i=1;i<10;i++) {
+ char name[16], a[32];
+ sprintf(name, "Zed %d", i);
+ sprintf(a, "nowhere@here-%d.com.au", i);
+ camel_internet_address_add(addr, name, a);
+ check(camel_address_length(CAMEL_ADDRESS(addr)) == i+1);
+ check(camel_internet_address_get(addr, i, &real, &where) == TRUE);
+ check_msg(string_equal(name, real), "name = '%s' real = '%s'", name, real);
+ check(strcmp(where, a) == 0);
+ }
+ pull();
+
+ /* put a few of these in to make it look like its doing something impressive ... :) */
+ camel_test_end();
+ camel_test_start("CamelInternetAddress, search");
+
+ push("Test search");
+ camel_test_nonfatal("Address comparisons should ignore whitespace??");
+ check(camel_internet_address_find_name(addr, "Zed 1", &where) == 1);
+ check(camel_internet_address_find_name(addr, "Zed 9", &where) == 9);
+ check(camel_internet_address_find_name(addr, "Zed", &where) == 0);
+ check(camel_internet_address_find_name(addr, " Zed", &where) == 0);
+ check(camel_internet_address_find_name(addr, "Zed ", &where) == 0);
+ check(camel_internet_address_find_name(addr, " Zed ", &where) == 0);
+ check(camel_internet_address_find_name(addr, "Zed 20", &where) == -1);
+ check(camel_internet_address_find_name(addr, "", &where) == -1);
+ /* interface dont handle nulls :) */
+ /*check(camel_internet_address_find_name(addr, NULL, &where) == -1);*/
+
+ check(camel_internet_address_find_address(addr, "nowhere@here-1.com.au", &where) == 1);
+ check(camel_internet_address_find_address(addr, "nowhere@here-1 . com.au", &where) == 1);
+ check(camel_internet_address_find_address(addr, "nowhere@here-2 .com.au ", &where) == 2);
+ check(camel_internet_address_find_address(addr, " nowhere @here-3.com.au", &where) == 3);
+ check(camel_internet_address_find_address(addr, "nowhere@here-20.com.au ", &where) == -1);
+ check(camel_internet_address_find_address(addr, "", &where) == -1);
+ /*check(camel_internet_address_find_address(addr, NULL, &where) == -1);*/
+ camel_test_fatal();
+ pull();
+
+ camel_test_end();
+ camel_test_start("CamelInternetAddress, copy/cat/clone");
+
+ push("Test clone many");
+ addr2 = CAMEL_INTERNET_ADDRESS(camel_address_new_clone(CAMEL_ADDRESS(addr)));
+ test_address_compare(addr, addr2);
+ pull();
+
+ push("Test remove items");
+ camel_address_remove(CAMEL_ADDRESS(addr2), 0);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 9);
+ camel_address_remove(CAMEL_ADDRESS(addr2), 0);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 8);
+ camel_address_remove(CAMEL_ADDRESS(addr2), 5);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 7);
+ camel_address_remove(CAMEL_ADDRESS(addr2), 10);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 7);
+ camel_address_remove(CAMEL_ADDRESS(addr2), -1);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 0);
+ check_unref(addr2, 1);
+ pull();
+
+ push("Testing copy/cat");
+ push("clone + cat");
+ addr2 = CAMEL_INTERNET_ADDRESS(camel_address_new_clone(CAMEL_ADDRESS(addr)));
+ camel_address_cat(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
+ check(camel_address_length(CAMEL_ADDRESS(addr)) == 10);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 20);
+ check_unref(addr2, 1);
+ pull();
+
+ push("cat + cat + copy");
+ addr2 = camel_internet_address_new();
+ camel_address_cat(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
+ test_address_compare(addr, addr2);
+ camel_address_cat(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
+ check(camel_address_length(CAMEL_ADDRESS(addr)) == 10);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 20);
+ camel_address_copy(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
+ test_address_compare(addr, addr2);
+ check_unref(addr2, 1);
+ pull();
+
+ push("copy");
+ addr2 = camel_internet_address_new();
+ camel_address_copy(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
+ test_address_compare(addr, addr2);
+ check_unref(addr2, 1);
+ pull();
+
+ pull();
+
+ check_unref(addr, 1);
+
+ camel_test_end();
+
+ camel_test_start("CamelInternetAddress, I18N");
+
+ for (i=0;i<ARRAY_LEN(test_lines);i++) {
+ push("Testing text line %d (%s) '%s'", i, test_lines[i].type, test_lines[i].line);
+
+ addr = camel_internet_address_new();
+
+ /* first, convert to api format (utf-8) */
+ charset = test_lines[i].type;
+ name = to_utf8(test_lines[i].line, charset);
+
+ push("Address setup");
+ camel_internet_address_add(addr, name, "nobody@nowhere.com");
+ check(camel_internet_address_get(addr, 0, &real, &where) == TRUE);
+ check_msg(string_equal(name, real), "name = '%s' real = '%s'", name, real);
+ check(strcmp(where, "nobody@nowhere.com") == 0);
+
+ check(camel_internet_address_get(addr, 1, &real, &where) == FALSE);
+ check(camel_address_length(CAMEL_ADDRESS(addr)) == 1);
+ pull();
+
+ push("Address encode/decode");
+ enc = camel_address_encode(CAMEL_ADDRESS(addr));
+
+ addr2 = camel_internet_address_new();
+ check(camel_address_decode(CAMEL_ADDRESS(addr2), enc) == 1);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 1);
+
+ enc2 = camel_address_encode(CAMEL_ADDRESS(addr2));
+ check_msg(string_equal(enc, enc2), "enc = '%s' enc2 = '%s'", enc, enc2);
+ test_free(enc2);
+
+ push("Compare addresses");
+ test_address_compare(addr, addr2);
+ pull();
+ test_free(enc);
+ pull();
+
+ /* FIXME: format/unformat arne't guaranteed to be reversible, at least at the moment */
+ camel_test_nonfatal("format/unformat not (yet) reversible for all cases");
+
+ push("Address format/unformat");
+ format = camel_address_format(CAMEL_ADDRESS(addr));
+
+ addr2 = camel_internet_address_new();
+ check(camel_address_unformat(CAMEL_ADDRESS(addr2), format) == 1);
+ check(camel_address_length(CAMEL_ADDRESS(addr2)) == 1);
+
+ format2 = camel_address_format(CAMEL_ADDRESS(addr2));
+ check_msg(string_equal(format, format2), "format = '%s\n\tformat2 = '%s'", format, format2);
+ test_free(format2);
+
+ /* currently format/unformat doesn't handle ,'s and other special chars at all */
+ if (camel_address_length(CAMEL_ADDRESS(addr2)) == 1) {
+ push("Compare addresses");
+ test_address_compare(addr, addr2);
+ pull();
+ }
+
+ test_free(format);
+ pull();
+
+ camel_test_fatal();
+
+ check_unref(addr2, 1);
+
+ check_unref(addr, 1);
+ pull();
+
+ }
+
+ camel_test_end();
+
+ /* FIXME: Add test of decoding of externally defined addresses */
+ /* FIXME: Add test of decoding of broken addresses */
+
+ return 0;
+}