-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(NetSSL): Non-blocking sockets support #4773
- Loading branch information
1 parent
cedd086
commit bf09be3
Showing
12 changed files
with
447 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
NetSSL_OpenSSL/testsuite/src/SecureStreamSocketTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
// | ||
// SecureStreamSocketTest.cpp | ||
// | ||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. | ||
// and Contributors. | ||
// | ||
// SPDX-License-Identifier: BSL-1.0 | ||
// | ||
|
||
|
||
#include "SecureStreamSocketTest.h" | ||
#include "CppUnit/TestCaller.h" | ||
#include "CppUnit/TestSuite.h" | ||
#include "Poco/Net/TCPServer.h" | ||
#include "Poco/Net/TCPServerConnection.h" | ||
#include "Poco/Net/TCPServerConnectionFactory.h" | ||
#include "Poco/Net/TCPServerParams.h" | ||
#include "Poco/Net/SecureStreamSocket.h" | ||
#include "Poco/Net/SecureServerSocket.h" | ||
#include "Poco/Net/Context.h" | ||
#include "Poco/Net/RejectCertificateHandler.h" | ||
#include "Poco/Net/AcceptCertificateHandler.h" | ||
#include "Poco/Net/Session.h" | ||
#include "Poco/Net/SSLManager.h" | ||
#include "Poco/Util/Application.h" | ||
#include "Poco/Util/AbstractConfiguration.h" | ||
#include "Poco/Thread.h" | ||
#include <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; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
SecureStreamSocketTest::SecureStreamSocketTest(const std::string& name): CppUnit::TestCase(name) | ||
{ | ||
} | ||
|
||
|
||
SecureStreamSocketTest::~SecureStreamSocketTest() | ||
{ | ||
} | ||
|
||
|
||
void SecureStreamSocketTest::testSendReceive() | ||
{ | ||
SecureServerSocket svs(0); | ||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs); | ||
srv.start(); | ||
|
||
SocketAddress sa("127.0.0.1", svs.address().port()); | ||
SecureStreamSocket ss1(sa); | ||
std::string data("hello, world"); | ||
ss1.sendBytes(data.data(), static_cast<int>(data.size())); | ||
char buffer[8192]; | ||
int n = ss1.receiveBytes(buffer, sizeof(buffer)); | ||
assertTrue (n > 0); | ||
assertTrue (std::string(buffer, n) == data); | ||
|
||
const std::vector<std::size_t> sizes = {67, 467, 7883, 19937}; | ||
for (const auto n: sizes) | ||
{ | ||
data.assign(n, 'X'); | ||
ss1.sendBytes(data.data(), static_cast<int>(data.size())); | ||
std::string received; | ||
while (received.size() < n) | ||
{ | ||
int rc = ss1.receiveBytes(buffer, sizeof(buffer)); | ||
if (rc > 0) | ||
{ | ||
received.append(buffer, rc); | ||
} | ||
else if (n == 0) | ||
{ | ||
break; | ||
} | ||
} | ||
assertTrue (received == data); | ||
} | ||
|
||
ss1.close(); | ||
} | ||
|
||
|
||
void SecureStreamSocketTest::testPeek() | ||
{ | ||
SecureServerSocket svs(0); | ||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs); | ||
srv.start(); | ||
|
||
SocketAddress sa("127.0.0.1", svs.address().port()); | ||
SecureStreamSocket ss(sa); | ||
|
||
int n = ss.sendBytes("hello, world!", 13); | ||
assertTrue (n == 13); | ||
char buffer[256]; | ||
n = ss.receiveBytes(buffer, 5, MSG_PEEK); | ||
assertTrue (n == 5); | ||
assertTrue (std::string(buffer, n) == "hello"); | ||
n = ss.receiveBytes(buffer, sizeof(buffer), MSG_PEEK); | ||
assertTrue (n == 13); | ||
assertTrue (std::string(buffer, n) == "hello, world!"); | ||
n = ss.receiveBytes(buffer, 7); | ||
assertTrue (n == 7); | ||
assertTrue (std::string(buffer, n) == "hello, "); | ||
n = ss.receiveBytes(buffer, 6); | ||
assertTrue (n == 6); | ||
assertTrue (std::string(buffer, n) == "world!"); | ||
ss.close(); | ||
} | ||
|
||
|
||
void SecureStreamSocketTest::testNB() | ||
{ | ||
SecureServerSocket svs(0); | ||
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs); | ||
srv.start(); | ||
|
||
SocketAddress sa("127.0.0.1", svs.address().port()); | ||
SecureStreamSocket ss1(sa); | ||
ss1.setBlocking(false); | ||
ss1.setSendBufferSize(32000); | ||
|
||
char buffer[8192]; | ||
const std::vector<std::size_t> sizes = {67, 467, 7883, 19937}; | ||
for (const auto n: sizes) | ||
{ | ||
std::string data(n, 'X'); | ||
int rc = ss1.sendBytes(data.data(), static_cast<int>(data.size())); | ||
assertTrue (rc == n); | ||
|
||
rc = -1; | ||
while (rc < 0) | ||
{ | ||
rc = ss1.receiveBytes(buffer, sizeof(buffer), MSG_PEEK); | ||
} | ||
assertTrue (rc > 0 && rc <= n); | ||
assertTrue (data.compare(0, rc, buffer, rc) == 0); | ||
|
||
std::string received; | ||
while (received.size() < n) | ||
{ | ||
int rc = ss1.receiveBytes(buffer, sizeof(buffer)); | ||
if (rc > 0) | ||
{ | ||
received.append(buffer, rc); | ||
} | ||
else if (n == 0) | ||
{ | ||
break; | ||
} | ||
} | ||
assertTrue (received == data); | ||
} | ||
|
||
ss1.close(); | ||
} | ||
|
||
|
||
void SecureStreamSocketTest::setUp() | ||
{ | ||
} | ||
|
||
|
||
void SecureStreamSocketTest::tearDown() | ||
{ | ||
} | ||
|
||
|
||
CppUnit::Test* SecureStreamSocketTest::suite() | ||
{ | ||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SecureStreamSocketTest"); | ||
|
||
CppUnit_addTest(pSuite, SecureStreamSocketTest, testSendReceive); | ||
CppUnit_addTest(pSuite, SecureStreamSocketTest, testPeek); | ||
CppUnit_addTest(pSuite, SecureStreamSocketTest, testNB); | ||
|
||
return pSuite; | ||
} |
Oops, something went wrong.