test/message_api_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 01 Jun 2018 10:30:19 +0200
branchENGINE-233
changeset 2727 b8b0443804da
parent 1945 9503c962f746
child 2288 8aee10631677
child 2332 23ed7bbd53f4
permissions -rw-r--r--
close branch
     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 int main() {
    16     cout << "\n*** message_api_test ***\n\n";
    17 
    18     PEP_SESSION session;
    19     
    20     cout << "calling init()\n";
    21     PEP_STATUS status1 = init(&session);
    22     assert(status1 == PEP_STATUS_OK);
    23     assert(session);
    24     cout << "init() completed.\n";
    25 
    26     // message_api test code
    27 
    28     cout << "creating message…\n";
    29     pEp_identity * me2 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    30     // pEp_identity * me2 = new_identity("test@nokey.plop", NULL, PEP_OWN_USERID, "Test no key");
    31     identity_list *to2 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
    32     // identity_list *to2 = new_identity_list(new_identity("still@nokey.blup", NULL, "42", "Still no key"));
    33     message *msg2 = new_message(PEP_dir_outgoing);
    34     assert(msg2);
    35     msg2->from = me2;
    36     msg2->to = to2;
    37     msg2->shortmsg = strdup("hello, world");
    38     msg2->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    39     cout << "message created.\n";
    40 
    41     char *text2 = nullptr;
    42     PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
    43     assert(status2 == PEP_STATUS_OK);
    44     assert(text2);
    45 
    46     cout << "decrypted:\n\n";
    47     cout << text2 << "\n";
    48 
    49     free(text2);
    50 
    51     cout << "encrypting message as MIME multipart…\n";
    52     message *enc_msg2 = nullptr;
    53     cout << "calling encrypt_message()\n";
    54     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME, 0);
    55     cout << "encrypt_message() returns " << status2 << '.' << endl;
    56     assert(status2 == PEP_STATUS_OK);
    57     assert(enc_msg2);
    58     cout << "message encrypted.\n";
    59     
    60     status2 = mime_encode_message(enc_msg2, false, &text2);
    61     assert(status2 == PEP_STATUS_OK);
    62     assert(text2);
    63 
    64     cout << "encrypted:\n\n";
    65     cout << text2 << "\n";
    66 
    67     message *msg3 = nullptr;
    68     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
    69     assert(status3 == PEP_STATUS_OK);
    70     const string string3 = text2;
    71     //free(text2);
    72 
    73     unlink("msg4.asc");
    74     ofstream outFile3("msg4.asc");
    75     outFile3.write(string3.c_str(), string3.size());
    76     outFile3.close();
    77 
    78     message *msg4 = nullptr;
    79     stringlist_t *keylist4 = nullptr;
    80     PEP_rating rating;
    81     PEP_decrypt_flags_t flags;
    82     
    83     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &rating, &flags);
    84     assert(status4 == PEP_STATUS_OK);
    85     assert(msg4);
    86     assert(keylist4);
    87     assert(rating);
    88     PEP_comm_type ct = enc_msg2->from->comm_type;
    89     assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
    90 
    91     free_stringpair_list(enc_msg2->opt_fields);
    92     enc_msg2->opt_fields = NULL;
    93 
    94     cout << "keys used:";
    95 
    96     for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    97     {
    98         cout << " " << kl4->value;
    99     }
   100     cout << "\n\n";
   101 
   102     free_stringlist(keylist4);
   103 
   104     cout << "opening msg_no_key.asc for reading\n";
   105     ifstream inFile3 ("msg_no_key.asc");
   106     assert(inFile3.is_open());
   107 
   108     string text3;
   109 
   110     cout << "reading msg_no_key.asc sample\n";
   111     while (!inFile3.eof()) {
   112         static string line;
   113         getline(inFile3, line);
   114         text3 += line + "\r\n";
   115     }
   116     inFile3.close();
   117 
   118     message *msg5 = nullptr;
   119     PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
   120     assert(status5 == PEP_STATUS_OK);
   121 
   122     message *msg6 = nullptr;
   123     stringlist_t *keylist5 = nullptr;
   124     PEP_rating rating2;
   125     PEP_decrypt_flags_t flags2;
   126     PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &rating2, &flags2);
   127     assert(status6 == PEP_DECRYPT_NO_KEY);
   128     assert(msg6 == NULL);
   129     assert(keylist5 == NULL);
   130     assert(rating2 == PEP_rating_have_no_key);
   131     cout << "rating :" << rating2 << "\n";
   132     free_stringlist(keylist5);
   133 
   134     cout << "\nTesting MIME_encrypt_message / MIME_decrypt_message...\n\n";
   135 
   136     cout << "opening alice_bob_encrypt_test_plaintext_mime.eml for reading\n";
   137     ifstream inFile4 ("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
   138     assert(inFile4.is_open());
   139     
   140     string text4;
   141     
   142     cout << "reading alice_bob_encrypt_test_plaintext_mime.eml sample\n";
   143     while (!inFile4.eof()) {
   144         static string line;
   145         getline(inFile4, line);
   146         text4 += line + "\r\n";
   147     }
   148     inFile4.close();
   149     
   150     const char* out_msg_plain = text4.c_str();
   151     
   152 //    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";
   153     char* enc_msg = NULL;
   154     char* dec_msg = NULL;
   155 
   156     PEP_STATUS status7 = MIME_encrypt_message(session, text4.c_str(), text4.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   157 //    PEP_STATUS status7 = MIME_encrypt_message(session, out_msg_plain, strlen(out_msg_plain), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   158     assert(status7 == PEP_STATUS_OK);
   159     
   160     cout << enc_msg << endl;
   161 
   162     string text5 = enc_msg;
   163     
   164     PEP_decrypt_flags_t dec_flags;
   165     stringlist_t* keys_used;
   166     
   167     PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags);
   168     assert(status8 == PEP_STATUS_OK);
   169     
   170     cout << dec_msg << endl;
   171     
   172     
   173     cout << "freeing messages…\n";
   174     free_message(msg4);
   175     free_message(msg3);
   176     free_message(msg2);
   177     free_message(enc_msg2);
   178     free_message(msg6);
   179     free_message(msg5);
   180     cout << "done.\n";
   181 
   182     free(enc_msg);
   183     free(dec_msg);
   184     cout << "calling release()\n";
   185     release(session);
   186     return 0;
   187 }