test/least_color_group_test.cc
author Krista Grothoff <krista@pep-project.org>
Fri, 03 Mar 2017 14:24:44 +0100
branchENGINE-174
changeset 1618 0fefbfbae9e1
child 1654 60778ef1e780
permissions -rw-r--r--
ENGINE-174: added tests/keys based on Patrick Brunschwig\'s bug description.
krista@1618
     1
#include <iostream>
krista@1618
     2
#include <iostream>
krista@1618
     3
#include <fstream>
krista@1618
     4
#include <string>
krista@1618
     5
#include <cstring> // for strcmp()
krista@1618
     6
#include <assert.h>
krista@1618
     7
#include "blacklist.h"
krista@1618
     8
#include "keymanagement.h"
krista@1618
     9
#include "message_api.h"
krista@1618
    10
#include "mime.h"
krista@1618
    11
krista@1618
    12
using namespace std;
krista@1618
    13
krista@1618
    14
int main(int argc, char** argv) {
krista@1618
    15
    cout << "\n*** least_color_group_test.cc ***\n\n";
krista@1618
    16
    
krista@1618
    17
    const char* mailfile = "test_mails/color_test.eml";
krista@1618
    18
    
krista@1618
    19
    const char* keynames[] = {"test_keys/priv/pep.color.test.P-0x3EBE215C_priv.asc",
krista@1618
    20
                              "test_keys/pub/pep.color.test.H-0xD17E598E_pub.asc",
krista@1618
    21
                              "test_keys/pub/pep.color.test.L-0xE9CDB4CE_pub.asc",
krista@1618
    22
                              "test_keys/pub/pep.color.test.P-0x3EBE215C_pub.asc",
krista@1618
    23
                              "test_keys/pub/pep.color.test.V-0x71FC6D28_pub.asc"
krista@1618
    24
                          };
krista@1618
    25
    
krista@1618
    26
    const int num_keyfiles = 5;
krista@1618
    27
        
krista@1618
    28
    PEP_SESSION session;
krista@1618
    29
    
krista@1618
    30
    cout << "calling init()\n";
krista@1618
    31
    PEP_STATUS status1 = init(&session);   
krista@1618
    32
    assert(status1 == PEP_STATUS_OK);
krista@1618
    33
    assert(session);
krista@1618
    34
    cout << "init() completed.\n";
krista@1618
    35
    
krista@1618
    36
    int i = 0;
krista@1618
    37
    
krista@1618
    38
    for ( ; i < num_keyfiles; i++) {
krista@1618
    39
        ifstream infilekey(keynames[i]);
krista@1618
    40
        string keytextkey;
krista@1618
    41
        while (!infilekey.eof()) {
krista@1618
    42
            static string line;
krista@1618
    43
            getline(infilekey, line);
krista@1618
    44
            keytextkey += line + "\n";
krista@1618
    45
        }
krista@1618
    46
        infilekey.close(); 
krista@1618
    47
        PEP_STATUS statuskey = import_key(session, keytextkey.c_str(), keytextkey.length(), NULL);
krista@1618
    48
        assert(statuskey == PEP_STATUS_OK);
krista@1618
    49
    }        
krista@1618
    50
        
krista@1618
    51
    ifstream infile(mailfile);
krista@1618
    52
    string mailtext;
krista@1618
    53
    while (!infile.eof()) {
krista@1618
    54
        static string line;
krista@1618
    55
        getline(infile, line);
krista@1618
    56
        mailtext += line + "\n";
krista@1618
    57
    }
krista@1618
    58
    infile.close(); 
krista@1618
    59
krista@1618
    60
    pEp_identity * me1 = new_identity("pep.color.test.P@kgrothoff.org", NULL, 
krista@1618
    61
                                      PEP_OWN_USERID, "Pep Color Test P (recip)");    
krista@1618
    62
    me1->me = true;    
krista@1618
    63
    PEP_STATUS status = update_identity(session, me1);
krista@1618
    64
    trust_personal_key(session, me1);
krista@1618
    65
    status = update_identity(session, me1);        
krista@1618
    66
    
krista@1618
    67
    pEp_identity * sender1 = new_identity("pep.color.test.V@kgrothoff.org",
krista@1618
    68
                                          NULL, "TOFU_pep.color.test.V@kgrothoff.org",
krista@1618
    69
                                          "Pep Color Test V (sender)");
krista@1618
    70
                                          
krista@1618
    71
    sender1->me = false;
krista@1618
    72
    status = update_identity(session, sender1);
krista@1618
    73
    trust_personal_key(session, sender1);
krista@1618
    74
    status = update_identity(session, sender1);    
krista@1618
    75
    
krista@1618
    76
    message* msg_ptr = nullptr;
krista@1618
    77
    message* dest_msg = nullptr;
krista@1618
    78
    message* final_ptr = nullptr;
krista@1618
    79
    stringlist_t* keylist = nullptr;
krista@1618
    80
    PEP_rating rating;
krista@1618
    81
    PEP_decrypt_flags_t flags;
krista@1618
    82
    
krista@1618
    83
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1618
    84
    assert(status == PEP_STATUS_OK);
krista@1618
    85
    assert(msg_ptr);
krista@1618
    86
    final_ptr = msg_ptr;
krista@1618
    87
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1618
    88
    final_ptr = dest_msg ? dest_msg : msg_ptr;
krista@1618
    89
  
krista@1618
    90
    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
krista@1618
    91
    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
krista@1618
    92
    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
krista@1618
    93
    cout << "rating: " << rating << endl << endl;
krista@1618
    94
    cout << "keys used: " << endl;
krista@1618
    95
    i = 0;
krista@1618
    96
    for (stringlist_t* k = keylist; k; k = k->next) {
krista@1618
    97
        if (i == 0)
krista@1618
    98
       	    cout << "\t Signer (key 0):\t" << k->value << endl;
krista@1618
    99
        else
krista@1618
   100
            cout << "\t #" << i << ":\t" << k->value << endl;
krista@1618
   101
        i++;
krista@1618
   102
    }
krista@1618
   103
    
krista@1618
   104
//    free_identity(me1);
krista@1618
   105
    if (final_ptr == dest_msg)
krista@1618
   106
    	free_message(dest_msg);
krista@1618
   107
    free_message(msg_ptr);
krista@1618
   108
    free_stringlist(keylist);
krista@1618
   109
    
krista@1618
   110
    cout << "calling release()\n";
krista@1618
   111
    release(session);
krista@1618
   112
    return 0;
krista@1618
   113
}