test/message_api_test.cc
author Edouard Tisserant
Tue, 21 Jun 2016 15:33:06 +0200
changeset 747 d357dceebec6
parent 746 753b1ec63285
child 756 8a06c2465017
child 759 d7efa7b697bd
permissions -rw-r--r--
Support for 0b attachment
     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     assert(status2 == PEP_STATUS_OK);
    54     assert(enc_msg2);
    55     cout << "message encrypted.\n";
    56     
    57     status2 = mime_encode_message(enc_msg2, false, &text2);
    58     assert(status2 == PEP_STATUS_OK);
    59     assert(text2);
    60 
    61     cout << "encrypted:\n\n";
    62     cout << text2 << "\n";
    63 
    64     message *msg3;
    65     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
    66     assert(status3 == PEP_STATUS_OK);
    67     string string3 = text2;
    68     //free(text2);
    69 
    70     unlink("msg4.asc");
    71     ofstream outFile3("msg4.asc");
    72     outFile3.write(string3.c_str(), string3.size());
    73     outFile3.close();
    74 
    75     message *msg4;
    76     stringlist_t *keylist4;
    77     PEP_color color;
    78     PEP_decrypt_flags_t flags;
    79     
    80     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &color, &flags);
    81     assert(status4 == PEP_STATUS_OK);
    82     assert(msg4);
    83     assert(keylist4);
    84     assert(color);
    85 
    86     cout << "keys used:";
    87     stringlist_t *kl4;
    88     for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    89         cout << " " << kl4->value;
    90     cout << "\n\n";
    91 
    92     free_stringlist(keylist4);
    93 
    94     cout << "opening msg_no_key.asc for reading\n";
    95     ifstream inFile3 ("msg_no_key.asc");
    96     assert(inFile3.is_open());
    97 
    98     string text3;
    99 
   100     cout << "reading msg_no_key.asc sample\n";
   101     while (!inFile3.eof()) {
   102         static string line;
   103         getline(inFile3, line);
   104         text3 += line + "\r\n";
   105     }
   106     inFile3.close();
   107 
   108     message *msg5;
   109     PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
   110     assert(status5 == PEP_STATUS_OK);
   111 
   112     message *msg6;
   113     stringlist_t *keylist5;
   114     PEP_color color2;
   115     PEP_decrypt_flags_t flags2;
   116     PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &color2, &flags2);
   117     assert(status6 == PEP_DECRYPT_NO_KEY);
   118     assert(msg6 == NULL);
   119     assert(keylist5 == NULL);
   120     assert(color2 == PEP_rating_have_no_key);
   121     cout << "color :" << color2 << "\n";
   122     free_stringlist(keylist5);
   123 
   124     cout << "freeing messages…\n";
   125     free_message(msg4);
   126     free_message(msg3);
   127     free_message(msg2);
   128     free_message(enc_msg2);
   129     free_message(msg6);
   130     free_message(msg5);
   131     cout << "done.\n";
   132 
   133     cout << "calling release()\n";
   134     release(session);
   135     return 0;
   136 }
   137