Navigation Menu

Skip to content

Commit

Permalink
[test_read_report] Added another read report test
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed Feb 23, 2015
1 parent 7a5ae34 commit 5468f32
Showing 1 changed file with 141 additions and 33 deletions.
174 changes: 141 additions & 33 deletions mms-lib/test/test_read_report/test_read_report.c
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013-2014 Jolla Ltd.
* Copyright (C) 2013-2015 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
Expand All @@ -26,11 +27,22 @@
#include <gio/gio.h>
#include <libsoup/soup-status.h>

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

#define TEST_TIMEOUT (10) /* seconds */

typedef struct test_desc {
const char* name;
MMSReadStatus status;
const char* phone;
enum mms_message_read_status rr_status;
const char* to;
} TestDesc;

typedef struct test {
const TestDesc* desc;
MMSDispatcherDelegate delegate;
MMSConnMan* cm;
MMSHandler* handler;
Expand All @@ -41,33 +53,58 @@ typedef struct test {
int ret;
} Test;

static const TestDesc tests[] = {
{
"Read",
MMS_READ_STATUS_READ,
"+358501111111",
MMS_MESSAGE_READ_STATUS_READ,
"+358501111111/TYPE=PLMN"
},{
"Deleted",
MMS_READ_STATUS_DELETED,
"+358501111111/TYPE=PLMN",
MMS_MESSAGE_READ_STATUS_DELETED,
"+358501111111/TYPE=PLMN"
}
};

static
void
test_done(
MMSDispatcherDelegate* delegate,
MMSDispatcher* dispatcher)
{
Test* test = MMS_CAST(delegate,Test,delegate);
const TestDesc* desc = test->desc;
const char* name = desc->name;
if (test->ret == RET_OK) {
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);
test->ret = RET_ERR;
if (mms_message_decode(resp_data, resp_len, pdu)) {
if (pdu->type != MMS_MESSAGE_TYPE_READ_REC_IND) {
test->ret = RET_ERR;
MMS_ERR("Unexpected PDU type %u", pdu->type);
} else if (pdu->ri.rr_status != desc->rr_status) {
MMS_ERR("Read status %d, expected %d",
pdu->ri.rr_status, desc->rr_status);
} else if (g_strcmp0(pdu->ri.to, desc->to)) {
MMS_ERR("Phone number %s, expected %s",
pdu->ri.to, desc->to);
} else {
test->ret = RET_OK;
}
} else {
test->ret = RET_ERR;
MMS_ERR("Can't decode PDU");
}
mms_message_free(pdu);
}
}
MMS_INFO("%s", (test->ret == RET_OK) ? "OK" : "FAILED");
MMS_INFO("%s: %s", (test->ret == RET_OK) ? "OK" : "FAILED", name);
g_main_loop_quit(test->loop);
}

Expand All @@ -90,20 +127,27 @@ static
void
test_init(
Test* test,
const MMSConfig* config)
const MMSConfig* config,
const TestDesc* desc,
gboolean debug)
{
MMSSettings* settings = mms_settings_default_new(config);
MMS_DEBUG(">>>>>>>>>> %s <<<<<<<<<<", desc->name);
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->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(NULL, NULL, SOUP_STATUS_OK);
mms_connman_test_set_port(test->cm, test_http_get_port(test->http), TRUE);
mms_settings_unref(settings);
test->ret = RET_ERR;
if (!debug) {
test->timeout_id = g_timeout_add_seconds(TEST_TIMEOUT,
test_timeout, test);
}
}

static
Expand All @@ -126,14 +170,16 @@ test_finalize(

static
int
test_read_report(
const MMSConfig* config)
test_read_report_once(
const MMSConfig* config,
const TestDesc* desc,
gboolean debug)
{
Test test;
GError* error = NULL;
test_init(&test, config);
test_init(&test, config, desc, debug);
if (mms_dispatcher_send_read_report(test.disp, "1", "IMSI",
"MessageID", "+358501111111", MMS_READ_STATUS_READ, &error)) {
"MessageID", desc->phone, desc->status, &error)) {
if (mms_dispatcher_start(test.disp)) {
test.ret = RET_OK;
g_main_loop_run(test.loop);
Expand All @@ -148,30 +194,92 @@ test_read_report(
return test.ret;
}

static
int
test_read_report(
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(tests); i++) {
const TestDesc* test = tests + i;
if (!strcmp(test->name, name)) {
ret = test_read_report_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(tests); i++) {
int status = test_read_report_once(config, tests + i, debug);
if (ret == RET_OK && status != RET_OK) ret = status;
}
}
return ret;
}

int main(int argc, char* argv[])
{
int ret;
MMSConfig config;
char* tmpd = g_mkdtemp(g_strdup("/tmp/test_retrieve_XXXXXX"));
mms_lib_init(argv[0]);
mms_lib_default_config(&config);
config.idle_secs = 0;
config.root_dir = tmpd;
mms_log_default.name = "test_read_report";
if (argc > 1 && !strcmp(argv[1], "-v")) {
mms_log_default.level = MMS_LOGLEVEL_VERBOSE;
int ret = RET_ERR;
gboolean debug = FALSE;
gboolean verbose = FALSE;
GError* error = NULL;
GOptionContext* options;
GOptionEntry entries[] = {
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
"Enable verbose output", NULL },
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
"Disable timeout for debugging", NULL },
{ NULL }
};

options = g_option_context_new("[TEST] - MMS read report test");
g_option_context_add_main_entries(options, entries, NULL);
if (g_option_context_parse(options, &argc, &argv, &error)) {
MMSConfig config;
char* tmpd = g_mkdtemp(g_strdup("/tmp/test_read_report_XXXXXX"));
char* msgdir = g_strconcat(tmpd, "/msg", NULL);

mms_lib_init(argv[0]);
mms_lib_default_config(&config);
config.idle_secs = 0;
config.root_dir = tmpd;
mms_log_default.name = "test_read_report";
if (verbose) {
mms_log_default.level = MMS_LOGLEVEL_VERBOSE;
} else {
mms_log_default.level = MMS_LOGLEVEL_INFO;
mms_task_decode_log.level =
mms_task_retrieve_log.level =
mms_task_notification_log.level = MMS_LOGLEVEL_NONE;
mms_log_stdout_timestamp = FALSE;
}

MMS_VERBOSE("Temporary directory %s", tmpd);
if (argc < 2) {
ret = test_read_report(&config, NULL, debug);
} else {
int i;
for (i=1, ret = RET_OK; i<argc; i++) {
int test_status = test_read_report(&config, argv[i], debug);
if (ret == RET_OK && test_status != RET_OK) ret = test_status;
}
}
rmdir(msgdir);
rmdir(tmpd);
remove(tmpd);
g_free(tmpd);
mms_lib_deinit();
} else {
mms_log_default.level = MMS_LOGLEVEL_INFO;
mms_task_decode_log.level =
mms_task_retrieve_log.level =
mms_task_notification_log.level = MMS_LOGLEVEL_NONE;
mms_log_stdout_timestamp = FALSE;
fprintf(stderr, "%s\n", MMS_ERRMSG(error));
g_error_free(error);
ret = RET_ERR;
}
MMS_VERBOSE("Temporary directory %s", tmpd);
ret = test_read_report(&config);
remove(tmpd);
g_free(tmpd);
mms_lib_deinit();
g_option_context_free(options);
return ret;
}

Expand Down

0 comments on commit 5468f32

Please sign in to comment.