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
|
/*
* Unit tests for XmlRpc++
*
* Copyright (C) 2017, Zoox Inc
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Austin Hendrix <austin@zoox.com>
* Loosely based on HelloServer.cpp and HelloClient.cpp by Chris Morley
*
*/
#include "xmlrpcpp/XmlRpc.h"
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#ifndef _WIN32
# include <sys/socket.h>
#else
# include <winsock2.h>
#endif
#include <iostream>
#include <functional>
#include <gtest/gtest.h>
#include "test_fixtures.h"
using namespace XmlRpc;
TEST_F(XmlRpcTest, Hello)
{
XmlRpcClient c("localhost", port);
XmlRpcValue noArgs, result;
// Call the Hello method
ASSERT_TRUE(c.execute("Hello", noArgs, result));
EXPECT_FALSE(c.isFault());
XmlRpcValue hello("Hello");
EXPECT_EQ(result, hello);
}
TEST_F(XmlRpcTest, HelloNonBlock)
{
XmlRpcClient c("localhost", port);
XmlRpcValue noArgs, result;
// Call the Hello method, non-blocking
ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
bool done = false;
for (int i = 0; i < 30; i++)
{
done = c.executeCheckDone(result);
if (done)
break;
// run the client's dispatch loop to service the respond when it comes back
c._disp.work(0.1);
}
ASSERT_TRUE(done);
XmlRpcValue hello("Hello");
EXPECT_EQ(result, hello);
}
TEST_F(XmlRpcTest, HelloNonBlock2)
{
XmlRpcClient c("localhost", port);
XmlRpcValue noArgs, result;
// Lock the hello mutex so that the service call cannot return immediately
hello.hello_mutex.lock();
// Call the Hello method, non-blocking
ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
bool done = false;
for (int i = 0; i < 100; i++)
{
done = c.executeCheckDone(result);
if (done)
break;
// run the client's dispatch loop to service the respond when it comes back
c._disp.work(0.1);
// unlock the hello mutex after 10 cycles
if (i == 10)
hello.hello_mutex.unlock();
}
ASSERT_TRUE(done);
XmlRpcValue hello("Hello");
EXPECT_EQ(result, hello);
}
TEST_F(XmlRpcTest, ClientDisconnect)
{
XmlRpcClient* c = new XmlRpcClient("localhost", port);
XmlRpcValue noArgs, result;
// Lock the hello mutex so that the service call cannot return immediately
hello.hello_mutex.lock();
// Call the Hello method, non-blocking
ASSERT_TRUE(c->executeNonBlock("Hello", noArgs));
// Destroy the client before the server can answer
delete c;
// Unlock the mutex so the server can finish
hello.hello_mutex.unlock();
}
TEST_F(XmlRpcTest, ServerDisconnect)
{
XmlRpcClient c("localhost", port);
XmlRpcValue noArgs, result;
XmlRpc::setVerbosity(3);
// Stop calling the work method on the server
server_done = true;
server_thread.join();
// Call the Hello method, non-blocking
ASSERT_TRUE(c.executeNonBlock("Hello", noArgs));
// Destroy the server before it can answer
s.shutdown();
// Run the client to completion
bool done = false;
for (int i = 0; i < 100; i++)
{
done = c.executeCheckDone(result);
if (done)
break;
// run the client's dispatch loop to service the respond when it comes back
c._disp.work(0.1);
}
// The client should return true because the request is done, even though it
// timed out and wasn't able to complete.
EXPECT_TRUE(done);
EXPECT_EQ(-1, c.getfd());
EXPECT_EQ(result, XmlRpcValue()); // Expect empty result
}
TEST_F(XmlRpcTest, ServerDisconnect2)
{
XmlRpcClient c("localhost", port);
XmlRpcValue noArgs, result;
// Stop calling the work method on the server
server_done = true;
server_thread.join();
// Close the server socket to reads (ie incoming connections)
#ifndef _WIN32
shutdown(s.getfd(), SHUT_RD);
#else
shutdown(s.getfd(), SD_RECEIVE);
#endif
// Call the Hello method. Expect failure since the server socket is not
// accepting new connections.
ASSERT_FALSE(c.execute("Hello", noArgs, result));
XmlRpcValue hello; // Expect empty result
EXPECT_EQ(result, hello);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|