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