Skip to content

Commit

Permalink
[test] Updated test_retrieve_no_proxy to use glib test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed Aug 14, 2020
1 parent a3d90bf commit eabdd0e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 189 deletions.
1 change: 0 additions & 1 deletion mms-lib/test/test_retrieve_no_proxy/Makefile
@@ -1,7 +1,6 @@
# -*- Mode: makefile-gmake -*-

EXE = test_retrieve_no_proxy
SRC = $(EXE).c
COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c \
test_transfer_list.c test_util.c

Expand Down
283 changes: 95 additions & 188 deletions mms-lib/test/test_retrieve_no_proxy/test_retrieve_no_proxy.c
@@ -1,14 +1,14 @@
/*
* Copyright (C) 2013-2018 Jolla Ltd.
* Copyright (C) 2013-2018 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2013-2020 Jolla Ltd.
* Copyright (C) 2013-2020 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

Expand All @@ -18,7 +18,6 @@
#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"
Expand All @@ -29,14 +28,12 @@
#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/"

static TestOpt test_opt;

typedef struct test {
const MMSConfig* config;
MMSDispatcherDelegate delegate;
Expand All @@ -46,81 +43,50 @@ typedef struct test {
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(
test_retrieve_no_proxy_done(
MMSDispatcherDelegate* delegate,
MMSDispatcher* dispatcher)
{
Test* test = G_CAST(delegate,Test,delegate);
test_finish(test);
const void* resp_data = NULL;
gsize resp_len = 0;
GBytes* reply = test_http_get_post_data(test->http);
MMSPdu* pdu = g_new0(MMSPdu, 1);
MMS_RECEIVE_STATE state = mms_handler_test_receive_state
(test->handler, NULL);

g_assert_cmpint(state, == ,MMS_RECEIVE_STATE_DECODING);
g_assert(reply);
resp_data = g_bytes_get_data(reply, &resp_len);
g_assert(resp_len > 0);

g_assert(mms_message_decode(resp_data, resp_len, pdu));
g_assert_cmpint(pdu->type, == ,MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND);
mms_message_free(pdu);
mms_handler_test_reset(test->handler);
g_main_loop_quit(test->loop);
}

static
gboolean
test_timeout(
gpointer data)
void
test_retrieve_no_proxy(
void)
{
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;
}
MMSConfig config;
MMSSettings* settings;
Test test;
TestDirs dirs;
GError* error = NULL;
guint port;
char* port_string;
char* push_data;
gsize push_len;

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,
Expand All @@ -133,133 +99,74 @@ test_init(
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;
test_dirs_init(&dirs, "test_retrieve_no_proxy");
mms_lib_default_config(&config);
config.network_idle_secs = 0;
config.root_dir = dirs.root;
settings = mms_settings_default_new(&config);

/* Initialize */
memset(&test, 0, sizeof(test));
test.config = &config;
test.retrieve_conf = g_mapped_file_new(DATA_DIR "m-retrieve.conf", FALSE,
&error);
g_assert(test.retrieve_conf);
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_retrieve_no_proxy_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(settings);

/* Start the dispatcher */
g_assert(mms_dispatcher_handle_push(test.disp, "TestConnection",
test.notification_ind, &error));
g_assert(mms_dispatcher_start(test.disp));

/* Run the loop */
test_run_loop(&test_opt, test.loop);

/* 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);
g_bytes_unref(test.notification_ind);
g_mapped_file_unref(test.retrieve_conf);

test_dirs_cleanup(&dirs, TRUE);
}

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;
}
}
#define TEST_(x) "/RetrieveNoProxy/" x

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.network_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;
}

g_test_init(&argc, &argv, NULL);
test_init(&test_opt, &argc, argv);
g_test_add_func(TEST_("OK"), test_retrieve_no_proxy);
ret = g_test_run();
mms_lib_deinit();
return ret;
}
Expand Down

0 comments on commit eabdd0e

Please sign in to comment.