2
* xmlrpctest.c: simple client for XML-RPC tests
4
* Copyright (C) 2005, 2008 Red Hat, Inc.
6
* See COPYING.LIB for the License of this software
8
* Karel Zak <kzak@redhat.com>
10
* $Id: xmlrpctest.c,v 1.14 2008/05/23 08:24:44 rjones Exp $
20
#include <libxml/parser.h>
21
#include <libxml/tree.h>
22
#include <libxml/xpath.h>
28
#include "testutils.h"
31
#define NLOOPS 100 /* default number of loops per test */
33
static char *progname;
37
testMethodPlusINT(const void *data)
40
xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
42
if (xmlRpcCall(cxt, "plus", "i", "ii",
43
(const char *) &retval, 10, 10) < 0)
46
return retval==(10+10) ? 0 : -1;
50
testMethodPlusDOUBLE(const void *data)
53
xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
55
if (xmlRpcCall(cxt, "plus", "f", "ff",
56
(const char *) &retval, 10.1234, 10.1234) < 0)
59
return retval==(10.1234+10.1234) ? 0 : -1;
63
marshalRequest(virBufferPtr buf, const char *fmt, ...)
70
argv = xmlRpcArgvNew(fmt, ap, &argc);
73
xmlRpcMarshalRequest("test", buf, argc, argv);
75
xmlRpcArgvFree(argc, argv);
79
checkRequestValue(const char *xmlstr, const char *xpath, int type, void *expected)
82
xmlXPathContextPtr ctxt = NULL;
83
xmlXPathObjectPtr obj = NULL;
86
xml = xmlReadDoc((const xmlChar *) xmlstr, "xmlrpctest.xml", NULL,
87
XML_PARSE_NOENT | XML_PARSE_NONET |
88
XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
92
if (!(ctxt = xmlXPathNewContext(xml)))
95
if (!(obj = xmlXPathEval(BAD_CAST xpath, ctxt)))
100
if ((obj->type != XPATH_NUMBER) ||
101
((int) obj->floatval != *((int *)expected)))
105
if ((obj->type != XPATH_NUMBER) ||
106
((double) obj->floatval != *((double *)expected)))
110
if ((obj->type != XPATH_STRING) ||
111
(STRNEQ((const char *)obj->stringval, (const char *)expected)))
120
xmlXPathFreeObject(obj);
121
xmlXPathFreeContext(ctxt);
128
testMarshalRequestINT(const void *data)
132
int check = data ? *((int *)data) : 0;
133
virBuffer buf = VIR_BUFFER_INITIALIZER;
134
marshalRequest(&buf, "i", num);
137
if (virBufferError(&buf))
140
content = virBufferContentAndReset(&buf);
143
ret = checkRequestValue(content,
144
"number(/methodCall/params/param[1]/value/int)",
145
XML_RPC_INTEGER, (void *) &num);
152
testMarshalRequestSTRING(const void *data ATTRIBUTE_UNUSED)
154
const char *str = "This library will be really sexy.";
156
int check = data ? *((int *)data) : 0;
157
virBuffer buf = VIR_BUFFER_INITIALIZER;
160
marshalRequest(&buf, "s", str);
162
if (virBufferError(&buf))
165
content = virBufferContentAndReset(&buf);
167
ret = checkRequestValue(content,
168
"string(/methodCall/params/param[1]/value/string)",
169
XML_RPC_STRING, (void *) str);
176
testMarshalRequestDOUBLE(const void *data)
178
double num = 123456789.123;
180
int check = data ? *((int *)data) : 0;
181
virBuffer buf = VIR_BUFFER_INITIALIZER;
184
marshalRequest(&buf, "f", num);
186
if (virBufferError(&buf))
189
content = virBufferContentAndReset(&buf);
191
ret = checkRequestValue(content,
192
"number(/methodCall/params/param[1]/value/double)",
193
XML_RPC_DOUBLE, (void *) &num);
201
main(int argc, char **argv)
203
xmlRpcContextPtr cxt = NULL;
206
const char *url = "http://localhost:8000";
212
fprintf(stderr, "Usage: %s [url]\n", progname);
219
* client-server tests
221
if (!(cxt = xmlRpcContextNew(url)))
223
fprintf(stderr, "%s: failed create new RPC context\n", progname);
227
if (virtTestRun("XML-RPC methodCall INT+INT",
228
NLOOPS, testMethodPlusINT, (const void *) cxt) != 0)
231
if (virtTestRun("XML-RPC methodCall DOUBLE+DOUBLE",
232
NLOOPS, testMethodPlusDOUBLE, (const void *) cxt) != 0)
235
xmlRpcContextFree(cxt);
238
* regression / performance tests
240
if (virtTestRun("XML-RPC request marshalling: INT (check)",
241
1, testMarshalRequestINT, (const void *) &check) != 0)
243
if (virtTestRun("XML-RPC request marshalling: INT",
244
NLOOPS, testMarshalRequestINT, NULL) != 0)
247
if (virtTestRun("XML-RPC request marshalling: DOUBLE (check)",
248
1, testMarshalRequestDOUBLE, (const void *) &check) != 0)
250
if (virtTestRun("XML-RPC request marshalling: DOUBLE",
251
NLOOPS, testMarshalRequestDOUBLE, NULL) != 0)
254
if (virtTestRun("XML-RPC request marshalling: STRING (check)",
255
1, testMarshalRequestSTRING, (void *) &check) != 0)
257
if (virtTestRun("XML-RPC request marshalling: STRING",
258
NLOOPS, testMarshalRequestSTRING, NULL) != 0)
261
exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
266
* vim: set tabstop=4:
267
* vim: set shiftwidth=4:
268
* vim: set expandtab: