test/src/engine_tests/DeviceModelTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 26 Feb 2019 08:43:48 +0100
branchsync_test_refactor
changeset 3320 49df110e65d2
parent 3307 b3d26f9f0225
permissions -rw-r--r--
Changed device tests to account for sync mails (gee, when you try to make something work automatically, it works, uh, automativcally\!
krista@3300
     1
// This file is under GNU General Public License 3.0
krista@3300
     2
// see LICENSE.txt
krista@3300
     3
krista@3300
     4
#include <stdlib.h>
krista@3300
     5
#include <string>
krista@3300
     6
#include <sys/stat.h>
krista@3300
     7
#include <errno.h>
krista@3300
     8
#include <unistd.h>
krista@3300
     9
#include <assert.h>
krista@3300
    10
#include <fstream>
krista@3300
    11
#include <iostream>
krista@3300
    12
#include <sys/types.h>
krista@3300
    13
krista@3300
    14
#include "pEpEngine.h"
krista@3305
    15
#include "mime.h"
krista@3300
    16
krista@3300
    17
#include "TestUtils.h"
krista@3300
    18
#include "EngineTestIndividualSuite.h"
krista@3300
    19
#include "DeviceModelTests.h"
krista@3300
    20
#include <cpptest.h>
krista@3300
    21
#include <cstring>
krista@3300
    22
krista@3300
    23
using namespace std;
krista@3300
    24
krista@3320
    25
static void remove_sync_mails(vector<message*> &mails) {
krista@3320
    26
    for (vector<message*>::iterator it = mails.begin(); it != mails.end(); ) {
krista@3320
    27
        stringpair_list_t* opt_fields = (*it)->opt_fields;
krista@3320
    28
        bool erased = false;
krista@3320
    29
        while (opt_fields && opt_fields->value && opt_fields->value->key && opt_fields->value->value) {
krista@3320
    30
            if (strcmp(opt_fields->value->key, "pEp-auto-consume") == 0 &&
krista@3320
    31
                strcmp(opt_fields->value->value, "yes") == 0) {
krista@3320
    32
                it = mails.erase(it);
krista@3320
    33
                erased = true;
krista@3320
    34
            }
krista@3320
    35
            opt_fields = opt_fields->next;
krista@3320
    36
        }
krista@3320
    37
        if (!erased)
krista@3320
    38
            it++;
krista@3320
    39
    }
krista@3320
    40
}
krista@3320
    41
krista@3300
    42
DeviceModelTests::DeviceModelTests(string suitename, string test_home_dir) :
krista@3300
    43
    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir, false) {
krista@3300
    44
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_device_model"),
krista@3300
    45
                                                                      static_cast<Func>(&DeviceModelTests::check_device_model)));
krista@3300
    46
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_two_device_model"),
krista@3300
    47
                                                                      static_cast<Func>(&DeviceModelTests::check_two_device_model)));
krista@3307
    48
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_two_device_functionality"),
krista@3307
    49
                                                                      static_cast<Func>(&DeviceModelTests::check_two_device_functionality)));
krista@3305
    50
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_mbox"),
krista@3305
    51
                                                                      static_cast<Func>(&DeviceModelTests::check_mbox)));
krista@3307
    52
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_three_device_mbox_with_send"),
krista@3307
    53
                                                                      static_cast<Func>(&DeviceModelTests::check_three_device_mbox_with_send)));
krista@3307
    54
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_switch_context"),
krista@3307
    55
                                                                      static_cast<Func>(&DeviceModelTests::check_switch_context)));
krista@3307
    56
}
krista@3307
    57
krista@3307
    58
void DeviceModelTests::setup() {
krista@3307
    59
    EngineTestIndividualSuite::setup();
krista@3307
    60
    pEpTestDevice::active = NULL;
krista@3305
    61
}
krista@3305
    62
krista@3305
    63
void DeviceModelTests::tear_down() {
krista@3305
    64
    for (vector<pEpTestDevice*>::iterator it = devices.begin();
krista@3305
    65
                                         it != devices.end(); it++)
krista@3305
    66
        delete(*it);                                         
krista@3305
    67
    devices.clear();
krista@3305
    68
    EngineTestIndividualSuite::tear_down();
krista@3300
    69
}
krista@3300
    70
krista@3300
    71
void DeviceModelTests::check_device_model() {
krista@3300
    72
    pEpTestDevice* single = new pEpTestDevice(temp_test_home, "SingleDevice");
krista@3300
    73
    TEST_ASSERT_MSG(device == NULL, "EngineTestSuite created device when it should not have.");
krista@3300
    74
    TEST_ASSERT_MSG(session == NULL, "EngineTestSuite has a default session - not cool.");    
krista@3300
    75
    TEST_ASSERT_MSG(single->session, "Device has no session.");
krista@3300
    76
krista@3300
    77
    single->set_mailbox_dir(single->device_dir + "/mbox");
krista@3300
    78
    const string mbox_dir = single->mbox_dir;
krista@3300
    79
    struct stat dirchk;
krista@3300
    80
    TEST_ASSERT_MSG(stat(mbox_dir.c_str(), &dirchk) == 0,
krista@3300
    81
                    "Device mbox dir not created.");
krista@3300
    82
    TEST_ASSERT_MSG(S_ISDIR(dirchk.st_mode), "Device mbox dir exists, but isn't a directory.");                
krista@3300
    83
krista@3300
    84
    const string device_dir = string(single->device_dir);
krista@3300
    85
    delete(single);
krista@3300
    86
    TEST_ASSERT_MSG(stat(device_dir.c_str(), &dirchk) != 0,
krista@3300
    87
                         "Device dir not removed.");
krista@3300
    88
}
krista@3300
    89
krista@3300
    90
void DeviceModelTests::check_two_device_model() {
krista@3300
    91
    pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "First");
krista@3305
    92
    devices.push_back(first_device);
krista@3300
    93
    first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
krista@3300
    94
    string homedir = getenv("HOME");
krista@3300
    95
    TEST_ASSERT_MSG(strcmp(homedir.c_str(), first_device->device_dir.c_str()) == 0, "First device didn't set $HOME correctly.");
krista@3300
    96
    string gpgdir = getenv("GNUPGHOME");
krista@3300
    97
    TEST_ASSERT_MSG(strcmp(gpgdir.c_str(), (first_device->device_dir + "/gnupg").c_str()) == 0, "First device didn't set $GNUPGHOME correctly.");    
krista@3307
    98
krista@3300
    99
    pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Second");
krista@3305
   100
    devices.push_back(second_device);
krista@3300
   101
    homedir = getenv("HOME");
krista@3300
   102
    TEST_ASSERT_MSG(strcmp(homedir.c_str(), second_device->device_dir.c_str()) == 0, "Second device didn't set $HOME correctly");
krista@3300
   103
    gpgdir = getenv("GNUPGHOME");
krista@3300
   104
    TEST_ASSERT_MSG(strcmp(gpgdir.c_str(), (second_device->device_dir + "/gnupg").c_str()) == 0, "Second device didn't set $GNUPGHOME correctly.");    
krista@3300
   105
    second_device->set_mailbox_dir(first_device->device_dir + "/mbox");
krista@3300
   106
    first_device->grab_context(second_device);
krista@3300
   107
    homedir = getenv("HOME");
krista@3300
   108
    TEST_ASSERT_MSG(strcmp(homedir.c_str(), first_device->device_dir.c_str()) == 0, "First device failed to grab context.");
krista@3300
   109
    gpgdir = getenv("GNUPGHOME");
krista@3300
   110
    TEST_ASSERT_MSG(strcmp(gpgdir.c_str(), (first_device->device_dir + "/gnupg").c_str()) == 0, "First device context switch didn't set $GNUPGHOME correctly.");    
krista@3300
   111
    second_device->grab_context(first_device);
krista@3300
   112
    homedir = getenv("HOME");
krista@3300
   113
    TEST_ASSERT_MSG(strcmp(homedir.c_str(), second_device->device_dir.c_str()) == 0, "Second device failed to grab context.");
krista@3300
   114
    gpgdir = getenv("GNUPGHOME");
krista@3300
   115
    TEST_ASSERT_MSG(strcmp(gpgdir.c_str(), (second_device->device_dir + "/gnupg").c_str()) == 0, "Second device context switch didn't set $GNUPGHOME correctly.");        
krista@3300
   116
}
krista@3300
   117
krista@3300
   118
void DeviceModelTests::check_two_device_functionality() {
krista@3300
   119
    // Set up devices and shared mailbox
krista@3300
   120
    pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "First");
krista@3305
   121
    devices.push_back(first_device);    
krista@3300
   122
    first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
krista@3307
   123
    
krista@3300
   124
    pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Second");
krista@3305
   125
    devices.push_back(second_device);    
krista@3300
   126
    second_device->set_mailbox_dir(first_device->device_dir + "/mbox");
krista@3307
   127
krista@3300
   128
    first_device->grab_context(second_device);
krista@3300
   129
    TEST_ASSERT_MSG(first_device->mbox_dir.compare(second_device->mbox_dir) == 0,
krista@3300
   130
                    "Shared mailbox is not really shared");
krista@3300
   131
krista@3300
   132
    string alice_email = "pep.test.alice@pep-project.org";
krista@3300
   133
    string alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
krista@3300
   134
    
krista@3300
   135
    // First device is Alice's established one with the current key
krista@3300
   136
    TEST_ASSERT_MSG(slurp_and_import_key(first_device->session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"),
krista@3300
   137
                    "Alice's pubkey not imported for first device.");
krista@3300
   138
    TEST_ASSERT_MSG(slurp_and_import_key(first_device->session, "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc"),
krista@3300
   139
                    "Alice's privkey not imported for first device.");
krista@3300
   140
krista@3300
   141
    pEp_identity* alice_dev_1_ident = new_identity(alice_email.c_str(), alice_fpr.c_str(), "ALICE", "Alice From Mel's Diner");
krista@3300
   142
    
krista@3300
   143
    PEP_STATUS status = set_own_key(first_device->session, alice_dev_1_ident, alice_fpr.c_str());    
krista@3300
   144
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, 
krista@3300
   145
        (string("Unable to set own key on first device. status is ") + tl_status_string(status)).c_str());
krista@3300
   146
krista@3300
   147
    free(alice_dev_1_ident->fpr);
krista@3300
   148
    alice_dev_1_ident->fpr = NULL;
krista@3300
   149
    status = myself(first_device->session, alice_dev_1_ident);
krista@3300
   150
    TEST_ASSERT(alice_dev_1_ident->fpr);
krista@3300
   151
    TEST_ASSERT_MSG(alice_fpr.compare(alice_dev_1_ident->fpr) == 0,
krista@3300
   152
                    "set_own_key does not seem to have set alice's key for device 1.");
krista@3300
   153
                
krista@3300
   154
    second_device->grab_context(first_device);
krista@3300
   155
krista@3300
   156
    pEp_identity* alice_dev_2_ident = new_identity(alice_email.c_str(), NULL, PEP_OWN_USERID, "Alice Miller");
krista@3300
   157
    // Second device is one Alice is setting up (we'll use this model for keysync tests, so why not?)
krista@3300
   158
krista@3300
   159
    status = myself(second_device->session, alice_dev_2_ident);
krista@3300
   160
krista@3300
   161
    TEST_ASSERT_MSG(alice_dev_2_ident->fpr, "No fpr for alice on second device");
krista@3300
   162
    TEST_ASSERT_MSG(alice_fpr.compare(alice_dev_2_ident->fpr) != 0,
krista@3300
   163
                    "myself did not generate new key for alice on device 2; alice's old key was found.");
krista@3300
   164
    
krista@3300
   165
    const char* alice_2_fpr = alice_dev_2_ident->fpr;
krista@3300
   166
    
krista@3300
   167
    first_device->grab_context(second_device);
krista@3300
   168
    
krista@3300
   169
    stringlist_t* keylist = NULL;
krista@3300
   170
    
krista@3300
   171
    status = find_keys(first_device->session, alice_2_fpr, &keylist);
krista@3300
   172
    
krista@3300
   173
    TEST_ASSERT(!keylist);
krista@3307
   174
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@3300
   175
    free_stringlist(keylist);
krista@3300
   176
    keylist = NULL;
krista@3300
   177
krista@3300
   178
    second_device->grab_context(first_device);
krista@3300
   179
    
krista@3300
   180
    char* alice_2_keydata = NULL;
krista@3300
   181
    size_t alice_2_keydata_size = 0;
krista@3300
   182
    
krista@3307
   183
    status = export_key(second_device->session, alice_2_fpr, &alice_2_keydata, &alice_2_keydata_size);
krista@3300
   184
krista@3300
   185
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@3300
   186
    TEST_ASSERT(alice_2_keydata);
krista@3300
   187
    
krista@3300
   188
    first_device->grab_context(second_device);
krista@3300
   189
krista@3300
   190
    status = import_key(first_device->session, alice_2_keydata, alice_2_keydata_size, NULL);
krista@3300
   191
krista@3300
   192
    free(alice_2_keydata);
krista@3300
   193
    alice_2_keydata = NULL;
krista@3300
   194
krista@3300
   195
    status = find_keys(first_device->session, alice_2_fpr, &keylist);    
krista@3300
   196
    TEST_ASSERT(keylist);
krista@3300
   197
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@3300
   198
    free_stringlist(keylist);
krista@3300
   199
    keylist = NULL;
krista@3300
   200
krista@3300
   201
    second_device->grab_context(first_device);
krista@3300
   202
    TEST_ASSERT_MSG(slurp_and_import_key(second_device->session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"),
krista@3300
   203
                    "Alice's first pubkey not imported for second device.");
krista@3300
   204
krista@3300
   205
    // Ok, so, we're relatively certain we have all that set up. Now let's have both
krista@3300
   206
    // import Bob's key, but only one of them trust it. Then we're sure we have 
krista@3300
   207
    // different, functioning trust dbs, and we're done with this case and ready 
krista@3300
   208
    // to move on to checking mboxes
krista@3300
   209
    string bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
krista@3300
   210
    TEST_ASSERT_MSG(slurp_and_import_key(second_device->session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"), 
krista@3300
   211
                    "Second device couldn't import Bob's pubkey");
krista@3300
   212
    pEp_identity* bob_id = new_identity("pep.test.bob@pep-project.org", NULL, NULL, "Bob Barker");    
krista@3300
   213
    status = update_identity(second_device->session, bob_id);
krista@3300
   214
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@3300
   215
    TEST_ASSERT(bob_id->fpr);
krista@3300
   216
    TEST_ASSERT(bob_fpr.compare(bob_id->fpr) == 0);
krista@3300
   217
krista@3300
   218
    status = trust_personal_key(second_device->session, bob_id);
krista@3300
   219
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@3300
   220
    status = update_identity(second_device->session, bob_id);
krista@3300
   221
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@3300
   222
    TEST_ASSERT(bob_id->comm_type == PEP_ct_OpenPGP);    
krista@3300
   223
    
krista@3300
   224
    first_device->grab_context(second_device);
krista@3300
   225
    TEST_ASSERT_MSG(slurp_and_import_key(first_device->session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"), 
krista@3300
   226
                    "First device couldn't import Bob's pubkey");
krista@3300
   227
    pEp_identity* bob_id_2 = new_identity("pep.test.bob@pep-project.org", NULL, NULL, "Bob Barker");    
krista@3300
   228
    status = update_identity(first_device->session, bob_id_2);
krista@3300
   229
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@3300
   230
    TEST_ASSERT(bob_id_2->comm_type == PEP_ct_OpenPGP_unconfirmed);    
krista@3300
   231
krista@3300
   232
    free_identity(alice_dev_1_ident);                  
krista@3300
   233
    free_identity(alice_dev_2_ident);
krista@3300
   234
    free_identity(bob_id);
krista@3300
   235
    free_identity(bob_id_2);              
krista@3305
   236
}
krista@3305
   237
krista@3305
   238
void DeviceModelTests::check_mbox() {
krista@3305
   239
    // Set up devices and shared mailbox
krista@3305
   240
    pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "Device");
krista@3305
   241
    devices.push_back(first_device);
krista@3305
   242
    
krista@3305
   243
    first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
krista@3305
   244
krista@3305
   245
    string alice_email = "pep.test.alice@pep-project.org";
krista@3305
   246
    string alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
krista@3305
   247
    
krista@3305
   248
    slurp_and_import_key(first_device->session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
krista@3305
   249
    slurp_and_import_key(first_device->session, "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
krista@3305
   250
    
krista@3305
   251
    pEp_identity* alice_ident = new_identity(alice_email.c_str(), alice_fpr.c_str(), "ALICE", "Alice From Mel's Diner");    
krista@3305
   252
    PEP_STATUS status = set_own_key(first_device->session, alice_ident, alice_fpr.c_str());    
krista@3305
   253
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, 
krista@3305
   254
        (string("Unable to set own key. status is ") + tl_status_string(status)).c_str());
krista@3305
   255
        
krista@3305
   256
    message* new_msg = new_message(PEP_dir_outgoing);
krista@3305
   257
    
krista@3305
   258
    new_msg->from = alice_ident;
krista@3305
   259
    new_msg->to = new_identity_list(identity_dup(alice_ident));
krista@3305
   260
    new_msg->longmsg = strdup("Some dumb message.\nBlahblahblah.");
krista@3305
   261
    new_msg->shortmsg = strdup("hello, world");
krista@3305
   262
    new_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3305
   263
krista@3305
   264
    message* enc_msg = NULL;
krista@3305
   265
    
krista@3305
   266
    status = encrypt_message(first_device->session, new_msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@3305
   267
krista@3305
   268
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@3305
   269
    TEST_ASSERT(enc_msg);
krista@3305
   270
    char* msg_text = NULL;
krista@3305
   271
    mime_encode_message(enc_msg, false, &msg_text);
krista@3305
   272
    TEST_ASSERT(msg_text);
krista@3305
   273
    
krista@3306
   274
    string filename = first_device->receive_mail(msg_text);
krista@3305
   275
    TEST_ASSERT(!filename.empty());
krista@3305
   276
krista@3305
   277
    vector<string> curr_mail_received;
krista@3305
   278
    first_device->check_mail(curr_mail_received);
krista@3305
   279
    TEST_ASSERT_MSG(curr_mail_received.size() == 1, 
krista@3305
   280
                    (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 1.").c_str());
krista@3305
   281
krista@3306
   282
    first_device->receive_mail(msg_text);
krista@3306
   283
    first_device->receive_mail(msg_text);
krista@3306
   284
    first_device->receive_mail(msg_text);
krista@3305
   285
    first_device->check_mail(curr_mail_received);
krista@3305
   286
    TEST_ASSERT_MSG(curr_mail_received.size() == 3, 
krista@3305
   287
                    (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 3.").c_str());
krista@3305
   288
    
krista@3306
   289
    first_device->receive_mail(msg_text);
krista@3306
   290
    first_device->receive_mail(msg_text);
krista@3305
   291
    first_device->check_mail(curr_mail_received);
krista@3305
   292
    TEST_ASSERT_MSG(curr_mail_received.size() == 2, 
krista@3305
   293
                    (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 2.").c_str());
krista@3300
   294
}
krista@3307
   295
krista@3307
   296
void DeviceModelTests::check_three_device_mbox_with_send() {
krista@3307
   297
    try {
krista@3307
   298
        // Set up devices and shared mailbox
krista@3307
   299
        pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "Alex");
krista@3307
   300
        devices.push_back(first_device);    
krista@3307
   301
        first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
krista@3307
   302
        string alex_email = "alex@darthmama.cool";
krista@3307
   303
        pEp_identity* alex_identity = new_identity(alex_email.c_str(), NULL, "AlexID", "Alex Braithwaite");
krista@3307
   304
        PEP_STATUS status = myself(first_device->session, alex_identity);
krista@3307
   305
        
krista@3307
   306
        pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Bree");
krista@3307
   307
        devices.push_back(second_device);    
krista@3307
   308
        second_device->set_mailbox_dir(second_device->device_dir + "/mbox");
krista@3307
   309
        string bree_email = "bree@cheese.melted";
krista@3307
   310
        pEp_identity* bree_identity = new_identity(bree_email.c_str(), NULL, "BreeID", "Briana Cheeserton");
krista@3307
   311
        status = myself(second_device->session, bree_identity);
krista@3307
   312
        
krista@3307
   313
        pEpTestDevice* third_device = new pEpTestDevice(temp_test_home, "Charmander");
krista@3307
   314
        devices.push_back(third_device);    
krista@3307
   315
        third_device->set_mailbox_dir(third_device->device_dir + "/mbox");
krista@3307
   316
        string charm_email = "charmander@poke.mon";
krista@3307
   317
        pEp_identity* charm_identity = new_identity(charm_email.c_str(), NULL, "CharmID", "Charmander T. Pokemon");
krista@3307
   318
        status = myself(third_device->session, charm_identity);
krista@3307
   319
        first_device->grab_context(third_device);
krista@3307
   320
krista@3307
   321
        map<string,string> address_maps = {
krista@3307
   322
            {alex_email,first_device->mbox_dir},
krista@3307
   323
            {bree_email,second_device->mbox_dir},
krista@3307
   324
            {charm_email,third_device->mbox_dir},
krista@3307
   325
        };
krista@3307
   326
krista@3307
   327
        // this just simulates the ability to address and deliver, so everyone has
krista@3307
   328
        // the same maps.
krista@3307
   329
        first_device->address_to_mbox_map = second_device->address_to_mbox_map =
krista@3307
   330
            third_device->address_to_mbox_map = address_maps;
krista@3307
   331
        // Note to self - I'll bet this is some C++ mem nightmare.
krista@3307
   332
        
krista@3307
   333
        message* msg = new_message(PEP_dir_outgoing);
krista@3307
   334
        msg->from = identity_dup(alex_identity);
krista@3307
   335
        msg->to = new_identity_list(new_identity(bree_email.c_str(), NULL, "ItsBree", "Bree Cheeserton"));
krista@3307
   336
        msg->shortmsg = strdup("First test message!");
krista@3307
   337
        msg->longmsg = strdup("Yo Bree! This is Alex! Hi!\nEr, hi!\n");
krista@3307
   338
        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3307
   339
krista@3307
   340
        // engine_passthrough
krista@3307
   341
        message* enc_msg = NULL;
krista@3307
   342
        status = encrypt_message(first_device->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@3307
   343
        TEST_ASSERT(status == PEP_UNENCRYPTED);
krista@3307
   344
        TEST_ASSERT(enc_msg == NULL);
krista@3307
   345
krista@3307
   346
        // when sent, msg is freed, so do NOT free it after this.
krista@3307
   347
        first_device->add_message_to_send_queue(msg);
krista@3307
   348
        second_device->grab_context(first_device);
krista@3307
   349
        msg = NULL;
krista@3307
   350
        
krista@3307
   351
        vector<string> inbox_list;
krista@3307
   352
        second_device->check_mail(inbox_list);
krista@3307
   353
krista@3307
   354
        vector<message*> inbox_mails;
krista@3307
   355
        stringlist_t* keylist = NULL;
krista@3307
   356
        PEP_rating rating;
krista@3307
   357
        PEP_decrypt_flags_t flags;
krista@3307
   358
        
krista@3307
   359
        second_device->read_mail(inbox_list, inbox_mails);
krista@3320
   360
        remove_sync_mails(inbox_mails);
krista@3307
   361
        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
krista@3307
   362
        
krista@3307
   363
        // Get Alex's key
krista@3307
   364
        status = decrypt_message(second_device->session,
krista@3307
   365
                                 inbox_mails.at(0), &msg, &keylist,
krista@3307
   366
                                 &rating, &flags);
krista@3307
   367
        TEST_ASSERT(status == PEP_UNENCRYPTED);
krista@3307
   368
        TEST_ASSERT(msg == NULL);
krista@3307
   369
krista@3307
   370
        inbox_list.clear();
krista@3307
   371
        free(inbox_mails.at(0));
krista@3307
   372
        inbox_mails.clear();
krista@3307
   373
        
krista@3307
   374
        third_device->grab_context(second_device);
krista@3307
   375
            
krista@3307
   376
        msg = new_message(PEP_dir_outgoing);
krista@3307
   377
        msg->from = identity_dup(charm_identity);
krista@3307
   378
        msg->to = new_identity_list(new_identity(bree_email.c_str(), NULL, "SuperBree", "Bree Cheeserton"));
krista@3307
   379
        msg->shortmsg = strdup("First test message!");
krista@3307
   380
        msg->longmsg = strdup("Yo Bree! This is Charmander! I'm a cool Pokemon! Hi!\nEr, hi!\n");
krista@3307
   381
        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3307
   382
krista@3307
   383
        // engine_passthrough
krista@3307
   384
        enc_msg = NULL;
krista@3307
   385
        status = encrypt_message(third_device->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@3307
   386
        TEST_ASSERT(status == PEP_UNENCRYPTED);
krista@3307
   387
        TEST_ASSERT(enc_msg == NULL);
krista@3307
   388
krista@3307
   389
        // when sent, msg is freed, so do NOT free it after this.
krista@3307
   390
        third_device->add_message_to_send_queue(msg);
krista@3307
   391
        second_device->grab_context(third_device);
krista@3307
   392
        msg = NULL;
krista@3307
   393
            
krista@3307
   394
        second_device->check_mail(inbox_list);
krista@3320
   395
            
krista@3307
   396
        keylist = NULL;
krista@3307
   397
        flags = 0;
krista@3307
   398
        
krista@3307
   399
        second_device->read_mail(inbox_list, inbox_mails);
krista@3320
   400
        
krista@3320
   401
        remove_sync_mails(inbox_mails);
krista@3320
   402
        
krista@3307
   403
        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
krista@3307
   404
        
krista@3307
   405
        // Get Charmander's key
krista@3307
   406
        status = decrypt_message(second_device->session,
krista@3307
   407
                                 inbox_mails.at(0), &msg, &keylist,
krista@3307
   408
                                 &rating, &flags);
krista@3307
   409
        TEST_ASSERT(status == PEP_UNENCRYPTED);
krista@3307
   410
        TEST_ASSERT(msg == NULL);
krista@3307
   411
krista@3307
   412
        inbox_list.clear();
krista@3307
   413
        free(inbox_mails.at(0));
krista@3307
   414
        inbox_mails.clear();    
krista@3307
   415
krista@3307
   416
        // Ok, now, revenge of encrypting Bree
krista@3307
   417
        msg = new_message(PEP_dir_outgoing);
krista@3307
   418
        msg->from = identity_dup(bree_identity);
krista@3307
   419
        msg->to = new_identity_list(new_identity(alex_email.c_str(), NULL, "Alexei", "Alex Braithwaite is a char in a bad novel"));
krista@3307
   420
        identity_list_add(msg->to, new_identity(charm_email.c_str(), NULL, "Charming", "Charmanderpoke E. Mon is NOT a Pokemon"));
krista@3307
   421
        msg->shortmsg = strdup("Last test message!");
krista@3307
   422
        msg->longmsg = strdup("You guys are fools :)\n");
krista@3307
   423
        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3307
   424
krista@3307
   425
        enc_msg = NULL;
krista@3307
   426
        status = encrypt_message(second_device->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@3307
   427
        TEST_ASSERT(status == PEP_STATUS_OK);
krista@3307
   428
        TEST_ASSERT(enc_msg);
krista@3307
   429
         
krista@3307
   430
        free_message(msg);
krista@3307
   431
        msg = NULL;
krista@3307
   432
        free_stringlist(keylist);
krista@3307
   433
        flags = 0;
krista@3307
   434
        keylist = NULL;
krista@3307
   435
        
krista@3307
   436
        // when sent, enc_msg is freed, so do NOT free it after this.
krista@3307
   437
        second_device->add_message_to_send_queue(enc_msg);
krista@3307
   438
        first_device->grab_context(second_device);
krista@3307
   439
        enc_msg = NULL;
krista@3307
   440
krista@3320
   441
        first_device->check_mail(inbox_list);            
krista@3307
   442
        first_device->read_mail(inbox_list, inbox_mails);
krista@3320
   443
        remove_sync_mails(inbox_mails);
krista@3307
   444
        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
krista@3307
   445
        
krista@3307
   446
        status = decrypt_message(first_device->session,
krista@3307
   447
                                 inbox_mails.at(0), &msg, &keylist,
krista@3307
   448
                                 &rating, &flags);
krista@3307
   449
        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@3307
   450
        TEST_ASSERT_MSG(rating == PEP_rating_reliable, tl_rating_string(rating));
krista@3307
   451
        TEST_ASSERT(msg);
krista@3307
   452
krista@3307
   453
        free_message(msg);
krista@3307
   454
        inbox_list.clear();
krista@3307
   455
        free(inbox_mails.at(0));
krista@3307
   456
        inbox_mails.clear();    
krista@3307
   457
krista@3307
   458
        msg = NULL;
krista@3307
   459
        free_stringlist(keylist);
krista@3307
   460
        flags = 0;
krista@3307
   461
        keylist = NULL;
krista@3307
   462
        
krista@3307
   463
        third_device->grab_context(first_device);
krista@3307
   464
krista@3307
   465
        third_device->check_mail(inbox_list);
krista@3307
   466
            
krista@3307
   467
        third_device->read_mail(inbox_list, inbox_mails);
krista@3320
   468
        remove_sync_mails(inbox_mails);
krista@3320
   469
krista@3307
   470
        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
krista@3307
   471
        
krista@3307
   472
        status = decrypt_message(third_device->session,
krista@3307
   473
                                 inbox_mails.at(0), &msg, &keylist,
krista@3307
   474
                                 &rating, &flags);
krista@3307
   475
        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@3307
   476
        TEST_ASSERT_MSG(rating == PEP_rating_reliable, tl_rating_string(rating));
krista@3307
   477
        TEST_ASSERT(msg);
krista@3307
   478
krista@3307
   479
        free_message(msg);
krista@3307
   480
        inbox_list.clear();
krista@3307
   481
        free(inbox_mails.at(0));
krista@3307
   482
        inbox_mails.clear();    
krista@3307
   483
        free_stringlist(keylist);
krista@3307
   484
    }
krista@3307
   485
    catch (const runtime_error& error) {
krista@3307
   486
        TEST_ASSERT_MSG(false, error.what());
krista@3307
   487
    }
krista@3307
   488
}
krista@3307
   489
krista@3307
   490
void DeviceModelTests::check_switch_context() {
krista@3307
   491
    try {
krista@3307
   492
        // Set up devices and shared mailbox
krista@3307
   493
        pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "Alex");
krista@3307
   494
        devices.push_back(first_device);    
krista@3307
   495
        first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
krista@3307
   496
        string alex_email = "alex@darthmama.cool";
krista@3307
   497
        pEp_identity* alex_identity = new_identity(alex_email.c_str(), NULL, "AlexID", "Alex Braithwaite");
krista@3307
   498
        PEP_STATUS status = myself(first_device->session, alex_identity);
krista@3307
   499
        
krista@3307
   500
        pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Bree");
krista@3307
   501
        devices.push_back(second_device);    
krista@3307
   502
        second_device->set_mailbox_dir(second_device->device_dir + "/mbox");
krista@3307
   503
        string bree_email = "bree@cheese.melted";
krista@3307
   504
        pEp_identity* bree_identity = new_identity(bree_email.c_str(), NULL, "BreeID", "Briana Cheeserton");
krista@3307
   505
        status = myself(second_device->session, bree_identity);
krista@3307
   506
        
krista@3307
   507
        pEpTestDevice* third_device = new pEpTestDevice(temp_test_home, "Charmander");
krista@3307
   508
        devices.push_back(third_device);    
krista@3307
   509
        third_device->set_mailbox_dir(third_device->device_dir + "/mbox");
krista@3307
   510
        string charm_email = "charmander@poke.mon";
krista@3307
   511
        pEp_identity* charm_identity = new_identity(charm_email.c_str(), NULL, "CharmID", "Charmander T. Pokemon");
krista@3307
   512
        status = myself(third_device->session, charm_identity);
krista@3307
   513
        
krista@3307
   514
        pEpTestDevice::switch_context(first_device);
krista@3307
   515
krista@3307
   516
        map<string,string> address_maps = {
krista@3307
   517
            {alex_email,first_device->mbox_dir},
krista@3307
   518
            {bree_email,second_device->mbox_dir},
krista@3307
   519
            {charm_email,third_device->mbox_dir},
krista@3307
   520
        };
krista@3307
   521
krista@3307
   522
        // this just simulates the ability to address and deliver, so everyone has
krista@3307
   523
        // the same maps.
krista@3307
   524
        first_device->address_to_mbox_map = second_device->address_to_mbox_map =
krista@3307
   525
            third_device->address_to_mbox_map = address_maps;
krista@3307
   526
        // Note to self - I'll bet this is some C++ mem nightmare.
krista@3307
   527
        
krista@3307
   528
        message* msg = new_message(PEP_dir_outgoing);
krista@3307
   529
        msg->from = identity_dup(alex_identity);
krista@3307
   530
        msg->to = new_identity_list(new_identity(bree_email.c_str(), NULL, "ItsBree", "Bree Cheeserton"));
krista@3307
   531
        msg->shortmsg = strdup("First test message!");
krista@3307
   532
        msg->longmsg = strdup("Yo Bree! This is Alex! Hi!\nEr, hi!\n");
krista@3307
   533
        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3307
   534
krista@3307
   535
        // engine_passthrough
krista@3307
   536
        message* enc_msg = NULL;
krista@3307
   537
        status = encrypt_message(pEpTestDevice::active->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@3307
   538
        TEST_ASSERT(status == PEP_UNENCRYPTED);
krista@3307
   539
        TEST_ASSERT(enc_msg == NULL);
krista@3307
   540
krista@3307
   541
        // when sent, msg is freed, so do NOT free it after this.
krista@3307
   542
        pEpTestDevice::active->add_message_to_send_queue(msg);
krista@3307
   543
        pEpTestDevice::switch_context(second_device);
krista@3307
   544
krista@3307
   545
        msg = NULL;
krista@3307
   546
        
krista@3307
   547
        vector<string> inbox_list;
krista@3307
   548
        pEpTestDevice::active->check_mail(inbox_list);
krista@3307
   549
krista@3307
   550
        vector<message*> inbox_mails;
krista@3307
   551
        stringlist_t* keylist = NULL;
krista@3307
   552
        PEP_rating rating;
krista@3307
   553
        PEP_decrypt_flags_t flags;
krista@3307
   554
        pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
krista@3320
   555
        remove_sync_mails(inbox_mails);
krista@3320
   556
krista@3307
   557
        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
krista@3307
   558
        
krista@3307
   559
        // Get Alex's key
krista@3307
   560
        status = decrypt_message(pEpTestDevice::active->session,
krista@3307
   561
                                 inbox_mails.at(0), &msg, &keylist,
krista@3307
   562
                                 &rating, &flags);
krista@3307
   563
        TEST_ASSERT(status == PEP_UNENCRYPTED);
krista@3307
   564
        TEST_ASSERT(msg == NULL);
krista@3307
   565
krista@3307
   566
        inbox_list.clear();
krista@3307
   567
        free(inbox_mails.at(0));
krista@3307
   568
        inbox_mails.clear();
krista@3307
   569
        
krista@3307
   570
        pEpTestDevice::switch_context(third_device);
krista@3307
   571
            
krista@3307
   572
        msg = new_message(PEP_dir_outgoing);
krista@3307
   573
        msg->from = identity_dup(charm_identity);
krista@3307
   574
        msg->to = new_identity_list(new_identity(bree_email.c_str(), NULL, "SuperBree", "Bree Cheeserton"));
krista@3307
   575
        msg->shortmsg = strdup("First test message!");
krista@3307
   576
        msg->longmsg = strdup("Yo Bree! This is Charmander! I'm a cool Pokemon! Hi!\nEr, hi!\n");
krista@3307
   577
        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3307
   578
krista@3307
   579
        // engine_passthrough
krista@3307
   580
        enc_msg = NULL;
krista@3307
   581
        status = encrypt_message(pEpTestDevice::active->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@3307
   582
        TEST_ASSERT(status == PEP_UNENCRYPTED);
krista@3307
   583
        TEST_ASSERT(enc_msg == NULL);
krista@3307
   584
krista@3307
   585
        // when sent, msg is freed, so do NOT free it after this.
krista@3307
   586
        pEpTestDevice::active->add_message_to_send_queue(msg);
krista@3307
   587
krista@3307
   588
        pEpTestDevice::switch_context(second_device);
krista@3307
   589
        msg = NULL;
krista@3307
   590
            
krista@3320
   591
        pEpTestDevice::active->check_mail(inbox_list);    
krista@3307
   592
        keylist = NULL;
krista@3307
   593
        flags = 0;
krista@3307
   594
        pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
krista@3320
   595
        remove_sync_mails(inbox_mails);
krista@3320
   596
krista@3307
   597
        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
krista@3307
   598
        
krista@3307
   599
        // Get Charmander's key
krista@3307
   600
        status = decrypt_message(pEpTestDevice::active->session,
krista@3307
   601
                                 inbox_mails.at(0), &msg, &keylist,
krista@3307
   602
                                 &rating, &flags);
krista@3307
   603
        TEST_ASSERT(status == PEP_UNENCRYPTED);
krista@3307
   604
        TEST_ASSERT(msg == NULL);
krista@3307
   605
krista@3307
   606
        inbox_list.clear();
krista@3307
   607
        free(inbox_mails.at(0));
krista@3307
   608
        inbox_mails.clear();    
krista@3307
   609
krista@3307
   610
        // Ok, now, revenge of encrypting Bree
krista@3307
   611
        msg = new_message(PEP_dir_outgoing);
krista@3307
   612
        msg->from = identity_dup(bree_identity);
krista@3307
   613
        msg->to = new_identity_list(new_identity(alex_email.c_str(), NULL, "Alexei", "Alex Braithwaite is a char in a bad novel"));
krista@3307
   614
        identity_list_add(msg->to, new_identity(charm_email.c_str(), NULL, "Charming", "Charmanderpoke E. Mon is NOT a Pokemon"));
krista@3307
   615
        msg->shortmsg = strdup("Last test message!");
krista@3307
   616
        msg->longmsg = strdup("You guys are fools :)\n");
krista@3307
   617
        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3307
   618
krista@3307
   619
        enc_msg = NULL;
krista@3307
   620
        status = encrypt_message(pEpTestDevice::active->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@3307
   621
        TEST_ASSERT(status == PEP_STATUS_OK);
krista@3307
   622
        TEST_ASSERT(enc_msg);
krista@3307
   623
         
krista@3307
   624
        free_message(msg);
krista@3307
   625
        msg = NULL;
krista@3307
   626
        free_stringlist(keylist);
krista@3307
   627
        flags = 0;
krista@3307
   628
        keylist = NULL;
krista@3307
   629
        
krista@3307
   630
        // when sent, enc_msg is freed, so do NOT free it after this.
krista@3307
   631
        pEpTestDevice::active->add_message_to_send_queue(enc_msg);
krista@3307
   632
krista@3307
   633
        pEpTestDevice::switch_context(first_device);
krista@3307
   634
                
krista@3307
   635
        enc_msg = NULL;
krista@3307
   636
krista@3307
   637
        pEpTestDevice::active->check_mail(inbox_list);
krista@3307
   638
            
krista@3307
   639
        pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
krista@3320
   640
        remove_sync_mails(inbox_mails);
krista@3307
   641
        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
krista@3307
   642
        
krista@3307
   643
        status = decrypt_message(pEpTestDevice::active->session,
krista@3307
   644
                                 inbox_mails.at(0), &msg, &keylist,
krista@3307
   645
                                 &rating, &flags);
krista@3307
   646
        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@3307
   647
        TEST_ASSERT_MSG(rating == PEP_rating_reliable, tl_rating_string(rating));
krista@3307
   648
        TEST_ASSERT(msg);
krista@3307
   649
krista@3307
   650
        free_message(msg);
krista@3307
   651
        inbox_list.clear();
krista@3307
   652
        free(inbox_mails.at(0));
krista@3307
   653
        inbox_mails.clear();    
krista@3307
   654
krista@3307
   655
        msg = NULL;
krista@3307
   656
        free_stringlist(keylist);
krista@3307
   657
        flags = 0;
krista@3307
   658
        keylist = NULL;
krista@3307
   659
        
krista@3307
   660
        pEpTestDevice::switch_context(third_device);
krista@3307
   661
krista@3307
   662
        pEpTestDevice::active->check_mail(inbox_list);
krista@3307
   663
        pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
krista@3320
   664
        remove_sync_mails(inbox_mails);
krista@3307
   665
        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
krista@3307
   666
        
krista@3307
   667
        status = decrypt_message(pEpTestDevice::active->session,
krista@3307
   668
                                 inbox_mails.at(0), &msg, &keylist,
krista@3307
   669
                                 &rating, &flags);
krista@3307
   670
        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@3307
   671
        TEST_ASSERT_MSG(rating == PEP_rating_reliable, tl_rating_string(rating));
krista@3307
   672
        TEST_ASSERT(msg);
krista@3307
   673
krista@3307
   674
        free_message(msg);
krista@3307
   675
        inbox_list.clear();
krista@3307
   676
        free(inbox_mails.at(0));
krista@3307
   677
        inbox_mails.clear();    
krista@3307
   678
        free_stringlist(keylist);
krista@3307
   679
    }
krista@3307
   680
    catch (const runtime_error& error) {
krista@3307
   681
        TEST_ASSERT_MSG(false, error.what());
krista@3307
   682
    }
krista@3307
   683
}