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 #include <map>
13 #include <algorithm>
14 #include <atomic>
15 #include <future>
16
17 #include <gtest/gtest.h>
18
19 #include <vsomeip/vsomeip.hpp>
20 #include <vsomeip/internal/logger.hpp>
21
22 #include "malicious_data_test_globals.hpp"
23
24 class malicious_data_test_service {
25 public:
malicious_data_test_service(struct malicious_data_test::service_info _service_info,malicious_data_test::test_mode_e _testmode)26 malicious_data_test_service(struct malicious_data_test::service_info _service_info, malicious_data_test::test_mode_e _testmode) :
27 service_info_(_service_info),
28 testmode_(_testmode),
29 app_(vsomeip::runtime::get()->create_application("malicious_data_test_service")),
30 wait_until_registered_(true),
31 wait_until_shutdown_method_called_(true),
32 received_events_(0),
33 received_methodcalls_(0),
34 offer_thread_(std::bind(&malicious_data_test_service::run, this)) {
35 if (!app_->init()) {
36 ADD_FAILURE() << "Couldn't initialize application";
37 return;
38 }
39 app_->register_state_handler(
40 std::bind(&malicious_data_test_service::on_state, this,
41 std::placeholders::_1));
42
43 std::set<vsomeip::eventgroup_t> its_eventgroups;
44 its_eventgroups.insert(_service_info.eventgroup_id);
45 app_->request_event(service_info_.service_id, service_info_.instance_id,
46 service_info_.event_id, its_eventgroups, vsomeip::event_type_e::ET_EVENT,
47 vsomeip::reliability_type_e::RT_UNKNOWN);
48 app_->register_message_handler(vsomeip::ANY_SERVICE,
49 vsomeip::ANY_INSTANCE, service_info_.shutdown_method_id,
50 std::bind(&malicious_data_test_service::on_shutdown_method_called, this,
51 std::placeholders::_1));
52 app_->register_message_handler(service_info_.service_id,
53 service_info_.instance_id, service_info_.event_id,
54 std::bind(&malicious_data_test_service::on_event, this,
55 std::placeholders::_1));
56
57 app_->register_message_handler(static_cast<vsomeip::service_t>(service_info_.service_id + 1u),
58 service_info_.instance_id, 0x1,
59 std::bind(&malicious_data_test_service::on_message, this,
60 std::placeholders::_1));
61
62 // request service of client
63 app_->request_service(service_info_.service_id, service_info_.instance_id);
64 app_->subscribe(service_info_.service_id, service_info_.instance_id,
65 service_info_.eventgroup_id, 0,
66 service_info_.event_id);
67
68 app_->start();
69 }
70
~malicious_data_test_service()71 ~malicious_data_test_service() {
72 if (testmode_ == malicious_data_test::test_mode_e::MALICIOUS_EVENTS) {
73 EXPECT_EQ(9u, received_events_);
74 EXPECT_EQ(9u, received_methodcalls_);
75 }
76 offer_thread_.join();
77 }
78
offer()79 void offer() {
80 app_->offer_service(static_cast<vsomeip::service_t>(service_info_.service_id + 1u), 0x1);
81 }
82
stop()83 void stop() {
84 app_->stop_offer_service(static_cast<vsomeip::service_t>(service_info_.service_id + 1u), 0x1);
85 app_->clear_all_handler();
86 app_->stop();
87 }
88
on_state(vsomeip::state_type_e _state)89 void on_state(vsomeip::state_type_e _state) {
90 VSOMEIP_INFO << "Application " << app_->get_name() << " is "
91 << (_state == vsomeip::state_type_e::ST_REGISTERED ?
92 "registered." : "deregistered.");
93
94 if (_state == vsomeip::state_type_e::ST_REGISTERED) {
95 std::lock_guard<std::mutex> its_lock(mutex_);
96 wait_until_registered_ = false;
97 condition_.notify_one();
98 }
99 }
100
on_shutdown_method_called(const std::shared_ptr<vsomeip::message> & _message)101 void on_shutdown_method_called(const std::shared_ptr<vsomeip::message> &_message) {
102 app_->send(vsomeip::runtime::get()->create_response(_message));
103 VSOMEIP_WARNING << "************************************************************";
104 VSOMEIP_WARNING << "Shutdown method called -> going down!";
105 VSOMEIP_WARNING << "************************************************************";
106 std::lock_guard<std::mutex> its_lock(mutex_);
107 wait_until_shutdown_method_called_ = false;
108 condition_.notify_one();
109 }
110
on_event(const std::shared_ptr<vsomeip::message> & _message)111 void on_event(const std::shared_ptr<vsomeip::message> &_message) {
112 EXPECT_EQ(service_info_.service_id, _message->get_service());
113 EXPECT_EQ(service_info_.instance_id, _message->get_instance());
114 EXPECT_EQ(service_info_.event_id, _message->get_method());
115 EXPECT_EQ(std::uint32_t(0x7F), _message->get_length());
116 received_events_++;
117 }
118
on_message(const std::shared_ptr<vsomeip::message> & _message)119 void on_message(const std::shared_ptr<vsomeip::message> &_message) {
120 EXPECT_EQ(static_cast<vsomeip::service_t>(service_info_.service_id + 1u), _message->get_service());
121 EXPECT_EQ(service_info_.instance_id, _message->get_instance());
122 EXPECT_EQ(vsomeip::method_t(0x1), _message->get_method());
123 EXPECT_EQ(std::uint32_t(0x7F), _message->get_length());
124 received_methodcalls_++;
125 }
126
run()127 void run() {
128 VSOMEIP_DEBUG << "[" << std::setw(4) << std::setfill('0') << std::hex
129 << service_info_.service_id << "] Running";
130 std::unique_lock<std::mutex> its_lock(mutex_);
131 while (wait_until_registered_) {
132 condition_.wait(its_lock);
133 }
134
135 VSOMEIP_DEBUG << "[" << std::setw(4) << std::setfill('0') << std::hex
136 << service_info_.service_id << "] Offering";
137 offer();
138
139 while (wait_until_shutdown_method_called_) {
140 condition_.wait(its_lock);
141 }
142 stop();
143 }
144
145 private:
146 struct malicious_data_test::service_info service_info_;
147 malicious_data_test::test_mode_e testmode_;
148 std::shared_ptr<vsomeip::application> app_;
149
150 bool wait_until_registered_;
151 bool wait_until_shutdown_method_called_;
152 std::uint32_t received_events_;
153 std::uint32_t received_methodcalls_;
154 std::mutex mutex_;
155 std::condition_variable condition_;
156 std::thread offer_thread_;
157 };
158
159 malicious_data_test::test_mode_e its_testmode(malicious_data_test::test_mode_e::MALICIOUS_EVENTS);
160
TEST(someip_malicious_data_test,block_subscription_handler)161 TEST(someip_malicious_data_test, block_subscription_handler)
162 {
163 malicious_data_test_service its_sample(malicious_data_test::service, its_testmode);
164 }
165
166
167 #ifndef _WIN32
main(int argc,char ** argv)168 int main(int argc, char** argv)
169 {
170 ::testing::InitGoogleTest(&argc, argv);
171 std::string its_passed_testmode = argv[1];
172 if (its_passed_testmode == std::string("MALICIOUS_EVENTS")) {
173 its_testmode = malicious_data_test::test_mode_e::MALICIOUS_EVENTS;
174 } else if (its_passed_testmode == std::string("PROTOCOL_VERSION")) {
175 its_testmode = malicious_data_test::test_mode_e::PROTOCOL_VERSION;
176 } else if (its_passed_testmode == std::string("MESSAGE_TYPE")) {
177 its_testmode = malicious_data_test::test_mode_e::MESSAGE_TYPE;
178 } else if (its_passed_testmode == std::string("RETURN_CODE")) {
179 its_testmode = malicious_data_test::test_mode_e::RETURN_CODE;
180 } else if (its_passed_testmode == std::string("WRONG_HEADER_FIELDS_UDP")) {
181 its_testmode = malicious_data_test::test_mode_e::WRONG_HEADER_FIELDS_UDP;
182 }
183 return RUN_ALL_TESTS();
184 }
185 #endif
186