test/encrypt_for_identity_test.cc
author Edouard Tisserant <edouard@pep-project.org>
Mon, 24 Oct 2016 20:44:48 +0200
changeset 1329 876ad7615d75
parent 1057 abff13590a4c
child 1513 e7f7e42385b5
permissions -rw-r--r--
sync : fix encrypted sync messages not being injected when decrypted from non-sync session
krista@995
     1
#include <stdlib.h>
krista@995
     2
#include <string.h>
krista@995
     3
#include "platform.h"
krista@995
     4
#include <iostream>
krista@995
     5
#include <fstream>
krista@995
     6
#include <assert.h>
krista@995
     7
#include "mime.h"
krista@995
     8
#include "message_api.h"
krista@995
     9
krista@995
    10
using namespace std;
krista@995
    11
krista@995
    12
int main() {
krista@995
    13
    cout << "\n*** encrypt_for_identity_test ***\n\n";
krista@995
    14
krista@995
    15
    PEP_SESSION session;
krista@995
    16
    
krista@995
    17
    cout << "calling init()\n";
krista@995
    18
    PEP_STATUS status1 = init(&session);
krista@995
    19
    assert(status1 == PEP_STATUS_OK);
krista@995
    20
    assert(session);
krista@995
    21
    cout << "init() completed.\n";
krista@995
    22
krista@995
    23
    // message_api test code
krista@995
    24
krista@995
    25
    cout << "creating message…\n";
krista@995
    26
    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
krista@995
    27
    pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
krista@995
    28
    alice->me = true;
krista@995
    29
    identity_list* to_list = new_identity_list(bob); // to bob
krista@995
    30
    message* outgoing_message = new_message(PEP_dir_outgoing);
krista@995
    31
    assert(outgoing_message);
krista@995
    32
    outgoing_message->from = alice;
krista@995
    33
    outgoing_message->to = to_list;
krista@995
    34
    outgoing_message->shortmsg = strdup("Greetings, humans!");
krista@995
    35
    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@995
    36
    cout << "message created.\n";
krista@995
    37
krista@995
    38
    char* encoded_text = nullptr;
krista@995
    39
    PEP_STATUS status = mime_encode_message(outgoing_message, false, &encoded_text);
krista@995
    40
    assert(status == PEP_STATUS_OK);
krista@995
    41
    assert(encoded_text);
krista@995
    42
krista@995
    43
    cout << "decrypted:\n\n";
krista@995
    44
    cout << encoded_text << "\n";
krista@995
    45
krista@995
    46
    free(encoded_text);
krista@995
    47
krista@995
    48
    cout << "encrypting message as MIME multipart…\n";
krista@995
    49
    message* encrypted_msg = nullptr;
krista@995
    50
    cout << "calling encrypt_message_for_identity()\n";
krista@995
    51
    status = encrypt_message_for_self(session, alice, outgoing_message, &encrypted_msg, PEP_enc_PGP_MIME);
krista@995
    52
    cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
krista@995
    53
    assert(status == PEP_STATUS_OK);
krista@995
    54
    assert(encrypted_msg);
krista@995
    55
    cout << "message encrypted.\n";
krista@995
    56
    
krista@995
    57
    status = mime_encode_message(encrypted_msg, false, &encoded_text);
krista@995
    58
    assert(status == PEP_STATUS_OK);
krista@995
    59
    assert(encoded_text);
krista@995
    60
krista@995
    61
    cout << "encrypted:\n\n";
krista@995
    62
    cout << encoded_text << "\n";
krista@995
    63
krista@995
    64
    message* decoded_msg = nullptr;
krista@995
    65
    status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg);
krista@995
    66
    assert(status == PEP_STATUS_OK);
krista@995
    67
    const string string3 = encoded_text;
krista@995
    68
krista@995
    69
    unlink("msg_encrypt_for_self.asc");
krista@995
    70
    ofstream outFile3("msg_encrypt_for_self.asc");
krista@995
    71
    outFile3.write(string3.c_str(), string3.size());
krista@995
    72
    outFile3.close();
krista@995
    73
krista@995
    74
    message* decrypted_msg = nullptr;
krista@995
    75
    stringlist_t* keylist_used = nullptr;
krista@995
    76
vb@1006
    77
    PEP_rating rating;
krista@995
    78
    PEP_decrypt_flags_t flags;
krista@995
    79
vb@1006
    80
    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
krista@995
    81
    assert(status == PEP_STATUS_OK);
krista@995
    82
    assert(decrypted_msg);
krista@995
    83
    assert(keylist_used);
vb@1006
    84
    assert(rating);
krista@995
    85
    PEP_comm_type ct = encrypted_msg->from->comm_type;
krista@995
    86
    assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
krista@995
    87
krista@995
    88
    cout << "keys used:\n";
krista@995
    89
krista@995
    90
    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
krista@995
    91
    {
krista@995
    92
        cout << "\t " << kl4->value << endl;
krista@1049
    93
        assert(strcasecmp("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value) == 0);
krista@995
    94
        cout << "Encrypted for Alice! Yay! It worked!" << endl;
krista@995
    95
    }
krista@995
    96
    cout << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
krista@995
    97
 
krista@995
    98
    cout << "freeing messages…\n";
krista@995
    99
    free_message(encrypted_msg);
krista@995
   100
    free_message(decrypted_msg);
krista@995
   101
    free_message(outgoing_message);
krista@995
   102
    cout << "done.\n";
krista@995
   103
krista@995
   104
    cout << "calling release()\n";
krista@995
   105
    release(session);
krista@995
   106
    return 0;
krista@995
   107
}