test/decorate_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 19 Jan 2018 14:12:02 +0100
changeset 2418 05678e77bc6a
parent 2370 b37530bf26fa
child 2462 48b526a0daac
permissions -rw-r--r--
fixed update_identity call that could be called on a myself value in outgoing_message_rating
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
krista@1400
     4
#include <stdlib.h>
krista@1400
     5
#include <string.h>
krista@1400
     6
#include "platform.h"
krista@1400
     7
#include <iostream>
krista@1400
     8
#include <fstream>
krista@1400
     9
#include <assert.h>
krista@1400
    10
#include <sstream>
krista@1400
    11
#include "mime.h"
krista@1400
    12
#include "message_api.h"
krista@2370
    13
#include "test_util.h"
krista@1400
    14
krista@1400
    15
using namespace std;
krista@1400
    16
krista@1400
    17
int main() {
krista@1400
    18
    cout << "\n*** check that X-pEp-Version is added to decorated text ***\n\n";
krista@1400
    19
krista@1400
    20
    PEP_SESSION session;
krista@1400
    21
    
krista@1400
    22
    cout << "calling init()\n";
krista@1400
    23
    PEP_STATUS status1 = init(&session);
krista@1400
    24
    assert(status1 == PEP_STATUS_OK);
krista@1400
    25
    assert(session);
krista@1400
    26
    cout << "init() completed.\n";
krista@1400
    27
krista@2370
    28
    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
krista@2370
    29
    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
krista@2370
    30
    const string bob_pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
krista@2370
    31
    PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
krista@2370
    32
    PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
krista@2370
    33
    PEP_STATUS statusbob = import_key(session, bob_pub_key.c_str(), bob_pub_key.length(), NULL);
krista@2370
    34
    assert(statuspub == PEP_STATUS_OK);
krista@2370
    35
    assert(statuspriv == PEP_STATUS_OK);
krista@2370
    36
    assert(statusbob == PEP_STATUS_OK);
krista@1400
    37
krista@1400
    38
    cout << "creating message…\n";
krista@1400
    39
    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
krista@1400
    40
    pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
krista@2288
    41
    alice->me = true;
krista@1400
    42
    identity_list* to_list = new_identity_list(bob); // to bob
krista@1400
    43
    message* outgoing_message = new_message(PEP_dir_outgoing);
krista@1400
    44
    assert(outgoing_message);
krista@1400
    45
    outgoing_message->from = alice;
krista@1400
    46
    outgoing_message->to = to_list;
krista@1400
    47
    outgoing_message->shortmsg = strdup("Greetings, humans!");
krista@1871
    48
    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@1400
    49
    outgoing_message->longmsg = strdup("This is a dumb message.\nBut it's done.\n");
krista@1400
    50
    assert(outgoing_message->longmsg);
krista@1400
    51
    cout << "message created.\n";
krista@1400
    52
krista@1400
    53
    char* encoded_text = nullptr;
krista@1400
    54
krista@1400
    55
    message* encrypted_msg = nullptr;
krista@1400
    56
    cout << "calling encrypt_message\n";
krista@1400
    57
    PEP_STATUS status = encrypt_message (session, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, 0);
krista@2370
    58
    cout << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
krista@1400
    59
    assert(status == PEP_STATUS_OK);
krista@1400
    60
    assert(encrypted_msg);
krista@1400
    61
    cout << "message encrypted.\n";
krista@1400
    62
    
krista@1400
    63
    status = mime_encode_message(encrypted_msg, false, &encoded_text);
krista@1400
    64
    assert(status == PEP_STATUS_OK);
krista@1400
    65
    assert(encoded_text);
krista@1400
    66
    
krista@1400
    67
    bool contains_version = false;
krista@1400
    68
    
krista@1400
    69
    const char* version_str = "X-pEp-Version: ";
krista@1400
    70
    size_t version_prefix_len = strlen(version_str);
krista@1400
    71
    
krista@1400
    72
    istringstream f(encoded_text);
krista@1400
    73
    string enc_string;
krista@1400
    74
    while (getline(f, enc_string)) {
krista@1400
    75
        if (strncmp(enc_string.c_str(), version_str, version_prefix_len) == 0)
krista@1400
    76
            contains_version = true;
krista@1400
    77
    }
krista@1400
    78
    assert(contains_version);
krista@1400
    79
    
krista@1400
    80
    if (contains_version)
krista@1400
    81
        cout << "Version string in encrypted message, as it should be." << endl;
krista@1400
    82
    
krista@1400
    83
    cout << "freeing messages…\n";
krista@1400
    84
    free_message(encrypted_msg);
krista@1400
    85
    free_message(outgoing_message);
krista@1400
    86
    cout << "done.\n";
krista@1400
    87
krista@1400
    88
    cout << "calling release()\n";
krista@1400
    89
    release(session);
krista@1400
    90
    return 0;
krista@1400
    91
}