2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <gtest/gtest.h>
20 #include <rua_internal.h>
27 #include "sqlite3_mock.h"
29 #include "test_fixture.h"
32 using ::testing::DoAll;
33 using ::testing::SetArgPointee;
34 using ::testing::Return;
36 static char** __create_table(void) {
37 char** table = (char** )calloc(10, sizeof(char*));
38 table[0] = strdup("pkgname");
39 if (table[0] == nullptr)
42 table[1] = strdup("apppath");
43 if (table[1] == nullptr)
46 table[2] = strdup("arg");
47 if (table[2] == nullptr)
50 table[3] = strdup("122232");
51 if (table[3] == nullptr)
54 table[4] = strdup("instance_id");
55 if (table[4] == nullptr)
58 table[5] = strdup("instance_name");
59 if (table[5] == nullptr)
62 table[6] = strdup("icon");
63 if (table[6] == nullptr)
66 table[7] = strdup("uri");
67 if (table[7] == nullptr)
70 table[8] = strdup("image");
71 if (table[8] == nullptr)
74 table[9] = strdup("compid");
75 if (table[9] == nullptr)
81 for (int i = 0; i < 10; i++) {
90 class InternalMocks : public ::testing::NiceMock<GioMock>,
91 public ::testing::NiceMock<SqlMock> {};
93 class RuaInternalTest : public TestFixture {
95 RuaInternalTest() : TestFixture(std::make_unique<InternalMocks>()) {}
96 virtual ~RuaInternalTest() {}
98 virtual void SetUp() {
99 table_ = __create_table();
100 if (table_ == nullptr)
104 virtual void TearDown() {
106 for (int i = 0; i < 10; i++)
113 char** table_ = nullptr;
116 TEST_F(RuaInternalTest, rua_db_add_history) {
117 ASSERT_TRUE(table_ != nullptr);
118 sqlite3* db = (sqlite3*) table_;
120 EXPECT_CALL(GetMock<SqlMock>(),
121 sqlite3_open_v2(_, _, _, _)).
122 WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
123 EXPECT_CALL(GetMock<SqlMock>(),
124 sqlite3_close_v2(_)).WillRepeatedly(Return(0));
125 EXPECT_CALL(GetMock<SqlMock>(),
126 sqlite3_free_table(_));
127 EXPECT_CALL(GetMock<SqlMock>(),
128 sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
129 EXPECT_CALL(GetMock<SqlMock>(),
130 sqlite3_get_table(_, _, _, _, _, _)).
131 WillOnce(DoAll(SetArgPointee<2>(table_),
133 SetArgPointee<4>(0), (Return(0))));
134 EXPECT_CALL(GetMock<SqlMock>(),
135 sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
136 EXPECT_CALL(GetMock<SqlMock>(),
137 sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
138 EXPECT_CALL(GetMock<SqlMock>(),
139 sqlite3_bind_int(_, _, _)).WillOnce(Return(0));
140 EXPECT_CALL(GetMock<SqlMock>(),
141 sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
142 EXPECT_CALL(GetMock<SqlMock>(),
143 sqlite3_finalize(_)).WillOnce(Return(0));
144 EXPECT_CALL(GetMock<GioMock>(),
145 g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
146 WillOnce(Return(true));
147 EXPECT_CALL(GetMock<GioMock>(),
148 g_dbus_connection_flush_sync(_, _, _)).
149 WillOnce(Return(true));
151 char** get_table = nullptr;
154 struct rua_rec record;
155 int ret = rua_history_load_db(&get_table, &rows, &cols);
157 EXPECT_TRUE(get_table != nullptr);
159 ret = rua_history_get_rec(&record, get_table, rows, cols, 0);
162 ret = rua_db_add_history(&record);
165 ret = rua_history_unload_db(&get_table);
169 TEST_F(RuaInternalTest, rua_db_add_history_for_update) {
170 ASSERT_TRUE(table_ != nullptr);
174 sqlite3* db = (sqlite3*) table_;
176 EXPECT_CALL(GetMock<SqlMock>(),
177 sqlite3_open_v2(_, _, _, _)).
178 WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
179 EXPECT_CALL(GetMock<SqlMock>(),
180 sqlite3_close_v2(_)).WillRepeatedly(Return(0));
181 EXPECT_CALL(GetMock<SqlMock>(),
182 sqlite3_free_table(_));
183 EXPECT_CALL(GetMock<SqlMock>(),
184 sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
185 EXPECT_CALL(GetMock<SqlMock>(),
186 sqlite3_get_table(_, _, _, _, _, _)).
187 WillOnce(DoAll(SetArgPointee<2>(table_),
189 SetArgPointee<4>(0), (Return(0))));
190 EXPECT_CALL(GetMock<SqlMock>(),
191 sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
192 EXPECT_CALL(GetMock<SqlMock>(),
193 sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
194 EXPECT_CALL(GetMock<SqlMock>(),
195 sqlite3_bind_int(_, _, _)).WillOnce(Return(0));
196 EXPECT_CALL(GetMock<SqlMock>(),
197 sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
198 EXPECT_CALL(GetMock<SqlMock>(),
199 sqlite3_finalize(_)).WillOnce(Return(0));
200 EXPECT_CALL(GetMock<GioMock>(),
201 g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
202 WillOnce(Return(true));
203 EXPECT_CALL(GetMock<GioMock>(),
204 g_dbus_connection_flush_sync(_, _, _)).
205 WillOnce(Return(true));
207 char** get_table = nullptr;
210 struct rua_rec record;
211 int ret = rua_history_load_db(&get_table, &rows, &cols);
213 EXPECT_TRUE(get_table != nullptr);
215 ret = rua_history_get_rec(&record, get_table, rows, cols, 0);
218 ret = rua_db_add_history(&record);
221 ret = rua_history_unload_db(&get_table);
225 TEST_F(RuaInternalTest, rua_usr_db_update_image) {
226 ASSERT_TRUE(table_ != nullptr);
227 sqlite3* db = (sqlite3*) table_;
229 EXPECT_CALL(GetMock<SqlMock>(),
230 sqlite3_open_v2(_, _, _, _)).
231 WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
232 EXPECT_CALL(GetMock<SqlMock>(),
233 sqlite3_close_v2(_)).WillRepeatedly(Return(0));
234 EXPECT_CALL(GetMock<SqlMock>(),
235 sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
236 EXPECT_CALL(GetMock<SqlMock>(),
237 sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
238 EXPECT_CALL(GetMock<SqlMock>(),
239 sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
240 EXPECT_CALL(GetMock<SqlMock>(),
241 sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
242 EXPECT_CALL(GetMock<SqlMock>(),
243 sqlite3_finalize(_)).WillOnce(Return(0));
244 EXPECT_CALL(GetMock<GioMock>(),
245 g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
246 WillOnce(Return(true));
247 EXPECT_CALL(GetMock<GioMock>(),
248 g_dbus_connection_flush_sync(_, _, _)).
249 WillOnce(Return(true));
251 int ret = rua_db_update_image("pkg_name", "comp_id", "inst_id", "image");
255 TEST_F(RuaInternalTest, rua_db_delete_history) {
256 ASSERT_TRUE(table_ != nullptr);
257 sqlite3* db = (sqlite3*) table_;
259 bundle* b = bundle_create();
260 ASSERT_TRUE(b != nullptr);
263 bundle_add_str(b, AUL_K_RUA_PKGNAME, "pkgname");
266 EXPECT_CALL(GetMock<SqlMock>(),
267 sqlite3_open_v2(_, _, _, _)).
268 WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
269 EXPECT_CALL(GetMock<SqlMock>(),
270 sqlite3_close_v2(_)).WillRepeatedly(Return(0));
271 EXPECT_CALL(GetMock<SqlMock>(),
272 sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
273 EXPECT_CALL(GetMock<SqlMock>(),
274 sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
275 EXPECT_CALL(GetMock<SqlMock>(),
276 sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
277 EXPECT_CALL(GetMock<SqlMock>(),
278 sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
279 EXPECT_CALL(GetMock<SqlMock>(),
280 sqlite3_finalize(_)).WillOnce(Return(0));
281 EXPECT_CALL(GetMock<GioMock>(),
282 g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
283 WillOnce(Return(true));
284 EXPECT_CALL(GetMock<GioMock>(),
285 g_dbus_connection_flush_sync(_, _, _)).
286 WillOnce(Return(true));
288 int ret = rua_db_delete_history(b);
295 TEST_F(RuaInternalTest, rua_db_delete_history_apppath) {
296 ASSERT_TRUE(table_ != nullptr);
297 sqlite3* db = (sqlite3*) table_;
299 bundle* b = bundle_create();
300 ASSERT_TRUE(b != nullptr);
303 bundle_add_str(b, AUL_K_RUA_APPPATH, "apppath");
305 EXPECT_CALL(GetMock<SqlMock>(),
306 sqlite3_open_v2(_, _, _, _)).
307 WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
308 EXPECT_CALL(GetMock<SqlMock>(),
309 sqlite3_close_v2(_)).WillRepeatedly(Return(0));
310 EXPECT_CALL(GetMock<SqlMock>(),
311 sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
312 EXPECT_CALL(GetMock<SqlMock>(),
313 sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
314 EXPECT_CALL(GetMock<SqlMock>(),
315 sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
316 EXPECT_CALL(GetMock<SqlMock>(),
317 sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
318 EXPECT_CALL(GetMock<SqlMock>(),
319 sqlite3_finalize(_)).WillOnce(Return(0));
320 EXPECT_CALL(GetMock<GioMock>(),
321 g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
322 WillOnce(Return(true));
323 EXPECT_CALL(GetMock<GioMock>(),
324 g_dbus_connection_flush_sync(_, _, _)).
325 WillOnce(Return(true));
327 int ret = rua_db_delete_history(b);
334 TEST_F(RuaInternalTest, rua_db_delete_history_clear) {
335 ASSERT_TRUE(table_ != nullptr);
336 sqlite3* db = (sqlite3*) table_;
338 EXPECT_CALL(GetMock<SqlMock>(),
339 sqlite3_open_v2(_, _, _, _)).
340 WillRepeatedly(DoAll(SetArgPointee<1>(db), (Return(0))));
341 EXPECT_CALL(GetMock<SqlMock>(),
342 sqlite3_close_v2(_)).WillRepeatedly(Return(0));
343 EXPECT_CALL(GetMock<SqlMock>(),
344 sqlite3_busy_handler(_, _, _)).WillRepeatedly(Return(0));
345 EXPECT_CALL(GetMock<SqlMock>(),
346 sqlite3_prepare_v2(_, _, _, _, _)).WillOnce(Return(0));
347 EXPECT_CALL(GetMock<SqlMock>(),
348 sqlite3_bind_text(_, _, _, _, _)).WillRepeatedly(Return(0));
349 EXPECT_CALL(GetMock<SqlMock>(),
350 sqlite3_step(_)).WillOnce(Return(SQLITE_DONE));
351 EXPECT_CALL(GetMock<SqlMock>(),
352 sqlite3_finalize(_)).WillOnce(Return(0));
353 EXPECT_CALL(GetMock<GioMock>(),
354 g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
355 WillOnce(Return(true));
356 EXPECT_CALL(GetMock<GioMock>(),
357 g_dbus_connection_flush_sync(_, _, _)).
358 WillOnce(Return(true));
360 int ret = rua_db_delete_history(nullptr);