test/message_api_test.cc
author Edouard Tisserant
Wed, 29 Jun 2016 12:10:15 +0200
changeset 771 c04f54a73ef8
parent 759 d7efa7b697bd
parent 756 8a06c2465017
child 781 21d47c8ae0ae
permissions -rw-r--r--
merged 0b_attachment branch
     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 = nullptr;
    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 = nullptr;
    51     cout << "calling encrypt_message()\n";
    52     status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME);
    53     cout << "encrypt_message() returns " << 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 = nullptr;
    66     PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
    67     assert(status3 == PEP_STATUS_OK);
    68     const 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 = nullptr;
    77     stringlist_t *keylist4 = nullptr;
    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     for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    89     {
    90         cout << " " << kl4->value;
    91     }
    92     cout << "\n\n";
    93 
    94     free_stringlist(keylist4);
    95 
    96     cout << "opening msg_no_key.asc for reading\n";
    97     ifstream inFile3 ("msg_no_key.asc");
    98     assert(inFile3.is_open());
    99 
   100     string text3;
   101 
   102     cout << "reading msg_no_key.asc sample\n";
   103     while (!inFile3.eof()) {
   104         static string line;
   105         getline(inFile3, line);
   106         text3 += line + "\r\n";
   107     }
   108     inFile3.close();
   109 
   110     message *msg5 = nullptr;
   111     PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
   112     assert(status5 == PEP_STATUS_OK);
   113 
   114     message *msg6 = nullptr;
   115     stringlist_t *keylist5 = nullptr;
   116     PEP_color color2;
   117     PEP_decrypt_flags_t flags2;
   118     PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &color2, &flags2);
   119     assert(status6 == PEP_DECRYPT_NO_KEY);
   120     assert(msg6 == NULL);
   121     assert(keylist5 == NULL);
   122     assert(color2 == PEP_rating_have_no_key);
   123     cout << "color :" << color2 << "\n";
   124     free_stringlist(keylist5);
   125 
   126     cout << "freeing messages…\n";
   127     free_message(msg4);
   128     free_message(msg3);
   129     free_message(msg2);
   130     free_message(enc_msg2);
   131     free_message(msg6);
   132     free_message(msg5);
   133     cout << "done.\n";
   134 
   135     cout << "calling release()\n";
   136     release(session);
   137     return 0;
   138 }