chore: add test for sendFile()

This commit is contained in:
Günter Obiltschnig 2025-02-07 18:42:17 +01:00
parent 13029fb524
commit 6fb0debd71
4 changed files with 93 additions and 0 deletions

View File

@ -22,6 +22,8 @@
#include "Poco/FIFOBuffer.h" #include "Poco/FIFOBuffer.h"
#include "Poco/Delegate.h" #include "Poco/Delegate.h"
#include "Poco/File.h" #include "Poco/File.h"
#include "Poco/TemporaryFile.h"
#include "Poco/FileStream.h"
#include "Poco/Path.h" #include "Poco/Path.h"
#include <iostream> #include <iostream>
@ -667,6 +669,46 @@ void SocketTest::testUseFd()
} }
void SocketTest::testSendFile()
{
EchoServer echoServer;
StreamSocket ss;
ss.connect(SocketAddress("127.0.0.1", echoServer.port()));
std::string sentData = "Hello, world!";
Poco::TemporaryFile file;
Poco::FileOutputStream ostr(file.path());
ostr.write(sentData.data(), sentData.size());
ostr.close();
Poco::FileInputStream istr(file.path());
std::streamsize sent = 0;
std::streamoff off = 0;
while (sent < file.getSize())
{
sent += ss.sendFile(istr, sent);
}
istr.close();
std::string receivedData;
char buffer[1024];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0)
{
receivedData.append(buffer, n);
if (receivedData.size() < sentData.size())
n = ss.receiveBytes(buffer, sizeof(buffer));
else
n = 0;
}
assertTrue (receivedData == sentData);
ss.close();
}
void SocketTest::onReadable(bool& b) void SocketTest::onReadable(bool& b)
{ {
if (b) ++_notToReadable; if (b) ++_notToReadable;
@ -724,6 +766,7 @@ CppUnit::Test* SocketTest::suite()
CppUnit_addTest(pSuite, SocketTest, testEchoUnixLocal); CppUnit_addTest(pSuite, SocketTest, testEchoUnixLocal);
CppUnit_addTest(pSuite, SocketTest, testUnixLocalAbstract); CppUnit_addTest(pSuite, SocketTest, testUnixLocalAbstract);
CppUnit_addTest(pSuite, SocketTest, testUseFd); CppUnit_addTest(pSuite, SocketTest, testUseFd);
CppUnit_addTest(pSuite, SocketTest, testSendFile);
return pSuite; return pSuite;
} }

View File

@ -45,9 +45,11 @@ public:
void testSelect2(); void testSelect2();
void testSelect3(); void testSelect3();
#endif #endif
void testEchoUnixLocal(); void testEchoUnixLocal();
void testUnixLocalAbstract(); void testUnixLocalAbstract();
void testUseFd(); void testUseFd();
void testSendFile();
void setUp() override; void setUp() override;
void tearDown() override; void tearDown() override;

View File

@ -25,6 +25,9 @@
#include "Poco/Util/Application.h" #include "Poco/Util/Application.h"
#include "Poco/Util/AbstractConfiguration.h" #include "Poco/Util/AbstractConfiguration.h"
#include "Poco/Thread.h" #include "Poco/Thread.h"
#include "Poco/File.h"
#include "Poco/TemporaryFile.h"
#include "Poco/FileStream.h"
#include <iostream> #include <iostream>
@ -200,6 +203,49 @@ void SecureStreamSocketTest::testNB()
} }
void SecureStreamSocketTest::testSendFile()
{
SecureServerSocket svs(0);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
srv.start();
SecureStreamSocket ss;
ss.connect(SocketAddress("127.0.0.1", srv.port()));
std::string sentData = "Hello, world!";
Poco::TemporaryFile file;
Poco::FileOutputStream ostr(file.path());
ostr.write(sentData.data(), sentData.size());
ostr.close();
Poco::FileInputStream istr(file.path());
std::streamsize sent = 0;
std::streamoff off = 0;
while (sent < file.getSize())
{
sent += ss.sendFile(istr, sent);
}
istr.close();
std::string receivedData;
char buffer[1024];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0)
{
receivedData.append(buffer, n);
if (receivedData.size() < sentData.size())
n = ss.receiveBytes(buffer, sizeof(buffer));
else
n = 0;
}
assertTrue (receivedData == sentData);
ss.close();
}
void SecureStreamSocketTest::setUp() void SecureStreamSocketTest::setUp()
{ {
} }
@ -217,6 +263,7 @@ CppUnit::Test* SecureStreamSocketTest::suite()
CppUnit_addTest(pSuite, SecureStreamSocketTest, testSendReceive); CppUnit_addTest(pSuite, SecureStreamSocketTest, testSendReceive);
CppUnit_addTest(pSuite, SecureStreamSocketTest, testPeek); CppUnit_addTest(pSuite, SecureStreamSocketTest, testPeek);
CppUnit_addTest(pSuite, SecureStreamSocketTest, testNB); CppUnit_addTest(pSuite, SecureStreamSocketTest, testNB);
CppUnit_addTest(pSuite, SecureStreamSocketTest, testSendFile);
return pSuite; return pSuite;
} }

View File

@ -27,6 +27,7 @@ public:
void testSendReceive(); void testSendReceive();
void testPeek(); void testPeek();
void testNB(); void testNB();
void testSendFile();
void setUp(); void setUp();
void tearDown(); void tearDown();