test/src/engine_tests/ExternalRevokeTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Fri, 15 Mar 2019 12:43:56 +0100
branchsync
changeset 3335 09af2cf0ac74
parent 3292 673b8c6da681
child 3886 ebd61a1cf6db
permissions -rw-r--r--
Reverted out device model
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "TestConstants.h"
     5 #include <stdlib.h>
     6 #include <string>
     7 #include <cstring>
     8 #include <time.h>
     9 #include "platform.h"
    10 #include <iostream>
    11 #include <fstream>
    12 #include "mime.h"
    13 #include "message_api.h"
    14 #include "test_util.h"
    15 
    16 #include "pEpEngine.h"
    17 
    18 #include <cpptest.h>
    19 #include "EngineTestSessionSuite.h"
    20 #include "ExternalRevokeTests.h"
    21 
    22 using namespace std;
    23 
    24 ExternalRevokeTests::ExternalRevokeTests(string suitename, string test_home_dir) :
    25     EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    26     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("ExternalRevokeTests::check_external_revoke"),
    27                                                                       static_cast<Func>(&ExternalRevokeTests::check_external_revoke)));
    28 }
    29 
    30 void ExternalRevokeTests::check_external_revoke() {
    31 
    32     PEP_STATUS status = PEP_STATUS_OK;   
    33 
    34 #ifndef NETPGP
    35     char* fprs[2];
    36 
    37     const string fenris_pub_key = slurp("test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc");
    38     const string fenris_priv_key = slurp("test_keys/priv/pep.test.fenris-0x4F3D2900_priv.asc");
    39 
    40     TEST_ASSERT_MSG((fenris_pub_key.length() != 0), "fenris_pub_key.length() != 0");
    41     TEST_ASSERT_MSG((fenris_priv_key.length() != 0), "fenris_priv_key.length() != 0");
    42     
    43     PEP_STATUS statuspub = import_key(session, fenris_pub_key.c_str(), fenris_pub_key.length(), NULL);
    44     PEP_STATUS statuspriv = import_key(session, fenris_priv_key.c_str(), fenris_priv_key.length(), NULL);
    45     TEST_ASSERT_MSG((statuspub == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspub == PEP_STATUS_OK");
    46     TEST_ASSERT_MSG((statuspriv == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspriv == PEP_STATUS_OK");
    47 
    48     // Create sender ID
    49     
    50     pEp_identity * me = new_identity("pep.test.fenris@thisstilldoesntwork.lu", "0969FA229DF21C832A64A04711B1B9804F3D2900", PEP_OWN_USERID, "Fenris Hawke");
    51     status = myself(session, me);
    52     
    53     // Create key
    54     cout << "Creating new id for : ";
    55     char *uniqname = strdup("AAAAtestuser@testdomain.org");
    56     srandom(time(NULL));
    57     for(int i=0; i < 4;i++)
    58         uniqname[i] += random() & 0xf;
    59     
    60     cout << uniqname << "\n";
    61     pEp_identity * recip1 = new_identity(uniqname, NULL, NULL, "Test User");
    62 
    63     status = generate_keypair(session, recip1);
    64     
    65     cout << "Generated fingerprint ";
    66     cout << recip1->fpr << "\n";
    67 
    68     fprs[0] = strdup(recip1->fpr);
    69     
    70     cout << endl << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl;
    71     cout << "Trust and revoke single key, ensure trust changes, then generate new key and ensure rating is correct." << endl;
    72     cout << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl << endl;
    73     
    74     cout << endl << "---------------------------------------------------------" << endl;
    75     cout << "1a. Encrypt message for trusted partner." << endl;
    76     cout << "---------------------------------------------------------" << endl << endl;
    77 
    78     cout << "Trusting personal key for " << uniqname << endl;
    79     recip1->me = false;
    80     // Trust it
    81     status = update_identity(session, recip1);
    82     status = trust_personal_key(session, recip1);
    83     status = update_identity(session, recip1);
    84     
    85     // TODO: Check trust?
    86     cout << "Done! Trusted personal key with fpr " << recip1->fpr << " for " << uniqname << endl;
    87 
    88     const char* r1_userid = (recip1->user_id ? strdup(recip1->user_id) : NULL);
    89 
    90     
    91     // encrypt something to the key
    92     cout << "Creating message…\n";
    93     identity_list* to_list = new_identity_list(identity_dup(recip1)); // to bob
    94     message* outgoing_msg = new_message(PEP_dir_outgoing);
    95     TEST_ASSERT_MSG((outgoing_msg), "outgoing_msg");
    96     outgoing_msg->from = identity_dup(me);
    97     outgoing_msg->to = to_list;
    98     outgoing_msg->shortmsg = strdup("Greetings, humans!");
    99     outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   100     outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   101     cout << "Message created.\n";
   102 
   103     message* encrypted_outgoing_msg = NULL;
   104 
   105     cout << "Encrypting message to " << uniqname << "…\n";    
   106     status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   107     cout << "Encrypted message with status " << tl_status_string(status) << endl;
   108     // check status
   109     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
   110     TEST_ASSERT_MSG((encrypted_outgoing_msg), "encrypted_outgoing_msg");
   111 
   112     cout << "Checking message recipient comm_type from message." << endl;
   113     // check comm_type
   114     cout << "comm_type: " << tl_ct_string(encrypted_outgoing_msg->to->ident->comm_type) << endl;
   115     TEST_ASSERT_MSG((encrypted_outgoing_msg->to->ident->comm_type == PEP_ct_OpenPGP), "encrypted_outgoing_msg->to->ident->comm_type == PEP_ct_OpenPGP");
   116     
   117     status = get_trust(session, recip1);
   118     
   119     cout << "Recip's trust DB comm_type = "  << tl_ct_string(recip1->comm_type) << endl;
   120     TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_OpenPGP), "recip1->comm_type == PEP_ct_OpenPGP"); // FIXME: PEP_ct_pEp???
   121 
   122     // decrypt message
   123     free_message(outgoing_msg);
   124     outgoing_msg = NULL;
   125 
   126     stringlist_t* keylist = nullptr;
   127     PEP_rating rating;
   128     PEP_decrypt_flags_t flags;
   129 
   130     cout << endl << "---------------------------------------------------------" << endl;
   131     cout << "1b. Decrypt message that was encrypted for trusted partner." << endl;
   132     cout << "---------------------------------------------------------" << endl << endl;
   133 
   134     flags = 0;
   135     cout << "Decrypting message." << endl;
   136     status = decrypt_message(session, encrypted_outgoing_msg, &outgoing_msg, &keylist, &rating, &flags);
   137     cout << "Decrypted message with status " << tl_status_string(status) << endl;
   138     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
   139     TEST_ASSERT_MSG((rating == PEP_rating_trusted), "rating == PEP_rating_trusted");
   140 
   141     // check rating
   142     cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
   143     TEST_ASSERT_MSG((rating == PEP_rating_trusted), "rating == PEP_rating_trusted"); // FIXME: trusted and anonymised?
   144     
   145     // check comm_type
   146     status = get_trust(session, recip1);
   147 
   148     cout << "Recip's trust DB comm_type = " << tl_ct_string(recip1->comm_type) << endl;
   149     TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_OpenPGP), "recip1->comm_type == PEP_ct_OpenPGP"); // FIXME: PEP_ct_pEp???
   150 
   151     cout << endl << "---------------------------------------------------------" << endl;
   152     cout << "2a. Revoke key for (currently) trusted partner." << endl;
   153     cout << "---------------------------------------------------------" << endl << endl;
   154     // externally revoke key
   155     // (note - as of 23.5.17, revoke_key() doesn't touch the trust db, just the keyring, so we can do this)
   156 
   157     cout << "Revoking key." << endl;
   158     status = update_identity(session, recip1);    
   159     status = revoke_key(session, recip1->fpr, "encrypt_for_identity_test");
   160     cout << "Status of revocation call for " << recip1->fpr << " is "<< tl_status_string(status) << endl;
   161 
   162     // free messages
   163     free_message(outgoing_msg);
   164     free_message(encrypted_outgoing_msg);
   165     outgoing_msg = NULL;
   166     encrypted_outgoing_msg = NULL;
   167     
   168     // encrypt something to the key
   169     cout << "creating message…\n";
   170     to_list = new_identity_list(identity_dup(recip1)); // to bob
   171     outgoing_msg = new_message(PEP_dir_outgoing);
   172     TEST_ASSERT_MSG((outgoing_msg), "outgoing_msg");
   173     outgoing_msg->from = identity_dup(me);
   174     outgoing_msg->to = to_list;
   175     outgoing_msg->shortmsg = strdup("Greetings, humans!");
   176     outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   177     outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   178     cout << "message created.\n";
   179 
   180     encrypted_outgoing_msg = NULL;
   181     message* decrypted_msg = NULL;
   182 
   183     cout << endl << "---------------------------------------------------------" << endl;
   184     cout << "2b. Encrypt message for recip whose key has been externally revoked in the keyring, not the app." << endl;
   185     cout << "---------------------------------------------------------" << endl << endl;
   186 
   187 
   188     status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   189     cout << "Encryption returns with status " << tl_status_string(status) << endl;
   190     TEST_ASSERT (status == PEP_UNENCRYPTED);
   191     TEST_ASSERT (encrypted_outgoing_msg == NULL);
   192     status = update_identity(session, recip1);
   193     TEST_ASSERT_MSG((recip1->comm_type = PEP_ct_key_not_found), "recip1->comm_type = PEP_ct_key_not_found");
   194 
   195     cout << endl << "---------------------------------------------------------" << endl;
   196     cout << "2c. Check trust of recip, whose only key has been revoked, once an encryption attempt has been made." << endl;
   197     cout << "---------------------------------------------------------" << endl << endl;
   198 
   199     TEST_ASSERT_MSG((recip1->fpr == NULL), "recip1->fpr == NULL");
   200     recip1->fpr = fprs[0];
   201     status = get_trust(session, recip1);
   202     recip1->fpr = NULL;
   203 
   204     cout << "Recip's trust DB comm_type = " << tl_ct_string(recip1->comm_type) << endl;
   205     TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_unknown || recip1->comm_type == PEP_ct_key_revoked), "recip1->comm_type == PEP_ct_unknown || recip1->comm_type == PEP_ct_key_revoked");
   206 
   207     free_message(decrypted_msg);
   208     free_message(outgoing_msg);
   209     outgoing_msg = NULL;
   210     decrypted_msg = NULL;
   211 
   212     cout << endl << "---------------------------------------------------------" << endl;
   213     cout << "3a. Generate new key, but don't explicitly trust it." << endl;
   214     cout << "---------------------------------------------------------" << endl << endl;
   215 
   216     // now: generate new key
   217     free(recip1->fpr);
   218     recip1->fpr = NULL;
   219     status = generate_keypair(session, recip1);
   220     
   221     cout << "Generated fingerprint \n";
   222     cout << recip1->fpr << "\n";
   223     fprs[1] = strdup(recip1->fpr);
   224 
   225     // try again
   226     cout << endl << "---------------------------------------------------------" << endl;
   227     cout << "3b. Try to send something to the email address of our revoked friend, make sure a new key is used to encrypt." << endl;
   228     cout << "---------------------------------------------------------" << endl << endl;
   229     
   230     // encrypt something to the key
   231     cout << "Creating message…\n";
   232     
   233     // cout << "First, update identity though!\n";
   234     // status = update_identity(session, recip1);
   235     to_list = new_identity_list(identity_dup(recip1)); // to bob
   236     outgoing_msg = new_message(PEP_dir_outgoing);
   237     TEST_ASSERT_MSG((outgoing_msg), "outgoing_msg");
   238     outgoing_msg->from = identity_dup(me);
   239     outgoing_msg->to = to_list;
   240     outgoing_msg->shortmsg = strdup("Greetings, humans!");
   241     outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   242     outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   243     cout << "Message created.\n";
   244 
   245     status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   246     PEP_comm_type ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   247     
   248 
   249     // CHECK STATUS???
   250     cout << "Encryption returns with status " << tl_status_string(status) << endl;
   251 
   252     // check comm_type
   253     cout << "comm_type: " << tl_ct_string(ct) << endl;
   254     TEST_ASSERT_MSG((ct == PEP_ct_OpenPGP_unconfirmed), "ct == PEP_ct_OpenPGP_unconfirmed");
   255     
   256     status = get_trust(session, recip1);
   257 
   258     cout << "Recip's trust DB comm_type (should be unknown, as we're using a keyring-only key, not in DB) = "  << tl_ct_string(recip1->comm_type) << endl;
   259     TEST_ASSERT_MSG((recip1->comm_type != PEP_ct_OpenPGP_unconfirmed), "recip1->comm_type != PEP_ct_OpenPGP_unconfirmed");
   260 
   261     // decrypt message
   262 //    free_message(outgoing_msg);
   263 //    outgoing_msg = NULL;
   264 
   265     cout << endl << "---------------------------------------------------------" << endl;
   266     cout << "3c. Decrypt... that... message!" << endl;
   267     cout << "---------------------------------------------------------" << endl << endl;
   268 
   269 
   270     flags = 0;
   271     status = decrypt_message(session, encrypted_outgoing_msg, &decrypted_msg, &keylist, &rating, &flags);
   272     cout << "Decryption returns with status " << tl_status_string(status) << endl;
   273     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
   274     TEST_ASSERT_MSG((decrypted_msg), "decrypted_msg");
   275     
   276     // check rating
   277     cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
   278     TEST_ASSERT_MSG((rating == PEP_rating_reliable), "rating == PEP_rating_reliable");
   279 
   280     status = update_identity(session, decrypted_msg->to->ident);
   281     ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   282 
   283     cout << "comm_type: " << tl_ct_string(ct) << endl;
   284     TEST_ASSERT_MSG((ct == PEP_ct_OpenPGP_unconfirmed), "ct == PEP_ct_OpenPGP_unconfirmed");
   285     
   286     status = get_trust(session, recip1);
   287     
   288     cout << "Recip's trust DB comm_type (should be unknown - there's nothing in the DB) = "  << tl_ct_string(recip1->comm_type) << endl;
   289     TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_unknown), "recip1->comm_type == PEP_ct_unknown");
   290 
   291     free_message(encrypted_outgoing_msg);
   292     free_message(decrypted_msg);
   293     free_message(outgoing_msg);
   294     outgoing_msg = NULL;
   295     decrypted_msg = NULL;
   296     encrypted_outgoing_msg = NULL;
   297 
   298     free_identity(me);
   299     free_identity(recip1);
   300     free(uniqname);
   301         
   302     free(fprs[0]);
   303     free(fprs[1]);
   304     
   305 #else
   306     cout << "Sorry, test is not defined for NETPGP at this time." << endl;
   307     
   308 #endif
   309 }