Files
poco/Net/testsuite/src/SocketStreamTest.cpp
Aleksandar Fabijanic 6184be0025 tests for GH#169
tests for GH#169: Exceptions due to bug BufferedBidirectionalStreamBuf.h
2013-06-06 21:31:26 -05:00

163 lines
4.1 KiB
C++

//
// SocketStreamTest.cpp
//
// $Id: //poco/1.4/Net/testsuite/src/SocketStreamTest.cpp#1 $
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "SocketStreamTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "EchoServer.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/NetException.h"
#include "Poco/Timespan.h"
#include "Poco/Stopwatch.h"
using Poco::Net::Socket;
using Poco::Net::SocketStream;
using Poco::Net::StreamSocket;
using Poco::Net::ServerSocket;
using Poco::Net::SocketAddress;
using Poco::Net::ConnectionRefusedException;
using Poco::Timespan;
using Poco::Stopwatch;
using Poco::TimeoutException;
using Poco::InvalidArgumentException;
SocketStreamTest::SocketStreamTest(const std::string& name): CppUnit::TestCase(name)
{
}
SocketStreamTest::~SocketStreamTest()
{
}
void SocketStreamTest::testStreamEcho()
{
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
SocketStream str(ss);
str << "hello";
assert (str.good());
str.flush();
assert (str.good());
ss.shutdownSend();
char buffer[5];
str.read(buffer, sizeof(buffer));
assert (str.good());
assert (str.gcount() == 5);
assert (std::string(buffer, 5) == "hello");
ss.close();
}
void SocketStreamTest::testLargeStreamEcho()
{
const int msgSize = 64000;
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("localhost", echoServer.port()));
SocketStream str(ss);
std::string payload(msgSize, 'x');
str << payload;
assert (str.good());
str.flush();
assert (str.good());
ss.shutdownSend();
assert (str.gcount() == 0);
char buffer[msgSize];
str.read(buffer, sizeof(buffer));
assert (str.good());
assert (str.gcount() == msgSize);
ss.close();
}
void SocketStreamTest::testEOF()
{
StreamSocket ss;
SocketStream str(ss);
{
EchoServer echoServer;
ss.connect(SocketAddress("localhost", echoServer.port()));
str << "hello";
assert (str.good());
str.flush();
assert (str.good());
ss.shutdownSend();
char buffer[5];
str.read(buffer, sizeof(buffer));
assert (str.good());
assert (str.gcount() == 5);
assert (std::string(buffer, 5) == "hello");
}
int c = str.get();
assert (c == -1);
assert (str.eof());
ss.close();
}
void SocketStreamTest::setUp()
{
}
void SocketStreamTest::tearDown()
{
}
CppUnit::Test* SocketStreamTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SocketStreamTest");
CppUnit_addTest(pSuite, SocketStreamTest, testStreamEcho);
CppUnit_addTest(pSuite, SocketStreamTest, testLargeStreamEcho);
CppUnit_addTest(pSuite, SocketStreamTest, testEOF);
return pSuite;
}