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