test/mime_test.cc
author Volker Birk <vb@pep-project.org>
Sun, 29 Mar 2015 21:03:41 +0200
changeset 159 ad6d0993d1ab
parent 147 b03a10242e4c
child 165 f0894860af4a
permissions -rw-r--r--
char conversion to utf-8
     1 #include "platform.h"
     2 
     3 #include <iostream>
     4 #include <fstream>
     5 #include <string>
     6 #include <assert.h>
     7 
     8 #include "mime.h"
     9 
    10 using namespace std;
    11 
    12 int main() {
    13     cout << "\n*** mime_test ***\n\n";
    14 
    15     PEP_SESSION session;
    16     
    17     cout << "calling init()\n";
    18     PEP_STATUS status1 = init(&session);   
    19     assert(status1 == PEP_STATUS_OK);
    20     assert(session);
    21     cout << "init() completed.\n";
    22 
    23     // mime test code
    24 
    25     // testing multipart/alternative
    26 
    27     message *msg2 = new_message(PEP_dir_incoming,
    28             new_identity("vb@dingens.org", NULL, NULL, "Volker Birk"),
    29             new_identity_list(new_identity("trischa@dingens.org", NULL, NULL, "Patricia Bädnar")),
    30             "my sübject");
    31     assert(msg2);
    32     string text2 = "my mèssage to yoü";
    33     msg2->longmsg = strdup(text2.c_str());
    34     string html2 = "<html><body><p>my message to you</p></body></html>";
    35     msg2->longmsg_formatted = strdup(html2.c_str());
    36     assert(msg2->longmsg_formatted);
    37 
    38     cout << "encoding message…\n";
    39     char *result2;
    40     PEP_STATUS status2 = mime_encode_message(msg2, false, &result2);
    41     assert(result2);
    42     assert(status2 == PEP_STATUS_OK);
    43 
    44     cout << "result:\n";
    45     cout << result2 << "\n";
    46 
    47     free(result2);
    48     free_message(msg2);
    49 
    50     cout << "opening mime_sample.txt for reading\n";
    51     ifstream inFile3 ("mime_sample.txt");
    52     assert(inFile3.is_open());
    53 
    54     string mimetext3;
    55 
    56     cout << "reading mime sample\n";
    57     while (!inFile3.eof()) {
    58         static string line;
    59         getline(inFile3, line);
    60         mimetext3 += line + "\n";
    61     }
    62     inFile3.close();
    63 
    64     cout << "decoding message…\n";
    65     message *msg3;
    66     PEP_STATUS status3 = mime_decode_message(mimetext3.c_str(), &msg3);
    67     assert(status3 == PEP_STATUS_OK);
    68     assert(msg3);
    69     cout << "decoded.\n\n";
    70     cout << "Subject: " << msg3->shortmsg << "\n\n";
    71     if (msg3->longmsg)
    72         cout << msg3->longmsg << "\n\n";
    73     if (msg3->longmsg_formatted)
    74         cout << msg3->longmsg_formatted << "\n\n";
    75     bloblist_t *_b;
    76     for (_b = msg3->attachments; _b; _b = _b->next) {
    77         cout << "attachment of type " << _b->mime_type << "\n";
    78         if (_b->filename) {
    79             cout << "filename: " << _b->filename << "\n";
    80             unlink(_b->filename);
    81             ofstream outFile3(_b->filename);
    82             outFile3.write(_b->data, _b->size);
    83             outFile3.close();
    84         }
    85     }
    86 
    87     free_message(msg3);
    88 
    89     cout << "calling release()\n";
    90     release(session);
    91     return 0;
    92 }
    93