test/message_api_test.cc
author Edouard Tisserant
Mon, 20 Jun 2016 13:10:01 +0200
changeset 746 753b1ec63285
parent 745 32d7952cccfa
parent 728 80b966277620
child 747 d357dceebec6
permissions -rw-r--r--
Merged import_own_key
     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     cout << "message created.\n";
    37 
    38     char *text2;
    39     PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
    40     assert(status2 == PEP_STATUS_OK);
    41     assert(text2);
    42 
    43     cout << "decrypted:\n\n";
    44     cout << text2 << "\n";
    45 
    46     free(text2);
    47 
    48     cout << "encrypting message as MIME multipart…\n";
    49     message *enc_msg2;
    50     cout << "calling encrypt_message()\n";
    51     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME);
    52     assert(status2 == PEP_STATUS_OK);
    53     assert(enc_msg2);
    54     cout << "message encrypted.\n";
    55     
    56     status2 = mime_encode_message(enc_msg2, false, &text2);
    57     assert(status2 == PEP_STATUS_OK);
    58     assert(text2);
    59 
    60     cout << "encrypted:\n\n";
    61     cout << text2 << "\n";
    62 
    63     message *msg3;
    64     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
    65     assert(status3 == PEP_STATUS_OK);
    66     string string3 = text2;
    67     //free(text2);
    68 
    69     unlink("msg4.asc");
    70     ofstream outFile3("msg4.asc");
    71     outFile3.write(string3.c_str(), string3.size());
    72     outFile3.close();
    73 
    74     message *msg4;
    75     stringlist_t *keylist4;
    76     PEP_color color;
    77     PEP_decrypt_flags_t flags;
    78     
    79     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &color, &flags);
    80     assert(status4 == PEP_STATUS_OK);
    81     assert(msg4);
    82     assert(keylist4);
    83     assert(color);
    84 
    85     cout << "keys used:";
    86     stringlist_t *kl4;
    87     for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    88         cout << " " << kl4->value;
    89     cout << "\n\n";
    90 
    91     free_stringlist(keylist4);
    92 
    93     cout << "opening msg_no_key.asc for reading\n";
    94     ifstream inFile3 ("msg_no_key.asc");
    95     assert(inFile3.is_open());
    96 
    97     string text3;
    98 
    99     cout << "reading msg_no_key.asc sample\n";
   100     while (!inFile3.eof()) {
   101         static string line;
   102         getline(inFile3, line);
   103         text3 += line + "\r\n";
   104     }
   105     inFile3.close();
   106 
   107     message *msg5;
   108     PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
   109     assert(status5 == PEP_STATUS_OK);
   110 
   111     message *msg6;
   112     stringlist_t *keylist5;
   113     PEP_color color2;
   114     PEP_decrypt_flags_t flags2;
   115     PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &color2, &flags2);
   116     assert(status6 == PEP_DECRYPT_NO_KEY);
   117     assert(msg6 == NULL);
   118     assert(keylist5 == NULL);
   119     assert(color2 == PEP_rating_have_no_key);
   120     cout << "color :" << color2 << "\n";
   121     free_stringlist(keylist5);
   122 
   123     cout << "freeing messages…\n";
   124     free_message(msg4);
   125     free_message(msg3);
   126     free_message(msg2);
   127     free_message(enc_msg2);
   128     free_message(msg6);
   129     free_message(msg5);
   130     cout << "done.\n";
   131 
   132     cout << "calling release()\n";
   133     release(session);
   134     return 0;
   135 }
   136