pkgmgr_common::DBOperationType GetOpType();
protected:
- bool Connect();
+ virtual bool Connect();
int GetPID();
uid_t GetUID();
- std::vector<std::pair<sqlite3*, uid_t>> GetConnection();
+ virtual std::vector<std::pair<sqlite3*, uid_t>> GetConnection();
void ClearDBHandle();
std::string GetLocale();
static std::shared_timed_mutex lock_;
#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
#endif
+#define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt"
+
namespace pkgmgr_server {
namespace database {
return PMINFO_R_OK;
}
+int CreateDBHandler::SetDBVersion(sqlite3* conn) {
+ static const char query_raw[] = "PRAGMA user_version=%Q";
+ int ret;
+ FILE *fp = NULL;
+ char version[PKG_STRING_LEN_MAX] = { 0 };
+ char *query = NULL;
+
+ fp = fopen(DB_VERSION_PATH, "r");
+ if (fp == NULL) {
+ printf("Failed to open db version file %s\n", DB_VERSION_PATH);
+ printf("errno : %d\n", errno);
+ LOGE("Failed to open db version file");
+ return -1;
+ }
+
+ if (fgets(version, sizeof(version), fp) == NULL) {
+ LOGE("Failed to get version information");
+ fclose(fp);
+ return -1;
+ }
+ fclose(fp);
+
+ query = sqlite3_mprintf(query_raw, version);
+ if (!query) {
+ LOGE("Out of memory");
+ return -1;
+ }
+
+ ret = sqlite3_exec(conn, query, NULL, NULL, NULL);
+ if (ret != SQLITE_OK) {
+ LOGE("exec failed: %s", sqlite3_errmsg(conn));
+ sqlite3_free(query);
+ return -1;
+ }
+ sqlite3_free(query);
+
+ return 0;
+}
+
int CreateDBHandler::CreateParserDB() {
SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
ClearDBHandle();
sqlite3* conn = conn_list.front().first;
uid_t uid = conn_list.front().second;
+ if (SetDBVersion(conn) < 0)
+ return PMINFO_R_ERROR;
+
return pkgmgr_parser_internal_initialize_db(conn, uid);
}
sqlite3* conn = conn_list.front().first;
uid_t uid = conn_list.front().second;
+ if (SetDBVersion(conn) < 0)
+ return PMINFO_R_ERROR;
+
return pkgmgr_parser_internal_initialize_db(conn, uid);
}
std::vector<std::vector<std::string>> GetResult();
int CreateParserDB();
int CreateCertDB();
+ virtual int SetDBVersion(sqlite3* conn);
private:
std::vector<std::vector<std::string>> result_;
_LOGD("chsmack -a %s %s", DB_LABEL, x); \
} while (0)
-#define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt"
-
static const char *__get_cert_db_path(void)
{
return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
return 0;
}
-static int __set_db_version(sqlite3 *db) {
- static const char query_raw[] = "PRAGMA user_version=%Q";
- int ret;
- FILE *fp = NULL;
- char version[PKG_STRING_LEN_MAX] = { 0 };
- char *query = NULL;
-
- fp = fopen(DB_VERSION_PATH, "r");
- if (fp == NULL) {
- _LOGE("Failed to open db version file");
- return -1;
- }
-
- if (fgets(version, sizeof(version), fp) == NULL) {
- _LOGE("Failed to get version information");
- fclose(fp);
- return -1;
- }
- fclose(fp);
-
- query = sqlite3_mprintf(query_raw, version);
- if (!query) {
- _LOGE("Out of memory");
- return -1;
- }
-
- ret = sqlite3_exec(db, query, NULL, NULL, NULL);
- if (ret != SQLITE_OK) {
- _LOGE("exec failed: %s", sqlite3_errmsg(db));
- sqlite3_free(query);
- return -1;
- }
- sqlite3_free(query);
-
- return 0;
-}
-
static int __create_tables(sqlite3 *db, const char **queries)
{
int ret;
{
const char **queries;
- if (__set_db_version(db))
- return -1;
-
if (strstr(dbpath, ".pkgmgr_parser.db")) {
queries = PARSER_INIT_QUERIES;
} else if (strstr(dbpath, ".pkgmgr_cert.db")) {
TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} ${libpkgs_LDFLAGS})
TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} ${libpkgmgr-parser_LDFLAGS})
TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PUBLIC pkgmgr-info)
+TARGET_LINK_LIBRARIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PUBLIC pkgmgr-info-server)
SET_TARGET_PROPERTIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE -fpic")
SET_TARGET_PROPERTIES(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie")
set_target_properties(${TARGET_PKGMGR_INFO_UNIT_TEST} PROPERTIES COMPILE_FLAGS "${unit_test_pkgs_CFLAGS_str}")
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gmock/gmock.h>
+
+#include <gtest/gtest.h>
+
+#include <cstdio>
+
+#include "db_type.hh"
+#include "create_db_handler.hh"
+#include "pkgmgrinfo_basic.h"
+
+#include "parcel_utils.hh"
+
+#define TEST_PARSER_DB "test.pkgmgr_parser.db"
+
+class CreateDBHandlerMock : public pkgmgr_server::database::CreateDBHandler {
+ public:
+ CreateDBHandlerMock(uid_t uid, int pid)
+ : pkgmgr_server::database::CreateDBHandler(uid, pid) {}
+
+ MOCK_METHOD0(Connect, bool());
+ MOCK_METHOD0(GetConnection, std::vector<std::pair<sqlite3*, uid_t>>());
+ MOCK_METHOD1(SetDBVersion, int(sqlite3 *conn));
+};
+
+class DBHandlerTest : public ::testing::Test {
+ public:
+ virtual void SetUp() {
+ sqlite3 *db;
+ ASSERT_EQ(sqlite3_open_v2(TEST_PARSER_DB, &db,
+ SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), SQLITE_OK);
+
+ SetDBHandles(
+ std::vector<std::pair<sqlite3*, uid_t>> { std::make_pair(db, 0) });
+ CreateDBHandlerMock handler(0, 0);
+
+ EXPECT_CALL(handler, Connect())
+ .Times(2).WillRepeatedly(testing::Return(true));
+ EXPECT_CALL(handler, GetConnection())
+ .Times(2).WillRepeatedly(testing::Return(GetDBHandles()));
+ EXPECT_CALL(handler, SetDBVersion(testing::_))
+ .Times(2).WillRepeatedly(testing::Return(0));
+ ASSERT_EQ(handler.Execute(), 0);
+ }
+
+ virtual void TearDown() {
+ for (auto& handle : db_handles_)
+ sqlite3_close_v2(handle.first);
+
+ ASSERT_EQ(remove(TEST_PARSER_DB), 0);
+ }
+
+ const std::vector<std::pair<sqlite3*, uid_t>>& GetDBHandles() {
+ return db_handles_;
+ }
+
+ private:
+ void SetDBHandles(std::vector<std::pair<sqlite3*, uid_t>>&& db_handles) {
+ db_handles_ = db_handles;
+ }
+
+ std::vector<std::pair<sqlite3*, uid_t>> db_handles_;
+};
+
+TEST_F(DBHandlerTest, EmptyTest) {
+}