1 // Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6 #include <chrono>
7 #include <condition_variable>
8 #include <iomanip>
9 #include <iostream>
10 #include <sstream>
11 #include <thread>
12
13 #include <gtest/gtest.h>
14
15 #include <vsomeip/vsomeip.hpp>
16
17 #include "../someip_test_globals.hpp"
18
19 class magic_cookies_test_service {
20 public:
magic_cookies_test_service(bool _use_static_routing)21 magic_cookies_test_service(bool _use_static_routing) :
22 app_(vsomeip::runtime::get()->create_application()),
23 is_registered_(false),
24 use_static_routing_(_use_static_routing),
25 blocked_(false),
26 offer_thread_(std::bind(&magic_cookies_test_service::run, this)) {
27 }
28
~magic_cookies_test_service()29 ~magic_cookies_test_service() {
30 offer_thread_.join();
31 }
init()32 void init() {
33 std::lock_guard<std::mutex> its_lock(mutex_);
34
35 if (!app_->init()) {
36 ADD_FAILURE() << "Couldn't initialize application";
37 exit(EXIT_FAILURE);
38 }
39 app_->register_message_handler(
40 vsomeip_test::TEST_SERVICE_SERVICE_ID,
41 vsomeip_test::TEST_SERVICE_INSTANCE_ID,
42 vsomeip_test::TEST_SERVICE_METHOD_ID,
43 std::bind(&magic_cookies_test_service::on_message, this,
44 std::placeholders::_1));
45
46 app_->register_state_handler(
47 std::bind(&magic_cookies_test_service::on_state, this,
48 std::placeholders::_1));
49
50 VSOMEIP_INFO<< "Static routing " << (use_static_routing_ ? "ON" : "OFF");
51 }
52
start()53 void start() {
54 app_->start();
55 }
56
offer()57 void offer() {
58 app_->offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
59 }
60
stop_offer()61 void stop_offer() {
62 app_->stop_offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
63 }
64
on_state(vsomeip::state_type_e _state)65 void on_state(vsomeip::state_type_e _state) {
66 VSOMEIP_INFO << "Application " << app_->get_name() << " is "
67 << (_state == vsomeip::state_type_e::ST_REGISTERED ?
68 "registered." : "deregistered.");
69
70 if (_state == vsomeip::state_type_e::ST_REGISTERED) {
71 if (!is_registered_) {
72 is_registered_ = true;
73 std::lock_guard<std::mutex> its_lock(mutex_);
74 blocked_ = true;
75 condition_.notify_one();
76 }
77 } else {
78 is_registered_ = false;
79 }
80 }
81
on_message(const std::shared_ptr<vsomeip::message> & _request)82 void on_message(const std::shared_ptr<vsomeip::message> &_request) {
83 VSOMEIP_INFO << "Received a message with Client/Session [" << std::setw(4)
84 << std::setfill('0') << std::hex << _request->get_client() << "/"
85 << std::setw(4) << std::setfill('0') << std::hex
86 << _request->get_session() << "]";
87
88 std::shared_ptr<vsomeip::message> its_response = vsomeip::runtime::get()
89 ->create_response(_request);
90
91 std::shared_ptr<vsomeip::payload> its_payload = vsomeip::runtime::get()
92 ->create_payload();
93 std::vector<vsomeip::byte_t> its_payload_data;
94 for (std::size_t i = 0; i < 120; ++i)
95 its_payload_data.push_back(static_cast<vsomeip::byte_t>(i % 256));
96 its_payload->set_data(its_payload_data);
97 its_response->set_payload(its_payload);
98
99 app_->send(its_response);
100 if(_request->get_session() == 0x0F) {
101 std::lock_guard<std::mutex> its_lock(mutex_);
102 blocked_ = true;
103 condition_.notify_one();
104 }
105 }
106
run()107 void run() {
108 std::unique_lock<std::mutex> its_lock(mutex_);
109 while (!blocked_)
110 condition_.wait(its_lock);
111
112 bool is_offer(true);
113 blocked_ = false;
114
115 if (use_static_routing_) {
116 offer();
117 while (!blocked_) {
118 if(std::cv_status::timeout ==
119 condition_.wait_for(its_lock, std::chrono::seconds(200))) {
120 GTEST_NONFATAL_FAILURE_("Didn't receive all requests within time");
121 break;
122 }
123 }
124 std::this_thread::sleep_for(std::chrono::milliseconds(5));
125 app_->clear_all_handler();
126 app_->stop();
127 } else {
128 while (true) {
129 if (is_offer)
130 offer();
131 else
132 stop_offer();
133 std::this_thread::sleep_for(std::chrono::milliseconds(10000));
134 is_offer = !is_offer;
135 }
136 }
137 }
138
139 private:
140 std::shared_ptr<vsomeip::application> app_;
141 bool is_registered_;
142 bool use_static_routing_;
143
144 std::mutex mutex_;
145 std::condition_variable condition_;
146 bool blocked_;
147 std::thread offer_thread_;
148 };
149
150 static bool use_static_routing = false;
151
TEST(someip_magic_cookies_test,reply_to_good_messages)152 TEST(someip_magic_cookies_test, reply_to_good_messages)
153 {
154 magic_cookies_test_service its_sample(use_static_routing);
155 its_sample.init();
156 its_sample.start();
157 }
158
main(int argc,char ** argv)159 int main(int argc, char** argv) {
160 ::testing::InitGoogleTest(&argc, argv);
161 std::string static_routing_enable("--static-routing");
162 for (int i = 1; i < argc; i++) {
163 if (static_routing_enable == argv[i]) {
164 use_static_routing = true;
165 }
166 }
167 return RUN_ALL_TESTS();
168 }
169