test/src/engine_tests/MessageTwoPointOhTests.cc
branchlocal_cpptest
changeset 2661 2b0b15a718bc
parent 2654 be057a8a002e
child 2669 1b103e8a95f7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/src/engine_tests/MessageTwoPointOhTests.cc	Tue May 08 15:43:07 2018 +0200
     1.3 @@ -0,0 +1,168 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <string>
     1.9 +#include <cstring>
    1.10 +#include <iostream>
    1.11 +#include <fstream>
    1.12 +
    1.13 +#include "pEpEngine.h"
    1.14 +#include "platform.h"
    1.15 +#include "mime.h"
    1.16 +#include "message_api.h"
    1.17 +#include "keymanagement.h"
    1.18 +#include "test_util.h"
    1.19 +
    1.20 +#include <cpptest.h>
    1.21 +#include "EngineTestSessionSuite.h"
    1.22 +#include "MessageTwoPointOhTests.h"
    1.23 +
    1.24 +using namespace std;
    1.25 +
    1.26 +MessageTwoPointOhTests::MessageTwoPointOhTests(string suitename, string test_home_dir) :
    1.27 +    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    1.28 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("MessageTwoPointOhTests::check_message_two_point_oh"),
    1.29 +                                                                      static_cast<Func>(&MessageTwoPointOhTests::check_message_two_point_oh)));
    1.30 +}
    1.31 +
    1.32 +void MessageTwoPointOhTests::check_message_two_point_oh() {
    1.33 +
    1.34 +    PEP_comm_type carol_comm_type = PEP_ct_OpenPGP_unconfirmed;
    1.35 +
    1.36 +    // message_api test code
    1.37 +
    1.38 +    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    1.39 +    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    1.40 +    const string carol_pub_key = slurp("test_keys/pub/pep-test-carol-0x42A85A42_pub.asc");
    1.41 +    const string carol_priv_key = slurp("test_keys/priv/pep-test-carol-0x42A85A42_priv.asc");
    1.42 +
    1.43 +    PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
    1.44 +    PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
    1.45 +    TEST_ASSERT(statuspub == PEP_STATUS_OK);
    1.46 +    TEST_ASSERT(statuspriv == PEP_STATUS_OK);
    1.47 +    statuspub = import_key(session, carol_pub_key.c_str(), carol_pub_key.length(), NULL);
    1.48 +    statuspriv = import_key(session, carol_priv_key.c_str(), carol_priv_key.length(), NULL);
    1.49 +    TEST_ASSERT(statuspub == PEP_STATUS_OK);
    1.50 +    TEST_ASSERT(statuspriv == PEP_STATUS_OK);
    1.51 +
    1.52 +    cout << "creating message…\n";
    1.53 +    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
    1.54 +    pEp_identity* carol = new_identity("pep-test-carol@pep-project.org", NULL, "TOFU_pep-test-carol@pep-project.org", "Carol Test");
    1.55 +
    1.56 +    PEP_STATUS alice_status = update_identity(session, alice);
    1.57 +    PEP_STATUS carol_status = update_identity(session, carol);
    1.58 +
    1.59 +    PEP_STATUS status = update_trust_for_fpr(session, alice->fpr, PEP_ct_pEp);
    1.60 +    status = update_trust_for_fpr(session, carol->fpr, carol_comm_type);
    1.61 +    
    1.62 +    PEP_STATUS mystatus = myself(session, alice);
    1.63 +    TEST_ASSERT(mystatus == PEP_STATUS_OK);
    1.64 +    alice_status = update_identity(session, alice);
    1.65 +    alice_status = update_identity(session, carol);
    1.66 +    TEST_ASSERT(alice->comm_type == PEP_ct_pEp);
    1.67 +    TEST_ASSERT(carol->comm_type == carol_comm_type);
    1.68 +    
    1.69 +    identity_list* to_list = new_identity_list(carol); // to carol
    1.70 +    message* outgoing_message = new_message(PEP_dir_outgoing);
    1.71 +    TEST_ASSERT(outgoing_message);
    1.72 +    outgoing_message->from = alice;
    1.73 +    outgoing_message->to = to_list;
    1.74 +    outgoing_message->shortmsg = strdup("Greetings, humans!");
    1.75 +    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
    1.76 +    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    1.77 +//    outgoing_message->id = strdup("blahblahyourmama@pep-project.org");
    1.78 +    outgoing_message->references = new_stringlist("one-839274982347239847@pep-project.org");
    1.79 +    stringlist_add(outgoing_message->references, "two-dfddffd839274982347239847@pep-project.org");
    1.80 +    stringlist_add(outgoing_message->references, "three-OMGWTFBBQ.edfddffd839274982347239847@pep-project.org");
    1.81 +    
    1.82 +    cout << "message created.\n";
    1.83 +
    1.84 +    char* encoded_text = nullptr;
    1.85 +    status = mime_encode_message(outgoing_message, false, &encoded_text);
    1.86 +    TEST_ASSERT(status == PEP_STATUS_OK);
    1.87 +    TEST_ASSERT(encoded_text);
    1.88 +
    1.89 +    cout << "unencrypted:\n\n";
    1.90 +    cout << encoded_text << "\n";
    1.91 +
    1.92 +    free(encoded_text);
    1.93 +
    1.94 +    cout << "encrypting message as MIME multipart…\n";
    1.95 +    message* encrypted_msg = nullptr;
    1.96 +    cout << "calling encrypt_message\n";
    1.97 +    status = encrypt_message(session, outgoing_message, NULL, 
    1.98 +        &encrypted_msg, PEP_enc_PGP_MIME, 0);
    1.99 +    cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
   1.100 +    TEST_ASSERT(status == PEP_STATUS_OK);
   1.101 +    TEST_ASSERT(encrypted_msg);
   1.102 +    cout << "message encrypted.\n";
   1.103 +    
   1.104 +    encrypted_msg->enc_format = PEP_enc_none;
   1.105 +    status = mime_encode_message(encrypted_msg, false, &encoded_text);
   1.106 +    TEST_ASSERT(status == PEP_STATUS_OK);
   1.107 +    TEST_ASSERT(encoded_text);
   1.108 +     
   1.109 +    cout << "encrypted:\n\n";
   1.110 +    cout << encoded_text << "\n";
   1.111 +     
   1.112 +    char* decrypted_text;
   1.113 +    
   1.114 +    message* decrypted_msg = nullptr;
   1.115 +    stringlist_t* keylist_used = nullptr;
   1.116 +    
   1.117 +    PEP_rating rating;
   1.118 +    PEP_decrypt_flags_t flags;
   1.119 +     
   1.120 +//    MIME_decrypt_message(session, encoded_text, strlen(encoded_text), &decrypted_text, &keylist_used, &rating, &flags);
   1.121 +    
   1.122 +//    cout << "HEY!" << endl;
   1.123 +//    cout << decrypted_text << endl;
   1.124 +    
   1.125 +    message* decoded_msg = nullptr;
   1.126 +    status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg);
   1.127 +    TEST_ASSERT(status == PEP_STATUS_OK);
   1.128 +    const string string3 = encoded_text;
   1.129 +      
   1.130 +    unlink("msg_2.0.asc");
   1.131 +    ofstream outFile3("msg_2.0.asc");
   1.132 +    outFile3.write(string3.c_str(), string3.size());
   1.133 +    outFile3.close();
   1.134 +    
   1.135 +    // message* decrypted_msg = nullptr;
   1.136 +    // stringlist_t* keylist_used = nullptr;
   1.137 +    // 
   1.138 +    // PEP_rating rating;
   1.139 +    // PEP_decrypt_flags_t flags;
   1.140 +    // 
   1.141 +    stringpair_t* autoconsume = new_stringpair("pEp-auto-consume", "yes");
   1.142 +    stringpair_list_add(encrypted_msg->opt_fields, autoconsume);
   1.143 +    flags = 0;
   1.144 +    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   1.145 +    TEST_ASSERT(decrypted_msg);
   1.146 +    TEST_ASSERT(keylist_used);
   1.147 +    TEST_ASSERT(rating);
   1.148 +    //TEST_ASSERT(status == PEP_STATUS_OK && rating == PEP_rating_reliable);
   1.149 +    //PEP_comm_type ct = encrypted_msg->from->comm_type;
   1.150 +    //TEST_ASSERT(ct == PEP_ct_pEp);
   1.151 +    
   1.152 +    cout << "keys used:\n";
   1.153 +    
   1.154 +    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
   1.155 +    {
   1.156 +       cout << "\t " << kl4->value << endl;
   1.157 +    }
   1.158 +     
   1.159 +    decrypted_msg->enc_format = PEP_enc_none; 
   1.160 +    status = _mime_encode_message_internal(decrypted_msg, false, &encoded_text, false);
   1.161 +    TEST_ASSERT(status == PEP_STATUS_OK);
   1.162 +    TEST_ASSERT(encoded_text);
   1.163 +    cout << "Decrypted message: " << endl;
   1.164 +    cout << encoded_text << endl;
   1.165 +     
   1.166 +    cout << "freeing messages…\n";
   1.167 +    free_message(encrypted_msg);
   1.168 +    free_message(decrypted_msg);
   1.169 +    free_message(outgoing_message);
   1.170 +    cout << "done.\n";
   1.171 +}