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