test/external_revoke_test.cc
author Krista Bennett <krista@pep-project.org>
Wed, 31 May 2017 17:47:42 +0200
branchENGINE-209
changeset 1811 04d2a7abcfa2
parent 1808 121361c60644
child 1812 925ad845e3ec
permissions -rw-r--r--
ENGINE-209: asserts put into tests. Still have question about PEP_ct_pEp
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string.h>
     6 #include <time.h>
     7 #include "platform.h"
     8 #include <iostream>
     9 #include <fstream>
    10 #include <assert.h>
    11 #include "mime.h"
    12 #include "message_api.h"
    13 
    14 using namespace std;
    15 
    16 int main() {
    17     cout << "\n*** external_revoke_test.cc ***\n\n";
    18 
    19     PEP_SESSION session;
    20     
    21     cout << "calling init()\n";
    22     PEP_STATUS status = init(&session);   
    23     assert(status == PEP_STATUS_OK);
    24     assert(session);
    25     cout << "init() completed.\n";
    26 
    27 #ifndef NETPGP
    28 
    29     // Create sender ID
    30     
    31     pEp_identity * me = new_identity("pep.test.apple@pep-project.org", NULL, PEP_OWN_USERID, "Alice Cooper");
    32     status = update_identity(session, me);
    33     status = trust_personal_key(session, me);
    34     status = update_identity(session, me);
    35     
    36     // Create key
    37 
    38     cout << "creating new id for : ";
    39     char *uniqname = strdup("AAAAtestuser@testdomain.org");
    40     srandom(time(NULL));
    41     for(int i=0; i < 4;i++)
    42         uniqname[i] += random() & 0xf;
    43     
    44     cout << uniqname << "\n";
    45     pEp_identity * recip1 = new_identity(uniqname, NULL, NULL, "Test User");
    46 
    47     status = generate_keypair(session, recip1);
    48     
    49     cout << "generated fingerprint \n";
    50     cout << recip1->fpr << "\n";
    51 
    52     // Trust it
    53     recip1->me = false;
    54     status = update_identity(session, recip1);
    55     status = trust_personal_key(session, recip1);
    56     status = update_identity(session, recip1);
    57 
    58     const char* r1_userid = (recip1->user_id ? strdup(recip1->user_id) : NULL);
    59 
    60     // encrypt something to the key
    61     cout << "creating message…\n";
    62     identity_list* to_list = new_identity_list(identity_dup(recip1)); // to bob
    63     message* outgoing_message = new_message(PEP_dir_outgoing);
    64     assert(outgoing_message);
    65     outgoing_message->from = identity_dup(me);
    66     outgoing_message->to = to_list;
    67     outgoing_message->shortmsg = strdup("Greetings, humans!");
    68     outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
    69     outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    70     cout << "message created.\n";
    71 
    72     message* encrypted_outgoing_msg = NULL;
    73     
    74     status = encrypt_message(session, outgoing_message, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
    75     
    76     // check status
    77     assert(status == PEP_STATUS_OK);
    78     assert(encrypted_outgoing_msg);
    79 
    80     // check comm_type
    81     cout << "comm_type: " << encrypted_outgoing_msg->to->ident->comm_type << endl;
    82     assert(encrypted_outgoing_msg->to->ident->comm_type == PEP_ct_OpenPGP);
    83     
    84     status = get_trust(session, recip1);
    85     
    86     cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
    87     assert(recip1->comm_type == PEP_ct_OpenPGP); // FIXME: PEP_ct_pEp???
    88 
    89     // decrypt message
    90     free_message(outgoing_message);
    91     outgoing_message = NULL;
    92 
    93     stringlist_t* keylist = nullptr;
    94     PEP_rating rating;
    95     PEP_decrypt_flags_t flags;
    96 
    97     status = decrypt_message(session, encrypted_outgoing_msg, &outgoing_message, &keylist, &rating, &flags);
    98     assert(status == PEP_STATUS_OK);
    99     assert(rating == PEP_rating_trusted);
   100 
   101     // check rating
   102     cout << "Rating of decrypted message to trusted recip: " << rating << endl;
   103     assert(rating == PEP_rating_trusted); // FIXME: trusted and anonymised?
   104     
   105     // check comm_type
   106     status = get_trust(session, recip1);
   107 
   108     cout << "Recip's trust DB comm_type = " << recip1->comm_type << endl;
   109     assert(recip1->comm_type == PEP_ct_OpenPGP); // FIXME: PEP_ct_pEp???
   110 
   111     // externally revoke key
   112     // (note - as of 23.5.17, revoke_key() doesn't touch the trust db, just the keyring, so we can do this)
   113 
   114     status = get_identity(session, uniqname, r1_userid, &recip1);
   115     
   116     status = revoke_key(session, recip1->fpr, "encrypt_for_identity_test");
   117     
   118     // free messages
   119     free_message(outgoing_message);
   120     free_message(encrypted_outgoing_msg);
   121     outgoing_message = NULL;
   122     encrypted_outgoing_msg = NULL;
   123     
   124     // encrypt something to the key
   125     cout << "creating message…\n";
   126     to_list = new_identity_list(identity_dup(recip1)); // to bob
   127     outgoing_message = new_message(PEP_dir_outgoing);
   128     assert(outgoing_message);
   129     outgoing_message->from = identity_dup(me);
   130     outgoing_message->to = to_list;
   131     outgoing_message->shortmsg = strdup("Greetings, humans!");
   132     outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   133     outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   134     cout << "message created.\n";
   135 
   136     encrypted_outgoing_msg = NULL;
   137     message* decrypted_msg = NULL;
   138 
   139     status = encrypt_message(session, outgoing_message, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   140 
   141     PEP_comm_type ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_message->to->ident->comm_type);
   142 
   143     // check comm_type
   144     cout << "comm_type: " << ct << endl;
   145     assert(ct == PEP_ct_key_revoked);
   146     
   147     status = get_trust(session, recip1);
   148 
   149     cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
   150     assert(recip1->comm_type == PEP_ct_key_revoked);
   151 
   152     // decrypt message
   153 //    free_message(outgoing_message);
   154 //    outgoing_message = NULL;
   155 
   156     status = decrypt_message(session, outgoing_message, &decrypted_msg, &keylist, &rating, &flags);
   157     assert(status == PEP_UNENCRYPTED);
   158 
   159     // check rating
   160     cout << "Rating of decrypted message to trusted recip: " << rating << endl;
   161     assert(rating == PEP_rating_unencrypted);
   162 
   163     ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_message->to->ident->comm_type);
   164 
   165     cout << "comm_type: " << ct << endl;
   166     assert(ct == PEP_ct_key_revoked);
   167     
   168     status = get_trust(session, recip1);
   169     
   170     cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
   171     assert(recip1->comm_type == PEP_ct_key_revoked);
   172 
   173     // generate new key
   174     status = generate_keypair(session, recip1);
   175     
   176     cout << "generated fingerprint \n";
   177     cout << recip1->fpr << "\n";
   178 
   179     // PART DEUX
   180     
   181     // Create key
   182 
   183     // DO NOT trust it
   184 
   185     // encrypt something to the key
   186 
   187     // check rating
   188 
   189     // check comm_type
   190 
   191     // externally revoke key
   192     
   193     // encrypt something to the key
   194 
   195     // check rating
   196 
   197     // check comm_type
   198 
   199 
   200     // PART TROIS
   201     
   202     // Create key
   203 
   204     // DO NOT trust it
   205 
   206     // encrypt something to the key
   207 
   208     // check rating
   209 
   210     // check comm_type
   211 
   212     // externally revoke key
   213     
   214     // encrypt something to the key
   215 
   216     // check rating
   217 
   218     // check comm_type
   219     
   220     free(uniqname);
   221 #else
   222     cout << "Sorry, test is not defined for NETPGP at this time." << endl;
   223     
   224 #endif
   225     
   226     release(session);
   227 
   228     return 0;
   229 }