~ubuntu-branches/ubuntu/natty/xmlrpc-c/natty

« back to all changes in this revision

Viewing changes to src/cpp/test/registry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-01-06 18:56:02 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20110106185602-09og2x3suqlzbf6s
Tags: 1.16.32-0ubuntu1
* New upstream version (stable release). LP: #659591.
  - No unresolved symbols in the shared libraries. LP: #690779.
  - Builds with --no-add-needed and --as-needed.
* Rename shared library packages.
* Add symbols files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=============================================================================
 
2
                                  registry
 
3
===============================================================================
 
4
  Test the method registry (server) C++ facilities of XML-RPC for C/C++.
 
5
  
 
6
=============================================================================*/
 
7
 
 
8
#include <string>
 
9
 
 
10
#include "xmlrpc-c/girerr.hpp"
 
11
using girerr::error;
 
12
using girerr::throwf;
 
13
#include "xmlrpc-c/base.hpp"
 
14
#include "xmlrpc-c/registry.hpp"
 
15
 
 
16
#include "tools.hpp"
 
17
#include "registry.hpp"
 
18
 
 
19
using namespace xmlrpc_c;
 
20
using namespace std;
 
21
 
 
22
 
 
23
static string const
 
24
xmlPrologue("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
 
25
 
 
26
static string const
 
27
apacheUrl("http://ws.apache.org/xmlrpc/namespaces/extensions");
 
28
 
 
29
static string const
 
30
xmlnsApache("xmlns:ex=\"" + apacheUrl + "\"");
 
31
 
 
32
 
 
33
namespace {
 
34
string const noElementFoundXml(
 
35
    xmlPrologue +
 
36
    "<methodResponse>\r\n"
 
37
    "<fault>\r\n"
 
38
    "<value><struct>\r\n"
 
39
    "<member><name>faultCode</name>\r\n"
 
40
    "<value><i4>-503</i4></value></member>\r\n"
 
41
    "<member><name>faultString</name>\r\n"
 
42
    "<value><string>Call XML not a proper XML-RPC call.  "
 
43
    "Call is not valid XML.  no element found</string></value>"
 
44
    "</member>\r\n"
 
45
    "</struct></value>\r\n"
 
46
    "</fault>\r\n"
 
47
    "</methodResponse>\r\n"
 
48
    );
 
49
 
 
50
string const sampleAddGoodCallXml(
 
51
    xmlPrologue +
 
52
    "<methodCall>\r\n"
 
53
    "<methodName>sample.add</methodName>\r\n"
 
54
    "<params>\r\n"
 
55
    "<param><value><i4>5</i4></value></param>\r\n"
 
56
    "<param><value><i4>7</i4></value></param>\r\n"
 
57
    "</params>\r\n"
 
58
    "</methodCall>\r\n"
 
59
    );
 
60
 
 
61
string const sampleAddGoodResponseXml(
 
62
    xmlPrologue +
 
63
    "<methodResponse>\r\n"
 
64
    "<params>\r\n"
 
65
    "<param><value><i4>12</i4></value></param>\r\n"
 
66
    "</params>\r\n"
 
67
    "</methodResponse>\r\n"
 
68
    );
 
69
 
 
70
 
 
71
string const sampleAddBadCallXml(
 
72
    xmlPrologue +
 
73
    "<methodCall>\r\n"
 
74
    "<methodName>sample.add</methodName>\r\n"
 
75
    "<params>\r\n"
 
76
    "<param><value><i4>5</i4></value></param>\r\n"
 
77
    "</params>\r\n"
 
78
    "</methodCall>\r\n"
 
79
    );
 
80
 
 
81
string const sampleAddBadResponseXml(
 
82
    xmlPrologue +
 
83
    "<methodResponse>\r\n"
 
84
    "<fault>\r\n"
 
85
    "<value><struct>\r\n"
 
86
    "<member><name>faultCode</name>\r\n"
 
87
    "<value><i4>-501</i4></value></member>\r\n"
 
88
    "<member><name>faultString</name>\r\n"
 
89
    "<value><string>Not enough parameters</string></value></member>\r\n"
 
90
    "</struct></value>\r\n"
 
91
    "</fault>\r\n"
 
92
    "</methodResponse>\r\n"
 
93
    );
 
94
 
 
95
 
 
96
string const nonexistentMethodCallXml(
 
97
    xmlPrologue +
 
98
    "<methodCall>\r\n"
 
99
    "<methodName>nosuchmethod</methodName>\r\n"
 
100
    "<params>\r\n"
 
101
    "<param><value><i4>5</i4></value></param>\r\n"
 
102
    "<param><value><i4>7</i4></value></param>\r\n"
 
103
    "</params>\r\n"
 
104
    "</methodCall>\r\n"
 
105
    );
 
106
 
 
107
string const nonexistentMethodYesDefResponseXml(
 
108
    xmlPrologue +
 
109
    "<methodResponse>\r\n"
 
110
    "<params>\r\n"
 
111
    "<param><value><string>no such method: nosuchmethod</string>"
 
112
    "</value></param>\r\n"
 
113
    "</params>\r\n"
 
114
    "</methodResponse>\r\n"
 
115
    );
 
116
 
 
117
string const nonexistentMethodNoDefResponseXml(
 
118
    xmlPrologue +
 
119
    "<methodResponse>\r\n"
 
120
    "<fault>\r\n"
 
121
    "<value><struct>\r\n"
 
122
    "<member><name>faultCode</name>\r\n"
 
123
    "<value><i4>-506</i4></value></member>\r\n"
 
124
    "<member><name>faultString</name>\r\n"
 
125
    "<value><string>Method 'nosuchmethod' not defined</string></value>"
 
126
    "</member>\r\n"
 
127
    "</struct></value>\r\n"
 
128
    "</fault>\r\n"
 
129
    "</methodResponse>\r\n"
 
130
    );
 
131
 
 
132
} // namespace
 
133
 
 
134
 
 
135
string const echoI8ApacheCall(
 
136
    xmlPrologue +
 
137
    "<methodCall " + xmlnsApache + ">\r\n"
 
138
    "<methodName>echo</methodName>\r\n"
 
139
    "<params>\r\n"
 
140
    "<param><value><ex:i8>5</ex:i8></value></param>\r\n"
 
141
    "</params>\r\n"
 
142
    "</methodCall>\r\n"
 
143
    );
 
144
 
 
145
string const echoI8ApacheResponse(
 
146
    xmlPrologue +
 
147
    "<methodResponse " + xmlnsApache + ">\r\n"
 
148
    "<params>\r\n"
 
149
    "<param><value><ex:i8>5</ex:i8></value></param>\r\n"
 
150
    "</params>\r\n"
 
151
    "</methodResponse>\r\n"
 
152
    );
 
153
 
 
154
string const echoNilApacheCall(
 
155
    xmlPrologue +
 
156
    "<methodCall " + xmlnsApache + ">\r\n"
 
157
    "<methodName>echo</methodName>\r\n"
 
158
    "<params>\r\n"
 
159
    "<param><value><nil/></value></param>\r\n"
 
160
    "</params>\r\n"
 
161
    "</methodCall>\r\n"
 
162
    );
 
163
 
 
164
string const echoNilApacheResponse(
 
165
    xmlPrologue +
 
166
    "<methodResponse " + xmlnsApache + ">\r\n"
 
167
    "<params>\r\n"
 
168
    "<param><value><ex:nil/></value></param>\r\n"
 
169
    "</params>\r\n"
 
170
    "</methodResponse>\r\n"
 
171
    );
 
172
 
 
173
 
 
174
class sampleAddMethod : public method {
 
175
public:
 
176
    sampleAddMethod() {
 
177
        this->_signature = "i:ii";
 
178
        this->_help = "This method adds two integers together";
 
179
    }
 
180
    void
 
181
    execute(xmlrpc_c::paramList const& paramList,
 
182
            value *             const  retvalP) {
 
183
        
 
184
        int const addend(paramList.getInt(0));
 
185
        int const adder(paramList.getInt(1));
 
186
        
 
187
        paramList.verifyEnd(2);
 
188
        
 
189
        *retvalP = value_int(addend + adder);
 
190
    }
 
191
};
 
192
 
 
193
 
 
194
 
 
195
class nameMethod : public defaultMethod {
 
196
 
 
197
    void
 
198
    execute(string              const& methodName,
 
199
            xmlrpc_c::paramList const& ,  // paramList
 
200
            value *             const  retvalP) {
 
201
        
 
202
        *retvalP = value_string(string("no such method: ") + methodName);
 
203
    }
 
204
};
 
205
 
 
206
 
 
207
 
 
208
class echoMethod : public method {
 
209
public:
 
210
    void
 
211
    execute(xmlrpc_c::paramList const& paramList,
 
212
            value *             const  retvalP) {
 
213
        
 
214
        paramList.verifyEnd(1);
 
215
        
 
216
        *retvalP = paramList[0];
 
217
    }
 
218
};
 
219
 
 
220
 
 
221
 
 
222
class registryRegMethodTestSuite : public testSuite {
 
223
 
 
224
public:
 
225
    virtual string suiteName() {
 
226
        return "registryRegMethodTestSuite";
 
227
    }
 
228
    virtual void runtests(unsigned int const) {
 
229
 
 
230
        xmlrpc_c::registry myRegistry;
 
231
        
 
232
        myRegistry.addMethod("sample.add", 
 
233
                             xmlrpc_c::methodPtr(new sampleAddMethod));
 
234
        
 
235
        myRegistry.disableIntrospection();
 
236
        {
 
237
            string response;
 
238
            myRegistry.processCall("", &response);
 
239
            TEST(response == noElementFoundXml);
 
240
        }
 
241
        {
 
242
            string response;
 
243
            myRegistry.processCall(sampleAddGoodCallXml, &response);
 
244
            TEST(response == sampleAddGoodResponseXml);
 
245
        }
 
246
        {
 
247
            string response;
 
248
            myRegistry.processCall(sampleAddBadCallXml, &response);
 
249
            TEST(response == sampleAddBadResponseXml);
 
250
        }
 
251
    }
 
252
};
 
253
 
 
254
 
 
255
 
 
256
class registryDefaultMethodTestSuite : public testSuite {
 
257
 
 
258
public:
 
259
    virtual string suiteName() {
 
260
        return "registryDefaultMethodTestSuite";
 
261
    }
 
262
    virtual void runtests(unsigned int const) {
 
263
 
 
264
        xmlrpc_c::registry myRegistry;
 
265
        
 
266
        myRegistry.addMethod("sample.add", methodPtr(new sampleAddMethod));
 
267
 
 
268
        {
 
269
            string response;
 
270
            myRegistry.processCall(sampleAddGoodCallXml, &response);
 
271
            TEST(response == sampleAddGoodResponseXml);
 
272
        }
 
273
        {
 
274
            string response;
 
275
            myRegistry.processCall(nonexistentMethodCallXml, &response);
 
276
            TEST(response == nonexistentMethodNoDefResponseXml);
 
277
        }
 
278
        // We're actually violating the spirit of setDefaultMethod by
 
279
        // doing this to a registry that's already been used, but as long
 
280
        // as it works, it's a convenient way to implement this test.
 
281
        myRegistry.setDefaultMethod(defaultMethodPtr(new nameMethod));
 
282
 
 
283
        {
 
284
            string response;
 
285
            myRegistry.processCall(nonexistentMethodCallXml, &response);
 
286
            TEST(response == nonexistentMethodYesDefResponseXml);
 
287
        }
 
288
    }
 
289
};
 
290
 
 
291
 
 
292
 
 
293
class registryShutdownTestSuite : public testSuite {
 
294
 
 
295
public:
 
296
    virtual string suiteName() {
 
297
        return "registryShutdownTestSuite";
 
298
    }
 
299
    virtual void runtests(unsigned int const) {
 
300
 
 
301
        xmlrpc_c::registry myRegistry;
 
302
 
 
303
        class myshutdown : public xmlrpc_c::registry::shutdown {
 
304
        public:
 
305
            void doit(string const&,
 
306
                      void * const) const {
 
307
                
 
308
            }
 
309
        };
 
310
 
 
311
        myshutdown shutdown;
 
312
        
 
313
        myRegistry.setShutdown(&shutdown);
 
314
    }
 
315
};
 
316
 
 
317
 
 
318
 
 
319
string
 
320
registryTestSuite::suiteName() {
 
321
    return "registryTestSuite";
 
322
}
 
323
 
 
324
 
 
325
 
 
326
void
 
327
registryTestSuite::runtests(unsigned int const indentation) {
 
328
 
 
329
    {
 
330
        registryPtr myRegistryP(new registry);
 
331
    
 
332
        myRegistryP->addMethod("sample.add", methodPtr(new sampleAddMethod));
 
333
    }
 
334
 
 
335
    registryRegMethodTestSuite().run(indentation+1);
 
336
    registryDefaultMethodTestSuite().run(indentation+1);
 
337
 
 
338
    registry myRegistry;
 
339
        
 
340
    myRegistry.addMethod("sample.add", methodPtr(new sampleAddMethod));
 
341
    myRegistry.addMethod("echo", methodPtr(new echoMethod));
 
342
 
 
343
    string response;
 
344
 
 
345
    myRegistry.disableIntrospection();
 
346
 
 
347
    myRegistry.setDialect(xmlrpc_dialect_i8);
 
348
 
 
349
    myRegistry.setDialect(xmlrpc_dialect_apache);
 
350
 
 
351
    registryShutdownTestSuite().run(indentation+1);
 
352
 
 
353
    myRegistry.processCall(echoI8ApacheCall, &response);
 
354
 
 
355
    TEST(response == echoI8ApacheResponse);
 
356
 
 
357
    myRegistry.processCall(echoNilApacheCall, &response);
 
358
 
 
359
    TEST(response == echoNilApacheResponse);
 
360
 
 
361
    EXPECT_ERROR(  // invalid dialect
 
362
        myRegistry.setDialect(static_cast<xmlrpc_dialect>(300));
 
363
        );
 
364
}