1 /* Copyright (c) 2013, Ford Motor Company
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided with the
15 * Neither the name of the Ford Motor Company nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <gtest/gtest.h>
33 #include <gmock/gmock.h>
35 #include "mock_policy_listener.h"
36 #include "mock_pt_representation.h"
37 #include "mock_pt_ext_representation.h"
38 #include "policy/policy_manager_impl.h"
39 #include "json/value.h"
42 using ::testing::Return;
43 using ::testing::DoAll;
44 using ::testing::SetArgPointee;
46 using ::policy::PTRepresentation;
47 using ::policy::MockPolicyListener;
48 using ::policy::MockPTRepresentation;
49 using ::policy::MockPTExtRepresentation;
50 using ::policy::PolicyManagerImpl;
51 using ::policy::PolicyTable;
52 using ::policy::EndpointUrls;
55 namespace components {
58 class PolicyManagerImplTest : public ::testing::Test {
60 static void SetUpTestCase() {
63 static void TearDownTestCase() {
67 TEST_F(PolicyManagerImplTest, ExceededIgnitionCycles) {
68 ::testing::NiceMock<MockPTRepresentation> mock_pt;
70 EXPECT_CALL(mock_pt, IgnitionCyclesBeforeExchange()).Times(2).WillOnce(
71 Return(5)).WillOnce(Return(0));
73 mock_pt, IncrementIgnitionCycles()).Times(1);
75 PolicyManagerImpl* manager = new PolicyManagerImpl();
76 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
77 EXPECT_FALSE(manager->ExceededIgnitionCycles());
78 manager->IncrementIgnitionCycles();
79 EXPECT_TRUE(manager->ExceededIgnitionCycles());
82 TEST_F(PolicyManagerImplTest, ExceededDays) {
83 ::testing::NiceMock<MockPTRepresentation> mock_pt;
85 EXPECT_CALL(mock_pt, DaysBeforeExchange(_)).Times(2).WillOnce(Return(5))
88 PolicyManagerImpl* manager = new PolicyManagerImpl();
89 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
90 EXPECT_FALSE(manager->ExceededDays(5));
91 EXPECT_TRUE(manager->ExceededDays(15));
94 TEST_F(PolicyManagerImplTest, ExceededKilometers) {
95 ::testing::NiceMock<MockPTRepresentation> mock_pt;
97 EXPECT_CALL(mock_pt, KilometersBeforeExchange(_)).Times(2).WillOnce(
98 Return(50)).WillOnce(Return(0));
100 PolicyManagerImpl* manager = new PolicyManagerImpl();
101 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
102 EXPECT_FALSE(manager->ExceededKilometers(50));
103 EXPECT_TRUE(manager->ExceededKilometers(150));
106 TEST_F(PolicyManagerImplTest, NextRetryTimeout) {
107 ::testing::NiceMock<MockPTRepresentation> mock_pt;
108 std::vector<int> seconds;
109 seconds.push_back(50);
110 seconds.push_back(100);
111 seconds.push_back(200);
113 EXPECT_CALL(mock_pt, TimeoutResponse()).Times(1).WillOnce(Return(60));
115 SecondsBetweenRetries(_)).Times(1).WillOnce(
116 DoAll(SetArgPointee<0>(seconds), Return(true)));
118 PolicyManagerImpl* manager = new PolicyManagerImpl();
119 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
120 EXPECT_EQ(50, manager->NextRetryTimeout());
121 EXPECT_EQ(100, manager->NextRetryTimeout());
122 EXPECT_EQ(200, manager->NextRetryTimeout());
123 EXPECT_EQ(0, manager->NextRetryTimeout());
126 TEST_F(PolicyManagerImplTest, GetUpdateUrl) {
127 ::testing::NiceMock<MockPTRepresentation> mock_pt;
128 EndpointUrls urls_1234, urls_4321;
129 urls_1234.push_back(::policy::EndpointData("http://ford.com/cloud/1"));
130 urls_1234.push_back(::policy::EndpointData("http://ford.com/cloud/2"));
131 urls_1234.push_back(::policy::EndpointData("http://ford.com/cloud/3"));
132 urls_4321.push_back(::policy::EndpointData("http://panasonic.com/cloud/1"));
133 urls_4321.push_back(::policy::EndpointData("http://panasonic.com/cloud/2"));
134 urls_4321.push_back(::policy::EndpointData("http://panasonic.com/cloud/3"));
136 EXPECT_CALL(mock_pt, GetUpdateUrls(7)).Times(4).WillRepeatedly(
139 GetUpdateUrls(4)).Times(2).WillRepeatedly(Return(urls_4321));
141 PolicyManagerImpl* manager = new PolicyManagerImpl();
142 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
143 EXPECT_EQ("http://ford.com/cloud/1", manager->GetUpdateUrl(7));
144 EXPECT_EQ("http://ford.com/cloud/2", manager->GetUpdateUrl(7));
145 EXPECT_EQ("http://ford.com/cloud/3", manager->GetUpdateUrl(7));
146 EXPECT_EQ("http://panasonic.com/cloud/1", manager->GetUpdateUrl(4));
147 EXPECT_EQ("http://ford.com/cloud/2", manager->GetUpdateUrl(7));
148 EXPECT_EQ("http://panasonic.com/cloud/3", manager->GetUpdateUrl(4));
151 TEST_F(PolicyManagerImplTest, RefreshRetrySequence) {
152 ::testing::NiceMock<MockPTRepresentation> mock_pt;
153 std::vector<int> seconds, seconds_empty;
154 seconds.push_back(50);
155 seconds.push_back(100);
156 seconds.push_back(200);
158 EXPECT_CALL(mock_pt, TimeoutResponse()).Times(2).WillOnce(Return(0)).WillOnce(
160 EXPECT_CALL(mock_pt, SecondsBetweenRetries(_)).Times(2).WillOnce(
161 DoAll(SetArgPointee<0>(seconds_empty), Return(true))).WillOnce(
162 DoAll(SetArgPointee<0>(seconds), Return(true)));
164 PolicyManagerImpl* manager = new PolicyManagerImpl();
165 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
166 manager->RefreshRetrySequence();
167 EXPECT_EQ(60, manager->TimeoutExchange());
168 EXPECT_EQ(50, manager->NextRetryTimeout());
169 EXPECT_EQ(100, manager->NextRetryTimeout());
170 EXPECT_EQ(200, manager->NextRetryTimeout());
173 #ifdef EXTENDED_POLICY
174 TEST_F(PolicyManagerImplTest, IncrementGlobalCounter) {
175 ::testing::NiceMock<MockPTExtRepresentation> mock_pt;
177 EXPECT_CALL(mock_pt, Increment("count_of_sync_reboots")).Times(1);
179 PolicyManagerImpl* manager = new PolicyManagerImpl();
180 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
181 manager->Increment(usage_statistics::SYNC_REBOOTS);
184 TEST_F(PolicyManagerImplTest, IncrementAppCounter) {
185 ::testing::NiceMock<MockPTExtRepresentation> mock_pt;
187 EXPECT_CALL(mock_pt, Increment("12345", "count_of_user_selections")).Times(1);
189 PolicyManagerImpl* manager = new PolicyManagerImpl();
190 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
191 manager->Increment("12345", usage_statistics::USER_SELECTIONS);
194 TEST_F(PolicyManagerImplTest, SetAppInfo) {
195 ::testing::NiceMock<MockPTExtRepresentation> mock_pt;
197 EXPECT_CALL(mock_pt, Set("12345", "app_registration_language_gui", "de-de")).
200 PolicyManagerImpl* manager = new PolicyManagerImpl();
201 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
202 manager->Set("12345", usage_statistics::LANGUAGE_GUI, "de-de");
205 TEST_F(PolicyManagerImplTest, AddAppStopwatch) {
206 ::testing::NiceMock<MockPTExtRepresentation> mock_pt;
208 EXPECT_CALL(mock_pt, Add("12345", "minutes_hmi_full", 30)).Times(1);
210 PolicyManagerImpl* manager = new PolicyManagerImpl();
211 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
212 manager->Add("12345", usage_statistics::SECONDS_HMI_FULL, 30);
214 #endif // EXTENDED_POLICY
216 TEST_F(PolicyManagerImplTest, ResetPT) {
217 ::testing::NiceMock<MockPTRepresentation> mock_pt;
219 EXPECT_CALL(mock_pt, Init()).WillOnce(Return(::policy::NONE))
220 //.WillOnce(Return(::policy::EXISTS));
221 //.WillOnce(Return(::policy::SUCCESS))
222 .WillOnce(Return(::policy::FAIL));
224 PolicyManagerImpl* manager = new PolicyManagerImpl();
225 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
226 EXPECT_FALSE(manager->ResetPT("filename"));
227 // TODO(AOleynik): Sometimes fails, check this
228 // EXPECT_TRUE(manager->ResetPT("filename"));
229 // EXPECT_TRUE(manager->ResetPT("filename"));
230 EXPECT_FALSE(manager->ResetPT("filename"));
233 TEST_F(PolicyManagerImplTest, CheckPermissions) {
234 ::testing::NiceMock<MockPTRepresentation> mock_pt;
235 MockPolicyListener mock_listener;
237 ::policy::CheckPermissionResult result;
238 result.hmi_level_permitted = ::policy::kRpcAllowed;
239 result.list_of_allowed_params = new std::vector< ::policy::PTString>();
240 result.list_of_allowed_params->push_back("speed");
241 result.list_of_allowed_params->push_back("gps");
243 #ifdef EXTENDED_POLICY
244 EXPECT_CALL(mock_pt, CheckPermissions("pre_DataConsent", "FULL", "Alert")).WillOnce(
246 #else // EXTENDED_POLICY
247 EXPECT_CALL(mock_pt, CheckPermissions("12345678", "FULL", "Alert")).WillOnce(
249 #endif // EXTENDED_POLICY
250 EXPECT_CALL(mock_listener, OnCurrentDeviceIdUpdateRequired("12345678")).Times(1);
252 PolicyManagerImpl* manager = new PolicyManagerImpl();
253 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
254 manager->set_listener(&mock_listener);
255 ::policy::CheckPermissionResult out_result = manager->CheckPermissions(
256 "12345678", "FULL", "Alert");
257 EXPECT_EQ(::policy::kRpcAllowed, out_result.hmi_level_permitted);
258 ASSERT_TRUE(out_result.list_of_allowed_params);
259 ASSERT_EQ(2, out_result.list_of_allowed_params->size());
260 EXPECT_EQ("speed", (*out_result.list_of_allowed_params)[0]);
261 EXPECT_EQ("gps", (*out_result.list_of_allowed_params)[1]);
264 TEST_F(PolicyManagerImplTest, DISABLED_LoadPT) {
265 // TODO(KKolodiy): PolicyManagerImpl is hard for testing
266 ::testing::NiceMock<MockPTRepresentation> mock_pt;
267 MockPolicyListener mock_listener;
269 Json::Value table(Json::objectValue);
270 table["policy_table"] = Json::Value(Json::objectValue);
272 Json::Value& policy_table = table["policy_table"];
273 policy_table["module_meta"] = Json::Value(Json::objectValue);
274 policy_table["module_config"] = Json::Value(Json::objectValue);
275 policy_table["usage_and_error_counts"] = Json::Value(Json::objectValue);
276 policy_table["device_data"] = Json::Value(Json::objectValue);
277 policy_table["functional_groupings"] = Json::Value(Json::objectValue);
278 policy_table["consumer_friendly_messages"] = Json::Value(Json::objectValue);
279 policy_table["app_policies"] = Json::Value(Json::objectValue);
280 policy_table["app_policies"]["1234"] = Json::Value(Json::objectValue);
282 Json::Value& module_meta = policy_table["module_meta"];
283 module_meta["ccpu_version"] = Json::Value("ccpu version");
284 module_meta["language"] = Json::Value("ru");
285 module_meta["wers_country_code"] = Json::Value("ru");
286 module_meta["pt_exchanged_at_odometer_x"] = Json::Value(0);
287 module_meta["pt_exchanged_x_days_after_epoch"] = Json::Value(0);
288 module_meta["ignition_cycles_since_last_exchange"] = Json::Value(0);
289 module_meta["vin"] = Json::Value("vin");
291 Json::Value& module_config = policy_table["module_config"];
292 module_config["preloaded_pt"] = Json::Value(true);
293 module_config["exchange_after_x_ignition_cycles"] = Json::Value(10);
294 module_config["exchange_after_x_kilometers"] = Json::Value(100);
295 module_config["exchange_after_x_days"] = Json::Value(5);
296 module_config["timeout_after_x_seconds"] = Json::Value(500);
297 module_config["seconds_between_retries"] = Json::Value(Json::arrayValue);
298 module_config["seconds_between_retries"][0] = Json::Value(10);
299 module_config["seconds_between_retries"][1] = Json::Value(20);
300 module_config["seconds_between_retries"][2] = Json::Value(30);
301 module_config["endpoints"] = Json::Value(Json::objectValue);
302 module_config["endpoints"]["0x00"] = Json::Value(Json::objectValue);
303 module_config["endpoints"]["0x00"]["default"] = Json::Value(Json::arrayValue);
304 module_config["endpoints"]["0x00"]["default"][0] = Json::Value(
305 "http://ford.com/cloud/default");
306 module_config["notifications_per_minute_by_priority"] = Json::Value(
308 module_config["notifications_per_minute_by_priority"]["emergency"] =
310 module_config["notifications_per_minute_by_priority"]["navigation"] =
312 module_config["notifications_per_minute_by_priority"]["voiceCommunication"] =
314 module_config["notifications_per_minute_by_priority"]["communication"] =
316 module_config["notifications_per_minute_by_priority"]["normal"] = Json::Value(
318 module_config["notifications_per_minute_by_priority"]["none"] = Json::Value(
320 module_config["vehicle_make"] = Json::Value("MakeT");
321 module_config["vehicle_model"] = Json::Value("ModelT");
322 module_config["vehicle_year"] = Json::Value(2014);
324 Json::Value& usage_and_error_counts = policy_table["usage_and_error_counts"];
325 usage_and_error_counts["count_of_iap_buffer_full"] = Json::Value(0);
326 usage_and_error_counts["count_sync_out_of_memory"] = Json::Value(0);
327 usage_and_error_counts["count_of_sync_reboots"] = Json::Value(0);
329 Json::Value& device_data = policy_table["device_data"];
330 device_data["DEVICEHASH"] = Json::Value(Json::objectValue);
331 device_data["DEVICEHASH"]["hardware"] = Json::Value("hardware");
332 device_data["DEVICEHASH"]["firmware_rev"] = Json::Value("firmware_rev");
333 device_data["DEVICEHASH"]["os"] = Json::Value("os");
334 device_data["DEVICEHASH"]["os_version"] = Json::Value("os_version");
335 device_data["DEVICEHASH"]["carrier"] = Json::Value("carrier");
336 device_data["DEVICEHASH"]["max_number_rfcom_ports"] = Json::Value(10);
338 Json::Value& functional_groupings = policy_table["functional_groupings"];
339 functional_groupings["default"] = Json::Value(Json::objectValue);
340 Json::Value& default_group = functional_groupings["default"];
341 default_group["rpcs"] = Json::Value(Json::objectValue);
342 default_group["rpcs"]["Update"] = Json::Value(Json::objectValue);
343 default_group["rpcs"]["Update"]["hmi_levels"] = Json::Value(Json::arrayValue);
344 default_group["rpcs"]["Update"]["hmi_levels"][0] = Json::Value("FULL");
345 default_group["rpcs"]["Update"]["parameters"] = Json::Value(Json::arrayValue);
346 default_group["rpcs"]["Update"]["parameters"][0] = Json::Value("speed");
348 Json::Value& consumer_friendly_messages =
349 policy_table["consumer_friendly_messages"];
350 consumer_friendly_messages["version"] = Json::Value("1.2");
352 Json::Value& app_policies = policy_table["app_policies"];
353 app_policies["default"] = Json::Value(Json::objectValue);
354 app_policies["default"]["memory_kb"] = Json::Value(50);
355 app_policies["default"]["watchdog_timer_ms"] = Json::Value(100);
356 app_policies["default"]["groups"] = Json::Value(Json::arrayValue);
357 app_policies["default"]["groups"][0] = Json::Value("default");
358 app_policies["default"]["priority"] = Json::Value("EMERGENCY");
359 app_policies["default"]["default_hmi"] = Json::Value("FULL");
360 app_policies["default"]["keep_context"] = Json::Value(true);
361 app_policies["default"]["steal_focus"] = Json::Value(true);
362 app_policies["default"]["certificate"] = Json::Value("sign");
364 std::string json = table.toStyledString();
365 ::policy::BinaryMessage msg(json.begin(), json.end());
367 EXPECT_CALL(mock_pt, Save(_)).Times(1).WillOnce(Return(true));
368 EXPECT_CALL(mock_listener, OnUpdateStatusChanged(_)).Times(1);
370 PolicyManagerImpl* manager = new PolicyManagerImpl();
371 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
372 manager->set_listener(&mock_listener);
374 EXPECT_TRUE(manager->LoadPT("file_pt_update.json", msg));
377 TEST_F(PolicyManagerImplTest, RequestPTUpdate) {
378 ::testing::NiceMock<MockPTRepresentation> mock_pt;
379 MockPolicyListener mock_listener;
381 ::utils::SharedPtr< ::policy_table::Table> p_table =
382 new ::policy_table::Table();
383 std::string json = p_table->ToJsonValue().toStyledString();
384 ::policy::BinaryMessageSptr expect = new ::policy::BinaryMessage(json.begin(),
387 EXPECT_CALL(mock_pt, GenerateSnapshot()).WillOnce(Return(p_table));
388 EXPECT_CALL(mock_listener, OnUpdateStatusChanged(_)).Times(2);
390 PolicyManagerImpl* manager = new PolicyManagerImpl();
391 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
392 manager->set_listener(&mock_listener);
393 ::policy::BinaryMessageSptr output = manager->RequestPTUpdate();
394 EXPECT_EQ(*expect, *output);
397 #ifdef EXTENDED_POLICY
398 TEST_F(PolicyManagerImplTest, ResetUserConsent) {
399 ::testing::NiceMock<MockPTExtRepresentation> mock_pt;
401 EXPECT_CALL(mock_pt, ResetUserConsent()).WillOnce(Return(true)).WillOnce(
404 PolicyManagerImpl* manager = new PolicyManagerImpl();
405 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
406 EXPECT_TRUE(manager->ResetUserConsent());
407 EXPECT_FALSE(manager->ResetUserConsent());
409 #endif // EXTENDED_POLICY
411 TEST_F(PolicyManagerImplTest, CheckAppPolicyState) {
412 ::testing::NiceMock<MockPTExtRepresentation> mock_pt;
414 // TODO(AOleynik): Implementation of method should be changed to avoid
416 PolicyManagerImpl* manager = new PolicyManagerImpl();
417 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
418 //manager->CheckAppPolicyState("12345678");
421 TEST_F(PolicyManagerImplTest, GetPolicyTableStatus) {
422 ::testing::NiceMock<MockPTExtRepresentation> mock_pt;
424 PolicyManagerImpl* manager = new PolicyManagerImpl();
425 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
426 // TODO(AOleynik): Test is not finished, to be continued
427 //manager->GetPolicyTableStatus();
430 TEST_F(PolicyManagerImplTest, MarkUnpairedDevice) {
431 ::testing::NiceMock<MockPTExtRepresentation> mock_pt;
433 EXPECT_CALL(mock_pt, SetUnpairedDevice("12345")).WillOnce(Return(true));
435 PolicyManagerImpl* manager = new PolicyManagerImpl();
436 manager->ResetDefaultPT(::policy::PolicyTable(&mock_pt));
437 manager->MarkUnpairedDevice("12345");
441 } // namespace policy
442 } // namespace components
445 int main(int argc, char** argv) {
446 testing::InitGoogleTest(&argc, argv);
447 return RUN_ALL_TESTS();