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