test/message_api_test.cc
author Edouard Tisserant
Mon, 31 Aug 2015 16:34:18 +0200
changeset 345 9e129a270a46
parent 306 abca32c31ace
child 678 f8181e59ae2f
permissions -rw-r--r--
Fixed unittests on Linux
Edouard@345
     1
#include <stdlib.h>
Edouard@345
     2
#include <string.h>
vb@165
     3
#include "platform.h"
vb@39
     4
#include <iostream>
vb@165
     5
#include <fstream>
vb@39
     6
#include <assert.h>
vb@221
     7
#include "mime.h"
vb@39
     8
#include "message_api.h"
vb@39
     9
vb@39
    10
using namespace std;
vb@39
    11
vb@39
    12
int main() {
vb@53
    13
    cout << "\n*** message_api_test ***\n\n";
vb@51
    14
vb@39
    15
    PEP_SESSION session;
vb@39
    16
    
vb@39
    17
    cout << "calling init()\n";
vb@39
    18
    PEP_STATUS status1 = init(&session);   
vb@39
    19
    assert(status1 == PEP_STATUS_OK);
vb@39
    20
    assert(session);
vb@39
    21
    cout << "init() completed.\n";
vb@39
    22
vb@53
    23
    // message_api test code
vb@53
    24
vb@40
    25
    cout << "creating message…\n";
vb@112
    26
    pEp_identity * me2 = new_identity("outlooktest@dingens.org", NULL, "23", "Outlook Test");
vb@112
    27
    me2->me = true;
vb@112
    28
    identity_list *to2 = new_identity_list(new_identity("vb@dingens.org", NULL, "42", "Volker Birk"));
vb@306
    29
    message *msg2 = new_message(PEP_dir_outgoing);
vb@112
    30
    assert(msg2);
vb@306
    31
    msg2->from = me2;
vb@306
    32
    msg2->to = to2;
vb@306
    33
    msg2->shortmsg = strdup("hello, world");
vb@40
    34
    cout << "message created.\n";
vb@39
    35
vb@112
    36
    char *text2;
vb@113
    37
    PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
vb@112
    38
    assert(status2 == PEP_STATUS_OK);
vb@112
    39
    assert(text2);
vb@112
    40
vb@112
    41
    cout << "decrypted:\n\n";
vb@112
    42
    cout << text2 << "\n";
vb@112
    43
vb@112
    44
    free(text2);
vb@112
    45
vb@48
    46
    cout << "encrypting message as MIME multipart…\n";
vb@112
    47
    message *enc_msg2;
vb@40
    48
    cout << "calling encrypt_message()\n";
vb@113
    49
    status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME);
vb@112
    50
    assert(status2 == PEP_STATUS_OK);
vb@112
    51
    assert(enc_msg2);
vb@40
    52
    cout << "message encrypted.\n";
vb@48
    53
    
vb@113
    54
    status2 = mime_encode_message(enc_msg2, false, &text2);
vb@112
    55
    assert(status2 == PEP_STATUS_OK);
vb@112
    56
    assert(text2);
vb@112
    57
vb@112
    58
    cout << "encrypted:\n\n";
vb@112
    59
    cout << text2 << "\n";
vb@112
    60
vb@116
    61
    message *msg3;
vb@281
    62
    PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
vb@116
    63
    assert(status3 == PEP_STATUS_OK);
vb@165
    64
    string string3 = text2;
vb@165
    65
    free(text2);
vb@165
    66
vb@165
    67
    unlink("msg4.asc");
vb@165
    68
    ofstream outFile3("msg4.asc");
vb@165
    69
    outFile3.write(string3.c_str(), string3.size());
vb@165
    70
    outFile3.close();
vb@71
    71
vb@166
    72
    message *msg4;
vb@241
    73
    stringlist_t *keylist4;
vb@251
    74
    PEP_color color;
vb@260
    75
    PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &color);
vb@267
    76
    assert(status4 == PEP_STATUS_OK);
vb@166
    77
    assert(msg4);
vb@241
    78
    assert(keylist4);
vb@267
    79
    assert(color);
vb@241
    80
vb@241
    81
    cout << "keys used:";
vb@241
    82
    stringlist_t *kl4;
vb@241
    83
    for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
vb@241
    84
        cout << " " << kl4->value;
vb@241
    85
    cout << "\n\n";
vb@241
    86
vb@241
    87
    free_stringlist(keylist4);
vb@166
    88
vb@40
    89
    cout << "freeing messages…\n";
vb@166
    90
    free_message(msg4);
vb@116
    91
    free_message(msg3);
vb@112
    92
    free_message(msg2);
vb@112
    93
    free_message(enc_msg2);
vb@40
    94
    cout << "done.\n";
vb@39
    95
vb@39
    96
    cout << "calling release()\n";
vb@39
    97
    release(session);
vb@39
    98
    return 0;
vb@39
    99
}
vb@39
   100