test/mime_test.cc
author Volker Birk <vb@pep-project.org>
Sun, 31 May 2015 19:36:55 +0200
changeset 306 abca32c31ace
parent 302 1d1a2e776631
child 345 9e129a270a46
permissions -rw-r--r--
tests adapted
     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(), mimetext3.length(), &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->value, _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     assert(msg2);
    70     msg2->from = new_identity("vb@dingens.org", NULL, NULL, "Volker Birk");
    71     msg2->to = new_identity_list(new_identity("trischa@dingens.org", NULL, NULL, "Patricia Bädnar")),
    72     msg2->shortmsg = strdup("my sübject");
    73 
    74     string text2 = "my mèssage to yoü";
    75     msg2->longmsg = strdup(text2.c_str());
    76     string html2 = "<html><body><p>my message to you</p></body></html>";
    77     msg2->longmsg_formatted = strdup(html2.c_str());
    78     assert(msg2->longmsg_formatted);
    79 
    80     cout << "encoding message…\n";
    81     char *result2;
    82     PEP_STATUS status2 = mime_encode_message(msg2, false, &result2);
    83     assert(result2);
    84     assert(status2 == PEP_STATUS_OK);
    85 
    86     cout << "result:\n";
    87     cout << result2 << "\n";
    88 
    89     free(result2);
    90     free_message(msg2);
    91 
    92     test_mime_decoding("msg1.asc");
    93     test_mime_decoding("msg2.asc");
    94     test_mime_decoding("msg3.asc");
    95 
    96     cout << "calling release()\n";
    97     release(session);
    98     return 0;
    99 }
   100