~ubuntu-branches/ubuntu/maverick/libvirt/maverick

« back to all changes in this revision

Viewing changes to tests/xmlrpctest.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2009-02-11 01:01:42 UTC
  • mto: (3.4.1 sid) (1.2.1 upstream) (0.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090211010142-wk9mgtbw8bmp3zcb
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * xmlrpctest.c: simple client for XML-RPC tests
3
 
 *
4
 
 * Copyright (C) 2005, 2008 Red Hat, Inc.
5
 
 *
6
 
 * See COPYING.LIB for the License of this software
7
 
 *
8
 
 * Karel Zak <kzak@redhat.com>
9
 
 *
10
 
 * $Id: xmlrpctest.c,v 1.14 2008/05/23 08:24:44 rjones Exp $
11
 
 */
12
 
 
13
 
#include <config.h>
14
 
 
15
 
#include <stdio.h>
16
 
#include <stdlib.h>
17
 
#include <stdarg.h>
18
 
#include <string.h>
19
 
#include <limits.h>
20
 
#include <libxml/parser.h>
21
 
#include <libxml/tree.h>
22
 
#include <libxml/xpath.h>
23
 
 
24
 
#include "internal.h"
25
 
#include "buf.h"
26
 
#include "xmlrpc.h"
27
 
 
28
 
#include "testutils.h"
29
 
 
30
 
 
31
 
#define NLOOPS  100     /* default number of loops per test */
32
 
 
33
 
static char *progname;
34
 
 
35
 
 
36
 
static int
37
 
testMethodPlusINT(const void *data)
38
 
{
39
 
    int retval = 0;
40
 
    xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
41
 
 
42
 
    if (xmlRpcCall(cxt, "plus", "i", "ii",
43
 
            (const char *) &retval, 10, 10) < 0)
44
 
        return -1;
45
 
 
46
 
    return retval==(10+10) ? 0 : -1;
47
 
}
48
 
 
49
 
static int
50
 
testMethodPlusDOUBLE(const void *data)
51
 
{
52
 
    double retval = 0;
53
 
    xmlRpcContextPtr cxt = (xmlRpcContextPtr) data;
54
 
 
55
 
    if (xmlRpcCall(cxt, "plus", "f", "ff",
56
 
            (const char *) &retval, 10.1234, 10.1234) < 0)
57
 
        return -1;
58
 
 
59
 
    return retval==(10.1234+10.1234) ? 0 : -1;
60
 
}
61
 
 
62
 
static void
63
 
marshalRequest(virBufferPtr buf, const char *fmt, ...)
64
 
{
65
 
    int argc;
66
 
    xmlRpcValuePtr *argv;
67
 
    va_list ap;
68
 
 
69
 
    va_start(ap, fmt);
70
 
    argv = xmlRpcArgvNew(fmt, ap, &argc);
71
 
    va_end(ap);
72
 
 
73
 
    xmlRpcMarshalRequest("test", buf, argc, argv);
74
 
 
75
 
    xmlRpcArgvFree(argc, argv);
76
 
}
77
 
 
78
 
static int
79
 
checkRequestValue(const char *xmlstr, const char *xpath, int type, void *expected)
80
 
{
81
 
    xmlDocPtr xml = NULL;
82
 
    xmlXPathContextPtr ctxt = NULL;
83
 
    xmlXPathObjectPtr obj = NULL;
84
 
    int ret = -1;
85
 
 
86
 
    xml = xmlReadDoc((const xmlChar *) xmlstr, "xmlrpctest.xml", NULL,
87
 
                          XML_PARSE_NOENT | XML_PARSE_NONET |
88
 
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
89
 
    if (!xml)
90
 
        goto error;
91
 
 
92
 
    if (!(ctxt = xmlXPathNewContext(xml)))
93
 
        goto error;
94
 
 
95
 
    if (!(obj = xmlXPathEval(BAD_CAST xpath, ctxt)))
96
 
        goto error;
97
 
 
98
 
    switch(type) {
99
 
        case XML_RPC_INTEGER:
100
 
            if ((obj->type != XPATH_NUMBER) ||
101
 
                    ((int) obj->floatval != *((int *)expected)))
102
 
                goto error;
103
 
            break;
104
 
         case XML_RPC_DOUBLE:
105
 
            if ((obj->type != XPATH_NUMBER) ||
106
 
                    ((double) obj->floatval != *((double *)expected)))
107
 
                goto error;
108
 
            break;
109
 
         case XML_RPC_STRING:
110
 
            if ((obj->type != XPATH_STRING) ||
111
 
                    (STRNEQ((const char *)obj->stringval, (const char *)expected)))
112
 
                goto error;
113
 
            break;
114
 
        default:
115
 
            goto error;
116
 
    }
117
 
    ret = 0;
118
 
 
119
 
error:
120
 
    xmlXPathFreeObject(obj);
121
 
    xmlXPathFreeContext(ctxt);
122
 
    if (xml)
123
 
        xmlFreeDoc(xml);
124
 
    return ret;
125
 
}
126
 
 
127
 
static int
128
 
testMarshalRequestINT(const void *data)
129
 
{
130
 
    int num = INT_MAX;
131
 
    int ret = 0;
132
 
    int check = data ? *((int *)data) : 0;
133
 
    virBuffer buf = VIR_BUFFER_INITIALIZER;
134
 
    marshalRequest(&buf, "i", num);
135
 
    char *content;
136
 
 
137
 
    if (virBufferError(&buf))
138
 
        return -1;
139
 
 
140
 
    content = virBufferContentAndReset(&buf);
141
 
 
142
 
    if (check)
143
 
        ret = checkRequestValue(content,
144
 
                "number(/methodCall/params/param[1]/value/int)",
145
 
                XML_RPC_INTEGER, (void *) &num);
146
 
 
147
 
    free(content);
148
 
    return ret;
149
 
}
150
 
 
151
 
static int
152
 
testMarshalRequestSTRING(const void *data ATTRIBUTE_UNUSED)
153
 
{
154
 
    const char *str = "This library will be really sexy.";
155
 
    int ret = 0;
156
 
    int check = data ? *((int *)data) : 0;
157
 
    virBuffer buf = VIR_BUFFER_INITIALIZER;
158
 
    char *content;
159
 
 
160
 
    marshalRequest(&buf, "s", str);
161
 
 
162
 
    if (virBufferError(&buf))
163
 
        return -1;
164
 
 
165
 
    content = virBufferContentAndReset(&buf);
166
 
    if (check)
167
 
        ret = checkRequestValue(content,
168
 
                "string(/methodCall/params/param[1]/value/string)",
169
 
                XML_RPC_STRING, (void *) str);
170
 
 
171
 
    free(content);
172
 
    return ret;
173
 
}
174
 
 
175
 
static int
176
 
testMarshalRequestDOUBLE(const void *data)
177
 
{
178
 
    double num = 123456789.123;
179
 
    int ret = 0;
180
 
    int check = data ? *((int *)data) : 0;
181
 
    virBuffer buf = VIR_BUFFER_INITIALIZER;
182
 
    char *content;
183
 
 
184
 
    marshalRequest(&buf, "f", num);
185
 
 
186
 
    if (virBufferError(&buf))
187
 
        return -1;
188
 
 
189
 
    content = virBufferContentAndReset(&buf);
190
 
    if (check)
191
 
        ret = checkRequestValue(content,
192
 
                "number(/methodCall/params/param[1]/value/double)",
193
 
                XML_RPC_DOUBLE, (void *) &num);
194
 
 
195
 
    free(content);
196
 
    return ret;
197
 
}
198
 
 
199
 
 
200
 
int
201
 
main(int argc, char **argv)
202
 
{
203
 
        xmlRpcContextPtr cxt = NULL;
204
 
    int check = 1;
205
 
        int ret = 0;
206
 
    const char *url = "http://localhost:8000";
207
 
 
208
 
        progname = argv[0];
209
 
 
210
 
        if (argc > 2)
211
 
        {
212
 
                fprintf(stderr, "Usage: %s [url]\n", progname);
213
 
                exit(EXIT_FAILURE);
214
 
        }
215
 
    if (argc == 2)
216
 
        url = argv[1];
217
 
 
218
 
     /*
219
 
      * client-server tests
220
 
      */
221
 
        if (!(cxt = xmlRpcContextNew(url)))
222
 
        {
223
 
                fprintf(stderr, "%s: failed create new RPC context\n", progname);
224
 
                exit(EXIT_FAILURE);
225
 
        }
226
 
 
227
 
       if (virtTestRun("XML-RPC methodCall INT+INT",
228
 
                NLOOPS, testMethodPlusINT, (const void *) cxt) != 0)
229
 
        ret = -1;
230
 
 
231
 
    if (virtTestRun("XML-RPC methodCall DOUBLE+DOUBLE",
232
 
                NLOOPS, testMethodPlusDOUBLE, (const void *) cxt) != 0)
233
 
        ret = -1;
234
 
 
235
 
        xmlRpcContextFree(cxt);
236
 
 
237
 
    /*
238
 
     * regression / performance tests
239
 
     */
240
 
    if (virtTestRun("XML-RPC request marshalling: INT (check)",
241
 
                1, testMarshalRequestINT, (const void *) &check) != 0)
242
 
        ret = -1;
243
 
    if (virtTestRun("XML-RPC request marshalling: INT",
244
 
                NLOOPS, testMarshalRequestINT, NULL) != 0)
245
 
        ret = -1;
246
 
 
247
 
    if (virtTestRun("XML-RPC request marshalling: DOUBLE (check)",
248
 
                1, testMarshalRequestDOUBLE, (const void *) &check) != 0)
249
 
        ret = -1;
250
 
    if (virtTestRun("XML-RPC request marshalling: DOUBLE",
251
 
                NLOOPS, testMarshalRequestDOUBLE, NULL) != 0)
252
 
        ret = -1;
253
 
 
254
 
    if (virtTestRun("XML-RPC request marshalling: STRING (check)",
255
 
                1, testMarshalRequestSTRING, (void *) &check) != 0)
256
 
        ret = -1;
257
 
    if (virtTestRun("XML-RPC request marshalling: STRING",
258
 
                NLOOPS, testMarshalRequestSTRING, NULL) != 0)
259
 
        ret = -1;
260
 
 
261
 
    exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
262
 
}
263
 
 
264
 
 
265
 
/*
266
 
 * vim: set tabstop=4:
267
 
 * vim: set shiftwidth=4:
268
 
 * vim: set expandtab:
269
 
 */