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