/
test_send.c
499 lines (460 loc) · 12.4 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
/*
* Copyright (C) 2013-2020 Jolla Ltd.
* Copyright (C) 2013-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2020 Open Mobile Platform LLC.
*
* 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 "test_util.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 "mms_attachment_info.h"
#include <gutil_macros.h>
#include <gutil_log.h>
#include <gio/gio.h>
#include <libsoup/soup-status.h>
#define DATA_DIR "data"
static TestOpt test_opt;
typedef struct test_attachment {
const char* file_name;
const char* content_type;
const char* content_id;
} TestAttachment;
typedef struct test_desc {
const char* name;
const TestAttachment* 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_DONT_CONVERT_TO_UTF8 (0x4000)
#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 |\
TEST_FLAG_DONT_CONVERT_TO_UTF8)
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;
TestHttp* http;
char* id;
GMappedFile* resp_file;
} Test;
static const TestAttachment 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" }
};
#ifdef HAVE_MAGIC
static const TestAttachment test_files_accept_no_ext [] = {
{ "smil", NULL, NULL },
{ "0001", NULL, "image1" },
{ "0001", "", "image2" },
{ "test.text", "text/plain;charset=utf-8", "text" }
};
#endif /* HAVE_MAGIC */
static const TestAttachment test_files_reject [] = {
{ "0001.png", "image/png", "image" },
{ "test.txt", "text/plain", "text" }
};
static const TestAttachment test_txt [] = {
{ "test.txt", NULL, "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"
},{
#ifdef HAVE_MAGIC
"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"
},{
#endif /* HAVE_MAGIC */
"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
},{
"DontConvertToUtf8",
ATTACHMENTS(test_txt),
100,
"No conversion to UTF-8",
"+1234567890",
NULL,
NULL,
NULL,
TEST_FLAG_DONT_CONVERT_TO_UTF8,
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;
MMSHandler* handler = test->handler;
MMS_SEND_STATE state = mms_handler_test_send_state(handler, test->id);
const char* details = mms_handler_test_send_details(handler, test->id);
g_assert_cmpint(state, == ,desc->expected_state);
g_assert_cmpstr(details, == ,desc->details);
if (desc->msgid) {
const char* msgid = mms_handler_test_send_msgid(handler, test->id);
if (msgid) {
g_assert_cmpstr(msgid, == ,desc->msgid);
} else {
g_assert(!desc->msgid);
}
}
mms_handler_test_reset(test->handler);
g_main_loop_quit(test->loop);
}
static
void
test_done(
MMSDispatcherDelegate* delegate,
MMSDispatcher* dispatcher)
{
Test* test = G_CAST(delegate,Test,delegate);
if (!mms_handler_test_receive_pending(test->handler, NULL)) {
test_finish(test);
}
}
static
void
test_cancel(
void* param)
{
Test* test = param;
GDEBUG("Cancelling %s", test->id);
mms_dispatcher_cancel(test->disp, test->id);
}
static
void
run_test(
gconstpointer data)
{
const TestDesc* desc = data;
MMSConfig config;
MMSSettings* settings;
GError* error = NULL;
TestDirs dirs;
Test test;
guint port;
int i;
const char* id;
char* imsi;
char* imsi2;
test_dirs_init(&dirs, "test_send");
mms_lib_default_config(&config);
config.root_dir = dirs.root;
config.keep_temp_files = (test_opt.flags & TEST_FLAG_DEBUG) != 0;
config.network_idle_secs = 0;
config.attic_enabled = TRUE;
if (desc->flags & TEST_FLAG_DONT_CONVERT_TO_UTF8) {
config.convert_to_utf8 = FALSE;
}
settings = mms_settings_default_new(&config);
memset(&test, 0, sizeof(test));
test.config = &config;
/* Initialize the test */
memset(&test, 0, sizeof(test));
if (desc->resp_file) {
char* f = g_build_filename(DATA_DIR, desc->name, desc->resp_file, NULL);
test.resp_file = g_mapped_file_new(f, FALSE, &error);
g_assert(test.resp_file);
g_free(f);
}
g_assert(!desc->resp_file || test.resp_file);
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_build_filename(DATA_DIR, desc->name,
desc->parts[i].file_name, NULL);
g_assert(mms_attachment_info_path(test.parts + i, test.files[i],
desc->parts[i].content_type, desc->parts[i].content_id, &error));
}
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, NULL);
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);
/* Send message and run the event loop */
imsi = desc->imsi ? g_strdup(desc->imsi) :
mms_connman_default_imsi(test.cm);
id = mms_handler_test_send_new(test.handler, imsi);
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) {
g_assert(!desc->imsi || !g_strcmp0(desc->imsi, imsi2));
g_assert(mms_dispatcher_start(test.disp));
test_run_loop(&test_opt, test.loop);
} else {
g_assert(desc->flags & TEST_FLAG_NO_SIM);
g_assert(error);
g_error_free(error);
}
/* Done */
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++) {
mms_attachment_info_cleanup(test.parts + i);
g_free(test.files[i]);
}
g_free(test.files);
g_free(test.parts);
g_free(test.id);
g_free(imsi);
g_free(imsi2);
test_dirs_cleanup(&dirs, TRUE);
}
#define TEST_(x) "/Send/" x
int main(int argc, char* argv[])
{
int ret;
guint i;
mms_lib_init(argv[0]);
g_test_init(&argc, &argv, NULL);
test_init(&test_opt, &argc, argv);
for (i = 0; i < G_N_ELEMENTS(send_tests); i++) {
const TestDesc* test = send_tests + i;
char* name = g_strdup_printf(TEST_("%s"), test->name);
g_test_add_data_func(name, test, run_test);
g_free(name);
}
ret = g_test_run();
mms_lib_deinit();
return ret;
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/