test/message_api_test.cc
author Edouard Tisserant
Wed, 08 Jun 2016 01:32:10 +0200
branchimport_own_key
changeset 728 80b966277620
parent 678 f8181e59ae2f
child 746 753b1ec63285
permissions -rw-r--r--
Preparing for import of gpg private key on android : decrypt flags (import own key from own trusted message) + pgp_gpg giving a list private keys when importing.
     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     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_decrypt_flags_t flags;
    76     
    77     PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &color, &flags);
    78     assert(status4 == PEP_STATUS_OK);
    79     assert(msg4);
    80     assert(keylist4);
    81     assert(color);
    82 
    83     cout << "keys used:";
    84     stringlist_t *kl4;
    85     for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    86         cout << " " << kl4->value;
    87     cout << "\n\n";
    88 
    89     free_stringlist(keylist4);
    90 
    91     cout << "freeing messages…\n";
    92     free_message(msg4);
    93     free_message(msg3);
    94     free_message(msg2);
    95     free_message(enc_msg2);
    96     cout << "done.\n";
    97 
    98     cout << "calling release()\n";
    99     release(session);
   100     return 0;
   101 }
   102