pull in new policy updates
[profile/ivi/smartdevicelink.git] / src / components / policy / test / policy / src / test_stress_policy_manager_impl.cc
1 /* Copyright (c) 2013, Ford Motor Company
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
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
13  * distribution.
14  *
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.
18  *
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.
30  */
31
32 #include <gtest/gtest.h>
33 #include <gmock/gmock.h>
34 #include <string>
35 #include <set>
36 #include <sstream>
37 #include <fstream>
38 #include "policy/policy_manager_impl.h"
39 #include "mock_policy_listener.h"
40
41 using ::testing::_;
42 using ::policy::PolicyManagerImpl;
43 using ::policy::BinaryMessage;
44 using ::policy::MockPolicyListener;
45
46 namespace test {
47 namespace components {
48 namespace policy {
49
50 class PolicyManagerImplTest : public ::testing::Test {
51  protected:
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;
58
59   static void SetUpTestCase() {
60     std::ofstream ofs;
61     ofs.open(kNameFile.c_str(), std::ofstream::out);
62     CreateTable(ofs);
63     ofs.close();
64
65     manager = new PolicyManagerImpl();
66     ASSERT_TRUE(manager->InitPT(kNameFile));
67   }
68
69   static void TearDownTestCase() {
70     delete manager;
71     remove(kNameFile.c_str());
72     remove("policy.sqlite");
73   }
74
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);
80 };
81
82 const std::string PolicyManagerImplTest::kNameFile = "pt.json";
83 PolicyManagerImpl* PolicyManagerImplTest::manager = 0;
84
85 void PolicyManagerImplTest::CreateGroups(std::ofstream& ofs) {
86   std::stringstream ss;
87   std::string number;
88   for (int i = 0; i < kNumberGroups - 1; ++i) {
89     ss << i << std::endl;
90     ss >> number;
91     ofs << "\"Group-" << number << "\":{ \"rpcs\":{";
92     CreateFuncs(ofs);
93     ofs << "} },";
94   }
95   ss << kNumberGroups - 1 << std::endl;
96   ss >> number;
97   ofs << "\"Group-" << number << "\":{ \"rpcs\":{";
98   CreateFuncs(ofs);
99   ofs << "} }";
100 }
101
102 void PolicyManagerImplTest::CreateFuncs(std::ofstream& ofs) {
103   std::string func = "{"
104       "\"hmi_levels\":["
105       "\"BACKGROUND\","
106       "\"FULL\","
107       "\"LIMITED\""
108       "],"
109       "\"parameters\":["
110       "\"gps\","
111       "\"speed\","
112       "\"enginetorque\","
113       "\"externaltemperature\","
114       "\"fuellevel\","
115       "\"fuellevel_state\","
116       "\"headlampstatus\","
117       "\"instantfuelconsumption\","
118       "\"odometer\","
119       "\"tirepressure\","
120       "\"wiperstatus\","
121       "\"vin\","
122       "\"accpedalposition\","
123       "\"beltstatus\","
124       "\"driverbraking\","
125       "\"prndl\","
126       "\"rpm\","
127       "\"steeringwheelangle\""
128       "]"
129       "}";
130
131   std::stringstream ss;
132   std::string number;
133   for (int i = 0; i < kNumberFuncs - 1; ++i) {
134     ss << i << std::endl;
135     ss >> number;
136     ofs << "\"Func-" << number << "\":" << func << ",";
137   }
138   ss << kNumberFuncs - 1 << std::endl;
139   ss >> number;
140   ofs << "\"Func-" << number << "\":" + func;
141 }
142
143 void PolicyManagerImplTest::CreateApps(std::ofstream& ofs) {
144   ofs << "\"default\":{"
145       "\"groups\":["
146       "\"Group-1\""
147       "]"
148       "},";
149
150   std::stringstream ss;
151   std::string number;
152   for (int i = 0; i < kNumberApps - 1; ++i) {
153     ss << i << std::endl;
154     ss >> number;
155     ofs << "\"" << number << "\": { \"groups\": ";
156     CreateAppGroups(ofs);
157     ofs << "},";
158   }
159   ss << kNumberApps - 1 << std::endl;
160   ss >> number;
161   ofs << "\"" << number << "\": { \"groups\": ";
162   CreateAppGroups(ofs);
163   ofs << "}";
164 }
165
166 void PolicyManagerImplTest::CreateAppGroups(std::ofstream& ofs) {
167   ofs << "[";
168
169   std::stringstream ss;
170   std::string number;
171   std::set<int> app_groups;
172   for (int i = 0; i < kNumberAppGroups; ++i) {
173     app_groups.insert(rand() % kNumberGroups);
174   }
175
176   std::set<int>::const_iterator i = app_groups.begin();
177   ss << *i << std::endl;
178   ss >> number;
179   ofs << "\"Group-" << number << "\"";
180   ++i;
181   for (; i != app_groups.end(); ++i) {
182     ss << *i << std::endl;
183     ss >> number;
184     ofs << ",\"Group-" << number << "\"";
185   }
186   ofs << "]";
187 }
188
189 void PolicyManagerImplTest::CreateTable(std::ofstream& ofs) {
190   ofs << "{"
191       "\"policy_table\":{"
192       "\"module_config\":{"
193       "\"preloaded_pt\":true,"
194       "\"endpoints\":{"
195       "\"default\": {"
196       "\"default\":["
197       "\"http://sdl.net/api\""
198       "]"
199       "}"
200       "},"
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]"
206       "},"
207       "\"consumer_friendly_messages\":{"
208       "\"version\":\"001.001.001\","
209       "\"messages\":{} },"
210       "\"functional_groupings\":{";
211
212   CreateGroups(ofs);
213
214   ofs << "}, \"app_policies\":{";
215
216   CreateApps(ofs);
217
218   ofs << "}";
219 }
220
221 TEST_F(PolicyManagerImplTest, StressTestOneCheck) {
222   MockPolicyListener mock_listener;
223
224   EXPECT_CALL(mock_listener, OnCurrentDeviceIdUpdateRequired("2")).Times(1);
225
226   manager->set_listener(&mock_listener);
227   ::policy::CheckPermissionResult output = manager->CheckPermissions("2",
228                                                                      "FULL",
229                                                                      "Func-1");
230   EXPECT_EQ(::policy::kRpcAllowed, output.hmi_level_permitted);
231 }
232
233 TEST_F(PolicyManagerImplTest, StressTestNoPermission) {
234   MockPolicyListener mock_listener;
235
236   EXPECT_CALL(mock_listener, OnCurrentDeviceIdUpdateRequired("150")).Times(1);
237
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);
242 }
243
244 TEST_F(PolicyManagerImplTest, StressTestFewChecks) {
245   MockPolicyListener mock_listener;
246
247   EXPECT_CALL(mock_listener, OnCurrentDeviceIdUpdateRequired(_)).Times(100);
248   manager->set_listener(&mock_listener);
249
250   const int kNumberOfCheckings = 100;
251   std::stringstream ss;
252   int app, func;
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;
258     ss >> app_number;
259     ss << func << std::endl;
260     ss >> func_number;
261
262     ::policy::CheckPermissionResult output = manager->CheckPermissions(
263         app_number, "FULL", "Func-" + func_number);
264     EXPECT_EQ(::policy::kRpcAllowed, output.hmi_level_permitted);
265   }
266 }
267
268 }  // namespace policy
269 }  // namespace components
270 }  // namespace test
271
272 int main(int argc, char** argv) {
273   testing::InitGoogleTest(&argc, argv);
274   return RUN_ALL_TESTS();
275 }