2018-11-01 04:18:07 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
#include <regex>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-01-20 10:45:53 -06:00
|
|
|
#include <test/lokassert.hpp>
|
|
|
|
|
2018-11-01 04:18:07 -05:00
|
|
|
#include <cppunit/BriefTestProgressListener.h>
|
|
|
|
#include <cppunit/CompilerOutputter.h>
|
|
|
|
#include <cppunit/TestResult.h>
|
|
|
|
#include <cppunit/TestResultCollector.h>
|
|
|
|
#include <cppunit/TestRunner.h>
|
|
|
|
#include <cppunit/TextTestProgressListener.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include <cppunit/extensions/TestFactoryRegistry.h>
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#undef MOBILEAPP
|
|
|
|
#define MOBILEAPP 1 // A bit ugly, but currently FakeSocket.hpp is surrounded by a MOBILEAPP ifdef,
|
|
|
|
// and probably it is not a good idea to remove that?
|
2018-11-01 04:18:07 -05:00
|
|
|
#include "FakeSocket.hpp"
|
|
|
|
|
|
|
|
class FakeSocketTest : public CPPUNIT_NS::TestFixture
|
|
|
|
{
|
|
|
|
CPPUNIT_TEST_SUITE(FakeSocketTest);
|
|
|
|
|
|
|
|
CPPUNIT_TEST(testBasic);
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void testBasic();
|
|
|
|
|
|
|
|
public:
|
|
|
|
FakeSocketTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
|
|
|
fakeSocketSetLoggingCallback([](const std::string& line)
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
std::cerr << line << '\n';
|
2018-11-01 04:18:07 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void FakeSocketTest::testBasic()
|
|
|
|
{
|
2022-02-16 16:22:18 -06:00
|
|
|
constexpr auto testname = __func__;
|
|
|
|
|
2018-11-01 09:50:42 -05:00
|
|
|
int rc;
|
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
// First check invalid fds.
|
|
|
|
|
|
|
|
rc = fakeSocketListen(10);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EBADF);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
rc = fakeSocketWrite(20, "hah", 3);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EBADF);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
rc = fakeSocketRead(30, buf, 3);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EBADF);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Create three sockets: s0, s1 and s2.
|
|
|
|
int s0 = fakeSocketSocket();
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(s0 >= 0);
|
2018-11-01 05:49:59 -05:00
|
|
|
int s1 = fakeSocketSocket();
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(s1 >= 0);
|
2018-11-01 05:49:59 -05:00
|
|
|
int s2 = fakeSocketSocket();
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(s2 >= 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(s0 != s1);
|
|
|
|
LOK_ASSERT(s1 != s2);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Close s1 and create it anew
|
|
|
|
fakeSocketClose(s1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
s1 = fakeSocketSocket();
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(s1 >= 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Listen on s0
|
2018-11-01 09:50:42 -05:00
|
|
|
rc = fakeSocketListen(s0);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc != -1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Start a thread that accepts two connections to s0, producing sockets s3 and s4.
|
|
|
|
int s3 = -1, s4 = -1;
|
|
|
|
std::thread t0([&] {
|
2020-01-20 10:45:53 -06:00
|
|
|
// Cannot use LOK_ASSERT here as that throws and this thread has no Cppunit
|
2018-11-01 09:50:42 -05:00
|
|
|
// exception handler. We check below after joining this thread.
|
2018-11-01 14:29:40 -05:00
|
|
|
s3 = fakeSocketAccept4(s0);
|
|
|
|
s4 = fakeSocketAccept4(s0);
|
2018-11-01 05:49:59 -05:00
|
|
|
});
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Connect s1 and s2 to s0 (that is, to the sockets produced by accepting connections to
|
|
|
|
// s0).
|
|
|
|
rc = fakeSocketConnect(s1, s0);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc != -1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketConnect(s2, s0);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc != -1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Verify that we got the accepts.
|
|
|
|
t0.join();
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(s3 != -1);
|
|
|
|
LOK_ASSERT(s4 != -1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// s1 should now be connected to s3, and s2 to s4.
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(fakeSocketPeer(s1) == s3);
|
|
|
|
LOK_ASSERT(fakeSocketPeer(s3) == s1);
|
|
|
|
LOK_ASSERT(fakeSocketPeer(s2) == s4);
|
|
|
|
LOK_ASSERT(fakeSocketPeer(s4) == s2);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Some writing and reading
|
|
|
|
rc = fakeSocketWrite(s1, "hello", 5);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc != -1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketWrite(s1, "greetings", 9);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc != -1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketWrite(s2, "moin", 4);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc != -1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketAvailableDataLength(s3);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 5);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketRead(s3, buf, 100);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 5);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketAvailableDataLength(s3);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 9);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketRead(s4, buf, 100);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 4);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketWrite(s3, "goodbye", 7);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc > 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketRead(s1, buf, 4);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EAGAIN); // Note: not really the right errno, but what else? See
|
2018-11-01 05:49:59 -05:00
|
|
|
// FakeSocket.cpp.
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketRead(s1, buf, 100);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc > 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Close s3. Reading from s1 should then return an EOF indication (0).
|
|
|
|
fakeSocketClose(s3);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketAvailableDataLength(s1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketRead(s1, buf, 100);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketAvailableDataLength(s1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketRead(s1, buf, 100);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
// Test the "pipe" functionality, that creates an already connected socket pair.
|
|
|
|
int pipe[2];
|
|
|
|
rc = fakeSocketPipe2(pipe);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketWrite(pipe[0], "x", 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketAvailableDataLength(pipe[1]);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketRead(pipe[1], buf, 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(buf[0] == 'x');
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketWrite(pipe[1], "y", 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketRead(pipe[0], buf, 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 1);
|
|
|
|
LOK_ASSERT(buf[0] == 'y');
|
2018-11-01 05:49:59 -05:00
|
|
|
|
|
|
|
rc = fakeSocketWrite(pipe[0], "z", 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 1);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
2018-11-01 05:49:59 -05:00
|
|
|
rc = fakeSocketShutdown(pipe[0]);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 05:49:59 -05:00
|
|
|
|
|
|
|
rc = fakeSocketRead(pipe[1], buf, 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 1);
|
|
|
|
LOK_ASSERT(buf[0] == 'z');
|
2018-11-01 05:49:59 -05:00
|
|
|
|
|
|
|
rc = fakeSocketWrite(pipe[0], "a", 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EPIPE);
|
2018-11-01 05:49:59 -05:00
|
|
|
|
|
|
|
rc = fakeSocketRead(pipe[0], buf, 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 05:49:59 -05:00
|
|
|
|
|
|
|
rc = fakeSocketRead(pipe[0], buf, 1);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
rc = fakeSocketClose(pipe[0]);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
rc = fakeSocketClose(pipe[0]);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EBADF);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
rc = fakeSocketClose(pipe[1]);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
rc = fakeSocketClose(pipe[1]);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EBADF);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
// Create a pipe again.
|
|
|
|
|
|
|
|
rc = fakeSocketPipe2(pipe);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 0);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
rc = fakeSocketAvailableDataLength(pipe[0]);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EAGAIN);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
rc = fakeSocketAvailableDataLength(pipe[1]);
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == -1);
|
|
|
|
LOK_ASSERT(errno == EAGAIN);
|
2018-11-01 09:50:42 -05:00
|
|
|
|
|
|
|
// Test poll functionality.
|
|
|
|
|
|
|
|
struct pollfd pollfds[4];
|
|
|
|
|
|
|
|
pollfds[0].fd = s0;
|
|
|
|
pollfds[0].events = POLLIN | POLLOUT;
|
|
|
|
pollfds[1].fd = s1;
|
|
|
|
pollfds[1].events = POLLIN | POLLOUT;
|
|
|
|
pollfds[2].fd = s2;
|
|
|
|
pollfds[2].events = POLLIN | POLLOUT;
|
|
|
|
pollfds[3].fd = 40;
|
|
|
|
pollfds[3].events = POLLIN | POLLOUT;
|
|
|
|
|
|
|
|
rc = fakeSocketPoll(pollfds, 4, -1);
|
|
|
|
// Hmm, does a real poll() set POLLIN for a listening socket? Probably only if there is a
|
|
|
|
// connection in progress, and that is not the case here for s0.
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT(rc == 3);
|
|
|
|
LOK_ASSERT(pollfds[0].revents == 0);
|
|
|
|
LOK_ASSERT(pollfds[1].revents == POLLIN);
|
|
|
|
LOK_ASSERT(pollfds[2].revents == POLLOUT);
|
|
|
|
LOK_ASSERT(pollfds[3].revents == POLLNVAL);
|
2018-11-01 05:49:59 -05:00
|
|
|
}
|
2018-11-01 04:18:07 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(FakeSocketTest);
|
|
|
|
|
|
|
|
int main(int, char**)
|
|
|
|
{
|
2019-08-02 02:57:16 -05:00
|
|
|
const char* envar = std::getenv("CPPUNIT_TEST_NAME");
|
|
|
|
std::string testName;
|
|
|
|
if (envar)
|
|
|
|
{
|
|
|
|
testName = std::string(envar);
|
|
|
|
}
|
|
|
|
if (!testName.empty() && testName != "FakeSocketTest")
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-01 04:18:07 -05:00
|
|
|
CPPUNIT_NS::TestResult controller;
|
|
|
|
CPPUNIT_NS::TestResultCollector result;
|
|
|
|
controller.addListener(&result);
|
|
|
|
CPPUNIT_NS::BriefTestProgressListener progress;
|
|
|
|
controller.addListener(&progress);
|
2019-09-21 21:21:30 -05:00
|
|
|
CPPUNIT_NS::TextTestProgressListener listener;
|
|
|
|
controller.addListener(&listener);
|
2018-11-01 04:18:07 -05:00
|
|
|
|
|
|
|
CPPUNIT_NS::Test* testRegistry = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
|
|
|
|
|
|
|
|
CPPUNIT_NS::TestRunner runner;
|
|
|
|
runner.addTest(testRegistry);
|
|
|
|
runner.run(controller);
|
|
|
|
|
|
|
|
CPPUNIT_NS::CompilerOutputter outputter(&result, std::cerr);
|
|
|
|
outputter.setNoWrap();
|
|
|
|
outputter.write();
|
|
|
|
|
|
|
|
fakeSocketDumpState();
|
|
|
|
|
2018-11-01 05:12:26 -05:00
|
|
|
return result.wasSuccessful() ? 0 : 1;
|
2018-11-01 04:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|