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

« back to all changes in this revision

Viewing changes to src/cpp/test/server_pstream.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
                                  server_pstream
 
3
===============================================================================
 
4
  Test the pstream server C++ facilities of XML-RPC for C/C++.
 
5
  
 
6
=============================================================================*/
 
7
#include <unistd.h>
 
8
#include <sys/socket.h>
 
9
#include <arpa/inet.h>
 
10
#include <errno.h>
 
11
#include <string>
 
12
#include <fcntl.h>
 
13
 
 
14
#include "xmlrpc-c/girerr.hpp"
 
15
using girerr::error;
 
16
using girerr::throwf;
 
17
#include "xmlrpc-c/base.hpp"
 
18
#include "xmlrpc-c/registry.hpp"
 
19
#include "xmlrpc-c/server_pstream.hpp"
 
20
 
 
21
#include "tools.hpp"
 
22
#include "server_pstream.hpp"
 
23
 
 
24
using namespace xmlrpc_c;
 
25
using namespace std;
 
26
 
 
27
 
 
28
 
 
29
class sampleAddMethod : public method {
 
30
public:
 
31
    sampleAddMethod() {
 
32
        this->_signature = "i:ii";
 
33
        this->_help = "This method adds two integers together";
 
34
    }
 
35
    void
 
36
    execute(xmlrpc_c::paramList const& paramList,
 
37
            value *             const  retvalP) {
 
38
        
 
39
        int const addend(paramList.getInt(0));
 
40
        int const adder(paramList.getInt(1));
 
41
        
 
42
        paramList.verifyEnd(2);
 
43
        
 
44
        *retvalP = value_int(addend + adder);
 
45
    }
 
46
};
 
47
 
 
48
 
 
49
 
 
50
static void
 
51
createTestFile(string const& contents,
 
52
               int *  const  fdP) {
 
53
 
 
54
    string const filename("/tmp/xmlrpc_test_pstream");
 
55
    unlink(filename.c_str());
 
56
    int rc;
 
57
    rc = open(filename.c_str(), O_RDWR | O_CREAT);
 
58
    unlink(filename.c_str());
 
59
    
 
60
    if (rc < 0)
 
61
        throwf("Failed to create file '%s' as a test tool.  errno=%d (%s)",
 
62
               filename.c_str(), errno, strerror(errno));
 
63
    else {
 
64
        int const fd(rc);
 
65
 
 
66
        int rc;
 
67
    
 
68
        rc = write(fd, contents.c_str(), contents.length());
 
69
 
 
70
        if (rc < 0)
 
71
            throwf("write() of test file failed, errno=%d (%s)",
 
72
                   errno, strerror(errno));
 
73
        else {
 
74
            unsigned int bytesWritten(rc);
 
75
 
 
76
            if (bytesWritten != contents.length())
 
77
                throwf("Short write");
 
78
            else {
 
79
                int rc;
 
80
                rc = lseek(fd, 0, SEEK_SET);
 
81
                
 
82
                if (rc < 0)
 
83
                    throwf("lseek(0) of test file failed, errno=%d (%s)",
 
84
                           errno, strerror(errno));
 
85
            }
 
86
        }
 
87
        *fdP = fd;
 
88
    }
 
89
}
 
90
 
 
91
 
 
92
 
 
93
class serverPstreamConnTestSuite : public testSuite {
 
94
 
 
95
public:
 
96
    virtual string suiteName() {
 
97
        return "serverPstreamConnTestSuite";
 
98
    }
 
99
    virtual void runtests(unsigned int const) {
 
100
        int const devNullFd(open("/dev/null", 0));
 
101
 
 
102
        if (devNullFd < 0)
 
103
            throwf("Failed to open /dev/null, needed for test.");
 
104
 
 
105
        registry myRegistry;
 
106
        
 
107
        myRegistry.addMethod("sample.add", methodPtr(new sampleAddMethod));
 
108
 
 
109
        registryPtr myRegistryP(new registry);
 
110
 
 
111
        myRegistryP->addMethod("sample.add", methodPtr(new sampleAddMethod));
 
112
 
 
113
        EXPECT_ERROR(  // Empty options
 
114
            serverPstreamConn::constrOpt opt;
 
115
            serverPstreamConn server(opt);
 
116
            );
 
117
 
 
118
        EXPECT_ERROR(  // No registry
 
119
            serverPstreamConn server(serverPstreamConn::constrOpt()
 
120
                                     .socketFd(3));
 
121
            );
 
122
 
 
123
        EXPECT_ERROR(  // No socket fd
 
124
            serverPstreamConn server(serverPstreamConn::constrOpt()
 
125
                                     .registryP(&myRegistry));
 
126
            );
 
127
        
 
128
        EXPECT_ERROR(  // No such file descriptor
 
129
            serverPstreamConn server(serverPstreamConn::constrOpt()
 
130
                                     .registryP(&myRegistry)
 
131
                                     .socketFd(37));
 
132
            );
 
133
        
 
134
        {
 
135
            serverPstreamConn server(serverPstreamConn::constrOpt()
 
136
                                     .registryP(&myRegistry)
 
137
                                     .socketFd(devNullFd));
 
138
 
 
139
            bool eof;
 
140
            server.runOnce(&eof);
 
141
            TEST(eof);
 
142
        }
 
143
        {
 
144
            int fd;
 
145
            createTestFile("junk", &fd);
 
146
 
 
147
            serverPstreamConn server(serverPstreamConn::constrOpt()
 
148
                                     .registryP(&myRegistry)
 
149
                                     .socketFd(fd));
 
150
 
 
151
            bool eof;
 
152
 
 
153
            EXPECT_ERROR(   // EOF in the middle of a packet
 
154
                server.runOnce(&eof);
 
155
                );
 
156
            close(fd);
 
157
        }
 
158
 
 
159
        close(devNullFd);
 
160
    }
 
161
};
 
162
 
 
163
 
 
164
 
 
165
string
 
166
serverPstreamTestSuite::suiteName() {
 
167
    return "serverPstreamTestSuite";
 
168
}
 
169
 
 
170
 
 
171
void
 
172
serverPstreamTestSuite::runtests(unsigned int const indentation) {
 
173
 
 
174
    serverPstreamConnTestSuite().run(indentation + 1);
 
175
 
 
176
}
 
177