Improve unit tests coverage
[platform/core/appfw/shortcut.git] / lib / src / shortcut_db.c
index 65cd203..ea1cc3c 100644 (file)
 
 static bool is_db_corrupted = false;
 
+static const char* _db_path = DB_PATH;
+
+int shortcut_set_db_path(const char *db_path)
+{
+       _db_path = db_path;
+       return 0;
+}
+
 /* LCOV_EXCL_START */
 static int __check_integrity_cb(void *pid, int argc, char **argv, char **notUsed)
 {
@@ -72,14 +80,14 @@ static int __recover_corrupted_db(sqlite3 *db)
        SHORTCUT_INFO("DB is corrupted, start to recover corrupted db");
        if (db)
                sqlite3_close(db);
-       unlink(DB_PATH);
+       unlink(_db_path);
 
-       sql_ret = sqlite3_open_v2(DB_PATH, &db,
+       sql_ret = sqlite3_open_v2(_db_path, &db,
                                SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                                NULL);
        if (sql_ret != SQLITE_OK) {
                SHORTCUT_ERR("Failed to open db[%d]", sql_ret);
-               unlink(DB_PATH);
+               unlink(_db_path);
                ret = SHORTCUT_ERROR_IO_ERROR;
                goto out;
        }
@@ -106,7 +114,7 @@ EAPI int shortcut_db_init(void)
        sqlite3 *db = NULL;
        char *errmsg = NULL;
 
-       sql_ret = sqlite3_open_v2(DB_PATH, &db,
+       sql_ret = sqlite3_open_v2(_db_path, &db,
                        SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
        if (sql_ret != SQLITE_OK) {
                SHORTCUT_ERR("Failed to open db[%d]", sql_ret);
@@ -144,7 +152,7 @@ out:
 static sqlite3 *_open_db(void)
 {
        int ret;
-       const char *dbfile = DB_PATH;
+       const char *dbfile = _db_path;
        sqlite3 *db = NULL;
 
        ret = db_util_open(dbfile, &db, 0);
@@ -239,8 +247,10 @@ static inline int _get_i18n_name(sqlite3 *handle, const char *lang, int id, char
                        if (!*icon) {
                                SHORTCUT_ERR("strdup: %d\n", errno);
                                ret = -ENOMEM;
-                               if (name && *name)
+                               if (name && *name) {
                                        free(*name);
+                                       *name = NULL;
+                               }
 
                                goto out;
                        }
@@ -413,11 +423,15 @@ EAPI int shortcut_db_get_list(const char *package_name, GList **shortcut_list)
                shortcut->extra_data = strdup((char *)extra_data);
                *shortcut_list = g_list_append(*shortcut_list, shortcut);
 
-               free(i18n_name);
-               i18n_name = NULL;
+               if (i18n_name) {
+                       free(i18n_name);
+                       i18n_name = NULL;
+               }
 
-               free(i18n_icon);
-               i18n_icon = NULL;
+               if (i18n_icon) {
+                       free(i18n_icon);
+                       i18n_icon = NULL;
+               }
                /* LCOV_EXCL_STOP */
        }