ae19b0b1501898264b5f57cf027195561c069446
[profile/ivi/smartdevicelink.git] / src / components / policy / test / policy / src / test_sql_pt_ext_representation.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 <sqlite3.h>
34 #include <vector>
35 #include "json/value.h"
36 #include "policy/sql_pt_ext_representation.h"
37 #include "policy/policy_types.h"
38 #include "policy_table_interface_base/types.h"
39 #include "policy_table_interface_base/enums.h"
40
41 using policy::SQLPTExtRepresentation;
42
43 namespace test {
44 namespace components {
45 namespace policy {
46
47 class SQLPTExtRepresentationTest : public ::testing::Test {
48  protected:
49   static sqlite3* conn;
50   static SQLPTExtRepresentation* reps;
51   static const std::string kFileName;
52
53   static void SetUpTestCase() {
54     reps = new SQLPTExtRepresentation;
55     EXPECT_EQ(::policy::SUCCESS, reps->Init());
56     EXPECT_EQ(SQLITE_OK, sqlite3_open(kFileName.c_str(), &conn));
57   }
58
59   static void TearDownTestCase() {
60     EXPECT_TRUE(reps->Clear());
61     EXPECT_TRUE(reps->Close());
62     delete reps;
63     sqlite3_close(conn);
64     remove(kFileName.c_str());
65   }
66 };
67
68 sqlite3* SQLPTExtRepresentationTest::conn = 0;
69 SQLPTExtRepresentation* SQLPTExtRepresentationTest::reps = 0;
70 const std::string SQLPTExtRepresentationTest::kFileName = "policy.sqlite";
71
72 ::testing::AssertionResult IsValid(const policy_table::Table &table) {
73   if (table.is_valid()) {
74     return ::testing::AssertionSuccess();
75   } else {
76     ::rpc::ValidationReport report(" - table");
77     table.ReportErrors(&report);
78     return ::testing::AssertionFailure() << ::rpc::PrettyFormat(report);
79   }
80 }
81
82 TEST_F(SQLPTExtRepresentationTest, SaveGenerateSnapshot) {
83   Json::Value expect(Json::objectValue);
84   expect["policy_table"] = Json::Value(Json::objectValue);
85
86   Json::Value& policy_table = expect["policy_table"];
87   policy_table["module_meta"] = Json::Value(Json::objectValue);
88   policy_table["module_config"] = Json::Value(Json::objectValue);
89   policy_table["usage_and_error_counts"] = Json::Value(Json::objectValue);
90   policy_table["device_data"] = Json::Value(Json::objectValue);
91   policy_table["functional_groupings"] = Json::Value(Json::objectValue);
92   policy_table["consumer_friendly_messages"] = Json::Value(Json::objectValue);
93   policy_table["app_policies"] = Json::Value(Json::objectValue);
94
95   Json::Value& module_meta = policy_table["module_meta"];
96   module_meta["ccpu_version"] = Json::Value("ccpu version");
97   module_meta["language"] = Json::Value("ru");
98   module_meta["wers_country_code"] = Json::Value("ru");
99   module_meta["pt_exchanged_at_odometer_x"] = Json::Value(0);
100   module_meta["pt_exchanged_x_days_after_epoch"] = Json::Value(0);
101   module_meta["ignition_cycles_since_last_exchange"] = Json::Value(0);
102   module_meta["vin"] = Json::Value("vin");
103
104   Json::Value& module_config = policy_table["module_config"];
105   module_config["preloaded_pt"] = Json::Value(true);
106   module_config["exchange_after_x_ignition_cycles"] = Json::Value(10);
107   module_config["exchange_after_x_kilometers"] = Json::Value(100);
108   module_config["exchange_after_x_days"] = Json::Value(5);
109   module_config["timeout_after_x_seconds"] = Json::Value(500);
110   module_config["seconds_between_retries"] = Json::Value(Json::arrayValue);
111   module_config["seconds_between_retries"][0] = Json::Value(10);
112   module_config["seconds_between_retries"][1] = Json::Value(20);
113   module_config["seconds_between_retries"][2] = Json::Value(30);
114   module_config["endpoints"] = Json::Value(Json::objectValue);
115   module_config["endpoints"]["0x00"] = Json::Value(Json::objectValue);
116   module_config["endpoints"]["0x00"]["default"] = Json::Value(Json::arrayValue);
117   module_config["endpoints"]["0x00"]["default"][0] = Json::Value(
118       "http://ford.com/cloud/default");
119   module_config["notifications_per_minute_by_priority"] = Json::Value(
120       Json::objectValue);
121   module_config["notifications_per_minute_by_priority"]["emergency"] =
122       Json::Value(1);
123   module_config["notifications_per_minute_by_priority"]["navigation"] =
124       Json::Value(2);
125   module_config["notifications_per_minute_by_priority"]["voiceCommunication"] =
126       Json::Value(3);
127   module_config["notifications_per_minute_by_priority"]["communication"] =
128       Json::Value(4);
129   module_config["notifications_per_minute_by_priority"]["normal"] = Json::Value(
130       5);
131   module_config["notifications_per_minute_by_priority"]["none"] = Json::Value(
132       6);
133   module_config["vehicle_make"] = Json::Value("MakeT");
134   module_config["vehicle_model"] = Json::Value("ModelT");
135   module_config["vehicle_year"] = Json::Value(2014);
136
137   Json::Value& usage_and_error_counts = policy_table["usage_and_error_counts"];
138   usage_and_error_counts["count_of_iap_buffer_full"] = Json::Value(0);
139   usage_and_error_counts["count_sync_out_of_memory"] = Json::Value(0);
140   usage_and_error_counts["count_of_sync_reboots"] = Json::Value(0);
141
142   Json::Value& device_data = policy_table["device_data"];
143   device_data["DEVICEHASH"] = Json::Value(Json::objectValue);
144   device_data["DEVICEHASH"]["hardware"] = Json::Value("hardware");
145   device_data["DEVICEHASH"]["firmware_rev"] = Json::Value("firmware_rev");
146   device_data["DEVICEHASH"]["os"] = Json::Value("os");
147   device_data["DEVICEHASH"]["os_version"] = Json::Value("os_version");
148   device_data["DEVICEHASH"]["carrier"] = Json::Value("carrier");
149   device_data["DEVICEHASH"]["max_number_rfcom_ports"] = Json::Value(10);
150
151   Json::Value& functional_groupings = policy_table["functional_groupings"];
152   functional_groupings["default"] = Json::Value(Json::objectValue);
153   Json::Value& default_group = functional_groupings["default"];
154   default_group["rpcs"] = Json::Value(Json::objectValue);
155   default_group["rpcs"]["Update"] = Json::Value(Json::objectValue);
156   default_group["rpcs"]["Update"]["hmi_levels"] = Json::Value(Json::arrayValue);
157   default_group["rpcs"]["Update"]["hmi_levels"][0] = Json::Value("FULL");
158   default_group["rpcs"]["Update"]["parameters"] = Json::Value(Json::arrayValue);
159   default_group["rpcs"]["Update"]["parameters"][0] = Json::Value("speed");
160
161   Json::Value& consumer_friendly_messages =
162       policy_table["consumer_friendly_messages"];
163   consumer_friendly_messages["version"] = Json::Value("1.2");
164
165   Json::Value& app_policies = policy_table["app_policies"];
166   app_policies["default"] = Json::Value(Json::objectValue);
167   app_policies["default"]["memory_kb"] = Json::Value(50);
168   app_policies["default"]["watchdog_timer_ms"] = Json::Value(100);
169   app_policies["default"]["groups"] = Json::Value(Json::arrayValue);
170   app_policies["default"]["groups"][0] = Json::Value("default");
171   app_policies["default"]["priority"] = Json::Value("EMERGENCY");
172   app_policies["default"]["default_hmi"] = Json::Value("FULL");
173   app_policies["default"]["keep_context"] = Json::Value(true);
174   app_policies["default"]["steal_focus"] = Json::Value(true);
175   app_policies["default"]["certificate"] = Json::Value("sign");
176
177   policy_table::Table table(&expect);
178
179   ASSERT_TRUE(IsValid(table));
180   ASSERT_TRUE(reps->Save(table));
181   ASSERT_TRUE(reps->SetMetaInfo("ccpu version", "ru", "ru"));
182   const char* query_vin = "UPDATE `module_meta` SET `vin` = 'vin'; ";
183   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_vin, NULL, NULL, NULL));
184   utils::SharedPtr<policy_table::Table> snapshot = reps->GenerateSnapshot();
185   EXPECT_TRUE(IsValid(*snapshot));
186   EXPECT_EQ(table.ToJsonValue().toStyledString(),
187             snapshot->ToJsonValue().toStyledString());
188 }
189
190 TEST_F(SQLPTExtRepresentationTest, CanAppKeepContext) {
191   const char* query_delete = "DELETE FROM `application`; ";
192   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_delete, NULL, NULL, NULL));
193   const char* query_insert = "INSERT INTO `application` (`id`, `memory_kb`,"
194       " `watchdog_timer_ms`, `keep_context`) VALUES ('12345', 5, 10, 1)";
195   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_insert, NULL, NULL, NULL));
196   EXPECT_FALSE(reps->CanAppKeepContext("0"));
197   EXPECT_TRUE(reps->CanAppKeepContext("12345"));
198 }
199
200 TEST_F(SQLPTExtRepresentationTest, CanAppStealFocus) {
201   const char* query_delete = "DELETE FROM `application`; ";
202   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_delete, NULL, NULL, NULL));
203   const char* query_insert = "INSERT INTO `application` (`id`, `memory_kb`,"
204       " `watchdog_timer_ms`, `steal_focus`) VALUES ('12345', 5, 10, 1)";
205   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_insert, NULL, NULL, NULL));
206   EXPECT_TRUE(reps->CanAppStealFocus("12345"));
207   EXPECT_FALSE(reps->CanAppStealFocus("0"));
208 }
209
210 TEST_F(SQLPTExtRepresentationTest, IncrementGlobalCounter) {
211   const char* query_update = "UPDATE `usage_and_error_count` SET"
212       " `count_of_sync_reboots` = 0";
213   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_update, NULL, NULL, NULL));
214
215   reps->Increment("count_of_sync_reboots");
216   reps->Increment("count_of_sync_reboots");
217   reps->Increment("count_of_sync_reboots");
218
219   const char* query_select =
220       "SELECT `count_of_sync_reboots` FROM `usage_and_error_count`";
221   sqlite3_stmt* statement;
222   ASSERT_EQ(SQLITE_OK,
223             sqlite3_prepare(conn, query_select, -1, &statement, NULL));
224   ASSERT_EQ(SQLITE_ROW, sqlite3_step(statement));
225   EXPECT_EQ(3, sqlite3_column_int(statement, 0));
226   EXPECT_EQ(SQLITE_DONE, sqlite3_step(statement));
227 }
228
229 TEST_F(SQLPTExtRepresentationTest, IncrementAppCounter) {
230   const char* query_delete =
231       "DELETE FROM `app_level` WHERE `application_id` = '12345'";
232   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_delete, NULL, NULL, NULL));
233
234   reps->Increment("12345", "count_of_user_selections");
235   reps->Increment("12345", "count_of_user_selections");
236   reps->Increment("12345", "count_of_user_selections");
237
238   const char* query_select =
239       "SELECT `count_of_user_selections` FROM `app_level`"
240           "  WHERE `application_id` = '12345'";
241   sqlite3_stmt* statement;
242   ASSERT_EQ(SQLITE_OK,
243             sqlite3_prepare(conn, query_select, -1, &statement, NULL));
244   ASSERT_EQ(SQLITE_ROW, sqlite3_step(statement));
245   EXPECT_EQ(3, sqlite3_column_int(statement, 0));
246   EXPECT_EQ(SQLITE_DONE, sqlite3_step(statement));
247 }
248
249 TEST_F(SQLPTExtRepresentationTest, SetAppInfo) {
250   const char* query_delete =
251       "DELETE FROM `app_level` WHERE `application_id` = '12345'";
252   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_delete, NULL, NULL, NULL));
253
254   reps->Set("12345", "app_registration_language_gui", "ru-ru");
255   reps->Set("12345", "app_registration_language_vui", "en-en");
256
257   const char* query_select = "SELECT `app_registration_language_gui`, "
258       " `app_registration_language_vui` FROM `app_level`"
259       "  WHERE `application_id` = '12345'";
260   sqlite3_stmt* statement;
261   ASSERT_EQ(SQLITE_OK,
262             sqlite3_prepare(conn, query_select, -1, &statement, NULL));
263   ASSERT_EQ(SQLITE_ROW, sqlite3_step(statement));
264
265   const unsigned char* gui = sqlite3_column_text(statement, 0);
266   const unsigned char* vui = sqlite3_column_text(statement, 1);
267   ASSERT_TRUE(gui);
268   ASSERT_TRUE(vui);
269   EXPECT_EQ("ru-ru", std::string(reinterpret_cast<const char*>(gui)));
270   EXPECT_EQ("en-en", std::string(reinterpret_cast<const char*>(vui)));
271   EXPECT_EQ(SQLITE_DONE, sqlite3_step(statement));
272 }
273
274 TEST_F(SQLPTExtRepresentationTest, AddAppStopwatch) {
275   const char* query_delete =
276       "DELETE FROM `app_level` WHERE `application_id` = '12345'";
277   ASSERT_EQ(SQLITE_OK, sqlite3_exec(conn, query_delete, NULL, NULL, NULL));
278
279   reps->Add("12345", "minutes_in_hmi_full", 10);
280   reps->Add("12345", "minutes_in_hmi_full", 60);
281
282   const char* query_select = "SELECT `minutes_in_hmi_full` FROM `app_level`"
283       "  WHERE `application_id` = '12345'";
284   sqlite3_stmt* statement;
285   ASSERT_EQ(SQLITE_OK,
286             sqlite3_prepare(conn, query_select, -1, &statement, NULL));
287   ASSERT_EQ(SQLITE_ROW, sqlite3_step(statement));
288   EXPECT_EQ(70, sqlite3_column_int(statement, 0));
289   EXPECT_EQ(SQLITE_DONE, sqlite3_step(statement));
290 }
291
292 }  // namespace policy
293 }  // namespace components
294 }  // namespace test
295
296 int main(int argc, char** argv) {
297   testing::InitGoogleTest(&argc, argv);
298   return RUN_ALL_TESTS();
299 }