test/src/engine_tests/MessageTwoPointOhTests.cc
author Krista Bennett <krista@pep-project.org>
Mon, 26 Nov 2018 14:22:02 +0100
branchsync
changeset 3159 0489203c122d
parent 3136 56e8a45b4fa6
child 3270 d8aea79654c3
permissions -rw-r--r--
moved SyncTests out to SyncTests.cc.b0rken so sync can still be tested.
krista@2395
     1
// This file is under GNU General Public License 3.0
krista@2395
     2
// see LICENSE.txt
krista@2395
     3
krista@2395
     4
#include <stdlib.h>
krista@2661
     5
#include <string>
krista@2661
     6
#include <cstring>
krista@2395
     7
#include <iostream>
krista@2395
     8
#include <fstream>
krista@2661
     9
krista@2661
    10
#include "pEpEngine.h"
krista@2661
    11
#include "platform.h"
krista@2395
    12
#include "mime.h"
krista@2395
    13
#include "message_api.h"
krista@2395
    14
#include "keymanagement.h"
krista@2395
    15
#include "test_util.h"
krista@2395
    16
krista@2661
    17
#include <cpptest.h>
krista@2661
    18
#include "EngineTestSessionSuite.h"
krista@2661
    19
#include "MessageTwoPointOhTests.h"
krista@2661
    20
krista@2395
    21
using namespace std;
krista@2395
    22
krista@2661
    23
MessageTwoPointOhTests::MessageTwoPointOhTests(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("MessageTwoPointOhTests::check_message_two_point_oh"),
krista@2661
    26
                                                                      static_cast<Func>(&MessageTwoPointOhTests::check_message_two_point_oh)));
krista@2661
    27
}
krista@2395
    28
krista@2661
    29
void MessageTwoPointOhTests::check_message_two_point_oh() {
krista@2395
    30
krista@3159
    31
    PEP_comm_type carol_comm_type = PEP_ct_pEp;
krista@2395
    32
krista@2395
    33
    // message_api test code
krista@2395
    34
krista@2395
    35
    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
krista@2395
    36
    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
krista@2395
    37
    const string carol_pub_key = slurp("test_keys/pub/pep-test-carol-0x42A85A42_pub.asc");
krista@2395
    38
    const string carol_priv_key = slurp("test_keys/priv/pep-test-carol-0x42A85A42_priv.asc");
krista@2395
    39
krista@2395
    40
    PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
krista@2395
    41
    PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
krista@2669
    42
    TEST_ASSERT_MSG((statuspub == PEP_STATUS_OK), "statuspub == PEP_STATUS_OK");
krista@2669
    43
    TEST_ASSERT_MSG((statuspriv == PEP_STATUS_OK), "statuspriv == PEP_STATUS_OK");
krista@2395
    44
    statuspub = import_key(session, carol_pub_key.c_str(), carol_pub_key.length(), NULL);
krista@2395
    45
    statuspriv = import_key(session, carol_priv_key.c_str(), carol_priv_key.length(), NULL);
krista@2669
    46
    TEST_ASSERT_MSG((statuspub == PEP_STATUS_OK), "statuspub == PEP_STATUS_OK");
krista@2669
    47
    TEST_ASSERT_MSG((statuspriv == PEP_STATUS_OK), "statuspriv == PEP_STATUS_OK");
krista@2395
    48
krista@2395
    49
    cout << "creating message…\n";
krista@2395
    50
    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
krista@2395
    51
    pEp_identity* carol = new_identity("pep-test-carol@pep-project.org", NULL, "TOFU_pep-test-carol@pep-project.org", "Carol Test");
krista@2395
    52
krista@2395
    53
    PEP_STATUS alice_status = update_identity(session, alice);
krista@2395
    54
    PEP_STATUS carol_status = update_identity(session, carol);
krista@2395
    55
krista@2395
    56
    PEP_STATUS status = update_trust_for_fpr(session, alice->fpr, PEP_ct_pEp);
krista@2395
    57
    status = update_trust_for_fpr(session, carol->fpr, carol_comm_type);
krista@2395
    58
    
krista@2395
    59
    PEP_STATUS mystatus = myself(session, alice);
krista@2669
    60
    TEST_ASSERT_MSG((mystatus == PEP_STATUS_OK), "mystatus == PEP_STATUS_OK");
krista@2395
    61
    alice_status = update_identity(session, alice);
krista@2395
    62
    alice_status = update_identity(session, carol);
krista@2669
    63
    TEST_ASSERT_MSG((alice->comm_type == PEP_ct_pEp), "alice->comm_type == PEP_ct_pEp");
krista@2669
    64
    TEST_ASSERT_MSG((carol->comm_type == carol_comm_type), "carol->comm_type == carol_comm_type");
krista@2395
    65
    
krista@2395
    66
    identity_list* to_list = new_identity_list(carol); // to carol
krista@2395
    67
    message* outgoing_message = new_message(PEP_dir_outgoing);
krista@2669
    68
    TEST_ASSERT_MSG((outgoing_message), "outgoing_message");
krista@2395
    69
    outgoing_message->from = alice;
krista@2395
    70
    outgoing_message->to = to_list;
krista@2395
    71
    outgoing_message->shortmsg = strdup("Greetings, humans!");
krista@2395
    72
    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@2395
    73
    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3136
    74
//    outgoing_message->id = strdup("blahblahyourmama@pep-project.org");
krista@2395
    75
    outgoing_message->references = new_stringlist("one-839274982347239847@pep-project.org");
krista@2395
    76
    stringlist_add(outgoing_message->references, "two-dfddffd839274982347239847@pep-project.org");
krista@2395
    77
    stringlist_add(outgoing_message->references, "three-OMGWTFBBQ.edfddffd839274982347239847@pep-project.org");
krista@2395
    78
    
krista@2395
    79
    cout << "message created.\n";
krista@2395
    80
krista@2395
    81
    char* encoded_text = nullptr;
krista@2395
    82
    status = mime_encode_message(outgoing_message, false, &encoded_text);
krista@2669
    83
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2669
    84
    TEST_ASSERT_MSG((encoded_text), "encoded_text");
krista@2395
    85
krista@2395
    86
    cout << "unencrypted:\n\n";
krista@2395
    87
    cout << encoded_text << "\n";
krista@2395
    88
krista@2395
    89
    free(encoded_text);
krista@2395
    90
krista@2395
    91
    cout << "encrypting message as MIME multipart…\n";
krista@2395
    92
    message* encrypted_msg = nullptr;
krista@2395
    93
    cout << "calling encrypt_message\n";
krista@2395
    94
    status = encrypt_message(session, outgoing_message, NULL, 
krista@2395
    95
        &encrypted_msg, PEP_enc_PGP_MIME, 0);
krista@2670
    96
    cout << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
krista@2669
    97
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2669
    98
    TEST_ASSERT_MSG((encrypted_msg), "encrypted_msg");
krista@2395
    99
    cout << "message encrypted.\n";
krista@2395
   100
    
krista@2395
   101
    encrypted_msg->enc_format = PEP_enc_none;
krista@2395
   102
    status = mime_encode_message(encrypted_msg, false, &encoded_text);
krista@2669
   103
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2669
   104
    TEST_ASSERT_MSG((encoded_text), "encoded_text");
krista@2395
   105
     
krista@2395
   106
    cout << "encrypted:\n\n";
krista@2395
   107
    cout << encoded_text << "\n";
krista@2395
   108
     
krista@2395
   109
    char* decrypted_text;
krista@2395
   110
    
krista@2395
   111
    message* decrypted_msg = nullptr;
krista@2395
   112
    stringlist_t* keylist_used = nullptr;
krista@2395
   113
    
krista@2395
   114
    PEP_rating rating;
krista@3063
   115
    PEP_decrypt_flags_t flags = 0;
krista@2395
   116
     
krista@2395
   117
//    MIME_decrypt_message(session, encoded_text, strlen(encoded_text), &decrypted_text, &keylist_used, &rating, &flags);
krista@2395
   118
    
krista@2395
   119
//    cout << "HEY!" << endl;
krista@2395
   120
//    cout << decrypted_text << endl;
krista@2395
   121
    
krista@2395
   122
    message* decoded_msg = nullptr;
krista@2395
   123
    status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg);
krista@2669
   124
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2395
   125
    const string string3 = encoded_text;
krista@2395
   126
      
krista@2395
   127
    unlink("msg_2.0.asc");
krista@2395
   128
    ofstream outFile3("msg_2.0.asc");
krista@2395
   129
    outFile3.write(string3.c_str(), string3.size());
krista@2395
   130
    outFile3.close();
krista@2395
   131
    
krista@2395
   132
    // message* decrypted_msg = nullptr;
krista@2395
   133
    // stringlist_t* keylist_used = nullptr;
krista@2395
   134
    // 
krista@2395
   135
    // PEP_rating rating;
krista@2395
   136
    // PEP_decrypt_flags_t flags;
krista@2395
   137
    // 
krista@2395
   138
    stringpair_t* autoconsume = new_stringpair("pEp-auto-consume", "yes");
krista@2395
   139
    stringpair_list_add(encrypted_msg->opt_fields, autoconsume);
krista@2615
   140
    flags = 0;
krista@2395
   141
    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
krista@2669
   142
    TEST_ASSERT_MSG((decrypted_msg), "decrypted_msg");
krista@2669
   143
    TEST_ASSERT_MSG((keylist_used), "keylist_used");
krista@2669
   144
    TEST_ASSERT_MSG((rating), "rating");
krista@2669
   145
    //TEST_ASSERT_MSG((status == PEP_STATUS_OK && rating == PEP_rating_reliable), "status == PEP_STATUS_OK && rating == PEP_rating_reliable");
krista@2395
   146
    //PEP_comm_type ct = encrypted_msg->from->comm_type;
krista@2669
   147
    //TEST_ASSERT_MSG((ct == PEP_ct_pEp), "ct == PEP_ct_pEp");
krista@2395
   148
    
krista@2395
   149
    cout << "keys used:\n";
krista@2395
   150
    
krista@2395
   151
    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
krista@2395
   152
    {
krista@2395
   153
       cout << "\t " << kl4->value << endl;
krista@2395
   154
    }
krista@2395
   155
     
krista@2395
   156
    decrypted_msg->enc_format = PEP_enc_none; 
krista@2395
   157
    status = _mime_encode_message_internal(decrypted_msg, false, &encoded_text, false);
krista@2669
   158
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2669
   159
    TEST_ASSERT_MSG((encoded_text), "encoded_text");
krista@2395
   160
    cout << "Decrypted message: " << endl;
krista@2395
   161
    cout << encoded_text << endl;
krista@2395
   162
     
krista@2395
   163
    cout << "freeing messages…\n";
krista@2395
   164
    free_message(encrypted_msg);
krista@2395
   165
    free_message(decrypted_msg);
krista@2395
   166
    free_message(outgoing_message);
krista@2395
   167
    cout << "done.\n";
krista@2395
   168
}