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