/
test_retrieve_no_proxy.c
273 lines (250 loc) · 7.6 KB
/
test_retrieve_no_proxy.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
/*
* 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 "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 <gutil_macros.h>
#include <gutil_log.h>
#include <gio/gio.h>
#include <libsoup/soup-status.h>
#define RET_OK (0)
#define RET_ERR (1)
#define RET_TIMEOUT (2)
#define MMS_MESSAGE_TYPE_NONE (0)
#define DATA_DIR "data/"
typedef struct test {
const MMSConfig* config;
MMSDispatcherDelegate delegate;
MMSConnMan* cm;
MMSHandler* handler;
MMSDispatcher* disp;
GBytes* notification_ind;
GMappedFile* retrieve_conf;
GMainLoop* loop;
guint timeout_id;
TestHttp* http;
int ret;
} Test;
static
void
test_finish(
Test* test)
{
if (test->ret == RET_OK) {
MMS_RECEIVE_STATE state;
state = mms_handler_test_receive_state(test->handler, NULL);
if (state != MMS_RECEIVE_STATE_DECODING) {
test->ret = RET_ERR;
GERR("Unexpected state %d", state);
} 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) {
MMSPdu* pdu = g_new0(MMSPdu, 1);
if (mms_message_decode(resp_data, resp_len, pdu)) {
if (pdu->type != MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND) {
test->ret = RET_ERR;
GERR("Unexpected reply %u", pdu->type);
}
} else {
test->ret = RET_ERR;
GERR("Failed to decode reply message");
}
mms_message_free(pdu);
} else {
test->ret = RET_ERR;
GERR("Reply expected");
}
}
}
GINFO("%s", (test->ret == RET_OK) ? "OK" : "FAILED");
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);
test_finish(test);
}
static
gboolean
test_timeout(
gpointer data)
{
Test* test = data;
test->timeout_id = 0;
test->ret = RET_TIMEOUT;
GINFO("TIMEOUT");
if (test->http) test_http_close(test->http);
mms_connman_test_close_connection(test->cm);
mms_dispatcher_cancel(test->disp, NULL);
return FALSE;
}
static
gboolean
test_init(
Test* test,
const MMSConfig* config)
{
static const guint8 push_template[] = {
0x8C,0x82,0x98,0x42,0x49,0x33,0x52,0x34,0x56,0x32,0x49,0x53,
0x4C,0x52,0x34,0x31,0x40,0x78,0x6D,0x61,0x2E,0x37,0x32,0x34,
0x2E,0x63,0x6F,0x6D,0x00,0x8D,0x91,0x86,0x80,0x88,0x05,0x81,
0x03,0x03,0xF4,0x7E,0x89,0x19,0x80,0x2B,0x33,0x35,0x38,0x35,
0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x2F,0x54,0x59,0x50,
0x45,0x3D,0x50,0x4C,0x4D,0x4E,0x00,0x8A,0x80,0x8E,0x03,0x01,
0xB9,0x7A,0x96,0x0D,0xEA,0x7F,0xD0,0x9F,0xD0,0xB8,0xD1,0x82,
0xD0,0xB5,0xD1,0x80,0x00,0x83,0x68,0x74,0x74,0x70,0x3A,0x2F,
0x2F,0x31,0x32,0x37,0x2E,0x30,0x2E,0x30,0x2E,0x31,0x3A
};
gboolean ok = FALSE;
GError* err = NULL;
memset(test, 0, sizeof(*test));
test->config = config;
test->retrieve_conf = g_mapped_file_new("data/m-retrieve.conf",FALSE,&err);
if (test->retrieve_conf) {
guint port;
char* port_string;
char* push_data;
gsize push_len;
MMSSettings* set = mms_settings_default_new(config);
test->cm = mms_connman_test_new();
test->handler = mms_handler_test_new();
test->disp = mms_dispatcher_new(set, test->cm, test->handler, NULL);
test->loop = g_main_loop_new(NULL, FALSE);
test->timeout_id = g_timeout_add_seconds(10, test_timeout, test);
test->delegate.fn_done = test_done;
mms_dispatcher_set_delegate(test->disp, &test->delegate);
test->http = test_http_new(test->retrieve_conf, MMS_CONTENT_TYPE,
SOUP_STATUS_OK);
port = test_http_get_port(test->http);
mms_connman_test_set_port(test->cm, port, FALSE);
port_string = g_strdup_printf("%u", port);
push_len = strlen(port_string) + 1 + sizeof(push_template);
push_data = g_malloc(push_len);
memcpy(push_data, push_template, sizeof(push_template));
strcpy(push_data + sizeof(push_template), port_string);
test->notification_ind = g_bytes_new(push_data, push_len);
g_free(push_data);
g_free(port_string);
mms_settings_unref(set);
test->ret = RET_ERR;
ok = TRUE;
} else {
GERR("%s", GERRMSG(err));
g_error_free(err);
}
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_bytes_unref(test->notification_ind);
g_mapped_file_unref(test->retrieve_conf);
}
static
int
test_retrieve_no_proxy(
const MMSConfig* config)
{
Test test;
if (test_init(&test, config)) {
GError* error = NULL;
if (mms_dispatcher_handle_push(test.disp, "TestConnection",
test.notification_ind, &error)) {
if (mms_dispatcher_start(test.disp)) {
test.ret = RET_OK;
g_main_loop_run(test.loop);
} else {
GINFO("FAILED");
}
} else {
g_error_free(error);
GINFO("FAILED");
}
test_finalize(&test);
return test.ret;
} else {
return RET_ERR;
}
}
int main(int argc, char* argv[])
{
int ret;
MMSConfig config;
const char* test = "test_retrieve_no_proxy";
mms_lib_init(argv[0]);
mms_lib_default_config(&config);
gutil_log_default.name = test;
if (argc > 1 && !strcmp(argv[1], "-v")) {
gutil_log_default.level = GLOG_LEVEL_VERBOSE;
argc--;
} else {
gutil_log_timestamp = FALSE;
gutil_log_default.level = GLOG_LEVEL_INFO;
mms_task_decode_log.level =
mms_task_retrieve_log.level =
mms_task_http_log.level =
mms_task_notification_log.level = GLOG_LEVEL_NONE;
}
if (argc == 1) {
TestDirs dirs;
test_dirs_init(&dirs, test);
config.root_dir = dirs.root;
config.idle_secs = 0;
config.attic_enabled = TRUE;
ret = test_retrieve_no_proxy(&config);
test_dirs_cleanup(&dirs, TRUE);
} else {
printf("Usage: test_retrieve [-v] [TEST]\n");
ret = RET_ERR;
}
mms_lib_deinit();
return ret;
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/