5 #include "test-utils.h"
6 #include "libsoup/soup.h"
16 static gboolean apache_running;
19 static SoupLogger *logger;
21 int debug_level, errors;
22 gboolean expect_warning, tls_available;
23 static int http_debug_level;
26 increment_debug_level (const char *option_name, const char *value,
27 gpointer data, GError **error)
34 increment_http_debug_level (const char *option_name, const char *value,
35 gpointer data, GError **error)
41 static GOptionEntry debug_entry[] = {
42 { "debug", 'd', G_OPTION_FLAG_NO_ARG,
43 G_OPTION_ARG_CALLBACK, increment_debug_level,
44 "Enable (or increase) test-specific debugging", NULL },
45 { "http-debug", 'h', G_OPTION_FLAG_NO_ARG,
46 G_OPTION_ARG_CALLBACK, increment_http_debug_level,
47 "Enable (or increase) HTTP-level debugging", NULL },
63 test_log_handler (const char *log_domain, GLogLevelFlags log_level,
64 const char *message, gpointer user_data)
66 if (log_level & (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL)) {
68 expect_warning = FALSE;
69 debug_printf (2, "Got expected warning: %s\n", message);
74 g_log_default_handler (log_domain, log_level, message, user_data);
78 test_init (int argc, char **argv, GOptionEntry *entries)
83 GTlsBackend *tls_backend;
85 setlocale (LC_ALL, "");
88 name = strrchr (argv[0], '/');
91 if (!strncmp (name, "lt-", 3))
95 opts = g_option_context_new (NULL);
96 g_option_context_add_main_entries (opts, debug_entry, NULL);
98 g_option_context_add_main_entries (opts, entries, NULL);
100 if (!g_option_context_parse (opts, &argc, &argv, &error)) {
101 fprintf (stderr, "Could not parse arguments: %s\n",
103 fprintf (stderr, "%s",
104 g_option_context_get_help (opts, TRUE, NULL));
107 g_option_context_free (opts);
109 /* Exit cleanly on ^C in case we're valgrinding. */
110 signal (SIGINT, quit);
112 g_log_set_default_handler (test_log_handler, NULL);
114 tls_backend = g_tls_backend_get_default ();
115 tls_available = g_tls_backend_supports_tls (tls_backend);
127 g_object_unref (logger);
129 g_main_context_unref (g_main_context_default ());
131 debug_printf (1, "\n");
133 printf ("%s: %d error(s).%s\n",
134 g_get_prgname (), errors,
135 debug_level == 0 ? " Run with '-d' for details" : "");
137 printf ("%s: OK\n", g_get_prgname ());
141 debug_printf (int level, const char *format, ...)
145 if (debug_level < level)
148 va_start (args, format);
149 vprintf (format, args);
156 apache_cmd (const char *cmd)
163 cwd = g_get_current_dir ();
164 conf = g_build_filename (cwd, "httpd.conf", NULL);
166 argv[0] = APACHE_HTTPD;
175 ok = g_spawn_sync (cwd, (char **)argv, NULL, 0, NULL, NULL,
176 NULL, NULL, &status, NULL);
189 if (!apache_cmd ("start")) {
190 fprintf (stderr, "Could not start apache\n");
193 apache_running = TRUE;
197 apache_cleanup (void)
202 if (g_file_get_contents ("httpd.pid", &contents, NULL, NULL)) {
203 pid = strtoul (contents, NULL, 10);
208 if (!apache_cmd ("graceful-stop"))
210 apache_running = FALSE;
213 while (kill (pid, 0) == 0)
218 #endif /* HAVE_APACHE */
221 soup_test_session_new (GType type, ...)
224 const char *propname;
225 SoupSession *session;
227 va_start (args, type);
228 propname = va_arg (args, const char *);
229 session = (SoupSession *)g_object_new_valist (type, propname, args);
232 g_object_set (G_OBJECT (session),
233 SOUP_SESSION_SSL_CA_FILE, SRCDIR "/test-cert.pem",
236 if (http_debug_level && !logger) {
237 SoupLoggerLogLevel level = MIN ((SoupLoggerLogLevel)http_debug_level, SOUP_LOGGER_LOG_BODY);
239 logger = soup_logger_new (level, -1);
243 soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
249 soup_test_session_abort_unref (SoupSession *session)
251 g_object_add_weak_pointer (G_OBJECT (session), (gpointer *)&session);
253 soup_session_abort (session);
254 g_object_unref (session);
258 debug_printf (1, "leaked SoupSession!\n");
259 g_object_remove_weak_pointer (G_OBJECT (session), (gpointer *)&session);
263 static gpointer run_server_thread (gpointer user_data);
266 test_server_new (gboolean in_own_thread, gboolean ssl)
269 GMainContext *async_context;
270 const char *ssl_cert_file, *ssl_key_file;
273 async_context = in_own_thread ? g_main_context_new () : NULL;
276 ssl_cert_file = SRCDIR "/test-cert.pem";
277 ssl_key_file = SRCDIR "/test-key.pem";
279 ssl_cert_file = ssl_key_file = NULL;
281 addr = soup_address_new ("127.0.0.1", SOUP_ADDRESS_ANY_PORT);
282 soup_address_resolve_sync (addr, NULL);
284 server = soup_server_new (SOUP_SERVER_INTERFACE, addr,
285 SOUP_SERVER_ASYNC_CONTEXT, async_context,
286 SOUP_SERVER_SSL_CERT_FILE, ssl_cert_file,
287 SOUP_SERVER_SSL_KEY_FILE, ssl_key_file,
289 g_object_unref (addr);
291 g_main_context_unref (async_context);
294 fprintf (stderr, "Unable to create server\n");
301 thread = g_thread_new ("server_thread", run_server_thread, server);
302 g_object_set_data (G_OBJECT (server), "thread", thread);
304 soup_server_run_async (server);
310 soup_test_server_new (gboolean in_own_thread)
312 return test_server_new (in_own_thread, FALSE);
316 soup_test_server_new_ssl (gboolean in_own_thread)
318 return test_server_new (in_own_thread, TRUE);
322 run_server_thread (gpointer user_data)
324 SoupServer *server = user_data;
326 soup_server_run (server);
331 idle_quit_server (gpointer server)
333 soup_server_quit (server);
338 soup_test_server_quit_unref (SoupServer *server)
342 g_object_add_weak_pointer (G_OBJECT (server),
343 (gpointer *)&server);
345 thread = g_object_get_data (G_OBJECT (server), "thread");
347 soup_add_completion (soup_server_get_async_context (server),
348 idle_quit_server, server);
349 g_thread_join (thread);
351 soup_server_quit (server);
352 g_object_unref (server);
356 debug_printf (1, "leaked SoupServer!\n");
357 g_object_remove_weak_pointer (G_OBJECT (server),
358 (gpointer *)&server);