test/encrypt_for_identity_test.cc
changeset 2589 3c5db90b50e8
parent 2588 858909bcf85b
child 2615 5cd3625e0d91
     1.1 --- a/test/encrypt_for_identity_test.cc	Thu Apr 05 14:13:20 2018 +0200
     1.2 +++ b/test/encrypt_for_identity_test.cc	Thu Apr 05 16:13:14 2018 +0200
     1.3 @@ -29,18 +29,31 @@
     1.4  
     1.5      const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
     1.6      const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
     1.7 +    const string gabrielle_pub_key = slurp("test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
     1.8 +    const string bella_pub_key = slurp("test_keys/pub/pep.test.bella-0xAF516AAE_pub.asc");    
     1.9  
    1.10      PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
    1.11      PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
    1.12      assert(statuspub == PEP_STATUS_OK);
    1.13      assert(statuspriv == PEP_STATUS_OK);
    1.14 +    
    1.15 +    statuspub = import_key(session, gabrielle_pub_key.c_str(), gabrielle_pub_key.length(), NULL);
    1.16 +    assert(statuspub == PEP_STATUS_OK);
    1.17 +    statuspub = import_key(session, bella_pub_key.c_str(), bella_pub_key.length(), NULL);
    1.18 +    assert(statuspub == PEP_STATUS_OK);
    1.19 +
    1.20 +    const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    1.21 +    const char* gabrielle_fpr = "906C9B8349954E82C5623C3C8C541BD4E203586C";
    1.22 +    const char* bella_fpr = "5631BF1357326A02AA470EEEB815EF7FA4516AAE";
    1.23 +    const char* nobody_fpr = "1111111111111111111111111111111111111111";
    1.24  
    1.25      cout << "creating message…\n";
    1.26 -    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
    1.27 +    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", alice_fpr, PEP_OWN_USERID, "Alice Test");
    1.28      pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
    1.29 +    
    1.30      alice->me = true;
    1.31  
    1.32 -    PEP_STATUS mystatus = set_own_key(session, alice, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
    1.33 +    PEP_STATUS mystatus = set_own_key(session, alice, alice_fpr);
    1.34      assert(mystatus == PEP_STATUS_OK);
    1.35  
    1.36      identity_list* to_list = new_identity_list(bob); // to bob
    1.37 @@ -63,7 +76,6 @@
    1.38  
    1.39      free(encoded_text);
    1.40  
    1.41 -    cout << "encrypting message as MIME multipart…\n";
    1.42      message* encrypted_msg = nullptr;
    1.43      cout << "calling encrypt_message_for_identity()\n";
    1.44      status = encrypt_message_for_self(session, alice, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
    1.45 @@ -123,8 +135,84 @@
    1.46      cout << "freeing messages…\n";
    1.47      free_message(encrypted_msg);
    1.48      free_message(decrypted_msg);
    1.49 +    free_stringlist (keylist_used);
    1.50 +    cout << "done.\n";
    1.51 +
    1.52 +    cout << "Now encrypt for self with extra keys." << endl;
    1.53 +    stringlist_t* extra_keys = new_stringlist(gabrielle_fpr);
    1.54 +    stringlist_add(extra_keys, bella_fpr);
    1.55 +    encrypted_msg = NULL;
    1.56 +    decrypted_msg = NULL;
    1.57 +    keylist_used = NULL;
    1.58 +
    1.59 +    cout << "calling encrypt_message_for_identity()\n";
    1.60 +    status = encrypt_message_for_self(session, alice, outgoing_message, extra_keys, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
    1.61 +    cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
    1.62 +    assert(status == PEP_STATUS_OK);
    1.63 +    assert(encrypted_msg);
    1.64 +    cout << "message encrypted.\n";
    1.65 +    
    1.66 +    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
    1.67 +    assert(decrypted_msg);
    1.68 +    assert(keylist_used);
    1.69 +    assert(rating);
    1.70 +    assert(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
    1.71 +    ct = encrypted_msg->from->comm_type;
    1.72 +    assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
    1.73 +
    1.74 +    cout << "keys used:\n";
    1.75 +
    1.76 +    for (stringlist_t* incoming_kl = extra_keys; incoming_kl && incoming_kl->value; incoming_kl = incoming_kl->next) {
    1.77 +        bool found = false;
    1.78 +        cout << "Encrypted for: ";
    1.79 +        for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++) {
    1.80 +            if (strcasecmp(incoming_kl->value, kl4->value) == 0) {
    1.81 +                cout << "\t " << kl4->value;
    1.82 +                found = true;
    1.83 +                break;
    1.84 +            }
    1.85 +        }
    1.86 +        cout << endl;
    1.87 +        assert(found);
    1.88 +    }
    1.89 +    cout << "Encrypted for all the extra keys!" << endl;
    1.90 +
    1.91 +    bool found = false;
    1.92 +    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
    1.93 +    {
    1.94 +        if (strcasecmp(alice_fpr, kl4->value) == 0) {
    1.95 +            found = true;
    1.96 +            cout << "Encrypted also for Alice! Yay!" << endl;
    1.97 +            break;
    1.98 +        }
    1.99 +    }
   1.100 +    assert(found);
   1.101 +
   1.102 +    free_message(encrypted_msg);
   1.103 +    encrypted_msg = NULL;
   1.104 +    free_message(decrypted_msg);
   1.105 +    decrypted_msg = NULL;
   1.106 +    free_stringlist(keylist_used);
   1.107 +    keylist_used = NULL;
   1.108 +
   1.109 +    cout << "Now add a bad fpr." << endl;
   1.110 +    
   1.111 +    stringlist_add(extra_keys, nobody_fpr);
   1.112 +    
   1.113 +    cout << "calling encrypt_message_for_identity()\n";
   1.114 +    status = encrypt_message_for_self(session, alice, outgoing_message, extra_keys, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   1.115 +    cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
   1.116 +    assert(status != PEP_STATUS_OK);
   1.117 +
   1.118      free_message(outgoing_message);
   1.119 -    cout << "done.\n";
   1.120 +    outgoing_message = NULL;
   1.121 +    free_message(encrypted_msg);
   1.122 +    encrypted_msg = NULL;
   1.123 +    free_message(decrypted_msg);
   1.124 +    decrypted_msg = NULL;
   1.125 +    free_stringlist(keylist_used);
   1.126 +    keylist_used = NULL;
   1.127 +
   1.128  
   1.129      cout << "*** Now testing MIME_encrypt_for_self ***" << endl;
   1.130  
   1.131 @@ -166,7 +254,7 @@
   1.132                                    &mimerating,
   1.133                                    &mimeflags);
   1.134  
   1.135 -    assert(decrypted_msg);
   1.136 +    assert(decrypted_mimetext);
   1.137      assert(keylist_used);
   1.138      assert(mimerating);
   1.139