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