mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-20 14:02:38 +02:00
NetSSL_OpenSSL: complete merge to 1.4.2
This commit is contained in:
@@ -1,84 +1,97 @@
|
||||
//
|
||||
// Driver.cpp
|
||||
//
|
||||
//
|
||||
// Driver.cpp
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/Driver.cpp#10 $
|
||||
//
|
||||
// Console-based test driver for Poco NetSSL.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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()
|
||||
{
|
||||
}
|
||||
|
||||
~NetSSLApp()
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
void initialize(Poco::Util::Application& self)
|
||||
{
|
||||
loadConfiguration(); // load default configuration files, if present
|
||||
Poco::Util::Application::initialize(self);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
Poco::Net::HTTPStreamFactory::registerFactory();
|
||||
Poco::Net::HTTPSStreamFactory::registerFactory();
|
||||
NetSSLApp app;
|
||||
try
|
||||
{
|
||||
app.init(1, av);
|
||||
}
|
||||
catch (Poco::Exception& exc)
|
||||
{
|
||||
std::cout << exc.displayText() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<std::string> args;
|
||||
for (int i = 0; i < ac; ++i)
|
||||
args.push_back(std::string(av[i]));
|
||||
CppUnit::TestRunner runner;
|
||||
runner.addTest("NetSSLTestSuite", NetSSLTestSuite::suite());
|
||||
return runner.run(args) ? 0 : 1;
|
||||
}
|
||||
//
|
||||
// Console-based test driver for Poco NetSSL.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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;
|
||||
}
|
||||
}
|
||||
|
@@ -1,363 +1,465 @@
|
||||
//
|
||||
// HTTPSClientSessionTest.cpp
|
||||
//
|
||||
//
|
||||
// HTTPSClientSessionTest.cpp
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.cpp#10 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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/StreamCopier.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/DateTimeFormatter.h"
|
||||
#include "Poco/DateTimeFormat.h"
|
||||
#include "HTTPSTestServer.h"
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
using namespace Poco::Net;
|
||||
using Poco::StreamCopier;
|
||||
|
||||
|
||||
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("localhost", srv.port());
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/small");
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testGetLarge()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", srv.port());
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/large");
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::LARGE_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testHead()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", srv.port());
|
||||
HTTPRequest request(HTTPRequest::HTTP_HEAD, "/large");
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
std::ostringstream ostr;
|
||||
assert (StreamCopier::copyStream(rs, ostr) == 0);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostSmallIdentity()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getContentLength() == body.length());
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostLargeIdentity()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getContentLength() == body.length());
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostSmallChunked()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostLargeChunked()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostLargeChunkedKeepAlive()
|
||||
{
|
||||
SecureServerSocket svs(32322);
|
||||
HTTPServer srv(new TestRequestHandlerFactory(), svs, new HTTPServerParams());
|
||||
srv.start();
|
||||
try
|
||||
{
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
srv.stop();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
srv.stop();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testKeepAlive()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr1;
|
||||
assert (StreamCopier::copyStream(rs1, ostr1) == 0);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_GET);
|
||||
request.setURI("/small");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs2 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr2;
|
||||
StreamCopier::copyStream(rs2, ostr2);
|
||||
assert (ostr2.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_GET);
|
||||
request.setURI("/large");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs3 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr3;
|
||||
int n = StreamCopier::copyStream(rs3, ostr3);
|
||||
assert (ostr3.str() == HTTPSTestServer::LARGE_BODY);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_HEAD);
|
||||
request.setURI("/large");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs4= s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (!response.getKeepAlive());
|
||||
std::ostringstream ostr4;
|
||||
assert (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);
|
||||
X509Certificate cert = s.serverCertificate();
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
std::string str(ostr.str());
|
||||
assert (str == "This is a test file for NetSSL.\n");
|
||||
assert (cert.commonName() == "secure.appinf.com");
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testProxy()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("secure.appinf.com");
|
||||
s.setProxy("proxy.aon.at", 8080);
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/public/poco/NetSSL.txt");
|
||||
s.sendRequest(request);
|
||||
X509Certificate cert = s.serverCertificate();
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
std::string str(ostr.str());
|
||||
assert (str == "This is a test file for NetSSL.\n");
|
||||
assert (cert.commonName() == "secure.appinf.com");
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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("localhost", srv.port());
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/small");
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testGetLarge()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", srv.port());
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/large");
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::LARGE_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testHead()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", srv.port());
|
||||
HTTPRequest request(HTTPRequest::HTTP_HEAD, "/large");
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
std::ostringstream ostr;
|
||||
assert (StreamCopier::copyStream(rs, ostr) == 0);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostSmallIdentity()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getContentLength() == body.length());
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostLargeIdentity()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getContentLength() == body.length());
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostSmallChunked()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostLargeChunked()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testPostLargeChunkedKeepAlive()
|
||||
{
|
||||
SecureServerSocket svs(32322);
|
||||
HTTPServer srv(new TestRequestHandlerFactory(), svs, new HTTPServerParams());
|
||||
srv.start();
|
||||
try
|
||||
{
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
srv.stop();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
srv.stop();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientSessionTest::testKeepAlive()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClientSession s("localhost", 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);
|
||||
assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr1;
|
||||
assert (StreamCopier::copyStream(rs1, ostr1) == 0);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_GET);
|
||||
request.setURI("/small");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs2 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr2;
|
||||
StreamCopier::copyStream(rs2, ostr2);
|
||||
assert (ostr2.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_GET);
|
||||
request.setURI("/large");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs3 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr3;
|
||||
StreamCopier::copyStream(rs3, ostr3);
|
||||
assert (ostr3.str() == HTTPSTestServer::LARGE_BODY);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_HEAD);
|
||||
request.setURI("/large");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs4 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (!response.getKeepAlive());
|
||||
std::ostringstream ostr4;
|
||||
assert (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);
|
||||
X509Certificate cert = s.serverCertificate();
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
std::string str(ostr.str());
|
||||
assert (str == "This is a test file for NetSSL.\n");
|
||||
assert (cert.commonName() == "secure.appinf.com");
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
X509Certificate cert = s.serverCertificate();
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
std::string str(ostr.str());
|
||||
assert (str == "This is a test file for NetSSL.\n");
|
||||
assert (cert.commonName() == "secure.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,
|
||||
Application::instance().config().getString("openSSL.server.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.server.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.server.caConfig"),
|
||||
Context::VERIFY_NONE,
|
||||
9,
|
||||
true,
|
||||
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
|
||||
pServerContext->enableSessionCache(true, "TestSuite");
|
||||
pServerContext->setSessionTimeout(10);
|
||||
pServerContext->setSessionCacheSize(1000);
|
||||
pServerContext->disableStatelessSessionResumption();
|
||||
|
||||
HTTPSTestServer srv(pServerContext);
|
||||
|
||||
Context::Ptr pClientContext = new Context(
|
||||
Context::CLIENT_USE,
|
||||
Application::instance().config().getString("openSSL.client.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.client.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.client.caConfig"),
|
||||
Context::VERIFY_RELAXED,
|
||||
9,
|
||||
true,
|
||||
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
|
||||
pClientContext->enableSessionCache(true);
|
||||
|
||||
HTTPSClientSession s1("localhost", 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);
|
||||
assert (response1.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response1.getContentType() == "text/plain");
|
||||
std::ostringstream ostr1;
|
||||
StreamCopier::copyStream(rs1, ostr1);
|
||||
assert (ostr1.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
HTTPSClientSession s2("localhost", 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);
|
||||
assert (response2.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response2.getContentType() == "text/plain");
|
||||
std::ostringstream ostr2;
|
||||
StreamCopier::copyStream(rs2, ostr2);
|
||||
assert (ostr2.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
assert (pSession1 == pSession2);
|
||||
|
||||
HTTPRequest request3(HTTPRequest::HTTP_GET, "/small");
|
||||
s2.sendRequest(request3);
|
||||
Session::Ptr pSession3 = s2.sslSession();
|
||||
HTTPResponse response3;
|
||||
std::istream& rs3 = s2.receiveResponse(response3);
|
||||
assert (response3.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response3.getContentType() == "text/plain");
|
||||
std::ostringstream ostr3;
|
||||
StreamCopier::copyStream(rs3, ostr3);
|
||||
assert (ostr3.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
assert (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);
|
||||
assert (response4.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response4.getContentType() == "text/plain");
|
||||
std::ostringstream ostr4;
|
||||
StreamCopier::copyStream(rs4, ostr4);
|
||||
assert (ostr4.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
assert (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;
|
||||
}
|
||||
|
@@ -1,70 +1,71 @@
|
||||
//
|
||||
// HTTPSClientSessionTest.h
|
||||
//
|
||||
//
|
||||
// HTTPSClientSessionTest.h
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.h#10 $
|
||||
//
|
||||
// Definition of the HTTPSClientSessionTest class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#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 setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
#endif // HTTPSClientSessionTest_INCLUDED
|
||||
//
|
||||
// Definition of the HTTPSClientSessionTest class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#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
|
||||
|
@@ -1,371 +1,371 @@
|
||||
//
|
||||
// HTTPSServerTest.cpp
|
||||
//
|
||||
//
|
||||
// HTTPSServerTest.cpp
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.cpp#9 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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::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();
|
||||
int n = 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("localhost", 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;
|
||||
assert (response.getContentLength() == body.size());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (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("localhost", 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;
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (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("localhost", 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;
|
||||
assert (response.getContentLength() == body.size());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getKeepAlive());
|
||||
assert (rbody == body);
|
||||
|
||||
body.assign(1000, 'y');
|
||||
request.setContentLength((int) body.length());
|
||||
request.setKeepAlive(false);
|
||||
cs.sendRequest(request) << body;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getContentLength() == body.size());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (!response.getKeepAlive());
|
||||
assert (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("localhost", 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;
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (rbody == body);
|
||||
|
||||
body.assign(1000, 'y');
|
||||
request.setKeepAlive(false);
|
||||
cs.sendRequest(request) << body;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (!response.getKeepAlive());
|
||||
assert (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("localhost", 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;
|
||||
assert (response.getContentLength() == body.size());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (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("localhost", svs.address().port());
|
||||
HTTPRequest request("GET", "/redirect");
|
||||
cs.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::string rbody;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getStatus() == HTTPResponse::HTTP_FOUND);
|
||||
assert (response.get("Location") == "http://www.appinf.com/");
|
||||
assert (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("localhost", svs.address().port());
|
||||
HTTPRequest request("GET", "/auth");
|
||||
cs.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::string rbody;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED);
|
||||
assert (response.get("WWW-Authenticate") == "Basic realm=\"/auth\"");
|
||||
assert (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("localhost", svs.address().port());
|
||||
HTTPRequest request("GET", "/notImpl");
|
||||
cs.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::string rbody;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getStatus() == HTTPResponse::HTTP_NOT_IMPLEMENTED);
|
||||
assert (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;
|
||||
}
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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::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("localhost", 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;
|
||||
assert (response.getContentLength() == body.size());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (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("localhost", 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;
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (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("localhost", 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;
|
||||
assert (response.getContentLength() == body.size());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getKeepAlive());
|
||||
assert (rbody == body);
|
||||
|
||||
body.assign(1000, 'y');
|
||||
request.setContentLength((int) body.length());
|
||||
request.setKeepAlive(false);
|
||||
cs.sendRequest(request) << body;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getContentLength() == body.size());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (!response.getKeepAlive());
|
||||
assert (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("localhost", 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;
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (rbody == body);
|
||||
|
||||
body.assign(1000, 'y');
|
||||
request.setKeepAlive(false);
|
||||
cs.sendRequest(request) << body;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (!response.getKeepAlive());
|
||||
assert (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("localhost", 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;
|
||||
assert (response.getContentLength() == body.size());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (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("localhost", svs.address().port());
|
||||
HTTPRequest request("GET", "/redirect");
|
||||
cs.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::string rbody;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getStatus() == HTTPResponse::HTTP_FOUND);
|
||||
assert (response.get("Location") == "http://www.appinf.com/");
|
||||
assert (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("localhost", svs.address().port());
|
||||
HTTPRequest request("GET", "/auth");
|
||||
cs.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::string rbody;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED);
|
||||
assert (response.get("WWW-Authenticate") == "Basic realm=\"/auth\"");
|
||||
assert (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("localhost", svs.address().port());
|
||||
HTTPRequest request("GET", "/notImpl");
|
||||
cs.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::string rbody;
|
||||
cs.receiveResponse(response) >> rbody;
|
||||
assert (response.getStatus() == HTTPResponse::HTTP_NOT_IMPLEMENTED);
|
||||
assert (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;
|
||||
}
|
||||
|
@@ -1,153 +1,159 @@
|
||||
//
|
||||
// HTTPSStreamFactoryTest.cpp
|
||||
//
|
||||
//
|
||||
// HTTPSStreamFactoryTest.cpp
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.cpp#8 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "HTTPSStreamFactoryTest.h"
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/Net/HTTPSStreamFactory.h"
|
||||
#include "Poco/Net/NetException.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::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://localhost/large");
|
||||
uri.setPort(server.port());
|
||||
std::auto_ptr<std::istream> pStr(factory.open(uri));
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(*pStr.get(), ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::LARGE_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSStreamFactoryTest::testEmptyPath()
|
||||
{
|
||||
HTTPSTestServer server;
|
||||
HTTPSStreamFactory factory;
|
||||
URI uri("https://localhost");
|
||||
uri.setPort(server.port());
|
||||
std::auto_ptr<std::istream> pStr(factory.open(uri));
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(*pStr.get(), ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSStreamFactoryTest::testRedirect()
|
||||
{
|
||||
HTTPSTestServer server;
|
||||
HTTPSStreamFactory factory;
|
||||
URI uri("https://localhost/redirect");
|
||||
uri.setPort(server.port());
|
||||
std::auto_ptr<std::istream> pStr(factory.open(uri));
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(*pStr.get(), ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::LARGE_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSStreamFactoryTest::testProxy()
|
||||
{
|
||||
HTTPSTestServer server;
|
||||
HTTPSStreamFactory factory("proxy.aon.at", 8080);
|
||||
URI uri("https://sourceforge.net/");
|
||||
std::auto_ptr<std::istream> pStr(factory.open(uri));
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(*pStr.get(), ostr);
|
||||
assert (ostr.str().length() > 0);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSStreamFactoryTest::testError()
|
||||
{
|
||||
HTTPSTestServer server;
|
||||
HTTPSStreamFactory factory;
|
||||
URI uri("https://localhost/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;
|
||||
}
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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://localhost/large");
|
||||
uri.setPort(server.port());
|
||||
std::auto_ptr<std::istream> pStr(factory.open(uri));
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(*pStr.get(), ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::LARGE_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSStreamFactoryTest::testEmptyPath()
|
||||
{
|
||||
HTTPSTestServer server;
|
||||
HTTPSStreamFactory factory;
|
||||
URI uri("https://localhost");
|
||||
uri.setPort(server.port());
|
||||
std::auto_ptr<std::istream> pStr(factory.open(uri));
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(*pStr.get(), ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSStreamFactoryTest::testRedirect()
|
||||
{
|
||||
HTTPSTestServer server;
|
||||
HTTPSStreamFactory factory;
|
||||
URI uri("https://localhost/redirect");
|
||||
uri.setPort(server.port());
|
||||
std::auto_ptr<std::istream> pStr(factory.open(uri));
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(*pStr.get(), ostr);
|
||||
assert (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::auto_ptr<std::istream> pStr(factory.open(uri));
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(*pStr.get(), ostr);
|
||||
assert (ostr.str().length() > 0);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSStreamFactoryTest::testError()
|
||||
{
|
||||
HTTPSTestServer server;
|
||||
HTTPSStreamFactory factory;
|
||||
URI uri("https://localhost/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;
|
||||
}
|
||||
|
@@ -1,241 +1,252 @@
|
||||
//
|
||||
// HTTPSTestServer.cpp
|
||||
//
|
||||
//
|
||||
// HTTPSTestServer.cpp
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.cpp#9 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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()
|
||||
{
|
||||
_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;
|
||||
}
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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;
|
||||
}
|
||||
|
@@ -1,81 +1,84 @@
|
||||
//
|
||||
// HTTPSTestServer.h
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.h#7 $
|
||||
//
|
||||
// Definition of the HTTPSTestServer class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#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.
|
||||
|
||||
~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
|
||||
//
|
||||
// HTTPSTestServer.h
|
||||
//
|
||||
// $Id: //poco/1.4/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.h#1 $
|
||||
//
|
||||
// Definition of the HTTPSTestServer class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#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
|
||||
|
@@ -1,277 +1,427 @@
|
||||
//
|
||||
// TCPServerTest.cpp
|
||||
//
|
||||
//
|
||||
// TCPServerTest.cpp
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/TCPServerTest.cpp#7 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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/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::Thread;
|
||||
|
||||
|
||||
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();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
SocketAddress sa("localhost", 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));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.currentThreads() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 1);
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 0);
|
||||
}
|
||||
|
||||
|
||||
void TCPServerTest::testTwoConnections()
|
||||
{
|
||||
SecureServerSocket svs(0);
|
||||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
|
||||
srv.start();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
SocketAddress sa("localhost", 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));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
n = ss2.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
assert (srv.currentConnections() == 2);
|
||||
assert (srv.currentThreads() == 2);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 2);
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.currentThreads() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 2);
|
||||
ss2.close();
|
||||
|
||||
Thread::sleep(300);
|
||||
assert (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();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
SocketAddress sa("localhost", 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));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
n = ss2.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
n = ss3.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
n = ss4.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
assert (srv.currentConnections() == 4);
|
||||
assert (srv.currentThreads() == 4);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 4);
|
||||
|
||||
SecureStreamSocket ss5(sa);
|
||||
Thread::sleep(200);
|
||||
assert (srv.queuedConnections() == 1);
|
||||
SecureStreamSocket ss6(sa);
|
||||
Thread::sleep(200);
|
||||
assert (srv.queuedConnections() == 2);
|
||||
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 4);
|
||||
assert (srv.currentThreads() == 4);
|
||||
assert (srv.queuedConnections() == 1);
|
||||
assert (srv.totalConnections() == 5);
|
||||
|
||||
ss2.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 4);
|
||||
assert (srv.currentThreads() == 4);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 6);
|
||||
|
||||
ss3.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 3);
|
||||
assert (srv.currentThreads() == 3);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 6);
|
||||
|
||||
ss4.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 2);
|
||||
assert (srv.currentThreads() == 2);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 6);
|
||||
|
||||
ss5.close();
|
||||
ss6.close();
|
||||
Thread::sleep(300);
|
||||
assert (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);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
SocketAddress sa("localhost", 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));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.currentThreads() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 1);
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 0);
|
||||
}
|
||||
|
||||
|
||||
void TCPServerTest::testTwoConnections()
|
||||
{
|
||||
SecureServerSocket svs(0);
|
||||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
|
||||
srv.start();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
SocketAddress sa("localhost", 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));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
n = ss2.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
assert (srv.currentConnections() == 2);
|
||||
assert (srv.currentThreads() == 2);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 2);
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.currentThreads() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 2);
|
||||
ss2.close();
|
||||
|
||||
Thread::sleep(300);
|
||||
assert (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();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
SocketAddress sa("localhost", 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));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
n = ss2.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
n = ss3.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
n = ss4.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
|
||||
assert (srv.currentConnections() == 4);
|
||||
assert (srv.currentThreads() == 4);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 4);
|
||||
|
||||
SecureStreamSocket ss5;
|
||||
ss5.setLazyHandshake();
|
||||
ss5.connect(sa);
|
||||
Thread::sleep(200);
|
||||
assert (srv.queuedConnections() == 1);
|
||||
SecureStreamSocket ss6;
|
||||
ss6.setLazyHandshake();
|
||||
ss6.connect(sa);
|
||||
Thread::sleep(200);
|
||||
assert (srv.queuedConnections() == 2);
|
||||
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 4);
|
||||
assert (srv.currentThreads() == 4);
|
||||
assert (srv.queuedConnections() == 1);
|
||||
assert (srv.totalConnections() == 5);
|
||||
|
||||
ss2.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 4);
|
||||
assert (srv.currentThreads() == 4);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 6);
|
||||
|
||||
ss3.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 3);
|
||||
assert (srv.currentThreads() == 3);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 6);
|
||||
|
||||
ss4.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 2);
|
||||
assert (srv.currentThreads() == 2);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 6);
|
||||
|
||||
ss5.close();
|
||||
ss6.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 0);
|
||||
}
|
||||
|
||||
|
||||
void TCPServerTest::testReuseSocket()
|
||||
{
|
||||
SecureServerSocket svs(0);
|
||||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
|
||||
srv.start();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
SocketAddress sa("localhost", 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));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.currentThreads() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 1);
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 0);
|
||||
|
||||
ss1.connect(sa);
|
||||
ss1.sendBytes(data.data(), (int) data.size());
|
||||
n = ss1.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 2);
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 0);
|
||||
}
|
||||
|
||||
|
||||
void TCPServerTest::testReuseSession()
|
||||
{
|
||||
// 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,
|
||||
Application::instance().config().getString("openSSL.server.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.server.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.server.caConfig"),
|
||||
Context::VERIFY_NONE,
|
||||
9,
|
||||
true,
|
||||
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
|
||||
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();
|
||||
assert (srv.currentConnections() == 0);
|
||||
assert (srv.currentThreads() == 0);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 0);
|
||||
|
||||
Context::Ptr pClientContext = new Context(
|
||||
Context::SERVER_USE,
|
||||
Application::instance().config().getString("openSSL.client.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.client.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.client.caConfig"),
|
||||
Context::VERIFY_RELAXED,
|
||||
9,
|
||||
true,
|
||||
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
|
||||
pClientContext->enableSessionCache(true);
|
||||
|
||||
SocketAddress sa("localhost", svs.address().port());
|
||||
SecureStreamSocket ss1(sa, pClientContext);
|
||||
assert (!ss1.sessionWasReused());
|
||||
std::string data("hello, world");
|
||||
ss1.sendBytes(data.data(), (int) data.size());
|
||||
char buffer[256];
|
||||
int n = ss1.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.currentThreads() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 1);
|
||||
|
||||
Session::Ptr pSession = ss1.currentSession();
|
||||
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 0);
|
||||
|
||||
ss1.useSession(pSession);
|
||||
ss1.connect(sa);
|
||||
assert (ss1.sessionWasReused());
|
||||
assert (ss1.currentSession() == pSession);
|
||||
ss1.sendBytes(data.data(), (int) data.size());
|
||||
n = ss1.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 2);
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (srv.currentConnections() == 0);
|
||||
|
||||
Thread::sleep(15000); // wait for session to expire
|
||||
pServerContext->flushSessionCache();
|
||||
|
||||
ss1.useSession(pSession);
|
||||
ss1.connect(sa);
|
||||
assert (!ss1.sessionWasReused());
|
||||
assert (ss1.currentSession() != pSession);
|
||||
ss1.sendBytes(data.data(), (int) data.size());
|
||||
n = ss1.receiveBytes(buffer, sizeof(buffer));
|
||||
assert (n > 0);
|
||||
assert (std::string(buffer, n) == data);
|
||||
assert (srv.currentConnections() == 1);
|
||||
assert (srv.queuedConnections() == 0);
|
||||
assert (srv.totalConnections() == 3);
|
||||
ss1.close();
|
||||
Thread::sleep(300);
|
||||
assert (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;
|
||||
}
|
||||
|
@@ -1,62 +1,64 @@
|
||||
//
|
||||
// TCPServerTest.h
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/TCPServerTest.h#7 $
|
||||
//
|
||||
// Definition of the TCPServerTest class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#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 setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
#endif // TCPServerTest_INCLUDED
|
||||
//
|
||||
// TCPServerTest.h
|
||||
//
|
||||
// $Id: //poco/1.4/NetSSL_OpenSSL/testsuite/src/TCPServerTest.h#1 $
|
||||
//
|
||||
// Definition of the TCPServerTest class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#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
|
||||
|
110
NetSSL_OpenSSL/testsuite/src/WinCEDriver.cpp
Normal file
110
NetSSL_OpenSSL/testsuite/src/WinCEDriver.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
//
|
||||
// WinCEDriver.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/NetSSL_OpenSSL/testsuite/src/WinCEDriver.cpp#1 $
|
||||
//
|
||||
// Console-based test driver for Windows CE.
|
||||
//
|
||||
// Copyright (c) 2004-2010, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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;
|
||||
}
|
||||
}
|
@@ -1,86 +1,97 @@
|
||||
//
|
||||
// WinDriver.cpp
|
||||
//
|
||||
// $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/WinDriver.cpp#8 $
|
||||
//
|
||||
// Windows test driver for Poco OpenSSL.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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()
|
||||
{
|
||||
}
|
||||
|
||||
~NetSSLApp()
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
void initialize(Application& self)
|
||||
{
|
||||
loadConfiguration(); // load default configuration files, if present
|
||||
Application::initialize(self);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class TestDriver: public CppUnit::WinTestRunnerApp
|
||||
{
|
||||
void TestMain()
|
||||
{
|
||||
CppUnit::WinTestRunner runner;
|
||||
Poco::Net::HTTPStreamFactory::registerFactory();
|
||||
Poco::Net::HTTPSStreamFactory::registerFactory();
|
||||
NetSSLApp app;
|
||||
std::string argv("OpenSSLTest");
|
||||
const char* pArgv = argv.c_str();
|
||||
try
|
||||
{
|
||||
app.init(1, (char**)&pArgv);
|
||||
}
|
||||
catch (Poco::Exception& exc)
|
||||
{
|
||||
app.logger().log(exc);
|
||||
}
|
||||
runner.addTest(NetSSLTestSuite::suite());
|
||||
runner.run();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
static TestDriver theDriver;
|
||||
//
|
||||
// WinDriver.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/NetSSL_OpenSSL/testsuite/src/WinDriver.cpp#1 $
|
||||
//
|
||||
// Windows test driver for Poco OpenSSL.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "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;
|
||||
|
Reference in New Issue
Block a user