test/message_api_test.cc
author Roker <roker@pep-project.org>
Tue, 26 Dec 2017 22:55:32 +0100
branchENGINE-322
changeset 2332 23ed7bbd53f4
parent 1945 9503c962f746
child 2333 10a26f0f071d
permissions -rw-r--r--
start branch ENGINE-322
     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 "platform.h"
     7 #include <iostream>
     8 #include <fstream>
     9 #include <assert.h>
    10 #include "mime.h"
    11 #include "message_api.h"
    12 
    13 using namespace std;
    14 
    15 void test_MIME_decrypt_message()
    16 {
    17 	const std::string mimetext =
    18 		"Return-Path: <roker@pep-project.org>\r\n"
    19 		"X-Original-To: roker@pep-project.org\r\n"
    20 		"Delivered-To: roker@pep-project.org\r\n"
    21 		"Received: from localhost (localhost [127.0.0.1])\r\n"
    22 		"	by dragon.pibit.ch (Postfix) with ESMTP id C4FF8171C055\r\n"
    23 		"	for <roker@pep-project.org>; Tue, 26 Dec 2017 17:14:42 +0100 (CET)\r\n"
    24 		"Received: from dragon.pibit.ch ([127.0.0.1])\r\n"
    25 		"	by localhost (dragon.pibit.ch [127.0.0.1]) (amavisd-new, port 10024)\r\n"
    26 		"	with ESMTP id GojZqayOfeAq for <roker@pep-project.org>;\r\n"
    27 		"	Tue, 26 Dec 2017 17:14:39 +0100 (CET)\r\n"
    28 		"To: Lars Rohwedder <roker@pep-project.org>\r\n"
    29 		"From: Lars Rohwedder <roker@pep-project.org>\r\n"
    30 		"Subject: Test mail PGP/INLINE\r\n"
    31 		"Message-ID: <8fff4ca8-a8aa-f016-a7fd-39c98a9a4f43@pep-project.org>\r\n"
    32 		"Date: Tue, 26 Dec 2017 17:14:38 +0100\r\n"
    33 		"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0)\r\n"
    34 		" Gecko/20100101 Thunderbird/52.5.0\r\n"
    35 		"MIME-Version: 1.0\r\n"
    36 		"Content-Type: text/plain; charset=utf-8\r\n"
    37 		"Content-Language: en-US\r\n"
    38 		"Content-Transfer-Encoding: 8bit\r\n"
    39 		"\r\n"
    40 		"-----BEGIN PGP MESSAGE-----\r\n"
    41 		"Charset: utf-8\r\n"
    42 		"\r\n"
    43 		"hQIMA+NkSS2yiGkeARAAgtW5xzq/ksfUIRxPZIDwGIANH0iLKhbnFbg/GssuyQOE\r\n"
    44 		"+7rdevKX9UcxwBcRGJYs/aSMvY9zQE4tJy3ohf6+jnRzh3RH055A6+bsUWS/cex3\r\n"
    45 		"fL7PtmQUT2PRXcXfrrk+oiCPXdJPVyRJXKGkKzwmpy7+U8mSSPoR3SIciFy/9CKs\r\n"
    46 		"zhiTuQm8momz5gP/FpNyIU6E0xOdvyv9G90Y0qLzQyWFCY9fo3uRvKtvQmp2A0vj\r\n"
    47 		"nI0rMBBnblLWLkLhTgEhsBSxi5/Emu2kzYXdhezb+IxuMrl7LhrZkFCSNnPWozJ7\r\n"
    48 		"gyEcxK9tPOyK/SCYY+s9vg6D3F8mdJIT8Vi/87v1hQjvehc+xtiY/nNadaSDPWOh\r\n"
    49 		"eWZ8FWVNRU3xowT5gyC75F8K+5IkhxsdmFGNznMzX9m+09fI515oVbDPe493JPNq\r\n"
    50 		"TjKOVq406EFGIJ5+DGd1IDStUZuZBl4Z79bz9uF/vKCnvFzskTW69NypoDOe3+XL\r\n"
    51 		"mP86vm3dUB3kWStHyWvH2RFbHnrbyHjZhQihsu8NdGBa3ZYatwlPhwVGaucSkrYM\r\n"
    52 		"vSEgKyN+7XqQQIQSv6ncxzoNlZ7Cgoyh0BeaTIjDVH/0H6oUrljGO8UwcNhEOxGH\r\n"
    53 		"WRBRI/emArMa8ro/tJ08jbFkRB1qiLSdfh0bufD0hWGGG16gXvvND4UgnVZ3VnzS\r\n"
    54 		"6QGNwgYIqTQqXgsj/9PkKVaoZp3OyldpS/tq93Y4cES9DjSAcxN9MVgPIdvkGvaa\r\n"
    55 		"MrKhpGu+prYEQJm1KfB4yTB+SuQOjngFiDXtsbN7jC7nCRpgQIjbCG3QKl8QUdrU\r\n"
    56 		"Uy3OHuaGJUgKwy330Hi9cfCKD3lCBHr7XhSbZ/spuZudB5/bv6doQlrR9W2ccMAF\r\n"
    57 		"RWsZAg7BXXzLvROgHCpcnNG/yHllQKpZ2REsUlbdNc2Sbw+tcjS1j18jGUwkSGhD\r\n"
    58 		"j0hVQSYVJ1DN17QYPHV9w7WvHHw9QgDK0eDIkcykVAQqB25U+H61wg3HrQ73CFj2\r\n"
    59 		"v0fyFYcSsCAj7LZnODLw6Mq+JzEIJUS1KPkJ4tiABXNCetwSUyJjaUlCfYHVE3Xi\r\n"
    60 		"OjgNm1QLqjt2adK1Yi0gj7Go4CIFS4UE03WZfQkEh6y16ZSivAEQ9Wzy8E+Uy5VX\r\n"
    61 		"diJQNPZ0VJozckp4wR+2Ao3yeBgQG9/zUbhihYBBGEPH6FmxscXcajssYrsbUh0D\r\n"
    62 		"3IyncNEwrneiP430RhkkF9yQb+xwNxvYE0B+11PhuvtBBukDQjLSKN21mR8vBYBS\r\n"
    63 		"kl1D/Z2Z388ObjPPXXGBpfWhhQyHKhPMJ0HMGUkCOf4IlmkH+vJt9a7Ex10URs1a\r\n"
    64 		"KuFTB5EeNGsI6lQaze+mai7On1hI/wEC51ul5n46c75iuLRLzSTxJ/gjwg0gdHHZ\r\n"
    65 		"5PLVTYfc58OXhiSDWhTvX+M+lWm49LQu+dCL3/pfLuR4D6Ytz/FDSFrSwNUQ7vOx\r\n"
    66 		"3MBVK7dshY5IbTF3jbLr0fkdX6wmXapgjMF7KYS8FqxNUC/IEC1mCgSiHZDCA8TT\r\n"
    67 		"WLXnnsGmB7eRAlYBpO2TAhhAhhg0aT+GhrvkZwROW4Keka+VGSt4R0+nKhrCuYkQ\r\n"
    68 		"Vo5t3qUtVAo2ic/ktisa4rNPND0XQAcCi58=\r\n"
    69 		"=vf0v\r\n"
    70 		"-----END PGP MESSAGE-----\r\n"
    71 		"\r\n";
    72 	
    73 	PEP_SESSION session;
    74 	PEP_STATUS status1 = init(&session);
    75 	assert(status1 == PEP_STATUS_OK);
    76 	assert(session);
    77 
    78 	char* plaintext = nullptr;
    79 	stringlist_t* keys_used = nullptr;
    80 	PEP_rating rating;
    81 	PEP_decrypt_flags_t dec_flags;
    82 	
    83 	PEP_STATUS status2 = MIME_decrypt_message(session, mimetext.c_str(), mimetext.length(),
    84 		&plaintext, &keys_used, &rating, &dec_flags);
    85 	
    86 	std::cout << "MIME_decrypt_message returned " << status2 << std::hex << " (0x" << status2 << ")" << endl;
    87 	
    88 	assert(status2 == PEP_STATUS_OK);
    89 	assert(plaintext);
    90 	
    91 	pEp_free(plaintext);
    92 }
    93 
    94 
    95 int main() {
    96     cout << "\n*** message_api_test ***\n\n";
    97     test_MIME_decrypt_message();
    98 
    99     PEP_SESSION session;
   100     
   101     cout << "calling init()\n";
   102     PEP_STATUS status1 = init(&session);
   103     assert(status1 == PEP_STATUS_OK);
   104     assert(session);
   105     cout << "init() completed.\n";
   106 
   107     // message_api test code
   108 
   109     cout << "creating message…\n";
   110     pEp_identity * me2 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
   111     // pEp_identity * me2 = new_identity("test@nokey.plop", NULL, PEP_OWN_USERID, "Test no key");
   112     identity_list *to2 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
   113     // identity_list *to2 = new_identity_list(new_identity("still@nokey.blup", NULL, "42", "Still no key"));
   114     message *msg2 = new_message(PEP_dir_outgoing);
   115     assert(msg2);
   116     msg2->from = me2;
   117     msg2->to = to2;
   118     msg2->shortmsg = strdup("hello, world");
   119     msg2->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   120     cout << "message created.\n";
   121 
   122     char *text2 = nullptr;
   123     PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
   124     assert(status2 == PEP_STATUS_OK);
   125     assert(text2);
   126 
   127     cout << "decrypted:\n\n";
   128     cout << text2 << "\n";
   129 
   130     free(text2);
   131 
   132     cout << "encrypting message as MIME multipart…\n";
   133     message *enc_msg2 = nullptr;
   134     cout << "calling encrypt_message()\n";
   135     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME, 0);
   136     cout << "encrypt_message() returns " << status2 << '.' << endl;
   137     assert(status2 == PEP_STATUS_OK);
   138     assert(enc_msg2);
   139     cout << "message encrypted.\n";
   140     
   141     status2 = mime_encode_message(enc_msg2, false, &text2);
   142     assert(status2 == PEP_STATUS_OK);
   143     assert(text2);
   144 
   145     cout << "encrypted:\n\n";
   146     cout << text2 << "\n";
   147 
   148     message *msg3 = nullptr;
   149     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
   150     assert(status3 == PEP_STATUS_OK);
   151     const string string3 = text2;
   152     //free(text2);
   153 
   154     unlink("msg4.asc");
   155     ofstream outFile3("msg4.asc");
   156     outFile3.write(string3.c_str(), string3.size());
   157     outFile3.close();
   158 
   159     message *msg4 = nullptr;
   160     stringlist_t *keylist4 = nullptr;
   161     PEP_rating rating;
   162     PEP_decrypt_flags_t flags;
   163     
   164     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &rating, &flags);
   165     assert(status4 == PEP_STATUS_OK);
   166     assert(msg4);
   167     assert(keylist4);
   168     assert(rating);
   169     PEP_comm_type ct = enc_msg2->from->comm_type;
   170     assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
   171 
   172     free_stringpair_list(enc_msg2->opt_fields);
   173     enc_msg2->opt_fields = NULL;
   174 
   175     cout << "keys used:";
   176 
   177     for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
   178     {
   179         cout << " " << kl4->value;
   180     }
   181     cout << "\n\n";
   182 
   183     free_stringlist(keylist4);
   184 
   185     cout << "opening msg_no_key.asc for reading\n";
   186     ifstream inFile3 ("msg_no_key.asc");
   187     assert(inFile3.is_open());
   188 
   189     string text3;
   190 
   191     cout << "reading msg_no_key.asc sample\n";
   192     while (!inFile3.eof()) {
   193         static string line;
   194         getline(inFile3, line);
   195         text3 += line + "\r\n";
   196     }
   197     inFile3.close();
   198 
   199     message *msg5 = nullptr;
   200     PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
   201     assert(status5 == PEP_STATUS_OK);
   202 
   203     message *msg6 = nullptr;
   204     stringlist_t *keylist5 = nullptr;
   205     PEP_rating rating2;
   206     PEP_decrypt_flags_t flags2;
   207     PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &rating2, &flags2);
   208     assert(status6 == PEP_DECRYPT_NO_KEY);
   209     assert(msg6 == NULL);
   210     assert(keylist5 == NULL);
   211     assert(rating2 == PEP_rating_have_no_key);
   212     cout << "rating :" << rating2 << "\n";
   213     free_stringlist(keylist5);
   214 
   215     cout << "\nTesting MIME_encrypt_message / MIME_decrypt_message...\n\n";
   216 
   217     cout << "opening alice_bob_encrypt_test_plaintext_mime.eml for reading\n";
   218     ifstream inFile4 ("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
   219     assert(inFile4.is_open());
   220     
   221     string text4;
   222     
   223     cout << "reading alice_bob_encrypt_test_plaintext_mime.eml sample\n";
   224     while (!inFile4.eof()) {
   225         static string line;
   226         getline(inFile4, line);
   227         text4 += line + "\r\n";
   228     }
   229     inFile4.close();
   230     
   231     const char* out_msg_plain = text4.c_str();
   232     
   233 //    const char* out_msg_plain = "From: krista@kgrothoff.org\nTo: Volker <vb@pep-project.org>\nSubject: Test\nContent-Type: text/plain; charset=utf-8\nContent-Language: en-US\nContent-Transfer-Encoding:quoted-printable\n\ngaga\n\n";
   234     char* enc_msg = NULL;
   235     char* dec_msg = NULL;
   236 
   237     PEP_STATUS status7 = MIME_encrypt_message(session, text4.c_str(), text4.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   238 //    PEP_STATUS status7 = MIME_encrypt_message(session, out_msg_plain, strlen(out_msg_plain), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   239     assert(status7 == PEP_STATUS_OK);
   240     
   241     cout << enc_msg << endl;
   242 
   243     string text5 = enc_msg;
   244     
   245     PEP_decrypt_flags_t dec_flags;
   246     stringlist_t* keys_used;
   247     
   248     PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags);
   249     assert(status8 == PEP_STATUS_OK);
   250     
   251     cout << dec_msg << endl;
   252     
   253     
   254     cout << "freeing messages…\n";
   255     free_message(msg4);
   256     free_message(msg3);
   257     free_message(msg2);
   258     free_message(enc_msg2);
   259     free_message(msg6);
   260     free_message(msg5);
   261     cout << "done.\n";
   262 
   263     free(enc_msg);
   264     free(dec_msg);
   265     cout << "calling release()\n";
   266     release(session);
   267     return 0;
   268 }