test/message_api_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 19 Jan 2018 14:12:02 +0100
changeset 2418 05678e77bc6a
parent 2394 88b9027db1bf
child 2462 48b526a0daac
permissions -rw-r--r--
fixed update_identity call that could be called on a myself value in outgoing_message_rating
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
Edouard@345
     4
#include <stdlib.h>
Edouard@345
     5
#include <string.h>
vb@165
     6
#include "platform.h"
vb@39
     7
#include <iostream>
vb@165
     8
#include <fstream>
vb@39
     9
#include <assert.h>
vb@221
    10
#include "mime.h"
vb@39
    11
#include "message_api.h"
vb@39
    12
vb@39
    13
using namespace std;
vb@39
    14
roker@2332
    15
void test_MIME_decrypt_message()
roker@2332
    16
{
roker@2333
    17
	static const std::string private_key =
roker@2333
    18
        "-----BEGIN PGP PRIVATE KEY BLOCK-----\n"
roker@2333
    19
        "\n"
roker@2333
    20
        "lQOmBFpCxkEBCCC8ugYsWOsv966JOl5Ahdw6YiobbU9peFLV8aMBOG+oNIPs3BQj\n"
roker@2333
    21
        "u3FUugkVqA5In93oqVgNZ2LU6Y/RWldN+Pc4IMf6qSZnTBj/1ffBjuqAow1hPEYV\n"
roker@2333
    22
        "354LNYoQtJXioY0X8FjRgt+NoBPphRbo+XJ0uIQckJey6uvvtukEZkMLM1ur1aID\n"
roker@2333
    23
        "9biJZ7yXtJM7KxN5792Vo2gGp/1hlFW6SfM7E0g60L5DT8C/BsYeKtMmxKNZngo6\n"
roker@2333
    24
        "ZBxLDAcxMcT5UpRW79B34pTINZAEsvLeT7TLajzqP/OggUrFkkwLr3KJk09aFF+6\n"
roker@2333
    25
        "TN6CI2fDdSqdoPVEgNrZE9zFqAgVWOdhLOHRpXgt7wARAQABAAgZATpqsN4xRaIk\n"
roker@2333
    26
        "giMdmujkGoMqB/ypoCOW0mqcp3ThESSqWR/Dh8n//k+poHj0Atf7fzie6JNsKruM\n"
roker@2333
    27
        "Yo3mdIzyuuxHsONp6xEtNnkDgEB4WTb2btQQFrNaWXNTPzGVqiBoBShcw5xI7SiG\n"
roker@2333
    28
        "CKaDlCePbaAHyBHO0uzBdVFo6czqkceXSJ+hNDY7xbURbkgIA5SGJ+8cssmcKKoc\n"
roker@2333
    29
        "LDY34S3Pu0gG3+K6gSedYaHAqVVQn8dmEitvDFQ96sTNIpPiMp5Tc+/8UXziF91f\n"
roker@2333
    30
        "XRrXYX9o3nkCvz01qtjR4LQVDztysm3/VmDcsjqhzALiiBLigyglpE5DEp0+wMUq\n"
roker@2333
    31
        "gw5TpWZ63lf6XjEEEM+c+D3HHsEnh24+DNU2OyfFZGpp9olSaTDcXtEBuBTt/Uhy\n"
roker@2333
    32
        "NnqF4MswQ2me4Lfr4D4sUS5jtsKkee+2IQuwtg/bBVYsL6MfV2llJco2vrOCb6y8\n"
roker@2333
    33
        "u9CnQhRbVBd6kBMiJeE50Ijk3jP78GrkPm3rGWKCWRuXVr07+U2MsEnvSkkBfpEE\n"
roker@2333
    34
        "EOi2NcXCCJux9xZv0xOyWogXDVLVrrhs8/PGjJO1IeaMtmrzleo3azUbWus/BYLA\n"
roker@2333
    35
        "vJkO+PElg/MCc2ub5hFs7IqSbLmteWFiSOzTcdHNWeqETsixAc8dpbq0zhkPwdzA\n"
roker@2333
    36
        "otLEBS3mpaB29Bpt9lzgewHoVI/o/OvUFFaP+b1Fd4wOJH8EELZPWw+85tSB1l63\n"
roker@2333
    37
        "4B+77YoBc9wDBFg7Pt4Eo0eghFZ/159YTA1bcr3fZ8bYIgiHHNLC/Fsx+sUu00PC\n"
roker@2333
    38
        "ctLrOxGRbuDyNy2rsezf8Bz8xpihBPuBmSbKoaZguSazgpuC1LPkVbQQVaN1h+Ir\n"
roker@2333
    39
        "9/tJufw+hT9Eggvx5xtCo7GD1trnx0xLKbQhVGVzdCBUb25pIDxlbmdpbmUtMzIy\n"
roker@2333
    40
        "QHBlcHRlc3QuY2g+iQE8BBMBCAAiBQJaQsZBAhsDBgsJCAcDAgYVCAIJCgsEFgID\n"
roker@2333
    41
        "AQIeAQIXgAAKCRCvYy6fx5m31NWeCCCXgm1J7Pq4ECKaMZcp7WoWguXQW+Ellgrx\n"
roker@2333
    42
        "B6EjZmvy1iB0RYaDz6F0HCXd0WtC2YDlkxJ47rKBHsnmcyHbFNlY0fgc5Uhs7apI\n"
roker@2333
    43
        "bBMHHRGwl8pie05DqxQ0jBxO2esk/IPJDhLXw7gZvaY/9PUS669QWoq+L/Hhph8V\n"
roker@2333
    44
        "5v65jnw2937bOaf6wvEUUj2cg6cUaPTZSXv26vxUrT8RD+DxbQiNjJIeGRfVj3QY\n"
roker@2333
    45
        "9GCTcp45ZaB8kLQEVayFrC3Jougcklk5DS1zlFCHiYLa4cco/68XHL7/CFdIxxsK\n"
roker@2333
    46
        "Rd/3FYWX6zfQZJs5U6KmGy18cXvk0OOtTru9aNHR0YiLj4Vs8K+mWA10vZ0DpgRa\n"
roker@2333
    47
        "QsZBAQggwrnOsiJ3JNB+mTm9pZbX4mUkw7OXrar1CvOVDqrnI+H+Z9/DC1FDEupw\n"
roker@2333
    48
        "8mD3fFV4veO6smjb9wWAXhmU88OxXziChM8WJlWz2GrZPoM2DIYu1gLycp7wo1Md\n"
roker@2333
    49
        "mzhd/5tpBWMJ4gGS9AjvQc5ffk7JVBAnmhh4ZtdoEctHMJs7+1RhXE7KUM1QWjew\n"
roker@2333
    50
        "2GAVAaw+KsuXvqsF8soXvlFaHe9sTHKXKUD/MN4WWPR3SIvC4yoadlUpCMfooXf8\n"
roker@2333
    51
        "ZCFLbVirkqGy5AakF7thlaTq7bxEX5BQbP/DjVuTTd311jk4x7oT+1bT7D6iIoES\n"
roker@2333
    52
        "DKfYijw+059CrCbjFUn3/RRg5sx/55FA2XEAEQEAAQAIHiu228SYwSeGGM2cLUt1\n"
roker@2333
    53
        "vBxKeYDnmeb2aJFfUnia/E3NZ7f4/0fUo9qkv9th0l1asMLsU1bG/I6NcR5u3sYE\n"
roker@2333
    54
        "iham0IIxHTdY6QluHzwN573TB8OqoLQDo2D/ATf95PhDcsWvUKIomU1ojhG3Wy+3\n"
roker@2333
    55
        "TzIseD97O9hWhjnsaRxr1QDclghnNffz589T40wAQAkdQlfDuBABberGNR0DsCZq\n"
roker@2333
    56
        "w1xx1+EaEt8o7sXfRMFKoBLJzya0toJNIBGdXCXVPFPtYx6RAiD1KoufgXwVCBaL\n"
roker@2333
    57
        "CHc8QvurgyMBghc9pBcdGs60fNhWn1U4qeWzPOHO95ZWVFObBiuGqkX25revf1Dg\n"
roker@2333
    58
        "RTY5OsBTBBDdnMElWA+l6ctlSX8vNUQBin7aPbHu1HrmUxH9EiKwUd6jdoFmssw7\n"
roker@2333
    59
        "soohAfYJCsuTQLHPI+9W6okF4rjlvl0aS2cN4HSbLjTwdNiUgIHVKJPDq8WDgCsV\n"
roker@2333
    60
        "bR09wsLkohmoX/qZFEQMKdr7A2ar64zQzpx6ZFB0kS71Fr6+rFUAIzePBBDg8QXr\n"
roker@2333
    61
        "J/R6iZYnMNv/mzyNunbM3B7siwbILS1kLeS6lTYeaKORa7JFRc776rkWV//P1Soc\n"
roker@2333
    62
        "nW2byeBxNxOmdNXlNsA6Gg8O4rn3rxEgBZlsOYUG1ZcPZZu8kNFkLTYP+mgFIP3I\n"
roker@2333
    63
        "izn858IbnNFm0HjRMGiS+zip2r/rgbniIhLQ2l7/BBCL+2x0+Ww5I/el6A7k+y2X\n"
roker@2333
    64
        "3xazBVOQfpPl9BgMdsuO0BBlN5RpbTfE8TVpWneeamAuRJh7ArApbAS508Thw7Sj\n"
roker@2333
    65
        "8iLKXj5yScAEC2WO9ZsHqJ/RN3VyhCCI0u3Y75wB3qfcroZUlwvQb/WfH8/vHr2O\n"
roker@2333
    66
        "E72YoprbNOmRFZ7LZkRX56URRn2JASMEGAEIAAkFAlpCxkECGwwACgkQr2Mun8eZ\n"
roker@2333
    67
        "t9RozQggkb/Lb08g4w99CcXq6hV28D5bOHjiEx4XNdkeLVExhfk1zgQ9lf4rjyb2\n"
roker@2333
    68
        "ox+e3Dc5S590NoYg/35vd/QWPsg1JiCvAu296lzOLtIiTAI1KGUJdbsLxRIduOU7\n"
roker@2333
    69
        "6n/KVxRG4w4kJqpbu+UzBY7KtDbWzapJx8v0sdsTOVxg7kFxvYtra5TRaPfce4EX\n"
roker@2333
    70
        "ox2V2HPhtFNSILH8Jqh/R0PV5RRbNFHZA4cKXkBJMw3BcpbpXeLCXiD6P3FNVSKh\n"
roker@2333
    71
        "Rkd3JY0XJhwbvGPCRWkobtxkieZe0bCmKu8+gw0Zqm2QNA7J6iMT4rMZWH9k7lpp\n"
roker@2333
    72
        "okyt7GheXlwKEtVQSAcH/NalK9Q+ckaQttA=\n"
roker@2333
    73
        "=KACn\n"
roker@2333
    74
        "-----END PGP PRIVATE KEY BLOCK-----\n"
roker@2333
    75
        "\n";
roker@2333
    76
roker@2333
    77
    static const std::string mimetext =
roker@2333
    78
        "To: denden@peptest.ch\r\n"
roker@2333
    79
        "From: Lars Rohwedder <roker@pep-project.org>\r\n"
roker@2333
    80
        "Subject: Test for ENGINE-322\r\n"
roker@2333
    81
        "Message-ID: <4b2d328c-b284-e359-1c2c-fe136358b8a6@pep-project.org>\r\n"
roker@2333
    82
        "Date: Thu, 28 Dec 2017 22:00:47 +0100\r\n"
roker@2333
    83
        "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0)\r\n"
roker@2333
    84
        " Gecko/20100101 Thunderbird/52.5.0\r\n"
roker@2333
    85
        "MIME-Version: 1.0\r\n"
roker@2333
    86
        "Content-Type: text/plain; charset=utf-8\r\n"
roker@2333
    87
        "Content-Language: en-US\r\n"
roker@2333
    88
        "Content-Transfer-Encoding: 8bit\r\n"
roker@2333
    89
        "\r\n"
roker@2333
    90
        "-----BEGIN PGP MESSAGE-----\r\n"
roker@2333
    91
        "Charset: utf-8\r\n"
roker@2333
    92
        "\r\n"
roker@2333
    93
        "hQEQA30SSxlFRbxRAQgggvPrko4vJ988ylqdGF7/Jtw/61VddHg7rjOTG7yZiq2J\r\n"
roker@2333
    94
        "p0lXb0N9Bz8SON/m4NWWS5ij0Bj1wMdTgLowLxMz0PfCIyyiQIfqEBAXUcAOQh2f\r\n"
roker@2333
    95
        "Pg2iZbynrQ1T4M/D0BlIn2bfXb790Cni+o1OmWTCx4wC6AKtGvlonLAIsH1hUfs+\r\n"
roker@2333
    96
        "yKOyCCNaNSmyAPeF3F5v7iEO5Eez1R/UrtxcYj2QmCdVt5v0AuAlm5HVPJgj7wCd\r\n"
roker@2333
    97
        "MeRmK4a1+sM51CcCf6Tk9uZbIVrr/XkyGVPmmHTK8E4QmvmL6PGeuIitwqKe80/L\r\n"
roker@2333
    98
        "XH4ZYPXIxVU9o5HoSo3YJ3BSKLQzCoDRCD8JlCo08K6mrpuFAgwD42RJLbKIaR4B\r\n"
roker@2333
    99
        "EADNgisKiIku4SrBmBkryyKMYmOTW0QKnY/wfNselpzuj5cMpKA4e60x/wEQRIvC\r\n"
roker@2333
   100
        "m1ZO7LjhbVjNf6ws2FgytnTRf+8R/R8mp6/XIDeUvaBvUku5yoRjTeznFRwpj6yT\r\n"
roker@2333
   101
        "WfQMlLojI2fe+y5fHKIIjTpW5HOQQv7sZ4UzE+jRpRErRVq4UI49l7yTBnG0j75w\r\n"
roker@2333
   102
        "UZTt05OnJMQrCCiD7Fu9xDw5If3x884GehKpFGm6XqZ8V7NhwGk6mf62rZEouBQh\r\n"
roker@2333
   103
        "AhTu/irkz30PpWY3gGj2YF0PIaQmweb9u2izox1kTfq2xRfPLg/4cOgBKy1/Xeea\r\n"
roker@2333
   104
        "IVeaACAcoNdJaYuZCSe9CMbr5s7kusE22/6fB0L1itGr09EzhwGJS/+XSt0IwcJw\r\n"
roker@2333
   105
        "7XDUGtmYXuTy08wUKU9McxXJ8nlUkXF9ZcswVxHKG36ZRfzx5eBPjzSnDo8EZHs2\r\n"
roker@2333
   106
        "wr7wnS8s8J+AvB7kZYFChAct4KH9OwT3/2pSdhd8/sSCkj2zGtrB+8h2QkIE4csD\r\n"
roker@2333
   107
        "rIHtBp4oWCJq1XOKYPvdGqWBxZ/7086kksB99Eyn+sy5C0iNTbEdUN4JkIYq1C0n\r\n"
roker@2333
   108
        "DExbr+dqip65DEJRj7TPfwTJ5D28djPYutanSRfJS/niPqztWu0R1ISucw1TMdGU\r\n"
roker@2333
   109
        "NcqsqQLPYzTV6xTq+bgBsi8DO4tXkcOpf7eDEP+5kDOncNLpAZGc9NUnVa/jpkYO\r\n"
roker@2333
   110
        "XE+CNJ5SYQPdsGcir9JNXNTDgKAGSTI7OAU/ZxOcMCsmctJATcAZERFgvUy8YZN1\r\n"
roker@2333
   111
        "3X4Ii6osc8u6shJrjL/detZs8LH8wSe6NYQdtipQo4ySAYQAO9tXdoRrfktXrYus\r\n"
roker@2333
   112
        "eLh86toD19D5R9RxHnVEMQP5CdNWgvX7X4ngK94kJq18QCDa1bZXhHBKmWOnAtyL\r\n"
roker@2333
   113
        "zpcUNnCWo2gml6GX2kyuL+5Ji6afwKHZg+iag8wBDLGQ+hoOMnzk1iP4DFeQ7iZN\r\n"
roker@2333
   114
        "Qvd4mWWASd2BCnf9ulKiMw1wdzN2mpYRNo+nRHx0Zu50VUyj0xMm8VSyUZis5+YH\r\n"
roker@2333
   115
        "I3Se7UEeS6ppLsiGcyaJDCMp/38xt5SU5NY4wAAubc6MJclECcvSkM1W/20wQ4di\r\n"
roker@2333
   116
        "z5FhKHlqZaPTXN02h0P78wKDDwJr7fFvqtB8G2LgtwbXAkOUvn8vbomQLHBkQ+GH\r\n"
roker@2333
   117
        "AuXqBGxKrIwyIEjLZf6hDz++0fDa/ACeFynpxNl1ehmvCl5CsEMcCiM+Ic2pZ/ML\r\n"
roker@2333
   118
        "+Hle0GWEKej4WBzXi0j4pzR4WZFt9XCv5+yYAg+UHKc2Kn0Q+bC1AZYxhQDicTP9\r\n"
roker@2333
   119
        "qNKTLBHRAeJoQ1y4vHXYGwRGH+penfJiKsQsyOOeoQlZar8tvRYR77K8FhxBtnYK\r\n"
roker@2333
   120
        "Xrv+rb1BT/2Ey7P4jb8PiZpbic7ACu1MjFdmlPKrExe5+MY+Pr9ms+hxJrdBH736\r\n"
roker@2333
   121
        "K6dojmWUQKpJRzue2lWsfESLxIVeB+vmbg2zU3PflCmMIsRNh2US7vZj2WdgqSqz\r\n"
roker@2333
   122
        "wR2eTG4MgPVy4iiGOVT2JWS5t+KXm3kwUZTy9Twi6P1ebNm/B8KQwlutssdWip2q\r\n"
roker@2333
   123
        "hON1aFYa4L601zrHgow592PdBkRPQZGiXNHffCvHgsxBHsj4G4JWZhmIkEK/cIWl\r\n"
roker@2333
   124
        "RanlZdQG6UPHkoUomh7hauUUgcYe4FWt4NBKdiba36Y=\r\n"
roker@2333
   125
        "=1lFj\r\n"
roker@2333
   126
        "-----END PGP MESSAGE-----\r\n"
roker@2333
   127
        "\r\n";
roker@2333
   128
roker@2332
   129
	PEP_SESSION session;
roker@2332
   130
	PEP_STATUS status1 = init(&session);
roker@2332
   131
	assert(status1 == PEP_STATUS_OK);
roker@2332
   132
	assert(session);
roker@2332
   133
roker@2333
   134
	// import secret key
roker@2333
   135
	identity_list* pk = NULL; 
roker@2333
   136
	status1 = import_key( session, private_key.c_str(), private_key.size(), &pk );
roker@2333
   137
	assert(status1 == PEP_STATUS_OK);
roker@2333
   138
	std::cout << "Imported " << identity_list_length(pk) << " private key(s)." << endl;
roker@2333
   139
	
roker@2332
   140
	char* plaintext = nullptr;
roker@2332
   141
	stringlist_t* keys_used = nullptr;
roker@2332
   142
	PEP_rating rating;
roker@2332
   143
	PEP_decrypt_flags_t dec_flags;
roker@2332
   144
	
roker@2332
   145
	PEP_STATUS status2 = MIME_decrypt_message(session, mimetext.c_str(), mimetext.length(),
roker@2332
   146
		&plaintext, &keys_used, &rating, &dec_flags);
roker@2332
   147
	
vb@2340
   148
	std::cout << "MIME_decrypt_message returned " << std::dec << status2 << std::hex << " (0x" << status2 << ")" << std::dec << endl;
roker@2332
   149
	
krista@2394
   150
    // We have no public key, so we cannot, in fact, VERIFY the message. SO cannot be PEP_STATUS_OK.
krista@2394
   151
	assert(status2 == PEP_DECRYPTED);
roker@2332
   152
	assert(plaintext);
roker@2332
   153
	
roker@2332
   154
	pEp_free(plaintext);
roker@2333
   155
	
roker@2333
   156
	identity_list* il = pk;
roker@2333
   157
	while(il)
roker@2333
   158
	{
roker@2333
   159
		std::cout << "Delete test key \"" << il->ident->fpr << "\"" << endl;
roker@2333
   160
		delete_keypair( session, il->ident->fpr );
roker@2333
   161
		il = il->next;
roker@2333
   162
	}
roker@2333
   163
	free_identity_list(pk);
roker@2332
   164
}
roker@2332
   165
vb@39
   166
int main() {
vb@53
   167
    cout << "\n*** message_api_test ***\n\n";
roker@2332
   168
    test_MIME_decrypt_message();
vb@51
   169
vb@39
   170
    PEP_SESSION session;
vb@39
   171
    
vb@39
   172
    cout << "calling init()\n";
roker@759
   173
    PEP_STATUS status1 = init(&session);
vb@39
   174
    assert(status1 == PEP_STATUS_OK);
vb@39
   175
    assert(session);
vb@39
   176
    cout << "init() completed.\n";
vb@39
   177
vb@53
   178
    // message_api test code
vb@53
   179
vb@40
   180
    cout << "creating message…\n";
vb@822
   181
    pEp_identity * me2 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
Edouard@745
   182
    // pEp_identity * me2 = new_identity("test@nokey.plop", NULL, PEP_OWN_USERID, "Test no key");
krista@2288
   183
    me2->me = true;
vb@822
   184
    identity_list *to2 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
Edouard@745
   185
    // identity_list *to2 = new_identity_list(new_identity("still@nokey.blup", NULL, "42", "Still no key"));
vb@306
   186
    message *msg2 = new_message(PEP_dir_outgoing);
vb@112
   187
    assert(msg2);
vb@306
   188
    msg2->from = me2;
vb@306
   189
    msg2->to = to2;
vb@306
   190
    msg2->shortmsg = strdup("hello, world");
krista@1871
   191
    msg2->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
vb@40
   192
    cout << "message created.\n";
vb@39
   193
roker@759
   194
    char *text2 = nullptr;
vb@113
   195
    PEP_STATUS status2 = mime_encode_message(msg2, false, &text2);
vb@112
   196
    assert(status2 == PEP_STATUS_OK);
vb@112
   197
    assert(text2);
vb@112
   198
vb@112
   199
    cout << "decrypted:\n\n";
vb@112
   200
    cout << text2 << "\n";
vb@112
   201
vb@112
   202
    free(text2);
vb@112
   203
vb@48
   204
    cout << "encrypting message as MIME multipart…\n";
roker@759
   205
    message *enc_msg2 = nullptr;
vb@40
   206
    cout << "calling encrypt_message()\n";
vb@940
   207
    status2 = encrypt_message(session, msg2, NULL, &enc_msg2, PEP_enc_PGP_MIME, 0);
roker@759
   208
    cout << "encrypt_message() returns " << status2 << '.' << endl;
vb@112
   209
    assert(status2 == PEP_STATUS_OK);
vb@112
   210
    assert(enc_msg2);
vb@40
   211
    cout << "message encrypted.\n";
vb@48
   212
    
vb@113
   213
    status2 = mime_encode_message(enc_msg2, false, &text2);
vb@112
   214
    assert(status2 == PEP_STATUS_OK);
vb@112
   215
    assert(text2);
vb@112
   216
vb@112
   217
    cout << "encrypted:\n\n";
vb@112
   218
    cout << text2 << "\n";
vb@112
   219
roker@759
   220
    message *msg3 = nullptr;
vb@281
   221
    PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
vb@116
   222
    assert(status3 == PEP_STATUS_OK);
roker@759
   223
    const string string3 = text2;
Edouard@745
   224
    //free(text2);
vb@165
   225
vb@165
   226
    unlink("msg4.asc");
vb@165
   227
    ofstream outFile3("msg4.asc");
vb@165
   228
    outFile3.write(string3.c_str(), string3.size());
vb@165
   229
    outFile3.close();
vb@71
   230
roker@759
   231
    message *msg4 = nullptr;
roker@759
   232
    stringlist_t *keylist4 = nullptr;
vb@1006
   233
    PEP_rating rating;
Edouard@728
   234
    PEP_decrypt_flags_t flags;
Edouard@728
   235
    
vb@1006
   236
    PEP_STATUS status4 = decrypt_message(session, enc_msg2, &msg4, &keylist4, &rating, &flags);
vb@267
   237
    assert(status4 == PEP_STATUS_OK);
vb@166
   238
    assert(msg4);
vb@241
   239
    assert(keylist4);
vb@1006
   240
    assert(rating);
vb@781
   241
    PEP_comm_type ct = enc_msg2->from->comm_type;
vb@822
   242
    assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
vb@781
   243
vb@781
   244
    free_stringpair_list(enc_msg2->opt_fields);
vb@781
   245
    enc_msg2->opt_fields = NULL;
vb@241
   246
vb@241
   247
    cout << "keys used:";
vb@781
   248
roker@759
   249
    for (stringlist_t* kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
roker@759
   250
    {
vb@241
   251
        cout << " " << kl4->value;
roker@759
   252
    }
vb@241
   253
    cout << "\n\n";
vb@241
   254
vb@241
   255
    free_stringlist(keylist4);
vb@166
   256
Edouard@745
   257
    cout << "opening msg_no_key.asc for reading\n";
Edouard@745
   258
    ifstream inFile3 ("msg_no_key.asc");
Edouard@745
   259
    assert(inFile3.is_open());
Edouard@745
   260
Edouard@745
   261
    string text3;
Edouard@745
   262
Edouard@745
   263
    cout << "reading msg_no_key.asc sample\n";
Edouard@745
   264
    while (!inFile3.eof()) {
Edouard@745
   265
        static string line;
Edouard@745
   266
        getline(inFile3, line);
Edouard@745
   267
        text3 += line + "\r\n";
Edouard@745
   268
    }
Edouard@745
   269
    inFile3.close();
Edouard@745
   270
roker@759
   271
    message *msg5 = nullptr;
Edouard@745
   272
    PEP_STATUS status5 = mime_decode_message(text3.c_str(), text3.length(), &msg5);
Edouard@745
   273
    assert(status5 == PEP_STATUS_OK);
Edouard@745
   274
roker@759
   275
    message *msg6 = nullptr;
roker@759
   276
    stringlist_t *keylist5 = nullptr;
vb@1006
   277
    PEP_rating rating2;
Edouard@746
   278
    PEP_decrypt_flags_t flags2;
vb@1006
   279
    PEP_STATUS status6 = decrypt_message(session, msg5, &msg6, &keylist5, &rating2, &flags2);
Edouard@745
   280
    assert(status6 == PEP_DECRYPT_NO_KEY);
Edouard@745
   281
    assert(msg6 == NULL);
Edouard@745
   282
    assert(keylist5 == NULL);
vb@1006
   283
    assert(rating2 == PEP_rating_have_no_key);
vb@1006
   284
    cout << "rating :" << rating2 << "\n";
Edouard@745
   285
    free_stringlist(keylist5);
Edouard@745
   286
krista@1325
   287
    cout << "\nTesting MIME_encrypt_message / MIME_decrypt_message...\n\n";
krista@1325
   288
krista@1325
   289
    cout << "opening alice_bob_encrypt_test_plaintext_mime.eml for reading\n";
krista@1325
   290
    ifstream inFile4 ("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
krista@1325
   291
    assert(inFile4.is_open());
krista@1325
   292
    
krista@1325
   293
    string text4;
krista@1325
   294
    
krista@1325
   295
    cout << "reading alice_bob_encrypt_test_plaintext_mime.eml sample\n";
krista@1325
   296
    while (!inFile4.eof()) {
krista@1325
   297
        static string line;
krista@1325
   298
        getline(inFile4, line);
krista@1325
   299
        text4 += line + "\r\n";
krista@1325
   300
    }
krista@1325
   301
    inFile4.close();
krista@1325
   302
    
krista@1359
   303
    const char* out_msg_plain = text4.c_str();
krista@1358
   304
    
krista@1359
   305
//    const char* out_msg_plain = "From: krista@kgrothoff.org\nTo: Volker <vb@pep-project.org>\nSubject: Test\nContent-Type: text/plain; charset=utf-8\nContent-Language: en-US\nContent-Transfer-Encoding:quoted-printable\n\ngaga\n\n";
krista@1325
   306
    char* enc_msg = NULL;
krista@1325
   307
    char* dec_msg = NULL;
krista@1325
   308
krista@1359
   309
    PEP_STATUS status7 = MIME_encrypt_message(session, text4.c_str(), text4.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@1359
   310
//    PEP_STATUS status7 = MIME_encrypt_message(session, out_msg_plain, strlen(out_msg_plain), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@1325
   311
    assert(status7 == PEP_STATUS_OK);
krista@1325
   312
    
krista@1325
   313
    cout << enc_msg << endl;
krista@1328
   314
krista@1328
   315
    string text5 = enc_msg;
krista@1328
   316
    
krista@1328
   317
    PEP_decrypt_flags_t dec_flags;
krista@1328
   318
    stringlist_t* keys_used;
krista@1328
   319
    
krista@1328
   320
    PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags);
krista@1328
   321
    assert(status8 == PEP_STATUS_OK);
krista@1328
   322
    
krista@1328
   323
    cout << dec_msg << endl;
krista@1328
   324
    
vb@2340
   325
    cout << "\nTesting encrypt_message() with enc_format = PEP_enc_none\n\n";
vb@2340
   326
vb@2340
   327
    message *msg7 = new_message(PEP_dir_outgoing);
vb@2340
   328
    pEp_identity * me7 = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
vb@2340
   329
    identity_list *to7 = new_identity_list(new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test"));
vb@2340
   330
    msg7->from = me7;
vb@2340
   331
    msg7->to = to7;
vb@2340
   332
    msg7->shortmsg = strdup("My Subject");
vb@2340
   333
    msg7->longmsg = strdup("This is some text.\n");
vb@2340
   334
vb@2340
   335
    message *enc7 = nullptr;
vb@2340
   336
    PEP_STATUS status9 = encrypt_message(session, msg7, NULL, &enc7, PEP_enc_none, 0);
vb@2340
   337
	std::cout << "encrypt_message returned " << std::dec << status9 << std::hex << " (0x" << status9 << ")" << std::dec << endl;
vb@2340
   338
    assert(status9 == PEP_UNENCRYPTED);
vb@2340
   339
    assert(enc7 == nullptr);
vb@2340
   340
    assert(msg7->shortmsg && msg7->longmsg);
vb@2340
   341
    cout << msg7->shortmsg << "\n";
vb@2340
   342
    cout << msg7->longmsg << "\n";
vb@2340
   343
    assert(strcmp(msg7->shortmsg, "My Subject") == 0);
vb@2340
   344
    assert(strcmp(msg7->longmsg, "This is some text.\n") == 0);
krista@1325
   345
    
vb@2340
   346
    cout << "\nfreeing messages…\n";
vb@2340
   347
    free_message(msg7);
vb@2340
   348
    free_message(msg6);
vb@2340
   349
    free_message(msg5);
vb@166
   350
    free_message(msg4);
vb@116
   351
    free_message(msg3);
vb@112
   352
    free_message(msg2);
vb@112
   353
    free_message(enc_msg2);
vb@40
   354
    cout << "done.\n";
vb@39
   355
krista@1328
   356
    free(enc_msg);
krista@1328
   357
    free(dec_msg);
vb@39
   358
    cout << "calling release()\n";
vb@39
   359
    release(session);
vb@39
   360
    return 0;
vb@39
   361
}