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