source: rtems/testsuites/libtests/mghttpd01/init.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <rtems.h>
20
21#include <tmacros.h>
22
23#include <rtems/rtems_bsdnet.h>
24
25#include <stdio.h>
26#include <mghttpd/mongoose.h>
27
28#include <rtems/imfs.h>
29#include <rtems/error.h>
30#include "init_fs.h"
31
32#include "test-http-client.h"
33
34const char rtems_test_name[] = "MGHTTPD 1";
35
36#define TARFILE_START init_fs_tar
37#define TARFILE_SIZE  init_fs_tar_size
38
39#define CBACKTEST_URI   "/callbacktest.txt"
40#define CBACKTEST_TXT   "HTTP/1.1 200 OK\r\n" \
41                        "Content-Type: text/plain\r\n" \
42                        "Content-Length: 47\r\n" \
43                        "\r\n" \
44                        "This is a message from the callback function.\r\n"
45
46#define INDEX_HTML      "HTTP/1.1 200 OK\r\n" \
47                        "Date: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \
48                        "Last-Modified: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \
49                        "Etag: \"21dae500.162\"\r\n" \
50                        "Content-Type: text/html\r\n" \
51                        "Content-Length: 162\r\n" \
52                        "Connection: close\r\n" \
53                        "Accept-Ranges: bytes\r\n" \
54                        "\r\n" \
55                        "<html>\r\n" \
56                        "<head>\r\n" \
57                        "<title>Second Instance</title>\r\n" \
58                        "</head>\r\n" \
59                        "\r\n" \
60                        "<body>\r\n" \
61                        "<h1>Second Instance</h1>\r\n" \
62                        "A test page for the Mongoose web server on RTEMS.\r\n" \
63                        "</body>\r\n" \
64                        "</html>\r\n"
65
66#define DATE_TAG        "Date: "
67#define LASTMOD_TAG     "Last-Modified: "
68#define TIMESTAMP_SIZE  (sizeof("Fri, 01 Jan 1988 00:00:26 GMT") - 1)
69
70#define BUFFERSIZE      1024
71
72static void test_tarfs_load(void)
73{
74  rtems_status_code sc;
75
76  printf("Loading tarfs image ... ");
77  sc = rtems_tarfs_load("/",(void *)TARFILE_START, TARFILE_SIZE);
78  if (sc != RTEMS_SUCCESSFUL) {
79    printf ("error: untar failed: %s\n", rtems_status_text (sc));
80    rtems_test_exit(1);
81  }
82  printf ("successful\n");
83}
84
85static int callback(struct mg_connection *conn)
86{
87  int cbacktest = strncmp(mg_get_request_info(conn)->uri, CBACKTEST_URI, sizeof(CBACKTEST_URI));
88  if (cbacktest == 0)
89  {
90    mg_write(conn, CBACKTEST_TXT, sizeof(CBACKTEST_TXT));
91
92    /* Mark as processed */
93    return 1;
94  }
95
96  return 0;
97}
98
99static void test_mg_index_html(void)
100{
101  httpc_context httpc_ctx;
102  char *buffer = malloc(BUFFERSIZE);
103  char *workpos = buffer;
104  bool brv = false;
105  int rv = 0;
106
107  rtems_test_assert(buffer != NULL);
108
109  puts("=== Get the index.html from second Mongoose instance:");
110
111  httpc_init_context(&httpc_ctx);
112  brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 8080);
113  rtems_test_assert(brv);
114  brv = httpc_send_request(&httpc_ctx, "GET /index.html", buffer, BUFFERSIZE);
115  rtems_test_assert(brv);
116  brv = httpc_close_connection(&httpc_ctx);
117  rtems_test_assert(brv);
118  puts(buffer);
119
120  /* remove timestamps from html-header */
121  workpos = strstr(buffer, DATE_TAG);
122  rtems_test_assert(workpos != NULL);
123  workpos += sizeof(DATE_TAG) - 1;
124  memset(workpos, 'x', TIMESTAMP_SIZE);
125
126  workpos = strstr(buffer, LASTMOD_TAG);
127  rtems_test_assert(workpos != NULL);
128  workpos += sizeof(LASTMOD_TAG) - 1;
129  memset(workpos, 'x', TIMESTAMP_SIZE);
130
131  rv = strcmp(buffer, INDEX_HTML);
132  rtems_test_assert(rv == 0);
133
134  puts("=== OK");
135
136  free(buffer);
137}
138
139static void test_mg_callback(void)
140{
141  httpc_context httpc_ctx;
142  char *buffer = malloc(BUFFERSIZE);
143  bool brv = false;
144  int rv = 0;
145
146  rtems_test_assert(buffer != NULL);
147
148  puts("=== Get a page generated from a callback function from" \
149      " first Mongoose instance:");
150
151  httpc_init_context(&httpc_ctx);
152  brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 80);
153  rtems_test_assert(brv);
154  brv = httpc_send_request(&httpc_ctx, "GET " CBACKTEST_URI, buffer, BUFFERSIZE);
155  rtems_test_assert(brv);
156  brv = httpc_close_connection(&httpc_ctx);
157  rtems_test_assert(brv);
158  puts(buffer);
159  rv = strcmp(buffer, CBACKTEST_TXT);
160  rtems_test_assert(rv == 0);
161
162  puts("=== OK");
163
164  free(buffer);
165}
166
167static void test_mongoose(void)
168{
169  const struct mg_callbacks callbacks = {
170    .begin_request = callback
171  };
172  const char *options[] = {
173    "listening_ports", "80",
174    "document_root", "/www",
175    "num_threads", "1",
176    "thread_stack_size", "16384",
177    "thread_priority", "250",
178    "thread_policy", "o",
179    NULL};
180  const struct mg_callbacks callbacks2 = {
181    NULL
182  };
183  const char *options2[] = {
184    "listening_ports", "8080",
185    "document_root", "/www2",
186    "num_threads", "1",
187    "thread_stack_size", "16384",
188    NULL};
189
190  struct mg_context *mg1 = mg_start(&callbacks, NULL, options);
191  struct mg_context *mg2 = mg_start(&callbacks2, NULL, options2);
192
193  test_mg_index_html();
194  test_mg_callback();
195
196  mg_stop(mg1);
197  mg_stop(mg2);
198}
199
200static void Init(rtems_task_argument arg)
201{
202  int rv = 0;
203
204  TEST_BEGIN();
205
206  rv = rtems_bsdnet_initialize_network();
207  rtems_test_assert(rv == 0);
208
209  test_tarfs_load();
210
211  test_mongoose();
212
213  TEST_END();
214
215  rtems_test_exit(0);
216}
217
218#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
219#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
220
221#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
222
223#define CONFIGURE_FILESYSTEM_IMFS
224
225#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 16
226
227#define CONFIGURE_UNLIMITED_OBJECTS
228
229#define CONFIGURE_UNIFIED_WORK_AREAS
230
231#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
232
233#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
234
235#define CONFIGURE_INIT_TASK_STACK_SIZE (16 * 1024)
236
237#define CONFIGURE_INIT
238
239#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.