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