Update libraries and make it build on windows.

Still gets some warnings because compilers have changed. But should work.
This commit is contained in:
Sandu Liviu Catalin
2025-06-25 22:34:23 +03:00
parent 520a5eacc5
commit f2b7499f85
3038 changed files with 251668 additions and 273857 deletions
@@ -25,6 +25,8 @@
#include "Poco/Net/Session.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Net/SSLException.h"
#include "Poco/Net/AcceptCertificateHandler.h"
#include "Poco/Net/PrivateKeyPassphraseHandler.h"
#include "Poco/Util/Application.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/StreamCopier.h"
@@ -86,6 +88,23 @@ HTTPSClientSessionTest::~HTTPSClientSessionTest()
}
void HTTPSClientSessionTest::testFromSocket()
{
HTTPSTestServer srv;
SecureStreamSocket sss("localhost");
HTTPSClientSession s(sss, "127.0.0.1", srv.port());
HTTPRequest request(HTTPRequest::HTTP_GET, "/small");
s.sendRequest(request);
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
assertTrue (response.getContentType() == "text/plain");
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == HTTPSTestServer::SMALL_BODY);
}
void HTTPSClientSessionTest::testGetSmall()
{
HTTPSTestServer srv;
@@ -285,6 +304,44 @@ void HTTPSClientSessionTest::testKeepAlive()
}
void HTTPSClientSessionTest::testMultipleSSLInit()
{
auto initSSL = []()
{
initializeSSL();
Poco::SharedPtr<InvalidCertificateHandler> ptrCert = new AcceptCertificateHandler(false);
Context::Ptr context(new Context(Context::CLIENT_USE, "", "", "",
Context::VerificationMode::VERIFY_STRICT, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"
)
);
SSLManager::instance().initializeClient(0, ptrCert, context);
};
auto deinitSSL = []()
{
uninitializeSSL();
};
try
{
initSSL();
deinitSSL();
initSSL();
HTTPSClientSession session("secure.appinf.com");
HTTPRequest request(HTTPRequest::HTTP_GET, "", HTTPMessage::HTTP_1_1);
(void)session.sendRequest(request);
deinitSSL();
}
catch(...)
{
failmsg("Double SSL init failed");
}
}
void HTTPSClientSessionTest::testInterop()
{
HTTPSClientSession s("secure.appinf.com");
@@ -418,6 +475,7 @@ void HTTPSClientSessionTest::testUnknownContentLength()
assertTrue (ostr.str() == HTTPSTestServer::SMALL_BODY);
}
void HTTPSClientSessionTest::testServerAbort()
{
HTTPSTestServer srv;
@@ -431,7 +489,7 @@ void HTTPSClientSessionTest::testServerAbort()
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == HTTPSTestServer::SMALL_BODY);
assertTrue ( dynamic_cast<const Poco::Net::SSLConnectionUnexpectedlyClosedException*>(
assertTrue (dynamic_cast<const Poco::Net::SSLConnectionUnexpectedlyClosedException*>(
s.networkException()) != NULL );
}
@@ -450,6 +508,7 @@ CppUnit::Test* HTTPSClientSessionTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSClientSessionTest");
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testFromSocket);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetSmall);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetLarge);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testHead);
@@ -459,6 +518,7 @@ CppUnit::Test* HTTPSClientSessionTest::suite()
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeChunked);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeChunkedKeepAlive);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testKeepAlive);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testMultipleSSLInit);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testInterop);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testProxy);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testCachedSession);
@@ -24,6 +24,7 @@ public:
HTTPSClientSessionTest(const std::string& name);
~HTTPSClientSessionTest();
void testFromSocket();
void testGetSmall();
void testGetLarge();
void testHead();
@@ -34,6 +35,7 @@ public:
void testPostLargeChunkedKeepAlive();
void testKeepAlive();
void testInterop();
void testMultipleSSLInit();
void testProxy();
void testCachedSession();
void testUnknownContentLength();
@@ -103,7 +103,7 @@ void HTTPSStreamFactoryTest::testError()
uri.setPort(server.port());
try
{
std::istream* pStr = factory.open(uri);
factory.open(uri);
fail("not found - must throw");
}
catch (HTTPException& exc)
@@ -9,7 +9,7 @@
#include "NetSSLTestSuite.h"
#include "SecureStreamSocketTestSuite.h"
#include "HTTPSClientTestSuite.h"
#include "TCPServerTestSuite.h"
#include "HTTPSServerTestSuite.h"
@@ -21,6 +21,7 @@ CppUnit::Test* NetSSLTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("OpenSSLTestSuite");
pSuite->addTest(SecureStreamSocketTestSuite::suite());
pSuite->addTest(HTTPSClientTestSuite::suite());
pSuite->addTest(TCPServerTestSuite::suite());
pSuite->addTest(HTTPSServerTestSuite::suite());
@@ -0,0 +1,391 @@
//
// SecureStreamSocketTest.cpp
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "SecureStreamSocketTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/TCPServer.h"
#include "Poco/Net/TCPServerConnection.h"
#include "Poco/Net/TCPServerConnectionFactory.h"
#include "Poco/Net/TCPServerParams.h"
#include "Poco/Net/SecureStreamSocket.h"
#include "Poco/Net/SecureServerSocket.h"
#include "Poco/Net/Context.h"
#include "Poco/Net/RejectCertificateHandler.h"
#include "Poco/Net/AcceptCertificateHandler.h"
#include "Poco/Net/Session.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Util/Application.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/Thread.h"
#include "Poco/File.h"
#include "Poco/TemporaryFile.h"
#include "Poco/FileStream.h"
#include <iostream>
using Poco::Net::TCPServer;
using Poco::Net::TCPServerConnection;
using Poco::Net::TCPServerConnectionFactory;
using Poco::Net::TCPServerConnectionFactoryImpl;
using Poco::Net::TCPServerParams;
using Poco::Net::StreamSocket;
using Poco::Net::SecureStreamSocket;
using Poco::Net::SecureServerSocket;
using Poco::Net::SocketAddress;
using Poco::Net::Context;
using Poco::Net::Session;
using Poco::Net::SSLManager;
using Poco::Thread;
using Poco::Util::Application;
namespace
{
class EchoConnection: public TCPServerConnection
{
public:
EchoConnection(const StreamSocket& s): TCPServerConnection(s)
{
}
void run()
{
StreamSocket& ss = socket();
try
{
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0)
{
ss.sendBytes(buffer, n);
n = ss.receiveBytes(buffer, sizeof(buffer));
}
}
catch (Poco::Exception& exc)
{
std::cerr << "EchoConnection: " << exc.displayText() << std::endl;
}
}
};
class CopyToStringConnection: public TCPServerConnection
{
public:
CopyToStringConnection(const StreamSocket& s):
TCPServerConnection(s)
{
}
void run()
{
{
Poco::FastMutex::ScopedLock lock(_mutex);
_data.clear();
}
StreamSocket& ss = socket();
try
{
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0)
{
{
Poco::FastMutex::ScopedLock lock(_mutex);
_data.append(buffer, n);
}
n = ss.receiveBytes(buffer, sizeof(buffer));
}
}
catch (Poco::Exception& exc)
{
std::cerr << "CopyToStringConnection: " << exc.displayText() << std::endl;
}
}
static const std::string data()
{
Poco::FastMutex::ScopedLock lock(_mutex);
return _data;
}
private:
static Poco::FastMutex _mutex;
static std::string _data;
};
Poco::FastMutex CopyToStringConnection::_mutex;
std::string CopyToStringConnection::_data;
}
SecureStreamSocketTest::SecureStreamSocketTest(const std::string& name): CppUnit::TestCase(name)
{
}
SecureStreamSocketTest::~SecureStreamSocketTest()
{
}
void SecureStreamSocketTest::testSendReceive()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
srv.start();
SocketAddress sa("127.0.0.1", svs.address().port());
SecureStreamSocket ss1(sa);
std::string data("hello, world");
ss1.sendBytes(data.data(), static_cast<int>(data.size()));
char buffer[8192];
int rc = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (rc > 0);
assertTrue (std::string(buffer, rc) == data);
const std::vector<std::size_t> sizes = {67, 467, 7883, 19937};
for (const auto n: sizes)
{
data.assign(n, 'X');
ss1.sendBytes(data.data(), static_cast<int>(data.size()));
std::string received;
while (received.size() < n)
{
rc = ss1.receiveBytes(buffer, sizeof(buffer));
if (rc > 0)
{
received.append(buffer, rc);
}
else if (rc == 0)
{
break;
}
}
assertTrue (received == data);
}
ss1.close();
}
void SecureStreamSocketTest::testPeek()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
srv.start();
SocketAddress sa("127.0.0.1", svs.address().port());
SecureStreamSocket ss(sa);
int n = ss.sendBytes("hello, world!", 13);
assertTrue (n == 13);
char buffer[256];
n = ss.receiveBytes(buffer, 5, MSG_PEEK);
assertTrue (n == 5);
assertTrue (std::string(buffer, n) == "hello");
n = ss.receiveBytes(buffer, sizeof(buffer), MSG_PEEK);
assertTrue (n == 13);
assertTrue (std::string(buffer, n) == "hello, world!");
n = ss.receiveBytes(buffer, 7);
assertTrue (n == 7);
assertTrue (std::string(buffer, n) == "hello, ");
n = ss.receiveBytes(buffer, 6);
assertTrue (n == 6);
assertTrue (std::string(buffer, n) == "world!");
ss.close();
}
void SecureStreamSocketTest::testNB()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
srv.start();
SocketAddress sa("127.0.0.1", svs.address().port());
SecureStreamSocket ss1(sa);
ss1.setBlocking(false);
ss1.setSendBufferSize(32000);
char buffer[8192];
const std::vector<std::size_t> sizes = {67, 467, 7883, 19937};
for (const auto s: sizes)
{
std::string data(s, 'X');
ss1.sendBytes(data.data(), static_cast<int>(data.size()));
int rc;
do
{
rc = ss1.receiveBytes(buffer, sizeof(buffer), MSG_PEEK);
}
while (rc < 0);
assertTrue (rc > 0 && rc <= s);
assertTrue (data.compare(0, rc, buffer, rc) == 0);
std::string received;
while (received.size() < s)
{
rc = ss1.receiveBytes(buffer, sizeof(buffer));
if (rc > 0)
{
received.append(buffer, rc);
}
else if (rc == 0)
{
break;
}
}
assertTrue (received == data);
}
ss1.close();
}
void SecureStreamSocketTest::testSendFile()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<CopyToStringConnection>(), svs);
srv.start();
SecureStreamSocket ss;
ss.connect(SocketAddress("127.0.0.1", srv.port()));
std::string sentData = "Hello, world!";
Poco::TemporaryFile file;
Poco::FileOutputStream ostr(file.path());
ostr.write(sentData.data(), sentData.size());
ostr.close();
Poco::FileInputStream istr(file.path());
std::streamsize n = ss.sendFile(istr);
assertTrue (n == file.getSize());
istr.close();
ss.close();
Poco::Thread::sleep(200);
while (srv.currentConnections() > 0)
{
Poco::Thread::sleep(100);
}
srv.stop();
assertTrue (CopyToStringConnection::data() == sentData);
}
void SecureStreamSocketTest::testSendFileLarge()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<CopyToStringConnection>(), svs);
srv.start();
SecureStreamSocket ss;
ss.connect(SocketAddress("127.0.0.1", srv.port()));
std::string sentData;
Poco::TemporaryFile file;
Poco::FileOutputStream ostr(file.path());
std::string data("0123456789abcdef");
for (int i = 0; i < 10000; i++)
{
ostr.write(data.data(), data.size());
sentData += data;
}
ostr.close();
Poco::FileInputStream istr(file.path());
std::streamsize n = ss.sendFile(istr);
assertTrue (n == file.getSize());
istr.close();
ss.close();
Poco::Thread::sleep(200);
while (srv.currentConnections() > 0)
{
Poco::Thread::sleep(100);
}
srv.stop();
assertTrue (CopyToStringConnection::data() == sentData);
}
void SecureStreamSocketTest::testSendFileRange()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<CopyToStringConnection>(), svs);
srv.start();
SecureStreamSocket ss;
ss.connect(SocketAddress("127.0.0.1", srv.port()));
std::string fileData;
Poco::TemporaryFile file;
Poco::FileOutputStream ostr(file.path());
std::string data("0123456789abcdef");
for (int i = 0; i < 1024; i++)
{
ostr.write(data.data(), data.size());
fileData += data;
}
ostr.close();
const std::streamoff offset = 4000;
const std::streamsize count = 10000;
Poco::FileInputStream istr(file.path());
std::streamsize n = ss.sendFile(istr, offset, count);
assertTrue (n == count);
istr.close();
ss.close();
Poco::Thread::sleep(200);
while (srv.currentConnections() > 0)
{
Poco::Thread::sleep(100);
}
srv.stop();
assertTrue (CopyToStringConnection::data() == fileData.substr(offset, count));
}
void SecureStreamSocketTest::setUp()
{
}
void SecureStreamSocketTest::tearDown()
{
}
CppUnit::Test* SecureStreamSocketTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SecureStreamSocketTest");
CppUnit_addTest(pSuite, SecureStreamSocketTest, testSendReceive);
CppUnit_addTest(pSuite, SecureStreamSocketTest, testPeek);
CppUnit_addTest(pSuite, SecureStreamSocketTest, testNB);
CppUnit_addTest(pSuite, SecureStreamSocketTest, testSendFile);
CppUnit_addTest(pSuite, SecureStreamSocketTest, testSendFileLarge);
CppUnit_addTest(pSuite, SecureStreamSocketTest, testSendFileRange);
return pSuite;
}
@@ -0,0 +1,43 @@
//
// SecureStreamSocketTest.h
//
// Definition of the SecureStreamSocketTest class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SecureStreamSocketTest_INCLUDED
#define SecureStreamSocketTest_INCLUDED
#include "Poco/Net/Net.h"
#include "CppUnit/TestCase.h"
class SecureStreamSocketTest: public CppUnit::TestCase
{
public:
SecureStreamSocketTest(const std::string& name);
~SecureStreamSocketTest();
void testSendReceive();
void testPeek();
void testNB();
void testSendFile();
void testSendFileLarge();
void testSendFileRange();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
};
#endif // SecureStreamSocketTest
@@ -0,0 +1,22 @@
//
// SecureStreamSocketTestSuite.cpp
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "SecureStreamSocketTestSuite.h"
#include "SecureStreamSocketTest.h"
CppUnit::Test* SecureStreamSocketTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SecureStreamSocketTestSuite");
pSuite->addTest(SecureStreamSocketTest::suite());
return pSuite;
}
@@ -0,0 +1,27 @@
//
// SecureStreamSocketTestSuite.h
//
// Definition of the SecureStreamSocketTestSuite class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SecureStreamSocketTestSuite_INCLUDED
#define SecureStreamSocketTestSuite_INCLUDED
#include "CppUnit/TestSuite.h"
class SecureStreamSocketTestSuite
{
public:
static CppUnit::Test* suite();
};
#endif // SecureStreamSocketTestSuite
+15 -5
View File
@@ -25,6 +25,7 @@
#include "Poco/Util/Application.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/Thread.h"
#include "Poco/Mutex.h"
#include <iostream>
@@ -46,6 +47,8 @@ using Poco::Util::Application;
namespace
{
static Poco::FastMutex cerrMutex;
class EchoConnection: public TCPServerConnection
{
public:
@@ -66,8 +69,10 @@ namespace
n = ss.receiveBytes(buffer, sizeof(buffer));
}
}
catch (Poco::Exception& exc)
catch (const Poco::Exception& exc)
{
Poco::FastMutex::ScopedLock l(cerrMutex);
std::cerr << "EchoConnection: " << exc.displayText() << std::endl;
}
}
@@ -324,6 +329,7 @@ void TCPServerTest::testReuseSession()
9,
true,
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
pServerContext->disableProtocols(Context::PROTO_TLSV1_3);
pServerContext->enableSessionCache(true, "TestSuite");
pServerContext->setSessionTimeout(10);
pServerContext->setSessionCacheSize(1000);
@@ -363,6 +369,11 @@ void TCPServerTest::testReuseSession()
assertTrue (srv.totalConnections() == 1);
Session::Ptr pSession = ss1.currentSession();
if (!pSession || !pSession->isResumable())
{
std::cerr << "WARNING: Server did not return a session or session is not resumable. Aborting test." << std::endl;
return;
}
ss1.close();
Thread::sleep(300);
@@ -370,15 +381,15 @@ void TCPServerTest::testReuseSession()
ss1.useSession(pSession);
ss1.connect(sa);
assertTrue (ss1.sessionWasReused());
assertTrue (ss1.currentSession() == pSession);
ss1.sendBytes(data.data(), (int) data.size());
n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (ss1.sessionWasReused());
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 1);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 2);
pSession = ss1.currentSession();
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
@@ -388,10 +399,9 @@ void TCPServerTest::testReuseSession()
ss1.useSession(pSession);
ss1.connect(sa);
assertTrue (!ss1.sessionWasReused());
assertTrue (ss1.currentSession() != pSession);
ss1.sendBytes(data.data(), (int) data.size());
n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (!ss1.sessionWasReused());
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 1);
+141 -6
View File
@@ -23,7 +23,7 @@
#include "Poco/Net/SecureServerSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Thread.h"
#include <iostream>
using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPRequest;
@@ -49,19 +49,20 @@ namespace
try
{
WebSocket ws(request, response);
std::unique_ptr<char[]> pBuffer(new char[_bufSize]);
Poco::Buffer<char> buffer(_bufSize);
int flags;
int n;
do
{
n = ws.receiveFrame(pBuffer.get(), static_cast<int>(_bufSize), flags);
n = ws.receiveFrame(buffer.begin(), static_cast<int>(_bufSize), flags);
if (n == 0)
break;
ws.sendFrame(pBuffer.get(), n, flags);
Poco::Thread::current()->sleep(static_cast<long>(getHandleDelay().totalMilliseconds()));
ws.sendFrame(buffer.begin(), n, flags);
}
while ((flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
}
catch (WebSocketException& exc)
catch (const WebSocketException& exc)
{
switch (exc.code())
{
@@ -79,10 +80,29 @@ namespace
}
}
static void setHandleDelay(Poco::Timespan newDelay)
{
Poco::FastMutex::ScopedLock mutex(_handleDelayMutex);
_handleDelay = newDelay;
}
static Poco::Timespan getHandleDelay()
{
Poco::FastMutex::ScopedLock mutex(_handleDelayMutex);
return _handleDelay;
}
private:
std::size_t _bufSize;
static Poco::FastMutex _handleDelayMutex;
static Poco::Timespan _handleDelay;
};
Poco::FastMutex WebSocketRequestHandler::_handleDelayMutex;
Poco::Timespan WebSocketRequestHandler::_handleDelay;
class WebSocketRequestHandlerFactory: public Poco::Net::HTTPRequestHandlerFactory
{
public:
@@ -90,7 +110,7 @@ namespace
{
}
Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) override
{
return new WebSocketRequestHandler(_bufSize);
}
@@ -111,6 +131,48 @@ WebSocketTest::~WebSocketTest()
}
void WebSocketTest::testWebSocketTimeout()
{
Poco::Net::SecureServerSocket ss(0);
Poco::Net::HTTPServer server(new WebSocketRequestHandlerFactory, ss, new Poco::Net::HTTPServerParams);
server.start();
Poco::Thread::sleep(200);
HTTPSClientSession cs("127.0.0.1", ss.address().port());
HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
HTTPResponse response;
WebSocket ws(cs, request, response);
ws.setSendTimeout( Poco::Timespan(2, 0));
ws.setReceiveTimeout( Poco::Timespan(2, 0));
Poco::Timestamp sendStart;
char buffer[1024] = {};
int flags;
try
{
// Server will take long to process and cause WS timeout
WebSocketRequestHandler::setHandleDelay(Poco::Timespan(3, 0));
std::string payload("x");
ws.sendFrame(payload.data(), (int) payload.size());
ws.receiveFrame(buffer, sizeof(buffer), flags);
failmsg("Data exchange shall time out.");
}
catch (const Poco::TimeoutException&)
{
assertTrue(sendStart.elapsed() < Poco::Timespan(4, 0).totalMicroseconds());
}
ws.shutdown();
ws.receiveFrame(buffer, sizeof(buffer), flags);
server.stop();
WebSocketRequestHandler::setHandleDelay(Poco::Timespan(0));
}
void WebSocketTest::testWebSocket()
{
Poco::Net::SecureServerSocket ss(0);
@@ -172,6 +234,7 @@ void WebSocketTest::testWebSocket()
assertTrue (n == 2);
assertTrue ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_CLOSE);
ws.close();
server.stop();
}
@@ -208,6 +271,76 @@ void WebSocketTest::testWebSocketLarge()
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, 0, n) == 0);
ws.shutdown();
ws.close();
server.stop();
}
void WebSocketTest::testWebSocketNB()
{
Poco::Net::SecureServerSocket ss(0);
Poco::Net::HTTPServer server(new WebSocketRequestHandlerFactory(256*1024), ss, new Poco::Net::HTTPServerParams);
server.start();
Poco::Thread::sleep(200);
HTTPSClientSession cs("127.0.0.1", ss.address().port());
HTTPRequest request(HTTPRequest::HTTP_GET, "/ws", HTTPRequest::HTTP_1_1);
HTTPResponse response;
WebSocket ws(cs, request, response);
ws.setBlocking(false);
int flags;
char buffer[256*1024] = {};
int n = ws.receiveFrame(buffer, sizeof(buffer), flags);
assertTrue (n < 0);
std::string payload("x");
n = ws.sendFrame(payload.data(), (int) payload.size());
assertTrue (n > 0);
if (ws.poll(1000000, Poco::Net::Socket::SELECT_READ))
{
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
while (n < 0)
{
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
}
}
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, n) == 0);
assertTrue (flags == WebSocket::FRAME_TEXT);
ws.setSendBufferSize(256*1024);
ws.setReceiveBufferSize(256*1024);
payload.assign(256000, 'z');
n = ws.sendFrame(payload.data(), (int) payload.size());
assertTrue (n > 0);
if (ws.poll(1000000, Poco::Net::Socket::SELECT_READ))
{
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
while (n < 0)
{
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
}
}
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, n) == 0);
assertTrue (flags == WebSocket::FRAME_TEXT);
n = ws.shutdown();
assertTrue (n > 0);
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
while (n < 0)
{
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
}
assertTrue (n == 2);
assertTrue ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_CLOSE);
ws.close();
server.stop();
}
@@ -227,7 +360,9 @@ CppUnit::Test* WebSocketTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("WebSocketTest");
CppUnit_addTest(pSuite, WebSocketTest, testWebSocket);
CppUnit_addTest(pSuite, WebSocketTest, testWebSocketTimeout);
CppUnit_addTest(pSuite, WebSocketTest, testWebSocketLarge);
CppUnit_addTest(pSuite, WebSocketTest, testWebSocketNB);
return pSuite;
}
+5 -3
View File
@@ -22,13 +22,15 @@ class WebSocketTest: public CppUnit::TestCase
{
public:
WebSocketTest(const std::string& name);
~WebSocketTest();
~WebSocketTest() override;
void testWebSocketTimeout();
void testWebSocket();
void testWebSocketLarge();
void testWebSocketNB();
void setUp();
void tearDown();
void setUp() override;
void tearDown() override;
static CppUnit::Test* suite();
@@ -1,88 +0,0 @@
//
// WinCEDriver.cpp
//
// Console-based test driver for Windows CE.
//
// Copyright (c) 2004-2010, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "CppUnit/TestRunner.h"
#include "NetSSLTestSuite.h"
#include "Poco/Util/Application.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/Net/HTTPSStreamFactory.h"
#include <cstdlib>
class NetSSLApp: public Poco::Util::Application
{
public:
NetSSLApp()
{
Poco::Net::initializeSSL();
Poco::Net::HTTPStreamFactory::registerFactory();
Poco::Net::HTTPSStreamFactory::registerFactory();
}
~NetSSLApp()
{
Poco::Net::uninitializeSSL();
}
int main(const std::vector<std::string>& args)
{
CppUnit::TestRunner runner;
runner.addTest("NetSSLTestSuite", NetSSLTestSuite::suite());
return runner.run(_targs) ? 0 : 1;
}
void setup(const std::vector<std::string>& args)
{
char* argv[] =
{
const_cast<char*>(args[0].c_str())
};
init(1, argv);
for (std::size_t i = 0; i < args.size(); ++i)
_targs.push_back(args[i]);
}
protected:
void initialize(Poco::Util::Application& self)
{
loadConfiguration(); // load default configuration files, if present
Poco::Util::Application::initialize(self);
}
private:
std::vector<std::string> _targs;
};
int _tmain(int argc, wchar_t* argv[])
{
std::vector<std::string> args;
for (int i = 0; i < argc; ++i)
{
char buffer[1024];
std::wcstombs(buffer, argv[i], sizeof(buffer));
args.push_back(std::string(buffer));
}
NetSSLApp app;
try
{
app.setup(args);
return app.run();
}
catch (Poco::Exception& exc)
{
std::cout << exc.displayText() << std::endl;
return 1;
}
}