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