rework test bin.
[platform/core/appfw/app2sd.git] / test / src / test_app2ext.c
1 /*
2  * test_app2ext
3  *
4  * Copyright (c) 2016 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
20 #include <stdio.h>
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <malloc.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <sys/wait.h>
29 #include <sys/types.h>
30 #include <app2ext_interface.h>
31
32 #define SUCCESS 0
33 #define FAIL 1
34 #define CMD_LEN 256
35
36 app2ext_handle *handle = NULL;
37
38 #define TEST_PKGNAME "org.example.basicuiapplication"
39
40 char pkg_ro_content_rpm[3][5] = { "bin", "res", "lib" };
41
42 char error_list[45][100] = {
43         "SUCCESS",
44         "APP2EXT_ERROR_UNKNOW",
45         "APP2EXT_ERROR_INVALID_ARGUMENTS",
46         "APP2EXT_ERROR_MOVE",
47         "APP2EXT_ERROR_PRE_UNINSTALL",
48         "APP2EXT_ERROR_MMC_STATUS",
49         "APP2EXT_ERROR_DB_INITIALIZE",
50         "APP2EXT_ERROR_SQLITE_REGISTRY",
51         "APP2EXT_ERROR_PASSWD_GENERATION",
52         "APP2EXT_ERROR_MMC_INFORMATION",
53         "APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY",
54         "APP2EXT_ERROR_DELETE_DIRECTORY",
55         "APP2EXT_ERROR_CREATE_SYMLINK",
56         "APP2EXT_ERROR_CREATE_DIRECTORY",
57         "APP2EXT_ERROR_DELETE_LINK_FILE",
58         "APP2EXT_ERROR_PKG_EXISTS",
59         "APP2EXT_ERROR_ACCESS_FILE",
60         "APP2EXT_ERROR_OPEN_DIR",
61         "APP2EXT_ERROR_ALREADY_FILE_PRESENT",
62         "APP2EXT_ERROR_FILE_ABSENT",
63         "APP2EXT_ERROR_STRCMP_FAILED",
64         "APP2EXT_ERROR_INVALID_PACKAGE",
65         "APP2EXT_ERROR_CREATE_DIR_ENTRY",
66         "APP2EXT_ERROR_PASSWORD_GENERATION",
67         "APP2EXT_ERROR_COPY_DIRECTORY",
68         "APP2EXT_ERROR_INVALID_CASE",
69         "APP2EXT_ERROR_SYMLINK_ALREADY_EXISTS",
70         "APP2EXT_ERROR_APPEND_HASH_TO_FILE",
71         "APP2EXT_ERROR_CREATE_DEVICE",
72         "APP2EXT_ERROR_DO_LOSETUP",
73         "APP2EXT_ERROR_CREATE_FS",
74         "APP2EXT_ERROR_MOUNT_PATH",
75         "APP2EXT_ERROR_CLEANUP",
76         "APP2EXT_ERROR_MOUNT",
77         "APP2EXT_ERROR_REMOUNT",
78         "APP2EXT_ERROR_PIPE_CREATION",
79         "APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE",
80         "APP2EXT_ERROR_VCONF_REGISTRY",
81         "APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE",
82         "APP2EXT_ERROR_UNMOUNT",
83         "APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE",
84         "APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE",
85         "APP2EXT_ERROR_ALREADY_MOUNTED",
86         "APP2EXT_ERROR_PLUGIN_INIT_FAILED",
87         "APP2EXT_ERROR_PLUGIN_DEINIT_FAILED"
88 };
89
90 static int __get_integer_input_data(void);
91
92 static int __get_integer_input_data(void)
93 {
94         char input_str[32] = { 0, };
95         int data = 0;
96
97         if (fgets(input_str, 32, stdin) == NULL) {
98                 printf("Input buffer overflow....\n");
99                 return -1;
100         }
101
102         if (sscanf(input_str, "%4d", &data) != 1) {
103                 printf("Input only integer option....\n");
104                 return -1;
105         }
106
107         return data;
108 }
109
110 static void usage(void)
111 {
112         printf("\n*********************************************\n");
113         printf("app2sd test\n");
114         printf("test_case\n");
115         printf("<1> app_install (pre-install, install, post-install, enable, launch, disable)\n");
116         printf("<2> app_uninstall (pre-uninstall, uninstall, post-uninstall)\n");
117         printf("<3> app_upgrade (pre-upgrade, upgrade, post-Upgrade)\n");
118         printf("<4> app_move\n");
119         printf("<5> app_get_location\n");
120         printf("<6> enable_external_dir\n");
121         printf("<7> disable_external_dir\n");
122         printf("<8> exit\n");
123 }
124
125 GList * populate_dir_details()
126 {
127         GList *dir_list = NULL;
128         GList *list = NULL;
129         app2ext_dir_details* dir_detail = NULL;
130         int i;
131
132
133         for (i=0; i<3; i++) {
134                 dir_detail = (app2ext_dir_details*) calloc(1, sizeof(app2ext_dir_details));
135                 if (dir_detail == NULL) {
136                         printf("\nMemory allocation failed\n");
137                         goto FINISH_OFF;
138                 }
139                 dir_detail->name = (char*) calloc(1, sizeof(char)*(strlen(pkg_ro_content_rpm[i])+2));
140                 if (dir_detail->name == NULL) {
141                         printf("\nMemory allocation failed\n");
142                         free(dir_detail);
143                         goto FINISH_OFF;
144                 }
145                 snprintf(dir_detail->name, (strlen(pkg_ro_content_rpm[i])+1), "%s", pkg_ro_content_rpm[i]);
146                 dir_detail->type = APP2EXT_DIR_RO;
147                 dir_list = g_list_append(dir_list, dir_detail);
148         }
149         if (dir_list) {
150                 list = g_list_first(dir_list);
151                 while (list) {
152                         dir_detail = (app2ext_dir_details *)list->data;
153                         list = g_list_next(list);
154                 }
155         }
156         return dir_list;
157 FINISH_OFF:
158         if (dir_list) {
159                 list = g_list_first(dir_list);
160                 while (list) {
161                         dir_detail = (app2ext_dir_details *)list->data;
162                         if (dir_detail && dir_detail->name) {
163                                 free(dir_detail->name);
164                         }
165                         list = g_list_next(list);
166                 }
167                 g_list_free(dir_list);
168         }
169         return NULL;
170 }
171
172 void clear_dir_list(GList* dir_list)
173 {
174         GList *list = NULL;
175         app2ext_dir_details* dir_detail = NULL;
176         if (dir_list) {
177                 list = g_list_first(dir_list);
178                 while (list) {
179                         dir_detail = (app2ext_dir_details *)list->data;
180                         if (dir_detail && dir_detail->name) {
181                                 free(dir_detail->name);
182                         }
183                         list = g_list_next(list);
184                 }
185                 g_list_free(dir_list);
186         }
187 }
188
189 int app_install()
190 {
191         printf("app_install %s\n", TEST_PKGNAME);
192         GList *dir_list = NULL;
193         int ret = -1;
194
195         //char cmd_install[CMD_LEN+1];
196         //snprintf(cmd_install, CMD_LEN,"tpk-backend -y %s", TEST_PKGNAME);
197
198         dir_list = populate_dir_details();
199         if (dir_list == NULL) {
200                 printf("Error in populating the directory list\n");
201                 return -1;
202         }
203         ret = handle->interface.pre_install(TEST_PKGNAME, dir_list, 20);
204         if (ret) {
205                 printf("pre_install failed(%s)\n", error_list[ret]);
206                 clear_dir_list(dir_list);
207                 return -1;
208         }
209
210         /*
211         printf("\n cmd_install is %s ", cmd_install);
212         ret = system(cmd_install);
213         if (ret) {
214
215                 printf("tpk-backend  install command  fail %d ", ret);
216                 ret = handle->interface.post_install(TEST_PKGNAME, 1);
217                 if (ret) {
218                         printf("post_install failed(%s)\n", error_list[ret]);
219                 }
220                 clear_dir_list(dir_list);
221                 return -1;
222         }
223         */
224
225         ret = handle->interface.post_install(TEST_PKGNAME, 2);
226         if (ret) {
227                 printf("post_install failed(%s)\n", error_list[ret]);
228                 clear_dir_list(dir_list);
229                 return -1;
230         }
231
232         ret = handle->interface.enable(TEST_PKGNAME);
233         if (ret) {
234                 printf("enable failed(%s)\n", error_list[ret]);
235                 clear_dir_list(dir_list);
236                 return -1;
237         }
238
239         /*
240         printf("\nLaunching application after install");
241         ret = aul_open_app(TEST_PKGNAME);
242
243         if (ret < 0)
244                 printf("\n launch fail");
245         else
246                 printf("\n application launched");
247
248         sleep(5);
249
250         ret = system("killall -9 basicuiapplication");
251         if (ret < 0)
252                 printf("\n app exit fail");
253         else
254                 printf("\n application exited");
255
256         sleep(5);
257         */
258
259         ret = handle->interface.disable(TEST_PKGNAME);
260         if (ret < 0 || ret > 44) {
261                 printf("disable failed : unknown error\n");
262         } else {
263                 printf("disable return(%s)\n", error_list[ret]);
264         }
265
266         clear_dir_list(dir_list);
267
268         return ret;
269 }
270
271 int app_uninstall()
272 {
273         printf("app_uninstall  %s\n", TEST_PKGNAME);
274         int ret = -1;
275         //char cmd_uninstall[CMD_LEN+1];
276         //snprintf(cmd_uninstall, CMD_LEN, "tpk-backend -y %s", TEST_PKGNAME);
277
278         ret = handle->interface.pre_uninstall(TEST_PKGNAME);
279         if (ret) {
280                 printf("pre_uninstall failed(%s)", error_list[ret]);
281                 return -1;
282         }
283
284         /*
285         printf("\n cmd_uninstall is %s ", cmd_uninstall);
286         ret = system(cmd_uninstall);
287         if (ret) {
288                 printf("\nrpm  uninstall command  fail Reason %s", error_list[ret]);
289                 return -1;
290         }
291         */
292
293         ret = handle->interface.post_uninstall(TEST_PKGNAME);
294         if (ret) {
295                 printf("post app uninstall API fail Reason %s\n", error_list[ret]);
296                 return -1;
297         }
298
299         return ret;
300 }
301
302 int app_upgrade()
303 {
304         printf("app_upgrade  %s\n", TEST_PKGNAME);
305         int ret = -1;
306         //char cmd_uninstall[CMD_LEN+1];
307         //snprintf(cmd_uninstall, CMD_LEN, "rpm -U %s", TEST_PKGNAME);
308
309         GList *dir_list = populate_dir_details();
310         if (dir_list == NULL) {
311                 printf("Error in populating the directory list\n");
312                 return -1;
313         }
314
315         ret = handle->interface.pre_upgrade(TEST_PKGNAME, dir_list, 40);
316         if (ret) {
317                 printf("pre app upgrade API fail. Reason %s\n", error_list[ret]);
318                 clear_dir_list(dir_list);
319                 return -1;
320         }
321
322         /*
323         printf("\n cmd_uninstall is %s ", cmd_uninstall);
324         ret = system(cmd_uninstall);
325         if (ret) {
326                 printf("\nrpm  upgrade command  fail Reason %s", error_list[ret]);
327                 ret = handle->interface.post_upgrade(TEST_PKGNAME_RPM, 1);
328                 if (ret) {
329                         printf("post app upgrade API fail Reason %s\n", error_list[ret]);
330                 }
331                 clear_dir_list(dir_list);
332                 return -1;
333         }
334         */
335
336         ret = handle->interface.post_upgrade(TEST_PKGNAME, 2);
337         if (ret) {
338                 printf("\n TC : post app upgrade API fail Reason %s", error_list[ret]);
339                 clear_dir_list(dir_list);
340                 return -1;
341         }
342         clear_dir_list(dir_list);
343         return ret;
344 }
345
346 int app_move()
347 {
348         printf("app_move  %s\n", TEST_PKGNAME);
349         int ret = -1;
350         int ret_check = -1;
351         GList *dir_list = populate_dir_details();
352         if (dir_list == NULL) {
353                 printf("\nError in populating the directory list\n");
354                 return -1;
355         }
356
357         ret = app2ext_get_app_location(TEST_PKGNAME);
358         printf("return value = (%d)", ret);
359         if (ret == APP2EXT_SD_CARD) {
360                 printf("\n app %s is in sd card ", TEST_PKGNAME);
361                 printf("\n app  %s  will be moved to internal memory ",
362                        TEST_PKGNAME);
363                 ret = handle->interface.move(TEST_PKGNAME, dir_list, APP2EXT_MOVE_TO_PHONE);
364                 if (ret) {
365                         printf("\n  TC: move API failed Reason %s", error_list[ret]);
366                         clear_dir_list(dir_list);
367                         return -1;
368                 }
369                 ret = app2ext_get_app_location(TEST_PKGNAME);
370                 if (ret_check == APP2EXT_INTERNAL_MEM)
371                         printf("\n app %s is moved to internal memory ",
372                                TEST_PKGNAME);
373         } else if (ret == APP2EXT_INTERNAL_MEM) {
374                 printf("\n app %s  is  in internal memory ", TEST_PKGNAME);
375                 printf("\n app %s will be moved to sd card", TEST_PKGNAME);
376
377                 ret = handle->interface.move(TEST_PKGNAME, dir_list, APP2EXT_MOVE_TO_EXT);
378                 if (ret) {
379                         printf("\n  TC: move API failed Reason %s", error_list[ret]);
380                         clear_dir_list(dir_list);
381                         return -1;
382                 }
383                 ret = app2ext_get_app_location(TEST_PKGNAME);
384                 if (ret_check == APP2EXT_SD_CARD)
385                         printf("\n app %s is moved to sd card ",
386                                TEST_PKGNAME);
387         }  else {
388                 ret = APP2EXT_ERROR_INVALID_PACKAGE;
389                 printf("\n errorReason %s", error_list[ret]);
390                 clear_dir_list(dir_list);
391                 return ret;
392         }
393         clear_dir_list(dir_list);
394         return ret;
395 }
396
397 void app_get_location()
398 {
399         printf("app_get_location  %s \n", TEST_PKGNAME);
400         int ret = -1;
401
402         ret = app2ext_get_app_location(TEST_PKGNAME);
403         if (ret == APP2EXT_SD_CARD) {
404                 printf("\n app %s is in sd card ", TEST_PKGNAME);
405         } else if (ret == APP2EXT_INTERNAL_MEM) {
406                 printf("\n app %s  is  in internal memory ", TEST_PKGNAME);
407         } else {
408                 printf("\napp %s is not installed", TEST_PKGNAME);
409         }
410 }
411
412 void enable_external_dir()
413 {
414         printf("enable_external_dir\n");
415         int ret = -1;
416
417         ret = app2ext_enable_external_dir();
418         if (ret == 0) {
419                 printf("\n app2ext_enable_external_dir() success");
420         } else {
421                 printf("\n app2ext_enable_external_dir() failed");
422         }
423 }
424
425 void disable_external_dir()
426 {
427         printf("disable_external_dir\n");
428         int ret = -1;
429
430         ret = app2ext_disable_external_dir();
431         if (ret == 0) {
432                 printf("\n app2ext_disable_external_dir() success");
433         } else {
434                 printf("\n app2ext_disable_external_dir() failed");
435         }
436 }
437
438 int main(int argc, char **argv)
439 {
440         int ret = 0;
441
442         /* check authorized user */
443
444         handle = app2ext_init(APP2EXT_SD_CARD);
445         if (handle == NULL) {
446                 ret = APP2EXT_ERROR_PLUGIN_INIT_FAILED;
447                 printf("app2ext_init failed (%s)\n", error_list[ret]);
448                 return -1;
449         }
450
451         do {
452                 usage();
453                 printf("enter testcase\n");
454                 int option = __get_integer_input_data();
455                 switch (option) {
456                 case 1:
457                         app_install();
458                         break;
459                 case 2:
460                         app_uninstall();
461                         break;
462                 case 3:
463                         app_upgrade();
464                         break;
465                 case 4:
466                         app_move();
467                         break;
468                 case 5:
469                         app_get_location();
470                         break;
471                 case 6:
472                         enable_external_dir();
473                         break;
474                 case 7:
475                         disable_external_dir();
476                         break;
477                 case 8:
478                         app2ext_deinit(handle);
479                         printf("Exit!\n");
480                         return 0;
481                 default:
482                         printf("\nInvalid test id\n");
483                         break;
484                 }
485         } while(1);
486
487         app2ext_deinit(handle);
488
489         return 0;
490 }