1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*=============================================================================
server_abyss
===============================================================================
Test the Abyss XML-RPC server C++ facilities of XML-RPC for C/C++.
=============================================================================*/
#define WIN32_LEAN_AND_MEAN /* required by xmlrpc-c/abyss.h */
#include <errno.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <memory>
#include <cstring>
#include <cstdlib>
#include <time.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#endif
using namespace std;
#include "xmlrpc-c/girerr.hpp"
using girerr::error;
using girerr::throwf;
#include "xmlrpc-c/base.hpp"
#include "xmlrpc-c/registry.hpp"
#include "xmlrpc-c/server_abyss.hpp"
#include "xmlrpc-c/abyss.h"
using namespace xmlrpc_c;
#include "tools.hpp"
#include "server_abyss.hpp"
namespace {
static void
closesock(int const fd) {
#ifdef WIN32
closesocket(fd);
#else
close(fd);
#endif
}
static sockaddr_in
localhostSockAddr(short const portNumber) {
struct sockaddr_in retval;
retval.sin_family = AF_INET;
retval.sin_port = htons(portNumber);
retval.sin_addr = test_ipAddrFromDecimal(127, 0, 0, 1);
return retval;
}
class boundSocket {
public:
boundSocket(short const portNumber) {
this->fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (this->fd < 0)
throwf("socket() failed. errno=%d (%s)",
errno, strerror(errno));
struct sockaddr_in sockAddr;
int rc;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(portNumber);
sockAddr.sin_addr.s_addr = 0;
rc = bind(this->fd, (struct sockaddr *)&sockAddr, sizeof(sockAddr));
if (rc != 0) {
closesock(this->fd);
throwf("Couldn't bind. bind() failed with errno=%d (%s)",
errno, strerror(errno));
}
}
~boundSocket() {
closesock(this->fd);
}
XMLRPC_SOCKET fd;
};
class sampleAddMethod : public method {
public:
sampleAddMethod() {
this->_signature = "i:ii";
this->_help = "This method adds two integers together";
}
void
execute(xmlrpc_c::paramList const& paramList,
value * const retvalP) {
int const addend(paramList.getInt(0));
int const adder(paramList.getInt(1));
paramList.verifyEnd(2);
*retvalP = value_int(addend + adder);
}
};
// We need 'global' because methods of class addHandlerTestSuite call
// functions in the Abyss C library. By virtue of global's static
// storage class, the program loader will call its constructor and
// destructor and thus initialize and terminate the Abyss C library.
static class abyssGlobalState {
public:
abyssGlobalState() {
const char * error;
AbyssInit(&error);
if (error) {
string const e(error);
free(const_cast<char *>(error));
throwf("AbyssInit() failed. %s", e.c_str());
}
}
~abyssGlobalState() {
AbyssTerm();
}
} const global;
class addHandlerTestSuite : public testSuite {
public:
virtual string suiteName() {
return "addHandlerTestSuite";
}
virtual void runtests(unsigned int const) {
TServer abyssServer;
ServerCreate(&abyssServer, "testserver", 8080, NULL, NULL);
registry myRegistry;
myRegistry.addMethod("sample.add", methodPtr(new sampleAddMethod));
registryPtr myRegistryP(new registry);
myRegistryP->addMethod("sample.add", methodPtr(new sampleAddMethod));
server_abyss_set_handlers(&abyssServer, myRegistry);
server_abyss_set_handlers(&abyssServer, &myRegistry);
server_abyss_set_handlers(&abyssServer, myRegistryP);
server_abyss_set_handlers(&abyssServer, myRegistry, "/RPC3");
server_abyss_set_handlers(&abyssServer, &myRegistry, "/RPC3");
server_abyss_set_handlers(&abyssServer, myRegistryP, "/RPC3");
ServerFree(&abyssServer);
}
};
class setShutdownTestSuite : public testSuite {
public:
virtual string suiteName() {
return "setShutdownTestSuite";
}
virtual void runtests(unsigned int const) {
registry myRegistry;
serverAbyss myServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.portNumber(12345)
);
serverAbyss::shutdown shutdown(&myServer);
myRegistry.setShutdown(&shutdown);
}
};
class createTestSuite : public testSuite {
public:
virtual string suiteName() {
return "createTestSuite";
}
virtual void runtests(unsigned int const) {
registry myRegistry;
myRegistry.addMethod("sample.add", methodPtr(new sampleAddMethod));
registryPtr myRegistryP(new registry);
myRegistryP->addMethod("sample.add", methodPtr(new sampleAddMethod));
struct sockaddr_in const sockAddr(localhostSockAddr(8080));
const struct sockaddr * const sockAddrP(
(const struct sockaddr *)&sockAddr);
EXPECT_ERROR( // No registry
serverAbyss::constrOpt opt;
serverAbyss abyssServer(opt);
);
EXPECT_ERROR( // Both registryP and registryPtr
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryPtr(myRegistryP)
.registryP(&myRegistry)
.portNumber(12345)
);
);
EXPECT_ERROR( // Both portNumber and socketFd
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.portNumber(8080)
.socketFd(3));
);
EXPECT_ERROR( // Both portNumber and sockAddrP
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.portNumber(8080)
.sockAddrP(sockAddrP)
.sockAddrLen(sizeof(sockAddr)));
);
EXPECT_ERROR( // Both socketFd and sockAddrP
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.socketFd(3)
.sockAddrP(sockAddrP)
.sockAddrLen(sizeof(sockAddr)));
);
EXPECT_ERROR( // sockAddrP but no sockAddrLen
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.sockAddrP(sockAddrP));
);
EXPECT_ERROR( // port number too big
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.portNumber(65536));
);
// Because of the vagaries of Abyss, construction of the following
// objects may exit the program if it detects an error, such as
// port number already in use. We need to fix Abyss some day.
{
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.portNumber(12345)
);
}
{
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryPtr(myRegistryP)
.portNumber(12345)
);
}
{
boundSocket socket(12345);
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.socketFd(socket.fd)
);
}
{
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
);
}
{
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryPtr(myRegistryP)
.sockAddrP(sockAddrP)
.sockAddrLen(sizeof(sockAddr))
);
}
{
// Test all the options
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryPtr(myRegistryP)
.portNumber(12345)
.maxConn(10)
.maxConnBacklog(10)
.keepaliveTimeout(5)
.keepaliveMaxConn(4)
.timeout(20)
.dontAdvertise(true)
.uriPath("/xmlrpc")
.chunkResponse(true)
.allowOrigin("*")
.accessCtlMaxAge(42)
.logFileName("/tmp/logfile")
.serverOwnsSignals(false)
.expectSigchld(true)
);
}
{
serverAbyss abyssServer(
myRegistry,
12345, // TCP port on which to listen
"/tmp/xmlrpc_log" // Log file
);
}
}
};
class testCallInfoMethod : public method2 {
public:
void
execute(paramList const& paramList,
const callInfo * const callInfoPtr,
value * const retvalP) {
const callInfo_serverAbyss * const callInfoP(
dynamic_cast<const callInfo_serverAbyss *>(callInfoPtr));
TEST(callInfoP != NULL);
paramList.verifyEnd(0);
TEST(callInfoP->serverAbyssP != NULL);
TEST(callInfoP->abyssSessionP != NULL);
*retvalP = value_nil();
}
};
class callInfoTestSuite : public testSuite {
public:
virtual string suiteName() {
return "callInfoTestSuite";
}
virtual void runtests(unsigned int const) {
registry myRegistry;
myRegistry.addMethod("sample.add", methodPtr(new testCallInfoMethod));
serverAbyss abyssServer(serverAbyss::constrOpt()
.registryP(&myRegistry)
.portNumber(12345)
);
}
};
} // unnamed namespace
string
serverAbyssTestSuite::suiteName() {
return "serverAbyssTestSuite";
}
void
serverAbyssTestSuite::runtests(unsigned int const indentation) {
addHandlerTestSuite().run(indentation+1);
setShutdownTestSuite().run(indentation+1);
createTestSuite().run(indentation+1);
callInfoTestSuite().run(indentation+1);
}
|