test/message_api_test.cc
author Roker <roker@pep-project.org>
Wed, 22 Jun 2016 16:59:07 +0200
branchnull_size_attachment
changeset 756 8a06c2465017
parent 747 d357dceebec6
child 771 c04f54a73ef8
permissions -rw-r--r--
print out the status before assert() crashes the program
     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("outlooktest@dingens.org", NULL, PEP_OWN_USERID, "Outlook 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("vb@dingens.org", NULL, "42", "Volker Birk"));
    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;
    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;
    51     cout << "calling encrypt_message()\n";
    52     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME);
    53     cout << "encrypt_message returned with status " << 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;
    66     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
    67     assert(status3 == PEP_STATUS_OK);
    68     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;
    77     stringlist_t *keylist4;
    78     PEP_color color;
    79     PEP_decrypt_flags_t flags;
    80     
    81     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &color, &flags);
    82     assert(status4 == PEP_STATUS_OK);
    83     assert(msg4);
    84     assert(keylist4);
    85     assert(color);
    86 
    87     cout << "keys used:";
    88     stringlist_t *kl4;
    89     for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    90         cout << " " << kl4->value;
    91     cout << "\n\n";
    92 
    93     free_stringlist(keylist4);
    94 
    95     cout << "opening msg_no_key.asc for reading\n";
    96     ifstream inFile3 ("msg_no_key.asc");
    97     assert(inFile3.is_open());
    98 
    99     string text3;
   100 
   101     cout << "reading msg_no_key.asc sample\n";
   102     while (!inFile3.eof()) {
   103         static string line;
   104         getline(inFile3, line);
   105         text3 += line + "\r\n";
   106     }
   107     inFile3.close();
   108 
   109     message *msg5;
   110     PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
   111     assert(status5 == PEP_STATUS_OK);
   112 
   113     message *msg6;
   114     stringlist_t *keylist5;
   115     PEP_color color2;
   116     PEP_decrypt_flags_t flags2;
   117     PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &color2, &flags2);
   118     assert(status6 == PEP_DECRYPT_NO_KEY);
   119     assert(msg6 == NULL);
   120     assert(keylist5 == NULL);
   121     assert(color2 == PEP_rating_have_no_key);
   122     cout << "color :" << color2 << "\n";
   123     free_stringlist(keylist5);
   124 
   125     cout << "freeing messages…\n";
   126     free_message(msg4);
   127     free_message(msg3);
   128     free_message(msg2);
   129     free_message(enc_msg2);
   130     free_message(msg6);
   131     free_message(msg5);
   132     cout << "done.\n";
   133 
   134     cout << "calling release()\n";
   135     release(session);
   136     return 0;
   137 }
   138