From fb9e623e6f98fa10304c5eb7c8dcaf4fa05ba4ab Mon Sep 17 00:00:00 2001 From: Youngjae Shin Date: Thu, 30 Jul 2020 14:00:44 +0900 Subject: increase code quality cleanup, revise coding style, fix build warnings and so on Change-Id: I4aa38f8b818f43c62643d5a1bf6aa505a5684bf9 --- tests/db-util-gtest.cpp | 366 +++++++++++++++++++++++++++--------------------- 1 file changed, 210 insertions(+), 156 deletions(-) (limited to 'tests/db-util-gtest.cpp') diff --git a/tests/db-util-gtest.cpp b/tests/db-util-gtest.cpp index 7000986..51abd5d 100644 --- a/tests/db-util-gtest.cpp +++ b/tests/db-util-gtest.cpp @@ -1,10 +1,26 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 +#include #include #include #include #include #include "gtest/gtest.h" - #include #include @@ -12,126 +28,151 @@ #define COL_NAME_LEN 255 #define BUF_LEN 255 +using namespace std; //======================================================================================= class DbUtil : public ::testing::Test { - public: - char pszFilePath[FILE_LEN + 1]; - sqlite3 *db; - int nOption; - int flags; - const char* zVfs; - char col_name[COL_NAME_LEN + 1]; - - DbUtil() { - strncpy(pszFilePath, "test.db", FILE_LEN); - db = NULL; - nOption = 0; - flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; - - zVfs = NULL; - } +protected: + void db_copy() + { + ifstream src("test.db.org", ios::binary);; + ofstream dst("test.db", ios::binary); - void SetUp() { - } + dst << src.rdbuf(); - void TearDown() { - int rc; - rc = system("rm -f test.db"); - rc = system("rm -f test.db-journal"); - } + src.close(); + dst.close(); + } +public: + char pszFilePath[FILE_LEN + 1]; + sqlite3 *db; + int nOption; + int flags; + const char* zVfs; + char col_name[COL_NAME_LEN + 1]; + + DbUtil() + { + strncpy(pszFilePath, "test.db", FILE_LEN); + db = NULL; + nOption = 0; + flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; + + zVfs = NULL; + } + + void SetUp() + { + } + + void TearDown() + { + remove("test.db"); + remove("test.db-journal"); + } }; //======================================================================================= // Testing for util_func.c //======================================================================================= -TEST_F(DbUtil, db_util_open_p) { +TEST_F(DbUtil, db_util_open_p) +{ int rc; rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_open_n1) { +TEST_F(DbUtil, db_util_open_n1) +{ int rc; // Invalid db name rc = db_util_open(".", &db, nOption); - ASSERT_NE ( DB_UTIL_OK , rc ); + ASSERT_NE(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_open_n2) { +TEST_F(DbUtil, db_util_open_n2) +{ int rc; // Invalid db pointer rc = db_util_open(pszFilePath, NULL, nOption); - ASSERT_NE ( DB_UTIL_OK , rc ); + ASSERT_NE(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_close_n) { +TEST_F(DbUtil, db_util_close_n) +{ int rc; // Invalid db close rc = db_util_close(NULL); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_open_with_options_p) { +TEST_F(DbUtil, db_util_open_with_options_p) +{ int rc; rc = db_util_open_with_options(pszFilePath, &db, flags, zVfs); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_open_with_options_n1) { +TEST_F(DbUtil, db_util_open_with_options_n1) +{ int rc; // Invalid db name rc = db_util_open_with_options(".", &db, flags, zVfs); - ASSERT_NE ( DB_UTIL_OK , rc ); + ASSERT_NE(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_open_with_options_n2) { +TEST_F(DbUtil, db_util_open_with_options_n2) +{ int rc; // Invalid db name rc = db_util_open_with_options(pszFilePath, NULL, flags, zVfs); - ASSERT_NE ( DB_UTIL_OK , rc ); + ASSERT_NE(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_open_n4) { +TEST_F(DbUtil, db_util_open_n4) +{ int rc; rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); // In case of (geteuid() != 0) && access(pszFilePath, R_OK) - system("chmod 000 test.db"); + chmod("test.db", 0000); - uid_t uid = getuid(); - setuid(1000); + uid_t uid = geteuid(); + if (seteuid(1000) < 0) + FAIL() << "errno = " << errno << std::endl; rc = db_util_open(pszFilePath, &db, nOption); - setuid(uid); - ASSERT_EQ ( SQLITE_PERM , rc ); + if (seteuid(uid) < 0) + FAIL() << "errno = " << errno << std::endl; + ASSERT_EQ(SQLITE_PERM, rc); } -TEST_F(DbUtil, db_util_busyhandler) { +TEST_F(DbUtil, db_util_busyhandler) +{ int rc; rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); sqlite3_exec(db, "BEGIN EXCLUSIVE TRANSACTION;", NULL, NULL, NULL); sqlite3 *db1; rc = db_util_open(pszFilePath, &db1, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); sqlite3_exec(db1, "BEGIN EXCLUSIVE TRANSACTION;", NULL, NULL, NULL); sqlite3_exec(db1, "END EXCLUSIVE TRANSACTION;", NULL, NULL, NULL); @@ -144,21 +185,21 @@ TEST_F(DbUtil, db_util_busyhandler) { // Testing for collation.c //======================================================================================= -int run_query(sqlite3 *db, char* col_name) { - +int run_query(sqlite3 *db, char* col_name) +{ int rc; sqlite3_stmt *stmt = NULL; - char sql[BUF_LEN+1]; - snprintf( sql, BUF_LEN, "SELECT key, value FROM collation order by value COLLATE %s", col_name); - rc = sqlite3_prepare(db, sql, -1, &stmt, 0); - if(rc == SQLITE_OK ){ + char sql[BUF_LEN + 1]; + snprintf(sql, BUF_LEN, "SELECT key, value FROM collation order by value COLLATE %s", col_name); + rc = sqlite3_prepare(db, sql, -1, &stmt, 0); + if (rc == SQLITE_OK) { rc = sqlite3_step(stmt); - while(rc == SQLITE_ROW){ + while (rc == SQLITE_ROW) { #if 0 printf("key : [%s] : Value [%s]\n", - sqlite3_column_text(stmt,0), - sqlite3_column_text(stmt,1) ); + sqlite3_column_text(stmt, 0), + sqlite3_column_text(stmt, 1)); #endif rc = sqlite3_step(stmt); } @@ -169,238 +210,251 @@ int run_query(sqlite3 *db, char* col_name) { return 0; } -TEST_F(DbUtil, db_util_create_collation_p1) { +TEST_F(DbUtil, db_util_create_collation_p1) +{ int rc; - rc = system("./test-collation-db.sh"); + db_copy(); - rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + rc = db_util_open(pszFilePath, &db, nOption); + ASSERT_EQ(DB_UTIL_OK, rc); // set vconf data - vconf_set_str("db/menu_widget/language", "en_US.UTF-8" ); + vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8"); - strncpy(col_name,"COL_UCA_UTF8", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_UCA, DB_UTIL_COL_UTF8, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_UCA_UTF8", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_UCA, DB_UTIL_COL_UTF8, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = run_query(db, col_name); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_p2) { +TEST_F(DbUtil, db_util_create_collation_p2) +{ int rc; - rc = system("./test-collation-db.sh"); + db_copy(); - rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + rc = db_util_open(pszFilePath, &db, nOption); + ASSERT_EQ(DB_UTIL_OK, rc); // set vconf data - vconf_set_str("db/menu_widget/language", "en_US.UTF-8" ); + vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8"); - strncpy(col_name,"COL_LS_AS_CI_UTF8", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_LS_AS_CI, DB_UTIL_COL_UTF8, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_LS_AS_CI_UTF8", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_LS_AS_CI, DB_UTIL_COL_UTF8, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = run_query(db, col_name); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_p3) { +TEST_F(DbUtil, db_util_create_collation_p3) +{ int rc; - rc = system("./test-collation-db.sh"); + db_copy(); - rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + rc = db_util_open(pszFilePath, &db, nOption); + ASSERT_EQ(DB_UTIL_OK, rc); // set vconf data - vconf_set_str("db/menu_widget/language", "en_US.UTF-8" ); + vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8"); - strncpy(col_name,"COL_LS_AI_CI_UTF8", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_LS_AI_CI, DB_UTIL_COL_UTF8, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_LS_AI_CI_UTF8", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_LS_AI_CI, DB_UTIL_COL_UTF8, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = run_query(db, col_name); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_p4) { +TEST_F(DbUtil, db_util_create_collation_p4) +{ int rc; - rc = system("./test-collation-db.sh"); + db_copy(); - rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + rc = db_util_open(pszFilePath, &db, nOption); + ASSERT_EQ(DB_UTIL_OK, rc); // set vconf data - vconf_set_str("db/menu_widget/language", "en_US.UTF-8" ); + vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8"); - strncpy(col_name,"COL_LS_AI_CI_LC_UTF8", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_LS_AI_CI_LC, DB_UTIL_COL_UTF8, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_LS_AI_CI_LC_UTF8", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_LS_AI_CI_LC, DB_UTIL_COL_UTF8, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = run_query(db, col_name); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_p5) { +TEST_F(DbUtil, db_util_create_collation_p5) +{ int rc; - rc = system("./test-collation-db.sh"); + db_copy(); - rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + rc = db_util_open(pszFilePath, &db, nOption); + ASSERT_EQ(DB_UTIL_OK, rc); // set vconf data - vconf_set_str("db/menu_widget/language", "en_US.UTF-8" ); + vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8"); - strncpy(col_name,"COL_UCA_UTF16", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_UCA, DB_UTIL_COL_UTF16, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_UCA_UTF16", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_UCA, DB_UTIL_COL_UTF16, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = run_query(db, col_name); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_p6) { +TEST_F(DbUtil, db_util_create_collation_p6) +{ int rc; - rc = system("./test-collation-db.sh"); + db_copy(); - rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + rc = db_util_open(pszFilePath, &db, nOption); + ASSERT_EQ(DB_UTIL_OK, rc); // set vconf data - vconf_set_str("db/menu_widget/language", "en_US.UTF-8" ); + vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8"); - strncpy(col_name,"COL_LS_AS_CI_UTF16", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_LS_AS_CI, DB_UTIL_COL_UTF16, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_LS_AS_CI_UTF16", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_LS_AS_CI, DB_UTIL_COL_UTF16, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = run_query(db, col_name); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_p7) { +TEST_F(DbUtil, db_util_create_collation_p7) +{ int rc; - rc = system("./test-collation-db.sh"); + db_copy(); - rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + rc = db_util_open(pszFilePath, &db, nOption); + ASSERT_EQ(DB_UTIL_OK, rc); // set vconf data - vconf_set_str("db/menu_widget/language", "en_US.UTF-8" ); + vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8"); - strncpy(col_name,"COL_LS_AI_CI_UTF16", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_LS_AI_CI, DB_UTIL_COL_UTF16, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_LS_AI_CI_UTF16", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_LS_AI_CI, DB_UTIL_COL_UTF16, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = run_query(db, col_name); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_p8) { +TEST_F(DbUtil, db_util_create_collation_p8) +{ int rc; - rc = system("./test-collation-db.sh"); + db_copy(); - rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + rc = db_util_open(pszFilePath, &db, nOption); + ASSERT_EQ(DB_UTIL_OK, rc); // set vconf data - vconf_set_str("db/menu_widget/language", "en_US.UTF-8" ); + vconf_set_str(VCONFKEY_LANGSET, "en_US.UTF-8"); - strncpy(col_name,"COL_LS_AI_CI_LC_UTF16", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_LS_AI_CI_LC, DB_UTIL_COL_UTF16, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_LS_AI_CI_LC_UTF16", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_LS_AI_CI_LC, DB_UTIL_COL_UTF16, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = run_query(db, col_name); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_n1) { +TEST_F(DbUtil, db_util_create_collation_n1) +{ int rc; rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); // vconf error - vconf_set_str("db/menu_widget/language", "" ); + vconf_set_str(VCONFKEY_LANGSET, ""); - strncpy(col_name,"COL_UCA_UTF8", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_UCA, DB_UTIL_COL_UTF8, col_name ); - ASSERT_EQ ( DB_UTIL_OK , rc ); + strncpy(col_name, "COL_UCA_UTF8", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_UCA, DB_UTIL_COL_UTF8, col_name); + ASSERT_EQ(DB_UTIL_OK, rc); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_n2) { +TEST_F(DbUtil, db_util_create_collation_n2) +{ int rc; rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); // collate_type error - strncpy(col_name,"COL_KO_IC_UTF8", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_KO_IC, DB_UTIL_COL_UTF8, col_name ); - ASSERT_EQ ( DB_UTIL_ERROR , rc ); + strncpy(col_name, "COL_KO_IC_UTF8", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_KO_IC, DB_UTIL_COL_UTF8, col_name); + ASSERT_EQ(DB_UTIL_ERROR, rc); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_n3) { +TEST_F(DbUtil, db_util_create_collation_n3) +{ int rc; rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); // collate_type error - strncpy(col_name,"COL_KO_IC_LC_UTF8", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_KO_IC_LC, DB_UTIL_COL_UTF8, col_name ); - ASSERT_EQ ( DB_UTIL_ERROR , rc ); + strncpy(col_name, "COL_KO_IC_LC_UTF8", COL_NAME_LEN); + rc = db_util_create_collation(db, DB_UTIL_COL_KO_IC_LC, DB_UTIL_COL_UTF8, col_name); + ASSERT_EQ(DB_UTIL_ERROR, rc); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } -TEST_F(DbUtil, db_util_create_collation_n4) { +TEST_F(DbUtil, db_util_create_collation_n4) +{ int rc; rc = db_util_open(pszFilePath, &db, nOption); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); // collate_textrep error strncpy(col_name, "COL_UCA_UTF8", COL_NAME_LEN); - rc = db_util_create_collation(db, DB_UTIL_COL_UCA, (db_util_collate_textrep)3, col_name ); - ASSERT_EQ ( DB_UTIL_ERROR , rc ); + rc = db_util_create_collation(db, DB_UTIL_COL_UCA, (db_util_collate_textrep)3, col_name); + ASSERT_EQ(DB_UTIL_ERROR, rc); rc = db_util_close(db); - ASSERT_EQ ( DB_UTIL_OK , rc ); + ASSERT_EQ(DB_UTIL_OK, rc); } //======================================================================================= -int main(int argc, char** argv) { +int main(int argc, char** argv) +{ ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } -- cgit v1.2.3