xmlrpctest.c 6.7 KB
Newer Older
K
Karel Zak 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * xmlrpctest.c: simple client for XML-RPC tests
 *
 * Copyright (C) 2005 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Karel Zak <kzak@redhat.com>
 *
 * $Id$
 */

#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>

22
#include "libvirt/libvirt.h"
23
#include "buf.h"
K
Karel Zak 已提交
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
#include "xmlrpc.h"

#include "testutils.h"


#define NLOOPS  100     /* default number of loops per test */

static char *progname;


static int
testMethodPlusINT(void *data)
{
    int retval = 0;
    xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
    
    if (xmlRpcCall(cxt, "plus", "i", "ii", 
            (const char *) &retval, 10, 10) < 0)
        return -1;
        
    return retval==(10+10) ? 0 : -1;   
}

static int
testMethodPlusDOUBLE(void *data)
{
    double retval = 0;
    xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
    
    if (xmlRpcCall(cxt, "plus", "f", "ff", 
            (const char *) &retval, 10.1234, 10.1234) < 0)
        return -1;
        
    return retval==(10.1234+10.1234) ? 0 : -1;   
}

static virBufferPtr
marshalRequest(const char *fmt, ...)
{
    int argc;
    xmlRpcValuePtr *argv;
    virBufferPtr buf;
    va_list ap;
    
    va_start(ap, fmt);
    argv = xmlRpcArgvNew(fmt, ap, &argc);
    va_end(ap);
    
    buf = xmlRpcMarshalRequest("test", argc, argv);

    xmlRpcArgvFree(argc, argv);
    return buf;
}

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;
    
    xml = xmlReadDoc((const xmlChar *) xmlstr, "xmlrpctest.xml", NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
    if (!xml)
        goto error;
    
    if (!(ctxt = xmlXPathNewContext(xml)))
        goto error;
    
    if (!(obj = xmlXPathEval(BAD_CAST xpath, ctxt)))
        goto error;

    switch(type) {
        case XML_RPC_INTEGER:
            if ((obj->type != XPATH_NUMBER) || 
                    ((int) obj->floatval != *((int *)expected)))
                goto error;
            break;
         case XML_RPC_DOUBLE:
            if ((obj->type != XPATH_NUMBER) || 
                    ((double) obj->floatval != *((double *)expected)))
                goto error;
            break;
         case XML_RPC_STRING:
            if ((obj->type != XPATH_STRING) || 
                    (strcmp((const char *)obj->stringval, (const char *)expected)))
                goto error;
            break;
        default:
            goto error;
    }
    ret = 0;

error:    
    if (obj)
        xmlXPathFreeObject(obj);
    if (ctxt)
        xmlXPathFreeContext(ctxt);
    if (xml)
        xmlFreeDoc(xml);
    return ret;
}

static int
testMarshalRequestINT(void *data)
{
    int num = INT_MAX;
    int ret = 0;
    int check = data ? *((int *)data) : 0;
    virBufferPtr buf = marshalRequest("i", num);

    if (check)
        ret = checkRequestValue(buf->content, 
                "number(/methodCall/params/param[1]/value/int)",
                XML_RPC_INTEGER, (void *) &num);
    
    virBufferFree(buf);
    return ret;
}

static int
testMarshalRequestSTRING(void *data ATTRIBUTE_UNUSED)
{
    const char *str = "This library will be really sexy.";
    int ret = 0;
    int check = data ? *((int *)data) : 0;
    virBufferPtr buf = marshalRequest("s", str);

K
Karel Zak 已提交
154
    if (check) 
K
Karel Zak 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        ret = checkRequestValue(buf->content, 
                "string(/methodCall/params/param[1]/value/string)",
                XML_RPC_STRING, (void *) str);
    virBufferFree(buf);
    return ret;
}

static int
testMarshalRequestDOUBLE(void *data)
{
    double num = 123456789.123;
    int ret = 0;
    int check = data ? *((int *)data) : 0;
    virBufferPtr buf = marshalRequest("f", num);

    if (check)
        ret = checkRequestValue(buf->content, 
                "number(/methodCall/params/param[1]/value/double)",
                XML_RPC_DOUBLE, (void *) &num);
    
    virBufferFree(buf);
    return ret;
}

K
Karel Zak 已提交
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
static int
testBufferStrcat(void *data ATTRIBUTE_UNUSED)
{
    virBufferPtr buf = virBufferNew(1000*32);  /* don't waste time with realloc */
    int i;
    
    for (i=0; i < 1000; i++)
        virBufferStrcat(buf, "My name is ", "libvirt", ".\n", NULL);

    virBufferFree(buf);
    return 0;
}

static int
testBufferVSprintf(void *data ATTRIBUTE_UNUSED)
{
    virBufferPtr buf = virBufferNew(1000*32);  /* don't waste time with realloc */
    int i;
    
    for (i=0; i < 1000; i++)
        virBufferVSprintf(buf, "My name is %s.\n", "libvirt");

    virBufferFree(buf);
    return 0;
}

K
Karel Zak 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
int
main(int argc, char **argv)
{
	xmlRpcContextPtr cxt = NULL;
    int check = 1;
	int ret = 0;
    const char *url = "http://localhost:8000";

	progname = argv[0];

	if (argc > 2)
	{
		fprintf(stderr, "Usage: %s [url]\n", progname); 
		exit(EXIT_FAILURE);
	}
    if (argc == 2)
        url = argv[1];
K
Karel Zak 已提交
222 223 224 225
    
     /* 
      * client-server tests 
      */
K
Karel Zak 已提交
226 227 228 229 230 231
	if (!(cxt = xmlRpcContextNew(url)))
	{
		fprintf(stderr, "%s: failed create new RPC context\n", progname);
		exit(EXIT_FAILURE);
	}

K
Karel Zak 已提交
232
       if (virtTestRun("XML-RPC methodCall INT+INT", 
K
Karel Zak 已提交
233 234 235 236 237 238 239
                NLOOPS, testMethodPlusINT, (void *) cxt) != 0)
        ret = -1;
    
    if (virtTestRun("XML-RPC methodCall DOUBLE+DOUBLE", 
                NLOOPS, testMethodPlusDOUBLE, (void *) cxt) != 0)
        ret = -1;
    
K
Karel Zak 已提交
240 241 242 243 244
 	xmlRpcContextFree(cxt);
   
    /* 
     * regression / performance tests 
     */
K
Karel Zak 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    if (virtTestRun("XML-RPC request marshalling: INT (check)", 
                1, testMarshalRequestINT, (void *) &check) != 0)
        ret = -1;
    if (virtTestRun("XML-RPC request marshalling: INT", 
                NLOOPS, testMarshalRequestINT, NULL) != 0)
        ret = -1;
    
    if (virtTestRun("XML-RPC request marshalling: DOUBLE (check)", 
                1, testMarshalRequestDOUBLE, (void *) &check) != 0)
        ret = -1;
    if (virtTestRun("XML-RPC request marshalling: DOUBLE", 
                NLOOPS, testMarshalRequestDOUBLE, NULL) != 0)
        ret = -1;

    if (virtTestRun("XML-RPC request marshalling: STRING (check)", 
                1, testMarshalRequestSTRING, (void *) &check) != 0)
        ret = -1;
    if (virtTestRun("XML-RPC request marshalling: STRING", 
                NLOOPS, testMarshalRequestSTRING, NULL) != 0)
        ret = -1;
K
Karel Zak 已提交
265 266 267 268 269 270

    if (virtTestRun("Buffer: strcat", NLOOPS, testBufferStrcat, NULL) != 0)
        ret = -1;
    if (virtTestRun("Buffer: sprintf", NLOOPS, testBufferVSprintf, NULL) != 0)
        ret = -1;
  
K
Karel Zak 已提交
271 272 273 274 275 276 277 278 279 280

	exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}


/*
 * vim: set tabstop=4:
 * vim: set shiftwidth=4:
 * vim: set expandtab:
 */