summaryrefslogtreecommitdiff
path: root/tests/xmlrpc-server-test.c
blob: 80f04ea89c3ad2bcab56e82b9ca8e35ba29f8f2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright 2008 Red Hat, Inc.
 * Copyright 2015, Collabora ltd.
 */

#include "test-utils.h"

static char *uri;

static GVariant *
parse_params (SoupMessage *msg, SoupXMLRPCParams *params, const char *signature)
{
	GVariant *args;
	GError *error = NULL;

	args = soup_xmlrpc_params_parse (params, signature, &error);
	if (!args) {
		soup_xmlrpc_message_set_fault (msg,
					       SOUP_XMLRPC_FAULT_SERVER_ERROR_INVALID_METHOD_PARAMETERS,
					       "Wrong method signature: expected %s: %s",
					       signature, error->message);
	}

	return args;
}

static void
do_sum (SoupMessage *msg, SoupXMLRPCParams *params)
{
	GVariant *args;
	GVariant *child;
	GVariantIter iter;
	double sum = 0.0, val;

	if (!(args = parse_params (msg, params, "(ad)")))
		return;

	child = g_variant_get_child_value (args, 0);

	g_variant_iter_init (&iter, child);
	while (g_variant_iter_loop (&iter, "d", &val))
		sum += val;

	soup_xmlrpc_message_set_response (msg, g_variant_new_double (sum), NULL);

	g_variant_unref (args);
	g_variant_unref (child);
}

static void
do_countBools (SoupMessage *msg, SoupXMLRPCParams *params)
{
	GVariant *args;
	GVariant *child;
	GVariantIter iter;
	gboolean val;
	int trues = 0, falses = 0;
	GVariantDict dict;

	if (!(args = parse_params (msg, params, "(ab)")))
		return;

	child = g_variant_get_child_value (args, 0);

	g_variant_iter_init (&iter, child);
	while (g_variant_iter_loop (&iter, "b", &val)) {
		if (val)
			trues++;
		else
			falses++;
	}

	g_variant_dict_init (&dict, NULL);
	g_variant_dict_insert (&dict, "true", "i", trues);
	g_variant_dict_insert (&dict, "false", "i", falses);

	soup_xmlrpc_message_set_response (msg, g_variant_dict_end (&dict), NULL);

	g_variant_unref (args);
	g_variant_unref (child);
}

static void
do_md5sum (SoupMessage *msg, SoupXMLRPCParams *params)
{
	GVariant *args;
	GVariant *child;
	GChecksum *checksum;
	GByteArray *digest;
	gsize digest_len = 16;

	if (!(args = parse_params (msg, params, "(ay)")))
		return;

	child = g_variant_get_child_value (args, 0);

	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum,
			   g_variant_get_data (child),
			   g_variant_get_size (child));
	digest = g_byte_array_new ();
	g_byte_array_set_size (digest, digest_len);
	g_checksum_get_digest (checksum, digest->data, &digest_len);
	g_checksum_free (checksum);

	soup_xmlrpc_message_set_response (msg,
					  g_variant_new_from_data (G_VARIANT_TYPE_BYTESTRING,
								   digest->data, digest_len,
								   TRUE, NULL, NULL),
					  NULL);
	g_byte_array_free (digest, TRUE);
	g_variant_unref (child);
	g_variant_unref (args);
}


static void
do_dateChange (SoupMessage *msg, SoupXMLRPCParams *params)
{
	GVariant *args;
	GVariant *timestamp;
	SoupDate *date;
	GVariant *arg;
	int val;
	GError *error = NULL;

	if (!(args = parse_params (msg, params, "(va{si})")))
		return;

	g_variant_get (args, "(v@a{si})", &timestamp, &arg);

	date = soup_xmlrpc_variant_get_datetime (timestamp, &error);
	if (!date) {
		soup_xmlrpc_message_set_fault (msg,
					       SOUP_XMLRPC_FAULT_SERVER_ERROR_INVALID_METHOD_PARAMETERS,
					       "%s", error->message);
		g_clear_error (&error);
		goto fail;
	}

	if (g_variant_lookup (arg, "tm_year", "i", &val))
		date->year = val + 1900;
	if (g_variant_lookup (arg, "tm_mon", "i", &val))
		date->month = val + 1;
	if (g_variant_lookup (arg, "tm_mday", "i", &val))
		date->day = val;
	if (g_variant_lookup (arg, "tm_hour", "i", &val))
		date->hour = val;
	if (g_variant_lookup (arg, "tm_min", "i", &val))
		date->minute = val;
	if (g_variant_lookup (arg, "tm_sec", "i", &val))
		date->second = val;

	soup_xmlrpc_message_set_response (msg,
					  soup_xmlrpc_variant_new_datetime (date),
					  NULL);

	soup_date_free (date);

fail:
	g_variant_unref (args);
	g_variant_unref (arg);
	g_variant_unref (timestamp);
}

static void
do_echo (SoupMessage *msg, SoupXMLRPCParams *params)
{
	GVariant *args;
	GVariant *child;

	if (!(args = parse_params (msg, params, "(as)")))
		return;

	child = g_variant_get_child_value (args, 0);
	soup_xmlrpc_message_set_response (msg, child, NULL);
	g_variant_unref (args);
	g_variant_unref (child);
}

static void
do_ping (SoupMessage *msg, SoupXMLRPCParams *params)
{
	GVariant *args;

	if (!(args = parse_params (msg, params, "()")))
		return;

	soup_xmlrpc_message_set_response (msg, g_variant_new_string ("pong"), NULL);
	g_variant_unref (args);
}

static void
server_callback (SoupServer *server, SoupMessage *msg,
		 const char *path, GHashTable *query,
		 SoupClientContext *context, gpointer data)
{
	char *method_name;
	SoupXMLRPCParams *params;
	GError *error = NULL;

	if (msg->method != SOUP_METHOD_POST) {
		soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
		return;
	}

	soup_message_set_status (msg, SOUP_STATUS_OK);

	method_name = soup_xmlrpc_parse_request (msg->request_body->data,
						 msg->request_body->length,
						 &params, &error);
	if (!method_name) {
		soup_xmlrpc_message_set_fault (msg, SOUP_XMLRPC_FAULT_PARSE_ERROR_NOT_WELL_FORMED,
				       "Could not parse method call: %s", error->message);
		g_clear_error (&error);
		return;
	}

	if (!strcmp (method_name, "sum"))
		do_sum (msg, params);
	else if (!strcmp (method_name, "countBools"))
		do_countBools (msg, params);
	else if (!strcmp (method_name, "md5sum"))
		do_md5sum (msg, params);
	else if (!strcmp (method_name, "dateChange"))
		do_dateChange (msg, params);
	else if (!strcmp (method_name, "echo"))
		do_echo (msg, params);
	else if (!strcmp (method_name, "ping"))
		do_ping (msg, params);
	else {
		soup_xmlrpc_message_set_fault (msg, SOUP_XMLRPC_FAULT_SERVER_ERROR_REQUESTED_METHOD_NOT_FOUND,
				       "Unknown method %s", method_name);
	}

	g_free (method_name);
	soup_xmlrpc_params_free (params);
}

static gboolean
run_xmlrpc_test (char **argv,
		 char **stdout_out,
		 char **stderr_out,
		 GError **error)
{
	gboolean ok;
	int status;

	argv[0] = g_test_build_filename (G_TEST_BUILT, "xmlrpc-test", NULL);
	ok = g_spawn_sync (NULL, argv, NULL, 0, NULL, NULL,
			   stdout_out, stderr_out, &status,
			   error);
	g_free (argv[0]);

	if (!ok)
		return FALSE;

	return g_spawn_check_exit_status (status, error);
}

static void
do_one_xmlrpc_test (gconstpointer data)
{
	const char *path = data;
	char *argv[12];
	char *stdout_out, *stderr_out;
	GError *error = NULL;
	int arg;

	argv[0] = NULL;
	argv[1] = "-S";
	argv[2] = "-U";
	argv[3] = uri;
	argv[4] = "-q";
	argv[5] = "-p";
	argv[6] = (char *) path;

	for (arg = 0; arg < debug_level && arg < 3; arg++)
		argv[arg + 7] = "-d";
	argv[arg + 7] = NULL;

	run_xmlrpc_test (argv, &stdout_out, &stderr_out, &error);
	if (stdout_out) {
		g_print ("%s", stdout_out);
		g_free (stdout_out);
	}
	if (stderr_out) {
		g_printerr ("%s", stderr_out);
		g_free (stderr_out);
	}

	if (   g_error_matches (error, G_SPAWN_EXIT_ERROR, 1)
	    || g_error_matches (error, G_SPAWN_EXIT_ERROR, 77))
		g_test_fail ();
	else
		g_assert_no_error (error);
	g_clear_error (&error);
}

gboolean run_tests = TRUE;

static GOptionEntry no_test_entry[] = {
        { "no-tests", 'n', G_OPTION_FLAG_REVERSE,
          G_OPTION_ARG_NONE, &run_tests,
          "Don't run tests, just run the test server", NULL },
        { NULL }
};

int
main (int argc, char **argv)
{
	SoupServer *server;
	SoupURI *server_uri;
	int ret;

	test_init (argc, argv, no_test_entry);

	server = soup_test_server_new (run_tests ? SOUP_TEST_SERVER_IN_THREAD : SOUP_TEST_SERVER_DEFAULT);
	soup_server_add_handler (server, "/xmlrpc-server.php",
				 server_callback, NULL, NULL);
	server_uri = soup_test_server_get_uri (server, "http", NULL);
	soup_uri_set_path (server_uri, "/xmlrpc-server.php");
	uri = soup_uri_to_string (server_uri, FALSE);

	if (run_tests) {
		char *out, **tests, *path;
		char *list_argv[4];
		GError *error = NULL;
		int i;

		list_argv[0] = NULL;
		list_argv[1] = "-S";
		list_argv[2] = "-l";
		list_argv[3] = NULL;

		if (!run_xmlrpc_test (list_argv, &out, NULL, &error)) {
			g_printerr ("'xmlrpc-test -l' failed: %s\n", error->message);
			g_error_free (error);
			return 1;
		}

		tests = g_strsplit (out, "\n", -1);
		g_free (out);

		for (i = 0; tests[i] && *tests[i]; i++) {
			g_assert_true (g_str_has_prefix (tests[i], "/xmlrpc/"));
			path = g_strdup_printf ("/xmlrpc-server/%s", tests[i] + strlen ("/xmlrpc/"));
			g_test_add_data_func (path, tests[i], do_one_xmlrpc_test);
			g_free (path);
		}

		ret = g_test_run ();

		g_strfreev (tests);
	} else {
		GMainLoop *loop;

		g_print ("Listening on port %d\n", server_uri->port);

		loop = g_main_loop_new (NULL, TRUE);
		g_main_loop_run (loop);
		g_main_loop_unref (loop);

		ret = 0;
	}

	soup_test_server_quit_unref (server);
	soup_uri_free (server_uri);
	g_free (uri);
	if (run_tests)
		test_cleanup ();
	return ret;
}