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