test/message_api_test.cc
author Volker Birk <vb@pep.foundation>
Tue, 27 Dec 2016 21:13:41 +0100
changeset 1513 e7f7e42385b5
parent 1359 e63926f8b166
child 1714 a6712af7a964
permissions -rw-r--r--
adding license info to each file
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
Edouard@345
     4
#include <stdlib.h>
Edouard@345
     5
#include <string.h>
vb@165
     6
#include "platform.h"
vb@39
     7
#include <iostream>
vb@165
     8
#include <fstream>
vb@39
     9
#include <assert.h>
vb@221
    10
#include "mime.h"
vb@39
    11
#include "message_api.h"
vb@39
    12
vb@39
    13
using namespace std;
vb@39
    14
vb@39
    15
int main() {
vb@53
    16
    cout << "\n*** message_api_test ***\n\n";
vb@51
    17
vb@39
    18
    PEP_SESSION session;
vb@39
    19
    
vb@39
    20
    cout << "calling init()\n";
roker@759
    21
    PEP_STATUS status1 = init(&session);
vb@39
    22
    assert(status1 == PEP_STATUS_OK);
vb@39
    23
    assert(session);
vb@39
    24
    cout << "init() completed.\n";
vb@39
    25
vb@53
    26
    // message_api test code
vb@53
    27
vb@40
    28
    cout << "creating message…\n";
vb@822
    29
    pEp_identity * me2 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
Edouard@745
    30
    // pEp_identity * me2 = new_identity("test@nokey.plop", NULL, PEP_OWN_USERID, "Test no key");
vb@112
    31
    me2->me = true;
vb@822
    32
    identity_list *to2 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
Edouard@745
    33
    // identity_list *to2 = new_identity_list(new_identity("still@nokey.blup", NULL, "42", "Still no key"));
vb@306
    34
    message *msg2 = new_message(PEP_dir_outgoing);
vb@112
    35
    assert(msg2);
vb@306
    36
    msg2->from = me2;
vb@306
    37
    msg2->to = to2;
vb@306
    38
    msg2->shortmsg = strdup("hello, world");
Edouard@747
    39
    msg2->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
vb@40
    40
    cout << "message created.\n";
vb@39
    41
roker@759
    42
    char *text2 = nullptr;
vb@113
    43
    PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
vb@112
    44
    assert(status2 == PEP_STATUS_OK);
vb@112
    45
    assert(text2);
vb@112
    46
vb@112
    47
    cout << "decrypted:\n\n";
vb@112
    48
    cout << text2 << "\n";
vb@112
    49
vb@112
    50
    free(text2);
vb@112
    51
vb@48
    52
    cout << "encrypting message as MIME multipart…\n";
roker@759
    53
    message *enc_msg2 = nullptr;
vb@40
    54
    cout << "calling encrypt_message()\n";
vb@940
    55
    status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME, 0);
roker@759
    56
    cout << "encrypt_message() returns " << status2 << '.' << endl;
vb@112
    57
    assert(status2 == PEP_STATUS_OK);
vb@112
    58
    assert(enc_msg2);
vb@40
    59
    cout << "message encrypted.\n";
vb@48
    60
    
vb@113
    61
    status2 = mime_encode_message(enc_msg2, false, &text2);
vb@112
    62
    assert(status2 == PEP_STATUS_OK);
vb@112
    63
    assert(text2);
vb@112
    64
vb@112
    65
    cout << "encrypted:\n\n";
vb@112
    66
    cout << text2 << "\n";
vb@112
    67
roker@759
    68
    message *msg3 = nullptr;
vb@281
    69
    PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
vb@116
    70
    assert(status3 == PEP_STATUS_OK);
roker@759
    71
    const string string3 = text2;
Edouard@745
    72
    //free(text2);
vb@165
    73
vb@165
    74
    unlink("msg4.asc");
vb@165
    75
    ofstream outFile3("msg4.asc");
vb@165
    76
    outFile3.write(string3.c_str(), string3.size());
vb@165
    77
    outFile3.close();
vb@71
    78
roker@759
    79
    message *msg4 = nullptr;
roker@759
    80
    stringlist_t *keylist4 = nullptr;
vb@1006
    81
    PEP_rating rating;
Edouard@728
    82
    PEP_decrypt_flags_t flags;
Edouard@728
    83
    
vb@1006
    84
    PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &rating, &flags);
vb@267
    85
    assert(status4 == PEP_STATUS_OK);
vb@166
    86
    assert(msg4);
vb@241
    87
    assert(keylist4);
vb@1006
    88
    assert(rating);
vb@781
    89
    PEP_comm_type ct = enc_msg2->from->comm_type;
vb@822
    90
    assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
vb@781
    91
vb@781
    92
    free_stringpair_list(enc_msg2->opt_fields);
vb@781
    93
    enc_msg2->opt_fields = NULL;
vb@241
    94
vb@241
    95
    cout << "keys used:";
vb@781
    96
roker@759
    97
    for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
roker@759
    98
    {
vb@241
    99
        cout << " " << kl4->value;
roker@759
   100
    }
vb@241
   101
    cout << "\n\n";
vb@241
   102
vb@241
   103
    free_stringlist(keylist4);
vb@166
   104
Edouard@745
   105
    cout << "opening msg_no_key.asc for reading\n";
Edouard@745
   106
    ifstream inFile3 ("msg_no_key.asc");
Edouard@745
   107
    assert(inFile3.is_open());
Edouard@745
   108
Edouard@745
   109
    string text3;
Edouard@745
   110
Edouard@745
   111
    cout << "reading msg_no_key.asc sample\n";
Edouard@745
   112
    while (!inFile3.eof()) {
Edouard@745
   113
        static string line;
Edouard@745
   114
        getline(inFile3, line);
Edouard@745
   115
        text3 += line + "\r\n";
Edouard@745
   116
    }
Edouard@745
   117
    inFile3.close();
Edouard@745
   118
roker@759
   119
    message *msg5 = nullptr;
Edouard@745
   120
    PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
Edouard@745
   121
    assert(status5 == PEP_STATUS_OK);
Edouard@745
   122
roker@759
   123
    message *msg6 = nullptr;
roker@759
   124
    stringlist_t *keylist5 = nullptr;
vb@1006
   125
    PEP_rating rating2;
Edouard@746
   126
    PEP_decrypt_flags_t flags2;
vb@1006
   127
    PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &rating2, &flags2);
Edouard@745
   128
    assert(status6 == PEP_DECRYPT_NO_KEY);
Edouard@745
   129
    assert(msg6 == NULL);
Edouard@745
   130
    assert(keylist5 == NULL);
vb@1006
   131
    assert(rating2 == PEP_rating_have_no_key);
vb@1006
   132
    cout << "rating :" << rating2 << "\n";
Edouard@745
   133
    free_stringlist(keylist5);
Edouard@745
   134
krista@1325
   135
    cout << "\nTesting MIME_encrypt_message / MIME_decrypt_message...\n\n";
krista@1325
   136
krista@1325
   137
    cout << "opening alice_bob_encrypt_test_plaintext_mime.eml for reading\n";
krista@1325
   138
    ifstream inFile4 ("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
krista@1325
   139
    assert(inFile4.is_open());
krista@1325
   140
    
krista@1325
   141
    string text4;
krista@1325
   142
    
krista@1325
   143
    cout << "reading alice_bob_encrypt_test_plaintext_mime.eml sample\n";
krista@1325
   144
    while (!inFile4.eof()) {
krista@1325
   145
        static string line;
krista@1325
   146
        getline(inFile4, line);
krista@1325
   147
        text4 += line + "\r\n";
krista@1325
   148
    }
krista@1325
   149
    inFile4.close();
krista@1325
   150
    
krista@1359
   151
    const char* out_msg_plain = text4.c_str();
krista@1358
   152
    
krista@1359
   153
//    const char* out_msg_plain = "From: krista@kgrothoff.org\nTo: Volker <vb@pep-project.org>\nSubject: Test\nContent-Type: text/plain; charset=utf-8\nContent-Language: en-US\nContent-Transfer-Encoding:quoted-printable\n\ngaga\n\n";
krista@1325
   154
    char* enc_msg = NULL;
krista@1325
   155
    char* dec_msg = NULL;
krista@1325
   156
krista@1359
   157
    PEP_STATUS status7 = MIME_encrypt_message(session, text4.c_str(), text4.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@1359
   158
//    PEP_STATUS status7 = MIME_encrypt_message(session, out_msg_plain, strlen(out_msg_plain), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@1325
   159
    assert(status7 == PEP_STATUS_OK);
krista@1325
   160
    
krista@1325
   161
    cout << enc_msg << endl;
krista@1328
   162
krista@1328
   163
    string text5 = enc_msg;
krista@1328
   164
    
krista@1328
   165
    PEP_decrypt_flags_t dec_flags;
krista@1328
   166
    stringlist_t* keys_used;
krista@1328
   167
    
krista@1328
   168
    PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags);
krista@1328
   169
    assert(status8 == PEP_STATUS_OK);
krista@1328
   170
    
krista@1328
   171
    cout << dec_msg << endl;
krista@1328
   172
    
krista@1325
   173
    
vb@40
   174
    cout << "freeing messages…\n";
vb@166
   175
    free_message(msg4);
vb@116
   176
    free_message(msg3);
vb@112
   177
    free_message(msg2);
vb@112
   178
    free_message(enc_msg2);
Edouard@745
   179
    free_message(msg6);
Edouard@745
   180
    free_message(msg5);
vb@40
   181
    cout << "done.\n";
vb@39
   182
krista@1328
   183
    free(enc_msg);
krista@1328
   184
    free(dec_msg);
vb@39
   185
    cout << "calling release()\n";
vb@39
   186
    release(session);
vb@39
   187
    return 0;
vb@39
   188
}