test/src/engine_tests/MimeTests.cc
author Krista Bennett <krista@pep-project.org>
Wed, 23 May 2018 11:41:33 +0200
branchENGINE-434
changeset 2685 01edd04b3c38
parent 2669 1b103e8a95f7
child 3276 c0b3430f1f1d
permissions -rw-r--r--
Fixed test suite problems and found stupid Heisenbug. Which, naturally, was my fault.
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>
krista@2661
     5
#include <string>
krista@2661
     6
#include <cstring>
vb@69
     7
#include <iostream>
vb@89
     8
#include <fstream>
vb@69
     9
#include <assert.h>
vb@159
    10
krista@2661
    11
#include "pEpEngine.h"
krista@2661
    12
#include "platform.h"
vb@69
    13
#include "mime.h"
vb@69
    14
krista@2661
    15
#include <cpptest.h>
krista@2661
    16
#include "EngineTestSessionSuite.h"
krista@2661
    17
#include "MimeTests.h"
krista@2661
    18
vb@69
    19
using namespace std;
vb@69
    20
krista@2661
    21
MimeTests::MimeTests(string suitename, string test_home_dir) :
krista@2661
    22
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
krista@2661
    23
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("MimeTests::check_mime"),
krista@2661
    24
                                                                      static_cast<Func>(&MimeTests::check_mime)));
krista@2661
    25
}
krista@2661
    26
krista@2661
    27
// FIXME: refactor so we can assert
krista@2661
    28
static void test_mime_decoding(string filename) {
vb@165
    29
    cout << "opening " << filename << " for reading\n";
Edouard@345
    30
    ifstream inFile3 (filename.c_str());
krista@2661
    31
vb@165
    32
    assert(inFile3.is_open());
vb@165
    33
vb@165
    34
    string mimetext3;
vb@165
    35
vb@165
    36
    cout << "reading mime sample\n";
vb@165
    37
    while (!inFile3.eof()) {
vb@165
    38
        static string line;
vb@165
    39
        getline(inFile3, line);
vb@165
    40
        mimetext3 += line + "\n";
vb@165
    41
    }
vb@165
    42
    inFile3.close();
vb@165
    43
vb@165
    44
    cout << "decoding message…\n";
vb@165
    45
    message *msg3;
vb@281
    46
    PEP_STATUS status3 = mime_decode_message(mimetext3.c_str(), mimetext3.length(), &msg3);
vb@165
    47
    assert(status3 == PEP_STATUS_OK);
vb@165
    48
    assert(msg3);
vb@165
    49
    cout << "decoded.\n\n";
vb@165
    50
    cout << "Subject: " << msg3->shortmsg << "\n\n";
vb@165
    51
    if (msg3->longmsg)
vb@165
    52
        cout << msg3->longmsg << "\n\n";
vb@165
    53
    if (msg3->longmsg_formatted)
vb@165
    54
        cout << msg3->longmsg_formatted << "\n\n";
vb@165
    55
    bloblist_t *_b;
vb@165
    56
    for (_b = msg3->attachments; _b; _b = _b->next) {
vb@165
    57
        cout << "attachment of type " << _b->mime_type << "\n";
vb@165
    58
        if (_b->filename) {
vb@165
    59
            cout << "filename: " << _b->filename << "\n";
vb@165
    60
            unlink(_b->filename);
vb@165
    61
            ofstream outFile3(_b->filename);
vb@302
    62
            outFile3.write(_b->value, _b->size);
vb@165
    63
            outFile3.close();
vb@165
    64
        }
vb@165
    65
    }
vb@165
    66
vb@165
    67
    free_message(msg3);
vb@165
    68
}
vb@165
    69
krista@2661
    70
void MimeTests::check_mime() {
krista@2661
    71
vb@89
    72
    // testing multipart/alternative
vb@69
    73
vb@306
    74
    message *msg2 = new_message(PEP_dir_incoming);
krista@2669
    75
    TEST_ASSERT_MSG((msg2), "msg2");
vb@306
    76
    msg2->from = new_identity("vb@dingens.org", NULL, NULL, "Volker Birk");
vb@306
    77
    msg2->to = new_identity_list(new_identity("trischa@dingens.org", NULL, NULL, "Patricia Bädnar")),
vb@306
    78
    msg2->shortmsg = strdup("my sübject");
vb@306
    79
vb@89
    80
    string text2 = "my mèssage to yoü";
vb@89
    81
    msg2->longmsg = strdup(text2.c_str());
vb@89
    82
    string html2 = "<html><body><p>my message to you</p></body></html>";
vb@89
    83
    msg2->longmsg_formatted = strdup(html2.c_str());
krista@2669
    84
    TEST_ASSERT_MSG((msg2->longmsg_formatted), "msg2->longmsg_formatted");
vb@89
    85
vb@69
    86
    cout << "encoding message…\n";
vb@69
    87
    char *result2;
vb@113
    88
    PEP_STATUS status2 = mime_encode_message(msg2, false, &result2);
krista@2669
    89
    TEST_ASSERT_MSG((result2), "result2");
krista@2669
    90
    TEST_ASSERT_MSG((status2 == PEP_STATUS_OK), "status2 == PEP_STATUS_OK");
vb@69
    91
vb@69
    92
    cout << "result:\n";
vb@69
    93
    cout << result2 << "\n";
vb@69
    94
vb@69
    95
    free(result2);
vb@89
    96
    free_message(msg2);
vb@69
    97
vb@165
    98
    test_mime_decoding("msg1.asc");
vb@165
    99
    test_mime_decoding("msg2.asc");
vb@165
   100
    test_mime_decoding("msg3.asc");
vb@69
   101
}