chiark / gitweb /
Fiddle with playing.tmpl a bit. not fully translated
[disorder] / lib / t-mime.c
index 882d1860e24f6db94aeb2c6cb81ab58d26cfde82..7f56c5d3ff0cea7c6095ac745ef745bedf14c0a3 100644 (file)
@@ -26,12 +26,20 @@ static int test_multipart_callback(const char *s, void *u) {
   return 0;
 }
 
-void test_mime(void) {
+static int header_callback(const char *name, const char *value,
+                          void *u) {
+  hash *const h = u;
+
+  hash_add(h, name, &value, HASH_INSERT);
+  return 0;
+}
+
+static void test_mime(void) {
   char *t, *n, *v;
   struct vector parts[1];
   struct kvp *k;
-
-  fprintf(stderr, "test_mime\n");
+  const char *s, *cs, *enc;
+  hash *h;
 
   t = 0;
   k = 0;
@@ -181,7 +189,37 @@ void test_mime(void) {
                "Content-Description: jpeg-1\r\n"
                "\r\n"
                "<jpeg data>");
+
+  /* Bogus inputs to mime_multipart() */
+  fprintf(stderr, "expect two mime_multipart errors:\n");
+  insist(mime_multipart("--inner\r\n"
+                        "Content-Type: text/plain\r\n"
+                        "Content-Disposition: inline\r\n"
+                        "Content-Description: text-part-2\r\n"
+                        "\r\n"
+                        "Some more text here.\r\n"
+                        "\r\n"
+                        "--inner\r\n"
+                        "Content-Type: image/jpeg\r\n"
+                        "Content-Disposition: attachment\r\n"
+                        "Content-Description: jpeg-1\r\n"
+                        "\r\n"
+                        "<jpeg data>\r\n",
+                        test_multipart_callback,
+                        "inner",
+                        parts) == -1);
+  insist(mime_multipart("--wrong\r\n"
+                        "Content-Type: text/plain\r\n"
+                        "Content-Disposition: inline\r\n"
+                        "Content-Description: text-part-2\r\n"
+                        "\r\n"
+                        "Some more text here.\r\n"
+                        "\r\n"
+                        "--inner--\r\n",
+                        test_multipart_callback,
+                        "inner",
+                        parts) == -1);
+    
   /* XXX mime_parse */
 
   check_string(mime_qp(""), "");
@@ -210,7 +248,9 @@ void test_mime(void) {
               "Now's the time for all folk to come to the aid of their country.");
 
 #define check_base64(encoded, decoded) do {                     \
-    check_string(mime_base64(encoded, 0), decoded);             \
+    size_t ns;                                                  \
+    check_string(mime_base64(encoded, &ns), decoded);           \
+    insist(ns == (sizeof decoded) - 1);                         \
     check_string(mime_to_base64((const uint8_t *)decoded,       \
                                          (sizeof decoded) - 1), \
                  encoded);                                      \
@@ -250,8 +290,83 @@ void test_mime(void) {
   /* Not actually valid base64 */
   check_string(mime_base64("BBBBx=", 0),
               "\x04\x10\x41");
+
+  h = hash_new(sizeof (char *));
+  s = mime_parse("From: sender@example.com\r\n"
+                "To: rcpt@example.com\r\n"
+                "Subject: test #1\r\n"
+                "\r\n"
+                "body\r\n",
+                header_callback, h);
+  insist(s != 0);
+  check_string(*(char **)hash_find(h, "from"), "sender@example.com");
+  check_string(*(char **)hash_find(h, "to"), "rcpt@example.com");
+  check_string(*(char **)hash_find(h, "subject"), "test #1");
+  check_string(s, "body\r\n");
+
+  h = hash_new(sizeof (char *));
+  s = mime_parse("FROM: sender@example.com\r\n"
+                "TO: rcpt@example.com\r\n"
+                "SUBJECT: test #1\r\n"
+                "CONTENT-TRANSFER-ENCODING: 7bit\r\n"
+                "\r\n"
+                "body\r\n",
+                header_callback, h);
+  insist(s != 0);
+  check_string(*(char **)hash_find(h, "from"), "sender@example.com");
+  check_string(*(char **)hash_find(h, "to"), "rcpt@example.com");
+  check_string(*(char **)hash_find(h, "subject"), "test #1");
+  check_string(*(char **)hash_find(h, "content-transfer-encoding"), "7bit");
+  check_string(s, "body\r\n");
+
+  h = hash_new(sizeof (char *));
+  s = mime_parse("From: sender@example.com\r\n"
+                "To:    \r\n"
+                "     rcpt@example.com\r\n"
+                "Subject: test #1\r\n"
+                "MIME-Version: 1.0\r\n"
+                "Content-Type: text/plain\r\n"
+                "Content-Transfer-Encoding: BASE64\r\n"
+                "\r\n"
+                "d2liYmxlDQo=\r\n",
+                header_callback, h);
+  insist(s != 0); 
+  check_string(*(char **)hash_find(h, "from"), "sender@example.com");
+  check_string(*(char **)hash_find(h, "to"), "rcpt@example.com");
+  check_string(*(char **)hash_find(h, "subject"), "test #1");
+  check_string(*(char **)hash_find(h, "mime-version"), "1.0");
+  check_string(*(char **)hash_find(h, "content-type"), "text/plain");
+  check_string(*(char **)hash_find(h, "content-transfer-encoding"), "BASE64");
+  check_string(s, "wibble\r\n");
+
+#define CHECK_QUOTE(INPUT, EXPECT) do {                 \
+  s = quote822(INPUT, 0);                               \
+  insist(s != 0);                                       \
+  check_string(s, EXPECT);                              \
+  s = mime_parse_word(s, &t, mime_http_separator);      \
+  check_string(t, INPUT);                               \
+} while(0)
+  CHECK_QUOTE("wibble", "wibble");
+  CHECK_QUOTE("wibble spong", "\"wibble spong\"");
+  CHECK_QUOTE("wibble\\spong", "\"wibble\\\\spong\"");
+  CHECK_QUOTE("wibble\"spong", "\"wibble\\\"spong\"");
+  CHECK_QUOTE("(wibble)", "\"(wibble)\"");
+
+  s = mime_encode_text("wibble\n", &cs, &enc);
+  insist(s != 0);
+  check_string(s, "wibble\n");
+  check_string(cs, "us-ascii");
+  check_string(enc, "7bit");
+
+  s = mime_encode_text("wibble\xC3\xB7\n", &cs, &enc);
+  insist(s != 0);
+  check_string(s, "wibble=C3=B7\n");
+  check_string(cs, "utf-8");
+  check_string(enc, "quoted-printable");
 }
 
+TEST(mime);
+
 /*
 Local Variables:
 c-basic-offset:2