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