test/decorate_test.cc
author Krista Grothoff <krista@pep-project.org>
Tue, 15 Nov 2016 23:40:28 +0100
changeset 1400 909966aa9d93
child 1513 e7f7e42385b5
permissions -rw-r--r--
Added test to ensure encrypted mails go out with X-pEp-Version from the engine
     1 #include <stdlib.h>
     2 #include <string.h>
     3 #include "platform.h"
     4 #include <iostream>
     5 #include <fstream>
     6 #include <assert.h>
     7 #include <sstream>
     8 #include "mime.h"
     9 #include "message_api.h"
    10 
    11 using namespace std;
    12 
    13 int main() {
    14     cout << "\n*** check that X-pEp-Version is added to decorated text ***\n\n";
    15 
    16     PEP_SESSION session;
    17     
    18     cout << "calling init()\n";
    19     PEP_STATUS status1 = init(&session);
    20     assert(status1 == PEP_STATUS_OK);
    21     assert(session);
    22     cout << "init() completed.\n";
    23 
    24     // message_api test code
    25 
    26     cout << "creating message…\n";
    27     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    28     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
    29     alice->me = true;
    30     identity_list* to_list = new_identity_list(bob); // to bob
    31     message* outgoing_message = new_message(PEP_dir_outgoing);
    32     assert(outgoing_message);
    33     outgoing_message->from = alice;
    34     outgoing_message->to = to_list;
    35     outgoing_message->shortmsg = strdup("Greetings, humans!");
    36     outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    37     outgoing_message->longmsg = strdup("This is a dumb message.\nBut it's done.\n");
    38     assert(outgoing_message->longmsg);
    39     cout << "message created.\n";
    40 
    41     char* encoded_text = nullptr;
    42 
    43     message* encrypted_msg = nullptr;
    44     cout << "calling encrypt_message\n";
    45     PEP_STATUS status = encrypt_message (session, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, 0);
    46     cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
    47     assert(status == PEP_STATUS_OK);
    48     assert(encrypted_msg);
    49     cout << "message encrypted.\n";
    50     
    51     status = mime_encode_message(encrypted_msg, false, &encoded_text);
    52     assert(status == PEP_STATUS_OK);
    53     assert(encoded_text);
    54     
    55     bool contains_version = false;
    56     
    57     const char* version_str = "X-pEp-Version: ";
    58     size_t version_prefix_len = strlen(version_str);
    59     
    60     istringstream f(encoded_text);
    61     string enc_string;
    62     while (getline(f, enc_string)) {
    63         if (strncmp(enc_string.c_str(), version_str, version_prefix_len) == 0)
    64             contains_version = true;
    65     }
    66     assert(contains_version);
    67     
    68     if (contains_version)
    69         cout << "Version string in encrypted message, as it should be." << endl;
    70     
    71     cout << "freeing messages…\n";
    72     free_message(encrypted_msg);
    73     free_message(outgoing_message);
    74     cout << "done.\n";
    75 
    76     cout << "calling release()\n";
    77     release(session);
    78     return 0;
    79 }