From: <svn...@op...> - 2009-03-16 15:13:07
|
Author: bellmich Date: Mon Mar 16 16:12:50 2009 New Revision: 973 URL: http://libsyncml.opensync.org/changeset/973 Log: added libsoup validation Added: trunk/tests/check_libsoup.c Modified: trunk/tests/CMakeLists.txt Modified: trunk/tests/CMakeLists.txt ============================================================================== --- trunk/tests/CMakeLists.txt Fri Mar 13 14:52:48 2009 (r972) +++ trunk/tests/CMakeLists.txt Mon Mar 16 16:12:50 2009 (r973) @@ -15,7 +15,7 @@ # include Testing Macro INCLUDE( Testing ) # include necessary headers - INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${GLIB2_INCLUDE_DIRS} ${LIBXML2_INCLUDE_DIRS} ${LIBWBXML2_INCLUDE_DIRS} ) + INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${GLIB2_INCLUDE_DIRS} ${LIBXML2_INCLUDE_DIRS} ${LIBWBXML2_INCLUDE_DIRS} ${LIBSOUP2_INCLUDE_DIRS} ) # add tests SET( TEST_TARGET_LIBRARIES support ) ADD_CHECK_TEST( error check_error.c ${TEST_TARGET_LIBRARIES} ) @@ -37,6 +37,7 @@ ENDIF( ENABLE_OBEX AND ENABLE_OPENOBEX_TCP ) IF ( ENABLE_HTTP ) + ADD_CHECK_TEST( libsoup check_libsoup.c ${TEST_TARGET_LIBRARIES} ) ADD_CHECK_TEST( http check_http.c ${TEST_TARGET_LIBRARIES} ) ADD_CHECK_TEST( manager check_manager.c ${TEST_TARGET_LIBRARIES} ) ENDIF ( ENABLE_HTTP ) Added: trunk/tests/check_libsoup.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/tests/check_libsoup.c Mon Mar 16 16:12:50 2009 (r973) @@ -0,0 +1,164 @@ +/* + * libsyncml - A syncml protocol implementation + * Copyright (C) 2009 Michael Bell <mic...@op...> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "support.h" + +#include <libsoup/soup-session-async.h> +#include <libsoup/soup-uri.h> +#include <libsoup/soup-server.h> +#include <libsoup/soup-message.h> +#include <libsoup/soup-session-sync.h> + +static int server_messages; +static int server_errors; + +static void init_testbed() +{ + setup_testbed(NULL); + + g_type_init(); + + server_messages = 0; + server_errors = 0; +} + +#ifdef HAVE_LIBSOUP22 +static void server_callback(SoupServerContext *context, SoupMessage *msg, gpointer data) +#else +static void server_callback( + SoupServer *server, + SoupMessage *msg, + const char *path, + GHashTable *query, + SoupClientContext *client, + gpointer data) +#endif +{ + smlTrace(TRACE_INTERNAL, "%s", __func__); +#ifdef HAVE_LIBSOUP22 + const char *content = msg->request.body; + size_t length = msg->request.length; +#else + const char *content = msg->request_body->data; + size_t length = msg->request_body->length; +#endif + if (length > strlen("test")) + length = strlen("test"); + if (memcmp("test", content, length) == 0) { + g_atomic_int_inc(&server_messages); + } else { + g_atomic_int_inc(&server_errors); + } +} + +START_TEST (libsoup_async) +{ + init_testbed(); + SmlError *error = NULL; + + /* prepare asynchronous runtime environment */ + + GMainContext *ctx = g_main_context_new(); + sml_fail_unless(ctx != NULL, NULL); + SmlThread *thread = smlThreadNew(ctx, &error); + sml_fail_unless(thread != NULL, "%s", smlErrorPrint(&error)); + + /* create async server */ + + SoupServer *server = soup_server_new (SOUP_SERVER_PORT, 13001, SOUP_SERVER_ASYNC_CONTEXT, ctx, NULL); + sml_fail_unless(server != NULL, NULL); +#ifdef HAVE_LIBSOUP22 + soup_server_add_handler (server, NULL, NULL, server_callback, NULL, NULL); +#else + soup_server_add_handler (server, NULL, server_callback, NULL, NULL); +#endif + soup_server_run_async (server); + + /* start thread */ + + smlThreadStart(thread); + + /* create synchonous client */ + + SoupSession *session = soup_session_sync_new (); + sml_fail_unless(session != NULL, NULL); + SoupMessage *msg = soup_message_new (SOUP_METHOD_GET, "http://127.0.0.1:13001/"); + soup_message_headers_append(msg->request_headers, "Accept", "text/plain"); + soup_message_set_request (msg, "text/plain", +#ifdef HAVE_LIBSOUP22 + SOUP_BUFFER_SYSTEM_OWNED, +#else + SOUP_MEMORY_TAKE, +#endif + g_memdup("test", 4), 4); + soup_session_send_message (session, msg); + + /* wait until message was received by server */ + + while (server_messages < 1 && server_errors < 1) + { + usleep(50); + } + + /* check counter */ + + sml_fail_unless(server_messages == 1, NULL); + sml_fail_unless(server_errors == 0, NULL); + + /* cleanup */ + soup_session_abort(session); + g_object_unref(session); + smlThreadStop(thread); + smlThreadFree(thread); + g_main_context_unref(ctx); + soup_server_quit(server); + g_object_unref(server); + +} +END_TEST + +Suite *http_suite(void) +{ + Suite *s = suite_create("libsoup validation"); + //Suite *s2 = suite_create("libsoup validation"); + + create_case(s, "libsoup_async", libsoup_async); + + return s; +} + +int main(void) +{ + configure_environment(); + + int nf; + + Suite *s = http_suite(); + + SRunner *sr; + sr = srunner_create(s); + srunner_run_all(sr, CK_VERBOSE); + nf = srunner_ntests_failed(sr); + srunner_free(sr); + + cleanup_environment(); + + return (nf == 0) ? 0 : 1; +} |