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