1 // Copyright (C) 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 "security_test_client.hpp"
7 
8 static bool is_remote_test = false;
9 static bool remote_client_allowed = true;
10 
security_test_client(bool _test_external_communication,bool _is_remote_client_allowed)11 security_test_client::security_test_client(bool _test_external_communication,
12                                            bool _is_remote_client_allowed)
13     : app_(vsomeip::runtime::get()->create_application()),
14       is_available_(false),
15       sender_(std::bind(&security_test_client::run, this)),
16       received_responses_(0),
17       received_allowed_events_(0),
18       test_external_communication_(_test_external_communication),
19       is_remote_client_allowed_(_is_remote_client_allowed) {
20 
21 }
22 
init()23 bool security_test_client::init() {
24     if (!app_->init()) {
25         ADD_FAILURE() << "Couldn't initialize application";
26         return false;
27     }
28 
29     app_->register_state_handler(
30             std::bind(&security_test_client::on_state, this,
31                     std::placeholders::_1));
32 
33     app_->register_message_handler(vsomeip::ANY_SERVICE,
34             vsomeip_test::TEST_SERVICE_INSTANCE_ID, vsomeip::ANY_METHOD,
35             std::bind(&security_test_client::on_message, this,
36                     std::placeholders::_1));
37 
38     app_->register_availability_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
39             vsomeip_test::TEST_SERVICE_INSTANCE_ID,
40             std::bind(&security_test_client::on_availability, this,
41                     std::placeholders::_1, std::placeholders::_2,
42                     std::placeholders::_3));
43 
44     app_->register_availability_handler(0x111,
45             vsomeip_test::TEST_SERVICE_INSTANCE_ID,
46             std::bind(&security_test_client::on_availability, this,
47                     std::placeholders::_1, std::placeholders::_2,
48                     std::placeholders::_3));
49 
50     app_->register_availability_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
51             0x02,
52             std::bind(&security_test_client::on_availability, this,
53                     std::placeholders::_1, std::placeholders::_2,
54                     std::placeholders::_3));
55     return true;
56 }
57 
start()58 void security_test_client::start() {
59     VSOMEIP_INFO << "Starting...";
60 
61     app_->start();
62 }
63 
stop()64 void security_test_client::stop() {
65     VSOMEIP_INFO << "Stopping...";
66 
67     if (is_remote_client_allowed_) {
68         shutdown_service();
69     }
70 
71     std::this_thread::sleep_for(std::chrono::milliseconds(100));
72 
73     app_->clear_all_handler();
74     app_->stop();
75 }
76 
on_state(vsomeip::state_type_e _state)77 void security_test_client::on_state(vsomeip::state_type_e _state) {
78     if(_state == vsomeip::state_type_e::ST_REGISTERED) {
79         app_->request_service(vsomeip_test::TEST_SERVICE_SERVICE_ID,
80                 vsomeip_test::TEST_SERVICE_INSTANCE_ID, false);
81 
82         // request not allowed service ID
83         app_->request_service(0x111,
84                 vsomeip_test::TEST_SERVICE_INSTANCE_ID, false);
85 
86         // request not allowed instance ID
87         app_->request_service(vsomeip_test::TEST_SERVICE_SERVICE_ID,
88                 0x02, false);
89 
90         // request events of eventgroup 0x01 which holds events 0x8001 (allowed) and 0x8002 (denied)
91         std::set<vsomeip::eventgroup_t> its_eventgroups;
92         its_eventgroups.insert(0x01);
93         app_->request_event(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID,
94                 static_cast<vsomeip::event_t>(0x8001),
95                 its_eventgroups, vsomeip::event_type_e::ET_FIELD,
96                 vsomeip::reliability_type_e::RT_UNRELIABLE);
97         app_->request_event(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID,
98                 static_cast<vsomeip::event_t>(0x8002),
99                 its_eventgroups, vsomeip::event_type_e::ET_FIELD,
100                 vsomeip::reliability_type_e::RT_UNRELIABLE);
101 
102         app_->subscribe(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID, 0x01,
103                 vsomeip::DEFAULT_MAJOR, static_cast<vsomeip::event_t>(0x8001));
104 
105         app_->subscribe(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID, 0x01,
106                 vsomeip::DEFAULT_MAJOR, static_cast<vsomeip::event_t>(0x8002));
107     }
108 }
109 
on_availability(vsomeip::service_t _service,vsomeip::instance_t _instance,bool _is_available)110 void security_test_client::on_availability(vsomeip::service_t _service,
111         vsomeip::instance_t _instance, bool _is_available) {
112 
113     VSOMEIP_INFO << std::hex << "Client 0x" << app_->get_client()
114             << " : Service [" << std::setw(4) << std::setfill('0') << std::hex
115             << _service << "." << _instance << "] is "
116             << (_is_available ? "available." : "NOT available.");
117 
118     // check that only the allowed service / instance ID gets available
119     if (_is_available) {
120         EXPECT_EQ(vsomeip_test::TEST_SERVICE_SERVICE_ID, _service);
121         EXPECT_EQ(vsomeip_test::TEST_SERVICE_INSTANCE_ID, _instance);
122     }
123 
124     if(vsomeip_test::TEST_SERVICE_SERVICE_ID == _service
125             && vsomeip_test::TEST_SERVICE_INSTANCE_ID == _instance) {
126         std::unique_lock<std::mutex> its_lock(mutex_);
127         if(is_available_ && !_is_available) {
128             is_available_ = false;
129         }
130         else if(_is_available && !is_available_) {
131             is_available_ = true;
132             condition_.notify_one();
133         }
134     }
135 }
136 
on_message(const std::shared_ptr<vsomeip::message> & _response)137 void security_test_client::on_message(const std::shared_ptr<vsomeip::message> &_response) {
138     VSOMEIP_INFO << "Received a response from Service ["
139                  << std::setw(4) << std::setfill('0') << std::hex << _response->get_service()
140                  << "."
141                  << std::setw(4) << std::setfill('0') << std::hex << _response->get_instance()
142                  << "] to Client/Session ["
143                  << std::setw(4) << std::setfill('0') << std::hex << _response->get_client()
144                  << "/"
145                  << std::setw(4) << std::setfill('0') << std::hex << _response->get_session()
146                  << "]";
147 
148     if(_response->get_message_type() == vsomeip::message_type_e::MT_RESPONSE) {
149         EXPECT_EQ(vsomeip_test::TEST_SERVICE_SERVICE_ID,  _response->get_service());
150         EXPECT_EQ(vsomeip_test::TEST_SERVICE_INSTANCE_ID, _response->get_instance());
151         EXPECT_EQ(vsomeip_test::TEST_SERVICE_METHOD_ID, _response->get_method());
152 
153         if (_response->get_service() == vsomeip_test::TEST_SERVICE_SERVICE_ID &&
154                 _response->get_instance() == vsomeip_test::TEST_SERVICE_INSTANCE_ID &&
155                 _response->get_method() == vsomeip_test::TEST_SERVICE_METHOD_ID) {
156             received_responses_++;
157             if (received_responses_ == vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_SECURITY_TESTS) {
158                 VSOMEIP_WARNING << std::hex << app_->get_client()
159                         << ": Received all messages ~> going down!";
160             }
161         }
162     } else if (_response->get_message_type() == vsomeip::message_type_e::MT_NOTIFICATION) {
163         // check that only allowed event 0x8001 is received
164         EXPECT_EQ(vsomeip_test::TEST_SERVICE_SERVICE_ID,  _response->get_service());
165         EXPECT_EQ(vsomeip_test::TEST_SERVICE_INSTANCE_ID, _response->get_instance());
166         EXPECT_EQ(0x8001, _response->get_method());
167         received_allowed_events_++;
168     }
169 }
170 
run()171 void security_test_client::run() {
172     for (uint32_t i = 0; i < vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_SECURITY_TESTS; ++i) {
173         {
174             std::unique_lock<std::mutex> its_lock(mutex_);
175             while (!is_available_)
176             {
177                 condition_.wait(its_lock);
178             }
179         }
180 
181         auto request = vsomeip::runtime::get()->create_request(false);
182         request->set_service(vsomeip_test::TEST_SERVICE_SERVICE_ID);
183         request->set_instance(vsomeip_test::TEST_SERVICE_INSTANCE_ID);
184         request->set_method(vsomeip_test::TEST_SERVICE_METHOD_ID);
185 
186         // send a request which is allowed by policy -> expect answer
187         app_->send(request);
188 
189         // send a request with a not allowed method ID -> expect no answer
190         request->set_method(0x888);
191         app_->send(request);
192 
193         std::this_thread::sleep_for(std::chrono::milliseconds(250));
194     }
195 
196     std::this_thread::sleep_for(std::chrono::milliseconds(250));
197 
198     if (!test_external_communication_) {
199         EXPECT_EQ(vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_SECURITY_TESTS,
200                 received_responses_);
201         EXPECT_EQ(received_allowed_events_, (uint32_t) 0x01);
202     } else if (test_external_communication_ && !is_remote_client_allowed_) {
203         EXPECT_EQ((uint32_t)0, received_responses_);
204         EXPECT_EQ((uint32_t)0, received_allowed_events_);
205     } else if (test_external_communication_ && is_remote_client_allowed_) {
206         EXPECT_EQ(vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_SECURITY_TESTS,
207                 received_responses_);
208         EXPECT_EQ(received_allowed_events_, (uint32_t) 0x01);
209     }
210     stop();
211 }
212 
join_sender_thread()213 void security_test_client::join_sender_thread()
214 {
215     if (sender_.joinable()) {
216         sender_.join();
217     }
218 }
219 
shutdown_service()220 void security_test_client::shutdown_service() {
221     auto request = vsomeip::runtime::get()->create_request(false);
222     request->set_service(vsomeip_test::TEST_SERVICE_SERVICE_ID);
223     request->set_instance(vsomeip_test::TEST_SERVICE_INSTANCE_ID);
224     request->set_method(vsomeip_test::TEST_SERVICE_METHOD_ID_SHUTDOWN);
225     app_->send(request);
226 }
227 
TEST(someip_security_test,basic_subscribe_request_response)228 TEST(someip_security_test, basic_subscribe_request_response)
229 {
230     security_test_client test_client(is_remote_test, remote_client_allowed);
231     if (test_client.init()) {
232         test_client.start();
233         test_client.join_sender_thread();
234     }
235 }
236 
main(int argc,char ** argv)237 int main(int argc, char** argv) {
238 
239     std::string test_remote("--remote");
240     std::string test_local("--local");
241     std::string test_allow_remote_client("--allow");
242     std::string test_deny_remote_client("--deny");
243     std::string help("--help");
244 
245     int i = 1;
246     while (i < argc)
247     {
248         if(test_remote == argv[i])
249         {
250             is_remote_test = true;
251         }
252         else if(test_local == argv[i])
253         {
254             is_remote_test = false;
255         }
256         else if(test_allow_remote_client == argv[i])
257         {
258             remote_client_allowed = true;
259         }
260         else if(test_deny_remote_client == argv[i])
261         {
262             remote_client_allowed = false;
263         }
264         else if(help == argv[i])
265         {
266             VSOMEIP_INFO << "Parameters:\n"
267             << "--remote: Run test between two hosts\n"
268             << "--local: Run test locally\n"
269             << "--allow: test is started with a policy that allows remote messages sent by this test client to the service\n"
270             << "--deny: test is started with a policy that denies remote messages sent by this test client to the service\n"
271             << "--help: print this help";
272         }
273         i++;
274     }
275 
276     ::testing::InitGoogleTest(&argc, argv);
277     return RUN_ALL_TESTS();
278 }
279