libvirt/tests/xmlrpctest.c

270 lines
6.4 KiB
C
Raw Normal View History

2006-05-09 15:35:46 +00:00
/*
* xmlrpctest.c: simple client for XML-RPC tests
*
* Copyright (C) 2005, 2008 Red Hat, Inc.
2006-05-09 15:35:46 +00:00
*
* See COPYING.LIB for the License of this software
*
* Karel Zak <kzak@redhat.com>
*
* $Id$
*/
#include <config.h>
2006-05-09 15:35:46 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include "internal.h"
#include "buf.h"
2006-05-09 15:35:46 +00:00
#include "xmlrpc.h"
#include "testutils.h"
#define NLOOPS 100 /* default number of loops per test */
static char *progname;
static int
2007-07-18 21:13:26 +00:00
testMethodPlusINT(const void *data)
2006-05-09 15:35:46 +00:00
{
int retval = 0;
xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
if (xmlRpcCall(cxt, "plus", "i", "ii",
2006-05-09 15:35:46 +00:00
(const char *) &retval, 10, 10) < 0)
return -1;
return retval==(10+10) ? 0 : -1;
2006-05-09 15:35:46 +00:00
}
static int
2007-07-18 21:13:26 +00:00
testMethodPlusDOUBLE(const void *data)
2006-05-09 15:35:46 +00:00
{
double retval = 0;
xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
if (xmlRpcCall(cxt, "plus", "f", "ff",
2006-05-09 15:35:46 +00:00
(const char *) &retval, 10.1234, 10.1234) < 0)
return -1;
return retval==(10.1234+10.1234) ? 0 : -1;
2006-05-09 15:35:46 +00:00
}
static void
marshalRequest(virBufferPtr buf, const char *fmt, ...)
2006-05-09 15:35:46 +00:00
{
int argc;
xmlRpcValuePtr *argv;
va_list ap;
2006-05-09 15:35:46 +00:00
va_start(ap, fmt);
argv = xmlRpcArgvNew(fmt, ap, &argc);
va_end(ap);
xmlRpcMarshalRequest("test", buf, argc, argv);
2006-05-09 15:35:46 +00:00
xmlRpcArgvFree(argc, argv);
}
static int
checkRequestValue(const char *xmlstr, const char *xpath, int type, void *expected)
{
xmlDocPtr xml = NULL;
xmlXPathContextPtr ctxt = NULL;
xmlXPathObjectPtr obj = NULL;
int ret = -1;
2006-05-09 15:35:46 +00:00
xml = xmlReadDoc((const xmlChar *) xmlstr, "xmlrpctest.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
if (!xml)
goto error;
2006-05-09 15:35:46 +00:00
if (!(ctxt = xmlXPathNewContext(xml)))
goto error;
2006-05-09 15:35:46 +00:00
if (!(obj = xmlXPathEval(BAD_CAST xpath, ctxt)))
goto error;
switch(type) {
case XML_RPC_INTEGER:
if ((obj->type != XPATH_NUMBER) ||
2006-05-09 15:35:46 +00:00
((int) obj->floatval != *((int *)expected)))
goto error;
break;
case XML_RPC_DOUBLE:
if ((obj->type != XPATH_NUMBER) ||
2006-05-09 15:35:46 +00:00
((double) obj->floatval != *((double *)expected)))
goto error;
break;
case XML_RPC_STRING:
if ((obj->type != XPATH_STRING) ||
(STRNEQ((const char *)obj->stringval, (const char *)expected)))
2006-05-09 15:35:46 +00:00
goto error;
break;
default:
goto error;
}
ret = 0;
error:
xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt);
2006-05-09 15:35:46 +00:00
if (xml)
xmlFreeDoc(xml);
return ret;
}
static int
2007-07-18 21:13:26 +00:00
testMarshalRequestINT(const void *data)
2006-05-09 15:35:46 +00:00
{
int num = INT_MAX;
int ret = 0;
int check = data ? *((int *)data) : 0;
virBuffer buf = VIR_BUFFER_INITIALIZER;
marshalRequest(&buf, "i", num);
char *content;
if (virBufferError(&buf))
return -1;
content = virBufferContentAndReset(&buf);
2006-05-09 15:35:46 +00:00
if (check)
ret = checkRequestValue(content,
2006-05-09 15:35:46 +00:00
"number(/methodCall/params/param[1]/value/int)",
XML_RPC_INTEGER, (void *) &num);
free(content);
2006-05-09 15:35:46 +00:00
return ret;
}
static int
2007-07-18 21:13:26 +00:00
testMarshalRequestSTRING(const void *data ATTRIBUTE_UNUSED)
2006-05-09 15:35:46 +00:00
{
const char *str = "This library will be really sexy.";
int ret = 0;
int check = data ? *((int *)data) : 0;
virBuffer buf = VIR_BUFFER_INITIALIZER;
char *content;
marshalRequest(&buf, "s", str);
2006-05-09 15:35:46 +00:00
if (virBufferError(&buf))
return -1;
content = virBufferContentAndReset(&buf);
if (check)
ret = checkRequestValue(content,
2006-05-09 15:35:46 +00:00
"string(/methodCall/params/param[1]/value/string)",
XML_RPC_STRING, (void *) str);
free(content);
2006-05-09 15:35:46 +00:00
return ret;
}
static int
2007-07-18 21:13:26 +00:00
testMarshalRequestDOUBLE(const void *data)
2006-05-09 15:35:46 +00:00
{
double num = 123456789.123;
int ret = 0;
int check = data ? *((int *)data) : 0;
virBuffer buf = VIR_BUFFER_INITIALIZER;
char *content;
2006-05-09 15:35:46 +00:00
marshalRequest(&buf, "f", num);
if (virBufferError(&buf))
return -1;
content = virBufferContentAndReset(&buf);
2006-05-09 15:35:46 +00:00
if (check)
ret = checkRequestValue(content,
2006-05-09 15:35:46 +00:00
"number(/methodCall/params/param[1]/value/double)",
XML_RPC_DOUBLE, (void *) &num);
free(content);
2006-05-09 15:35:46 +00:00
return ret;
}
2006-05-10 12:15:49 +00:00
2006-05-09 15:35:46 +00:00
int
main(int argc, char **argv)
{
xmlRpcContextPtr cxt = NULL;
2006-05-09 15:35:46 +00:00
int check = 1;
int ret = 0;
2006-05-09 15:35:46 +00:00
const char *url = "http://localhost:8000";
progname = argv[0];
2006-05-09 15:35:46 +00:00
if (argc > 2)
{
fprintf(stderr, "Usage: %s [url]\n", progname);
exit(EXIT_FAILURE);
}
2006-05-09 15:35:46 +00:00
if (argc == 2)
url = argv[1];
/*
* client-server tests
2006-05-10 12:15:49 +00:00
*/
if (!(cxt = xmlRpcContextNew(url)))
{
fprintf(stderr, "%s: failed create new RPC context\n", progname);
exit(EXIT_FAILURE);
}
2006-05-09 15:35:46 +00:00
if (virtTestRun("XML-RPC methodCall INT+INT",
2007-07-18 21:13:26 +00:00
NLOOPS, testMethodPlusINT, (const void *) cxt) != 0)
2006-05-09 15:35:46 +00:00
ret = -1;
if (virtTestRun("XML-RPC methodCall DOUBLE+DOUBLE",
2007-07-18 21:13:26 +00:00
NLOOPS, testMethodPlusDOUBLE, (const void *) cxt) != 0)
2006-05-09 15:35:46 +00:00
ret = -1;
xmlRpcContextFree(cxt);
/*
* regression / performance tests
2006-05-10 12:15:49 +00:00
*/
if (virtTestRun("XML-RPC request marshalling: INT (check)",
2007-07-18 21:13:26 +00:00
1, testMarshalRequestINT, (const void *) &check) != 0)
2006-05-09 15:35:46 +00:00
ret = -1;
if (virtTestRun("XML-RPC request marshalling: INT",
2006-05-09 15:35:46 +00:00
NLOOPS, testMarshalRequestINT, NULL) != 0)
ret = -1;
if (virtTestRun("XML-RPC request marshalling: DOUBLE (check)",
2007-07-18 21:13:26 +00:00
1, testMarshalRequestDOUBLE, (const void *) &check) != 0)
2006-05-09 15:35:46 +00:00
ret = -1;
if (virtTestRun("XML-RPC request marshalling: DOUBLE",
2006-05-09 15:35:46 +00:00
NLOOPS, testMarshalRequestDOUBLE, NULL) != 0)
ret = -1;
if (virtTestRun("XML-RPC request marshalling: STRING (check)",
2006-05-09 15:35:46 +00:00
1, testMarshalRequestSTRING, (void *) &check) != 0)
ret = -1;
if (virtTestRun("XML-RPC request marshalling: STRING",
2006-05-09 15:35:46 +00:00
NLOOPS, testMarshalRequestSTRING, NULL) != 0)
ret = -1;
2006-05-10 12:15:49 +00:00
exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
2006-05-09 15:35:46 +00:00
}
/*
* vim: set tabstop=4:
* vim: set shiftwidth=4:
* vim: set expandtab:
*/