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