Skip to content

Commit

Permalink
[mms-lib] Updated read report test
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed Mar 14, 2015
1 parent afb491d commit 606db9c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 16 deletions.
80 changes: 67 additions & 13 deletions mms-lib/test/common/test_handler.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 Down Expand Up @@ -57,6 +58,7 @@ typedef struct mms_handler_record_send {
typedef struct mms_handler_record_receive {
MMSHandlerRecord rec;
MMS_RECEIVE_STATE state;
MMS_READ_REPORT_STATUS read_report_status;
MMSDispatcher* dispatcher;
MMSMessage* msg;
GBytes* data;
Expand Down Expand Up @@ -213,6 +215,16 @@ mms_handler_test_receive_state(
return recv ? recv->state : MMS_RECEIVE_STATE_INVALID;
}

MMS_READ_REPORT_STATUS
mms_handler_test_read_report_status(
MMSHandler* handler,
const char* id)
{
MMSHandlerRecordReceive* recv =
mms_handler_test_get_receive_record(MMS_HANDLER_TEST(handler), id);
return recv ? recv->read_report_status : MMS_READ_REPORT_STATUS_INVALID;
}

typedef struct mms_handler_test_foreach_received_message_data {
mms_handler_test_get_received_message_fn cb;
void* user_data;
Expand Down Expand Up @@ -421,6 +433,32 @@ mms_handler_test_notify(
return FALSE;
}

static
MMSHandlerRecordReceive*
mms_handler_test_receive_create(
MMSHandlerTest* test,
const char* imsi)
{
MMSHandlerRecordReceive* recv = g_new0(MMSHandlerRecordReceive, 1);
char* id = g_strdup_printf("%u", (++test->last_id));
recv->rec.id = id;
recv->rec.imsi = g_strdup(imsi);
recv->rec.type = MMS_HANDLER_RECORD_RECEIVE;
recv->state = MMS_RECEIVE_STATE_INVALID;
recv->read_report_status = MMS_READ_REPORT_STATUS_INVALID;
g_hash_table_replace(test->recs, id, &recv->rec);
return recv;
}

const char*
mms_handler_test_receive_new(
MMSHandler* handler,
const char* imsi)
{
MMSHandlerTest* test = MMS_HANDLER_TEST(handler);
return mms_handler_test_receive_create(test, imsi)->rec.id;
}

static
MMSHandlerMessageNotifyCall*
mms_handler_test_message_notify(
Expand All @@ -444,15 +482,11 @@ mms_handler_test_message_notify(
expiry, data, test->prenotify_data)) {
MMS_DEBUG("Rejecting push imsi=%s from=%s subj=%s", imsi, from, subj);
} else {
MMSHandlerRecordReceive* recv = g_new0(MMSHandlerRecordReceive, 1);
char* id = g_strdup_printf("%u", (++test->last_id));
recv->rec.id = id;
recv->rec.imsi = g_strdup(imsi);
recv->rec.type = MMS_HANDLER_RECORD_RECEIVE;
recv->state = MMS_RECEIVE_STATE_INVALID;
MMSHandlerRecordReceive* recv =
mms_handler_test_receive_create(test, imsi);
const char* id = recv->rec.id;
recv->data = g_bytes_ref(data);
MMS_DEBUG("Push %s imsi=%s from=%s subj=%s", id, imsi, from, subj);
g_hash_table_replace(test->recs, id, &recv->rec);
recv->notify = notify;
notify->recv = recv;
if (test->dispatcher) {
Expand Down Expand Up @@ -628,15 +662,15 @@ mms_handler_test_delivery_report(
MMSHandlerTest* test = MMS_HANDLER_TEST(handler);
MMSHandlerRecordSend* send =
mms_handler_get_send_record_for_msgid(test, msgid);
MMS_DEBUG("Message %s delivered to %s", msgid, recipient);
MMS_DEBUG("Message %s delivered to %s", msgid, recipient);
if (send) {
MMS_ASSERT(send->delivery_status == MMS_DELIVERY_STATUS_INVALID);
if (send->delivery_status == MMS_DELIVERY_STATUS_INVALID) {
send->delivery_status = status;
return TRUE;
}
} else {
MMS_DEBUG("Unknown message id %s (this may be OK)", msgid);
MMS_DEBUG("Unknown message id %s (this may be OK)", msgid);
}
return FALSE;
}
Expand All @@ -653,19 +687,37 @@ mms_handler_test_read_report(
MMSHandlerTest* test = MMS_HANDLER_TEST(handler);
MMSHandlerRecordSend* send =
mms_handler_get_send_record_for_msgid(test, msgid);
MMS_DEBUG("Message %s read by %s", msgid, recipient);
MMS_DEBUG("Message %s read by %s", msgid, recipient);
if (send) {
MMS_ASSERT(send->read_status == MMS_READ_STATUS_INVALID);
if (send->read_status == MMS_READ_STATUS_INVALID) {
send->read_status = status;
return TRUE;
}
} else {
MMS_DEBUG("Unknown message id %s (this may be OK)", msgid);
MMS_DEBUG("Unknown message id %s (this may be OK)", msgid);
}
return FALSE;
}

gboolean
mms_handler_test_read_report_send_status(
MMSHandler* handler,
const char* id,
MMS_READ_REPORT_STATUS status)
{
MMSHandlerRecordReceive* recv =
mms_handler_test_get_receive_record(MMS_HANDLER_TEST(handler), id);
if (recv) {
MMS_DEBUG("Read report %s status %d", id, status);
recv->read_report_status = status;
return TRUE;
} else {
MMS_DEBUG("Unknown record id %s", id);
return FALSE;
}
}

void
mms_handler_test_finalize(
GObject* object)
Expand Down Expand Up @@ -694,7 +746,9 @@ mms_handler_test_class_init(
klass->fn_message_receive_state_changed =
mms_handler_test_message_receive_state_changed;
klass->fn_delivery_report = mms_handler_test_delivery_report;
klass->fn_read_report = mms_handler_test_read_report;
klass->fn_read_report = mms_handler_test_read_report;
klass->fn_read_report_send_status =
mms_handler_test_read_report_send_status;
}

static
Expand Down
13 changes: 12 additions & 1 deletion mms-lib/test/common/test_handler.h
@@ -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 @@ -25,6 +26,11 @@ mms_handler_test_send_new(
MMSHandler* handler,
const char* imsi);

const char*
mms_handler_test_receive_new(
MMSHandler* handler,
const char* imsi);

const char*
mms_handler_test_send_msgid(
MMSHandler* handler,
Expand All @@ -40,6 +46,11 @@ mms_handler_test_receive_state(
MMSHandler* handler,
const char* id);

MMS_READ_REPORT_STATUS
mms_handler_test_read_report_status(
MMSHandler* handler,
const char* id);

typedef void
(*mms_handler_test_get_received_message_fn)(
const char* id,
Expand Down
18 changes: 16 additions & 2 deletions mms-lib/test/test_read_report/test_read_report.c
Expand Up @@ -33,6 +33,8 @@

#define TEST_TIMEOUT (10) /* seconds */

#define TEST_IMSI "IMSI"

typedef struct test_desc {
const char* name;
MMSReadStatus status;
Expand All @@ -48,6 +50,8 @@ typedef struct test {
MMSHandler* handler;
MMSDispatcher* disp;
GMainLoop* loop;
const char* imsi;
char* id;
guint timeout_id;
TestHttp* http;
int ret;
Expand Down Expand Up @@ -96,7 +100,14 @@ test_done(
MMS_ERR("Phone number %s, expected %s",
pdu->ri.to, desc->to);
} else {
test->ret = RET_OK;
MMS_READ_REPORT_STATUS status =
mms_handler_test_read_report_status(test->handler,
test->id);
if (status != MMS_READ_REPORT_STATUS_OK) {
MMS_ERR("Unexpected status %d", status);
} else {
test->ret = RET_OK;
}
}
} else {
MMS_ERR("Can't decode PDU");
Expand Down Expand Up @@ -141,6 +152,7 @@ test_init(
test->delegate.fn_done = test_done;
mms_dispatcher_set_delegate(test->disp, &test->delegate);
test->http = test_http_new(NULL, NULL, SOUP_STATUS_OK);
test->id = g_strdup(mms_handler_test_receive_new(test->handler, TEST_IMSI));
mms_connman_test_set_port(test->cm, test_http_get_port(test->http), TRUE);
mms_settings_unref(settings);
test->ret = RET_ERR;
Expand All @@ -159,6 +171,7 @@ test_finalize(
g_source_remove(test->timeout_id);
test->timeout_id = 0;
}
g_free(test->id);
test_http_close(test->http);
test_http_unref(test->http);
mms_connman_test_close_connection(test->cm);
Expand All @@ -178,7 +191,7 @@ test_read_report_once(
Test test;
GError* error = NULL;
test_init(&test, config, desc, debug);
if (mms_dispatcher_send_read_report(test.disp, "1", "IMSI",
if (mms_dispatcher_send_read_report(test.disp, test.id, TEST_IMSI,
"MessageID", desc->phone, desc->status, &error)) {
if (mms_dispatcher_start(test.disp)) {
test.ret = RET_OK;
Expand Down Expand Up @@ -273,6 +286,7 @@ int main(int argc, char* argv[])
rmdir(tmpd);
remove(tmpd);
g_free(tmpd);
g_free(msgdir);
mms_lib_deinit();
} else {
fprintf(stderr, "%s\n", MMS_ERRMSG(error));
Expand Down

0 comments on commit 606db9c

Please sign in to comment.