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