test/message_api_test.cc
author Edouard Tisserant
Thu, 05 Nov 2015 18:14:06 +0100
changeset 381 87e3895c327f
parent 345 9e129a270a46
child 678 f8181e59ae2f
permissions -rw-r--r--
Change test pub keys file names to match usual namimg in attachements
     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, "23", "Outlook Test");
    27     me2->me = true;
    28     identity_list *to2 = new_identity_list(new_identity("vb@dingens.org", NULL, "42", "Volker Birk"));
    29     message *msg2 = new_message(PEP_dir_outgoing);
    30     assert(msg2);
    31     msg2->from = me2;
    32     msg2->to = to2;
    33     msg2->shortmsg = strdup("hello, world");
    34     cout << "message created.\n";
    35 
    36     char *text2;
    37     PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
    38     assert(status2 == PEP_STATUS_OK);
    39     assert(text2);
    40 
    41     cout << "decrypted:\n\n";
    42     cout << text2 << "\n";
    43 
    44     free(text2);
    45 
    46     cout << "encrypting message as MIME multipart…\n";
    47     message *enc_msg2;
    48     cout << "calling encrypt_message()\n";
    49     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME);
    50     assert(status2 == PEP_STATUS_OK);
    51     assert(enc_msg2);
    52     cout << "message encrypted.\n";
    53     
    54     status2 = mime_encode_message(enc_msg2, false, &text2);
    55     assert(status2 == PEP_STATUS_OK);
    56     assert(text2);
    57 
    58     cout << "encrypted:\n\n";
    59     cout << text2 << "\n";
    60 
    61     message *msg3;
    62     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
    63     assert(status3 == PEP_STATUS_OK);
    64     string string3 = text2;
    65     free(text2);
    66 
    67     unlink("msg4.asc");
    68     ofstream outFile3("msg4.asc");
    69     outFile3.write(string3.c_str(), string3.size());
    70     outFile3.close();
    71 
    72     message *msg4;
    73     stringlist_t *keylist4;
    74     PEP_color color;
    75     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &color);
    76     assert(status4 == PEP_STATUS_OK);
    77     assert(msg4);
    78     assert(keylist4);
    79     assert(color);
    80 
    81     cout << "keys used:";
    82     stringlist_t *kl4;
    83     for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    84         cout << " " << kl4->value;
    85     cout << "\n\n";
    86 
    87     free_stringlist(keylist4);
    88 
    89     cout << "freeing messages…\n";
    90     free_message(msg4);
    91     free_message(msg3);
    92     free_message(msg2);
    93     free_message(enc_msg2);
    94     cout << "done.\n";
    95 
    96     cout << "calling release()\n";
    97     release(session);
    98     return 0;
    99 }
   100