Fix lcov option for lcov 2.0
[platform/core/appfw/librua.git] / tests / unittest / rua_internal_unit_test.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <gtest/gtest.h>
18
19 #include <rua.h>
20 #include <rua_internal.h>
21
22 #include <string>
23 #include <vector>
24 #include <memory>
25
26 #include "aul_mock.h"
27 #include "sqlite3_mock.h"
28 #include "gio_mock.h"
29 #include "test_fixture.h"
30
31 using ::testing::_;
32 using ::testing::DoAll;
33 using ::testing::SetArgPointee;
34 using ::testing::Return;
35
36 static char** __create_table(void) {
37   char** table = (char** )calloc(10, sizeof(char*));
38   table[0] = strdup("pkgname");
39   if (table[0] == nullptr)
40     goto out;
41
42   table[1] = strdup("apppath");
43   if (table[1] == nullptr)
44     goto out;
45
46   table[2] = strdup("arg");
47   if (table[2] == nullptr)
48     goto out;
49
50   table[3] = strdup("122232");
51   if (table[3] == nullptr)
52     goto out;
53
54   table[4] = strdup("instance_id");
55   if (table[4] == nullptr)
56     goto out;
57
58   table[5] = strdup("instance_name");
59   if (table[5] == nullptr)
60     goto out;
61
62   table[6] = strdup("icon");
63   if (table[6] == nullptr)
64     goto out;
65
66   table[7] = strdup("uri");
67   if (table[7] == nullptr)
68     goto out;
69
70   table[8] = strdup("image");
71   if (table[8] == nullptr)
72     goto out;
73
74   table[9] = strdup("compid");
75   if (table[9] == nullptr)
76     goto out;
77
78   return table;
79
80 out:
81   for (int i = 0; i < 10; i++) {
82     if (table[i])
83       free(table[i]);
84   }
85
86   free(table);
87   return nullptr;
88 }
89
90 class InternalMocks : public ::testing::NiceMock<GioMock>,
91               public ::testing::NiceMock<SqlMock> {};
92
93 class RuaInternalTest : public TestFixture {
94  public:
95   RuaInternalTest() : TestFixture(std::make_unique<InternalMocks>()) {}
96   virtual ~RuaInternalTest() {}
97
98   virtual void SetUp() {
99     table_ = __create_table();
100     if (table_ == nullptr)
101       return;
102   }
103
104   virtual void TearDown() {
105     if (table_) {
106       for (int i = 0; i < 10; i++)
107         free(table_[i]);
108       free(table_);
109     }
110   }
111
112  public:
113   char** table_ = nullptr;
114 };
115
116 TEST_F(RuaInternalTest, rua_db_add_history) {
117   ASSERT_TRUE(table_ != nullptr);
118   sqlite3* db = (sqlite3*) table_;
119
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_),
132                      SetArgPointee<3>(1),
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));
150
151   char** get_table = nullptr;
152   int rows = 0;
153   int cols = 0;
154   struct rua_rec record;
155   int ret = rua_history_load_db(&get_table, &rows, &cols);
156   EXPECT_EQ(ret, 0);
157   EXPECT_TRUE(get_table != nullptr);
158
159   ret = rua_history_get_rec(&record, get_table, rows, cols, 0);
160   EXPECT_EQ(ret, 0);
161
162   ret = rua_db_add_history(&record);
163   EXPECT_EQ(ret, 0);
164
165   ret = rua_history_unload_db(&get_table);
166   EXPECT_EQ(ret, 0);
167 }
168
169 TEST_F(RuaInternalTest, rua_db_add_history_for_update) {
170   ASSERT_TRUE(table_ != nullptr);
171
172   free(table_[5]);
173   table_[5] = nullptr;
174   sqlite3* db = (sqlite3*) table_;
175
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_),
188                      SetArgPointee<3>(1),
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));
206
207   char** get_table = nullptr;
208   int rows = 0;
209   int cols = 0;
210   struct rua_rec record;
211   int ret = rua_history_load_db(&get_table, &rows, &cols);
212   EXPECT_EQ(ret, 0);
213   EXPECT_TRUE(get_table != nullptr);
214
215   ret = rua_history_get_rec(&record, get_table, rows, cols, 0);
216   EXPECT_EQ(ret, 0);
217
218   ret = rua_db_add_history(&record);
219   EXPECT_EQ(ret, 0);
220
221   ret = rua_history_unload_db(&get_table);
222   EXPECT_EQ(ret, 0);
223 }
224
225 TEST_F(RuaInternalTest, rua_usr_db_update_image) {
226   ASSERT_TRUE(table_ != nullptr);
227   sqlite3* db = (sqlite3*) table_;
228
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));
250
251   int ret = rua_db_update_image("pkg_name", "comp_id", "inst_id", "image");
252   EXPECT_EQ(ret, 0);
253 }
254
255 TEST_F(RuaInternalTest, rua_db_delete_history) {
256   ASSERT_TRUE(table_ != nullptr);
257   sqlite3* db = (sqlite3*) table_;
258
259   bundle* b = bundle_create();
260   ASSERT_TRUE(b != nullptr);
261
262   if (b) {
263     bundle_add_str(b, AUL_K_RUA_PKGNAME, "pkgname");
264   }
265
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));
287
288   int ret = rua_db_delete_history(b);
289   EXPECT_EQ(ret, 0);
290
291   if (b)
292     bundle_free(b);
293 }
294
295 TEST_F(RuaInternalTest, rua_db_delete_history_apppath) {
296   ASSERT_TRUE(table_ != nullptr);
297   sqlite3* db = (sqlite3*) table_;
298
299   bundle* b = bundle_create();
300   ASSERT_TRUE(b != nullptr);
301
302   if (b)
303     bundle_add_str(b, AUL_K_RUA_APPPATH, "apppath");
304
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));
326
327   int ret = rua_db_delete_history(b);
328   EXPECT_EQ(ret, 0);
329
330   if (b)
331     bundle_free(b);
332 }
333
334 TEST_F(RuaInternalTest, rua_db_delete_history_clear) {
335   ASSERT_TRUE(table_ != nullptr);
336   sqlite3* db = (sqlite3*) table_;
337
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));
359
360   int ret = rua_db_delete_history(nullptr);
361   EXPECT_EQ(ret, 0);
362 }