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

13
#include <config.h>
J
Jim Meyering 已提交
14

K
Karel Zak 已提交
15 16 17 18 19 20 21 22 23
#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>

24
#include "libvirt/libvirt.h"
25
#include "buf.h"
K
Karel Zak 已提交
26 27 28 29 30 31 32 33 34 35 36
#include "xmlrpc.h"

#include "testutils.h"


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

static char *progname;


static int
D
Daniel P. Berrange 已提交
37
testMethodPlusINT(const void *data)
K
Karel Zak 已提交
38 39 40
{
    int retval = 0;
    xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
41 42

    if (xmlRpcCall(cxt, "plus", "i", "ii",
K
Karel Zak 已提交
43 44
            (const char *) &retval, 10, 10) < 0)
        return -1;
45 46

    return retval==(10+10) ? 0 : -1;
K
Karel Zak 已提交
47 48 49
}

static int
D
Daniel P. Berrange 已提交
50
testMethodPlusDOUBLE(const void *data)
K
Karel Zak 已提交
51 52 53
{
    double retval = 0;
    xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
54 55

    if (xmlRpcCall(cxt, "plus", "f", "ff",
K
Karel Zak 已提交
56 57
            (const char *) &retval, 10.1234, 10.1234) < 0)
        return -1;
58 59

    return retval==(10.1234+10.1234) ? 0 : -1;
K
Karel Zak 已提交
60 61 62 63 64 65 66 67 68
}

static virBufferPtr
marshalRequest(const char *fmt, ...)
{
    int argc;
    xmlRpcValuePtr *argv;
    virBufferPtr buf;
    va_list ap;
69

K
Karel Zak 已提交
70 71 72
    va_start(ap, fmt);
    argv = xmlRpcArgvNew(fmt, ap, &argc);
    va_end(ap);
73

K
Karel Zak 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
    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;
87

K
Karel Zak 已提交
88 89 90 91 92
    xml = xmlReadDoc((const xmlChar *) xmlstr, "xmlrpctest.xml", NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
    if (!xml)
        goto error;
93

K
Karel Zak 已提交
94 95
    if (!(ctxt = xmlXPathNewContext(xml)))
        goto error;
96

K
Karel Zak 已提交
97 98 99 100 101
    if (!(obj = xmlXPathEval(BAD_CAST xpath, ctxt)))
        goto error;

    switch(type) {
        case XML_RPC_INTEGER:
102
            if ((obj->type != XPATH_NUMBER) ||
K
Karel Zak 已提交
103 104 105 106
                    ((int) obj->floatval != *((int *)expected)))
                goto error;
            break;
         case XML_RPC_DOUBLE:
107
            if ((obj->type != XPATH_NUMBER) ||
K
Karel Zak 已提交
108 109 110 111
                    ((double) obj->floatval != *((double *)expected)))
                goto error;
            break;
         case XML_RPC_STRING:
112
            if ((obj->type != XPATH_STRING) ||
K
Karel Zak 已提交
113 114 115 116 117 118 119 120
                    (strcmp((const char *)obj->stringval, (const char *)expected)))
                goto error;
            break;
        default:
            goto error;
    }
    ret = 0;

121
error:
122
    xmlXPathFreeObject(obj);
123
    xmlXPathFreeContext(ctxt);
K
Karel Zak 已提交
124 125 126 127 128 129
    if (xml)
        xmlFreeDoc(xml);
    return ret;
}

static int
D
Daniel P. Berrange 已提交
130
testMarshalRequestINT(const void *data)
K
Karel Zak 已提交
131 132 133 134 135 136 137
{
    int num = INT_MAX;
    int ret = 0;
    int check = data ? *((int *)data) : 0;
    virBufferPtr buf = marshalRequest("i", num);

    if (check)
138
        ret = checkRequestValue(buf->content,
K
Karel Zak 已提交
139 140
                "number(/methodCall/params/param[1]/value/int)",
                XML_RPC_INTEGER, (void *) &num);
141

K
Karel Zak 已提交
142 143 144 145 146
    virBufferFree(buf);
    return ret;
}

static int
D
Daniel P. Berrange 已提交
147
testMarshalRequestSTRING(const void *data ATTRIBUTE_UNUSED)
K
Karel Zak 已提交
148 149 150 151 152 153
{
    const char *str = "This library will be really sexy.";
    int ret = 0;
    int check = data ? *((int *)data) : 0;
    virBufferPtr buf = marshalRequest("s", str);

154 155
    if (check)
        ret = checkRequestValue(buf->content,
K
Karel Zak 已提交
156 157 158 159 160 161 162
                "string(/methodCall/params/param[1]/value/string)",
                XML_RPC_STRING, (void *) str);
    virBufferFree(buf);
    return ret;
}

static int
D
Daniel P. Berrange 已提交
163
testMarshalRequestDOUBLE(const void *data)
K
Karel Zak 已提交
164 165 166 167 168 169 170
{
    double num = 123456789.123;
    int ret = 0;
    int check = data ? *((int *)data) : 0;
    virBufferPtr buf = marshalRequest("f", num);

    if (check)
171
        ret = checkRequestValue(buf->content,
K
Karel Zak 已提交
172 173
                "number(/methodCall/params/param[1]/value/double)",
                XML_RPC_DOUBLE, (void *) &num);
174

K
Karel Zak 已提交
175 176 177 178
    virBufferFree(buf);
    return ret;
}

K
Karel Zak 已提交
179
static int
D
Daniel P. Berrange 已提交
180
testBufferStrcat(const void *data ATTRIBUTE_UNUSED)
K
Karel Zak 已提交
181 182 183
{
    virBufferPtr buf = virBufferNew(1000*32);  /* don't waste time with realloc */
    int i;
184

K
Karel Zak 已提交
185 186 187 188 189 190 191 192
    for (i=0; i < 1000; i++)
        virBufferStrcat(buf, "My name is ", "libvirt", ".\n", NULL);

    virBufferFree(buf);
    return 0;
}

static int
D
Daniel P. Berrange 已提交
193
testBufferVSprintf(const void *data ATTRIBUTE_UNUSED)
K
Karel Zak 已提交
194 195 196
{
    virBufferPtr buf = virBufferNew(1000*32);  /* don't waste time with realloc */
    int i;
197

K
Karel Zak 已提交
198 199 200 201 202 203 204
    for (i=0; i < 1000; i++)
        virBufferVSprintf(buf, "My name is %s.\n", "libvirt");

    virBufferFree(buf);
    return 0;
}

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

213
        progname = argv[0];
K
Karel Zak 已提交
214

215 216 217 218 219
        if (argc > 2)
        {
                fprintf(stderr, "Usage: %s [url]\n", progname);
                exit(EXIT_FAILURE);
        }
K
Karel Zak 已提交
220 221
    if (argc == 2)
        url = argv[1];
222 223 224

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

232
       if (virtTestRun("XML-RPC methodCall INT+INT",
D
Daniel P. Berrange 已提交
233
                NLOOPS, testMethodPlusINT, (const void *) cxt) != 0)
K
Karel Zak 已提交
234
        ret = -1;
235 236

    if (virtTestRun("XML-RPC methodCall DOUBLE+DOUBLE",
D
Daniel P. Berrange 已提交
237
                NLOOPS, testMethodPlusDOUBLE, (const void *) cxt) != 0)
K
Karel Zak 已提交
238
        ret = -1;
239

240
        xmlRpcContextFree(cxt);
241 242 243

    /*
     * regression / performance tests
K
Karel Zak 已提交
244
     */
245
    if (virtTestRun("XML-RPC request marshalling: INT (check)",
D
Daniel P. Berrange 已提交
246
                1, testMarshalRequestINT, (const void *) &check) != 0)
K
Karel Zak 已提交
247
        ret = -1;
248
    if (virtTestRun("XML-RPC request marshalling: INT",
K
Karel Zak 已提交
249 250
                NLOOPS, testMarshalRequestINT, NULL) != 0)
        ret = -1;
251 252

    if (virtTestRun("XML-RPC request marshalling: DOUBLE (check)",
D
Daniel P. Berrange 已提交
253
                1, testMarshalRequestDOUBLE, (const void *) &check) != 0)
K
Karel Zak 已提交
254
        ret = -1;
255
    if (virtTestRun("XML-RPC request marshalling: DOUBLE",
K
Karel Zak 已提交
256 257 258
                NLOOPS, testMarshalRequestDOUBLE, NULL) != 0)
        ret = -1;

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

    if (virtTestRun("Buffer: strcat", NLOOPS, testBufferStrcat, NULL) != 0)
        ret = -1;
    if (virtTestRun("Buffer: sprintf", NLOOPS, testBufferVSprintf, NULL) != 0)
        ret = -1;
270

K
Karel Zak 已提交
271

272
        exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
K
Karel Zak 已提交
273 274 275 276 277 278 279 280
}


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