test/encrypt_for_identity_test.cc
author Roker <roker@pep-project.org>
Tue, 20 Sep 2016 15:54:57 +0200
branchroker-linux
changeset 1168 4d4e3ecbaf8b
parent 1049 d35c550050e0
child 1057 abff13590a4c
permissions -rw-r--r--
merge "default" into my branch
     1 #include <stdlib.h>
     2 #include <string.h>
     3 #include "platform.h"
     4 #include <iostream>
     5 #include <fstream>
     6 #include <assert.h>
     7 #include "mime.h"
     8 #include "message_api.h"
     9 
    10 using namespace std;
    11 
    12 int main() {
    13     cout << "\n*** encrypt_for_identity_test ***\n\n";
    14 
    15     PEP_SESSION session;
    16     
    17     cout << "calling init()\n";
    18     PEP_STATUS status1 = init(&session);
    19     assert(status1 == PEP_STATUS_OK);
    20     assert(session);
    21     cout << "init() completed.\n";
    22 
    23     // message_api test code
    24 
    25     cout << "creating message…\n";
    26     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    27     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
    28     alice->me = true;
    29     identity_list* to_list = new_identity_list(bob); // to bob
    30     message* outgoing_message = new_message(PEP_dir_outgoing);
    31     assert(outgoing_message);
    32     outgoing_message->from = alice;
    33     outgoing_message->to = to_list;
    34     outgoing_message->shortmsg = strdup("Greetings, humans!");
    35     outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    36     cout << "message created.\n";
    37 
    38     char* encoded_text = nullptr;
    39     PEP_STATUS status = mime_encode_message(outgoing_message, false, &encoded_text);
    40     assert(status == PEP_STATUS_OK);
    41     assert(encoded_text);
    42 
    43     cout << "decrypted:\n\n";
    44     cout << encoded_text << "\n";
    45 
    46     free(encoded_text);
    47 
    48     cout << "encrypting message as MIME multipart…\n";
    49     message* encrypted_msg = nullptr;
    50     cout << "calling encrypt_message_for_identity()\n";
    51     status = encrypt_message_for_self(session, alice, outgoing_message, &encrypted_msg, PEP_enc_PGP_MIME);
    52     cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
    53     assert(status == PEP_STATUS_OK);
    54     assert(encrypted_msg);
    55     cout << "message encrypted.\n";
    56     
    57     status = mime_encode_message(encrypted_msg, false, &encoded_text);
    58     assert(status == PEP_STATUS_OK);
    59     assert(encoded_text);
    60 
    61     cout << "encrypted:\n\n";
    62     cout << encoded_text << "\n";
    63 
    64     message* decoded_msg = nullptr;
    65     status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg);
    66     assert(status == PEP_STATUS_OK);
    67     const string string3 = encoded_text;
    68 
    69     unlink("msg_encrypt_for_self.asc");
    70     ofstream outFile3("msg_encrypt_for_self.asc");
    71     outFile3.write(string3.c_str(), string3.size());
    72     outFile3.close();
    73 
    74     message* decrypted_msg = nullptr;
    75     stringlist_t* keylist_used = nullptr;
    76 
    77     PEP_color color;
    78     PEP_decrypt_flags_t flags;
    79 
    80     status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &color, &flags);
    81     assert(status == PEP_STATUS_OK);
    82     assert(decrypted_msg);
    83     assert(keylist_used);
    84     assert(color);
    85     PEP_comm_type ct = encrypted_msg->from->comm_type;
    86     assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
    87 
    88     cout << "keys used:\n";
    89 
    90     for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
    91     {
    92         cout << "\t " << kl4->value << endl;
    93         assert(strcasecmp("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value) == 0);
    94         cout << "Encrypted for Alice! Yay! It worked!" << endl;
    95     }
    96     cout << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
    97  
    98     cout << "freeing messages…\n";
    99     free_message(encrypted_msg);
   100     free_message(decrypted_msg);
   101     free_message(outgoing_message);
   102     cout << "done.\n";
   103 
   104     cout << "calling release()\n";
   105     release(session);
   106     return 0;
   107 }