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