test/message_api_test.cc
author Edouard Tisserant
Mon, 20 Jun 2016 13:10:01 +0200
changeset 746 753b1ec63285
parent 745 32d7952cccfa
parent 728 80b966277620
child 747 d357dceebec6
permissions -rw-r--r--
Merged import_own_key
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@678
    26
    pEp_identity * me2 = new_identity("outlooktest@dingens.org", NULL, PEP_OWN_USERID, "Outlook Test");
Edouard@745
    27
    // pEp_identity * me2 = new_identity("test@nokey.plop", NULL, PEP_OWN_USERID, "Test no key");
vb@112
    28
    me2->me = true;
vb@112
    29
    identity_list *to2 = new_identity_list(new_identity("vb@dingens.org", NULL, "42", "Volker Birk"));
Edouard@745
    30
    // identity_list *to2 = new_identity_list(new_identity("still@nokey.blup", NULL, "42", "Still no key"));
vb@306
    31
    message *msg2 = new_message(PEP_dir_outgoing);
vb@112
    32
    assert(msg2);
vb@306
    33
    msg2->from = me2;
vb@306
    34
    msg2->to = to2;
vb@306
    35
    msg2->shortmsg = strdup("hello, world");
vb@40
    36
    cout << "message created.\n";
vb@39
    37
vb@112
    38
    char *text2;
vb@113
    39
    PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
vb@112
    40
    assert(status2 == PEP_STATUS_OK);
vb@112
    41
    assert(text2);
vb@112
    42
vb@112
    43
    cout << "decrypted:\n\n";
vb@112
    44
    cout << text2 << "\n";
vb@112
    45
vb@112
    46
    free(text2);
vb@112
    47
vb@48
    48
    cout << "encrypting message as MIME multipart…\n";
vb@112
    49
    message *enc_msg2;
vb@40
    50
    cout << "calling encrypt_message()\n";
vb@113
    51
    status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME);
vb@112
    52
    assert(status2 == PEP_STATUS_OK);
vb@112
    53
    assert(enc_msg2);
vb@40
    54
    cout << "message encrypted.\n";
vb@48
    55
    
vb@113
    56
    status2 = mime_encode_message(enc_msg2, false, &text2);
vb@112
    57
    assert(status2 == PEP_STATUS_OK);
vb@112
    58
    assert(text2);
vb@112
    59
vb@112
    60
    cout << "encrypted:\n\n";
vb@112
    61
    cout << text2 << "\n";
vb@112
    62
vb@116
    63
    message *msg3;
vb@281
    64
    PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
vb@116
    65
    assert(status3 == PEP_STATUS_OK);
vb@165
    66
    string string3 = text2;
Edouard@745
    67
    //free(text2);
vb@165
    68
vb@165
    69
    unlink("msg4.asc");
vb@165
    70
    ofstream outFile3("msg4.asc");
vb@165
    71
    outFile3.write(string3.c_str(), string3.size());
vb@165
    72
    outFile3.close();
vb@71
    73
vb@166
    74
    message *msg4;
vb@241
    75
    stringlist_t *keylist4;
vb@251
    76
    PEP_color color;
Edouard@728
    77
    PEP_decrypt_flags_t flags;
Edouard@728
    78
    
Edouard@728
    79
    PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &color, &flags);
vb@267
    80
    assert(status4 == PEP_STATUS_OK);
vb@166
    81
    assert(msg4);
vb@241
    82
    assert(keylist4);
vb@267
    83
    assert(color);
vb@241
    84
vb@241
    85
    cout << "keys used:";
vb@241
    86
    stringlist_t *kl4;
vb@241
    87
    for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
vb@241
    88
        cout << " " << kl4->value;
vb@241
    89
    cout << "\n\n";
vb@241
    90
vb@241
    91
    free_stringlist(keylist4);
vb@166
    92
Edouard@745
    93
    cout << "opening msg_no_key.asc for reading\n";
Edouard@745
    94
    ifstream inFile3 ("msg_no_key.asc");
Edouard@745
    95
    assert(inFile3.is_open());
Edouard@745
    96
Edouard@745
    97
    string text3;
Edouard@745
    98
Edouard@745
    99
    cout << "reading msg_no_key.asc sample\n";
Edouard@745
   100
    while (!inFile3.eof()) {
Edouard@745
   101
        static string line;
Edouard@745
   102
        getline(inFile3, line);
Edouard@745
   103
        text3 += line + "\r\n";
Edouard@745
   104
    }
Edouard@745
   105
    inFile3.close();
Edouard@745
   106
Edouard@745
   107
    message *msg5;
Edouard@745
   108
    PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
Edouard@745
   109
    assert(status5 == PEP_STATUS_OK);
Edouard@745
   110
Edouard@745
   111
    message *msg6;
Edouard@745
   112
    stringlist_t *keylist5;
Edouard@745
   113
    PEP_color color2;
Edouard@746
   114
    PEP_decrypt_flags_t flags2;
Edouard@746
   115
    PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &color2, &flags2);
Edouard@745
   116
    assert(status6 == PEP_DECRYPT_NO_KEY);
Edouard@745
   117
    assert(msg6 == NULL);
Edouard@745
   118
    assert(keylist5 == NULL);
Edouard@745
   119
    assert(color2 == PEP_rating_have_no_key);
Edouard@745
   120
    cout << "color :" << color2 << "\n";
Edouard@745
   121
    free_stringlist(keylist5);
Edouard@745
   122
vb@40
   123
    cout << "freeing messages…\n";
vb@166
   124
    free_message(msg4);
vb@116
   125
    free_message(msg3);
vb@112
   126
    free_message(msg2);
vb@112
   127
    free_message(enc_msg2);
Edouard@745
   128
    free_message(msg6);
Edouard@745
   129
    free_message(msg5);
vb@40
   130
    cout << "done.\n";
vb@39
   131
vb@39
   132
    cout << "calling release()\n";
vb@39
   133
    release(session);
vb@39
   134
    return 0;
vb@39
   135
}
vb@39
   136