test_retrieve.c 23.8 KB
Newer Older
Slava Monich's avatar
Slava Monich committed
1
/*
2
 * Copyright (C) 2013-2017 Jolla Ltd.
3
 * Contact: Slava Monich <slava.monich@jolla.com>
Slava Monich's avatar
Slava Monich committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "test_connman.h"
#include "test_handler.h"
#include "test_http.h"
18
#include "test_transfer_list.h"
19
#include "test_util.h"
Slava Monich's avatar
Slava Monich committed
20 21

#include "mms_codec.h"
Slava Monich's avatar
Slava Monich committed
22
#include "mms_file_util.h"
Slava Monich's avatar
Slava Monich committed
23 24
#include "mms_lib_log.h"
#include "mms_lib_util.h"
25
#include "mms_settings.h"
Slava Monich's avatar
Slava Monich committed
26 27
#include "mms_dispatcher.h"

28
#include <gutil_macros.h>
29
#include <gutil_log.h>
Slava Monich's avatar
Slava Monich committed
30 31 32 33 34 35 36 37
#include <libsoup/soup-status.h>

#define RET_OK      (0)
#define RET_ERR     (1)
#define RET_TIMEOUT (2)

#define MMS_MESSAGE_TYPE_NONE (0)

38 39
#define DATA_DIR  "data/"
#define LOCALHOST "127.0.0.1"
Slava Monich's avatar
Slava Monich committed
40

41 42
#define TEST_TIMEOUT (10) /* seconds */

43 44 45 46 47 48
typedef struct test_part_desc {
    const char* content_type;
    const char* content_id;
    const char* file_name;
} TestPartDesc;

Slava Monich's avatar
Slava Monich committed
49 50 51 52 53 54 55
typedef struct test_desc {
    const char* name;
    const char* dir;
    const char* ni_file;
    const char* rc_file;
    unsigned int status;
    const char* content_type;
Slava Monich's avatar
Slava Monich committed
56
    const char* attic_file;
Slava Monich's avatar
Slava Monich committed
57 58
    MMS_RECEIVE_STATE expected_state;
    enum mms_message_type reply_msg;
59 60
    const TestPartDesc* parts;
    unsigned int nparts;
61
    const char* proxy;
Slava Monich's avatar
Slava Monich committed
62 63 64
    int flags;

#define TEST_PUSH_HANDLING_FAILURE_OK (0x01)
65 66
#define TEST_DEFER_RECEIVE            (0x02)
#define TEST_REJECT_RECEIVE           (0x04)
67 68
#define TEST_CONNECTION_FAILURE       (0x08)
#define TEST_OFFLINE                  (0x10)
69
#define TEST_CANCEL_RECEIVED          (0x20)
Slava Monich's avatar
Slava Monich committed
70 71 72 73 74

} TestDesc;

typedef struct test {
    const TestDesc* desc;
Slava Monich's avatar
Slava Monich committed
75
    const MMSConfig* config;
Slava Monich's avatar
Slava Monich committed
76 77 78 79 80 81 82 83
    MMSDispatcherDelegate delegate;
    MMSConnMan* cm;
    MMSHandler* handler;
    MMSDispatcher* disp;
    GMappedFile* notification_ind;
    GMappedFile* retrieve_conf;
    GMainLoop* loop;
    guint timeout_id;
84
    gulong msgreceived_id;
Slava Monich's avatar
Slava Monich committed
85 86 87 88
    TestHttp* http;
    int ret;
} Test;

89
static const TestPartDesc retrieve_success1_parts [] = {
90 91 92 93 94 95 96 97 98 99 100 101 102
    { "application/smil;charset=utf-8", "<0>", "0.smil" },
    { "text/plain;charset=utf-8", "<text_0011.txt>", "text_0011.txt" },
    { "image/jpeg", "<131200181.jpg>", "131200181.jpg" },
    { "text/plain;charset=utf-8", "<text_0021.txt>", "text_0021.txt" },
    { "image/jpeg", "<140100041.jpg>", "140100041.jpg" }
};

static const TestPartDesc retrieve_success2_parts [] = {
    { "image/jpeg", "<2014_03_.jpg>", "2014_03_.jpg" },
    { "text/plain;charset=utf-8", "<Test_mms.txt>", "Test_mms.txt" },
    { "application/smil;charset=utf-8", "<332047400>", "332047400" },
};

103 104 105 106 107 108
static const TestPartDesc retrieve_success3_parts [] = {
    { "application/smil;charset=utf-8", "<0>", "0" },
    { "image/jpeg", "<1>", "1.jpg" },
    { "text/plain;charset=utf-8", "<2>", "text_001.txt" }
};

109 110 111 112 113 114
static const TestPartDesc retrieve_transfer_encoding_parts [] = {
    { "application/smil;charset=utf-8", "<smil>", "smil" },
    { "image/jpeg", "<1.jpg>", "1.jpg" },
    { "text/plain;charset=utf-8", "<2.txt>", "2.txt" }
};

115 116 117 118 119
static const TestPartDesc retrieve_invalid_subject [] = {
    { "application/smil;charset=us-ascii", "<start>", "smil.smi" },
    { "image/jpeg", "<1>", "1" }
};

120 121 122
#define TEST_PARTS(parts) parts, G_N_ELEMENTS(parts)
#define TEST_PARTS_NONE NULL, 0

Slava Monich's avatar
Slava Monich committed
123 124
static const TestDesc retrieve_tests[] = {
    {
125
        "Success1",
Slava Monich's avatar
Slava Monich committed
126 127 128 129 130
        NULL,
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
Slava Monich's avatar
Slava Monich committed
131
        NULL,
Slava Monich's avatar
Slava Monich committed
132 133
        MMS_RECEIVE_STATE_DECODING,
        MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
134
        TEST_PARTS(retrieve_success1_parts),
135
        "0127.000.000.001",
Slava Monich's avatar
Slava Monich committed
136
        0
137 138 139
    },{
        "Success2", /* Generated by Nokia C6 (Symbian "Belle") */
        NULL,
Slava Monich's avatar
Slava Monich committed
140 141 142 143
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
Slava Monich's avatar
Slava Monich committed
144
        NULL,
Slava Monich's avatar
Slava Monich committed
145 146
        MMS_RECEIVE_STATE_DECODING,
        MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
147
        TEST_PARTS(retrieve_success2_parts),
148
        "127.0.0.001",
149
        0
Slava Monich's avatar
Slava Monich committed
150
    },{
151
        "Success3", /* Generated by Nokia N9 */
152 153 154 155 156 157 158 159
        NULL,
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
        NULL,
        MMS_RECEIVE_STATE_DECODING,
        MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
160
        TEST_PARTS(retrieve_success3_parts),
161
        LOCALHOST,
162
        0
163 164 165 166 167 168 169 170 171 172 173
    },{
        "QuotedPrintable", /* quoted-printable transfer encoding for text */
        NULL,
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
        NULL,
        MMS_RECEIVE_STATE_DECODING,
        MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
        TEST_PARTS(retrieve_transfer_encoding_parts),
174
        LOCALHOST,
175 176 177 178 179 180 181 182 183 184 185 186
        0
    },{
        "Base64", /* Base64 transfer encoding for text */
        NULL,
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
        NULL,
        MMS_RECEIVE_STATE_DECODING,
        MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
        TEST_PARTS(retrieve_transfer_encoding_parts),
187
        LOCALHOST,
188
        0
189 190 191 192 193 194 195 196 197 198 199
    },{
        "InvalidSubject",
        NULL,
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
        NULL,
        MMS_RECEIVE_STATE_DECODING,
        MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
        TEST_PARTS(retrieve_invalid_subject),
200
        LOCALHOST,
201
        0
202 203 204 205 206 207 208 209 210 211 212
   },{
        "DeferSuccess",
        "Success1",
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
        NULL,
        MMS_RECEIVE_STATE_DECODING,
        MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
        TEST_PARTS(retrieve_success1_parts),
213
        LOCALHOST,
214
        TEST_DEFER_RECEIVE
215 216 217 218 219 220 221 222 223 224 225
    },{
        "CancelReceive",
        "Success1",
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
        NULL,
        MMS_RECEIVE_STATE_DECODING,
        MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
        TEST_PARTS(retrieve_success1_parts),
226
        LOCALHOST,
227
        TEST_DEFER_RECEIVE | TEST_CANCEL_RECEIVED
228
     },{
229
        "Expired1",
Slava Monich's avatar
Slava Monich committed
230 231 232 233 234
        NULL,
        "m-notification.ind",
        NULL,
        SOUP_STATUS_NOT_FOUND,
        NULL,
Slava Monich's avatar
Slava Monich committed
235
        NULL,
Slava Monich's avatar
Slava Monich committed
236 237
        MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
        MMS_MESSAGE_TYPE_NONE,
238
        TEST_PARTS_NONE,
239
        LOCALHOST,
Slava Monich's avatar
Slava Monich committed
240
        0
241 242 243 244 245 246 247 248 249 250 251
     },{
        "Expired2",
        NULL,
        "m-notification.ind",
        NULL,
        SOUP_STATUS_OK,
        NULL,
        NULL,
        MMS_RECEIVE_STATE_INVALID,
        MMS_MESSAGE_TYPE_NOTIFYRESP_IND,
        TEST_PARTS_NONE,
252
        LOCALHOST,
253
        TEST_REJECT_RECEIVE
Slava Monich's avatar
Slava Monich committed
254 255 256 257 258 259 260
    },{
        "SoonExpired",
        NULL,
        "m-notification.ind",
        NULL,
        SOUP_STATUS_TRY_AGAIN,
        NULL,
Slava Monich's avatar
Slava Monich committed
261
        NULL,
Slava Monich's avatar
Slava Monich committed
262 263
        MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
        MMS_MESSAGE_TYPE_NONE,
264
        TEST_PARTS_NONE,
265
        LOCALHOST,
Slava Monich's avatar
Slava Monich committed
266
        0
267 268 269 270 271 272 273 274 275 276 277
    },{
        "Offline",
        NULL,
        "m-notification.ind",
        NULL,
        0,
        NULL,
        NULL,
        MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
        MMS_MESSAGE_TYPE_NONE,
        TEST_PARTS_NONE,
278
        LOCALHOST,
279 280 281 282 283 284 285 286 287 288 289 290
        TEST_OFFLINE
    },{
        "Timeout",
        NULL,
        "m-notification.ind",
        NULL,
        0,
        NULL,
        NULL,
        MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
        MMS_MESSAGE_TYPE_NONE,
        TEST_PARTS_NONE,
291
        LOCALHOST,
292
        TEST_CONNECTION_FAILURE
Slava Monich's avatar
Slava Monich committed
293 294 295 296 297 298 299
    },{
        "NotAllowed",
        NULL,
        "m-notification.ind",
        "not-allowed.html",
        SOUP_STATUS_BAD_REQUEST,
        "text/html",
Slava Monich's avatar
Slava Monich committed
300
        NULL,
Slava Monich's avatar
Slava Monich committed
301 302
        MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
        MMS_MESSAGE_TYPE_NONE,
303
        TEST_PARTS_NONE,
304
        LOCALHOST,
Slava Monich's avatar
Slava Monich committed
305 306 307 308 309 310 311 312
        0
    },{
        "NotFound",
        NULL,
        "m-notification.ind",
        "not-found.html",
        SOUP_STATUS_NOT_FOUND,
        "text/html",
Slava Monich's avatar
Slava Monich committed
313
        NULL,
Slava Monich's avatar
Slava Monich committed
314 315
        MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
        MMS_MESSAGE_TYPE_NONE,
316
        TEST_PARTS_NONE,
317
        LOCALHOST,
Slava Monich's avatar
Slava Monich committed
318
        0
319 320 321 322 323 324 325 326 327 328 329
    },{
        "MessageNotFound",
        NULL,
        "m-notification.ind",
        "m-retrieve.conf",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
        NULL,
        MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
        MMS_MESSAGE_TYPE_NONE,
        TEST_PARTS_NONE,
330
        LOCALHOST,
331
        0
Slava Monich's avatar
Slava Monich committed
332 333 334 335 336 337 338
    },{
        "GarbageRetrieve",
        NULL,
        "m-notification.ind",
        "garbage",
        SOUP_STATUS_OK,
        MMS_CONTENT_TYPE,
Slava Monich's avatar
Slava Monich committed
339
        NULL,
Slava Monich's avatar
Slava Monich committed
340 341
        MMS_RECEIVE_STATE_DECODING_ERROR,
        MMS_MESSAGE_TYPE_NOTIFYRESP_IND,
342
        TEST_PARTS_NONE,
343
        LOCALHOST,
Slava Monich's avatar
Slava Monich committed
344 345 346 347 348 349 350 351
        0
    },{
        "GarbagePush",
        NULL,
        "garbage",
        NULL,
        0,
        NULL,
Slava Monich's avatar
Slava Monich committed
352
        "000/push.pdu",
Slava Monich's avatar
Slava Monich committed
353 354
        MMS_RECEIVE_STATE_INVALID,
        MMS_MESSAGE_TYPE_NONE,
355
        TEST_PARTS_NONE,
356
        LOCALHOST,
Slava Monich's avatar
Slava Monich committed
357 358 359 360 361 362 363 364
        TEST_PUSH_HANDLING_FAILURE_OK
    },{
        "UnsupportedPush",
        NULL,
        "unsupported",
        NULL,
        0,
        NULL,
Slava Monich's avatar
Slava Monich committed
365
        "000/push.pdu",
Slava Monich's avatar
Slava Monich committed
366 367
        MMS_RECEIVE_STATE_INVALID,
        MMS_MESSAGE_TYPE_NONE,
368
        TEST_PARTS_NONE,
369
        LOCALHOST,
Slava Monich's avatar
Slava Monich committed
370 371 372 373
        TEST_PUSH_HANDLING_FAILURE_OK
    }
};

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
static
gboolean
test_files_equal(
    const char* path1,
    const char* path2)
{
    gboolean equal = FALSE;
    if (path1 && path2) {
        GError* error = NULL;
        GMappedFile* f1 = g_mapped_file_new(path1, FALSE, &error);
        if (f1) {
            GMappedFile* f2 = g_mapped_file_new(path2, FALSE, &error);
            if (f2) {
                const gsize size = g_mapped_file_get_length(f1);
                if (g_mapped_file_get_length(f2) == size) {
                    const void* data1 = g_mapped_file_get_contents(f1);
                    const void* data2 = g_mapped_file_get_contents(f2);
                    equal = !memcmp(data1, data2, size);
                }
                g_mapped_file_unref(f2);
            }
            g_mapped_file_unref(f1);
        }
    }
    if (!equal) {
399
        GERR("%s is not identical to %s", path1, path2);
400 401 402 403
    }
    return equal;
}

404 405 406 407 408 409 410 411 412 413
static
int
test_validate_parts(
    Test* test)
{
    const TestDesc* desc = test->desc;
    MMSMessage* msg = mms_handler_test_get_received_message(test->handler,NULL);
    if (!desc->nparts && (!msg || !g_slist_length(msg->parts))) {
        return RET_OK;
    } else {
414
        const char* dir = desc->dir ? desc->dir : desc->name;
415 416 417 418 419 420
        const unsigned int nparts = g_slist_length(msg->parts);
        if (desc->nparts == nparts) {
            const TestPartDesc* expect = test->desc->parts;
            GSList* list = msg->parts;
            while (list) {
                const MMSMessagePart* part = list->data;
421 422 423
                char* sample = g_strconcat(DATA_DIR, dir, "/parts/",
                    expect->file_name, NULL);
                const gboolean match = test_files_equal(part->file, sample);
424
                const char* fname;
425

426 427 428
                G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
                fname = g_basename(part->file);
                G_GNUC_END_IGNORE_DEPRECATIONS;
429 430 431 432 433 434 435

                g_free(sample);
                if (!match) {
                    /* Message message is printed by test_files_equal */
                    return RET_ERR;
                }
                
436
                if (strcmp(expect->content_type, part->content_type)) {
437
                    GERR("Content type mismatch: expected %s, got %s",
438 439 440 441
                        expect->content_type, part->content_type);
                    return RET_ERR;
                }
                if (strcmp(expect->file_name, fname)) {
442
                    GERR("File name mismatch: expected %s, got %s",
443 444 445 446
                        expect->file_name, fname);
                    return RET_ERR;
                }
                if (!part->content_id) {
447
                    GERR("Missing content id");
448 449 450
                    return RET_ERR;
                }
                if (strcmp(expect->content_id, part->content_id)) {
451
                    GERR("Content-ID mismatch: expected %s, got %s",
452 453 454
                        expect->content_id, part->content_id);
                    return RET_ERR;
                }
455
               list = list->next;
456 457 458 459
                expect++;
            }
            return RET_OK;
        }
460
        GERR("%u parts expected, got %u", desc->nparts, nparts);
461 462 463 464
        return RET_ERR;
    }
}

Slava Monich's avatar
Slava Monich committed
465 466 467 468 469
static
void
test_finish(
    Test* test)
{
Slava Monich's avatar
Slava Monich committed
470 471
    const TestDesc* desc = test->desc;
    const char* name = desc->name;
Slava Monich's avatar
Slava Monich committed
472 473 474
    if (test->ret == RET_OK) {
        MMS_RECEIVE_STATE state;
        state = mms_handler_test_receive_state(test->handler, NULL);
Slava Monich's avatar
Slava Monich committed
475
        if (state != desc->expected_state) {
Slava Monich's avatar
Slava Monich committed
476
            test->ret = RET_ERR;
477
            GERR("%s state %d, expected %d", name, state,
Slava Monich's avatar
Slava Monich committed
478
                desc->expected_state);
Slava Monich's avatar
Slava Monich committed
479 480 481 482 483 484
        } else {
            const void* resp_data = NULL;
            gsize resp_len = 0;
            GBytes* reply = test_http_get_post_data(test->http);
            if (reply) resp_data = g_bytes_get_data(reply, &resp_len);
            if (resp_len > 0) {
Slava Monich's avatar
Slava Monich committed
485
                if (desc->reply_msg) {
Slava Monich's avatar
Slava Monich committed
486 487
                    MMSPdu* pdu = g_new0(MMSPdu, 1);
                    if (mms_message_decode(resp_data, resp_len, pdu)) {
488 489 490
                        if (pdu->type == desc->reply_msg) {
                            test->ret = test_validate_parts(test);
                        } else {
Slava Monich's avatar
Slava Monich committed
491
                            test->ret = RET_ERR;
492
                            GERR("%s reply %u, expected %u", name,
Slava Monich's avatar
Slava Monich committed
493
                                pdu->type, desc->reply_msg);
Slava Monich's avatar
Slava Monich committed
494 495 496
                        }
                    } else {
                        test->ret = RET_ERR;
497
                        GERR("%s can't decode reply message", name);
Slava Monich's avatar
Slava Monich committed
498 499 500 501
                    }
                    mms_message_free(pdu);
                } else {
                    test->ret = RET_ERR;
502
                    GERR("%s expects no reply", name);
Slava Monich's avatar
Slava Monich committed
503
                }
Slava Monich's avatar
Slava Monich committed
504
            } else if (desc->reply_msg) {
Slava Monich's avatar
Slava Monich committed
505
                test->ret = RET_ERR;
506
                GERR("%s expects reply", name);
Slava Monich's avatar
Slava Monich committed
507 508 509
            }
        }
    }
Slava Monich's avatar
Slava Monich committed
510 511 512 513 514
    if (test->ret == RET_OK && desc->attic_file) {
        const char* dir = desc->dir ? desc->dir : desc->name;
        char* f1 = g_strconcat(DATA_DIR, dir, "/", desc->ni_file, NULL);
        char* f2 = g_strconcat(test->config->root_dir, "/" MMS_ATTIC_DIR "/",
            desc->attic_file, NULL);
515
        if (test_files_equal(f1, f2)) {
Slava Monich's avatar
Slava Monich committed
516 517
            char* dir = g_path_get_dirname(f2);
            remove(f2);
Slava Monich's avatar
Slava Monich committed
518
            rmdir(dir);
Slava Monich's avatar
Slava Monich committed
519 520 521 522 523 524 525
            g_free(dir);
        } else {
            test->ret = RET_ERR;
        }
        g_free(f1);
        g_free(f2);
    }
526
    GINFO("%s: %s", (test->ret == RET_OK) ? "OK" : "FAILED", name);
Slava Monich's avatar
Slava Monich committed
527
    mms_handler_test_reset(test->handler);
528
    g_signal_handler_disconnect(test->handler, test->msgreceived_id);
Slava Monich's avatar
Slava Monich committed
529 530 531 532 533 534 535 536 537
    g_main_loop_quit(test->loop);
}

static
void
test_done(
    MMSDispatcherDelegate* delegate,
    MMSDispatcher* dispatcher)
{
538
    Test* test = G_CAST(delegate,Test,delegate);
Slava Monich's avatar
Slava Monich committed
539 540 541 542 543 544 545 546 547 548 549 550 551
    if (!mms_handler_test_receive_pending(test->handler, NULL)) {
        test_finish(test);
    }
}

static
gboolean
test_timeout(
    gpointer data)
{
    Test* test = data;
    test->timeout_id = 0;
    test->ret = RET_TIMEOUT;
552
    GINFO("%s TIMEOUT", test->desc->name);
Slava Monich's avatar
Slava Monich committed
553 554 555 556 557 558
    if (test->http) test_http_close(test->http);
    mms_connman_test_close_connection(test->cm);
    mms_dispatcher_cancel(test->disp, NULL);
    return FALSE;
}

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
static
gboolean
test_notify(
    MMSHandler* handler,
    const char* imsi,
    const char* from,
    const char* subject,
    time_t expiry,
    GBytes* data,
    void* param)
{
    Test* test = param;
    return !(test->desc->flags & TEST_REJECT_RECEIVE);
}

typedef struct test_cancel_msg {
    MMSDispatcher* disp;
    MMSMessage* msg;
} TestCancelMsg;

static
gboolean
test_cancel_msg_proc(
    void* param)
{
   TestCancelMsg* cancel = param;
585
   GVERBOSE("Cancelling message %s", cancel->msg->id);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
   mms_dispatcher_cancel(cancel->disp, cancel->msg->id);
   mms_dispatcher_unref(cancel->disp);
   mms_message_unref(cancel->msg);
   g_free(cancel);
   return FALSE;
}

static
void
test_msgreceived(
    MMSHandler* handler,
    MMSMessage* msg,
    void* param)
{
    Test* test = param;
    if (test->desc->flags & TEST_CANCEL_RECEIVED) {
        TestCancelMsg* cancel = g_new(TestCancelMsg,1);
        cancel->disp = mms_dispatcher_ref(test->disp);
        cancel->msg = mms_message_ref(msg);
        g_idle_add_full(G_PRIORITY_HIGH, test_cancel_msg_proc, cancel, NULL);
    }
}

Slava Monich's avatar
Slava Monich committed
609 610 611 612 613
static
gboolean
test_init(
    Test* test,
    const MMSConfig* config,
614 615
    const TestDesc* desc,
    gboolean debug)
Slava Monich's avatar
Slava Monich committed
616 617 618 619 620 621 622
{
    gboolean ok = FALSE;
    GError* error = NULL;
    const char* dir = desc->dir ? desc->dir : desc->name;
    char* ni = g_strconcat(DATA_DIR, dir, "/", desc->ni_file, NULL);
    char* rc = desc->rc_file ? g_strconcat(DATA_DIR, dir, "/",
        desc->rc_file, NULL) : NULL;
623
    GDEBUG(">>>>>>>>>> %s <<<<<<<<<<", desc->name);
Slava Monich's avatar
Slava Monich committed
624
    memset(test, 0, sizeof(*test));
Slava Monich's avatar
Slava Monich committed
625
    test->config = config;
Slava Monich's avatar
Slava Monich committed
626 627 628 629
    test->notification_ind = g_mapped_file_new(ni, FALSE, &error);
    if (test->notification_ind) {
        if (rc) test->retrieve_conf = g_mapped_file_new(rc, FALSE, &error);
        if (test->retrieve_conf || !rc) {
630
            MMSSettings* settings = mms_settings_default_new(config);
631
            MMSTransferList* transfers = mms_transfer_list_test_new();
Slava Monich's avatar
Slava Monich committed
632 633 634 635
            g_mapped_file_ref(test->notification_ind);
            test->desc = desc;
            test->cm = mms_connman_test_new();
            test->handler = mms_handler_test_new();
636 637
            test->disp = mms_dispatcher_new(settings, test->cm, test->handler,
                transfers);
Slava Monich's avatar
Slava Monich committed
638
            test->loop = g_main_loop_new(NULL, FALSE);
639 640 641 642
            if (!debug) {
                test->timeout_id = g_timeout_add_seconds(TEST_TIMEOUT,
                    test_timeout, test);
            }
Slava Monich's avatar
Slava Monich committed
643 644
            test->delegate.fn_done = test_done;
            mms_dispatcher_set_delegate(test->disp, &test->delegate);
645 646 647
            if (!(desc->flags & TEST_CONNECTION_FAILURE)) {
                test->http = test_http_new(test->retrieve_conf,
                    test->desc->content_type, test->desc->status);
648 649
                mms_connman_test_set_proxy(test->cm, desc->proxy,
                    test_http_get_port(test->http));
650 651 652 653
            }
            if (desc->flags & TEST_OFFLINE) {
                mms_connman_test_set_offline(test->cm, TRUE);
            }
Slava Monich's avatar
Slava Monich committed
654 655 656
            if (desc->flags & TEST_DEFER_RECEIVE) {
                mms_handler_test_defer_receive(test->handler, test->disp);
            }
657
            mms_handler_test_set_prenotify_fn(test->handler, test_notify, test);
658 659 660
            test->msgreceived_id =
                mms_handler_test_add_msgreceived_fn(test->handler,
                    test_msgreceived, test);
661
            mms_transfer_list_unref(transfers);
662
            mms_settings_unref(settings);
Slava Monich's avatar
Slava Monich committed
663 664 665
            test->ret = RET_ERR;
            ok = TRUE;
        } else {
666
            GERR("%s", GERRMSG(error));
Slava Monich's avatar
Slava Monich committed
667 668 669 670
            g_error_free(error);
        }
        g_mapped_file_unref(test->notification_ind);
    } else {
671
        GERR("%s", GERRMSG(error));
Slava Monich's avatar
Slava Monich committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
        g_error_free(error);
    }
    g_free(ni);
    g_free(rc);
    return ok;
}

static
void
test_finalize(
    Test* test)
{
    if (test->timeout_id) {
        g_source_remove(test->timeout_id);
        test->timeout_id = 0;
    }
    if (test->http) {
        test_http_close(test->http);
        test_http_unref(test->http);
    }
    mms_connman_test_close_connection(test->cm);
    mms_connman_unref(test->cm);
    mms_handler_unref(test->handler);
    mms_dispatcher_unref(test->disp);
    g_main_loop_unref(test->loop);
    g_mapped_file_unref(test->notification_ind);
    if (test->retrieve_conf) g_mapped_file_unref(test->retrieve_conf);
}

static
int
test_retrieve_once(
    const MMSConfig* config,
705 706
    const TestDesc* desc,
    gboolean debug)
Slava Monich's avatar
Slava Monich committed
707 708
{
    Test test;
709
    if (test_init(&test, config, desc, debug)) {
710
        GError* error = NULL;
Slava Monich's avatar
Slava Monich committed
711 712 713
        GBytes* push = g_bytes_new_static(
            g_mapped_file_get_contents(test.notification_ind),
            g_mapped_file_get_length(test.notification_ind));
714 715
        if (mms_dispatcher_handle_push(test.disp, "TestConnection",
            push, &error)) {
Slava Monich's avatar
Slava Monich committed
716 717 718 719
            if (mms_dispatcher_start(test.disp)) {
                test.ret = RET_OK;
                g_main_loop_run(test.loop);
            } else {
720
                GINFO("%s FAILED", desc->name);
Slava Monich's avatar
Slava Monich committed
721 722
            }
        } else {
723
            g_error_free(error);
Slava Monich's avatar
Slava Monich committed
724 725
            if (desc->flags & TEST_PUSH_HANDLING_FAILURE_OK) {
                test.ret = RET_OK;
Slava Monich's avatar
Slava Monich committed
726
                test_finish(&test);
Slava Monich's avatar
Slava Monich committed
727
            } else {
728
                GINFO("%s FAILED", desc->name);
Slava Monich's avatar
Slava Monich committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742
            }
        }
        g_bytes_unref(push);
        test_finalize(&test);
        return test.ret;
    } else {
        return RET_ERR;
    }
}

static
int
test_retrieve(
    const MMSConfig* config,
743 744
    const char* name,
    gboolean debug)
Slava Monich's avatar
Slava Monich committed
745 746 747 748 749 750 751
{
    int i, ret;
    if (name) {
        const TestDesc* found = NULL;
        for (i=0, ret = RET_ERR; i<G_N_ELEMENTS(retrieve_tests); i++) {
            const TestDesc* test = retrieve_tests + i;
            if (!strcmp(test->name, name)) {
752
                ret = test_retrieve_once(config, test, debug);
Slava Monich's avatar
Slava Monich committed
753 754 755 756
                found = test;
                break;
            }
        }
757
        if (!found) GERR("No such test: %s", name);
Slava Monich's avatar
Slava Monich committed
758 759
    } else {
        for (i=0, ret = RET_OK; i<G_N_ELEMENTS(retrieve_tests); i++) {
760 761
            int status = test_retrieve_once(config, retrieve_tests + i, debug);
            if (ret == RET_OK && status != RET_OK) ret = status;
Slava Monich's avatar
Slava Monich committed
762 763 764 765 766 767 768
        }
    }
    return ret;
}

int main(int argc, char* argv[])
{
769 770 771
    int ret = RET_ERR;
    gboolean keep_temp = FALSE;
    gboolean verbose = FALSE;
772
    gboolean debug = FALSE;
773 774 775 776 777 778 779
    GError* error = NULL;
    GOptionContext* options;
    GOptionEntry entries[] = {
        { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
          "Enable verbose output", NULL },
        { "keep", 'k', 0, G_OPTION_ARG_NONE, &keep_temp,
          "Keep temporary files", NULL },
780 781
        { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
          "Disable timeout for debugging", NULL },
782 783
        { NULL }
    };
Slava Monich's avatar
Slava Monich committed
784

785 786
    options = g_option_context_new("[TEST] - MMS retrieve test");
    g_option_context_add_main_entries(options, entries, NULL);
787
    if (g_option_context_parse(options, &argc, &argv, &error)) {
788
        const char* test = "test_retrieve";
789
        MMSConfig config;
790
        TestDirs dirs;
791

792
        mms_lib_init(argv[0]);
793
        gutil_log_set_type(GLOG_TYPE_STDOUT, test);
794
        if (verbose) {
795
            gutil_log_default.level = GLOG_LEVEL_VERBOSE;
796
        } else {
797 798
            gutil_log_timestamp = FALSE;
            gutil_log_default.level = GLOG_LEVEL_INFO;
799 800 801
            mms_task_http_log.level =
            mms_task_decode_log.level =
            mms_task_retrieve_log.level =
802
            mms_task_notification_log.level = GLOG_LEVEL_NONE;
803 804
        }

805 806 807 808 809 810 811
        test_dirs_init(&dirs, test);
        mms_lib_default_config(&config);
        config.root_dir = dirs.root;
        config.keep_temp_files = keep_temp;
        config.idle_secs = 0;
        config.attic_enabled = TRUE;

812
        if (argc < 2) {
813
            ret = test_retrieve(&config, NULL, debug);
814 815 816
        } else {
            int i;
            for (i=1, ret = RET_OK; i<argc; i++) {
817
                int test_status =  test_retrieve(&config, argv[i], debug);
818 819 820 821
                if (ret == RET_OK && test_status != RET_OK) ret = test_status;
            }
        }

822
        test_dirs_cleanup(&dirs, !keep_temp);
823
        mms_lib_deinit();
Slava Monich's avatar
Slava Monich committed
824
    } else {
825
        fprintf(stderr, "%s\n", GERRMSG(error));
826
        g_error_free(error);
Slava Monich's avatar
Slava Monich committed
827
        ret = RET_ERR;
Slava Monich's avatar
Slava Monich committed
828
    }
829
    g_option_context_free(options);
Slava Monich's avatar
Slava Monich committed
830
    return ret;
Slava Monich's avatar
Slava Monich committed
831 832 833 834 835 836 837 838 839
}

/*
 * Local Variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */