Menu

[r1477]: / advanced / src / cpp / test / server_pstream.cpp  Maximize  Restore  History

Download this file

178 lines (130 with data), 4.7 kB

  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
/*=============================================================================
server_pstream
===============================================================================
Test the pstream server C++ facilities of XML-RPC for C/C++.
=============================================================================*/
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string>
#include <fcntl.h>
#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_pstream.hpp"
#include "tools.hpp"
#include "server_pstream.hpp"
using namespace xmlrpc_c;
using namespace std;
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);
}
};
static void
createTestFile(string const& contents,
int * const fdP) {
string const filename("/tmp/xmlrpc_test_pstream");
unlink(filename.c_str());
int rc;
rc = open(filename.c_str(), O_RDWR | O_CREAT);
unlink(filename.c_str());
if (rc < 0)
throwf("Failed to create file '%s' as a test tool. errno=%d (%s)",
filename.c_str(), errno, strerror(errno));
else {
int const fd(rc);
int rc;
rc = write(fd, contents.c_str(), contents.length());
if (rc < 0)
throwf("write() of test file failed, errno=%d (%s)",
errno, strerror(errno));
else {
unsigned int bytesWritten(rc);
if (bytesWritten != contents.length())
throwf("Short write");
else {
int rc;
rc = lseek(fd, 0, SEEK_SET);
if (rc < 0)
throwf("lseek(0) of test file failed, errno=%d (%s)",
errno, strerror(errno));
}
}
*fdP = fd;
}
}
class serverPstreamConnTestSuite : public testSuite {
public:
virtual string suiteName() {
return "serverPstreamConnTestSuite";
}
virtual void runtests(unsigned int const) {
int const devNullFd(open("/dev/null", 0));
if (devNullFd < 0)
throwf("Failed to open /dev/null, needed for test.");
registry myRegistry;
myRegistry.addMethod("sample.add", methodPtr(new sampleAddMethod));
registryPtr myRegistryP(new registry);
myRegistryP->addMethod("sample.add", methodPtr(new sampleAddMethod));
EXPECT_ERROR( // Empty options
serverPstreamConn::constrOpt opt;
serverPstreamConn server(opt);
);
EXPECT_ERROR( // No registry
serverPstreamConn server(serverPstreamConn::constrOpt()
.socketFd(3));
);
EXPECT_ERROR( // No socket fd
serverPstreamConn server(serverPstreamConn::constrOpt()
.registryP(&myRegistry));
);
EXPECT_ERROR( // No such file descriptor
serverPstreamConn server(serverPstreamConn::constrOpt()
.registryP(&myRegistry)
.socketFd(37));
);
{
serverPstreamConn server(serverPstreamConn::constrOpt()
.registryP(&myRegistry)
.socketFd(devNullFd));
bool eof;
server.runOnce(&eof);
TEST(eof);
}
{
int fd;
createTestFile("junk", &fd);
serverPstreamConn server(serverPstreamConn::constrOpt()
.registryP(&myRegistry)
.socketFd(fd));
bool eof;
EXPECT_ERROR( // EOF in the middle of a packet
server.runOnce(&eof);
);
close(fd);
}
close(devNullFd);
}
};
string
serverPstreamTestSuite::suiteName() {
return "serverPstreamTestSuite";
}
void
serverPstreamTestSuite::runtests(unsigned int const indentation) {
serverPstreamConnTestSuite().run(indentation + 1);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.