[PATCH 6/6] libtests/mghttpd01: New test
sebastian.huber at embedded-brains.de
sebastian.huber at embedded-brains.de
Tue Jul 3 08:45:41 UTC 2012
From: Sebastian Huber <sebastian.huber at embedded-brains.de>
---
testsuites/libtests/Makefile.am | 1 +
testsuites/libtests/configure.ac | 1 +
testsuites/libtests/mghttpd01/Makefile.am | 31 +++
testsuites/libtests/mghttpd01/init.c | 240 ++++++++++++++++++++++
testsuites/libtests/mghttpd01/init_fs.tar | Bin 0 -> 10240 bytes
testsuites/libtests/mghttpd01/mghttpd01.doc | 11 +
testsuites/libtests/mghttpd01/mghttpd01.scn | 33 +++
testsuites/libtests/mghttpd01/test-http-client.c | 95 +++++++++
testsuites/libtests/mghttpd01/test-http-client.h | 58 ++++++
9 files changed, 470 insertions(+), 0 deletions(-)
create mode 100644 testsuites/libtests/mghttpd01/Makefile.am
create mode 100644 testsuites/libtests/mghttpd01/init.c
create mode 100644 testsuites/libtests/mghttpd01/init_fs.tar
create mode 100644 testsuites/libtests/mghttpd01/mghttpd01.doc
create mode 100644 testsuites/libtests/mghttpd01/mghttpd01.scn
create mode 100644 testsuites/libtests/mghttpd01/test-http-client.c
create mode 100644 testsuites/libtests/mghttpd01/test-http-client.h
diff --git a/testsuites/libtests/Makefile.am b/testsuites/libtests/Makefile.am
index 3710ad8..69f4e8d 100644
--- a/testsuites/libtests/Makefile.am
+++ b/testsuites/libtests/Makefile.am
@@ -20,6 +20,7 @@ SUBDIRS += bspcmdline01 cpuuse devfs01 devfs02 devfs03 devfs04 \
mouse01
if NETTESTS
+SUBDIRS += mghttpd01
SUBDIRS += ftp01
SUBDIRS += syscall01
endif
diff --git a/testsuites/libtests/configure.ac b/testsuites/libtests/configure.ac
index b58c4a8..77c1e43 100644
--- a/testsuites/libtests/configure.ac
+++ b/testsuites/libtests/configure.ac
@@ -41,6 +41,7 @@ AM_CONDITIONAL(NETTESTS,test "$rtems_cv_RTEMS_NETWORKING" = "yes")
# Explicitly list all Makefiles here
AC_CONFIG_FILES([Makefile
+mghttpd01/Makefile
block15/Makefile
block14/Makefile
block13/Makefile
diff --git a/testsuites/libtests/mghttpd01/Makefile.am b/testsuites/libtests/mghttpd01/Makefile.am
new file mode 100644
index 0000000..1e3f3a9
--- /dev/null
+++ b/testsuites/libtests/mghttpd01/Makefile.am
@@ -0,0 +1,31 @@
+rtems_tests_PROGRAMS = mghttpd01
+
+mghttpd01_SOURCES = init.c init_fs.c init_fs.h \
+ test-http-client.c test-http-client.h
+mghttpd01_LDADD = -lmghttpd
+BUILT_SOURCES = init_fs.c init_fs.h
+
+dist_rtems_tests_DATA = mghttpd01.scn mghttpd01.doc
+
+include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP at .cfg
+include $(top_srcdir)/../automake/compile.am
+include $(top_srcdir)/../automake/leaf.am
+
+AM_CPPFLAGS += -I$(top_srcdir)/../support/include
+
+LINK_OBJS = $(mghttpd01_OBJECTS) $(mghttpd01_LDADD)
+LINK_LIBS = $(mghttpd01_LDLIBS)
+
+mghttpd01$(EXEEXT): $(mghttpd01_OBJECTS) $(mghttpd01_DEPENDENCIES)
+ @rm -f mghttpd01$(EXEEXT)
+ $(make-exe)
+
+init_fs.c:
+ $(BIN2C) -C $(srcdir)/init_fs.tar init_fs
+CLEANFILES += init_fs.c
+
+init_fs.h:
+ $(BIN2C) -H $(srcdir)/init_fs.tar init_fs
+CLEANFILES += init_fs.h
+
+include $(top_srcdir)/../automake/local.am
diff --git a/testsuites/libtests/mghttpd01/init.c b/testsuites/libtests/mghttpd01/init.c
new file mode 100644
index 0000000..5d85aae
--- /dev/null
+++ b/testsuites/libtests/mghttpd01/init.c
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 2012 embedded brains GmbH. All rights reserved.
+ *
+ * embedded brains GmbH
+ * Obere Lagerstr. 30
+ * 82178 Puchheim
+ * Germany
+ * <rtems at embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ */
+
+#ifdef HAVE_CONFIG_H
+ #include "config.h"
+#endif
+
+#include <rtems.h>
+
+#include <tmacros.h>
+
+#include <rtems/rtems_bsdnet.h>
+
+#include <stdio.h>
+#include <mghttpd/mongoose.h>
+
+#include <rtems/imfs.h>
+#include <rtems/error.h>
+#include "init_fs.h"
+
+#include "test-http-client.h"
+
+#define TARFILE_START init_fs_tar
+#define TARFILE_SIZE init_fs_tar_size
+
+#define CBACKTEST_URI "/callbacktest.txt"
+#define CBACKTEST_TXT "HTTP/1.1 200 OK\r\n" \
+ "Content-Type: text/plain\r\n" \
+ "Content-Length: 47\r\n" \
+ "\r\n" \
+ "This is a message from the callback function.\r\n"
+
+#define INDEX_HTML "HTTP/1.1 200 OK\r\n" \
+ "Date: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \
+ "Last-Modified: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \
+ "Etag: \"21dae501.a2\"\r\n" \
+ "Content-Type: text/html\r\n" \
+ "Content-Length: 162\r\n" \
+ "Connection: close\r\n" \
+ "Accept-Ranges: bytes\r\n" \
+ "\r\n" \
+ "<html>\r\n" \
+ "<head>\r\n" \
+ "<title>Second Instance</title>\r\n" \
+ "</head>\r\n" \
+ "\r\n" \
+ "<body>\r\n" \
+ "<h1>Second Instance</h1>\r\n" \
+ "A test page for the Mongoose web server on RTEMS.\r\n" \
+ "</body>\r\n" \
+ "</html>\r\n"
+
+#define DATE_TAG "Date: "
+#define LASTMOD_TAG "Last-Modified: "
+#define TIMESTAMP_SIZE (sizeof("Fri, 01 Jan 1988 00:00:26 GMT") - 1)
+
+#define BUFFERSIZE 1024
+
+static void test_tarfs_load(void)
+{
+ rtems_status_code sc;
+
+ printf("Loading tarfs image ... ");
+ sc = rtems_tarfs_load("/",(void *)TARFILE_START, TARFILE_SIZE);
+ if (sc != RTEMS_SUCCESSFUL) {
+ printf ("error: untar failed: %s\n", rtems_status_text (sc));
+ rtems_test_exit(1);
+ }
+ printf ("successful\n");
+}
+
+typedef struct {
+ char *string;
+ int size;
+} printctx;
+
+static void *callback(enum mg_event event,
+ struct mg_connection *conn,
+ const struct mg_request_info *request_info)
+{
+ if (event == MG_NEW_REQUEST) {
+ int cbacktest = strncmp(request_info->uri, CBACKTEST_URI, sizeof(CBACKTEST_URI));
+ if (cbacktest == 0)
+ {
+ mg_write(conn, CBACKTEST_TXT, sizeof(CBACKTEST_TXT));
+
+ /* Mark as processed */
+ return "";
+ }
+ }
+ return NULL;
+}
+
+static void test_mg_index_html(void)
+{
+ httpc_context httpc_ctx;
+ char *buffer = malloc(BUFFERSIZE);
+ char *workpos = buffer;
+ bool brv = false;
+ int rv = 0;
+
+ rtems_test_assert(buffer != NULL);
+
+ puts("=== Get the index.html from second Mongoose instance:");
+
+ httpc_init_context(&httpc_ctx);
+ brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 8080);
+ rtems_test_assert(brv);
+ brv = httpc_send_request(&httpc_ctx, "GET /index.html", buffer, BUFFERSIZE);
+ rtems_test_assert(brv);
+ brv = httpc_close_connection(&httpc_ctx);
+ rtems_test_assert(brv);
+ puts(buffer);
+
+ /* remove timestamps from html-header */
+ workpos = strstr(buffer, DATE_TAG);
+ rtems_test_assert(workpos != NULL);
+ workpos += sizeof(DATE_TAG) - 1;
+ memset(workpos, 'x', TIMESTAMP_SIZE);
+
+ workpos = strstr(buffer, LASTMOD_TAG);
+ rtems_test_assert(workpos != NULL);
+ workpos += sizeof(LASTMOD_TAG) - 1;
+ memset(workpos, 'x', TIMESTAMP_SIZE);
+
+ rv = strcmp(buffer, INDEX_HTML);
+ rtems_test_assert(rv == 0);
+
+ puts("=== OK");
+
+ free(buffer);
+}
+
+static void test_mg_callback(void)
+{
+ httpc_context httpc_ctx;
+ char *buffer = malloc(BUFFERSIZE);
+ bool brv = false;
+ int rv = 0;
+
+ rtems_test_assert(buffer != NULL);
+
+ puts("=== Get a page generated from a callback function from" \
+ " first Mongoose instance:");
+
+ httpc_init_context(&httpc_ctx);
+ brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 80);
+ rtems_test_assert(brv);
+ brv = httpc_send_request(&httpc_ctx, "GET " CBACKTEST_URI, buffer, BUFFERSIZE);
+ rtems_test_assert(brv);
+ brv = httpc_close_connection(&httpc_ctx);
+ rtems_test_assert(brv);
+ puts(buffer);
+ rv = strcmp(buffer, CBACKTEST_TXT);
+ rtems_test_assert(rv == 0);
+
+ puts("=== OK");
+
+ free(buffer);
+}
+
+static void test_mongoose(void)
+{
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ const char *options[] = {
+ "listening_ports", "80",
+ "document_root", "/www/",
+ "num_threads", "1",
+ "max_request_size", "2048",
+ "thread_stack_size", "16384",
+ "thread_priority", "250",
+ "thread_policy", "o",
+ NULL};
+ const char *options2[] = {
+ "listening_ports", "8080",
+ "document_root", "/www2/",
+ "num_threads", "1",
+ "thread_stack_size", "16384",
+ "max_request_size", "2048",
+ NULL};
+
+ struct mg_context *mg1 = mg_start(&callback, NULL, options);
+ struct mg_context *mg2 = mg_start(NULL, NULL, options2);
+
+ test_mg_index_html();
+ test_mg_callback();
+
+ mg_stop(mg1);
+ mg_stop(mg2);
+}
+
+static void Init(rtems_task_argument arg)
+{
+ int rv = 0;
+
+ puts("\n\n*** TEST MGHTTPD 01 ***");
+
+ rv = rtems_bsdnet_initialize_network();
+ rtems_test_assert(rv == 0);
+
+ test_tarfs_load();
+
+ test_mongoose();
+
+ puts("*** END OF TEST MGHTTPD 01 ***");
+
+ rtems_test_exit(0);
+}
+
+#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+
+#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
+
+#define CONFIGURE_FILESYSTEM_IMFS
+
+#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 16
+
+#define CONFIGURE_UNLIMITED_OBJECTS
+
+#define CONFIGURE_UNIFIED_WORK_AREAS
+
+#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
+
+#define CONFIGURE_INIT_TASK_STACK_SIZE (16 * 1024)
+
+#define CONFIGURE_INIT
+
+#include <rtems/confdefs.h>
diff --git a/testsuites/libtests/mghttpd01/init_fs.tar b/testsuites/libtests/mghttpd01/init_fs.tar
new file mode 100644
index 0000000000000000000000000000000000000000..67132d5122eb81c9f0fdd940a3cad335d7e0045c
GIT binary patch
literal 10240
zcmeH}zfQw25XLiC>O0&MG<N<jQ6&ZjhAxzaF-;7qNMjT`q`W=nv at n#YFf>TO=j9xq
zV_$w>eCNLJeQ)9>xTSIIv5{#koE0*P2J8CVJSo#O6f%jU&=XPwLF!RFaa_alow0dK
z<Q2Q~yLCpj=KPf3=<QkmBCqRBUOZW4?85HtrN6 at ooE-EX*Pnd{{Y4N<r+*m9z$0<#
zK*rYl=lb94j!dPyx}-+EsFsXUWUH;AoiZkWREzm+3>(J&1_3_*!PEu7o&Rs=|2T2^
zKa?@@|4;m3gZ|s5RQp9`cl9J&ewF`&DEP?#VaWFc$>l#m{+~pQlSMP9`f@(YDwUVc
z+O4hCa;*y8lyuYZ?WieK<{yUH?;j^}ccaVK;Z^y0ekIv+Mea&L&s;|6LAP926s>gg
zsI^hls|^>M*3}%{(CyvzYR$DK^WRtXkCB}Oj7Jav0T2KI5C8!X009sH0T2KI5C8!X
a009sH0T2KI5C8!X009sH0T2LzQ37w3*o|5M
literal 0
HcmV?d00001
diff --git a/testsuites/libtests/mghttpd01/mghttpd01.doc b/testsuites/libtests/mghttpd01/mghttpd01.doc
new file mode 100644
index 0000000..7db498b
--- /dev/null
+++ b/testsuites/libtests/mghttpd01/mghttpd01.doc
@@ -0,0 +1,11 @@
+This file describes the directives and concepts tested by this test set.
+
+test set name: mghttpd01
+
+directives:
+
+ TBD
+
+concepts:
+
+ - Ensure that the Mongoose HTTP server works with a basic setup
diff --git a/testsuites/libtests/mghttpd01/mghttpd01.scn b/testsuites/libtests/mghttpd01/mghttpd01.scn
new file mode 100644
index 0000000..d3c061f
--- /dev/null
+++ b/testsuites/libtests/mghttpd01/mghttpd01.scn
@@ -0,0 +1,33 @@
+*** TEST MGHTTPD 01 ***
+Loading tarfs image ... successful
+=== Get the index.html from second Mongoose instance:
+HTTP/1.1 200 OK
+Date: Fri, 01 Jan 1988 00:00:01 GMT
+Last-Modified: Fri, 01 Jan 1988 00:00:01 GMT
+Etag: "21dae501.a2"
+Content-Type: text/html
+Content-Length: 162
+Connection: close
+Accept-Ranges: bytes
+
+<html>
+<head>
+<title>Second Instance</title>
+</head>
+
+<body>
+<h1>Second Instance</h1>
+A test page for the Mongoose web server on RTEMS.
+</body>
+</html>
+
+=== OK
+=== Get a page generated from a callback function from first Mongoose instance:
+HTTP/1.1 200 OK
+Content-Type: text/plain
+Content-Length: 47
+
+This is a message from the callback function.
+
+=== OK
+*** END OF TEST MGHTTPD 01 ***
diff --git a/testsuites/libtests/mghttpd01/test-http-client.c b/testsuites/libtests/mghttpd01/test-http-client.c
new file mode 100644
index 0000000..6e78457
--- /dev/null
+++ b/testsuites/libtests/mghttpd01/test-http-client.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2012 embedded brains GmbH. All rights reserved.
+ *
+ * embedded brains GmbH
+ * Obere Lagerstr. 30
+ * 82178 Puchheim
+ * Germany
+ * <rtems at embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ */
+
+#include "test-http-client.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netdb.h>
+#include <unistd.h>
+
+void httpc_init_context(
+ httpc_context *ctx
+)
+{
+ ctx->socket = -1;
+ ctx->fd = NULL;
+}
+
+bool httpc_open_connection(
+ httpc_context *ctx,
+ char *targethost,
+ int targetport
+)
+{
+ struct sockaddr_in addr;
+
+ struct hostent *server;
+ struct servent *service;
+
+ ctx->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if(ctx->socket < 0) { return false; }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(targetport);
+
+ server = gethostbyname(targethost);
+ if(server == NULL) { return false; }
+ memcpy(&addr.sin_addr.s_addr, server->h_addr, (size_t) server->h_length);
+
+ if(connect(ctx->socket, (struct sockaddr *)&addr, sizeof(addr)) != 0)
+ {
+ return false;
+ }
+
+ ctx->fd = fdopen(ctx->socket,"rw");
+ if(ctx->fd == NULL) { return false; }
+
+ return true;
+}
+
+bool httpc_close_connection(
+ httpc_context *ctx
+)
+{
+ if(close(ctx->socket) != 0)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+bool httpc_send_request(
+ httpc_context *ctx,
+ char *request,
+ char *response,
+ int responsesize
+)
+{
+ int size = strlen(request);
+ char lineend[] = " HTTP/1.1\r\n\r\n";
+
+ write(ctx->socket, request, size);
+ write(ctx->socket, lineend, sizeof(lineend));
+
+ size = read(ctx->socket, response, responsesize-1);
+ response[size] = '\0';
+
+ return true;
+}
+
diff --git a/testsuites/libtests/mghttpd01/test-http-client.h b/testsuites/libtests/mghttpd01/test-http-client.h
new file mode 100644
index 0000000..5e06f96
--- /dev/null
+++ b/testsuites/libtests/mghttpd01/test-http-client.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2012 embedded brains GmbH. All rights reserved.
+ *
+ * embedded brains GmbH
+ * Obere Lagerstr. 30
+ * 82178 Puchheim
+ * Germany
+ * <rtems at embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ */
+
+#ifndef TEST_WEB_CLIENT_H
+#define TEST_WEB_CLIENT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+typedef struct
+{
+ int socket;
+ FILE *fd;
+}
+httpc_context;
+
+void httpc_init_context(
+ httpc_context *ctx
+);
+
+bool httpc_open_connection(
+ httpc_context *ctx,
+ char *targethost,
+ int targetport
+);
+
+bool httpc_close_connection(
+ httpc_context *ctx
+);
+
+bool httpc_send_request(
+ httpc_context *ctx,
+ char *request,
+ char *response,
+ int responsesize
+);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* TEST_WEB_CLIENT_H */
--
1.6.4.2
More information about the devel
mailing list