Skip to content

Commit

Permalink
[mms-lib] More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed May 1, 2014
1 parent 21f7992 commit 8d03661
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 17 deletions.
4 changes: 4 additions & 0 deletions mms-lib/include/mms_log.h
Expand Up @@ -103,8 +103,12 @@ mms_logv(
const char* format,
va_list va);

extern const char MMS_LOG_TYPE_STDOUT[];
extern const char MMS_LOG_TYPE_GLIB[];
extern const char MMS_LOG_TYPE_CUSTOM[];
#ifdef unix
# define MMS_LOG_SYSLOG
extern const char MMS_LOG_TYPE_SYSLOG[];
#endif

/* Available log handlers */
Expand Down
8 changes: 4 additions & 4 deletions mms-lib/src/mms_log.c
Expand Up @@ -50,11 +50,11 @@ static const struct _mms_log_level {
{ "verbose", "From verbose trace messages to errors" }
};

static const char MMS_LOG_TYPE_STDOUT[] = "stdout";
static const char MMS_LOG_TYPE_GLIB[] = "glib";
static const char MMS_LOG_TYPE_CUSTOM[] = "custom";
const char MMS_LOG_TYPE_STDOUT[] = "stdout";
const char MMS_LOG_TYPE_GLIB[] = "glib";
const char MMS_LOG_TYPE_CUSTOM[] = "custom";
#ifdef MMS_LOG_SYSLOG
static const char MMS_LOG_TYPE_SYSLOG[] = "syslog";
const char MMS_LOG_TYPE_SYSLOG[] = "syslog";
#endif

G_STATIC_ASSERT(G_N_ELEMENTS(mms_log_levels) > MMS_LOGLEVEL_MAX);
Expand Down
14 changes: 14 additions & 0 deletions mms-lib/test/common/test_connman.c
Expand Up @@ -27,6 +27,8 @@ typedef struct mms_connman_test {
unsigned short port;
gboolean proxy;
char* default_imsi;
mms_connman_test_connect_fn connect_fn;
void* connect_param;
} MMSConnManTest;

G_DEFINE_TYPE(MMSConnManTest, mms_connman_test, MMS_TYPE_CONNMAN);
Expand Down Expand Up @@ -68,6 +70,17 @@ mms_connman_test_close_connection(
}
}

void
mms_connman_test_set_connect_callback(
MMSConnMan* cm,
mms_connman_test_connect_fn fn,
void* param)
{
MMSConnManTest* test = MMS_CONNMAN_TEST(cm);
test->connect_fn = fn;
test->connect_param = param;
}

static
char*
mms_connman_test_default_imsi(
Expand All @@ -87,6 +100,7 @@ mms_connman_test_open_connection(
mms_connman_test_close_connection(cm);
if (test->port) {
test->conn = mms_connection_test_new(imsi, test->port, test->proxy);
if (test->connect_fn) test->connect_fn(test->connect_param);
return mms_connection_ref(test->conn);
} else {
return NULL;
Expand Down
11 changes: 11 additions & 0 deletions mms-lib/test/common/test_connman.h
Expand Up @@ -17,6 +17,11 @@

#include "mms_connman.h"

typedef
void
(*mms_connman_test_connect_fn)(
void* param);

MMSConnMan*
mms_connman_test_new(void);

Expand All @@ -31,6 +36,12 @@ mms_connman_test_set_default_imsi(
MMSConnMan* cm,
const char* imsi);

void
mms_connman_test_set_connect_callback(
MMSConnMan* cm,
mms_connman_test_connect_fn fn,
void* param);

void
mms_connman_test_close_connection(
MMSConnMan* cm);
Expand Down
3 changes: 3 additions & 0 deletions mms-lib/test/resize/test_resize.c
Expand Up @@ -395,6 +395,9 @@ test_run_one(
} else if (!ok && !test->size.width && !test->size.height) {
ret = RET_OK;
}
/* Extra ref/unref improves the coverage */
mms_attachment_ref(at);
mms_attachment_unref(at);
mms_attachment_unref(at);
} else {
MMS_ERR("%s", MMS_ERRMSG(error));
Expand Down
1 change: 1 addition & 0 deletions mms-lib/test/send/data/Cancel/test.txt
@@ -0,0 +1 @@
Send cancel test
Binary file added mms-lib/test/send/data/EmptyMessageID/m-send.conf
Binary file not shown.
1 change: 1 addition & 0 deletions mms-lib/test/send/data/EmptyMessageID/test.txt
@@ -0,0 +1 @@
Test of empty message ID
2 changes: 2 additions & 0 deletions mms-lib/test/send/data/TooBig/test.txt
@@ -0,0 +1,2 @@
This test sets a tiny size limit so that there's no way
that PDU would possibly fit into that limit.
Binary file added mms-lib/test/send/data/UnexpectedResp/m-send.conf
Binary file not shown.
1 change: 1 addition & 0 deletions mms-lib/test/send/data/UnexpectedResp/test.txt
@@ -0,0 +1 @@
Test of m-send.conf containing parsable PDU of unexpected type
1 change: 1 addition & 0 deletions mms-lib/test/send/data/UnparsableResp/m-send.conf
@@ -0,0 +1 @@
This is not a valid MMS PDU
1 change: 1 addition & 0 deletions mms-lib/test/send/data/UnparsableResp/test.txt
@@ -0,0 +1 @@
Test of m-send.conf containing unparsable garbage
133 changes: 120 additions & 13 deletions mms-lib/test/send/test_send.c
Expand Up @@ -36,6 +36,7 @@ typedef struct test_desc {
const char* name;
const MMSAttachmentInfo* parts;
int nparts;
gsize size_limit;
const char* subject;
const char* to;
const char* cc;
Expand All @@ -49,11 +50,16 @@ typedef struct test_desc {
const char* msgid;
} TestDesc;

#define TEST_FLAG_CANCEL (0x1000)
#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
G_STATIC_ASSERT(!(TEST_PRIVATE_FLAGS & TEST_DISPATCHER_FLAGS));

typedef struct test {
const TestDesc* desc;
Expand Down Expand Up @@ -90,12 +96,17 @@ static const MMSAttachmentInfo test_files_reject [] = {
{ "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",
Expand All @@ -110,6 +121,7 @@ static const TestDesc send_tests[] = {
},{
"AcceptNoExt",
ATTACHMENTS(test_files_accept_no_ext),
0,
"Test of successful delivery (no extensions)",
"+1234567890",
"+2345678901,+3456789012",
Expand All @@ -124,6 +136,7 @@ static const TestDesc send_tests[] = {
},{
"ServiceDenied",
ATTACHMENTS(test_files_reject),
0,
"Rejection test",
"+1234567890",
NULL,
Expand All @@ -138,6 +151,7 @@ static const TestDesc send_tests[] = {
},{
"Failure",
ATTACHMENTS(test_files_reject),
0,
"Failure test",
"+1234567890",
NULL,
Expand All @@ -149,6 +163,81 @@ static const TestDesc send_tests[] = {
SOUP_STATUS_OK,
MMS_SEND_STATE_SEND_ERROR,
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
},{
"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
},{
"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
},{
"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
},{
"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
}
};

Expand All @@ -174,7 +263,7 @@ test_finish(
MMS_ERR("%s msgid %s, expected %s", name, msgid, desc->msgid);
} else if (msgid && !desc->msgid) {
test->ret = RET_ERR;
MMS_ERR("%s msgidis not expected", name);
MMS_ERR("%s msgid is not expected", name);
}
}
}
Expand Down Expand Up @@ -210,19 +299,36 @@ test_timeout(
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,
const MMSConfig* config,
MMSConfig* config,
const TestDesc* desc)
{
gboolean ok = FALSE;
GError* error = NULL;
char* fn = g_strconcat(DATA_DIR, desc->name, "/", desc->resp_file, NULL);
memset(test, 0, sizeof(*test));
test->resp_file = g_mapped_file_new(fn, FALSE, &error);
if (test->resp_file) {
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;
test->parts = g_new0(MMSAttachmentInfo, desc->nparts);
Expand All @@ -245,13 +351,13 @@ test_init(
desc->resp_status);
port = test_http_get_port(test->http);
mms_connman_test_set_port(test->cm, port, TRUE);
if (desc->flags & TEST_FLAG_CANCEL) {
mms_connman_test_set_connect_callback(test->cm, test_cancel, test);
}
if (desc->size_limit) config->size_limit = desc->size_limit;
test->ret = RET_ERR;
ok = TRUE;
} else {
MMS_ERR("%s", MMS_ERRMSG(error));
g_error_free(error);
}
g_free(fn);
return ok;
}

Expand All @@ -274,7 +380,7 @@ test_finalize(
mms_handler_unref(test->handler);
mms_dispatcher_unref(test->disp);
g_main_loop_unref(test->loop);
g_mapped_file_unref(test->resp_file);
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);
Expand All @@ -289,7 +395,8 @@ test_run_once(
gboolean debug)
{
Test test;
if (test_init(&test, config, desc)) {
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);
Expand Down Expand Up @@ -380,7 +487,7 @@ int main(int argc, char* argv[])
config.root_dir = tmpd;
config.idle_secs = 0;

mms_log_default.name = "test_send";
mms_log_set_type(MMS_LOG_TYPE_STDOUT, "test_send");
if (verbose) {
mms_log_default.level = MMS_LOGLEVEL_VERBOSE;
} else {
Expand Down

0 comments on commit 8d03661

Please sign in to comment.