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>
38 #include "policy/policy_manager_impl.h"
39 #include "mock_policy_listener.h"
42 using ::policy::PolicyManagerImpl;
43 using ::policy::BinaryMessage;
44 using ::policy::MockPolicyListener;
47 namespace components {
50 class PolicyManagerImplTest : public ::testing::Test {
52 static const std::string kNameFile;
53 static const int kNumberGroups = 100;
54 static const int kNumberFuncs = 200;
55 static const int kNumberApps = 100;
56 static const int kNumberAppGroups = 50;
57 static PolicyManagerImpl* manager;
59 static void SetUpTestCase() {
61 ofs.open(kNameFile.c_str(), std::ofstream::out);
65 manager = new PolicyManagerImpl();
66 ASSERT_TRUE(manager->LoadPTFromFile(kNameFile));
69 static void TearDownTestCase() {
71 remove(kNameFile.c_str());
72 remove("policy.sqlite");
75 static void CreateTable(std::ofstream& ofs);
76 static void CreateGroups(std::ofstream& ofs);
77 static void CreateFuncs(std::ofstream& ofs);
78 static void CreateApps(std::ofstream& ofs);
79 static void CreateAppGroups(std::ofstream& ofs);
82 const std::string PolicyManagerImplTest::kNameFile = "pt.json";
83 PolicyManagerImpl* PolicyManagerImplTest::manager = 0;
85 void PolicyManagerImplTest::CreateGroups(std::ofstream& ofs) {
88 for (int i = 0; i < kNumberGroups - 1; ++i) {
91 ofs << "\"Group-" << number << "\":{ \"rpcs\":{";
95 ss << kNumberGroups - 1 << std::endl;
97 ofs << "\"Group-" << number << "\":{ \"rpcs\":{";
102 void PolicyManagerImplTest::CreateFuncs(std::ofstream& ofs) {
103 std::string func = "{"
113 "\"externaltemperature\","
115 "\"fuellevel_state\","
116 "\"headlampstatus\","
117 "\"instantfuelconsumption\","
122 "\"accpedalposition\","
127 "\"steeringwheelangle\""
131 std::stringstream ss;
133 for (int i = 0; i < kNumberFuncs - 1; ++i) {
134 ss << i << std::endl;
136 ofs << "\"Func-" << number << "\":" << func << ",";
138 ss << kNumberFuncs - 1 << std::endl;
140 ofs << "\"Func-" << number << "\":" + func;
143 void PolicyManagerImplTest::CreateApps(std::ofstream& ofs) {
144 ofs << "\"default\":{"
150 std::stringstream ss;
152 for (int i = 0; i < kNumberApps - 1; ++i) {
153 ss << i << std::endl;
155 ofs << "\"" << number << "\": { \"groups\": ";
156 CreateAppGroups(ofs);
159 ss << kNumberApps - 1 << std::endl;
161 ofs << "\"" << number << "\": { \"groups\": ";
162 CreateAppGroups(ofs);
166 void PolicyManagerImplTest::CreateAppGroups(std::ofstream& ofs) {
169 std::stringstream ss;
171 std::set<int> app_groups;
172 for (int i = 0; i < kNumberAppGroups; ++i) {
173 app_groups.insert(rand() % kNumberGroups);
176 std::set<int>::const_iterator i = app_groups.begin();
177 ss << *i << std::endl;
179 ofs << "\"Group-" << number << "\"";
181 for (; i != app_groups.end(); ++i) {
182 ss << *i << std::endl;
184 ofs << ",\"Group-" << number << "\"";
189 void PolicyManagerImplTest::CreateTable(std::ofstream& ofs) {
192 "\"module_config\":{"
193 "\"preloaded_pt\":true,"
197 "\"http://sdl.net/api\""
201 "\"exchange_after_x_ignition_cycles\": 40,"
202 "\"exchange_after_x_kilometers\" : 2,"
203 "\"exchange_after_x_days\" : 23,"
204 "\"timeout_after_x_seconds\" : 20,"
205 "\"seconds_between_retries\" : [10, 7, 5, 3, 1]"
207 "\"consumer_friendly_messages\":{"
208 "\"version\":\"001.001.001\","
210 "\"functional_groupings\":{";
214 ofs << "}, \"app_policies\":{";
221 TEST_F(PolicyManagerImplTest, StressTestOneCheck) {
222 MockPolicyListener mock_listener;
224 EXPECT_CALL(mock_listener, OnCurrentDeviceIdUpdateRequired("2")).Times(1);
226 manager->set_listener(&mock_listener);
227 ::policy::CheckPermissionResult output = manager->CheckPermissions("2",
230 EXPECT_EQ(::policy::kRpcAllowed, output.hmi_level_permitted);
233 TEST_F(PolicyManagerImplTest, StressTestNoPermission) {
234 MockPolicyListener mock_listener;
236 EXPECT_CALL(mock_listener, OnCurrentDeviceIdUpdateRequired("150")).Times(1);
238 manager->set_listener(&mock_listener);
239 ::policy::CheckPermissionResult output = manager->CheckPermissions(
240 "150", "FULL", "Func-400");
241 EXPECT_EQ(::policy::kRpcDisallowed, output.hmi_level_permitted);
244 TEST_F(PolicyManagerImplTest, StressTestFewChecks) {
245 MockPolicyListener mock_listener;
247 EXPECT_CALL(mock_listener, OnCurrentDeviceIdUpdateRequired(_)).Times(100);
248 manager->set_listener(&mock_listener);
250 const int kNumberOfCheckings = 100;
251 std::stringstream ss;
253 std::string app_number, func_number;
254 for (int i = 0; i < kNumberOfCheckings; ++i) {
255 app = rand() % kNumberApps;
256 func = rand() % kNumberFuncs;
257 ss << app << std::endl;
259 ss << func << std::endl;
262 ::policy::CheckPermissionResult output = manager->CheckPermissions(
263 app_number, "FULL", "Func-" + func_number);
264 EXPECT_EQ(::policy::kRpcAllowed, output.hmi_level_permitted);
268 } // namespace policy
269 } // namespace components
272 int main(int argc, char** argv) {
273 testing::InitGoogleTest(&argc, argv);
274 return RUN_ALL_TESTS();