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