/
test_send.c
568 lines (538 loc) · 15.1 KB
/
test_send.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright (C) 2013-2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* 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"
#include "mms_log.h"
#include "mms_codec.h"
#include "mms_file_util.h"
#include "mms_lib_log.h"
#include "mms_lib_util.h"
#include "mms_settings.h"
#include "mms_dispatcher.h"
#include <gio/gio.h>
#include <libsoup/soup-status.h>
#define RET_OK (0)
#define RET_ERR (1)
#define RET_TIMEOUT (2)
#define DATA_DIR "data/"
typedef struct test_desc {
const char* name;
const MMSAttachmentInfo* parts;
int nparts;
gsize size_limit;
const char* subject;
const char* to;
const char* cc;
const char* bcc;
const char* imsi;
unsigned int flags;
const char* resp_file;
const char* resp_type;
unsigned int resp_status;
MMS_SEND_STATE expected_state;
const char* details;
const char* msgid;
} TestDesc;
#define TEST_FLAG_CANCEL (0x1000)
#define TEST_FLAG_NO_SIM (0x2000)
#define TEST_FLAG_REQUEST_DELIVERY_REPORT MMS_SEND_FLAG_REQUEST_DELIVERY_REPORT
#define TEST_FLAG_REQUEST_READ_REPORT MMS_SEND_FLAG_REQUEST_READ_REPORT
/* ASSERT that test and dispatcher flags don't interfere with each other */
#define TEST_DISPATCHER_FLAGS (\
TEST_FLAG_REQUEST_DELIVERY_REPORT |\
TEST_FLAG_REQUEST_READ_REPORT )
#define TEST_PRIVATE_FLAGS (\
TEST_FLAG_CANCEL |\
TEST_FLAG_NO_SIM )
G_STATIC_ASSERT(!(TEST_PRIVATE_FLAGS & TEST_DISPATCHER_FLAGS));
typedef struct test {
const TestDesc* desc;
const MMSConfig* config;
MMSAttachmentInfo* parts;
char** files;
MMSDispatcherDelegate delegate;
MMSConnMan* cm;
MMSHandler* handler;
MMSDispatcher* disp;
GMainLoop* loop;
guint timeout_id;
TestHttp* http;
char* id;
GMappedFile* resp_file;
int ret;
} Test;
static const MMSAttachmentInfo test_files_accept [] = {
{ "smil", "application/smil;charset=us-ascii", NULL },
{ "0001.jpg", "image/jpeg;name=0001.jpg", "image" },
{ "test.txt", "text/plain;charset=utf-8;name=wrong.name", "text" }
};
static const MMSAttachmentInfo test_files_accept_no_ext [] = {
{ "smil", NULL, NULL },
{ "0001", NULL, "image1" },
{ "0001", "", "image2" },
{ "test.text", "text/plain;charset=utf-8", "text" }
};
static const MMSAttachmentInfo test_files_reject [] = {
{ "0001.png", "image/png", "image" },
{ "test.txt", "text/plain", "text" }
};
static const MMSAttachmentInfo test_txt [] = {
{ "test.txt", "text/plain", "text" }
};
#define ATTACHMENTS(a) a, G_N_ELEMENTS(a)
static const TestDesc send_tests[] = {
{
"Accept",
ATTACHMENTS(test_files_accept),
0,
"Test of successful delivery",
"+1234567890",
"+2345678901,+3456789012",
"+4567890123",
"IMSI",
0,
"m-send.conf",
MMS_CONTENT_TYPE,
SOUP_STATUS_OK,
MMS_SEND_STATE_SENDING,
NULL,
"TestMessageId"
},{
"AcceptNoExt",
ATTACHMENTS(test_files_accept_no_ext),
0,
"Test of successful delivery (no extensions)",
"+1234567890",
"+2345678901,+3456789012",
"+4567890123",
"IMSI",
0,
"m-send.conf",
MMS_CONTENT_TYPE,
SOUP_STATUS_OK,
MMS_SEND_STATE_SENDING,
NULL,
"TestMessageId"
},{
"ServiceDenied",
ATTACHMENTS(test_files_reject),
0,
"Rejection test",
"+1234567890",
NULL,
NULL,
NULL,
0,
"m-send.conf",
MMS_CONTENT_TYPE,
SOUP_STATUS_OK,
MMS_SEND_STATE_REFUSED,
"Unable to send",
NULL
},{
"Failure",
ATTACHMENTS(test_files_reject),
0,
"Failure test",
"+1234567890",
NULL,
NULL,
NULL,
0,
"m-send.conf",
MMS_CONTENT_TYPE,
SOUP_STATUS_OK,
MMS_SEND_STATE_SEND_ERROR,
NULL,
NULL
},{
"UnparsableResp",
ATTACHMENTS(test_txt),
0,
"Testing unparsable response",
"+1234567890",
NULL,
NULL,
NULL,
0,
"m-send.conf",
MMS_CONTENT_TYPE,
SOUP_STATUS_OK,
MMS_SEND_STATE_SEND_ERROR,
NULL,
NULL
},{
"UnexpectedResp",
ATTACHMENTS(test_txt),
0,
"Testing unexpected response",
"+1234567890",
NULL,
NULL,
NULL,
0,
"m-send.conf",
MMS_CONTENT_TYPE,
SOUP_STATUS_OK,
MMS_SEND_STATE_SEND_ERROR,
NULL,
NULL
},{
"EmptyMessageID",
ATTACHMENTS(test_txt),
0,
"Testing empty message id",
"+1234567890",
NULL,
NULL,
NULL,
0,
"m-send.conf",
MMS_CONTENT_TYPE,
SOUP_STATUS_OK,
MMS_SEND_STATE_SEND_ERROR,
NULL,
NULL
},{
"Cancel",
ATTACHMENTS(test_txt),
0,
"Failure test",
"+1234567890",
NULL,
NULL,
NULL,
TEST_FLAG_CANCEL,
NULL,
NULL,
SOUP_STATUS_INTERNAL_SERVER_ERROR,
MMS_SEND_STATE_SEND_ERROR,
NULL,
NULL
},{
"TooBig",
ATTACHMENTS(test_txt),
100,
"Size limit test",
"+1234567890",
NULL,
NULL,
NULL,
0,
NULL,
NULL,
SOUP_STATUS_INTERNAL_SERVER_ERROR,
MMS_SEND_STATE_TOO_BIG,
NULL,
NULL
},{
"NoSim",
NULL, 0,
0,
NULL,
NULL,
NULL,
NULL,
NULL,
TEST_FLAG_NO_SIM,
NULL,
NULL,
0,
MMS_SEND_STATE_SEND_ERROR,
NULL,
NULL
}
};
static
void
test_finish(
Test* test)
{
const TestDesc* desc = test->desc;
const char* name = desc->name;
if (test->ret == RET_OK) {
MMS_SEND_STATE state;
const char* details;
state = mms_handler_test_send_state(test->handler, test->id);
details = mms_handler_test_send_details(test->handler, test->id);
if (state != desc->expected_state) {
test->ret = RET_ERR;
MMS_ERR("%s state %d, expected %d", name, state,
desc->expected_state);
} else if (g_strcmp0(details, desc->details)) {
test->ret = RET_ERR;
MMS_ERR("%s details '%s', expected '%s'", name, details,
desc->details);
} else if (desc->msgid) {
const char* msgid =
mms_handler_test_send_msgid(test->handler, test->id);
if (!msgid || strcmp(msgid, desc->msgid)) {
test->ret = RET_ERR;
MMS_ERR("%s msgid %s, expected %s", name, msgid, desc->msgid);
} else if (msgid && !desc->msgid) {
test->ret = RET_ERR;
MMS_ERR("%s msgid is not expected", name);
}
}
}
MMS_INFO("%s: %s", (test->ret == RET_OK) ? "OK" : "FAILED", name);
mms_handler_test_reset(test->handler);
g_main_loop_quit(test->loop);
}
static
void
test_done(
MMSDispatcherDelegate* delegate,
MMSDispatcher* dispatcher)
{
Test* test = MMS_CAST(delegate,Test,delegate);
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;
MMS_INFO("%s TIMEOUT", test->desc->name);
if (test->http) test_http_close(test->http);
mms_connman_test_close_connection(test->cm);
mms_dispatcher_cancel(test->disp, NULL);
return FALSE;
}
static
void
test_cancel(
void* param)
{
Test* test = param;
MMS_DEBUG("Cancelling %s", test->id);
mms_dispatcher_cancel(test->disp, test->id);
}
static
gboolean
test_init(
Test* test,
MMSConfig* config,
const TestDesc* desc)
{
gboolean ok = FALSE;
memset(test, 0, sizeof(*test));
if (desc->resp_file) {
GError* error = NULL;
char* f = g_strconcat(DATA_DIR, desc->name, "/", desc->resp_file, NULL);
test->resp_file = g_mapped_file_new(f, FALSE, &error);
if (!test->resp_file) {
MMS_ERR("%s", MMS_ERRMSG(error));
g_error_free(error);
}
g_free(f);
}
if (!desc->resp_file || test->resp_file) {
int i;
guint port;
MMSSettings* settings = mms_settings_default_new(config);
test->parts = g_new0(MMSAttachmentInfo, desc->nparts);
test->files = g_new0(char*, desc->nparts);
for (i=0; i<desc->nparts; i++) {
test->files[i] = g_strconcat(DATA_DIR, desc->name, "/",
desc->parts[i].file_name, NULL);
test->parts[i] = desc->parts[i];
test->parts[i].file_name = test->files[i];
}
test->config = config;
test->desc = desc;
test->cm = mms_connman_test_new();
test->handler = mms_handler_test_new();
test->disp = mms_dispatcher_new(settings, test->cm, test->handler);
test->loop = g_main_loop_new(NULL, FALSE);
test->delegate.fn_done = test_done;
mms_dispatcher_set_delegate(test->disp, &test->delegate);
test->http = test_http_new(test->resp_file, desc->resp_type,
desc->resp_status);
port = test_http_get_port(test->http);
mms_connman_test_set_port(test->cm, port, TRUE);
if (desc->flags & TEST_FLAG_NO_SIM) {
mms_connman_test_set_default_imsi(test->cm, NULL);
}
if (desc->flags & TEST_FLAG_CANCEL) {
mms_connman_test_set_connect_callback(test->cm, test_cancel, test);
}
if (desc->size_limit) {
MMSSettingsSimData sim_settings;
mms_settings_sim_data_default(&sim_settings);
sim_settings.size_limit = desc->size_limit;
mms_settings_set_sim_defaults(settings, NULL);
mms_settings_set_sim_defaults(settings, &sim_settings);
}
mms_settings_unref(settings);
test->ret = RET_ERR;
ok = TRUE;
}
return ok;
}
static
void
test_finalize(
Test* test)
{
int i;
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);
if (test->resp_file) g_mapped_file_unref(test->resp_file);
for (i=0; i<test->desc->nparts; i++) g_free(test->files[i]);
g_free(test->files);
g_free(test->parts);
g_free(test->id);
}
static
int
test_run_once(
const MMSConfig* config,
const TestDesc* desc,
gboolean debug)
{
Test test;
MMSConfig test_config = *config;
if (test_init(&test, &test_config, desc)) {
GError* error = NULL;
char* imsi = desc->imsi ? g_strdup(desc->imsi) :
mms_connman_default_imsi(test.cm);
const char* id = mms_handler_test_send_new(test.handler, imsi);
char* imsi2 = mms_dispatcher_send_message(test.disp, id, desc->imsi,
desc->to, desc->cc, desc->bcc, desc->subject,
desc->flags & TEST_DISPATCHER_FLAGS, test.parts,
desc->nparts, &error);
test.id = g_strdup(id);
if (imsi2 && (!desc->imsi || !strcmp(desc->imsi, imsi2)) &&
mms_dispatcher_start(test.disp)) {
if (!debug) {
test.timeout_id = g_timeout_add_seconds(10,
test_timeout, &test);
}
test.ret = RET_OK;
g_main_loop_run(test.loop);
} else if (!imsi2 && (desc->flags & TEST_FLAG_NO_SIM)) {
MMS_INFO("OK: %s", desc->name);
test.ret = RET_OK;
} else {
MMS_INFO("FAILED: %s", desc->name);
}
if (error) g_error_free(error);
g_free(imsi);
g_free(imsi2);
test_finalize(&test);
return test.ret;
} else {
return RET_ERR;
}
}
static
int
test_run(
const MMSConfig* config,
const char* name,
gboolean debug)
{
int i, ret;
if (name) {
const TestDesc* found = NULL;
for (i=0, ret = RET_ERR; i<G_N_ELEMENTS(send_tests); i++) {
const TestDesc* test = send_tests + i;
if (!strcmp(test->name, name)) {
ret = test_run_once(config, test, debug);
found = test;
break;
}
}
if (!found) MMS_ERR("No such test: %s", name);
} else {
for (i=0, ret = RET_OK; i<G_N_ELEMENTS(send_tests); i++) {
int test_status = test_run_once(config, send_tests + i, debug);
if (ret == RET_OK && test_status != RET_OK) ret = test_status;
}
}
return ret;
}
int main(int argc, char* argv[])
{
int ret = RET_ERR;
gboolean keep_temp = FALSE;
gboolean verbose = FALSE;
gboolean debug = FALSE;
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 },
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
"Disable timeout for debugging", NULL },
{ NULL }
};
mms_lib_init(argv[0]);
options = g_option_context_new("[TEST] - MMS send test");
g_option_context_add_main_entries(options, entries, NULL);
if (g_option_context_parse(options, &argc, &argv, NULL) && argc < 3) {
MMSConfig config;
const char* test_name = (argc == 2) ? argv[1] : NULL;
char* tmpd = g_mkdtemp(g_strdup("/tmp/test_send_XXXXXX"));
MMS_VERBOSE("Temporary directory %s", tmpd);
mms_lib_default_config(&config);
config.keep_temp_files = keep_temp;
config.root_dir = tmpd;
config.idle_secs = 0;
mms_log_set_type(MMS_LOG_TYPE_STDOUT, "test_send");
if (verbose) {
mms_log_default.level = MMS_LOGLEVEL_VERBOSE;
} else {
mms_task_send_log.level = MMS_LOGLEVEL_NONE;
mms_log_default.level = MMS_LOGLEVEL_INFO;
mms_log_stdout_timestamp = FALSE;
}
ret = test_run(&config, test_name, debug);
remove(tmpd);
g_free(tmpd);
} else {
printf("Usage: test_send [-v] [TEST]\n");
ret = RET_ERR;
}
g_option_context_free(options);
mms_lib_deinit();
return ret;
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/