Added SILC Thread Queue API
[silc.git] / lib / silcutil / tests / test_silcmime.c
index 01e7fdd1b6827aa779ddf365e6b353802ac16d05..d3b7d35d9fe956a74dc03d51087c703eb11b8760 100644 (file)
@@ -1,31 +1,11 @@
 /* SilcMime tests */
 
-#include "silcincludes.h"
+#include "silc.h"
 #include "silcmime.h"
 
-struct foo {
-  int i;
-  struct foo *next;
-};
-
-static void ass_complete(SilcMime mime, void *context)
-{
-  unsigned char *enc;
-  SilcUInt32 enc_len;
-
-  SILC_LOG_DEBUG(("Defragmentation completed"));
-  SILC_LOG_DEBUG(("Encoding MIME context"));
-  enc = silc_mime_encode(mime, &enc_len);
-  if (!enc)
-    SILC_LOG_DEBUG(("Error encoding"));
-  SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
-  silc_free(enc);
-  silc_mime_free(mime);
-}
-
 int main(int argc, char **argv)
 {
-  bool success = FALSE;
+  SilcBool success = FALSE;
   SilcMime mime, part, part2;
   SilcMimeAssembler ass;
   int i;
@@ -34,14 +14,17 @@ int main(int argc, char **argv)
   SilcUInt32 enc_len;
   SilcDList frag;
   SilcBuffer buf;
+  const char *mtype;
 
   if (argc > 1 && !strcmp(argv[1], "-d")) {
     silc_log_debug(TRUE);
     silc_log_debug_hexdump(TRUE);
-    silc_log_set_debug_string("*mime*");
+    silc_log_set_debug_string("*mime*,*errno*");
   }
 
-  /* Simple MIME test */
+  /* 
+   * Simple MIME test
+   */
   SILC_LOG_DEBUG(("Allocating MIME message context"));
   mime = silc_mime_alloc();
   if (!mime)
@@ -67,7 +50,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
   silc_mime_free(mime);
   SILC_LOG_DEBUG(("Decoding MIME message"));
-  mime = silc_mime_decode(enc, enc_len);
+  mime = silc_mime_decode(NULL, enc, enc_len);
   if (!mime)
     goto err;
   SILC_LOG_DEBUG(("Re-encoding MIME context"));
@@ -79,7 +62,38 @@ int main(int argc, char **argv)
   silc_free(enc);
   silc_mime_free(mime);
 
-  /* Multipart test, with nesting */
+  /*
+   * Empty data area, only headers present
+   */
+  SILC_LOG_DEBUG(("Allocating MIME message context"));
+  mime = silc_mime_alloc();
+  if (!mime)
+    goto err;
+  SILC_LOG_DEBUG(("Adding Content-Transfer-Encoding: binary"));
+  silc_mime_add_field(mime, "Content-Transfer-Encoding", "binary");
+  SILC_LOG_DEBUG(("No data area, only header present"));
+  SILC_LOG_DEBUG(("Encoding MIME context"));
+  enc = silc_mime_encode(mime, &enc_len);
+  if (!enc)
+    goto err;
+  SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
+  silc_mime_free(mime);
+  SILC_LOG_DEBUG(("Decoding MIME message"));
+  mime = silc_mime_decode(NULL, enc, enc_len);
+  if (!mime)
+    goto err;
+  SILC_LOG_DEBUG(("Re-encoding MIME context"));
+  silc_free(enc);
+  enc = silc_mime_encode(mime, &enc_len);
+  if (!enc)
+    goto err;
+  SILC_LOG_HEXDUMP(("Re-encoded MIME message:"), enc, enc_len);
+  silc_free(enc);
+  silc_mime_free(mime);
+
+  /*
+   * Multipart test, with nesting
+   */
   SILC_LOG_DEBUG(("Allocating MIME message context"));
   mime = silc_mime_alloc();
   if (!mime)
@@ -125,7 +139,7 @@ int main(int argc, char **argv)
   part = silc_mime_alloc();
   if (!part)
     goto err;
-  SILC_LOG_DEBUG(("Adding MIME data, 10 A's + 1 B"));
+  SILC_LOG_DEBUG(("Adding MIME data (NO HEADERS), 10 A's + 1 B"));
   for (i = 0; i < 10; i++)
     tmp[i] = 'A';
   tmp[10] = 'B';
@@ -135,6 +149,16 @@ int main(int argc, char **argv)
     goto err;
   SILC_LOG_DEBUG(("Allocating part"));
   part = silc_mime_alloc();
+  if (!part)
+    goto err;
+  SILC_LOG_DEBUG(("Adding Content-Type: image/foobar"));
+  SILC_LOG_DEBUG(("No data area, only header"));
+  silc_mime_add_field(part, "Content-Type", "image/foobar");
+  SILC_LOG_DEBUG(("Adding part to MIME message"));
+  if (!silc_mime_add_multipart(mime, part))
+    goto err;
+  SILC_LOG_DEBUG(("Allocating part"));
+  part = silc_mime_alloc();
   if (!part)
     goto err;
   SILC_LOG_DEBUG(("Adding part to MIME message"));
@@ -162,7 +186,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
   silc_mime_free(mime);
   SILC_LOG_DEBUG(("Decoding MIME message"));
-  mime = silc_mime_decode(enc, enc_len);
+  mime = silc_mime_decode(NULL, enc, enc_len);
   if (!mime)
     goto err;
   SILC_LOG_DEBUG(("Re-encoding MIME context"));
@@ -173,9 +197,10 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Re-encoded MIME message: \n%s", enc));
   silc_free(enc);
   SILC_LOG_DEBUG(("Get multiparts"));
-  frag = silc_mime_get_multiparts(mime);
+  frag = silc_mime_get_multiparts(mime, &mtype);
   if (!frag)
     goto err;
+  SILC_LOG_DEBUG(("Multipart type '%s'", mtype));
   silc_dlist_start(frag);
   while ((part = silc_dlist_get(frag)) != SILC_LIST_END) {
     SILC_LOG_DEBUG(("Encoding MIME part"));
@@ -189,9 +214,11 @@ int main(int argc, char **argv)
   }
   silc_mime_free(mime);
 
-  /* Fragmentation test */
+  /*
+   * Fragmentation test
+   */
   SILC_LOG_DEBUG(("Allocating MIME assembler"));
-  ass = silc_mime_assembler_alloc(ass_complete, NULL);
+  ass = silc_mime_assembler_alloc();
   if (!ass)
     goto err;
   SILC_LOG_DEBUG(("Allocating MIME message context"));
@@ -224,14 +251,24 @@ int main(int argc, char **argv)
     goto err;
   silc_dlist_start(frag);
   while ((buf = silc_dlist_get(frag)) != SILC_LIST_END)
-    SILC_LOG_DEBUG(("Fragment \n%s", buf->data, buf->len));
+    SILC_LOG_DEBUG(("Fragment \n%s", buf->data, silc_buffer_len(buf)));
   SILC_LOG_DEBUG(("Defragment"));
   silc_dlist_start(frag);
   while ((buf = silc_dlist_get(frag)) != SILC_LIST_END) {
-    part = silc_mime_decode(buf->data, buf->len);
+    part = silc_mime_decode(NULL, buf->data, silc_buffer_len(buf));
     if (!silc_mime_is_partial(part))
         goto err;
-    silc_mime_assemble(ass, part);
+    part = silc_mime_assemble(ass, part);
+    if (part) {
+      SILC_LOG_DEBUG(("Defragmentation completed"));
+      SILC_LOG_DEBUG(("Encoding MIME context"));
+      enc = silc_mime_encode(mime, &enc_len);
+      if (!enc)
+        SILC_LOG_DEBUG(("Error encoding"));
+      SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
+      silc_free(enc);
+      silc_mime_free(part);
+    }
   }
   silc_mime_partial_free(frag);
   silc_mime_assembler_free(ass);