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_service.hpp"
7 
8 static bool is_remote_test = false;
9 static bool remote_client_allowed = true;
10 
security_test_service()11 security_test_service::security_test_service() :
12     app_(vsomeip::runtime::get()->create_application()),
13     is_registered_(false),
14     blocked_(false),
15     number_of_received_messages_(0),
16     offer_thread_(std::bind(&security_test_service::run, this)) {
17 }
18 
init()19 bool security_test_service::init() {
20     std::lock_guard<std::mutex> its_lock(mutex_);
21 
22     if (!app_->init()) {
23         ADD_FAILURE() << "Couldn't initialize application";
24         return false;
25     }
26     app_->register_message_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
27             vsomeip_test::TEST_SERVICE_INSTANCE_ID, vsomeip_test::TEST_SERVICE_METHOD_ID,
28             std::bind(&security_test_service::on_message, this,
29                     std::placeholders::_1));
30 
31     app_->register_message_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
32             vsomeip_test::TEST_SERVICE_INSTANCE_ID,
33             vsomeip_test::TEST_SERVICE_METHOD_ID_SHUTDOWN,
34             std::bind(&security_test_service::on_message_shutdown, this,
35                     std::placeholders::_1));
36 
37     app_->register_state_handler(
38             std::bind(&security_test_service::on_state, this,
39                     std::placeholders::_1));
40 
41     // offer allowed field 0x8001 eventgroup 0x01
42     std::set<vsomeip::eventgroup_t> its_eventgroups;
43     its_eventgroups.insert(0x01);
44 
45     app_->offer_event(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID,
46                 static_cast<vsomeip::event_t>(0x8001), its_eventgroups,
47                 vsomeip::event_type_e::ET_FIELD, std::chrono::milliseconds::zero(),
48                 false, true, nullptr, vsomeip::reliability_type_e::RT_UNRELIABLE);
49 
50     // also offer field 0x8002 which is not allowed to be received by client
51     app_->offer_event(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID,
52                 static_cast<vsomeip::event_t>(0x8002), its_eventgroups,
53                 vsomeip::event_type_e::ET_FIELD, std::chrono::milliseconds::zero(),
54                 false, true, nullptr, vsomeip::reliability_type_e::RT_UNRELIABLE);
55 
56     // set value to fields
57     std::shared_ptr<vsomeip::payload> its_payload =
58             vsomeip::runtime::get()->create_payload();
59     vsomeip::byte_t its_data[2] = {static_cast<vsomeip::byte_t>((vsomeip_test::TEST_SERVICE_SERVICE_ID & 0xFF00) >> 8),
60             static_cast<vsomeip::byte_t>((vsomeip_test::TEST_SERVICE_SERVICE_ID & 0xFF))};
61     its_payload->set_data(its_data, 2);
62 
63     app_->notify(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID,
64             static_cast<vsomeip::event_t>(0x8001), its_payload);
65 
66     app_->notify(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID,
67             static_cast<vsomeip::event_t>(0x8002), its_payload);
68 
69     return true;
70 }
71 
start()72 void security_test_service::start() {
73     VSOMEIP_INFO << "Starting...";
74     app_->start();
75 }
76 
stop()77 void security_test_service::stop() {
78     VSOMEIP_INFO << "Stopping...";
79     app_->clear_all_handler();
80     app_->stop();
81 }
82 
join_offer_thread()83 void security_test_service::join_offer_thread() {
84     if (offer_thread_.joinable()) {
85         offer_thread_.join();
86     }
87 }
88 
offer()89 void security_test_service::offer() {
90     app_->offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
91 
92     // try to offer a not allowed instance ID 0x02 (client requesting the service should not get available)
93     app_->offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, 0x02);
94 
95     // try to offer a not allowed service ID 0x111 (client requesting the service should not get available)
96     app_->offer_service(0x111, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
97 }
98 
stop_offer()99 void security_test_service::stop_offer() {
100     app_->stop_offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
101 }
102 
on_state(vsomeip::state_type_e _state)103 void security_test_service::on_state(vsomeip::state_type_e _state) {
104     VSOMEIP_INFO << "Application " << app_->get_name() << " is "
105             << (_state == vsomeip::state_type_e::ST_REGISTERED ? "registered." :
106                     "deregistered.");
107 
108     if(_state == vsomeip::state_type_e::ST_REGISTERED) {
109         if(!is_registered_) {
110             is_registered_ = true;
111             std::lock_guard<std::mutex> its_lock(mutex_);
112             blocked_ = true;
113             // "start" the run method thread
114             condition_.notify_one();
115         }
116     }
117     else {
118         is_registered_ = false;
119     }
120 }
121 
on_message(const std::shared_ptr<vsomeip::message> & _request)122 void security_test_service::on_message(const std::shared_ptr<vsomeip::message>& _request) {
123     ASSERT_EQ(vsomeip_test::TEST_SERVICE_SERVICE_ID, _request->get_service());
124     ASSERT_EQ(vsomeip_test::TEST_SERVICE_INSTANCE_ID, _request->get_instance());
125 
126     VSOMEIP_INFO << "Received a message with Client/Session [" << std::setw(4)
127         << std::setfill('0') << std::hex << _request->get_client() << "/"
128         << std::setw(4) << std::setfill('0') << std::hex
129         << _request->get_session() << "] method: " << _request->get_method() ;
130 
131     // send response
132     std::shared_ptr<vsomeip::message> its_response =
133             vsomeip::runtime::get()->create_response(_request);
134 
135     app_->send(its_response);
136 
137     number_of_received_messages_++;
138     if(number_of_received_messages_ == vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_SECURITY_TESTS) {
139         VSOMEIP_INFO << "Received all messages!";
140     }
141 }
142 
on_message_shutdown(const std::shared_ptr<vsomeip::message> & _request)143 void security_test_service::on_message_shutdown(
144         const std::shared_ptr<vsomeip::message>& _request) {
145     (void)_request;
146     VSOMEIP_INFO << "Shutdown method was called, going down now.";
147     stop();
148 }
149 
run()150 void security_test_service::run() {
151     std::unique_lock<std::mutex> its_lock(mutex_);
152     while (!blocked_)
153         condition_.wait(its_lock);
154 
155    offer();
156 
157    // do not wait for the shutdown method to be called
158    if (is_remote_test && !remote_client_allowed) {
159        std::this_thread::sleep_for(std::chrono::milliseconds(250 * vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_SECURITY_TESTS + 10000));
160        VSOMEIP_INFO << "Shutdown the service after timeout as remote client is not allowed by policy to call shutdown method!";
161        stop();
162    }
163 
164 }
165 
TEST(someip_security_test,basic_subscribe_request_response)166 TEST(someip_security_test, basic_subscribe_request_response) {
167     security_test_service test_service;
168     if (test_service.init()) {
169         test_service.start();
170         test_service.join_offer_thread();
171     }
172 }
173 
174 #ifndef _WIN32
main(int argc,char ** argv)175 int main(int argc, char** argv) {
176 
177     std::string test_remote("--remote");
178     std::string test_local("--local");
179     std::string test_allow_remote_client("--allow");
180     std::string test_deny_remote_client("--deny");
181     std::string help("--help");
182 
183     int i = 1;
184     while (i < argc)
185     {
186         if(test_remote == argv[i])
187         {
188             is_remote_test = true;
189         }
190         else if(test_local == argv[i])
191         {
192             is_remote_test = false;
193         }
194         else if(test_allow_remote_client == argv[i])
195         {
196             remote_client_allowed = true;
197         }
198         else if(test_deny_remote_client == argv[i])
199         {
200             remote_client_allowed = false;
201         }
202         else if(help == argv[i])
203         {
204             VSOMEIP_INFO << "Parameters:\n"
205             << "--remote: Run test between two hosts\n"
206             << "--local: Run test locally\n"
207             << "--allow: test is started with a policy that allows remote messages sent by this test client to the service\n"
208             << "--deny: test is started with a policy that denies remote messages sent by this test client to the service\n"
209             << "--help: print this help";
210         }
211         i++;
212     }
213 
214     ::testing::InitGoogleTest(&argc, argv);
215     return RUN_ALL_TESTS();
216 }
217 #endif
218