From b229ab4ae803c0db9cc155bd1cf82cd7ba759b70 Mon Sep 17 00:00:00 2001 From: Yura Zarudniy Date: Sat, 28 Sep 2019 15:38:33 +0300 Subject: [PATCH] some changes in tests --- CMakeLists.txt | 3 +- p2p/common/visitor.hpp | 102 +++++++++++++++ p2p/crypto/CMakeLists.txt | 5 +- p2p/peer/CMakeLists.txt | 3 +- test/CMakeLists.txt | 2 + test/acceptance/p2p/host/basic_host_test.cpp | 29 +++-- .../p2p/host/host_integration_test.cpp | 17 +-- test/acceptance/p2p/host/peer/test_peer.cpp | 39 +++--- test/acceptance/p2p/host/peer/test_peer.hpp | 11 +- .../p2p/host/protocol/client_test_session.cpp | 10 +- .../p2p/host/protocol/client_test_session.hpp | 4 +- test/deps/CMakeLists.txt | 11 ++ test/deps/di_test.cpp | 63 +++++++++ test/deps/outcome_test.cpp | 120 ++++++++++++++++++ test/p2p/basic/CMakeLists.txt | 1 - test/p2p/basic/message_read_writer_test.cpp | 21 +-- .../plaintext_connection_test.cpp | 4 +- test/p2p/crypto/CMakeLists.txt | 4 +- test/p2p/crypto/hmac_test.cpp | 28 ++-- test/p2p/crypto/key_generator_test.cpp | 11 +- test/p2p/crypto/key_validator_test.cpp | 8 +- test/p2p/crypto/keys_test.cpp | 6 +- test/p2p/crypto/random_test.cpp | 4 +- test/p2p/multi/CMakeLists.txt | 1 - test/p2p/network/CMakeLists.txt | 1 - test/p2p/peer/key_book/CMakeLists.txt | 1 - test/p2p/security/CMakeLists.txt | 2 - test/testutil/CMakeLists.txt | 2 + test/testutil/clock/CMakeLists.txt | 8 ++ test/testutil/clock/clock.hpp | 51 ++++++++ test/testutil/clock/impl/clock_impl.cpp | 18 +++ test/testutil/clock/impl/clock_impl.hpp | 25 ++++ test/testutil/ma_generator.hpp | 2 +- 33 files changed, 503 insertions(+), 114 deletions(-) create mode 100644 p2p/common/visitor.hpp create mode 100644 test/deps/CMakeLists.txt create mode 100644 test/deps/di_test.cpp create mode 100644 test/deps/outcome_test.cpp create mode 100644 test/testutil/clock/CMakeLists.txt create mode 100644 test/testutil/clock/clock.hpp create mode 100644 test/testutil/clock/impl/clock_impl.cpp create mode 100644 test/testutil/clock/impl/clock_impl.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 68f77394..acd047cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,6 +170,7 @@ libp2p_install( keys_proto plaintext_protobuf logger + sha ) include(CMakePackageConfigHelpers) @@ -188,4 +189,4 @@ install(FILES export(PACKAGE libp2p) enable_testing() -#add_subdirectory(test) +add_subdirectory(test) diff --git a/p2p/common/visitor.hpp b/p2p/common/visitor.hpp new file mode 100644 index 00000000..3b50504e --- /dev/null +++ b/p2p/common/visitor.hpp @@ -0,0 +1,102 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef LIBP2P_VISITOR_HPP +#define LIBP2P_VISITOR_HPP + +#include // for std::decay +#include // for std::forward + +#include // for boost::apply_visitor + +namespace libp2p { + + template + struct lambda_visitor; + + template + struct lambda_visitor + : public Lambda, public lambda_visitor { + using Lambda::operator(); + using lambda_visitor::operator(); + + // NOLINTNEXTLINE(google-explicit-constructor) + lambda_visitor(Lambda lambda, Lambdas... lambdas) + : Lambda(lambda), lambda_visitor(lambdas...) {} + }; + + template + struct lambda_visitor : public Lambda { + using Lambda::operator(); + + // NOLINTNEXTLINE(google-explicit-constructor) + lambda_visitor(Lambda lambda) : Lambda(lambda) {} + }; + + /** + * @brief Convenient in-place compile-time visitor creation, from a set of + * lambdas + * + * @code + * make_visitor([](int a){ return 1; }, + * [](std::string b) { return 2; }); + * @nocode + * + * is essentially the same as + * + * @code + * struct visitor : public boost::static_visitor { + * int operator()(int a) { return 1; } + * int operator()(std::string b) { return 2; } + * } + * @nocode + * + * @param lambdas + * @return visitor + */ + template + constexpr auto make_visitor(Fs &&... fs) { + using visitor_type = lambda_visitor...>; + return visitor_type(std::forward(fs)...); + } + + /** + * @brief Inplace visitor for boost::variant. + * @code + * boost::variant value = "1234"; + * ... + * visit_in_place(value, + * [](int v) { std::cout << "(int)" << v; }, + * [](std::string v) { std::cout << "(string)" << v;} + * ); + * @nocode + * + * @param variant + * @param lambdas + * @param lambdas + */ + template + constexpr decltype(auto) visit_in_place(TVariant &&variant, + TVisitors &&... visitors) { + return boost::apply_visitor( + make_visitor(std::forward(visitors)...), + std::forward(variant)); + } + + /// apply Matcher to optional T + template + constexpr decltype(auto) match(T &&t, Matcher &&m) { + return std::forward(t) ? std::forward(m)(*std::forward(t)) + : std::forward(m)(); + } + + /// construct visitor from Fs and apply it to optional T + template + constexpr decltype(auto) match_in_place(T &&t, Fs &&... fs) { + return match(std::forward(t), make_visitor(std::forward(fs)...)); + } +} // namespace kagome + +#endif // LIBP2P_VISITOR_HPP diff --git a/p2p/crypto/CMakeLists.txt b/p2p/crypto/CMakeLists.txt index 1afb8146..c65ca6bf 100644 --- a/p2p/crypto/CMakeLists.txt +++ b/p2p/crypto/CMakeLists.txt @@ -5,11 +5,12 @@ add_subdirectory(aes_provider) add_subdirectory(hmac_provider) +add_subdirectory(key_generator) add_subdirectory(key_marshaller) +add_subdirectory(key_validator) add_subdirectory(protobuf) add_subdirectory(random_generator) -add_subdirectory(key_generator) -add_subdirectory(key_validator) +add_subdirectory(sha) add_library(crypto_error error.hpp diff --git a/p2p/peer/CMakeLists.txt b/p2p/peer/CMakeLists.txt index 812fa71a..30935e2a 100644 --- a/p2p/peer/CMakeLists.txt +++ b/p2p/peer/CMakeLists.txt @@ -30,7 +30,8 @@ target_link_libraries(libp2p_peer_id outcome multihash multibase_codec - hasher + sha +# hasher ) add_library(libp2p_peer_address diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5795f075..3e022457 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,7 +4,9 @@ # include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +include_directories(${PROJECT_SOURCE_DIR}) add_subdirectory(acceptance) +add_subdirectory(deps) add_subdirectory(p2p) add_subdirectory(testutil) diff --git a/test/acceptance/p2p/host/basic_host_test.cpp b/test/acceptance/p2p/host/basic_host_test.cpp index 1b427002..dfee487c 100644 --- a/test/acceptance/p2p/host/basic_host_test.cpp +++ b/test/acceptance/p2p/host/basic_host_test.cpp @@ -3,17 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "libp2p/host/basic_host/basic_host.hpp" +#include "p2p/host/basic_host/basic_host.hpp" #include -#include "mock/libp2p/connection/stream_mock.hpp" -#include "mock/libp2p/network/dialer_mock.hpp" -#include "mock/libp2p/network/listener_mock.hpp" -#include "mock/libp2p/network/network_mock.hpp" -#include "mock/libp2p/peer/address_repository_mock.hpp" -#include "mock/libp2p/peer/identity_manager_mock.hpp" -#include "mock/libp2p/peer/peer_repository_mock.hpp" +#include "mock/p2p/connection/stream_mock.hpp" +#include "mock/p2p/network/dialer_mock.hpp" +#include "mock/p2p/network/listener_mock.hpp" +#include "mock/p2p/network/network_mock.hpp" +#include "mock/p2p/peer/address_repository_mock.hpp" +#include "mock/p2p/peer/identity_manager_mock.hpp" +#include "mock/p2p/peer/peer_repository_mock.hpp" #include "testutil/gmock_actions.hpp" #include "testutil/literals.hpp" @@ -78,12 +78,18 @@ TEST_F(BasicHostTest, GetId) { * @then peer's info is returned */ TEST_F(BasicHostTest, GetPeerInfo) { - EXPECT_CALL(network, getListener()).Times(2).WillRepeatedly(ReturnRef(*listener)); + EXPECT_CALL(network, getListener()) + .Times(2) + .WillRepeatedly(ReturnRef(*listener)); EXPECT_CALL(*idmgr, getId()).Times(2).WillRepeatedly(ReturnRef(id)); EXPECT_CALL(repo, getAddressRepository()).WillOnce(ReturnRef(*addr_repo)); EXPECT_CALL(*addr_repo, getAddresses(id)).WillOnce(Return(mas)); - EXPECT_CALL(*listener, getListenAddresses()).Times(1).WillRepeatedly(Return(mas)); - EXPECT_CALL(*listener, getListenAddressesInterfaces()).Times(1).WillRepeatedly(Return(mas)); + EXPECT_CALL(*listener, getListenAddresses()) + .Times(1) + .WillRepeatedly(Return(mas)); + EXPECT_CALL(*listener, getListenAddressesInterfaces()) + .Times(1) + .WillRepeatedly(Return(mas)); auto pinfo = host->getPeerInfo(); auto expected = peer::PeerInfo{id, mas}; @@ -151,4 +157,3 @@ TEST_F(BasicHostTest, NewStream) { ASSERT_TRUE(executed); } - diff --git a/test/acceptance/p2p/host/host_integration_test.cpp b/test/acceptance/p2p/host/host_integration_test.cpp index 0845d536..72a25722 100644 --- a/test/acceptance/p2p/host/host_integration_test.cpp +++ b/test/acceptance/p2p/host/host_integration_test.cpp @@ -3,21 +3,21 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "libp2p/host/basic_host/basic_host.hpp" +#include "p2p/host/basic_host/basic_host.hpp" #include #include #include -#include "acceptance/libp2p/host/peer/test_peer.hpp" -#include "acceptance/libp2p/host/peer/tick_counter.hpp" +#include "acceptance/p2p/host/peer/test_peer.hpp" +#include "acceptance/p2p/host/peer/tick_counter.hpp" #include "testutil/ma_generator.hpp" using namespace libp2p; using std::chrono_literals::operator""s; using std::chrono_literals::operator""ms; -using Duration = kagome::clock::SteadyClockImpl::Duration; +using Duration = libp2p::clock::SteadyClockImpl::Duration; /** * @brief host integration test configuration @@ -60,11 +60,7 @@ struct HostIntegrationTest * @then all clients interact with all servers predefined number of times */ TEST_P(HostIntegrationTest, InteractAllToAllSuccess) { - const auto [peer_count, - ping_times, - start_port, - timeout, - future_timeout, + const auto [peer_count, ping_times, start_port, timeout, future_timeout, system_timeout] = GetParam(); const auto addr_prefix = "/ip4/127.0.0.1/tcp/"; testutil::MultiaddressGenerator ma_generator(addr_prefix, start_port); @@ -124,8 +120,7 @@ namespace { using Config = HostIntegrationTestConfig; } -INSTANTIATE_TEST_CASE_P(AllTestCases, - HostIntegrationTest, +INSTANTIATE_TEST_CASE_P(AllTestCases, HostIntegrationTest, ::testing::Values( // ports are not freed, so new ports each time Config{1u, 1u, 40510u, 2s, 2s, 200ms}, diff --git a/test/acceptance/p2p/host/peer/test_peer.cpp b/test/acceptance/p2p/host/peer/test_peer.cpp index 79cafa1d..c945c335 100644 --- a/test/acceptance/p2p/host/peer/test_peer.cpp +++ b/test/acceptance/p2p/host/peer/test_peer.cpp @@ -3,12 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "acceptance/libp2p/host/peer/test_peer.hpp" +#include "acceptance/p2p/host/peer/test_peer.hpp" #include -#include "acceptance/libp2p/host/peer/tick_counter.hpp" -#include "acceptance/libp2p/host/protocol/client_test_session.hpp" -#include "libp2p/security/plaintext/exchange_message_marshaller_impl.hpp" +#include "acceptance/p2p/host/peer/tick_counter.hpp" +#include "acceptance/p2p/host/protocol/client_test_session.hpp" +#include "p2p/security/plaintext/exchange_message_marshaller_impl.hpp" Peer::Peer(Peer::Duration timeout) : muxed_config_{1024576, 1000}, @@ -20,8 +20,7 @@ Peer::Peer(Peer::Duration timeout) key_generator_{ std::make_shared(*random_provider_)} { EXPECT_OUTCOME_TRUE_MSG( - keys, - key_generator_->generateKeys(crypto::Key::Type::ED25519), + keys, key_generator_->generateKeys(crypto::Key::Type::ED25519), "failed to generate keys"); host_ = makeHost(std::move(keys)); @@ -43,39 +42,31 @@ void Peer::startServer(const multi::Multiaddress &address, thread_ = std::thread([this] { context_->run_for(timeout_); }); } -void Peer::startClient(const peer::PeerInfo &pinfo, - size_t message_count, +void Peer::startClient(const peer::PeerInfo &pinfo, size_t message_count, Peer::sptr counter) { - context_->post([this, - server_id = pinfo.id.toBase58(), - pinfo, - message_count, + context_->post([this, server_id = pinfo.id.toBase58(), pinfo, message_count, counter = std::move(counter)]() mutable { this->host_->newStream( - pinfo, - echo_->getProtocolId(), - [server_id = std::move(server_id), - ping_times = message_count, + pinfo, echo_->getProtocolId(), + [server_id = std::move(server_id), ping_times = message_count, counter = std::move(counter)]( outcome::result> rstream) mutable { // get stream - EXPECT_OUTCOME_TRUE_MSG( - stream, rstream, "failed to connect to server: " + server_id); + EXPECT_OUTCOME_TRUE_MSG(stream, rstream, + "failed to connect to server: " + server_id); // make client session auto client = std::make_shared(stream, ping_times); // handle session client->handle( - [server_id = std::move(server_id), - client, + [server_id = std::move(server_id), client, counter = std::move(counter)]( outcome::result> res) mutable { // count message exchange counter->tick(); // ensure message returned EXPECT_OUTCOME_TRUE_MSG( - vec, - res, + vec, res, "failed to receive response from server: " + server_id); // ensure message is correct ASSERT_EQ(vec.size(), client->bufferSize()); // NOLINT @@ -145,6 +136,6 @@ Peer::sptr Peer::makeHost(crypto::KeyPair keyPair) { auto peer_repo = std::make_unique( std::move(addr_repo), std::move(key_repo), std::move(protocol_repo)); - return std::make_shared( - idmgr, std::move(network), std::move(peer_repo)); + return std::make_shared(idmgr, std::move(network), + std::move(peer_repo)); } diff --git a/test/acceptance/p2p/host/peer/test_peer.hpp b/test/acceptance/p2p/host/peer/test_peer.hpp index 12f44f49..48456cea 100644 --- a/test/acceptance/p2p/host/peer/test_peer.hpp +++ b/test/acceptance/p2p/host/peer/test_peer.hpp @@ -9,9 +9,9 @@ #include #include -#include "clock/impl/clock_impl.hpp" -#include "libp2p/injector/host_injector.hpp" -#include "libp2p/protocol/echo.hpp" +#include "p2p/injector/host_injector.hpp" +#include "p2p/protocol/echo.hpp" +#include "testutil/clock/impl/clock_impl.hpp" #include "testutil/outcome.hpp" using namespace libp2p; @@ -30,7 +30,7 @@ class Peer { using Stream = connection::Stream; public: - using Duration = kagome::clock::SteadyClockImpl::Duration; + using Duration = libp2p::clock::SteadyClockImpl::Duration; /** * @brief constructs peer @@ -52,8 +52,7 @@ class Peer { * @param message_count number of messages to send * @param tester object for testing purposes */ - void startClient(const peer::PeerInfo &pinfo, - size_t message_count, + void startClient(const peer::PeerInfo &pinfo, size_t message_count, sptr tester); /** diff --git a/test/acceptance/p2p/host/protocol/client_test_session.cpp b/test/acceptance/p2p/host/protocol/client_test_session.cpp index 6b5e45e0..396639dc 100644 --- a/test/acceptance/p2p/host/protocol/client_test_session.cpp +++ b/test/acceptance/p2p/host/protocol/client_test_session.cpp @@ -2,11 +2,11 @@ * Copyright Soramitsu Co., Ltd. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ -#include "acceptance/libp2p/host/protocol/client_test_session.hpp" +#include "acceptance/p2p/host/protocol/client_test_session.hpp" #include #include -#include "libp2p/crypto/random_generator/boost_generator.hpp" +#include "p2p/crypto/random_generator/boost_generator.hpp" namespace libp2p::protocol { @@ -34,8 +34,7 @@ namespace libp2p::protocol { write_buf_ = random_generator_->randomBytes(buffer_size_); - stream_->write(write_buf_, - buffer_size_, + stream_->write(write_buf_, buffer_size_, [self = shared_from_this(), cb{std::move(cb)}](outcome::result rw) mutable { if (!rw) { @@ -51,8 +50,7 @@ namespace libp2p::protocol { read_buf_ = std::vector(buffer_size_); - stream_->read(read_buf_, - buffer_size_, + stream_->read(read_buf_, buffer_size_, [self = shared_from_this(), cb{std::move(cb)}](outcome::result rr) mutable { if (!rr) { diff --git a/test/acceptance/p2p/host/protocol/client_test_session.hpp b/test/acceptance/p2p/host/protocol/client_test_session.hpp index b420e20d..6af87ab8 100644 --- a/test/acceptance/p2p/host/protocol/client_test_session.hpp +++ b/test/acceptance/p2p/host/protocol/client_test_session.hpp @@ -8,8 +8,8 @@ #include -#include "libp2p/connection/stream.hpp" -#include "libp2p/crypto/random_generator.hpp" +#include "p2p/connection/stream.hpp" +#include "p2p/crypto/random_generator.hpp" namespace libp2p::protocol { diff --git a/test/deps/CMakeLists.txt b/test/deps/CMakeLists.txt new file mode 100644 index 00000000..5f1927c9 --- /dev/null +++ b/test/deps/CMakeLists.txt @@ -0,0 +1,11 @@ +# +# Copyright Soramitsu Co., Ltd. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +# + +addtest(outcome_test outcome_test.cpp) +target_link_libraries(outcome_test + outcome + ) + +addtest(di_test di_test.cpp) diff --git a/test/deps/di_test.cpp b/test/deps/di_test.cpp new file mode 100644 index 00000000..c6e7e590 --- /dev/null +++ b/test/deps/di_test.cpp @@ -0,0 +1,63 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +class ctor { + public: + explicit ctor(int i) : i(i) {} + int i; +}; + +struct aggregate { + double d; +}; + +class example { + public: + virtual ~example() = default; + + example(aggregate a, const ctor &c) { + EXPECT_EQ(87.0, a.d); + EXPECT_EQ(42, c.i); + }; + + virtual void func() {} +}; + +struct Derived : public example { + ~Derived() override = default; + + void func() override {} +}; + +template +auto useBind(){ + return boost::di::bind.template to(); +} + +/** + * @brief If test compiles, then DI works. + */ +TEST(Boost, DI) { + using namespace boost; + + // clang-format off + const auto injector = di::make_injector( + di::bind.to(42)[boost::di::override], + di::bind.to(87.0), + useBind() + ); + // clang-format on + + auto a = injector.create(); + auto b = injector.create>(); + auto c = injector.create>(); + + (void)a; + (void)b; + (void)c; +} diff --git a/test/deps/outcome_test.cpp b/test/deps/outcome_test.cpp new file mode 100644 index 00000000..497a2c3d --- /dev/null +++ b/test/deps/outcome_test.cpp @@ -0,0 +1,120 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +using std::string_literals::operator""s; + +#define ILLEGAL_CHAR_MSG "illegal char"s +#define DIV_0_MSG "division by 0"s + +enum class ConversionErrc { + SUCCESS = 0, // 0 should not represent an error + EMPTY_STRING = 1, // (for rationale, see tutorial on error codes) + ILLEGAL_CHAR = 2, + TOO_LONG = 3, +}; + +namespace sooper::loong::ns { + enum class DivisionErrc { + DIVISION_BY_ZERO = 1, + }; +} + +OUTCOME_HPP_DECLARE_ERROR(ConversionErrc) +OUTCOME_CPP_DEFINE_CATEGORY(ConversionErrc, e) { + switch (e) { + case ConversionErrc::SUCCESS: + return "success"; + case ConversionErrc::EMPTY_STRING: + return "empty string"; + case ConversionErrc::ILLEGAL_CHAR: + return ILLEGAL_CHAR_MSG; + case ConversionErrc::TOO_LONG: + return "too long"; + default: + return "unknown"; + } +} + +OUTCOME_HPP_DECLARE_ERROR(sooper::loong::ns, DivisionErrc) +OUTCOME_CPP_DEFINE_CATEGORY(sooper::loong::ns, DivisionErrc, e) { + using sooper::loong::ns::DivisionErrc; + switch (e) { + case DivisionErrc::DIVISION_BY_ZERO: + return "division by 0"; + default: + return "unknown"; + } +} + +outcome::result convert(const std::string &str) { + if (str.empty()) + return ConversionErrc::EMPTY_STRING; + + if (!std::all_of(str.begin(), str.end(), ::isdigit)) + return ConversionErrc::ILLEGAL_CHAR; + + if (str.length() > 9) + return ConversionErrc::TOO_LONG; + + return atoi(str.c_str()); +} + +outcome::result divide(int a, int b) { + using sooper::loong::ns::DivisionErrc; + if (b == 0) + return DivisionErrc::DIVISION_BY_ZERO; + + return a / b; +} + +outcome::result convert_and_divide(const std::string &a, + const std::string &b) { + OUTCOME_TRY(valA, convert(a)); + OUTCOME_TRY(valB, convert(b)); + OUTCOME_TRY(valDiv, divide(valA, valB)); + return valDiv; +} + +/** + * @given valid arguments for convert_and_divide + * @when execute method which returns result + * @then returns value + */ +TEST(Outcome, CorrectCase) { + auto r = convert_and_divide("500", "2"); + ASSERT_TRUE(r); + auto &&val = r.value(); + ASSERT_EQ(val, 250); +} + +/** + * @given arguments to cause conversion error for convert_and_divide + * @when execute method which returns result + * @then returns error + */ +TEST(Outcome, ConversionError) { + auto r = convert_and_divide("500", "a"); + ASSERT_FALSE(r); + auto &&err = r.error(); + ASSERT_EQ(err.message(), ILLEGAL_CHAR_MSG); +} + +/** + * @given arguments to cause division error for convert_and_divide + * @when execute method which returns result + * @then returns error + */ +TEST(Outcome, DivisionError) { + auto r = convert_and_divide("500", "0"); + ASSERT_FALSE(r); + auto &&err = r.error(); + ASSERT_EQ(err.message(), DIV_0_MSG); // name of the enum + using sooper::loong::ns::DivisionErrc; + ASSERT_EQ(err.category().name(), typeid(DivisionErrc).name()); +} diff --git a/test/p2p/basic/CMakeLists.txt b/test/p2p/basic/CMakeLists.txt index 093998c8..b5ff2826 100644 --- a/test/p2p/basic/CMakeLists.txt +++ b/test/p2p/basic/CMakeLists.txt @@ -12,5 +12,4 @@ target_link_libraries(message_read_writer_test message_read_writer_error uvarint outcome - buffer ) diff --git a/test/p2p/basic/message_read_writer_test.cpp b/test/p2p/basic/message_read_writer_test.cpp index d1d9e41c..2e451e5a 100644 --- a/test/p2p/basic/message_read_writer_test.cpp +++ b/test/p2p/basic/message_read_writer_test.cpp @@ -3,12 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "libp2p/basic/message_read_writer.hpp" +#include "p2p/basic/message_read_writer.hpp" #include -#include "libp2p/basic/protobuf_message_read_writer.hpp" -#include "libp2p/multi/uvarint.hpp" -#include "mock/libp2p/connection/raw_connection_mock.hpp" +#include "mock/p2p/connection/raw_connection_mock.hpp" +#include "p2p/basic/protobuf_message_read_writer.hpp" +#include "p2p/multi/uvarint.hpp" #include "testutil/gmock_actions.hpp" using namespace libp2p; @@ -16,7 +16,7 @@ using namespace basic; using namespace connection; using namespace multi; -using kagome::common::Buffer; +using libp2p::common::ByteArray; using testing::_; using testing::Return; @@ -31,10 +31,13 @@ class MessageReadWriterTest : public testing::Test { static constexpr uint64_t kMsgLength = 4; UVarint len_varint_ = UVarint{kMsgLength}; - Buffer msg_bytes_{0x11, 0x22, 0x33, 0x44}; + ByteArray msg_bytes_{0x11, 0x22, 0x33, 0x44}; - Buffer msg_with_varint_bytes_ = - Buffer{}.put(len_varint_.toBytes()).put(msg_bytes_); + ByteArray msg_with_varint_bytes_ = [this]() -> ByteArray { + ByteArray buffer = len_varint_.toVector(); + buffer.insert(buffer.cend(), msg_bytes_.begin(), msg_bytes_.end()); + return buffer; + }(); bool operation_completed_ = false; }; @@ -53,7 +56,7 @@ TEST_F(MessageReadWriterTest, Read) { msg_rw_->read([this](auto &&res) { ASSERT_TRUE(res); - ASSERT_EQ(*res.value(), msg_bytes_.toVector()); + ASSERT_EQ(*res.value(), msg_bytes_); operation_completed_ = true; }); diff --git a/test/p2p/connection/security_conn/plaintext_connection_test.cpp b/test/p2p/connection/security_conn/plaintext_connection_test.cpp index 4458e34b..103e1f8d 100644 --- a/test/p2p/connection/security_conn/plaintext_connection_test.cpp +++ b/test/p2p/connection/security_conn/plaintext_connection_test.cpp @@ -3,11 +3,11 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "libp2p/security/plaintext/plaintext_connection.hpp" +#include "p2p/security/plaintext/plaintext_connection.hpp" #include #include -#include "mock/libp2p/connection/raw_connection_mock.hpp" +#include "mock/p2p/connection/raw_connection_mock.hpp" #include "testutil/gmock_actions.hpp" #include "testutil/literals.hpp" diff --git a/test/p2p/crypto/CMakeLists.txt b/test/p2p/crypto/CMakeLists.txt index e8e79f1d..adfe0bcd 100644 --- a/test/p2p/crypto/CMakeLists.txt +++ b/test/p2p/crypto/CMakeLists.txt @@ -6,9 +6,6 @@ addtest(keys_test keys_test.cpp ) -target_link_libraries(keys_test - buffer - ) addtest(aes_test aes_test.cpp @@ -22,6 +19,7 @@ addtest(hmac_test ) target_link_libraries(hmac_test hmac_provider + testutil ) addtest(random_test diff --git a/test/p2p/crypto/hmac_test.cpp b/test/p2p/crypto/hmac_test.cpp index 787f6520..0c287333 100644 --- a/test/p2p/crypto/hmac_test.cpp +++ b/test/p2p/crypto/hmac_test.cpp @@ -3,47 +3,49 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "libp2p/crypto/hmac_provider/hmac_provider_impl.hpp" +#include "p2p/crypto/hmac_provider/hmac_provider_impl.hpp" #include #include +#include "common/types.hpp" +#include "p2p/crypto/common.hpp" +#include "p2p/crypto/error.hpp" #include "testutil/literals.hpp" -#include "common/buffer.hpp" -#include "libp2p/crypto/common.hpp" -#include "libp2p/crypto/error.hpp" -using kagome::common::Buffer; +using libp2p::common::ByteArray; using namespace libp2p::crypto; class HmacTest : public testing::Test { protected: void SetUp() override { // use the same message for all tests - message.put("The fly got to the jam that's all the poem"); + std::string m = "The fly got to the jam that's all the poem"; + message.insert(message.end(), m.begin(), m.end()); } /// hash provider hmac::HmacProviderImpl provider; /// message to be hashed - Buffer message; + ByteArray message; - Buffer sha1_key{"55cd433be9568ee79525a0919cf4b31c28108cee"_unhex}; // 20 bytes + ByteArray sha1_key{ + "55cd433be9568ee79525a0919cf4b31c28108cee"_unhex}; // 20 bytes - Buffer sha256_key{ + ByteArray sha256_key{ "a1990aeb68efb1b59d3165795f6338960aa7238ba74779ea5df3a435fdbb8d4c"_unhex}; // 32 bytes - Buffer sha512_key{ + ByteArray sha512_key{ // 64 bytes "dd114c7351b2186aeba2d3fb4d96496da9e1681ae6272df553a8235a05e6f1ae" "66d5c4efa32cdfbf1b0f3b9542c14444a523859cde43736c7b5b899803d1a96a"_unhex}; - Buffer sha1_dgst{ + ByteArray sha1_dgst{ "42985601b3d61125e02bcca5a4dcb9e3763bc942"_unhex}; // 20 bytes - Buffer sha256_dgst{ + ByteArray sha256_dgst{ "bdb5a9c8f3e08fdb8c0ee7189d76fd6c487d5789e0141850bcc945558488097a"_unhex}; // 32 bytes - Buffer sha512_dgst{ + ByteArray sha512_dgst{ "0f5bf6af4943b35b76d7d89714b681900e03262e997f2519befd7b1cb0cb56e8" "e648fa297ba1855382123240f6cded44174b851b94665b9a56b249d4d88deb63"_unhex}; // 64 bytes }; diff --git a/test/p2p/crypto/key_generator_test.cpp b/test/p2p/crypto/key_generator_test.cpp index 5a6d3b9d..3a93643a 100644 --- a/test/p2p/crypto/key_generator_test.cpp +++ b/test/p2p/crypto/key_generator_test.cpp @@ -3,17 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "libp2p/crypto/key_generator/key_generator_impl.hpp" +#include "p2p/crypto/key_generator/key_generator_impl.hpp" #include #include #include #include -#include "libp2p/crypto/error.hpp" -#include "libp2p/crypto/random_generator/boost_generator.hpp" +#include "p2p/crypto/error.hpp" +#include "p2p/crypto/random_generator/boost_generator.hpp" #include "testutil/outcome.hpp" -using kagome::common::Buffer; +using libp2p::common::ByteArray; using libp2p::crypto::Key; using libp2p::crypto::KeyGeneratorError; using libp2p::crypto::KeyGeneratorImpl; @@ -87,7 +87,8 @@ INSTANTIATE_TEST_CASE_P( std::tuple(Key::Type::SECP256K1, 32, 33))); /** - * @given key generator and tuple of + * @given key generator and tuple of * @when key pair is generated * @then public and private key lengths are equal to those in parameters */ diff --git a/test/p2p/crypto/key_validator_test.cpp b/test/p2p/crypto/key_validator_test.cpp index 47e95de6..7749d0af 100644 --- a/test/p2p/crypto/key_validator_test.cpp +++ b/test/p2p/crypto/key_validator_test.cpp @@ -3,13 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "libp2p/crypto/key_validator/key_validator_impl.hpp" +#include "p2p/crypto/key_validator/key_validator_impl.hpp" #include #include -#include "libp2p/crypto/key_generator/key_generator_impl.hpp" -#include "libp2p/crypto/key_validator/key_validator_impl.hpp" -#include "libp2p/crypto/random_generator/boost_generator.hpp" +#include "p2p/crypto/key_generator/key_generator_impl.hpp" +#include "p2p/crypto/key_validator/key_validator_impl.hpp" +#include "p2p/crypto/random_generator/boost_generator.hpp" #include "testutil/outcome.hpp" using ::testing::_; diff --git a/test/p2p/crypto/keys_test.cpp b/test/p2p/crypto/keys_test.cpp index ee7acdb2..6b596530 100644 --- a/test/p2p/crypto/keys_test.cpp +++ b/test/p2p/crypto/keys_test.cpp @@ -5,11 +5,9 @@ #include #include -#include "common/buffer.hpp" -#include "libp2p/crypto/common.hpp" -#include "libp2p/crypto/key.hpp" +#include "p2p/crypto/common.hpp" +#include "p2p/crypto/key.hpp" -using kagome::common::Buffer; using namespace libp2p::crypto; /** diff --git a/test/p2p/crypto/random_test.cpp b/test/p2p/crypto/random_test.cpp index 3b566390..4f3eadb1 100644 --- a/test/p2p/crypto/random_test.cpp +++ b/test/p2p/crypto/random_test.cpp @@ -5,10 +5,10 @@ #include "libp2p/crypto/random_generator/boost_generator.hpp" +#include + #include -#include "common/buffer.hpp" -using kagome::common::Buffer; using libp2p::crypto::random::BoostRandomGenerator; using libp2p::crypto::random::RandomGenerator; diff --git a/test/p2p/multi/CMakeLists.txt b/test/p2p/multi/CMakeLists.txt index aed5a60d..7f7332c1 100644 --- a/test/p2p/multi/CMakeLists.txt +++ b/test/p2p/multi/CMakeLists.txt @@ -25,7 +25,6 @@ addtest(multiaddress_test ) target_link_libraries(multiaddress_test multiaddress - buffer ) addtest(multibase_codec_test diff --git a/test/p2p/network/CMakeLists.txt b/test/p2p/network/CMakeLists.txt index bf19ec12..0c89d5a3 100644 --- a/test/p2p/network/CMakeLists.txt +++ b/test/p2p/network/CMakeLists.txt @@ -9,7 +9,6 @@ addtest(router_test target_link_libraries(router_test libp2p_router multiaddress - buffer libp2p_peer_id ) diff --git a/test/p2p/peer/key_book/CMakeLists.txt b/test/p2p/peer/key_book/CMakeLists.txt index 06a5bf77..11614cfd 100644 --- a/test/p2p/peer/key_book/CMakeLists.txt +++ b/test/p2p/peer/key_book/CMakeLists.txt @@ -4,5 +4,4 @@ addtest(inmem_key_repository_test inmem_key_repository_test.cpp) target_link_libraries(inmem_key_repository_test libp2p_inmem_key_repository - buffer ) diff --git a/test/p2p/security/CMakeLists.txt b/test/p2p/security/CMakeLists.txt index 66ee8b2e..5066c243 100644 --- a/test/p2p/security/CMakeLists.txt +++ b/test/p2p/security/CMakeLists.txt @@ -8,7 +8,6 @@ addtest(plaintext_adaptor_test ) target_link_libraries(plaintext_adaptor_test libp2p_plaintext - buffer libp2p_peer_id multihash multiaddress @@ -21,7 +20,6 @@ addtest(plaintext_exchange_message_marshaller_test plaintext_exchange_message_marshaller_test.cpp ) target_link_libraries(plaintext_exchange_message_marshaller_test - buffer libp2p_peer_id multihash multiaddress diff --git a/test/testutil/CMakeLists.txt b/test/testutil/CMakeLists.txt index b56b358e..f4a0b31e 100644 --- a/test/testutil/CMakeLists.txt +++ b/test/testutil/CMakeLists.txt @@ -1,9 +1,11 @@ # Copyright Soramitsu Co., Ltd. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 +add_subdirectory(clock) add_subdirectory(libp2p) add_library(testutil INTERFACE) target_link_libraries(testutil INTERFACE testutil_peer + clock ) diff --git a/test/testutil/clock/CMakeLists.txt b/test/testutil/clock/CMakeLists.txt new file mode 100644 index 00000000..1d2be422 --- /dev/null +++ b/test/testutil/clock/CMakeLists.txt @@ -0,0 +1,8 @@ +# +# Copyright Soramitsu Co., Ltd. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +# + +add_library(clock + impl/clock_impl.cpp + ) diff --git a/test/testutil/clock/clock.hpp b/test/testutil/clock/clock.hpp new file mode 100644 index 00000000..1cd18c98 --- /dev/null +++ b/test/testutil/clock/clock.hpp @@ -0,0 +1,51 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef LIBP2P_CLOCK_HPP +#define LIBP2P_CLOCK_HPP + +#include + +namespace libp2p::clock { + + /** + * An interface for a clock + * @tparam clock type is an underlying clock type, such as std::steady_clock + */ + template + class Clock { + public: + /** + * Difference between two time points + */ + using Duration = typename ClockType::duration; + /** + * A moment in time, stored in milliseconds since Unix epoch start + */ + using TimePoint = typename ClockType::time_point; + + virtual ~Clock() = default; + + /** + * @return a time point representing the current time + */ + virtual TimePoint now() const = 0; + }; + + /** + * SteadyClock alias over Clock. Should be used when we need to measure + * interval between two moments in time + */ + using SteadyClock = Clock; + + /** + * SystemClock alias over Clock. Should be used when we need to watch current + * time + */ + using SystemClock = Clock; + +} // namespace libp2p::clock + +#endif // LIBP2P_CLOCK_HPP diff --git a/test/testutil/clock/impl/clock_impl.cpp b/test/testutil/clock/impl/clock_impl.cpp new file mode 100644 index 00000000..fd877815 --- /dev/null +++ b/test/testutil/clock/impl/clock_impl.cpp @@ -0,0 +1,18 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "testutil/clock/impl/clock_impl.hpp" + +namespace libp2p::clock { + + template + typename Clock::TimePoint ClockImpl::now() const { + return ClockType::now(); + } + + template class ClockImpl; + template class ClockImpl; + +} // namespace libp2p::clock diff --git a/test/testutil/clock/impl/clock_impl.hpp b/test/testutil/clock/impl/clock_impl.hpp new file mode 100644 index 00000000..0b871a54 --- /dev/null +++ b/test/testutil/clock/impl/clock_impl.hpp @@ -0,0 +1,25 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef LIBP2P_CLOCK_IMPL_CLOCK_IMPL_HPP +#define LIBP2P_CLOCK_IMPL_CLOCK_IMPL_HPP + +#include "testutil/clock/clock.hpp" + +namespace libp2p::clock { + + template + class ClockImpl : public Clock { + public: + typename Clock::TimePoint now() const override; + }; + + // aliases for implementations + using SteadyClockImpl = ClockImpl; + using SystemClockImpl = ClockImpl; + +} // namespace libp2p::clock + +#endif // LIBP2P_CLOCK_IMPL_CLOCK_IMPL_HPP diff --git a/test/testutil/ma_generator.hpp b/test/testutil/ma_generator.hpp index d596bd72..14bc45d0 100644 --- a/test/testutil/ma_generator.hpp +++ b/test/testutil/ma_generator.hpp @@ -8,7 +8,7 @@ #include -#include "libp2p/multi/multiaddress.hpp" +#include "p2p/multi/multiaddress.hpp" #include "testutil/outcome.hpp" namespace testutil {