1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-22 01:57:14 +02:00

Major plugin refactor and cleanup.

Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
This commit is contained in:
Sandu Liviu Catalin
2021-01-30 08:51:39 +02:00
parent e0e34b4030
commit 4a6bfc086c
6219 changed files with 1209835 additions and 454916 deletions

View File

@ -0,0 +1,75 @@
//
// Driver.cpp
//
// Console-based test driver for Poco NetSSL.
//
// Copyright (c) 2006-2014, 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 <iostream>
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(int argc, char** argv)
{
init(1, argv);
for (int i = 0; i < argc; ++i)
_targs.push_back(std::string(argv[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 main(int ac, char **av)
{
NetSSLApp app;
try
{
app.setup(ac, av);
return app.run();
}
catch (Poco::Exception& exc)
{
std::cout << exc.displayText() << std::endl;
return 1;
}
}

View File

@ -0,0 +1,439 @@
//
// HTTPSClientSessionTest.cpp
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HTTPSClientSessionTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/SecureStreamSocket.h"
#include "Poco/Net/Context.h"
#include "Poco/Net/Session.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Util/Application.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/StreamCopier.h"
#include "Poco/Exception.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/Thread.h"
#include "HTTPSTestServer.h"
#include <istream>
#include <ostream>
#include <sstream>
using namespace Poco::Net;
using Poco::Util::Application;
using Poco::StreamCopier;
using Poco::Thread;
class TestRequestHandler: public HTTPRequestHandler
/// Return a HTML document with the current date and time.
{
public:
TestRequestHandler()
{
}
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.setChunkedTransferEncoding(true);
response.setContentType(request.getContentType());
std::ostream& ostr = response.send();
Poco::StreamCopier::copyStream(request.stream(), ostr);
}
};
class TestRequestHandlerFactory: public HTTPRequestHandlerFactory
{
public:
TestRequestHandlerFactory()
{
}
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
return new TestRequestHandler();
}
};
HTTPSClientSessionTest::HTTPSClientSessionTest(const std::string& name): CppUnit::TestCase(name)
{
}
HTTPSClientSessionTest::~HTTPSClientSessionTest()
{
}
void HTTPSClientSessionTest::testGetSmall()
{
HTTPSTestServer srv;
HTTPSClientSession s("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::testGetLarge()
{
HTTPSTestServer srv;
HTTPSClientSession s("127.0.0.1", srv.port());
HTTPRequest request(HTTPRequest::HTTP_GET, "/large");
s.sendRequest(request);
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
assertTrue (response.getContentType() == "text/plain");
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == HTTPSTestServer::LARGE_BODY);
}
void HTTPSClientSessionTest::testHead()
{
HTTPSTestServer srv;
HTTPSClientSession s("127.0.0.1", srv.port());
HTTPRequest request(HTTPRequest::HTTP_HEAD, "/large");
s.sendRequest(request);
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
assertTrue (response.getContentType() == "text/plain");
std::ostringstream ostr;
assertTrue (StreamCopier::copyStream(rs, ostr) == 0);
}
void HTTPSClientSessionTest::testPostSmallIdentity()
{
HTTPSTestServer srv;
HTTPSClientSession s("127.0.0.1", srv.port());
HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
std::string body("this is a random request body\r\n0\r\n");
request.setContentLength((int) body.length());
s.sendRequest(request) << body;
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getContentLength() == body.length());
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == body);
}
void HTTPSClientSessionTest::testPostLargeIdentity()
{
HTTPSTestServer srv;
HTTPSClientSession s("127.0.0.1", srv.port());
HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
std::string body(8000, 'x');
body.append("\r\n0\r\n");
request.setContentLength((int) body.length());
s.sendRequest(request) << body;
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getContentLength() == body.length());
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == body);
}
void HTTPSClientSessionTest::testPostSmallChunked()
{
HTTPSTestServer srv;
HTTPSClientSession s("127.0.0.1", srv.port());
HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
std::string body("this is a random request body");
request.setChunkedTransferEncoding(true);
s.sendRequest(request) << body;
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getChunkedTransferEncoding());
assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == body);
}
void HTTPSClientSessionTest::testPostLargeChunked()
{
HTTPSTestServer srv;
HTTPSClientSession s("127.0.0.1", srv.port());
HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
std::string body(16000, 'x');
request.setChunkedTransferEncoding(true);
s.sendRequest(request) << body;
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getChunkedTransferEncoding());
assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == body);
}
void HTTPSClientSessionTest::testPostLargeChunkedKeepAlive()
{
SecureServerSocket svs(32322);
HTTPServer srv(new TestRequestHandlerFactory(), svs, new HTTPServerParams());
srv.start();
try
{
HTTPSClientSession s("127.0.0.1", srv.port());
s.setKeepAlive(true);
for (int i = 0; i < 10; ++i)
{
HTTPRequest request(HTTPRequest::HTTP_POST, "/keepAlive", HTTPMessage::HTTP_1_1);
std::string body(16000, 'x');
request.setChunkedTransferEncoding(true);
s.sendRequest(request) << body;
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getChunkedTransferEncoding());
assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == body);
}
srv.stop();
}
catch (...)
{
srv.stop();
throw;
}
}
void HTTPSClientSessionTest::testKeepAlive()
{
HTTPSTestServer srv;
HTTPSClientSession s("127.0.0.1", srv.port());
s.setKeepAlive(true);
HTTPRequest request(HTTPRequest::HTTP_HEAD, "/keepAlive", HTTPMessage::HTTP_1_1);
s.sendRequest(request);
HTTPResponse response;
std::istream& rs1 = s.receiveResponse(response);
assertTrue (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
assertTrue (response.getContentType() == "text/plain");
assertTrue (response.getKeepAlive());
std::ostringstream ostr1;
assertTrue (StreamCopier::copyStream(rs1, ostr1) == 0);
request.setMethod(HTTPRequest::HTTP_GET);
request.setURI("/small");
s.sendRequest(request);
std::istream& rs2 = s.receiveResponse(response);
assertTrue (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
assertTrue (response.getKeepAlive());
std::ostringstream ostr2;
StreamCopier::copyStream(rs2, ostr2);
assertTrue (ostr2.str() == HTTPSTestServer::SMALL_BODY);
request.setMethod(HTTPRequest::HTTP_GET);
request.setURI("/large");
s.sendRequest(request);
std::istream& rs3 = s.receiveResponse(response);
assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
assertTrue (response.getChunkedTransferEncoding());
assertTrue (response.getKeepAlive());
std::ostringstream ostr3;
StreamCopier::copyStream(rs3, ostr3);
assertTrue (ostr3.str() == HTTPSTestServer::LARGE_BODY);
request.setMethod(HTTPRequest::HTTP_HEAD);
request.setURI("/large");
s.sendRequest(request);
std::istream& rs4 = s.receiveResponse(response);
assertTrue (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
assertTrue (response.getContentType() == "text/plain");
assertTrue (!response.getKeepAlive());
std::ostringstream ostr4;
assertTrue (StreamCopier::copyStream(rs4, ostr4) == 0);
}
void HTTPSClientSessionTest::testInterop()
{
HTTPSClientSession s("secure.appinf.com");
HTTPRequest request(HTTPRequest::HTTP_GET, "/public/poco/NetSSL.txt");
s.sendRequest(request);
Poco::Net::X509Certificate cert = s.serverCertificate();
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
std::string str(ostr.str());
assertTrue (str == "This is a test file for NetSSL.\n");
std::string commonName;
std::set<std::string> domainNames;
cert.extractNames(commonName, domainNames);
assertTrue (commonName == "secure.appinf.com" || commonName == "*.appinf.com");
assertTrue (domainNames.find("appinf.com") != domainNames.end()
|| domainNames.find("*.appinf.com") != domainNames.end());
}
void HTTPSClientSessionTest::testProxy()
{
HTTPSTestServer srv;
HTTPSClientSession s("secure.appinf.com");
s.setProxy(
Application::instance().config().getString("testsuite.proxy.host"),
Application::instance().config().getInt("testsuite.proxy.port")
);
HTTPRequest request(HTTPRequest::HTTP_GET, "/public/poco/NetSSL.txt");
s.sendRequest(request);
Poco::Net::X509Certificate cert = s.serverCertificate();
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
std::string str(ostr.str());
assertTrue (str == "This is a test file for NetSSL.\n");
assertTrue (cert.commonName() == "secure.appinf.com" || cert.commonName() == "*.appinf.com");
}
void HTTPSClientSessionTest::testCachedSession()
{
// ensure OpenSSL machinery is fully setup
Context::Ptr pDefaultServerContext = SSLManager::instance().defaultServerContext();
Context::Ptr pDefaultClientContext = SSLManager::instance().defaultClientContext();
Context::Ptr pServerContext = new Context(
Context::SERVER_USE,
"");
//pServerContext->enableSessionCache(true, "TestSuite");
//pServerContext->setSessionTimeout(10);
//pServerContext->setSessionCacheSize(1000);
//pServerContext->disableStatelessSessionResumption();
HTTPSTestServer srv(pServerContext);
Context::Ptr pClientContext = new Context(
Context::CLIENT_USE,
"");
//pClientContext->enableSessionCache(true);
HTTPSClientSession s1("127.0.0.1", srv.port(), pClientContext);
HTTPRequest request1(HTTPRequest::HTTP_GET, "/small");
s1.sendRequest(request1);
Session::Ptr pSession1 = s1.sslSession();
HTTPResponse response1;
std::istream& rs1 = s1.receiveResponse(response1);
assertTrue (response1.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
assertTrue (response1.getContentType() == "text/plain");
std::ostringstream ostr1;
StreamCopier::copyStream(rs1, ostr1);
assertTrue (ostr1.str() == HTTPSTestServer::SMALL_BODY);
HTTPSClientSession s2("127.0.0.1", srv.port(), pClientContext, pSession1);
HTTPRequest request2(HTTPRequest::HTTP_GET, "/small");
s2.sendRequest(request2);
Session::Ptr pSession2 = s2.sslSession();
HTTPResponse response2;
std::istream& rs2 = s2.receiveResponse(response2);
assertTrue (response2.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
assertTrue (response2.getContentType() == "text/plain");
std::ostringstream ostr2;
StreamCopier::copyStream(rs2, ostr2);
assertTrue (ostr2.str() == HTTPSTestServer::SMALL_BODY);
assertTrue (pSession1 == pSession2);
HTTPRequest request3(HTTPRequest::HTTP_GET, "/small");
s2.sendRequest(request3);
Session::Ptr pSession3 = s2.sslSession();
HTTPResponse response3;
std::istream& rs3 = s2.receiveResponse(response3);
assertTrue (response3.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
assertTrue (response3.getContentType() == "text/plain");
std::ostringstream ostr3;
StreamCopier::copyStream(rs3, ostr3);
assertTrue (ostr3.str() == HTTPSTestServer::SMALL_BODY);
assertTrue (pSession1 == pSession3);
Thread::sleep(15000); // wait for session to expire
//pServerContext->flushSessionCache();
HTTPRequest request4(HTTPRequest::HTTP_GET, "/small");
s2.sendRequest(request4);
Session::Ptr pSession4 = s2.sslSession();
HTTPResponse response4;
std::istream& rs4 = s2.receiveResponse(response4);
assertTrue (response4.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
assertTrue (response4.getContentType() == "text/plain");
std::ostringstream ostr4;
StreamCopier::copyStream(rs4, ostr4);
assertTrue (ostr4.str() == HTTPSTestServer::SMALL_BODY);
assertTrue (pSession1 != pSession4);
}
void HTTPSClientSessionTest::setUp()
{
}
void HTTPSClientSessionTest::tearDown()
{
}
CppUnit::Test* HTTPSClientSessionTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSClientSessionTest");
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetSmall);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetLarge);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testHead);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostSmallIdentity);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeIdentity);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostSmallChunked);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeChunked);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeChunkedKeepAlive);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testKeepAlive);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testInterop);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testProxy);
//CppUnit_addTest(pSuite, HTTPSClientSessionTest, testCachedSession);
return pSuite;
}

View File

@ -0,0 +1,49 @@
//
// HTTPSClientSessionTest.h
//
// Definition of the HTTPSClientSessionTest class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef HTTPSClientSessionTest_INCLUDED
#define HTTPSClientSessionTest_INCLUDED
#include "Poco/Net/Net.h"
#include "CppUnit/TestCase.h"
class HTTPSClientSessionTest: public CppUnit::TestCase
{
public:
HTTPSClientSessionTest(const std::string& name);
~HTTPSClientSessionTest();
void testGetSmall();
void testGetLarge();
void testHead();
void testPostSmallIdentity();
void testPostLargeIdentity();
void testPostSmallChunked();
void testPostLargeChunked();
void testPostLargeChunkedKeepAlive();
void testKeepAlive();
void testInterop();
void testProxy();
void testCachedSession();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
};
#endif // HTTPSClientSessionTest_INCLUDED

View File

@ -0,0 +1,24 @@
//
// HTTPSClientTestSuite.cpp
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HTTPSClientTestSuite.h"
#include "HTTPSClientSessionTest.h"
#include "HTTPSStreamFactoryTest.h"
CppUnit::Test* HTTPSClientTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSClientTestSuite");
pSuite->addTest(HTTPSClientSessionTest::suite());
pSuite->addTest(HTTPSStreamFactoryTest::suite());
return pSuite;
}

View File

@ -0,0 +1,27 @@
//
// HTTPSClientTestSuite.h
//
// Definition of the HTTPSClientTestSuite class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef HTTPSClientTestSuite_INCLUDED
#define HTTPSClientTestSuite_INCLUDED
#include "CppUnit/TestSuite.h"
class HTTPSClientTestSuite
{
public:
static CppUnit::Test* suite();
};
#endif // HTTPSClientTestSuite_INCLUDED

View File

@ -0,0 +1,351 @@
//
// HTTPSServerTest.cpp
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HTTPSServerTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/SecureServerSocket.h"
#include "Poco/StreamCopier.h"
#include <sstream>
using Poco::Net::Context;
using Poco::Net::HTTPServer;
using Poco::Net::HTTPServerParams;
using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPRequestHandlerFactory;
using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPServerResponse;
using Poco::Net::HTTPMessage;
using Poco::Net::SecureServerSocket;
using Poco::StreamCopier;
namespace
{
class EchoBodyRequestHandler: public HTTPRequestHandler
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
if (request.getChunkedTransferEncoding())
response.setChunkedTransferEncoding(true);
else if (request.getContentLength() != HTTPMessage::UNKNOWN_CONTENT_LENGTH)
response.setContentLength(request.getContentLength());
response.setContentType(request.getContentType());
std::istream& istr = request.stream();
std::ostream& ostr = response.send();
StreamCopier::copyStream(istr, ostr);
}
};
class EchoHeaderRequestHandler: public HTTPRequestHandler
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
std::ostringstream osstr;
request.write(osstr);
int n = (int) osstr.str().length();
response.setContentLength(n);
std::ostream& ostr = response.send();
if (request.getMethod() != HTTPRequest::HTTP_HEAD)
request.write(ostr);
}
};
class RedirectRequestHandler: public HTTPRequestHandler
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.redirect("http://www.appinf.com/");
}
};
class AuthRequestHandler: public HTTPRequestHandler
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.requireAuthentication("/auth");
response.send();
}
};
class RequestHandlerFactory: public HTTPRequestHandlerFactory
{
public:
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
if (request.getURI() == "/echoBody")
return new EchoBodyRequestHandler;
else if (request.getURI() == "/echoHeader")
return new EchoHeaderRequestHandler;
else if (request.getURI() == "/redirect")
return new RedirectRequestHandler();
else if (request.getURI() == "/auth")
return new AuthRequestHandler();
else
return 0;
}
};
}
HTTPSServerTest::HTTPSServerTest(const std::string& name): CppUnit::TestCase(name)
{
}
HTTPSServerTest::~HTTPSServerTest()
{
}
void HTTPSServerTest::testIdentityRequest()
{
SecureServerSocket svs(0);
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setKeepAlive(false);
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
srv.start();
HTTPSClientSession cs("127.0.0.1", svs.address().port());
std::string body(5000, 'x');
HTTPRequest request("POST", "/echoBody");
request.setContentLength((int) body.length());
request.setContentType("text/plain");
cs.sendRequest(request) << body;
HTTPResponse response;
std::string rbody;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getContentLength() == body.size());
assertTrue (response.getContentType() == "text/plain");
assertTrue (rbody == body);
}
void HTTPSServerTest::testChunkedRequest()
{
SecureServerSocket svs(0);
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setKeepAlive(false);
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
srv.start();
HTTPSClientSession cs("127.0.0.1", svs.address().port());
std::string body(5000, 'x');
HTTPRequest request("POST", "/echoBody");
request.setContentType("text/plain");
request.setChunkedTransferEncoding(true);
cs.sendRequest(request) << body;
HTTPResponse response;
std::string rbody;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
assertTrue (response.getContentType() == "text/plain");
assertTrue (response.getChunkedTransferEncoding());
assertTrue (rbody == body);
}
void HTTPSServerTest::testIdentityRequestKeepAlive()
{
SecureServerSocket svs(0);
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setKeepAlive(true);
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
srv.start();
HTTPSClientSession cs("127.0.0.1", svs.address().port());
cs.setKeepAlive(true);
std::string body(5000, 'x');
HTTPRequest request("POST", "/echoBody", HTTPMessage::HTTP_1_1);
request.setContentLength((int) body.length());
request.setContentType("text/plain");
cs.sendRequest(request) << body;
HTTPResponse response;
std::string rbody;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getContentLength() == body.size());
assertTrue (response.getContentType() == "text/plain");
assertTrue (response.getKeepAlive());
assertTrue (rbody == body);
body.assign(1000, 'y');
request.setContentLength((int) body.length());
request.setKeepAlive(false);
cs.sendRequest(request) << body;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getContentLength() == body.size());
assertTrue (response.getContentType() == "text/plain");
assertTrue (!response.getKeepAlive());
assertTrue (rbody == body);
}
void HTTPSServerTest::testChunkedRequestKeepAlive()
{
SecureServerSocket svs(0);
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setKeepAlive(true);
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
srv.start();
HTTPSClientSession cs("127.0.0.1", svs.address().port());
cs.setKeepAlive(true);
std::string body(5000, 'x');
HTTPRequest request("POST", "/echoBody", HTTPMessage::HTTP_1_1);
request.setContentType("text/plain");
request.setChunkedTransferEncoding(true);
cs.sendRequest(request) << body;
HTTPResponse response;
std::string rbody;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
assertTrue (response.getContentType() == "text/plain");
assertTrue (response.getChunkedTransferEncoding());
assertTrue (rbody == body);
body.assign(1000, 'y');
request.setKeepAlive(false);
cs.sendRequest(request) << body;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
assertTrue (response.getContentType() == "text/plain");
assertTrue (response.getChunkedTransferEncoding());
assertTrue (!response.getKeepAlive());
assertTrue (rbody == body);
}
void HTTPSServerTest::test100Continue()
{
SecureServerSocket svs(0);
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setKeepAlive(false);
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
srv.start();
HTTPSClientSession cs("127.0.0.1", svs.address().port());
std::string body(5000, 'x');
HTTPRequest request("POST", "/echoBody");
request.setContentLength((int) body.length());
request.setContentType("text/plain");
request.set("Expect", "100-Continue");
cs.sendRequest(request) << body;
HTTPResponse response;
std::string rbody;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getContentLength() == body.size());
assertTrue (response.getContentType() == "text/plain");
assertTrue (rbody == body);
}
void HTTPSServerTest::testRedirect()
{
SecureServerSocket svs(0);
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setKeepAlive(false);
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
srv.start();
HTTPSClientSession cs("127.0.0.1", svs.address().port());
HTTPRequest request("GET", "/redirect");
cs.sendRequest(request);
HTTPResponse response;
std::string rbody;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getStatus() == HTTPResponse::HTTP_FOUND);
assertTrue (response.get("Location") == "http://www.appinf.com/");
assertTrue (rbody.empty());
}
void HTTPSServerTest::testAuth()
{
SecureServerSocket svs(0);
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setKeepAlive(false);
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
srv.start();
HTTPSClientSession cs("127.0.0.1", svs.address().port());
HTTPRequest request("GET", "/auth");
cs.sendRequest(request);
HTTPResponse response;
std::string rbody;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED);
assertTrue (response.get("WWW-Authenticate") == "Basic realm=\"/auth\"");
assertTrue (rbody.empty());
}
void HTTPSServerTest::testNotImpl()
{
SecureServerSocket svs(0);
HTTPServerParams* pParams = new HTTPServerParams;
pParams->setKeepAlive(false);
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
srv.start();
HTTPSClientSession cs("127.0.0.1", svs.address().port());
HTTPRequest request("GET", "/notImpl");
cs.sendRequest(request);
HTTPResponse response;
std::string rbody;
cs.receiveResponse(response) >> rbody;
assertTrue (response.getStatus() == HTTPResponse::HTTP_NOT_IMPLEMENTED);
assertTrue (rbody.empty());
}
void HTTPSServerTest::setUp()
{
}
void HTTPSServerTest::tearDown()
{
}
CppUnit::Test* HTTPSServerTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSServerTest");
CppUnit_addTest(pSuite, HTTPSServerTest, testIdentityRequest);
CppUnit_addTest(pSuite, HTTPSServerTest, testChunkedRequest);
CppUnit_addTest(pSuite, HTTPSServerTest, testIdentityRequestKeepAlive);
CppUnit_addTest(pSuite, HTTPSServerTest, testChunkedRequestKeepAlive);
CppUnit_addTest(pSuite, HTTPSServerTest, test100Continue);
CppUnit_addTest(pSuite, HTTPSServerTest, testRedirect);
CppUnit_addTest(pSuite, HTTPSServerTest, testAuth);
CppUnit_addTest(pSuite, HTTPSServerTest, testNotImpl);
return pSuite;
}

View File

@ -0,0 +1,45 @@
//
// HTTPSServerTest.h
//
// Definition of the HTTPSServerTest class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef HTTPSServerTest_INCLUDED
#define HTTPSServerTest_INCLUDED
#include "Poco/Net/Net.h"
#include "CppUnit/TestCase.h"
class HTTPSServerTest: public CppUnit::TestCase
{
public:
HTTPSServerTest(const std::string& name);
~HTTPSServerTest();
void testIdentityRequest();
void testChunkedRequest();
void testIdentityRequestKeepAlive();
void testChunkedRequestKeepAlive();
void test100Continue();
void testRedirect();
void testAuth();
void testNotImpl();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
};
#endif // HTTPSServerTest_INCLUDED

View File

@ -0,0 +1,22 @@
//
// HTTPSServerTestSuite.cpp
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HTTPSServerTestSuite.h"
#include "HTTPSServerTest.h"
CppUnit::Test* HTTPSServerTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSServerTestSuite");
pSuite->addTest(HTTPSServerTest::suite());
return pSuite;
}

View File

@ -0,0 +1,27 @@
//
// HTTPSServerTestSuite.h
//
// Definition of the HTTPSServerTestSuite class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef HTTPSServerTestSuite_INCLUDED
#define HTTPSServerTestSuite_INCLUDED
#include "CppUnit/TestSuite.h"
class HTTPSServerTestSuite
{
public:
static CppUnit::Test* suite();
};
#endif // HTTPSServerTestSuite_INCLUDED

View File

@ -0,0 +1,137 @@
//
// HTTPSStreamFactoryTest.cpp
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HTTPSStreamFactoryTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/HTTPSStreamFactory.h"
#include "Poco/Net/NetException.h"
#include "Poco/Util/Application.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include "Poco/StreamCopier.h"
#include "HTTPSTestServer.h"
#include <sstream>
#include <memory>
using Poco::Net::HTTPSStreamFactory;
using Poco::Net::NetException;
using Poco::Net::HTTPException;
using Poco::Util::Application;
using Poco::URI;
using Poco::StreamCopier;
HTTPSStreamFactoryTest::HTTPSStreamFactoryTest(const std::string& name): CppUnit::TestCase(name)
{
}
HTTPSStreamFactoryTest::~HTTPSStreamFactoryTest()
{
}
void HTTPSStreamFactoryTest::testNoRedirect()
{
HTTPSTestServer server;
HTTPSStreamFactory factory;
URI uri("https://127.0.0.1/large");
uri.setPort(server.port());
std::unique_ptr<std::istream> pStr(factory.open(uri));
std::ostringstream ostr;
StreamCopier::copyStream(*pStr.get(), ostr);
assertTrue (ostr.str() == HTTPSTestServer::LARGE_BODY);
}
void HTTPSStreamFactoryTest::testEmptyPath()
{
HTTPSTestServer server;
HTTPSStreamFactory factory;
URI uri("https://127.0.0.1");
uri.setPort(server.port());
std::unique_ptr<std::istream> pStr(factory.open(uri));
std::ostringstream ostr;
StreamCopier::copyStream(*pStr.get(), ostr);
assertTrue (ostr.str() == HTTPSTestServer::SMALL_BODY);
}
void HTTPSStreamFactoryTest::testRedirect()
{
HTTPSTestServer server;
HTTPSStreamFactory factory;
URI uri("https://127.0.0.1/redirect");
uri.setPort(server.port());
std::unique_ptr<std::istream> pStr(factory.open(uri));
std::ostringstream ostr;
StreamCopier::copyStream(*pStr.get(), ostr);
assertTrue (ostr.str() == HTTPSTestServer::LARGE_BODY);
}
void HTTPSStreamFactoryTest::testProxy()
{
HTTPSTestServer server;
HTTPSStreamFactory factory(
Application::instance().config().getString("testsuite.proxy.host"),
Application::instance().config().getInt("testsuite.proxy.port")
);
URI uri("https://secure.appinf.com/public/poco/NetSSL.txt");
std::unique_ptr<std::istream> pStr(factory.open(uri));
std::ostringstream ostr;
StreamCopier::copyStream(*pStr.get(), ostr);
assertTrue (ostr.str().length() > 0);
}
void HTTPSStreamFactoryTest::testError()
{
HTTPSTestServer server;
HTTPSStreamFactory factory;
URI uri("https://127.0.0.1/notfound");
uri.setPort(server.port());
try
{
std::istream* pStr = factory.open(uri);
fail("not found - must throw");
}
catch (HTTPException& exc)
{
std::string m = exc.displayText();
}
}
void HTTPSStreamFactoryTest::setUp()
{
}
void HTTPSStreamFactoryTest::tearDown()
{
}
CppUnit::Test* HTTPSStreamFactoryTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSStreamFactoryTest");
CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testNoRedirect);
CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testEmptyPath);
CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testRedirect);
CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testProxy);
CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testError);
return pSuite;
}

View File

@ -0,0 +1,42 @@
//
// HTTPSStreamFactoryTest.h
//
// Definition of the HTTPSStreamFactoryTest class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef HTTPSStreamFactoryTest_INCLUDED
#define HTTPSStreamFactoryTest_INCLUDED
#include "Poco/Net/Net.h"
#include "CppUnit/TestCase.h"
class HTTPSStreamFactoryTest: public CppUnit::TestCase
{
public:
HTTPSStreamFactoryTest(const std::string& name);
~HTTPSStreamFactoryTest();
void testNoRedirect();
void testEmptyPath();
void testRedirect();
void testProxy();
void testError();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
};
#endif // HTTPSStreamFactoryTest_INCLUDED

View File

@ -0,0 +1,230 @@
//
// HTTPSTestServer.cpp
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HTTPSTestServer.h"
#include "Poco/Net/SecureStreamSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timespan.h"
#include "Poco/NumberFormatter.h"
#include <iostream>
using Poco::Net::Socket;
using Poco::Net::StreamSocket;
using Poco::Net::SecureStreamSocket;
using Poco::Net::SecureServerSocket;
using Poco::Net::SocketAddress;
using Poco::NumberFormatter;
const std::string HTTPSTestServer::SMALL_BODY("This is some random text data returned by the server");
const std::string HTTPSTestServer::LARGE_BODY(4000, 'x');
HTTPSTestServer::HTTPSTestServer():
_socket(SocketAddress()),
_thread("HTTPSTestServer"),
_stop(false)
{
_thread.start(*this);
_ready.wait();
_lastRequest.reserve(4000);
}
HTTPSTestServer::HTTPSTestServer(Poco::Net::Context::Ptr pContext):
_socket(SocketAddress(), 64, pContext),
_thread("HTTPSTestServer"),
_stop(false)
{
_thread.start(*this);
_ready.wait();
_lastRequest.reserve(4000);
}
HTTPSTestServer::~HTTPSTestServer()
{
_stop = true;
_thread.join();
}
Poco::UInt16 HTTPSTestServer::port() const
{
return _socket.address().port();
}
const std::string& HTTPSTestServer::lastRequest() const
{
return _lastRequest;
}
void HTTPSTestServer::run()
{
_ready.set();
Poco::Timespan span(250000);
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
StreamSocket ss = _socket.acceptConnection();
try
{
_lastRequest.clear();
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0 && !_stop)
{
_lastRequest.append(buffer, n);
if (!requestComplete())
n = ss.receiveBytes(buffer, sizeof(buffer));
else
n = 0;
}
std::string response = handleRequest();
ss.sendBytes(response.data(), (int) response.size());
Poco::Thread::sleep(1000);
}
catch (Poco::Exception& exc)
{
std::cerr << "HTTPSTestServer: " << exc.displayText() << std::endl;
}
}
}
}
bool HTTPSTestServer::requestComplete() const
{
return ((_lastRequest.substr(0, 3) == "GET" || _lastRequest.substr(0, 4) == "HEAD") &&
(_lastRequest.find("\r\n\r\n") != std::string::npos)) ||
(_lastRequest.find("\r\n0\r\n") != std::string::npos);
}
std::string HTTPSTestServer::handleRequest() const
{
std::string response;
response.reserve(16000);
if (_lastRequest.substr(0, 10) == "GET /small" ||
_lastRequest.substr(0, 11) == "HEAD /small")
{
std::string body(SMALL_BODY);
response.append("HTTP/1.0 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
response.append("Connection: Close\r\n");
response.append("\r\n");
if (_lastRequest.substr(0, 3) == "GET")
response.append(body);
}
else if (_lastRequest.substr(0, 10) == "GET /large" ||
_lastRequest.substr(0, 11) == "HEAD /large" ||
_lastRequest.substr(0, 36) == "GET http://www.somehost.com:80/large")
{
std::string body(LARGE_BODY);
response.append("HTTP/1.0 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
response.append("Connection: Close\r\n");
response.append("\r\n");
if (_lastRequest.substr(0, 3) == "GET")
response.append(body);
}
else if (_lastRequest.substr(0, 4) == "POST")
{
std::string::size_type pos = _lastRequest.find("\r\n\r\n");
pos += 4;
std::string body = _lastRequest.substr(pos);
response.append("HTTP/1.0 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
if (_lastRequest.find("Content-Length") != std::string::npos)
{
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
}
else if (_lastRequest.find("chunked") != std::string::npos)
{
response.append("Transfer-Encoding: chunked\r\n");
}
if (_lastRequest.substr(0,15) == "POST /keepAlive")
response.append("Connection: keep-alive\r\n");
else
response.append("Connection: Close\r\n");
response.append("\r\n");
response.append(body);
}
else if (_lastRequest.substr(0, 15) == "HEAD /keepAlive")
{
std::string body(SMALL_BODY);
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: keep-alive\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n\r\n");
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: Keep-Alive\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n\r\n");
response.append(body);
body = LARGE_BODY;
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: keep-alive\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Transfer-Encoding: chunked\r\n\r\n");
response.append(NumberFormatter::formatHex((unsigned) body.length()));
response.append("\r\n");
response.append(body);
response.append("\r\n0\r\n\r\n");
response.append("HTTP/1.1 200 OK\r\n");
response.append("Connection: close\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n\r\n");
}
else if (_lastRequest.substr(0, 13) == "GET /redirect")
{
response.append("HTTP/1.0 302 Found\r\n");
response.append("Location: /large\r\n");
response.append("\r\n");
}
else if (_lastRequest.substr(0, 13) == "GET /notfound")
{
response.append("HTTP/1.0 404 Not Found\r\n");
response.append("\r\n");
}
else if (_lastRequest.substr(0, 5) == "GET /" ||
_lastRequest.substr(0, 6) == "HEAD /")
{
std::string body(SMALL_BODY);
response.append("HTTP/1.0 200 OK\r\n");
response.append("Content-Type: text/plain\r\n");
response.append("Content-Length: ");
response.append(NumberFormatter::format((int) body.size()));
response.append("\r\n");
response.append("Connection: Close\r\n");
response.append("\r\n");
if (_lastRequest.substr(0, 3) == "GET")
response.append(body);
}
return response;
}

View File

@ -0,0 +1,62 @@
//
// HTTPSTestServer.h
//
// Definition of the HTTPSTestServer class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef HTTPSTestServer_INCLUDED
#define HTTPSTestServer_INCLUDED
#include "Poco/Net/Net.h"
#include "Poco/Net/SecureServerSocket.h"
#include "Poco/Thread.h"
#include "Poco/Event.h"
class HTTPSTestServer: public Poco::Runnable
/// A simple sequential echo server.
{
public:
HTTPSTestServer();
/// Creates the HTTPSTestServer.
explicit HTTPSTestServer(Poco::Net::Context::Ptr pContext);
/// Creates the HTTPSTestServer using the given Context.
~HTTPSTestServer();
/// Destroys the HTTPSTestServer.
Poco::UInt16 port() const;
/// Returns the port the echo server is
/// listening on.
void run();
/// Does the work.
const std::string& lastRequest() const;
/// Returns the last request.
static const std::string SMALL_BODY;
static const std::string LARGE_BODY;
protected:
bool requestComplete() const;
std::string handleRequest() const;
private:
Poco::Net::SecureServerSocket _socket;
Poco::Thread _thread;
Poco::Event _ready;
bool _stop;
std::string _lastRequest;
};
#endif // HTTPSTestServer_INCLUDED

View File

@ -0,0 +1,27 @@
//
// NetSSLTestSuite.cpp
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "NetSSLTestSuite.h"
#include "HTTPSClientTestSuite.h"
#include "TCPServerTestSuite.h"
#include "HTTPSServerTestSuite.h"
CppUnit::Test* NetSSLTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NetSSLTestSuite");
pSuite->addTest(HTTPSClientTestSuite::suite());
pSuite->addTest(TCPServerTestSuite::suite());
pSuite->addTest(HTTPSServerTestSuite::suite());
return pSuite;
}

View File

@ -0,0 +1,27 @@
//
// NetSSLTestSuite.h
//
// Definition of the NetSSLTestSuite class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef NetSSLTestSuite_INCLUDED
#define NetSSLTestSuite_INCLUDED
#include "CppUnit/TestSuite.h"
class NetSSLTestSuite
{
public:
static CppUnit::Test* suite();
};
#endif // NetSSLTestSuite_INCLUDED

View File

@ -0,0 +1,393 @@
//
// TCPServerTest.cpp
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "TCPServerTest.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/Session.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Util/Application.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/Thread.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;
}
}
};
}
TCPServerTest::TCPServerTest(const std::string& name): CppUnit::TestCase(name)
{
}
TCPServerTest::~TCPServerTest()
{
}
void TCPServerTest::testOneConnection()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
srv.start();
assertTrue (srv.currentConnections() == 0);
assertTrue (srv.currentThreads() == 0);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 0);
SocketAddress sa("127.0.0.1", svs.address().port());
SecureStreamSocket ss1(sa);
std::string data("hello, world");
ss1.sendBytes(data.data(), (int) data.size());
char buffer[256];
int n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 1);
assertTrue (srv.currentThreads() == 1);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 1);
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
}
void TCPServerTest::testTwoConnections()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
srv.start();
assertTrue (srv.currentConnections() == 0);
assertTrue (srv.currentThreads() == 0);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 0);
SocketAddress sa("127.0.0.1", svs.address().port());
SecureStreamSocket ss1(sa);
SecureStreamSocket ss2(sa);
std::string data("hello, world");
ss1.sendBytes(data.data(), (int) data.size());
ss2.sendBytes(data.data(), (int) data.size());
char buffer[256];
int n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
n = ss2.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 2);
assertTrue (srv.currentThreads() == 2);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 2);
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 1);
assertTrue (srv.currentThreads() == 1);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 2);
ss2.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
}
void TCPServerTest::testMultiConnections()
{
SecureServerSocket svs(0);
TCPServerParams* pParams = new TCPServerParams;
pParams->setMaxThreads(4);
pParams->setMaxQueued(4);
pParams->setThreadIdleTime(100);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs, pParams);
srv.start();
assertTrue (srv.currentConnections() == 0);
assertTrue (srv.currentThreads() == 0);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 0);
SocketAddress sa("127.0.0.1", svs.address().port());
SecureStreamSocket ss1(sa);
SecureStreamSocket ss2(sa);
SecureStreamSocket ss3(sa);
SecureStreamSocket ss4(sa);
std::string data("hello, world");
ss1.sendBytes(data.data(), (int) data.size());
ss2.sendBytes(data.data(), (int) data.size());
ss3.sendBytes(data.data(), (int) data.size());
ss4.sendBytes(data.data(), (int) data.size());
char buffer[256];
int n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
n = ss2.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
n = ss3.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
n = ss4.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 4);
assertTrue (srv.currentThreads() == 4);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 4);
SecureStreamSocket ss5;
ss5.setLazyHandshake();
ss5.connect(sa);
Thread::sleep(200);
assertTrue (srv.queuedConnections() == 1);
SecureStreamSocket ss6;
ss6.setLazyHandshake();
ss6.connect(sa);
Thread::sleep(200);
assertTrue (srv.queuedConnections() == 2);
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 4);
assertTrue (srv.currentThreads() == 4);
assertTrue (srv.queuedConnections() == 1);
assertTrue (srv.totalConnections() == 5);
ss2.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 4);
assertTrue (srv.currentThreads() == 4);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 6);
ss3.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 3);
assertTrue (srv.currentThreads() == 3);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 6);
ss4.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 2);
assertTrue (srv.currentThreads() == 2);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 6);
ss5.close();
ss6.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
}
void TCPServerTest::testReuseSocket()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
srv.start();
assertTrue (srv.currentConnections() == 0);
assertTrue (srv.currentThreads() == 0);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 0);
SocketAddress sa("127.0.0.1", svs.address().port());
SecureStreamSocket ss1(sa);
std::string data("hello, world");
ss1.sendBytes(data.data(), (int) data.size());
char buffer[256];
int n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 1);
assertTrue (srv.currentThreads() == 1);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 1);
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
ss1.connect(sa);
ss1.sendBytes(data.data(), (int) data.size());
n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 1);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 2);
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
}
void TCPServerTest::testReuseSession()
{
// ensure SSL machinery is fully setup
Context::Ptr pDefaultServerContext = SSLManager::instance().defaultServerContext();
Context::Ptr pDefaultClientContext = SSLManager::instance().defaultClientContext();
Context::Ptr pServerContext = new Context(
Context::SERVER_USE,
"test.appinf.com");
//pServerContext->enableSessionCache(true, "TestSuite");
//pServerContext->setSessionTimeout(10);
//pServerContext->setSessionCacheSize(1000);
//pServerContext->disableStatelessSessionResumption();
SecureServerSocket svs(0, 64, pServerContext);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
srv.start();
assertTrue (srv.currentConnections() == 0);
assertTrue (srv.currentThreads() == 0);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 0);
Context::Ptr pClientContext = new Context(
Context::CLIENT_USE,
"");
//pClientContext->enableSessionCache(true);
SocketAddress sa("127.0.0.1", svs.address().port());
SecureStreamSocket ss1(sa, pClientContext);
assertTrue (!ss1.sessionWasReused());
std::string data("hello, world");
ss1.sendBytes(data.data(), (int) data.size());
char buffer[256];
int n = ss1.receiveBytes(buffer, sizeof(buffer));
assertTrue (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 1);
assertTrue (srv.currentThreads() == 1);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 1);
Session::Ptr pSession = ss1.currentSession();
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
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 (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 1);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 2);
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
Thread::sleep(15000); // wait for session to expire
//pServerContext->flushSessionCache();
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 (n > 0);
assertTrue (std::string(buffer, n) == data);
assertTrue (srv.currentConnections() == 1);
assertTrue (srv.queuedConnections() == 0);
assertTrue (srv.totalConnections() == 3);
ss1.close();
Thread::sleep(300);
assertTrue (srv.currentConnections() == 0);
}
void TCPServerTest::setUp()
{
}
void TCPServerTest::tearDown()
{
}
CppUnit::Test* TCPServerTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TCPServerTest");
CppUnit_addTest(pSuite, TCPServerTest, testOneConnection);
CppUnit_addTest(pSuite, TCPServerTest, testTwoConnections);
CppUnit_addTest(pSuite, TCPServerTest, testMultiConnections);
CppUnit_addTest(pSuite, TCPServerTest, testReuseSocket);
//CppUnit_addTest(pSuite, TCPServerTest, testReuseSession);
return pSuite;
}

View File

@ -0,0 +1,42 @@
//
// TCPServerTest.h
//
// Definition of the TCPServerTest class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef TCPServerTest_INCLUDED
#define TCPServerTest_INCLUDED
#include "Poco/Net/Net.h"
#include "CppUnit/TestCase.h"
class TCPServerTest: public CppUnit::TestCase
{
public:
TCPServerTest(const std::string& name);
~TCPServerTest();
void testOneConnection();
void testTwoConnections();
void testMultiConnections();
void testReuseSocket();
void testReuseSession();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
};
#endif // TCPServerTest_INCLUDED

View File

@ -0,0 +1,22 @@
//
// TCPServerTestSuite.cpp
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "TCPServerTestSuite.h"
#include "TCPServerTest.h"
CppUnit::Test* TCPServerTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TCPServerTestSuite");
pSuite->addTest(TCPServerTest::suite());
return pSuite;
}

View File

@ -0,0 +1,27 @@
//
// TCPServerTestSuite.h
//
// Definition of the TCPServerTestSuite class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef TCPServerTestSuite_INCLUDED
#define TCPServerTestSuite_INCLUDED
#include "CppUnit/TestSuite.h"
class TCPServerTestSuite
{
public:
static CppUnit::Test* suite();
};
#endif // TCPServerTestSuite_INCLUDED

View File

@ -0,0 +1,231 @@
//
// WebSocketTest.cpp
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "WebSocketTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/SecureServerSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Thread.h"
using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPServerResponse;
using Poco::Net::SocketStream;
using Poco::Net::WebSocket;
using Poco::Net::WebSocketException;
namespace
{
class WebSocketRequestHandler: public Poco::Net::HTTPRequestHandler
{
public:
WebSocketRequestHandler(std::size_t bufSize = 1024): _bufSize(bufSize)
{
}
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
try
{
WebSocket ws(request, response);
std::unique_ptr<char[]> pBuffer(new char[_bufSize]);
int flags;
int n;
do
{
n = ws.receiveFrame(pBuffer.get(), static_cast<int>(_bufSize), flags);
if (n == 0)
break;
ws.sendFrame(pBuffer.get(), n, flags);
}
while ((flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
}
catch (WebSocketException& exc)
{
switch (exc.code())
{
case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
// fallthrough
case WebSocket::WS_ERR_NO_HANDSHAKE:
case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
response.send();
break;
}
}
}
private:
std::size_t _bufSize;
};
class WebSocketRequestHandlerFactory: public Poco::Net::HTTPRequestHandlerFactory
{
public:
WebSocketRequestHandlerFactory(std::size_t bufSize = 1024): _bufSize(bufSize)
{
}
Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
return new WebSocketRequestHandler(_bufSize);
}
private:
std::size_t _bufSize;
};
}
WebSocketTest::WebSocketTest(const std::string& name): CppUnit::TestCase(name)
{
}
WebSocketTest::~WebSocketTest()
{
}
void WebSocketTest::testWebSocket()
{
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);
std::string payload("x");
ws.sendFrame(payload.data(), (int) payload.size());
char buffer[1024] = {};
int flags;
int n = ws.receiveFrame(buffer, sizeof(buffer), flags);
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, 0, n) == 0);
assertTrue (flags == WebSocket::FRAME_TEXT);
for (int i = 2; i < 20; i++)
{
payload.assign(i, 'x');
ws.sendFrame(payload.data(), (int) payload.size());
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, 0, n) == 0);
assertTrue (flags == WebSocket::FRAME_TEXT);
}
for (int i = 125; i < 129; i++)
{
payload.assign(i, 'x');
ws.sendFrame(payload.data(), (int) payload.size());
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, 0, n) == 0);
assertTrue (flags == WebSocket::FRAME_TEXT);
}
payload = "Hello, world!";
ws.sendFrame(payload.data(), (int) payload.size());
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, 0, n) == 0);
assertTrue (flags == WebSocket::FRAME_TEXT);
payload = "Hello, universe!";
ws.sendFrame(payload.data(), (int) payload.size(), WebSocket::FRAME_BINARY);
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, 0, n) == 0);
assertTrue (flags == WebSocket::FRAME_BINARY);
ws.shutdown();
n = ws.receiveFrame(buffer, sizeof(buffer), flags);
assertTrue (n == 2);
assertTrue ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_CLOSE);
server.stop();
}
void WebSocketTest::testWebSocketLarge()
{
const int msgSize = 64000;
Poco::Net::SecureServerSocket ss(0);
Poco::Net::HTTPServer server(new WebSocketRequestHandlerFactory(msgSize), 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.setSendBufferSize(msgSize);
ws.setReceiveBufferSize(msgSize);
std::string payload(msgSize, 'x');
SocketStream sstr(ws);
sstr << payload;
sstr.flush();
char buffer[msgSize + 1] = {};
int flags;
int n = 0;
do
{
n += ws.receiveFrame(buffer + n, sizeof(buffer) - n, flags);
} while (n > 0 && n < msgSize);
assertTrue (n == payload.size());
assertTrue (payload.compare(0, payload.size(), buffer, 0, n) == 0);
}
void WebSocketTest::setUp()
{
}
void WebSocketTest::tearDown()
{
}
CppUnit::Test* WebSocketTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("WebSocketTest");
CppUnit_addTest(pSuite, WebSocketTest, testWebSocket);
CppUnit_addTest(pSuite, WebSocketTest, testWebSocketLarge);
return pSuite;
}

View File

@ -0,0 +1,39 @@
//
// WebSocketTest.h
//
// Definition of the WebSocketTest class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef WebSocketTest_INCLUDED
#define WebSocketTest_INCLUDED
#include "Poco/Net/Net.h"
#include "CppUnit/TestCase.h"
class WebSocketTest: public CppUnit::TestCase
{
public:
WebSocketTest(const std::string& name);
~WebSocketTest();
void testWebSocket();
void testWebSocketLarge();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
};
#endif // WebSocketTest_INCLUDED

View File

@ -0,0 +1,89 @@
//
// WinCEDriver.cpp
//
// Console-based test driver for Windows CE.
//
// Copyright (c) 2006-2014, 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>
#include <iostream>
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;
}
}

View File

@ -0,0 +1,75 @@
//
// WinDriver.cpp
//
// Windows test driver for Poco NetSSL.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "WinTestRunner/WinTestRunner.h"
#include "NetSSLTestSuite.h"
#include "Poco/Util/Application.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/Net/HTTPSStreamFactory.h"
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::WinTestRunner runner;
runner.addTest(NetSSLTestSuite::suite());
runner.run();
return 0;
}
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;
};
class TestDriver: public CppUnit::WinTestRunnerApp
{
void TestMain()
{
NetSSLApp app;
std::string argv("TestSuite");
const char* pArgv = argv.c_str();
try
{
app.init(1, (char**)&pArgv);
app.run();
}
catch (Poco::Exception& exc)
{
app.logger().log(exc);
}
}
};
static TestDriver theDriver;