test/message_api_test.cc
author Volker Birk <vb@pep-project.org>
Tue, 12 May 2015 13:42:48 +0200
changeset 267 31d89e1b5e08
parent 260 75481f577114
child 281 9f7691853773
permissions -rw-r--r--
return value of decrypt_message()
     1 #include "platform.h"
     2 #include <iostream>
     3 #include <fstream>
     4 #include <assert.h>
     5 #include "mime.h"
     6 #include "message_api.h"
     7 
     8 using namespace std;
     9 
    10 int main() {
    11     cout << "\n*** message_api_test ***\n\n";
    12 
    13     PEP_SESSION session;
    14     
    15     cout << "calling init()\n";
    16     PEP_STATUS status1 = init(&session);   
    17     assert(status1 == PEP_STATUS_OK);
    18     assert(session);
    19     cout << "init() completed.\n";
    20 
    21     // message_api test code
    22 
    23     cout << "creating message…\n";
    24     pEp_identity * me2 = new_identity("outlooktest@dingens.org", NULL, "23", "Outlook Test");
    25     me2->me = true;
    26     identity_list *to2 = new_identity_list(new_identity("vb@dingens.org", NULL, "42", "Volker Birk"));
    27     message *msg2 = new_message(PEP_dir_outgoing, me2, to2, "hello, world");
    28     assert(msg2);
    29     cout << "message created.\n";
    30 
    31     char *text2;
    32     PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
    33     assert(status2 == PEP_STATUS_OK);
    34     assert(text2);
    35 
    36     cout << "decrypted:\n\n";
    37     cout << text2 << "\n";
    38 
    39     free(text2);
    40 
    41     cout << "encrypting message as MIME multipart…\n";
    42     message *enc_msg2;
    43     cout << "calling encrypt_message()\n";
    44     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME);
    45     assert(status2 == PEP_STATUS_OK);
    46     assert(enc_msg2);
    47     cout << "message encrypted.\n";
    48     
    49     status2 = mime_encode_message(enc_msg2, false, &text2);
    50     assert(status2 == PEP_STATUS_OK);
    51     assert(text2);
    52 
    53     cout << "encrypted:\n\n";
    54     cout << text2 << "\n";
    55 
    56     message *msg3;
    57     PEP_STATUS status3 = mime_decode_message(text2, &msg3);
    58     assert(status3 == PEP_STATUS_OK);
    59     string string3 = text2;
    60     free(text2);
    61 
    62     unlink("msg4.asc");
    63     ofstream outFile3("msg4.asc");
    64     outFile3.write(string3.c_str(), string3.size());
    65     outFile3.close();
    66 
    67     message *msg4;
    68     stringlist_t *keylist4;
    69     PEP_color color;
    70     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &color);
    71     assert(status4 == PEP_STATUS_OK);
    72     assert(msg4);
    73     assert(keylist4);
    74     assert(color);
    75 
    76     cout << "keys used:";
    77     stringlist_t *kl4;
    78     for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    79         cout << " " << kl4->value;
    80     cout << "\n\n";
    81 
    82     free_stringlist(keylist4);
    83 
    84     cout << "freeing messages…\n";
    85     free_message(msg4);
    86     free_message(msg3);
    87     free_message(msg2);
    88     free_message(enc_msg2);
    89     cout << "done.\n";
    90 
    91     cout << "calling release()\n";
    92     release(session);
    93     return 0;
    94 }
    95