test/src/engine_tests/MessageApiTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 26 Feb 2019 08:53:05 +0100
branchsync_test_refactor
changeset 3321 528a262bd1f0
parent 3318 0d482a4f1d18
parent 3319 b36b64dfcf0d
child 3335 09af2cf0ac74
permissions -rw-r--r--
merged in sync
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include "TestConstants.h"
     6 #include <string>
     7 #include <cstring>
     8 #include <iostream>
     9 #include <fstream>
    10 
    11 #include "pEpEngine.h"
    12 #include "platform.h"
    13 #include "mime.h"
    14 #include "message_api.h"
    15 #include "TestUtils.h"
    16 
    17 #include <cpptest.h>
    18 #include "EngineTestSessionSuite.h"
    19 #include "MessageApiTests.h"
    20 
    21 using namespace std;
    22 
    23 MessageApiTests::MessageApiTests(string suitename, string test_home_dir) :
    24     EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    25     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("MessageApiTests::check_message_api"),
    26                                                                       static_cast<Func>(&MessageApiTests::check_message_api)));
    27 }
    28 
    29 void MessageApiTests::check_message_api() {
    30     cout << "Importing Alice's key " << endl;
    31     const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    32     const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    33     const string bob_pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    34 
    35     PEP_STATUS status0 = import_key(session, alice_pub_key.c_str(), alice_pub_key.size(), NULL);
    36     TEST_ASSERT_MSG((status0 == PEP_TEST_KEY_IMPORT_SUCCESS), "status0 == PEP_STATUS_OK");
    37     status0 = import_key(session, alice_priv_key.c_str(), alice_priv_key.size(), NULL);
    38     TEST_ASSERT_MSG((status0 == PEP_TEST_KEY_IMPORT_SUCCESS), "status0 == PEP_STATUS_OK");
    39     status0 = import_key(session, bob_pub_key.c_str(), bob_pub_key.size(), NULL);
    40     TEST_ASSERT_MSG((status0 == PEP_TEST_KEY_IMPORT_SUCCESS), "status0 == PEP_STATUS_OK");
    41     // message_api test code
    42 
    43     cout << "creating message…\n";
    44     pEp_identity * me2 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    45     // pEp_identity * me2 = new_identity("test@nokey.plop", NULL, PEP_OWN_USERID, "Test no key");
    46     me2->me = true;
    47     identity_list *to2 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
    48     // identity_list *to2 = new_identity_list(new_identity("still@nokey.blup", NULL, "42", "Still no key"));
    49     message *msg2 = new_message(PEP_dir_outgoing);
    50     TEST_ASSERT_MSG((msg2), "msg2");
    51     msg2->from = me2;
    52     msg2->to = to2;
    53     msg2->shortmsg = strdup("hello, world");
    54     msg2->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    55     cout << "message created.\n";
    56 
    57     char *text2 = nullptr;
    58     PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
    59     TEST_ASSERT_MSG((status2 == PEP_STATUS_OK), "status2 == PEP_STATUS_OK");
    60     TEST_ASSERT_MSG((text2), "text2");
    61 
    62     cout << "decrypted:\n\n";
    63     cout << text2 << "\n";
    64 
    65     free(text2);
    66 
    67     cout << "encrypting message as MIME multipart…\n";
    68     message *enc_msg2 = nullptr;
    69     cout << "calling encrypt_message()\n";
    70     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME, 0);
    71     cout << "encrypt_message() returns " << status2 << '.' << endl;
    72     TEST_ASSERT_MSG((status2 == PEP_STATUS_OK), "status2 == PEP_STATUS_OK");
    73     TEST_ASSERT_MSG((enc_msg2), "enc_msg2");
    74     cout << "message encrypted.\n";
    75     
    76     status2 = mime_encode_message(enc_msg2, false, &text2);
    77     TEST_ASSERT_MSG((status2 == PEP_STATUS_OK), "status2 == PEP_STATUS_OK");
    78     TEST_ASSERT_MSG((text2), "text2");
    79 
    80     cout << "encrypted:\n\n";
    81     cout << text2 << "\n";
    82 
    83     message *msg3 = nullptr;
    84     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
    85     TEST_ASSERT_MSG((status3 == PEP_STATUS_OK), "status3 == PEP_STATUS_OK");
    86     const string string3 = text2;
    87     //free(text2);
    88 
    89     unlink("msg4.asc");
    90     ofstream outFile3("msg4.asc");
    91     outFile3.write(string3.c_str(), string3.size());
    92     outFile3.close();
    93 
    94     message *msg4 = nullptr;
    95     stringlist_t *keylist4 = nullptr;
    96     PEP_rating rating;
    97     PEP_decrypt_flags_t flags;
    98     
    99     flags = 0;
   100     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &rating, &flags);
   101     TEST_ASSERT_MSG((status4 == PEP_STATUS_OK), tl_status_string(status4));
   102     TEST_ASSERT_MSG((msg4), "msg4");
   103     TEST_ASSERT_MSG((keylist4), "keylist4");
   104     TEST_ASSERT_MSG((rating), "rating");
   105     PEP_comm_type ct = enc_msg2->from->comm_type;
   106     TEST_ASSERT_MSG((ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed ), "ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed ");
   107 
   108     free_stringpair_list(enc_msg2->opt_fields);
   109     enc_msg2->opt_fields = NULL;
   110 
   111     cout << "keys used:";
   112 
   113     for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
   114     {
   115         cout << " " << kl4->value;
   116     }
   117     cout << "\n\n";
   118 
   119     free_stringlist(keylist4);
   120 
   121     cout << "opening msg_no_key.asc for reading\n";
   122     ifstream inFile3 ("msg_no_key.asc");
   123     TEST_ASSERT_MSG((inFile3.is_open()), "inFile3.is_open()");
   124 
   125     string text3;
   126 
   127     cout << "reading msg_no_key.asc sample\n";
   128     while (!inFile3.eof()) {
   129         static string line;
   130         getline(inFile3, line);
   131         text3 += line + "\r\n";
   132     }
   133     inFile3.close();
   134 
   135     message *msg5 = nullptr;
   136     PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
   137     TEST_ASSERT_MSG((status5 == PEP_STATUS_OK), "status5 == PEP_STATUS_OK");
   138 
   139     message *msg6 = nullptr;
   140     stringlist_t *keylist5 = nullptr;
   141     PEP_rating rating2;
   142     PEP_decrypt_flags_t flags2;
   143     flags2 = 0;
   144     PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &rating2, &flags2);
   145     TEST_ASSERT_MSG((status6 == PEP_DECRYPT_NO_KEY), tl_status_string(status6));
   146     TEST_ASSERT_MSG((msg6 == NULL), "msg6 == NULL");
   147     TEST_ASSERT_MSG((keylist5 == NULL), "keylist5 == NULL");
   148     TEST_ASSERT_MSG((rating2 == PEP_rating_have_no_key), "rating2 == PEP_rating_have_no_key");
   149     cout << "rating :" << rating2 << "\n";
   150     free_stringlist(keylist5);
   151 
   152     cout << "\nTesting MIME_encrypt_message / MIME_decrypt_message...\n\n";
   153 
   154     cout << "opening alice_bob_encrypt_test_plaintext_mime.eml for reading\n";
   155     ifstream inFile4 ("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
   156     TEST_ASSERT_MSG((inFile4.is_open()), "inFile4.is_open()");
   157     
   158     string text4;
   159     
   160     cout << "reading alice_bob_encrypt_test_plaintext_mime.eml sample\n";
   161     while (!inFile4.eof()) {
   162         static string line;
   163         getline(inFile4, line);
   164         text4 += line + "\r\n";
   165     }
   166     inFile4.close();
   167     
   168     const char* out_msg_plain = text4.c_str();
   169     
   170 //    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";
   171     char* enc_msg = NULL;
   172     char* dec_msg = NULL;
   173 
   174     PEP_STATUS status7 = MIME_encrypt_message(session, text4.c_str(), text4.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   175 //    PEP_STATUS status7 = MIME_encrypt_message(session, out_msg_plain, strlen(out_msg_plain), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   176     TEST_ASSERT_MSG((status7 == PEP_STATUS_OK), "status7 == PEP_STATUS_OK");
   177     
   178     cout << enc_msg << endl;
   179 
   180     string text5 = enc_msg;
   181     
   182     PEP_decrypt_flags_t dec_flags;
   183     stringlist_t* keys_used;
   184     
   185     dec_flags = 0;
   186     char* modified_src = NULL;
   187     PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags, &modified_src);
   188     TEST_ASSERT_MSG((status8 == PEP_STATUS_OK), "status8 == PEP_STATUS_OK");
   189     
   190     cout << dec_msg << endl;
   191     
   192     cout << "\nTesting encrypt_message() with enc_format = PEP_enc_none\n\n";
   193 
   194     message *msg7 = new_message(PEP_dir_outgoing);
   195     pEp_identity * me7 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
   196     identity_list *to7 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
   197     msg7->from = me7;
   198     msg7->to = to7;
   199     msg7->shortmsg = strdup("My Subject");
   200     msg7->longmsg = strdup("This is some text.\n");
   201 
   202     message *enc7 = nullptr;
   203     PEP_STATUS status9 = encrypt_message(session, msg7, NULL, &enc7, PEP_enc_none, 0);
   204 	std::cout << "encrypt_message returned " << std::dec << status9 << std::hex << " (0x" << status9 << ")" << std::dec << endl;
   205     TEST_ASSERT_MSG((status9 == PEP_UNENCRYPTED), "status9 == PEP_UNENCRYPTED");
   206     TEST_ASSERT_MSG((enc7 == nullptr), "enc7 == nullptr");
   207     TEST_ASSERT_MSG((msg7->shortmsg && msg7->longmsg), "msg7->shortmsg && msg7->longmsg");
   208     cout << msg7->shortmsg << "\n";
   209     cout << msg7->longmsg << "\n";
   210     TEST_ASSERT_MSG((strcmp(msg7->shortmsg, "My Subject") == 0), "strcmp(msg7->shortmsg, \"My Subject\") == 0");
   211     TEST_ASSERT_MSG((strcmp(msg7->longmsg, "This is some text.\n") == 0), "strcmp(msg7->longmsg, \"This is some text.\n\") == 0");
   212     
   213     cout << "\nfreeing messages…\n";
   214     free_message(msg7);
   215     free_message(msg6);
   216     free_message(msg5);
   217     free_message(msg4);
   218     free_message(msg3);
   219     free_message(msg2);
   220     free_message(enc_msg2);
   221     cout << "done.\n";
   222 
   223     free(enc_msg);
   224     free(dec_msg);    
   225 }