test/message_api_test.cc
author Edouard Tisserant <edouard@pep-project.org>
Mon, 24 Oct 2016 20:44:48 +0200
changeset 1329 876ad7615d75
parent 1006 0dc1800f8b2d
child 1325 5f2e643a4fd7
permissions -rw-r--r--
sync : fix encrypted sync messages not being injected when decrypted from non-sync session
     1 #include <stdlib.h>
     2 #include <string.h>
     3 #include "platform.h"
     4 #include <iostream>
     5 #include <fstream>
     6 #include <assert.h>
     7 #include "mime.h"
     8 #include "message_api.h"
     9 
    10 using namespace std;
    11 
    12 int main() {
    13     cout << "\n*** message_api_test ***\n\n";
    14 
    15     PEP_SESSION session;
    16     
    17     cout << "calling init()\n";
    18     PEP_STATUS status1 = init(&session);
    19     assert(status1 == PEP_STATUS_OK);
    20     assert(session);
    21     cout << "init() completed.\n";
    22 
    23     // message_api test code
    24 
    25     cout << "creating message…\n";
    26     pEp_identity * me2 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    27     // pEp_identity * me2 = new_identity("test@nokey.plop", NULL, PEP_OWN_USERID, "Test no key");
    28     me2->me = true;
    29     identity_list *to2 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
    30     // identity_list *to2 = new_identity_list(new_identity("still@nokey.blup", NULL, "42", "Still no key"));
    31     message *msg2 = new_message(PEP_dir_outgoing);
    32     assert(msg2);
    33     msg2->from = me2;
    34     msg2->to = to2;
    35     msg2->shortmsg = strdup("hello, world");
    36     msg2->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    37     cout << "message created.\n";
    38 
    39     char *text2 = nullptr;
    40     PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
    41     assert(status2 == PEP_STATUS_OK);
    42     assert(text2);
    43 
    44     cout << "decrypted:\n\n";
    45     cout << text2 << "\n";
    46 
    47     free(text2);
    48 
    49     cout << "encrypting message as MIME multipart…\n";
    50     message *enc_msg2 = nullptr;
    51     cout << "calling encrypt_message()\n";
    52     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME, 0);
    53     cout << "encrypt_message() returns " << status2 << '.' << endl;
    54     assert(status2 == PEP_STATUS_OK);
    55     assert(enc_msg2);
    56     cout << "message encrypted.\n";
    57     
    58     status2 = mime_encode_message(enc_msg2, false, &text2);
    59     assert(status2 == PEP_STATUS_OK);
    60     assert(text2);
    61 
    62     cout << "encrypted:\n\n";
    63     cout << text2 << "\n";
    64 
    65     message *msg3 = nullptr;
    66     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
    67     assert(status3 == PEP_STATUS_OK);
    68     const string string3 = text2;
    69     //free(text2);
    70 
    71     unlink("msg4.asc");
    72     ofstream outFile3("msg4.asc");
    73     outFile3.write(string3.c_str(), string3.size());
    74     outFile3.close();
    75 
    76     message *msg4 = nullptr;
    77     stringlist_t *keylist4 = nullptr;
    78     PEP_rating rating;
    79     PEP_decrypt_flags_t flags;
    80     
    81     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &rating, &flags);
    82     assert(status4 == PEP_STATUS_OK);
    83     assert(msg4);
    84     assert(keylist4);
    85     assert(rating);
    86     PEP_comm_type ct = enc_msg2->from->comm_type;
    87     assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
    88 
    89     free_stringpair_list(enc_msg2->opt_fields);
    90     enc_msg2->opt_fields = NULL;
    91 
    92     cout << "keys used:";
    93 
    94     for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    95     {
    96         cout << " " << kl4->value;
    97     }
    98     cout << "\n\n";
    99 
   100     free_stringlist(keylist4);
   101 
   102     cout << "opening msg_no_key.asc for reading\n";
   103     ifstream inFile3 ("msg_no_key.asc");
   104     assert(inFile3.is_open());
   105 
   106     string text3;
   107 
   108     cout << "reading msg_no_key.asc sample\n";
   109     while (!inFile3.eof()) {
   110         static string line;
   111         getline(inFile3, line);
   112         text3 += line + "\r\n";
   113     }
   114     inFile3.close();
   115 
   116     message *msg5 = nullptr;
   117     PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
   118     assert(status5 == PEP_STATUS_OK);
   119 
   120     message *msg6 = nullptr;
   121     stringlist_t *keylist5 = nullptr;
   122     PEP_rating rating2;
   123     PEP_decrypt_flags_t flags2;
   124     PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &rating2, &flags2);
   125     assert(status6 == PEP_DECRYPT_NO_KEY);
   126     assert(msg6 == NULL);
   127     assert(keylist5 == NULL);
   128     assert(rating2 == PEP_rating_have_no_key);
   129     cout << "rating :" << rating2 << "\n";
   130     free_stringlist(keylist5);
   131 
   132     cout << "freeing messages…\n";
   133     free_message(msg4);
   134     free_message(msg3);
   135     free_message(msg2);
   136     free_message(enc_msg2);
   137     free_message(msg6);
   138     free_message(msg5);
   139     cout << "done.\n";
   140 
   141     cout << "calling release()\n";
   142     release(session);
   143     return 0;
   144 }