| 1 | // |
| 2 | // SocketStreamTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "SocketStreamTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "EchoServer.h" |
| 15 | #include "Poco/Net/SocketStream.h" |
| 16 | #include "Poco/Net/StreamSocket.h" |
| 17 | #include "Poco/Net/ServerSocket.h" |
| 18 | #include "Poco/Net/SocketAddress.h" |
| 19 | #include "Poco/Net/NetException.h" |
| 20 | #include "Poco/Timespan.h" |
| 21 | #include "Poco/Stopwatch.h" |
| 22 | |
| 23 | |
| 24 | using Poco::Net::Socket; |
| 25 | using Poco::Net::SocketStream; |
| 26 | using Poco::Net::StreamSocket; |
| 27 | using Poco::Net::ServerSocket; |
| 28 | using Poco::Net::SocketAddress; |
| 29 | using Poco::Net::ConnectionRefusedException; |
| 30 | using Poco::Timespan; |
| 31 | using Poco::Stopwatch; |
| 32 | using Poco::TimeoutException; |
| 33 | using Poco::InvalidArgumentException; |
| 34 | |
| 35 | |
| 36 | SocketStreamTest::SocketStreamTest(const std::string& name): CppUnit::TestCase(name) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | |
| 41 | SocketStreamTest::~SocketStreamTest() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | |
| 46 | void SocketStreamTest::testStreamEcho() |
| 47 | { |
| 48 | EchoServer echoServer; |
| 49 | StreamSocket ss; |
| 50 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
| 51 | SocketStream str(ss); |
| 52 | str << "hello" ; |
| 53 | assertTrue (str.good()); |
| 54 | str.flush(); |
| 55 | assertTrue (str.good()); |
| 56 | ss.shutdownSend(); |
| 57 | |
| 58 | char buffer[5]; |
| 59 | str.read(buffer, sizeof(buffer)); |
| 60 | assertTrue (str.good()); |
| 61 | assertTrue (str.gcount() == 5); |
| 62 | assertTrue (std::string(buffer, 5) == "hello" ); |
| 63 | |
| 64 | ss.close(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void SocketStreamTest::testLargeStreamEcho() |
| 69 | { |
| 70 | const int msgSize = 64000; |
| 71 | EchoServer echoServer; |
| 72 | StreamSocket ss; |
| 73 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
| 74 | SocketStream str(ss); |
| 75 | ss.setSendBufferSize(msgSize); |
| 76 | ss.setReceiveBufferSize(msgSize); |
| 77 | std::string payload(msgSize, 'x'); |
| 78 | str << payload; |
| 79 | assertTrue (str.good()); |
| 80 | str.flush(); |
| 81 | assertTrue (str.good()); |
| 82 | ss.shutdownSend(); |
| 83 | |
| 84 | assertTrue (str.gcount() == 0); |
| 85 | char buffer[msgSize]; |
| 86 | str.read(buffer, sizeof(buffer)); |
| 87 | assertTrue (str.good()); |
| 88 | assertTrue (str.gcount() == msgSize); |
| 89 | |
| 90 | ss.close(); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void SocketStreamTest::testEOF() |
| 95 | { |
| 96 | StreamSocket ss; |
| 97 | SocketStream str(ss); |
| 98 | { |
| 99 | EchoServer echoServer; |
| 100 | |
| 101 | ss.connect(SocketAddress("127.0.0.1" , echoServer.port())); |
| 102 | str << "hello" ; |
| 103 | assertTrue (str.good()); |
| 104 | str.flush(); |
| 105 | assertTrue (str.good()); |
| 106 | ss.shutdownSend(); |
| 107 | |
| 108 | char buffer[5]; |
| 109 | str.read(buffer, sizeof(buffer)); |
| 110 | assertTrue (str.good()); |
| 111 | assertTrue (str.gcount() == 5); |
| 112 | assertTrue (std::string(buffer, 5) == "hello" ); |
| 113 | } |
| 114 | |
| 115 | int c = str.get(); |
| 116 | assertTrue (c == -1); |
| 117 | assertTrue (str.eof()); |
| 118 | |
| 119 | ss.close(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void SocketStreamTest::setUp() |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void SocketStreamTest::tearDown() |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | |
| 133 | CppUnit::Test* SocketStreamTest::suite() |
| 134 | { |
| 135 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SocketStreamTest" ); |
| 136 | |
| 137 | CppUnit_addTest(pSuite, SocketStreamTest, testStreamEcho); |
| 138 | CppUnit_addTest(pSuite, SocketStreamTest, testLargeStreamEcho); |
| 139 | CppUnit_addTest(pSuite, SocketStreamTest, testEOF); |
| 140 | |
| 141 | return pSuite; |
| 142 | } |
| 143 | |