Commit a911b08b authored by Slava Monich's avatar Slava Monich

[test] Added test_retrieve_order

parent 2f610f21
......@@ -11,4 +11,5 @@ all:
@$(MAKE) -C test_retrieve $*
@$(MAKE) -C test_retrieve_cancel $*
@$(MAKE) -C test_retrieve_no_proxy $*
@$(MAKE) -C test_retrieve_order $*
@$(MAKE) -C test_send $*
......@@ -38,6 +38,27 @@ G_DEFINE_TYPE(MMSConnManTest, mms_connman_test, MMS_TYPE_CONNMAN);
#define MMS_CONNMAN_TEST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
MMS_TYPE_CONNMAN_TEST, MMSConnManTest))
static
gboolean
mms_connman_test_busy_cb(
gpointer data)
{
MMSConnManTest* test = MMS_CONNMAN_TEST(data);
mms_connman_busy_dec(&test->cm);
mms_connman_ref(&test->cm);
return G_SOURCE_REMOVE;
}
static
void
mms_connman_test_make_busy(
MMSConnManTest* test)
{
mms_connman_ref(&test->cm);
mms_connman_busy_inc(&test->cm);
g_idle_add(mms_connman_test_busy_cb, test);
}
void
mms_connman_test_set_port(
MMSConnMan* cm,
......@@ -74,6 +95,7 @@ mms_connman_test_close_connection(
MMSConnManTest* test = MMS_CONNMAN_TEST(cm);
if (test->conn) {
MMS_DEBUG("Closing connection...");
mms_connman_test_make_busy(test);
mms_connection_close(test->conn);
mms_connection_unref(test->conn);
test->conn = NULL;
......@@ -111,6 +133,7 @@ mms_connman_test_open_connection(
if (test->offline) {
return NULL;
} else {
mms_connman_test_make_busy(test);
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);
......
/*
* Copyright (C) 2013-2015 Jolla Ltd.
* Copyright (C) 2013-2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
......@@ -30,10 +30,8 @@ typedef struct mms_handler_test {
MMSDispatcher* dispatcher;
mms_handler_test_prenotify_fn prenotify_fn;
mms_handler_test_postnotify_fn postnotify_fn;
mms_handler_test_msgreceived_fn msgreceived_fn;
void* prenotify_data;
void* postnotify_data;
void* msgreceived_data;
} MMSHandlerTest;
typedef enum mms_handler_record_type {
......@@ -84,6 +82,19 @@ struct mms_handler_message_received_call {
void* param;
};
enum mms_handler_signal {
SIGNAL_MESSAGE_RECEIVED,
SIGNAL_MESSAGE_RECEIVE_STATE_CHANGED,
SIGNAL_MESSAGE_SEND_STATE_CHANGED,
SIGNAL_COUNT
};
#define SIGNAL_MESSAGE_RECEIVED_NAME "message-received"
#define SIGNAL_MESSAGE_RECEIVE_STATE_CHANGED_NAME "receive-state-changed"
#define SIGNAL_MESSAGE_SEND_STATE_CHANGED_NAME "send-state-changed"
static guint mms_handler_test_signals[SIGNAL_COUNT] = { 0 };
G_DEFINE_TYPE(MMSHandlerTest, mms_handler_test, MMS_TYPE_HANDLER);
#define MMS_TYPE_HANDLER_TEST (mms_handler_test_get_type())
#define MMS_HANDLER_TEST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
......@@ -569,9 +580,8 @@ mms_handler_test_message_received(
recv->msg = mms_message_ref(msg);
mms_handler_busy_inc(handler);
if (test->msgreceived_fn) {
test->msgreceived_fn(handler, msg, test->msgreceived_data);
}
g_signal_emit(handler, mms_handler_test_signals[
SIGNAL_MESSAGE_RECEIVED], 0, msg);
call->handler = mms_handler_ref(handler);
call->msg = mms_message_ref(msg);
......@@ -596,6 +606,8 @@ mms_handler_test_message_receive_state_changed(
if (recv) {
recv->state = state;
MMS_DEBUG("Message %s receive state %d", id, state);
g_signal_emit(handler, mms_handler_test_signals[
SIGNAL_MESSAGE_RECEIVE_STATE_CHANGED], 0, id, state);
return TRUE;
} else {
MMS_ERR("No such incoming message: %s", id);
......@@ -641,6 +653,8 @@ mms_handler_test_message_send_state_changed(
} else {
MMS_DEBUG("Message %s send state %d", id, state);
}
g_signal_emit(handler, mms_handler_test_signals[
SIGNAL_MESSAGE_SEND_STATE_CHANGED], 0, id, state, details);
return TRUE;
} else {
MMS_ERR("No such outbound message: %s", id);
......@@ -768,6 +782,21 @@ mms_handler_test_class_init(
klass->fn_read_report = mms_handler_test_read_report;
klass->fn_read_report_send_status =
mms_handler_test_read_report_send_status;
mms_handler_test_signals[SIGNAL_MESSAGE_RECEIVED] =
g_signal_new(SIGNAL_MESSAGE_RECEIVED_NAME,
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL, G_TYPE_NONE,
1, G_TYPE_POINTER);
mms_handler_test_signals[SIGNAL_MESSAGE_RECEIVE_STATE_CHANGED] =
g_signal_new(SIGNAL_MESSAGE_RECEIVE_STATE_CHANGED_NAME,
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL, G_TYPE_NONE,
2, G_TYPE_POINTER, G_TYPE_INT);
mms_handler_test_signals[SIGNAL_MESSAGE_SEND_STATE_CHANGED] =
g_signal_new(SIGNAL_MESSAGE_SEND_STATE_CHANGED_NAME,
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL, G_TYPE_NONE,
3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_POINTER);
}
static
......@@ -819,16 +848,34 @@ mms_handler_test_set_postnotify_fn(
test->postnotify_data = user_data;
}
void
mms_handler_test_set_msgreceived_fn(
gulong
mms_handler_test_add_msgreceived_fn(
MMSHandler* handler,
mms_handler_test_msgreceived_fn cb,
void* user_data)
{
MMSHandlerTest* test = MMS_HANDLER_TEST(handler);
MMS_ASSERT(!test->msgreceived_fn);
test->msgreceived_fn = cb;
test->msgreceived_data = user_data;
return g_signal_connect(handler, SIGNAL_MESSAGE_RECEIVED_NAME,
G_CALLBACK(cb), user_data);
}
gulong
mms_handler_test_add_send_state_fn(
MMSHandler* handler,
mms_handler_test_send_state_fn cb,
void* user_data)
{
return g_signal_connect(handler, SIGNAL_MESSAGE_SEND_STATE_CHANGED_NAME,
G_CALLBACK(cb), user_data);
}
gulong
mms_handler_test_add_receive_state_fn(
MMSHandler* handler,
mms_handler_test_receive_state_fn cb,
void* user_data)
{
return g_signal_connect(handler, SIGNAL_MESSAGE_RECEIVE_STATE_CHANGED_NAME,
G_CALLBACK(cb), user_data);
}
void
......@@ -843,8 +890,6 @@ mms_handler_test_reset(
test->prenotify_data = NULL;
test->postnotify_fn = NULL;
test->postnotify_data = NULL;
test->msgreceived_fn = NULL;
test->msgreceived_data = NULL;
}
/*
......
/*
* Copyright (C) 2013-2015 Jolla Ltd.
* Copyright (C) 2013-2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
......@@ -116,6 +116,21 @@ typedef void
MMSMessage* msg,
void* user_data);
typedef void
(*mms_handler_test_receive_state_fn)(
MMSHandler* handler,
const char* id,
MMS_RECEIVE_STATE state,
void* user_data);
typedef void
(*mms_handler_test_send_state_fn)(
MMSHandler* handler,
const char* id,
MMS_SEND_STATE state,
const char* details,
void* user_data);
void
mms_handler_test_set_prenotify_fn(
MMSHandler* handler,
......@@ -128,12 +143,24 @@ mms_handler_test_set_postnotify_fn(
mms_handler_test_postnotify_fn cb,
void* user_data);
void
mms_handler_test_set_msgreceived_fn(
gulong
mms_handler_test_add_msgreceived_fn(
MMSHandler* handler,
mms_handler_test_msgreceived_fn cb,
void* user_data);
gulong
mms_handler_test_add_send_state_fn(
MMSHandler* handler,
mms_handler_test_send_state_fn cb,
void* user_data);
gulong
mms_handler_test_add_receive_state_fn(
MMSHandler* handler,
mms_handler_test_receive_state_fn cb,
void* user_data);
void
mms_handler_test_reset(
MMSHandler* handler);
......
/*
* Copyright (C) 2013-2014 Jolla Ltd.
* Copyright (C) 2013-2016 Jolla Ltd.
*
* 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
......@@ -18,17 +18,30 @@
#include <libsoup/soup.h>
/* A single HTTP transaction */
/* A single HTTP response */
typedef struct test_http_response {
GMappedFile* file;
char* content_type;
int status;
} TestHttpResponse;
struct test_http {
gint ref_count;
SoupServer* server;
GMappedFile* resp_file;
GBytes* req_bytes;
int resp_status;
char* resp_content_type;
GPtrArray* responses;
GPtrArray* post_data;
gboolean disconnected;
guint current_resp;
};
static
void
test_http_post_data_free(
gpointer data)
{
soup_buffer_free((SoupBuffer*)data);
}
static
void
test_http_callback(
......@@ -39,6 +52,7 @@ test_http_callback(
SoupClientContext* context,
gpointer data)
{
TestHttp* http = data;
char* uri = soup_uri_to_string(soup_message_get_uri (msg), FALSE);
MMS_VERBOSE("%s %s HTTP/1.%d", msg->method, uri,
soup_message_get_http_version(msg));
......@@ -46,29 +60,34 @@ test_http_callback(
if (msg->method == SOUP_METHOD_CONNECT) {
soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED);
} else {
TestHttp* http = data;
if (msg->request_body->length) {
SoupBuffer* request = soup_message_body_flatten(msg->request_body);
if (http->req_bytes) g_bytes_unref(http->req_bytes);
http->req_bytes = g_bytes_new_with_free_func(request->data,
request->length, (GDestroyNotify)soup_buffer_free, request);
SoupBuffer* post = soup_message_body_flatten(msg->request_body);
g_ptr_array_add(http->post_data,
g_bytes_new_with_free_func(post->data, post->length,
test_http_post_data_free, post));
}
soup_message_set_status(msg, http->resp_status);
soup_message_headers_set_content_type(msg->response_headers,
http->resp_content_type ? http->resp_content_type : "text/plain",
NULL);
soup_message_headers_append(msg->response_headers,
"Accept-Ranges", "bytes");
soup_message_headers_append(msg->response_headers,
"Connection", "close");
if (http->resp_file) {
soup_message_headers_set_content_length(msg->response_headers,
g_mapped_file_get_length(http->resp_file));
soup_message_body_append(msg->response_body, SOUP_MEMORY_TEMPORARY,
g_mapped_file_get_contents(http->resp_file),
g_mapped_file_get_length(http->resp_file));
if (http->current_resp >= http->responses->len) {
soup_message_set_status(msg, SOUP_STATUS_NOT_FOUND);
} else {
soup_message_headers_set_content_length(msg->response_headers, 0);
const TestHttpResponse* resp =
http->responses->pdata[(http->current_resp)++];
soup_message_set_status(msg, resp->status);
soup_message_headers_set_content_type(msg->response_headers,
resp->content_type ? resp->content_type : "text/plain", NULL);
soup_message_headers_append(msg->response_headers,
"Accept-Ranges", "bytes");
soup_message_headers_append(msg->response_headers,
"Connection", "close");
if (resp->file) {
soup_message_headers_set_content_length(msg->response_headers,
g_mapped_file_get_length(resp->file));
soup_message_body_append(msg->response_body,
SOUP_MEMORY_TEMPORARY,
g_mapped_file_get_contents(resp->file),
g_mapped_file_get_length(resp->file));
} else {
soup_message_headers_set_content_length(msg->response_headers,0);
}
}
}
soup_message_body_complete(msg->request_body);
......@@ -81,11 +100,27 @@ test_http_get_port(
return soup_server_get_port(http->server);
}
guint
test_http_get_post_count(
TestHttp* http)
{
return http ? http->post_data->len : 0;
}
GBytes*
test_http_get_post_data_at(
TestHttp* http,
guint index)
{
return (http && index < http->post_data->len) ?
http->post_data->pdata[index] : NULL;
}
GBytes*
test_http_get_post_data(
TestHttp* http)
{
return http ? http->req_bytes : NULL;
return (http && http->post_data->len) ? http->post_data->pdata[0] : NULL;
}
void
......@@ -98,23 +133,60 @@ test_http_close(
}
}
static
void
test_http_post_data_bytes_free(
gpointer data)
{
g_bytes_unref(data);
}
static
void
test_http_response_free(
gpointer data)
{
TestHttpResponse* resp = data;
if (resp->file) {
g_mapped_file_unref(resp->file);
g_free(resp->content_type);
}
g_free(resp);
}
void
test_http_add_response(
TestHttp* http,
GMappedFile* file,
const char* content_type,
int status)
{
TestHttpResponse* resp = g_new0(TestHttpResponse, 1);
if (file) {
resp->file = g_mapped_file_ref(file);
resp->content_type = g_strdup(content_type);
}
resp->status = status;
g_ptr_array_add(http->responses, resp);
}
TestHttp*
test_http_new(
GMappedFile* resp_file,
GMappedFile* get_file,
const char* resp_content_type,
int resp_status)
{
TestHttp* http = g_new0(TestHttp, 1);
http->ref_count = 1;
if (resp_file) {
http->resp_file = g_mapped_file_ref(resp_file);
http->resp_content_type = g_strdup(resp_content_type);
}
http->resp_status = resp_status;
http->responses = g_ptr_array_new_full(0, test_http_response_free);
http->post_data = g_ptr_array_new_full(0, test_http_post_data_bytes_free);
http->server = g_object_new(SOUP_TYPE_SERVER, NULL);
MMS_DEBUG("Listening on port %hu", soup_server_get_port(http->server));
soup_server_add_handler(http->server, NULL, test_http_callback, http, NULL);
soup_server_run_async(http->server);
if (get_file || resp_content_type || resp_status) {
test_http_add_response(http, get_file, resp_content_type, resp_status);
}
return http;
}
......@@ -137,10 +209,9 @@ test_http_unref(
MMS_ASSERT(http->ref_count > 0);
if (g_atomic_int_dec_and_test(&http->ref_count)) {
test_http_close(http);
if (http->resp_file) g_mapped_file_unref(http->resp_file);
if (http->req_bytes) g_bytes_unref(http->req_bytes);
g_ptr_array_unref(http->responses);
g_ptr_array_unref(http->post_data);
g_object_unref(http->server);
g_free(http->resp_content_type);
g_free(http);
}
}
......
/*
* Copyright (C) 2013-2014 Jolla Ltd.
* Copyright (C) 2013-2016 Jolla Ltd.
*
* 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
......@@ -38,6 +38,22 @@ guint
test_http_get_port(
TestHttp* http);
void
test_http_add_response(
TestHttp* http,
GMappedFile* file,
const char* content_type,
int status);
guint
test_http_get_post_count(
TestHttp* http);
GBytes*
test_http_get_post_data_at(
TestHttp* http,
guint index);
GBytes*
test_http_get_post_data(
TestHttp* http);
......
......@@ -5,7 +5,8 @@
TESTS="test_media_type test_mms_codec test_delivery_ind \
test_read_ind test_read_report test_resize test_retrieve \
test_retrieve_cancel test_retrieve_no_proxy test_send"
test_retrieve_cancel test_retrieve_no_proxy test_retrieve_order \
test_send"
FLAVOR="release"
pushd `dirname $0` > /dev/null
......
/*
* Copyright (C) 2013-2015 Jolla Ltd.
* Copyright (C) 2013-2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
......@@ -76,6 +76,7 @@ typedef struct test {
GMappedFile* retrieve_conf;
GMainLoop* loop;
guint timeout_id;
gulong msgreceived_id;
TestHttp* http;
int ret;
} Test;
......@@ -500,6 +501,7 @@ test_finish(
}
MMS_INFO("%s: %s", (test->ret == RET_OK) ? "OK" : "FAILED", name);
mms_handler_test_reset(test->handler);
g_signal_handler_disconnect(test->handler, test->msgreceived_id);
g_main_loop_quit(test->loop);
}
......@@ -627,8 +629,9 @@ test_init(
mms_handler_test_defer_receive(test->handler, test->disp);
}
mms_handler_test_set_prenotify_fn(test->handler, test_notify, test);
mms_handler_test_set_msgreceived_fn(test->handler,
test_msgreceived, test);
test->msgreceived_id =
mms_handler_test_add_msgreceived_fn(test->handler,
test_msgreceived, test);
mms_settings_unref(settings);
test->ret = RET_ERR;
ok = TRUE;
......
# -*- Mode: makefile-gmake -*-
EXE = test_retrieve_order
SRC = $(EXE).c
COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c
include ../common/Makefile
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment