1.1 --- a/test/src/EncryptForIdentityTest.cc Thu May 28 11:39:38 2020 +0200
1.2 +++ b/test/src/EncryptForIdentityTest.cc Thu May 28 11:48:42 2020 +0200
1.3 @@ -114,6 +114,271 @@
1.4 const char* nobody_fpr = "1111111111111111111111111111111111111111";
1.5
1.6 output_stream << "creating message…\n";
1.7 + pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", alice_fpr, PEP_OWN_USERID, "Alice Test");
1.8 + pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
1.9 +
1.10 + alice->me = true;
1.11 +
1.12 + PEP_STATUS mystatus = set_own_key(session, alice, alice_fpr);
1.13 + ASSERT_EQ(mystatus, PEP_STATUS_OK);
1.14 +
1.15 + identity_list* to_list = new_identity_list(bob); // to bob
1.16 + message* outgoing_message = new_message(PEP_dir_outgoing);
1.17 + ASSERT_NE(outgoing_message, nullptr);
1.18 + outgoing_message->from = alice;
1.19 + outgoing_message->to = to_list;
1.20 + outgoing_message->shortmsg = strdup("Greetings, humans!");
1.21 + outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
1.22 + outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
1.23 + output_stream << "message created.\n";
1.24 +
1.25 + char* encoded_text = nullptr;
1.26 + PEP_STATUS status = mime_encode_message(outgoing_message, false, &encoded_text, false);
1.27 + ASSERT_EQ(status, PEP_STATUS_OK);
1.28 + ASSERT_NE(encoded_text, nullptr);
1.29 +
1.30 + output_stream << "decrypted:\n\n";
1.31 + output_stream << encoded_text << "\n";
1.32 +
1.33 + free(encoded_text);
1.34 +
1.35 + message* encrypted_msg = nullptr;
1.36 + output_stream << "calling encrypt_message_for_identity()\n";
1.37 + 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.38 + output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
1.39 + ASSERT_EQ(status, PEP_STATUS_OK);
1.40 + ASSERT_NE(encrypted_msg, nullptr);
1.41 + output_stream << "message encrypted.\n";
1.42 +
1.43 + status = mime_encode_message(encrypted_msg, false, &encoded_text, false);
1.44 + ASSERT_EQ(status, PEP_STATUS_OK);
1.45 + ASSERT_NE(encoded_text, nullptr);
1.46 +
1.47 + output_stream << "encrypted:\n\n";
1.48 + output_stream << encoded_text << "\n";
1.49 +
1.50 + message* decoded_msg = nullptr;
1.51 + status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg, NULL);
1.52 + ASSERT_EQ(status, PEP_STATUS_OK);
1.53 + const string string3 = encoded_text;
1.54 +
1.55 + unlink("tmp/msg_encrypt_for_self.asc");
1.56 + ofstream outFile3("tmp/msg_encrypt_for_self.asc");
1.57 + outFile3.write(string3.c_str(), string3.size());
1.58 + outFile3.close();
1.59 +
1.60 + message* decrypted_msg = nullptr;
1.61 + stringlist_t* keylist_used = nullptr;
1.62 +
1.63 + PEP_rating rating;
1.64 + PEP_decrypt_flags_t flags;
1.65 +
1.66 + flags = 0;
1.67 + status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
1.68 + ASSERT_NE(decrypted_msg, nullptr);
1.69 + ASSERT_NE(keylist_used, nullptr);
1.70 + ASSERT_NE(rating, 0);
1.71 + ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
1.72 + PEP_comm_type ct = encrypted_msg->from->comm_type;
1.73 + ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
1.74 +
1.75 + output_stream << "keys used:\n";
1.76 +
1.77 + int i = 0;
1.78 +
1.79 + for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
1.80 + {
1.81 + if (i == 0) {
1.82 + ASSERT_STRCASEEQ("",kl4->value);
1.83 + }
1.84 + else {
1.85 + output_stream << "\t " << kl4->value << endl;
1.86 + ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
1.87 + output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
1.88 + }
1.89 + ASSERT_LT(i , 2);
1.90 + }
1.91 + output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
1.92 +
1.93 + output_stream << "freeing messages…\n";
1.94 + free_message(encrypted_msg);
1.95 + free_message(decrypted_msg);
1.96 + free_stringlist (keylist_used);
1.97 + output_stream << "done.\n";
1.98 +
1.99 + output_stream << "Now encrypt for self with extra keys." << endl;
1.100 + stringlist_t* extra_keys = new_stringlist(gabrielle_fpr);
1.101 + stringlist_add(extra_keys, bella_fpr);
1.102 + encrypted_msg = NULL;
1.103 + decrypted_msg = NULL;
1.104 + keylist_used = NULL;
1.105 +
1.106 + output_stream << "calling encrypt_message_for_identity()\n";
1.107 + 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.108 + output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
1.109 + ASSERT_EQ(status, PEP_STATUS_OK);
1.110 + ASSERT_NE(encrypted_msg, nullptr);
1.111 + output_stream << "message encrypted.\n";
1.112 +
1.113 + flags = 0;
1.114 + status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
1.115 + ASSERT_NE(decrypted_msg, nullptr);
1.116 + ASSERT_NE(keylist_used, nullptr);
1.117 + ASSERT_NE(rating, 0);
1.118 + ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
1.119 + ct = encrypted_msg->from->comm_type;
1.120 + ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
1.121 +
1.122 + output_stream << "keys used:\n";
1.123 +
1.124 + for (stringlist_t* incoming_kl = extra_keys; incoming_kl && incoming_kl->value; incoming_kl = incoming_kl->next) {
1.125 + bool found = false;
1.126 + output_stream << "Encrypted for: ";
1.127 + for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++) {
1.128 + if (strcasecmp(incoming_kl->value, kl4->value) == 0) {
1.129 + output_stream << "\t " << kl4->value;
1.130 + found = true;
1.131 + break;
1.132 + }
1.133 + }
1.134 + output_stream << endl;
1.135 + ASSERT_TRUE(found);
1.136 + }
1.137 + output_stream << "Encrypted for all the extra keys!" << endl;
1.138 +
1.139 + bool found = false;
1.140 + for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
1.141 + {
1.142 + if (strcasecmp(alice_fpr, kl4->value) == 0) {
1.143 + found = true;
1.144 + output_stream << "Encrypted also for Alice! Yay!" << endl;
1.145 + break;
1.146 + }
1.147 + }
1.148 + ASSERT_TRUE(found);
1.149 +
1.150 + free_message(encrypted_msg);
1.151 + encrypted_msg = NULL;
1.152 + free_message(decrypted_msg);
1.153 + decrypted_msg = NULL;
1.154 + free_stringlist(keylist_used);
1.155 + keylist_used = NULL;
1.156 +
1.157 + output_stream << "Now add a bad fpr." << endl;
1.158 +
1.159 + stringlist_add(extra_keys, nobody_fpr);
1.160 +
1.161 + output_stream << "calling encrypt_message_for_identity()\n";
1.162 + 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.163 + output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
1.164 + ASSERT_NE(status, PEP_STATUS_OK);
1.165 +
1.166 + free_message(outgoing_message);
1.167 + outgoing_message = NULL;
1.168 + free_message(encrypted_msg);
1.169 + encrypted_msg = NULL;
1.170 + free_message(decrypted_msg);
1.171 + decrypted_msg = NULL;
1.172 + free_stringlist(keylist_used);
1.173 + keylist_used = NULL;
1.174 +
1.175 +
1.176 + output_stream << "*** Now testing MIME_encrypt_for_self ***" << endl;
1.177 +
1.178 + alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
1.179 + bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
1.180 +
1.181 + output_stream << "Reading in alice_bob_encrypt_test_plaintext_mime.eml..." << endl;
1.182 +
1.183 + const string mimetext = slurp("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
1.184 +
1.185 + output_stream << "Text read:" << endl;
1.186 + output_stream << mimetext.c_str() << endl;
1.187 + char* encrypted_mimetext = nullptr;
1.188 +
1.189 + output_stream << "Calling MIME_encrypt_message_for_self" << endl;
1.190 + status = MIME_encrypt_message_for_self(session, alice, mimetext.c_str(),
1.191 + mimetext.size(),
1.192 + NULL,
1.193 + &encrypted_mimetext,
1.194 + PEP_enc_PGP_MIME,
1.195 + PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
1.196 +
1.197 + output_stream << "Encrypted message:" << endl;
1.198 + output_stream << encrypted_mimetext << endl;
1.199 +
1.200 + output_stream << "Calling MIME_decrypt_message" << endl;
1.201 +
1.202 + char* decrypted_mimetext = nullptr;
1.203 + free_stringlist(keylist_used);
1.204 + keylist_used = nullptr;
1.205 + PEP_decrypt_flags_t mimeflags;
1.206 + PEP_rating mimerating;
1.207 + char* modified_src = NULL;
1.208 +
1.209 + mimeflags = 0;
1.210 + status = MIME_decrypt_message(session,
1.211 + encrypted_mimetext,
1.212 + strlen(encrypted_mimetext),
1.213 + &decrypted_mimetext,
1.214 + &keylist_used,
1.215 + &mimerating,
1.216 + &mimeflags,
1.217 + &modified_src);
1.218 +
1.219 + ASSERT_NE(decrypted_mimetext, nullptr);
1.220 + ASSERT_NE(keylist_used, nullptr);
1.221 + ASSERT_NE(mimerating, 0);
1.222 +
1.223 + ASSERT_TRUE(status == PEP_DECRYPTED && mimerating == PEP_rating_unreliable);
1.224 +
1.225 + output_stream << "Decrypted message:" << endl;
1.226 + output_stream << decrypted_mimetext << endl;
1.227 +
1.228 + output_stream << "keys used:\n";
1.229 +
1.230 + i = 0;
1.231 +
1.232 + for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
1.233 + {
1.234 + if (i == 0) {
1.235 + ASSERT_STRCASEEQ("",kl4->value);
1.236 + }
1.237 + else {
1.238 + output_stream << "\t " << kl4->value << endl;
1.239 + ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
1.240 + output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
1.241 + }
1.242 + ASSERT_LT(i , 2);
1.243 + }
1.244 + output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
1.245 +}
1.246 +
1.247 +TEST_F(EncryptForIdentityTest, check_encrypt_for_identity_with_URI) {
1.248 +
1.249 + // message_api test code
1.250 +
1.251 + const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
1.252 + const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
1.253 + const string gabrielle_pub_key = slurp("test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
1.254 + const string bella_pub_key = slurp("test_keys/pub/pep.test.bella-0xAF516AAE_pub.asc");
1.255 +
1.256 + PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
1.257 + PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
1.258 + ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
1.259 + ASSERT_EQ(statuspriv, PEP_TEST_KEY_IMPORT_SUCCESS);
1.260 +
1.261 + statuspub = import_key(session, gabrielle_pub_key.c_str(), gabrielle_pub_key.length(), NULL);
1.262 + ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
1.263 + statuspub = import_key(session, bella_pub_key.c_str(), bella_pub_key.length(), NULL);
1.264 + ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
1.265 +
1.266 + const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
1.267 + const char* gabrielle_fpr = "906C9B8349954E82C5623C3C8C541BD4E203586C";
1.268 + const char* bella_fpr = "5631BF1357326A02AA470EEEB815EF7FA4516AAE";
1.269 + const char* nobody_fpr = "1111111111111111111111111111111111111111";
1.270 +
1.271 + output_stream << "creating message…\n";
1.272 pEp_identity* alice = new_identity("payto://BIC/SYSTEMA", alice_fpr, PEP_OWN_USERID, "Alice Test");
1.273 pEp_identity* bob = new_identity("payto://BIC/SYSTEMB", NULL, "42", "Bob Test");
1.274
1.275 @@ -353,3 +618,4 @@
1.276 }
1.277 output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
1.278 }
1.279 +