test/message_api_test.cc
author Krista Grothoff <krista@pep-project.org>
Mon, 24 Oct 2016 14:35:42 +0200
branchENGINE-128
changeset 1325 5f2e643a4fd7
parent 1006 0dc1800f8b2d
child 1328 b8163caba309
permissions -rw-r--r--
ENGINE-128: MIME_encrypt/decrypt convenience functions almost up and running/tested! Need to fix bug in default first.
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";
roker@759
    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@822
    26
    pEp_identity * me2 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice 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@822
    29
    identity_list *to2 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
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");
Edouard@747
    36
    msg2->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
vb@40
    37
    cout << "message created.\n";
vb@39
    38
roker@759
    39
    char *text2 = nullptr;
vb@113
    40
    PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
vb@112
    41
    assert(status2 == PEP_STATUS_OK);
vb@112
    42
    assert(text2);
vb@112
    43
vb@112
    44
    cout << "decrypted:\n\n";
vb@112
    45
    cout << text2 << "\n";
vb@112
    46
vb@112
    47
    free(text2);
vb@112
    48
vb@48
    49
    cout << "encrypting message as MIME multipart…\n";
roker@759
    50
    message *enc_msg2 = nullptr;
vb@40
    51
    cout << "calling encrypt_message()\n";
vb@940
    52
    status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME, 0);
roker@759
    53
    cout << "encrypt_message() returns " << status2 << '.' << endl;
vb@112
    54
    assert(status2 == PEP_STATUS_OK);
vb@112
    55
    assert(enc_msg2);
vb@40
    56
    cout << "message encrypted.\n";
vb@48
    57
    
vb@113
    58
    status2 = mime_encode_message(enc_msg2, false, &text2);
vb@112
    59
    assert(status2 == PEP_STATUS_OK);
vb@112
    60
    assert(text2);
vb@112
    61
vb@112
    62
    cout << "encrypted:\n\n";
vb@112
    63
    cout << text2 << "\n";
vb@112
    64
roker@759
    65
    message *msg3 = nullptr;
vb@281
    66
    PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
vb@116
    67
    assert(status3 == PEP_STATUS_OK);
roker@759
    68
    const string string3 = text2;
Edouard@745
    69
    //free(text2);
vb@165
    70
vb@165
    71
    unlink("msg4.asc");
vb@165
    72
    ofstream outFile3("msg4.asc");
vb@165
    73
    outFile3.write(string3.c_str(), string3.size());
vb@165
    74
    outFile3.close();
vb@71
    75
roker@759
    76
    message *msg4 = nullptr;
roker@759
    77
    stringlist_t *keylist4 = nullptr;
vb@1006
    78
    PEP_rating rating;
Edouard@728
    79
    PEP_decrypt_flags_t flags;
Edouard@728
    80
    
vb@1006
    81
    PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &rating, &flags);
vb@267
    82
    assert(status4 == PEP_STATUS_OK);
vb@166
    83
    assert(msg4);
vb@241
    84
    assert(keylist4);
vb@1006
    85
    assert(rating);
vb@781
    86
    PEP_comm_type ct = enc_msg2->from->comm_type;
vb@822
    87
    assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
vb@781
    88
vb@781
    89
    free_stringpair_list(enc_msg2->opt_fields);
vb@781
    90
    enc_msg2->opt_fields = NULL;
vb@241
    91
vb@241
    92
    cout << "keys used:";
vb@781
    93
roker@759
    94
    for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
roker@759
    95
    {
vb@241
    96
        cout << " " << kl4->value;
roker@759
    97
    }
vb@241
    98
    cout << "\n\n";
vb@241
    99
vb@241
   100
    free_stringlist(keylist4);
vb@166
   101
Edouard@745
   102
    cout << "opening msg_no_key.asc for reading\n";
Edouard@745
   103
    ifstream inFile3 ("msg_no_key.asc");
Edouard@745
   104
    assert(inFile3.is_open());
Edouard@745
   105
Edouard@745
   106
    string text3;
Edouard@745
   107
Edouard@745
   108
    cout << "reading msg_no_key.asc sample\n";
Edouard@745
   109
    while (!inFile3.eof()) {
Edouard@745
   110
        static string line;
Edouard@745
   111
        getline(inFile3, line);
Edouard@745
   112
        text3 += line + "\r\n";
Edouard@745
   113
    }
Edouard@745
   114
    inFile3.close();
Edouard@745
   115
roker@759
   116
    message *msg5 = nullptr;
Edouard@745
   117
    PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
Edouard@745
   118
    assert(status5 == PEP_STATUS_OK);
Edouard@745
   119
roker@759
   120
    message *msg6 = nullptr;
roker@759
   121
    stringlist_t *keylist5 = nullptr;
vb@1006
   122
    PEP_rating rating2;
Edouard@746
   123
    PEP_decrypt_flags_t flags2;
vb@1006
   124
    PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &rating2, &flags2);
Edouard@745
   125
    assert(status6 == PEP_DECRYPT_NO_KEY);
Edouard@745
   126
    assert(msg6 == NULL);
Edouard@745
   127
    assert(keylist5 == NULL);
vb@1006
   128
    assert(rating2 == PEP_rating_have_no_key);
vb@1006
   129
    cout << "rating :" << rating2 << "\n";
Edouard@745
   130
    free_stringlist(keylist5);
Edouard@745
   131
krista@1325
   132
    cout << "\nTesting MIME_encrypt_message / MIME_decrypt_message...\n\n";
krista@1325
   133
krista@1325
   134
    cout << "opening alice_bob_encrypt_test_plaintext_mime.eml for reading\n";
krista@1325
   135
    ifstream inFile4 ("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
krista@1325
   136
    assert(inFile4.is_open());
krista@1325
   137
    
krista@1325
   138
    string text4;
krista@1325
   139
    
krista@1325
   140
    cout << "reading alice_bob_encrypt_test_plaintext_mime.eml sample\n";
krista@1325
   141
    while (!inFile4.eof()) {
krista@1325
   142
        static string line;
krista@1325
   143
        getline(inFile4, line);
krista@1325
   144
        text4 += line + "\r\n";
krista@1325
   145
    }
krista@1325
   146
    inFile4.close();
krista@1325
   147
    
krista@1325
   148
    const char* out_msg_plain = text4.c_str();
krista@1325
   149
    char* enc_msg = NULL;
krista@1325
   150
    char* dec_msg = NULL;
krista@1325
   151
krista@1325
   152
    PEP_STATUS status7 = MIME_encrypt_message(session, text4.c_str(), text4.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@1325
   153
    assert(status7 == PEP_STATUS_OK);
krista@1325
   154
    
krista@1325
   155
    cout << enc_msg << endl;
krista@1325
   156
    
vb@40
   157
    cout << "freeing messages…\n";
vb@166
   158
    free_message(msg4);
vb@116
   159
    free_message(msg3);
vb@112
   160
    free_message(msg2);
vb@112
   161
    free_message(enc_msg2);
Edouard@745
   162
    free_message(msg6);
Edouard@745
   163
    free_message(msg5);
vb@40
   164
    cout << "done.\n";
vb@39
   165
vb@39
   166
    cout << "calling release()\n";
vb@39
   167
    release(session);
vb@39
   168
    return 0;
vb@39
   169
}