test/src/engine_tests/DecorateTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Fri, 15 Mar 2019 12:43:56 +0100
branchsync
changeset 3335 09af2cf0ac74
parent 3292 673b8c6da681
permissions -rw-r--r--
Reverted out device model
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "TestConstants.h"
     5 #include <cpptest.h>
     6 #include <cpptest-suite.h>
     7 #include <cpptest-textoutput.h>
     8 #include <stdlib.h>
     9 #include <string.h>
    10 #include "platform.h"
    11 #include <iostream>
    12 #include <fstream>
    13 #include <assert.h>
    14 #include <sstream>
    15 #include "mime.h"
    16 #include "message_api.h"
    17 #include "test_util.h"
    18 
    19 #include "EngineTestSuite.h"
    20 #include "EngineTestSessionSuite.h"
    21 #include "DecorateTests.h"
    22 
    23 using namespace std;
    24 
    25 DecorateTests::DecorateTests(string suitename, string test_home_dir) :
    26     EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    27     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DecorateTests::check_decorate"),
    28                                                                       static_cast<Func>(&DecorateTests::check_decorate)));
    29 }
    30 
    31 void DecorateTests::check_decorate() {
    32 
    33     const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    34     const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    35     const string bob_pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    36     PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
    37     PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
    38     PEP_STATUS statusbob = import_key(session, bob_pub_key.c_str(), bob_pub_key.length(), NULL);
    39     TEST_ASSERT_MSG((statuspub == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspub == PEP_STATUS_OK");
    40     TEST_ASSERT_MSG((statuspriv == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspriv == PEP_STATUS_OK");
    41     TEST_ASSERT_MSG((statusbob == PEP_TEST_KEY_IMPORT_SUCCESS), "statusbob == PEP_STATUS_OK");
    42 
    43     cout << "creating message…\n";
    44     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    45     pEp_identity* alice_dup = identity_dup(alice);
    46     PEP_STATUS status = set_own_key(session, alice_dup, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
    47     TEST_ASSERT(status == PEP_STATUS_OK);
    48     free_identity(alice_dup);
    49 
    50     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
    51     alice->me = true;
    52     identity_list* to_list = new_identity_list(bob); // to bob
    53     message* outgoing_message = new_message(PEP_dir_outgoing);
    54     TEST_ASSERT_MSG((outgoing_message), "outgoing_message");
    55     outgoing_message->from = alice;
    56     outgoing_message->to = to_list;
    57     outgoing_message->shortmsg = strdup("Greetings, humans!");
    58     outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    59     outgoing_message->longmsg = strdup("This is a dumb message.\nBut it's done.\n");
    60     TEST_ASSERT_MSG((outgoing_message->longmsg), "outgoing_message->longmsg");
    61     cout << "message created.\n";
    62 
    63     char* encoded_text = nullptr;
    64 
    65     message* encrypted_msg = nullptr;
    66     cout << "calling encrypt_message\n";
    67     status = encrypt_message (session, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, 0);
    68     cout << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
    69     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    70     TEST_ASSERT_MSG((encrypted_msg), "encrypted_msg");
    71     cout << "message encrypted.\n";
    72 
    73     status = mime_encode_message(encrypted_msg, false, &encoded_text);
    74     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    75     TEST_ASSERT_MSG((encoded_text), "encoded_text");
    76 
    77     bool contains_version = false;
    78 
    79     const char* version_str = "X-pEp-Version: ";
    80     size_t version_prefix_len = strlen(version_str);
    81 
    82     istringstream f(encoded_text);
    83     string enc_string;
    84     while (getline(f, enc_string)) {
    85         if (strncmp(enc_string.c_str(), version_str, version_prefix_len) == 0)
    86             contains_version = true;
    87     }
    88     TEST_ASSERT_MSG((contains_version), "contains_version");
    89 
    90     if (contains_version)
    91         cout << "Version string in encrypted message, as it should be." << endl;
    92 }