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