diff --git a/CMakeLists.txt b/CMakeLists.txt index ebf9a6fd7cd04dcd0b3667dd300cbd5f0cfea602..99bd9de20be82159043c1b16be07f3fe079aa533 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -303,13 +303,22 @@ install (FILES ${CMAKE_CURRENT_BINARY_DIR}/opendhtConfigVersion.cmake DESTINATIO IF(OPENDHT_TESTS) FIND_PACKAGE(Cppunit REQUIRED) # unit testing - add_executable(opendht_unit_tests - tests/tests_runner.cpp + list (APPEND test_FILES tests/infohashtester.h tests/infohashtester.cpp tests/dhtrunnertester.h tests/dhtrunnertester.cpp ) + if (OPENDHT_PROXY_SERVER AND OPENDHT_PROXY_CLIENT) + list (APPEND test_FILES + tests/dhtproxytester.h + tests/dhtproxytester.cpp + ) + endif() + add_executable(opendht_unit_tests + tests/tests_runner.cpp + ${test_FILES} + ) if (OPENDHT_SHARED) TARGET_LINK_LIBRARIES(opendht_unit_tests opendht) else () diff --git a/tests/Makefile.am b/tests/Makefile.am index 22b6ce4d3600909fc2eb880790b041d08c9c2ddf..32ddda65146efac570973654c6f7ecf4ed490f1c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,7 +3,7 @@ bin_PROGRAMS = opendht_unit_tests AM_CPPFLAGS = -I../include -nobase_include_HEADERS = infohashtester.h dhtrunnertester.h -opendht_unit_tests_SOURCES = tests_runner.cpp infohashtester.cpp dhtrunnertester.cpp +nobase_include_HEADERS = infohashtester.h dhtrunnertester.h dhtproxytester.h +opendht_unit_tests_SOURCES = tests_runner.cpp infohashtester.cpp dhtrunnertester.cpp dhtproxytester.cpp opendht_unit_tests_LDFLAGS = -lopendht -lcppunit -L@top_builddir@/src/.libs @GnuTLS_LIBS@ endif diff --git a/tests/dhtproxytester.cpp b/tests/dhtproxytester.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff421688b7414b87bffbbb18f6e60b01a026555e --- /dev/null +++ b/tests/dhtproxytester.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2018 Savoir-faire Linux Inc. + * + * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "dhtproxytester.h" + +// std +#include <iostream> +#include <string> + +#include <chrono> +#include <condition_variable> + + +namespace test { +CPPUNIT_TEST_SUITE_REGISTRATION(DhtProxyTester); + +void +DhtProxyTester::setUp() { + nodePeer.run(42222, {}, true); + nodeProxy = std::make_shared<dht::DhtRunner>(); + nodeClient = std::make_shared<dht::DhtRunner>(); + + nodeProxy->run(42232, {}, true); + nodeProxy->bootstrap(nodePeer.getBound()); + server = std::unique_ptr<dht::DhtProxyServer>(new dht::DhtProxyServer(nodeProxy, 8080)); + + nodeClient->run(42242, {}, true); + nodeClient->bootstrap(nodePeer.getBound()); + nodeClient->setProxyServer("127.0.0.1:8080"); + nodeClient->enableProxy(true); +} + +void +DhtProxyTester::tearDown() { + nodePeer.join(); + nodeClient->join(); + server->stop(); + nodeProxy->join(); +} + +void +DhtProxyTester::testGetPut() { + auto cv = std::make_shared<std::condition_variable>(); + std::mutex cv_m; + + auto key = dht::InfoHash::get("GLaDOs"); + dht::Value val {"Hei! It's been a long time. How have you been?"}; + auto val_data = val.data; + + nodePeer.put(key, std::move(val), [cv](bool) { + cv->notify_all(); + }); + std::unique_lock<std::mutex> lk(cv_m); + cv->wait_for(lk, std::chrono::seconds(10)); + + auto vals = nodeClient->get(key).get(); + CPPUNIT_ASSERT(not vals.empty()); + CPPUNIT_ASSERT(vals.front()->data == val_data); +} + +} // namespace test diff --git a/tests/dhtproxytester.h b/tests/dhtproxytester.h new file mode 100644 index 0000000000000000000000000000000000000000..c086b787ae4f466bb0b0c2a2d4c88dca26e8e36b --- /dev/null +++ b/tests/dhtproxytester.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2018 Savoir-faire Linux Inc. + * + * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#pragma once + +// cppunit +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> + +#include <opendht/dhtrunner.h> +#include <opendht/dht_proxy_server.h> + +namespace test { + +class DhtProxyTester : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(DhtProxyTester); + CPPUNIT_TEST(testGetPut); + CPPUNIT_TEST_SUITE_END(); + + public: + /** + * Method automatically called before each test by CppUnit + * Init nodes + */ + void setUp(); + /** + * Method automatically called after each test CppUnit + */ + void tearDown(); + /** + * Test get and put methods + */ + void testGetPut(); + + private: + dht::DhtRunner nodePeer {}; + + std::shared_ptr<dht::DhtRunner> nodeClient; + std::shared_ptr<dht::DhtRunner> nodeProxy; + std::unique_ptr<dht::DhtProxyServer> server; +}; + +} // namespace test