test/src/engine_tests/MessageApiTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 29 Jan 2019 19:19:30 +0100
branchENGINE-448
changeset 3254 6e7f6bc9460a
parent 2928 6d8e51098e7a
child 3276 c0b3430f1f1d
permissions -rw-r--r--
ENGINE-448: made key removal much less aggressive. NetPGP will still be a problem, but versions built against gpg will now only remove actual keys
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
Edouard@345
     4
#include <stdlib.h>
krista@2661
     5
#include <string>
krista@2661
     6
#include <cstring>
vb@39
     7
#include <iostream>
vb@165
     8
#include <fstream>
krista@2661
     9
krista@2661
    10
#include "pEpEngine.h"
krista@2661
    11
#include "platform.h"
vb@221
    12
#include "mime.h"
vb@39
    13
#include "message_api.h"
krista@2496
    14
#include "test_util.h"
vb@39
    15
krista@2661
    16
#include <cpptest.h>
krista@2661
    17
#include "EngineTestSessionSuite.h"
krista@2661
    18
#include "MessageApiTests.h"
krista@2661
    19
vb@39
    20
using namespace std;
vb@39
    21
krista@2661
    22
MessageApiTests::MessageApiTests(string suitename, string test_home_dir) :
krista@2661
    23
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
krista@2661
    24
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("MessageApiTests::check_message_api"),
krista@2661
    25
                                                                      static_cast<Func>(&MessageApiTests::check_message_api)));
krista@2661
    26
}
vb@51
    27
krista@2661
    28
void MessageApiTests::check_message_api() {
krista@2496
    29
    cout << "Importing Alice's key " << endl;
krista@2496
    30
    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
krista@2496
    31
    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
krista@2496
    32
    const string bob_pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
krista@2496
    33
krista@2496
    34
    PEP_STATUS status0 = import_key(session, alice_pub_key.c_str(), alice_pub_key.size(), NULL);
krista@3254
    35
    TEST_ASSERT_MSG((status0 == PEP_KEY_IMPORTED), "status0 == PEP_STATUS_OK");
krista@2496
    36
    status0 = import_key(session, alice_priv_key.c_str(), alice_priv_key.size(), NULL);
krista@3254
    37
    TEST_ASSERT_MSG((status0 == PEP_KEY_IMPORTED), "status0 == PEP_STATUS_OK");
krista@2496
    38
    status0 = import_key(session, bob_pub_key.c_str(), bob_pub_key.size(), NULL);
krista@3254
    39
    TEST_ASSERT_MSG((status0 == PEP_KEY_IMPORTED), "status0 == PEP_STATUS_OK");
vb@53
    40
    // message_api test code
vb@53
    41
vb@40
    42
    cout << "creating message…\n";
vb@822
    43
    pEp_identity * me2 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
Edouard@745
    44
    // pEp_identity * me2 = new_identity("test@nokey.plop", NULL, PEP_OWN_USERID, "Test no key");
krista@2461
    45
    me2->me = true;
vb@822
    46
    identity_list *to2 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
Edouard@745
    47
    // identity_list *to2 = new_identity_list(new_identity("still@nokey.blup", NULL, "42", "Still no key"));
vb@306
    48
    message *msg2 = new_message(PEP_dir_outgoing);
krista@2669
    49
    TEST_ASSERT_MSG((msg2), "msg2");
vb@306
    50
    msg2->from = me2;
vb@306
    51
    msg2->to = to2;
vb@306
    52
    msg2->shortmsg = strdup("hello, world");
krista@1871
    53
    msg2->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
vb@40
    54
    cout << "message created.\n";
vb@39
    55
roker@759
    56
    char *text2 = nullptr;
vb@113
    57
    PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
krista@2669
    58
    TEST_ASSERT_MSG((status2 == PEP_STATUS_OK), "status2 == PEP_STATUS_OK");
krista@2669
    59
    TEST_ASSERT_MSG((text2), "text2");
vb@112
    60
vb@112
    61
    cout << "decrypted:\n\n";
vb@112
    62
    cout << text2 << "\n";
vb@112
    63
vb@112
    64
    free(text2);
vb@112
    65
vb@48
    66
    cout << "encrypting message as MIME multipart…\n";
roker@759
    67
    message *enc_msg2 = nullptr;
vb@48
    68
    cout << "calling encrypt_message()\n";
vb@940
    69
    status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME, 0);
roker@759
    70
    cout << "encrypt_message() returns " << status2 << '.' << endl;
krista@2669
    71
    TEST_ASSERT_MSG((status2 == PEP_STATUS_OK), "status2 == PEP_STATUS_OK");
krista@2669
    72
    TEST_ASSERT_MSG((enc_msg2), "enc_msg2");
vb@48
    73
    cout << "message encrypted.\n";
vb@48
    74
    
vb@113
    75
    status2 = mime_encode_message(enc_msg2, false, &text2);
krista@2669
    76
    TEST_ASSERT_MSG((status2 == PEP_STATUS_OK), "status2 == PEP_STATUS_OK");
krista@2669
    77
    TEST_ASSERT_MSG((text2), "text2");
vb@112
    78
vb@112
    79
    cout << "encrypted:\n\n";
vb@112
    80
    cout << text2 << "\n";
vb@112
    81
roker@759
    82
    message *msg3 = nullptr;
vb@281
    83
    PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
krista@2669
    84
    TEST_ASSERT_MSG((status3 == PEP_STATUS_OK), "status3 == PEP_STATUS_OK");
roker@759
    85
    const string string3 = text2;
Edouard@745
    86
    //free(text2);
vb@165
    87
vb@165
    88
    unlink("msg4.asc");
vb@165
    89
    ofstream outFile3("msg4.asc");
vb@165
    90
    outFile3.write(string3.c_str(), string3.size());
vb@165
    91
    outFile3.close();
vb@71
    92
roker@759
    93
    message *msg4 = nullptr;
roker@759
    94
    stringlist_t *keylist4 = nullptr;
vb@1006
    95
    PEP_rating rating;
Edouard@728
    96
    PEP_decrypt_flags_t flags;
Edouard@728
    97
    
krista@2615
    98
    flags = 0;
vb@1006
    99
    PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &rating, &flags);
krista@3254
   100
    TEST_ASSERT_MSG((status4 == PEP_STATUS_OK), tl_status_string(status4));
krista@2669
   101
    TEST_ASSERT_MSG((msg4), "msg4");
krista@2669
   102
    TEST_ASSERT_MSG((keylist4), "keylist4");
krista@2669
   103
    TEST_ASSERT_MSG((rating), "rating");
vb@781
   104
    PEP_comm_type ct = enc_msg2->from->comm_type;
krista@2669
   105
    TEST_ASSERT_MSG((ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed ), "ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed ");
vb@781
   106
vb@781
   107
    free_stringpair_list(enc_msg2->opt_fields);
vb@781
   108
    enc_msg2->opt_fields = NULL;
vb@241
   109
vb@241
   110
    cout << "keys used:";
vb@781
   111
roker@759
   112
    for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
roker@759
   113
    {
vb@241
   114
        cout << " " << kl4->value;
roker@759
   115
    }
vb@241
   116
    cout << "\n\n";
vb@241
   117
vb@241
   118
    free_stringlist(keylist4);
vb@166
   119
Edouard@745
   120
    cout << "opening msg_no_key.asc for reading\n";
Edouard@745
   121
    ifstream inFile3 ("msg_no_key.asc");
krista@2669
   122
    TEST_ASSERT_MSG((inFile3.is_open()), "inFile3.is_open()");
Edouard@745
   123
Edouard@745
   124
    string text3;
Edouard@745
   125
Edouard@745
   126
    cout << "reading msg_no_key.asc sample\n";
Edouard@745
   127
    while (!inFile3.eof()) {
Edouard@745
   128
        static string line;
Edouard@745
   129
        getline(inFile3, line);
Edouard@745
   130
        text3 += line + "\r\n";
Edouard@745
   131
    }
Edouard@745
   132
    inFile3.close();
Edouard@745
   133
roker@759
   134
    message *msg5 = nullptr;
Edouard@745
   135
    PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
krista@2669
   136
    TEST_ASSERT_MSG((status5 == PEP_STATUS_OK), "status5 == PEP_STATUS_OK");
Edouard@745
   137
roker@759
   138
    message *msg6 = nullptr;
roker@759
   139
    stringlist_t *keylist5 = nullptr;
vb@1006
   140
    PEP_rating rating2;
Edouard@746
   141
    PEP_decrypt_flags_t flags2;
krista@2615
   142
    flags2 = 0;
vb@1006
   143
    PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &rating2, &flags2);
krista@2669
   144
    TEST_ASSERT_MSG((status6 == PEP_DECRYPT_NO_KEY), "status6 == PEP_DECRYPT_NO_KEY");
krista@2669
   145
    TEST_ASSERT_MSG((msg6 == NULL), "msg6 == NULL");
krista@2669
   146
    TEST_ASSERT_MSG((keylist5 == NULL), "keylist5 == NULL");
krista@2669
   147
    TEST_ASSERT_MSG((rating2 == PEP_rating_have_no_key), "rating2 == PEP_rating_have_no_key");
vb@1006
   148
    cout << "rating :" << rating2 << "\n";
Edouard@745
   149
    free_stringlist(keylist5);
Edouard@745
   150
krista@1325
   151
    cout << "\nTesting MIME_encrypt_message / MIME_decrypt_message...\n\n";
krista@1325
   152
krista@1325
   153
    cout << "opening alice_bob_encrypt_test_plaintext_mime.eml for reading\n";
krista@1325
   154
    ifstream inFile4 ("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
krista@2669
   155
    TEST_ASSERT_MSG((inFile4.is_open()), "inFile4.is_open()");
krista@1325
   156
    
krista@1325
   157
    string text4;
krista@1325
   158
    
krista@1325
   159
    cout << "reading alice_bob_encrypt_test_plaintext_mime.eml sample\n";
krista@1325
   160
    while (!inFile4.eof()) {
krista@1325
   161
        static string line;
krista@1325
   162
        getline(inFile4, line);
krista@1325
   163
        text4 += line + "\r\n";
krista@1325
   164
    }
krista@1325
   165
    inFile4.close();
krista@1325
   166
    
krista@1359
   167
    const char* out_msg_plain = text4.c_str();
krista@1358
   168
    
krista@1359
   169
//    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
   170
    char* enc_msg = NULL;
krista@1325
   171
    char* dec_msg = NULL;
krista@1325
   172
krista@1359
   173
    PEP_STATUS status7 = MIME_encrypt_message(session, text4.c_str(), text4.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@1359
   174
//    PEP_STATUS status7 = MIME_encrypt_message(session, out_msg_plain, strlen(out_msg_plain), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@2669
   175
    TEST_ASSERT_MSG((status7 == PEP_STATUS_OK), "status7 == PEP_STATUS_OK");
krista@1325
   176
    
krista@1325
   177
    cout << enc_msg << endl;
krista@1328
   178
krista@1328
   179
    string text5 = enc_msg;
krista@1328
   180
    
krista@1328
   181
    PEP_decrypt_flags_t dec_flags;
krista@1328
   182
    stringlist_t* keys_used;
krista@1328
   183
    
krista@2615
   184
    dec_flags = 0;
krista@2623
   185
    char* modified_src = NULL;
krista@2623
   186
    PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags, &modified_src);
krista@2669
   187
    TEST_ASSERT_MSG((status8 == PEP_STATUS_OK), "status8 == PEP_STATUS_OK");
krista@1328
   188
    
krista@1328
   189
    cout << dec_msg << endl;
krista@1328
   190
    
vb@2340
   191
    cout << "\nTesting encrypt_message() with enc_format = PEP_enc_none\n\n";
vb@2340
   192
vb@2340
   193
    message *msg7 = new_message(PEP_dir_outgoing);
vb@2340
   194
    pEp_identity * me7 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
vb@2340
   195
    identity_list *to7 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
vb@2340
   196
    msg7->from = me7;
vb@2340
   197
    msg7->to = to7;
vb@2340
   198
    msg7->shortmsg = strdup("My Subject");
vb@2340
   199
    msg7->longmsg = strdup("This is some text.\n");
vb@2340
   200
vb@2340
   201
    message *enc7 = nullptr;
vb@2340
   202
    PEP_STATUS status9 = encrypt_message(session, msg7, NULL, &enc7, PEP_enc_none, 0);
vb@2340
   203
	std::cout << "encrypt_message returned " << std::dec << status9 << std::hex << " (0x" << status9 << ")" << std::dec << endl;
krista@2669
   204
    TEST_ASSERT_MSG((status9 == PEP_UNENCRYPTED), "status9 == PEP_UNENCRYPTED");
krista@2669
   205
    TEST_ASSERT_MSG((enc7 == nullptr), "enc7 == nullptr");
krista@2669
   206
    TEST_ASSERT_MSG((msg7->shortmsg && msg7->longmsg), "msg7->shortmsg && msg7->longmsg");
vb@2340
   207
    cout << msg7->shortmsg << "\n";
vb@2340
   208
    cout << msg7->longmsg << "\n";
krista@2669
   209
    TEST_ASSERT_MSG((strcmp(msg7->shortmsg, "My Subject") == 0), "strcmp(msg7->shortmsg, \"My Subject\") == 0");
krista@2669
   210
    TEST_ASSERT_MSG((strcmp(msg7->longmsg, "This is some text.\n") == 0), "strcmp(msg7->longmsg, \"This is some text.\n\") == 0");
krista@1325
   211
    
vb@2340
   212
    cout << "\nfreeing messages…\n";
vb@2340
   213
    free_message(msg7);
vb@2340
   214
    free_message(msg6);
vb@2340
   215
    free_message(msg5);
vb@166
   216
    free_message(msg4);
vb@116
   217
    free_message(msg3);
vb@112
   218
    free_message(msg2);
vb@112
   219
    free_message(enc_msg2);
vb@40
   220
    cout << "done.\n";
vb@39
   221
krista@1328
   222
    free(enc_msg);
krista@2928
   223
    free(dec_msg);    
vb@39
   224
}