diff options
Diffstat (limited to 'test/internal_routing_disabled_acceptance_test')
10 files changed, 525 insertions, 0 deletions
diff --git a/test/internal_routing_disabled_acceptance_test/CMakeLists.txt b/test/internal_routing_disabled_acceptance_test/CMakeLists.txt new file mode 100644 index 0000000..c823350 --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.4...3.22) + +# Workaround for version range in cmake_minimum_required() before 3.12 +if(${CMAKE_VERSION} VERSION_LESS 3.12) + cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) +endif() + +project(internal_routing_disabled_acceptance_test LANGUAGES CXX) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_INIT} -pedantic -Wall -Wconversion -Wextra") +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(THREADS_PREFER_PTHREAD_FLAG ON) + +find_package(Threads REQUIRED) +find_package(Boost 1.55 COMPONENTS system REQUIRED) + +add_executable(${PROJECT_NAME} applet.cpp client.cpp server.cpp main.cpp) +target_include_directories(${PROJECT_NAME} PRIVATE ${gtest_SOURCE_DIR}/include) +target_link_libraries(${PROJECT_NAME} PRIVATE gtest Threads::Threads vsomeip3 ${Boost_LIBRARIES}) + +enable_testing() +add_test(NAME ${PROJECT_NAME} COMMAND $<TARGET_FILE:${PROJECT_NAME}>) +add_dependencies(build_network_tests ${PROJECT_NAME}) +configure_file(vsomeip.json vsomeip.json COPYONLY) +set_property( + TEST ${PROJECT_NAME} + APPEND PROPERTY ENVIRONMENT + "LD_LIBRARY_PATH=$<TARGET_FILE_DIR:vsomeip3>" + "VSOMEIP_CONFIGURATION=${CMAKE_CURRENT_BINARY_DIR}/vsomeip.json" +) +set_property( + TEST ${PROJECT_NAME} + APPEND PROPERTY TIMEOUT + 60 +) diff --git a/test/internal_routing_disabled_acceptance_test/applet.cpp b/test/internal_routing_disabled_acceptance_test/applet.cpp new file mode 100644 index 0000000..50bf41c --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/applet.cpp @@ -0,0 +1,44 @@ +#include "applet.hpp" + +#include <stdexcept> +#include <string> + +#include <vsomeip/enumeration_types.hpp> +#include <vsomeip/runtime.hpp> + +applet::applet(std::string_view name) : application{vsomeip_v3::runtime::get()->create_application(std::string{name})} +{ + if(!this->application->init()) + { + using namespace std::string_literals; + throw std::runtime_error{__func__ + "(): vSomeIP application init failure"s}; + } + + this->async_start = std::async( + std::launch::async, + &vsomeip_v3::application::start, + this->application + ); + + this->application->register_state_handler( + [this](vsomeip_v3::state_type_e state){ + switch(state) + { + case vsomeip_v3::state_type_e::ST_REGISTERED: + return this->on_state_registered(); + case vsomeip_v3::state_type_e::ST_DEREGISTERED: + return this->on_state_deregistered(); + } + } + ); +} + +applet::~applet() +{ + this->application->clear_all_handler(); + this->application->stop(); + this->async_start.wait(); +} + +void applet::on_state_registered() {} +void applet::on_state_deregistered() {} diff --git a/test/internal_routing_disabled_acceptance_test/applet.hpp b/test/internal_routing_disabled_acceptance_test/applet.hpp new file mode 100644 index 0000000..d536dc0 --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/applet.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include <future> +#include <memory> +#include <string_view> + +#include <vsomeip/application.hpp> + +struct applet +{ +protected: + std::shared_ptr<vsomeip_v3::application> application; + + applet(std::string_view name); + virtual ~applet(); + +private: + std::future<void> async_start; + + virtual void on_state_registered(); + virtual void on_state_deregistered(); +}; diff --git a/test/internal_routing_disabled_acceptance_test/client.cpp b/test/internal_routing_disabled_acceptance_test/client.cpp new file mode 100644 index 0000000..ff7be78 --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/client.cpp @@ -0,0 +1,152 @@ +#include "client.hpp" + +#include <chrono> +#include <iostream> +#include <thread> + +#include <vsomeip/constants.hpp> +#include <vsomeip/enumeration_types.hpp> +#include <vsomeip/message.hpp> +#include <vsomeip/payload.hpp> +#include <vsomeip/primitive_types.hpp> +#include <vsomeip/runtime.hpp> + +#include "config.hpp" + +client::client() : applet{"client"}, counter_event_received{}, counter_method_request{}, counter_method_response{} +{ + this->application->register_message_handler( + config::SERVICE_ID, + config::INSTANCE_ID, + vsomeip_v3::ANY_METHOD, + [this](const std::shared_ptr<vsomeip_v3::message>& message){ + std::shared_ptr runtime = vsomeip_v3::runtime::get(); + std::shared_ptr payload = message->get_payload(); + + switch(message->get_message_type()) + { + case vsomeip_v3::message_type_e::MT_RESPONSE: + std::cout + << "received:\n" + << "\tservice: " << std::hex << message->get_service() << '\n' + << "\tinstance: " << std::hex << message->get_instance() << '\n' + << "\tmethod: " << std::hex << message->get_method() << '\n' + << "\tpayload: " << payload->get_data() << '\n'; + this->counter_method_response++; + break; + + case vsomeip_v3::message_type_e::MT_NOTIFICATION: + std::cout << "GOT NOTIFICATION\n"; + this->counter_event_received++; + [[fallthrough]]; + + default: + std::cout << "unhandled message type: " << unsigned(message->get_message_type()) << '\n'; + } + } + ); + + this->application->register_availability_handler( + config::SERVICE_ID, + config::INSTANCE_ID, + [this](vsomeip_v3::service_t service, vsomeip_v3::instance_t instance, bool available){ + std::cout + << __func__ << '(' + << std::hex << service << ", " + << std::hex << instance << ", " + << std::boolalpha << available << ")\n"; + + if(service != config::SERVICE_ID) + return; + if(instance != config::INSTANCE_ID) + return; + if(!available) + return; + + std::shared_ptr runtime = vsomeip_v3::runtime::get(); + + std::shared_ptr payload = runtime->create_payload(); + constexpr vsomeip_v3::byte_t str[]{"hello world"}; + payload->set_data(str, sizeof(str)); + + std::shared_ptr request = runtime->create_request(); + request->set_service(config::SERVICE_ID); + request->set_instance(config::INSTANCE_ID); + request->set_method(config::METHOD_ID); + request->set_payload(payload); + + for(int i = 0; i < 10; i++) + { + std::cout << "sending: " << str << '\n'; + this->application->send(request); + this->counter_method_request++; + + using namespace std::chrono_literals; + std::this_thread::sleep_for(1s); + } + } + ); + + this->application->request_event( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENT_ID, + {config::EVENTGROUP_ID}, + vsomeip_v3::event_type_e::ET_FIELD, + vsomeip_v3::reliability_type_e::RT_UNRELIABLE + ); + + this->application->subscribe( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENTGROUP_ID + ); +} + +client::~client() +{ + this->application->unsubscribe( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENTGROUP_ID + ); + + this->application->release_event( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENT_ID + ); + + this->application->release_service( + config::SERVICE_ID, + config::INSTANCE_ID + ); +} + +std::size_t client::get_event_count() noexcept +{ + return this->counter_event_received; +} + +std::size_t client::get_method_request_count() noexcept +{ + return this->counter_method_request; +} + +std::size_t client::get_method_response_count() noexcept +{ + return this->counter_method_response; +} + +void client::on_state_registered() +{ + this->application->request_service( + config::SERVICE_ID, + config::INSTANCE_ID + ); +} + +void client::on_state_deregistered() +{ + std::cout << "Client is deregistered!!! Probably could not be registered!!!\n"; +} diff --git a/test/internal_routing_disabled_acceptance_test/client.hpp b/test/internal_routing_disabled_acceptance_test/client.hpp new file mode 100644 index 0000000..f6eba65 --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/client.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include <atomic> +#include <cstddef> + +#include "applet.hpp" + +struct client final : applet +{ + client(); + ~client(); + + std::size_t get_event_count() noexcept; + std::size_t get_method_request_count() noexcept; + std::size_t get_method_response_count() noexcept; + +private: + void on_state_registered() override; + void on_state_deregistered() override; + + std::atomic_size_t counter_event_received; + std::atomic_size_t counter_method_request; + std::atomic_size_t counter_method_response; +}; diff --git a/test/internal_routing_disabled_acceptance_test/config.hpp b/test/internal_routing_disabled_acceptance_test/config.hpp new file mode 100644 index 0000000..50dab14 --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/config.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include <vsomeip/primitive_types.hpp> + +namespace config +{ + constexpr vsomeip_v3::service_t SERVICE_ID = 0x2222; + constexpr vsomeip_v3::instance_t INSTANCE_ID = 0x3333; + constexpr vsomeip_v3::method_t METHOD_ID = 0x4444; + constexpr vsomeip_v3::event_t EVENT_ID = 0x5555; + constexpr vsomeip_v3::eventgroup_t EVENTGROUP_ID = 0x6666; +} diff --git a/test/internal_routing_disabled_acceptance_test/main.cpp b/test/internal_routing_disabled_acceptance_test/main.cpp new file mode 100644 index 0000000..7a2e7fc --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/main.cpp @@ -0,0 +1,43 @@ +#include <chrono> +#include <iostream> +#include <thread> + +#include <gtest/gtest.h> + +#include "client.hpp" +#include "server.hpp" + +TEST(internal_routing_disabled_acceptance_test, check_connectivity) +{ + server s; + client c; + + using namespace std::chrono_literals; + std::this_thread::sleep_for(15s); + + std::cout + << "[server]\n" + << "\tevents: " << s.get_event_count() << '\n' + << "\tmethod requests: " << s.get_method_request_count() << '\n' + << "\tmethod responses: " << s.get_method_response_count() << '\n'; + + std::cout + << "[client]\n" + << "\tevents: " << c.get_event_count() << '\n' + << "\tmethod requests: " << c.get_method_request_count() << '\n' + << "\tmethod responses: " << c.get_method_response_count() << '\n'; + + EXPECT_EQ(s.get_event_count(), 10); + EXPECT_EQ(s.get_method_request_count(), 0); + EXPECT_EQ(s.get_method_response_count(), 0); + + EXPECT_EQ(c.get_event_count(), 0); + EXPECT_EQ(c.get_method_request_count(), 0); + EXPECT_EQ(c.get_method_response_count(), 0); +} + +int main(int count, char** values) +{ + testing::InitGoogleTest(&count, values); + return RUN_ALL_TESTS(); +} diff --git a/test/internal_routing_disabled_acceptance_test/server.cpp b/test/internal_routing_disabled_acceptance_test/server.cpp new file mode 100644 index 0000000..6e00b1e --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/server.cpp @@ -0,0 +1,132 @@ +#include "server.hpp" + +#include <chrono> +#include <iostream> +#include <thread> + +#include <vsomeip/enumeration_types.hpp> +#include <vsomeip/message.hpp> +#include <vsomeip/payload.hpp> +#include <vsomeip/runtime.hpp> + +#include "config.hpp" + +server::server() : applet{"server"}, counter_event_sent{}, counter_method_request{}, counter_method_response{} +{ + this->application->register_message_handler( + config::SERVICE_ID, + config::INSTANCE_ID, + config::METHOD_ID, + [this](const std::shared_ptr<vsomeip_v3::message>& message){ + std::shared_ptr runtime = vsomeip_v3::runtime::get(); + std::shared_ptr payload = message->get_payload(); + + switch(message->get_message_type()) + { + case vsomeip_v3::message_type_e::MT_REQUEST: + std::cout << "GOT REQUEST\n"; + this->counter_method_request++; + { + std::shared_ptr response = runtime->create_response(message); + response->set_payload(payload); + + this->application->send(response); + this->counter_method_response++; + + this->application->notify( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENT_ID, + payload, + true + ); + this->counter_event_sent++; + } + break; + + default: + std::cout << "unhandled message type: " << unsigned(message->get_message_type()) << '\n'; + } + } + ); + + this->application->offer_event( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENT_ID, + {config::EVENTGROUP_ID}, + vsomeip_v3::event_type_e::ET_FIELD, + {}, + false, + true, + nullptr, + vsomeip_v3::reliability_type_e::RT_UNRELIABLE + ); + + std::thread{ + [this]{ + using namespace std::chrono_literals; + std::this_thread::sleep_for(1s); + + std::shared_ptr runtime = vsomeip_v3::runtime::get(); + std::shared_ptr payload = runtime->create_payload(); + for(int i = 0; i < 10; i++) + { + int j = i | 0x30; + payload->set_data(reinterpret_cast<vsomeip_v3::byte_t*>(&j), sizeof(j)); + this->application->notify( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENT_ID, + payload, + true + ); + this->counter_event_sent++; + + std::this_thread::sleep_for(1s); + } + } + }.detach(); +} + +server::~server() +{ + this->application->stop_offer_event( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENT_ID + ); + + this->application->stop_offer_service( + config::SERVICE_ID, + config::INSTANCE_ID + ); +} + +std::size_t server::get_event_count() noexcept +{ + return this->counter_event_sent; +} + +std::size_t server::get_method_request_count() noexcept +{ + return this->counter_method_request; +} + +std::size_t server::get_method_response_count() noexcept +{ + return this->counter_method_response; +} + +void server::on_state_registered() +{ + this->application->offer_service( + config::SERVICE_ID, + config::INSTANCE_ID + ); +} + +void server::on_state_deregistered() +{ + std::cout << "Server is deregistered!!! Probably could not be registered!!!\n"; +} diff --git a/test/internal_routing_disabled_acceptance_test/server.hpp b/test/internal_routing_disabled_acceptance_test/server.hpp new file mode 100644 index 0000000..53685f3 --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/server.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include <atomic> +#include <cstddef> + +#include "applet.hpp" + +struct server final : applet +{ + server(); + ~server(); + + std::size_t get_event_count() noexcept; + std::size_t get_method_request_count() noexcept; + std::size_t get_method_response_count() noexcept; + +private: + void on_state_registered() override; + void on_state_deregistered() override; + + std::atomic_size_t counter_event_sent; + std::atomic_size_t counter_method_request; + std::atomic_size_t counter_method_response; +}; diff --git a/test/internal_routing_disabled_acceptance_test/vsomeip.json b/test/internal_routing_disabled_acceptance_test/vsomeip.json new file mode 100644 index 0000000..f8ec145 --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/vsomeip.json @@ -0,0 +1,35 @@ +{ + "logging": { + "level": "trace", + "console": "true" + }, + "applications": [ + { + "name": "server", + "id": "0x1001" + }, + { + "name": "client", + "id": "0x1002" + } + ], + "services": [ + { + "service": "0x2222", + "instance": "0x3333", + "unreliable": 12345 + } + ], + "routing": { + "enabled": "false", + "host": "server", + "address": "127.0.0.1", + "port": 1270 + }, + "service-discovery": { + "enable": "true", + "multicast": "224.244.224.244", + "port": 2240, + "protocol": "udp" + } +} |