test/encrypt_for_identity_test.cc
author Krista Bennett <krista@pep-project.org>
Wed, 14 Jun 2017 18:52:55 +0200
branchENGINE-215
changeset 1856 99348d336639
parent 1848 7400c532bb36
child 1849 8b7bed853a1f
child 1858 600dc0395ec0
permissions -rw-r--r--
ENGINE-215: needs to be tested, test cases impacted by the change need to be adjusted
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
krista@995
     4
#include <stdlib.h>
krista@995
     5
#include <string.h>
krista@995
     6
#include "platform.h"
krista@995
     7
#include <iostream>
krista@995
     8
#include <fstream>
krista@995
     9
#include <assert.h>
krista@995
    10
#include "mime.h"
krista@995
    11
#include "message_api.h"
edouard@1848
    12
#include "keymanagement.h"
roker@1660
    13
#include "test_util.h"
krista@995
    14
krista@995
    15
using namespace std;
krista@995
    16
krista@995
    17
int main() {
krista@995
    18
    cout << "\n*** encrypt_for_identity_test ***\n\n";
krista@995
    19
krista@995
    20
    PEP_SESSION session;
krista@995
    21
    
krista@995
    22
    cout << "calling init()\n";
krista@995
    23
    PEP_STATUS status1 = init(&session);
krista@995
    24
    assert(status1 == PEP_STATUS_OK);
krista@995
    25
    assert(session);
krista@995
    26
    cout << "init() completed.\n";
krista@995
    27
krista@995
    28
    // message_api test code
krista@995
    29
krista@1702
    30
    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
krista@1702
    31
    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
krista@1702
    32
krista@1702
    33
    PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
krista@1702
    34
    PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
krista@1702
    35
    assert(statuspub == PEP_STATUS_OK);
krista@1702
    36
    assert(statuspriv == PEP_STATUS_OK);
krista@1702
    37
krista@995
    38
    cout << "creating message…\n";
edouard@1848
    39
    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
krista@995
    40
    pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
krista@995
    41
    alice->me = true;
edouard@1848
    42
edouard@1848
    43
    PEP_STATUS mystatus = myself(session, alice);
edouard@1848
    44
    assert(mystatus == PEP_STATUS_OK);
edouard@1848
    45
krista@995
    46
    identity_list* to_list = new_identity_list(bob); // to bob
krista@995
    47
    message* outgoing_message = new_message(PEP_dir_outgoing);
krista@995
    48
    assert(outgoing_message);
krista@995
    49
    outgoing_message->from = alice;
krista@995
    50
    outgoing_message->to = to_list;
krista@995
    51
    outgoing_message->shortmsg = strdup("Greetings, humans!");
krista@1644
    52
    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@995
    53
    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@995
    54
    cout << "message created.\n";
krista@995
    55
krista@995
    56
    char* encoded_text = nullptr;
krista@995
    57
    PEP_STATUS status = mime_encode_message(outgoing_message, false, &encoded_text);
krista@995
    58
    assert(status == PEP_STATUS_OK);
krista@995
    59
    assert(encoded_text);
krista@995
    60
krista@995
    61
    cout << "decrypted:\n\n";
krista@995
    62
    cout << encoded_text << "\n";
krista@995
    63
krista@995
    64
    free(encoded_text);
krista@995
    65
krista@995
    66
    cout << "encrypting message as MIME multipart…\n";
krista@995
    67
    message* encrypted_msg = nullptr;
krista@995
    68
    cout << "calling encrypt_message_for_identity()\n";
krista@1640
    69
    status = encrypt_message_for_self(session, alice, outgoing_message, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
krista@995
    70
    cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
krista@995
    71
    assert(status == PEP_STATUS_OK);
krista@995
    72
    assert(encrypted_msg);
krista@995
    73
    cout << "message encrypted.\n";
krista@995
    74
    
krista@995
    75
    status = mime_encode_message(encrypted_msg, false, &encoded_text);
krista@995
    76
    assert(status == PEP_STATUS_OK);
krista@995
    77
    assert(encoded_text);
krista@995
    78
krista@995
    79
    cout << "encrypted:\n\n";
krista@995
    80
    cout << encoded_text << "\n";
krista@995
    81
krista@995
    82
    message* decoded_msg = nullptr;
krista@995
    83
    status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg);
krista@995
    84
    assert(status == PEP_STATUS_OK);
krista@995
    85
    const string string3 = encoded_text;
krista@995
    86
krista@995
    87
    unlink("msg_encrypt_for_self.asc");
krista@995
    88
    ofstream outFile3("msg_encrypt_for_self.asc");
krista@995
    89
    outFile3.write(string3.c_str(), string3.size());
krista@995
    90
    outFile3.close();
krista@995
    91
krista@995
    92
    message* decrypted_msg = nullptr;
krista@995
    93
    stringlist_t* keylist_used = nullptr;
krista@995
    94
vb@1006
    95
    PEP_rating rating;
krista@995
    96
    PEP_decrypt_flags_t flags;
krista@995
    97
vb@1006
    98
    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
krista@995
    99
    assert(decrypted_msg);
krista@995
   100
    assert(keylist_used);
vb@1006
   101
    assert(rating);
edouard@1832
   102
    assert(status == PEP_STATUS_OK && rating == PEP_rating_unreliable);
krista@995
   103
    PEP_comm_type ct = encrypted_msg->from->comm_type;
krista@995
   104
    assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
krista@995
   105
krista@995
   106
    cout << "keys used:\n";
krista@995
   107
krista@1640
   108
    int i = 0;
krista@1640
   109
krista@1640
   110
    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
krista@995
   111
    {
krista@1640
   112
        if (i == 0)
krista@1640
   113
            assert(strcasecmp("",kl4->value) == 0);
krista@1640
   114
        else {
krista@1640
   115
            cout << "\t " << kl4->value << endl;
krista@1640
   116
            assert(strcasecmp("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value) == 0);
krista@1640
   117
            cout << "Encrypted for Alice! Yay! It worked!" << endl;
krista@1640
   118
        }
krista@1640
   119
        assert(i < 2);
krista@995
   120
    }
krista@995
   121
    cout << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
krista@995
   122
 
krista@995
   123
    cout << "freeing messages…\n";
krista@995
   124
    free_message(encrypted_msg);
krista@995
   125
    free_message(decrypted_msg);
krista@995
   126
    free_message(outgoing_message);
krista@995
   127
    cout << "done.\n";
krista@995
   128
krista@1644
   129
    cout << "*** Now testing MIME_encrypt_for_self ***" << endl;
krista@1644
   130
krista@1644
   131
    alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
krista@1644
   132
    bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
krista@1644
   133
krista@1644
   134
    cout << "Reading in alice_bob_encrypt_test_plaintext_mime.eml..." << endl;
krista@1644
   135
    
roker@1660
   136
    const string mimetext = slurp("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
krista@1644
   137
krista@1644
   138
    cout << "Text read:" << endl;
krista@1644
   139
    cout << mimetext.c_str() << endl;
krista@1644
   140
    char* encrypted_mimetext = nullptr;
krista@1644
   141
    
krista@1644
   142
    cout << "Calling MIME_encrypt_message_for_self" << endl;
krista@1644
   143
    status = MIME_encrypt_message_for_self(session, alice, mimetext.c_str(),
krista@1644
   144
                                           mimetext.size(), 
krista@1644
   145
                                           &encrypted_mimetext, 
krista@1644
   146
                                           PEP_enc_PGP_MIME, 
krista@1644
   147
                                           PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
krista@1644
   148
    
krista@1644
   149
    cout << "Encrypted message:" << endl;
krista@1644
   150
    cout << encrypted_mimetext << endl;
krista@1644
   151
krista@1644
   152
    cout << "Calling MIME_decrypt_message" << endl;
krista@1644
   153
    
krista@1644
   154
    char* decrypted_mimetext = nullptr;
krista@1644
   155
    free_stringlist(keylist_used);
krista@1644
   156
    keylist_used = nullptr;
krista@1644
   157
    PEP_decrypt_flags_t mimeflags;
krista@1644
   158
    PEP_rating mimerating;
krista@1644
   159
krista@1644
   160
    status = MIME_decrypt_message(session,
krista@1644
   161
                                  encrypted_mimetext,
krista@1644
   162
                                  strlen(encrypted_mimetext),
krista@1644
   163
                                  &decrypted_mimetext,
krista@1644
   164
                                  &keylist_used,
krista@1644
   165
                                  &mimerating,
krista@1644
   166
                                  &mimeflags);
krista@1644
   167
krista@1644
   168
    assert(decrypted_msg);
krista@1644
   169
    assert(keylist_used);
krista@1644
   170
    assert(mimerating);
krista@1644
   171
                             
edouard@1832
   172
    assert(status == PEP_STATUS_OK && mimerating == PEP_rating_unreliable);
krista@1644
   173
krista@1644
   174
    cout << "Decrypted message:" << endl;
krista@1644
   175
    cout << decrypted_mimetext << endl;
krista@1644
   176
krista@1644
   177
    cout << "keys used:\n";
krista@1644
   178
krista@1644
   179
    i = 0;
krista@1644
   180
krista@1644
   181
    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
krista@1644
   182
    {
krista@1644
   183
        if (i == 0)
krista@1644
   184
            assert(strcasecmp("",kl4->value) == 0);
krista@1644
   185
        else {
krista@1644
   186
            cout << "\t " << kl4->value << endl;
krista@1644
   187
            assert(strcasecmp("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value) == 0);
krista@1644
   188
            cout << "Encrypted for Alice! Yay! It worked!" << endl;
krista@1644
   189
        }
krista@1644
   190
        assert(i < 2);
krista@1644
   191
    }
krista@1644
   192
    cout << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
krista@1644
   193
    
krista@995
   194
    cout << "calling release()\n";
krista@995
   195
    release(session);
krista@995
   196
    return 0;
krista@995
   197
}