0c470accb6d3f733f3c52a18eb6bce3643429c59
[platform/core/appfw/app2sd.git] / unit-tests / server / test_server.cc
1 /*
2  * app2sd-unittest
3  *
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <gmock/gmock.h>
20 #include <dlfcn.h>
21 #include <gio/gio.h>
22
23 #include <tzplatform_config.h>
24
25 #include "inc/app2ext_interface.h"
26
27 namespace app2sd {
28
29 static GDBusInterfaceMethodCallFunc __dbus_method;
30 static GVariant* __last_parameter;
31
32 class App2sdEnvironment : public ::testing::Environment {
33  public:
34   void SetUp() override {
35     int (*dl_main)(int, char**);
36     handle_ = dlopen("../plugin/app2sd/app2sd-server", RTLD_LOCAL | RTLD_LAZY);
37     dl_main = reinterpret_cast<int (*)(int, char**)>(dlsym(handle_, "main"));
38     dl_main(0, nullptr);
39   }
40
41   void TearDown() override {
42     dlclose(handle_);
43   }
44
45   static void TriggerMethod(const char* method, GVariant* parameters) {
46     __dbus_method(nullptr, "test", "org.test.test", "test", method, parameters, nullptr, nullptr);
47   }
48
49   static int GetLastResult() {
50     int r;
51     g_variant_get(__last_parameter, "(i)", &r);
52     return r;
53   }
54
55   void* handle_ = nullptr;
56 };
57
58 extern "C" guint g_bus_own_name(GBusType bus_type, const gchar *name,
59     GBusNameOwnerFlags flags, GBusAcquiredCallback bus_acquired_handler,
60     GBusNameAcquiredCallback name_acquired_handler,
61     GBusNameLostCallback name_lost_handler,
62     gpointer user_data,
63     GDestroyNotify user_data_free_func) {
64   bus_acquired_handler(nullptr, "testbus", user_data);
65   return 0;
66 }
67
68 extern "C" guint g_dbus_connection_register_object(GDBusConnection *connection,
69     const gchar *object_path, GDBusInterfaceInfo *interface_info,
70     const GDBusInterfaceVTable *vtable, gpointer user_data,
71     GDestroyNotify user_data_free_func, GError **error) {
72   __dbus_method = vtable->method_call;
73   return 1;
74 }
75
76 extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_sync(
77     GDBusConnection *connection, GDBusMessage *message,
78     GDBusSendMessageFlags flags, gint timeout_msec,
79     volatile guint32 *out_serial, GCancellable *cancellable,
80     GError **error) {
81   GDBusMessage* reply = g_dbus_message_new();
82   GVariant* body = g_variant_new("(u)", 0);
83   g_dbus_message_set_body(reply, body);
84   return reply;
85 }
86
87 extern "C" void g_dbus_method_invocation_return_value(GDBusMethodInvocation* invocation,
88     GVariant* parameters) {
89   __last_parameter = parameters;
90 }
91
92 extern "C" int storage_get_primary_sdcard(int *storage_id, char **path) {
93   *path = strdup("/tmp");
94   return 0;
95 }
96
97 extern "C" int system(const char* command) {
98   std::cout << "Execute" << std::endl;
99   std::cout << command << std::endl;
100   return 0;
101 }
102
103 typedef enum {
104         LOG_ID_INVALID = -1,
105         LOG_ID_MAIN,
106         LOG_ID_RADIO,
107         LOG_ID_SYSTEM,
108         LOG_ID_APPS,
109         LOG_ID_KMSG,
110         LOG_ID_SYSLOG,
111         LOG_ID_MAX
112 } log_id_t;
113
114 extern "C" int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
115 {
116         va_list ap;
117         va_start(ap, fmt);
118         vprintf(fmt, ap);
119         va_end(ap);
120         printf("\n");
121
122         return 0;
123 }
124
125 extern "C" const char* tzplatform_mkpath(enum tzplatform_variable id, const char* path) {
126   return path;
127 }
128
129 }
130
131 namespace {
132
133 const ::testing::Environment* env = nullptr;
134
135 class App2sdServerTest : public ::testing::Test {
136  protected:
137   void SetUp() override {}
138
139   void TearDown() override {
140     g_variant_unref(app2sd::__last_parameter);
141   }
142 };
143
144 TEST_F(App2sdServerTest, Install) {
145   GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(si)"));
146   g_variant_builder_add(builder, "(si)", "testname1", APP2EXT_DIR_RO);
147   g_variant_builder_add(builder, "(si)", "testname2", APP2EXT_DIR_RW);
148
149   GVariant* body = g_variant_new("(sia(si)i)", "not.exists.package", 10, builder, 0);
150
151   app2sd::App2sdEnvironment::TriggerMethod("PreAppInstall", body);
152   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Ne(0));
153
154   g_variant_builder_unref(builder);
155   g_variant_unref(body);
156
157   body = g_variant_new("(sii)", "not.exists.package", APP2EXT_STATUS_SUCCESS, 0);
158   app2sd::App2sdEnvironment::TriggerMethod("PostAppInstall", body);
159   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE));
160
161   g_variant_unref(body);
162 }
163
164 TEST_F(App2sdServerTest, Uninstall) {
165   GVariant* body = g_variant_new("(si)", "not.exsists.package", 0);
166
167   app2sd::App2sdEnvironment::TriggerMethod("PreAppUninstall", body);
168   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(APP2EXT_ERROR_INVALID_PACKAGE));
169
170   app2sd::App2sdEnvironment::TriggerMethod("PostAppUninstall", body);
171   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE));
172
173   g_variant_unref(body);
174 }
175
176 TEST_F(App2sdServerTest, Upgrade) {
177   GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(si)"));
178   g_variant_builder_add(builder, "(si)", "testname1", APP2EXT_DIR_RO);
179   g_variant_builder_add(builder, "(si)", "testname2", APP2EXT_DIR_RW);
180
181   GVariant* body = g_variant_new("(sia(si)i)", "not.exists.package", 10, builder, 0);
182
183   app2sd::App2sdEnvironment::TriggerMethod("PreAppUpgrade", body);
184   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Ne(0));
185
186   g_variant_builder_unref(builder);
187   g_variant_unref(body);
188
189   body = g_variant_new("(sii)", "not.exists.package", APP2EXT_STATUS_SUCCESS, 0);
190   app2sd::App2sdEnvironment::TriggerMethod("PostAppUpgrade", body);
191   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE));
192
193   g_variant_unref(body);
194 }
195
196 TEST_F(App2sdServerTest, Move) {
197   GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(si)"));
198   g_variant_builder_add(builder, "(si)", "testname1", APP2EXT_DIR_RO);
199   g_variant_builder_add(builder, "(si)", "testname2", APP2EXT_DIR_RW);
200
201   GVariant* body = g_variant_new("(sia(si)i)", "not.exists.package", APP2EXT_MOVE_TO_PHONE, builder, 0);
202
203   app2sd::App2sdEnvironment::TriggerMethod("PreMoveInstalledApp", body);
204   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Ne(0));
205
206   g_variant_builder_unref(builder);
207   g_variant_unref(body);
208
209   body = g_variant_new("(sii)", "not.exists.package", APP2EXT_STATUS_SUCCESS, 0);
210   app2sd::App2sdEnvironment::TriggerMethod("PostMoveInstalledApp", body);
211   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(0));
212
213   g_variant_unref(body);
214 }
215
216 TEST_F(App2sdServerTest, Enable) {
217   app2sd::App2sdEnvironment::TriggerMethod("EnableFullPkg", nullptr);
218   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(0));
219 }
220
221 TEST_F(App2sdServerTest, Ondemand) {
222   GVariant* body = g_variant_new("(si)", "not.exists.package", 0);
223   app2sd::App2sdEnvironment::TriggerMethod("OndemandSetupInit", body);
224   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Ne(0));
225   app2sd::App2sdEnvironment::TriggerMethod("OndemandSetupExit", body);
226   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE));
227   g_variant_unref(body);
228 }
229
230 TEST_F(App2sdServerTest, Migrate) {
231   GVariant* body = g_variant_new("(si)", "not.exists.package", 0);
232   app2sd::App2sdEnvironment::TriggerMethod("PreMigrateLegacy", body);
233   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(APP2EXT_ERROR_INVALID_PACKAGE));
234   app2sd::App2sdEnvironment::TriggerMethod("PostMigrateLegacy", body);
235   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE));
236   g_variant_unref(body);
237   app2sd::App2sdEnvironment::TriggerMethod("MigrateLegacyAll", nullptr);
238   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(APP2EXT_ERROR_PKGMGR_ERROR));
239 }
240
241 TEST_F(App2sdServerTest, ForceClean) {
242   GVariant* body = g_variant_new("(si)", "not.exists.package", 0);
243   app2sd::App2sdEnvironment::TriggerMethod("ForceClean", body);
244   EXPECT_THAT(app2sd::App2sdEnvironment::GetLastResult(), testing::Eq(0));
245   g_variant_unref(body);
246 }
247
248 } // namespace
249
250 int main(int argc, char* argv[]) {
251   try {
252     ::testing::InitGoogleTest(&argc, argv);
253     ::env = testing::AddGlobalTestEnvironment(new app2sd::App2sdEnvironment);
254     return RUN_ALL_TESTS();
255   } catch (...) {
256     std::cerr << "Exception occurred" << std::endl;
257     return -1;
258   }
259 }