Skip to content

Commit

Permalink
Merge pull request #3 from monich/content-id
Browse files Browse the repository at this point in the history
Handle missing content-id headers
  • Loading branch information
Slava Monich committed Mar 17, 2014
2 parents 5240182 + ebe17ff commit ce733e0
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 8 deletions.
4 changes: 3 additions & 1 deletion mms-handler-dbus/src/mms_handler_dbus.c
Expand Up @@ -102,7 +102,9 @@ mms_handler_dbus_message_received(
OrgNemomobileMmsHandler* proxy = mms_handler_dbus_connect(handler);
MMS_ASSERT(msg->id && msg->id[0]);
if (msg->id && msg->id[0] && proxy) {
const char* nothing = NULL;
const char* subject = msg->subject ? msg->subject : "";
const char** cc = msg->cc ? (const char**)msg->cc : &nothing;
GError* error = NULL;
GSList* list = msg->parts;
GVariant* parts;
Expand All @@ -119,7 +121,7 @@ mms_handler_dbus_message_received(
parts = g_variant_ref_sink(g_variant_builder_end(&b));
ok = org_nemomobile_mms_handler_call_message_received_sync(
proxy, msg->id, msg->message_id, msg->from, (const char**)msg->to,
(const char**)msg->cc, subject, msg->date, msg->priority,
cc, subject, msg->date, msg->priority,
msg->cls, msg->read_report_req, parts, NULL, &error);
if (!ok) {
MMS_ERR("Failed to nofity commhistoryd: %s", MMS_ERRMSG(error));
Expand Down
33 changes: 28 additions & 5 deletions mms-lib/src/mms_task_decode.c
Expand Up @@ -84,6 +84,22 @@ mms_task_decode_add_file_name(
return file;
}

static
char*
mms_task_decode_make_content_id(
GPtrArray* ids,
char* proposed)
{
char* id = proposed;
while (mms_task_decode_array_contains_string(ids, id)) {
char* tmp = g_strconcat("_", id, NULL);
if (id != proposed) g_free(id);
id = tmp;
}
g_ptr_array_add(ids, id);
return id;
}

static
MMSMessage*
mms_task_decode_process_retrieve_conf(
Expand All @@ -95,6 +111,7 @@ mms_task_decode_process_retrieve_conf(
GSList* entry;
int i, nparts = g_slist_length(pdu->attachments);
GPtrArray* part_files = g_ptr_array_new_full(nparts, g_free);
GPtrArray* part_ids = g_ptr_array_new();
char* dir = mms_task_dir(task);
const struct mms_retrieve_conf* rc = &pdu->rc;
MMSMessage* msg = mms_message_new();
Expand Down Expand Up @@ -148,29 +165,35 @@ mms_task_decode_process_retrieve_conf(
msg->parts_dir = g_strconcat(dir, "/" , MMS_PARTS_DIR, NULL);
for (i=0, entry = pdu->attachments; entry; entry = entry->next, i++) {
struct mms_attachment* attach = entry->data;
const char* id = attach->content_id;
const char* name = attach->content_location ?
attach->content_location : attach->content_id;
char* path = NULL;
char* file;
if (id && id[0]) {
file = mms_task_decode_add_file_name(part_files, id);
if (name && name[0]) {
file = mms_task_decode_add_file_name(part_files, name);
} else {
char* name = g_strdup_printf("part_%d",i);
file = mms_task_decode_add_file_name(part_files, name);
g_free(name);
}
MMS_DEBUG("Part: %s %s", id, attach->content_type);
MMS_DEBUG("Part: %s %s", name, attach->content_type);
MMS_ASSERT(attach->offset < pdu_size);
if (mms_write_file(msg->parts_dir, file, pdu_data + attach->offset,
attach->length, &path)) {
MMSMessagePart* part = g_new0(MMSMessagePart, 1);
char* tmp = NULL;
char* id = attach->content_id ? g_strdup(attach->content_id) :
(tmp = g_strconcat("<", file, ">", NULL));
part->content_type = g_strdup(attach->content_type);
part->content_id = g_strdup(id);
part->content_id = mms_task_decode_make_content_id(part_ids, id);
part->file = path;
msg->parts = g_slist_append(msg->parts, part);
if (tmp && tmp != part->content_id) g_free(tmp);
}
}

g_ptr_array_free(part_files, TRUE);
g_ptr_array_free(part_ids, TRUE);
g_free(dir);
return msg;
}
Expand Down
10 changes: 10 additions & 0 deletions mms-lib/test/common/test_handler.c
Expand Up @@ -148,6 +148,16 @@ mms_handler_test_receive_state(
return recv ? recv->state : MMS_RECEIVE_STATE_INVALID;
}

MMSMessage*
mms_handler_test_get_received_message(
MMSHandler* handler,
const char* id)
{
MMSHandlerRecordReceive* recv =
mms_handler_test_get_receive_record(MMS_HANDLER_TEST(handler), id);
return recv ? recv->msg : NULL;
}

const char*
mms_handler_test_send_msgid(
MMSHandler* handler,
Expand Down
5 changes: 5 additions & 0 deletions mms-lib/test/common/test_handler.h
Expand Up @@ -40,6 +40,11 @@ mms_handler_test_receive_state(
MMSHandler* handler,
const char* id);

MMSMessage*
mms_handler_test_get_received_message(
MMSHandler* handler,
const char* id);

gboolean
mms_handler_test_receive_pending(
MMSHandler* handler,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
103 changes: 101 additions & 2 deletions mms-lib/test/retrieve/test_retrieve.c
Expand Up @@ -34,6 +34,12 @@

#define DATA_DIR "data/"

typedef struct test_part_desc {
const char* content_type;
const char* content_id;
const char* file_name;
} TestPartDesc;

typedef struct test_desc {
const char* name;
const char* dir;
Expand All @@ -44,6 +50,8 @@ typedef struct test_desc {
const char* attic_file;
MMS_RECEIVE_STATE expected_state;
enum mms_message_type reply_msg;
const TestPartDesc* parts;
unsigned int nparts;
int flags;

#define TEST_PUSH_HANDLING_FAILURE_OK (0x01)
Expand All @@ -66,6 +74,23 @@ typedef struct test {
int ret;
} Test;

static const TestPartDesc retrieve_success_parts [] = {
{ "application/smil;charset=utf-8", "<0>", "0.smil" },
{ "text/plain;charset=utf-8", "<text_0011.txt>", "text_0011.txt" },
{ "image/jpeg", "<131200181.jpg>", "131200181.jpg" },
{ "text/plain;charset=utf-8", "<text_0021.txt>", "text_0021.txt" },
{ "image/jpeg", "<140100041.jpg>", "140100041.jpg" }
};

static const TestPartDesc retrieve_success2_parts [] = {
{ "image/jpeg", "<2014_03_.jpg>", "2014_03_.jpg" },
{ "text/plain;charset=utf-8", "<Test_mms.txt>", "Test_mms.txt" },
{ "application/smil;charset=utf-8", "<332047400>", "332047400" },
};

#define TEST_PARTS(parts) parts, G_N_ELEMENTS(parts)
#define TEST_PARTS_NONE NULL, 0

static const TestDesc retrieve_tests[] = {
{
"Success",
Expand All @@ -77,8 +102,9 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_DECODING,
MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
TEST_PARTS(retrieve_success_parts),
0
},{
},{
"DeferSuccess",
"Success",
"m-notification.ind",
Expand All @@ -88,8 +114,21 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_DECODING,
MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
TEST_PARTS(retrieve_success_parts),
TEST_DEFER_RECEIVE
},{
"Success2", /* Generated by Nokia C6 (Symbian "Belle") */
NULL,
"m-notification.ind",
"m-retrieve.conf",
SOUP_STATUS_OK,
MMS_CONTENT_TYPE,
NULL,
MMS_RECEIVE_STATE_DECODING,
MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND,
TEST_PARTS(retrieve_success2_parts),
0
},{
"Expired",
NULL,
"m-notification.ind",
Expand All @@ -99,6 +138,7 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
MMS_MESSAGE_TYPE_NONE,
TEST_PARTS_NONE,
0
},{
"SoonExpired",
Expand All @@ -110,6 +150,7 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
MMS_MESSAGE_TYPE_NONE,
TEST_PARTS_NONE,
0
},{
"NotAllowed",
Expand All @@ -121,6 +162,7 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
MMS_MESSAGE_TYPE_NONE,
TEST_PARTS_NONE,
0
},{
"NotFound",
Expand All @@ -132,6 +174,7 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_DOWNLOAD_ERROR,
MMS_MESSAGE_TYPE_NONE,
TEST_PARTS_NONE,
0
},{
"GarbageRetrieve",
Expand All @@ -143,6 +186,7 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_DECODING_ERROR,
MMS_MESSAGE_TYPE_NOTIFYRESP_IND,
TEST_PARTS_NONE,
0
},{
"GarbagePush",
Expand All @@ -154,6 +198,7 @@ static const TestDesc retrieve_tests[] = {
"000/push.pdu",
MMS_RECEIVE_STATE_INVALID,
MMS_MESSAGE_TYPE_NONE,
TEST_PARTS_NONE,
TEST_PUSH_HANDLING_FAILURE_OK
},{
"UnsupportedPush",
Expand All @@ -165,6 +210,7 @@ static const TestDesc retrieve_tests[] = {
"000/push.pdu",
MMS_RECEIVE_STATE_INVALID,
MMS_MESSAGE_TYPE_NONE,
TEST_PARTS_NONE,
TEST_PUSH_HANDLING_FAILURE_OK
},{
"ReadOrigInd",
Expand All @@ -176,6 +222,7 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_INVALID,
MMS_MESSAGE_TYPE_NONE,
TEST_PARTS_NONE,
0
},{
"DeliveryInd",
Expand All @@ -187,10 +234,60 @@ static const TestDesc retrieve_tests[] = {
NULL,
MMS_RECEIVE_STATE_INVALID,
MMS_MESSAGE_TYPE_NONE,
TEST_PARTS_NONE,
0
}
};

static
int
test_validate_parts(
Test* test)
{
const TestDesc* desc = test->desc;
MMSMessage* msg = mms_handler_test_get_received_message(test->handler,NULL);
if (!desc->nparts && (!msg || !g_slist_length(msg->parts))) {
return RET_OK;
} else {
const unsigned int nparts = g_slist_length(msg->parts);
if (desc->nparts == nparts) {
const TestPartDesc* expect = test->desc->parts;
GSList* list = msg->parts;
while (list) {
const MMSMessagePart* part = list->data;
const char* fname;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
fname = g_basename(part->file);
G_GNUC_END_IGNORE_DEPRECATIONS;
if (strcmp(expect->content_type, part->content_type)) {
MMS_ERR("Content type mismatch: expected %s, got %s",
expect->content_type, part->content_type);
return RET_ERR;
}
if (strcmp(expect->file_name, fname)) {
MMS_ERR("File name mismatch: expected %s, got %s",
expect->file_name, fname);
return RET_ERR;
}
if (!part->content_id) {
MMS_ERR("Missing content id");
return RET_ERR;
}
if (strcmp(expect->content_id, part->content_id)) {
MMS_ERR("Content-ID mismatch: expected %s, got %s",
expect->content_id, part->content_id);
return RET_ERR;
}
list = list->next;
expect++;
}
return RET_OK;
}
MMS_ERR("%u parts expected, got %u", desc->nparts, nparts);
return RET_ERR;
}
}

static
void
test_finish(
Expand All @@ -214,7 +311,9 @@ test_finish(
if (desc->reply_msg) {
MMSPdu* pdu = g_new0(MMSPdu, 1);
if (mms_message_decode(resp_data, resp_len, pdu)) {
if (pdu->type != desc->reply_msg) {
if (pdu->type == desc->reply_msg) {
test->ret = test_validate_parts(test);
} else {
test->ret = RET_ERR;
MMS_ERR("%s reply %u, expected %u", name,
pdu->type, desc->reply_msg);
Expand Down

0 comments on commit ce733e0

Please sign in to comment.