4 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include <sys/types.h>
32 #include <storage-internal.h>
33 #include <tzplatform_config.h>
35 #include "app2ext_interface.h"
40 #define TEST_PKGNAME "org.example.basicuiapplication"
41 #define TEST_PKGNAME_PATH "/tmp/org.example.basicuiapplication-1.0.0-arm.tpk"
43 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
45 app2ext_handle *handle = NULL;
47 char pkg_ro_content_rpm[3][5] = { "bin", "res", "lib" };
49 #define COUNT_OF_ERROR_LIST APP2EXT_ERROR_ENUM_MAX
50 char error_list[COUNT_OF_ERROR_LIST][100] = {
52 "APP2EXT_ERROR_UNKNOWN",
53 "APP2EXT_ERROR_INVALID_ARGUMENTS",
55 "APP2EXT_ERROR_PRE_UNINSTALL",
56 "APP2EXT_ERROR_MMC_STATUS",
57 "APP2EXT_ERROR_DB_INITIALIZE",
58 "APP2EXT_ERROR_SQLITE_REGISTRY",
59 "APP2EXT_ERROR_PASSWD_GENERATION",
60 "APP2EXT_ERROR_MMC_INFORMATION",
61 "APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY",
62 "APP2EXT_ERROR_DELETE_DIRECTORY",
63 "APP2EXT_ERROR_CREATE_SYMLINK",
64 "APP2EXT_ERROR_CREATE_DIRECTORY",
65 "APP2EXT_ERROR_DELETE_LINK_FILE",
66 "APP2EXT_ERROR_PKG_EXISTS",
67 "APP2EXT_ERROR_ACCESS_FILE",
68 "APP2EXT_ERROR_OPEN_DIR",
69 "APP2EXT_ERROR_ALREADY_FILE_PRESENT",
70 "APP2EXT_ERROR_FILE_ABSENT",
71 "APP2EXT_ERROR_STRCMP_FAILED",
72 "APP2EXT_ERROR_INVALID_PACKAGE",
73 "APP2EXT_ERROR_CREATE_DIR_ENTRY",
74 "APP2EXT_ERROR_COPY_DIRECTORY",
75 "APP2EXT_ERROR_INVALID_CASE",
76 "APP2EXT_ERROR_SYMLINK_ALREADY_EXISTS",
77 "APP2EXT_ERROR_APPEND_HASH_TO_FILE",
78 "APP2EXT_ERROR_CREATE_DEVICE",
79 "APP2EXT_ERROR_DO_LOSETUP",
80 "APP2EXT_ERROR_CREATE_FS",
81 "APP2EXT_ERROR_MOUNT_PATH",
82 "APP2EXT_ERROR_CLEANUP",
83 "APP2EXT_ERROR_MOUNT",
84 "APP2EXT_ERROR_REMOUNT",
85 "APP2EXT_ERROR_PIPE_CREATION",
86 "APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE",
87 "APP2EXT_ERROR_VCONF_REGISTRY",
88 "APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE",
89 "APP2EXT_ERROR_UNMOUNT",
90 "APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE",
91 "APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE",
92 "APP2EXT_ERROR_ALREADY_MOUNTED",
93 "APP2EXT_ERROR_PLUGIN_INIT_FAILED",
94 "APP2EXT_ERROR_PLUGIN_DEINIT_FAILED",
95 "APP2EXT_ERROR_DBUS_FAILED",
96 "APP2EXT_ERROR_MEMORY_ALLOC_FAILED",
97 "APP2EXT_ERROR_OPERATION_NOT_PERMITTED",
98 "APP2EXT_ERROR_SAME_LOOPBACK_DEVICE_EXISTS",
99 "APP2EXT_ERROR_PKGMGR_ERROR",
100 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
101 "APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE",
102 "APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE",
103 "APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE",
104 "APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE",
105 "APP2EXT_ERROR_DMCRYPT_DEVICE_UNAVAILABLE",
109 static void usage(void)
112 printf("*************************************************\n");
113 printf("app2sd test usage:\n");
114 printf("pre-condition: /tmp/org.example.basicuiapplication-1.0.0-arm.tpk\n");
116 printf("<INSTALL>\n");
117 printf("1.(at target_user)$test_app2ext --pre-install\n");
118 printf("2.(at target_user)$pkgcmd -it tpk {pkg-path}\n");
119 printf("3.(at target_user)$test_app2ext --post-install\n");
120 printf("------------------------------------------------\n");
122 printf("<UPGRADE>\n");
123 printf("1.(at target_user)$test_app2ext --pre-upgrade\n");
124 printf("2.(at target_user)$pkgcmd -it tpk {pkg-path}\n");
125 printf("3.(at target_user)$test_app2ext --post-upgrade\n");
126 printf("------------------------------------------------\n");
128 printf("<INSTALL>\n");
129 printf("1.(at target_user)$test_app2ext --pre-uninstall\n");
130 printf("2.(at target_user)$pkgcmd -un {pkg-id}\n");
131 printf("3.(at target_user)$test_app2ext --post-uninstall\n");
132 printf("------------------------------------------------\n");
134 printf("<MOVE PKG TEST>\n");
135 printf("(at target_user)$test_app2ext --move-to-ext\n");
136 printf("(at target_user)$test_app2ext --move-to-int\n");
137 printf("------------------------------------------------\n");
139 printf("<ENABLE(mount)/DISABLE(umount) TEST W/ Installed PKG>\n");
140 printf("(at target_user)$test_app2ext --enable\n");
141 printf("(at target_user)$test_app2ext --disable\n");
142 printf("(at target_user)$test_app2ext --enable-full\n");
143 printf("(at target_user)$test_app2ext --disable-full\n");
144 printf("------------------------------------------------\n");
145 printf("**************************************************\n");
149 #define OPTVAL_PRE_INSTALL 1000
150 #define OPTVAL_POST_INSTALL 1001
151 #define OPTVAL_PRE_UNINSTALL 1002
152 #define OPTVAL_POST_UNINSTALL 1003
153 #define OPTVAL_PRE_UPGRADE 1004
154 #define OPTVAL_POST_UPGRADE 1005
155 #define OPTVAL_MOVE_TO_EXTERNAL 1006
156 #define OPTVAL_MOVE_TO_INTERNAL 1007
157 #define OPTVAL_ENABLE_APP 1008
158 #define OPTVAL_DISABLE_APP 1009
159 #define OPTVAL_ENABLE_FULL 1010
160 #define OPTVAL_DISABLE_FULL 1011
161 #define OPTVAL_USAGE 1012
163 /* Supported options */
164 const struct option long_opts[] = {
165 { "pre-install", 0, NULL, OPTVAL_PRE_INSTALL },
166 { "post-install", 0, NULL, OPTVAL_POST_INSTALL },
167 { "pre-uninstall", 0, NULL, OPTVAL_PRE_UNINSTALL },
168 { "post-uninstall", 0, NULL, OPTVAL_POST_UNINSTALL },
169 { "pre-upgrade", 0, NULL, OPTVAL_PRE_UPGRADE },
170 { "post-upgrade", 0, NULL, OPTVAL_POST_UPGRADE },
171 { "move-to-ext", 0, NULL, OPTVAL_MOVE_TO_EXTERNAL },
172 { "move-to-int", 0, NULL, OPTVAL_MOVE_TO_INTERNAL },
173 { "enable", 0, NULL, OPTVAL_ENABLE_APP },
174 { "disable", 0, NULL, OPTVAL_DISABLE_APP },
175 { "enable-full", 0, NULL, OPTVAL_ENABLE_FULL },
176 { "disable-full", 0, NULL, OPTVAL_DISABLE_FULL },
177 { "help", 0, NULL, OPTVAL_USAGE },
178 { "usage", 0, NULL, OPTVAL_USAGE },
179 { 0, 0, 0, 0 } /* sentinel */
182 void clear_dir_list(GList *dir_list)
185 app2ext_dir_details *dir_detail = NULL;
188 list = g_list_first(dir_list);
190 dir_detail = (app2ext_dir_details *)list->data;
191 if (dir_detail && dir_detail->name)
192 free(dir_detail->name);
194 list = g_list_next(list);
196 g_list_free(dir_list);
200 GList *populate_dir_details()
202 GList *dir_list = NULL;
204 app2ext_dir_details *dir_detail = NULL;
207 for (i = 0; i < 3; i++) {
208 dir_detail = (app2ext_dir_details *)calloc(1, sizeof(app2ext_dir_details));
209 if (dir_detail == NULL) {
210 printf("memory allocation failed\n");
214 dir_detail->name = (char *)calloc(1, sizeof(char) * (strlen(pkg_ro_content_rpm[i]) + 2));
215 if (dir_detail->name == NULL) {
216 printf("memory allocation failed\n");
220 snprintf(dir_detail->name, (strlen(pkg_ro_content_rpm[i]) + 1), "%s", pkg_ro_content_rpm[i]);
221 dir_detail->type = APP2EXT_DIR_RO;
222 dir_list = g_list_append(dir_list, dir_detail);
226 list = g_list_first(dir_list);
228 dir_detail = (app2ext_dir_details *)list->data;
229 list = g_list_next(list);
237 clear_dir_list(dir_list);
242 static int get_unzip_size(const char *item, unsigned long long *size)
246 char *filename = NULL;
247 unz_file_info fileInfo = { 0 };
249 if (!item || !size) {
250 printf("get size : invalid argument\n");
253 uzf = unzOpen64(item);
255 printf("get size : failed to open item : [%s]\n", item);
259 ret = unzGoToFirstFile(uzf);
261 printf("get size : error get first zip file\n");
267 ret = unzOpenCurrentFile(uzf);
269 printf("get size : error unzOpenCurrentFile\n");
275 filename = (char *)calloc(1, 4096);
276 ret = unzGetCurrentFileInfo(uzf, &fileInfo, filename, (4096 - 1), NULL, 0, NULL, 0);
277 *size = (unsigned long long)fileInfo.uncompressed_size + *size;
279 printf("get size : error get current file info\n");
280 unzCloseCurrentFile(uzf);
289 } while (unzGoToNextFile(uzf) == UNZ_OK);
297 static void print_error_code(const char *func_name, int ret)
299 if (ret < 0 || ret > COUNT_OF_ERROR_LIST - 1)
300 printf("%s failed : unknown error(%d)\n", func_name, ret);
302 printf("%s return(%s)\n", func_name, error_list[ret]);
305 static int pre_app_install()
307 GList *dir_list = NULL;
309 unsigned long long size_byte = 0;
312 printf("pre_app_install for [%s]\n", TEST_PKGNAME_PATH);
314 dir_list = populate_dir_details();
315 if (dir_list == NULL) {
316 printf("error in populating the directory list\n");
321 ret = get_unzip_size(TEST_PKGNAME_PATH, &size_byte);
322 if (ret < 0 || size_byte == 0)
323 printf("wrong pkg size, ret(%d), size_byte(%llu)\n", ret, size_byte);
325 size_mega = size_byte / (1024 * 1024) + 1;
326 printf("get pkg size : (%d)MB\n", size_mega);
328 ret = handle->interface.client_pre_install(TEST_PKGNAME,
329 dir_list, size_mega);
330 print_error_code(__func__, ret);
332 clear_dir_list(dir_list);
337 static int post_app_install()
341 ret = handle->interface.client_post_install(TEST_PKGNAME,
342 APP2EXT_STATUS_SUCCESS);
343 print_error_code(__func__, ret);
348 static int app_enable()
352 ret = handle->interface.client_enable(TEST_PKGNAME);
353 print_error_code(__func__, ret);
358 static int app_disable()
362 ret = handle->interface.client_disable(TEST_PKGNAME);
363 print_error_code(__func__, ret);
368 static int fullpkg_enable()
372 ret = handle->interface.client_enable_full_pkg();
373 print_error_code(__func__, ret);
378 static int fullpkg_disable()
382 ret = handle->interface.client_disable_full_pkg();
383 print_error_code(__func__, ret);
388 static int pre_app_uninstall()
392 printf("pre_app_uninstall for [%s]\n", TEST_PKGNAME);
394 ret = handle->interface.client_pre_uninstall(TEST_PKGNAME);
395 print_error_code(__func__, ret);
400 static int post_app_uninstall()
404 ret = handle->interface.client_post_uninstall(TEST_PKGNAME);
405 print_error_code(__func__, ret);
410 static int pre_app_upgrade()
412 GList *dir_list = NULL;
414 unsigned long long size_byte = 0;
417 printf("pre_app_upgrade for [%s]\n", TEST_PKGNAME);
419 dir_list = populate_dir_details();
420 if (dir_list == NULL) {
421 printf("Error in populating the directory list\n");
426 ret = get_unzip_size(TEST_PKGNAME_PATH, &size_byte);
427 if (ret < 0 || size_byte == 0)
428 printf("wrong pkg size, ret(%d), size_byte(%llu)\n", ret, size_byte);
430 size_mega = size_byte / (1024 * 1024) + 1;
431 printf("get pkg size : (%d)MB\n", size_mega);
433 ret = handle->interface.client_pre_upgrade(TEST_PKGNAME, dir_list,
435 print_error_code(__func__, ret);
437 clear_dir_list(dir_list);
442 static int post_app_upgrade()
446 ret = handle->interface.client_post_upgrade(TEST_PKGNAME,
447 APP2EXT_STATUS_SUCCESS);
448 print_error_code(__func__, ret);
453 static int app_move_to_external()
455 GList *dir_list = NULL;
458 printf("app_move_to_external %s\n", TEST_PKGNAME);
460 dir_list = populate_dir_details();
461 if (dir_list == NULL) {
462 printf("Error in populating the directory list\n");
466 printf("pkg %s will be moved to sd card\n", TEST_PKGNAME);
467 ret = handle->interface.client_pre_move(TEST_PKGNAME,
468 dir_list, APP2EXT_MOVE_TO_EXT);
469 print_error_code(__func__, ret);
470 ret = handle->interface.client_post_move(TEST_PKGNAME,
471 APP2EXT_MOVE_TO_EXT);
472 print_error_code(__func__, ret);
474 clear_dir_list(dir_list);
479 static int app_move_to_internal()
481 GList *dir_list = NULL;
484 printf("app_move_to_internal %s\n", TEST_PKGNAME);
486 dir_list = populate_dir_details();
487 if (dir_list == NULL) {
488 printf("Error in populating the directory list\n");
492 printf("pkg %s will be moved to internal memory\n", TEST_PKGNAME);
493 ret = handle->interface.client_pre_move(TEST_PKGNAME,
494 dir_list, APP2EXT_MOVE_TO_PHONE);
495 print_error_code(__func__, ret);
496 ret = handle->interface.client_post_move(TEST_PKGNAME,
497 APP2EXT_MOVE_TO_PHONE);
498 print_error_code(__func__, ret);
500 clear_dir_list(dir_list);
505 int main(int argc, char **argv)
511 char *sd_mount_path = NULL;
512 uid_t uid = getuid();
515 if (uid == GLOBAL_USER) {
516 printf("test for global app\n");
517 } else if (uid == OWNER_ROOT) {
518 printf("for root user, a test isn't supproted yet\n");
521 printf("test for user(%d) app\n", uid);
524 /* check sdcard info */
525 ret = storage_get_primary_sdcard(&storage_id, &sd_mount_path);
526 if (ret != STORAGE_ERROR_NONE) {
527 printf("failed to get primary sdcard (%d)\n", ret);
533 printf("primary sdcard: id(%d), mount_path(%s)\n",
534 storage_id, sd_mount_path);
537 printf("there is no primary sdcard\n");
540 handle = app2ext_init(APP2EXT_SD_CARD);
541 if (handle == NULL) {
542 ret = APP2EXT_ERROR_PLUGIN_INIT_FAILED;
543 printf("app2ext_init failed (%s)\n", error_list[ret]);
548 optind = 1; /* Initialize optind to clear prev. index */
550 c = getopt_long(argc, argv, "", long_opts, &opt_idx);
553 break; /* Parse is end */
556 case OPTVAL_PRE_INSTALL:
559 case OPTVAL_POST_INSTALL:
562 case OPTVAL_PRE_UNINSTALL:
565 case OPTVAL_POST_UNINSTALL:
566 post_app_uninstall();
568 case OPTVAL_PRE_UPGRADE:
571 case OPTVAL_POST_UPGRADE:
574 case OPTVAL_MOVE_TO_EXTERNAL:
575 app_move_to_external();
577 case OPTVAL_MOVE_TO_INTERNAL:
578 app_move_to_internal();
580 case OPTVAL_ENABLE_APP:
583 case OPTVAL_DISABLE_APP:
586 case OPTVAL_ENABLE_FULL:
589 case OPTVAL_DISABLE_FULL:
601 app2ext_deinit(handle);