diff --git a/BUILD.gn b/BUILD.gn index f334150b7b10a5f69643740bf143b0ba266f705d..d208a70e0aadd9dca3c1d9bd4a268e3e67ef8844 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -25,6 +25,7 @@ group("accesstoken_build_module_test") { "//base/security/access_token/interfaces/innerkits/token_callback/test:unittest", "//base/security/access_token/interfaces/innerkits/token_setproc/test:unittest", "//base/security/access_token/services/accesstokenmanager/test:unittest", + "//base/security/access_token/services/common/database/test:unittest", ] } if (token_sync_enable == true) { diff --git a/services/common/database/test/BUILD.gn b/services/common/database/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3727a34458f6b8ce31b8a6226f1671766b74cd9f --- /dev/null +++ b/services/common/database/test/BUILD.gn @@ -0,0 +1,50 @@ +# Copyright (c) 2022 Huawei Device 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. + +import("//build/test.gni") + +ohos_unittest("libdatabase_values_test") { + subsystem_name = "security" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "//third_party/googletest/include", + "//base/security/access_token/services/common/database/include", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/services/accesstokenmanager/main/cpp/include/database", + ] + + sources = [ "unittest/src/generic_values_test.cpp" ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + configs = [ "//base/security/access_token/config:coverage_flags" ] + + deps = [ + "//base/security/access_token/services/common/database:accesstoken_database_cxx", + "//third_party/sqlite:sqlite", + "//base/security/access_token/services/accesstokenmanager:accesstoken_manager_service", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] +} + +group("unittest") { + testonly = true + deps = [ ":libdatabase_values_test" ] +} diff --git a/services/common/database/test/unittest/src/generic_values_test.cpp b/services/common/database/test/unittest/src/generic_values_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55b5915f5463c17a3114194f9526269ffda16240 --- /dev/null +++ b/services/common/database/test/unittest/src/generic_values_test.cpp @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2022 Huawei Device 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 +#include +#include +#include "sqlite_storage.h" +#include "parcel.h" +#include "generic_values.h" +#include "variant_value.h" + +using namespace testing::ext; + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr int32_t GET_INT64_TRUE_VALUE = -1; +static constexpr int32_t ROLLBACKTRANSACTION_RESULT_ABNORMAL = -1; +static constexpr int32_t EXECUTESQL_RESULT_ABNORMAL = -1; +static const int32_t DEFAULT_VALUE = -1; +static const std::string PRAGMA_VERSION_COMMAND = "PRAGMA user_version"; +} // namespace +class DatabaseTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DatabaseTest::SetUpTestCase(void) +{} +void DatabaseTest::TearDownTestCase(void) +{} +void DatabaseTest::SetUp(void) +{} +void DatabaseTest::TearDown(void) +{} + +/** + * @tc.name: PutInt64001 + * @tc.desc: Verify the GenericValues put and get int64 value function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, PutInt64001, TestSize.Level1) +{ + GenericValues outGenericValues; + std::string key = "databasetest"; + int64_t data = 1; + outGenericValues.Put(key, data); + int64_t outdata = outGenericValues.GetInt64(key); + EXPECT_EQ(outdata, data); + outGenericValues.Remove(key); + outdata = outGenericValues.GetInt64(key); + EXPECT_EQ(GET_INT64_TRUE_VALUE, outdata); +} + +/** + * @tc.name: RollbackTransaction001 + * @tc.desc: RollbackTransaction001 Abnormal branch res != SQLITE_OK + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, RollbackTransaction001, TestSize.Level1) +{ + int32_t result = SqliteStorage::GetInstance().RollbackTransaction(); + EXPECT_EQ(result, ROLLBACKTRANSACTION_RESULT_ABNORMAL); +} + +/** + * @tc.name: RollbackTransaction002 + * @tc.desc: RollbackTransaction002 Abnormal branch db_ = nullptr + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, RollbackTransaction002, TestSize.Level1) +{ + SqliteStorage::GetInstance().Close(); + int32_t result = SqliteStorage::GetInstance().RollbackTransaction(); + EXPECT_EQ(result, ROLLBACKTRANSACTION_RESULT_ABNORMAL); +} +/** + * @tc.name: ExecuteSql001 + * @tc.desc: ExecuteSql001 Abnormal branch res != SQLITE_OK + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, ExecuteSql001, TestSize.Level1) +{ + int32_t result; + std::string TestSql = "test"; + result = SqliteStorage::GetInstance().ExecuteSql(TestSql); + EXPECT_EQ(result, EXECUTESQL_RESULT_ABNORMAL); +} + +/** + * @tc.name: ExecuteSql002 + * @tc.desc: ExecuteSql002 Abnormal branch db_ = nullptr + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, ExecuteSql002, TestSize.Level1) +{ + int32_t result; + std::string TestSql = "test"; + SqliteStorage::GetInstance().Close(); + result = SqliteStorage::GetInstance().ExecuteSql(TestSql); + EXPECT_EQ(result, EXECUTESQL_RESULT_ABNORMAL); +} + +/** + * @tc.name: SpitError001 + * @tc.desc: SpitError001 Abnormal branch db_ = nullptr + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, SpitError001, TestSize.Level1) +{ + SqliteStorage::GetInstance().Close(); + std::string result = SqliteStorage::GetInstance().SpitError().c_str(); + EXPECT_EQ(result.empty(), true); +} + +/** + * @tc.name: SpitError002 + * @tc.desc: SpitError002 use SpitError + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, SpitError002, TestSize.Level1) +{ + SqliteStorage::GetInstance().Open(); + std::string result = SqliteStorage::GetInstance().SpitError().c_str(); + EXPECT_EQ(result.length() > 0, true); +} + +/** + * @tc.name: VariantValue64001 + * @tc.desc: VariantValue64001 use VariantValue + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, VariantValue64001, TestSize.Level1) +{ + VariantValue Ntest; + int64_t TestValue = 1; + VariantValue Test(TestValue); + int64_t result = Test.GetInt64(); + EXPECT_EQ(result, TestValue); +} + +/** + * @tc.name: VariantValue64002 + * @tc.desc: VariantValue64002 getint and getint64 Abnormal branch + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DatabaseTest, VariantValue64002, TestSize.Level1) +{ + int32_t NtestValue = 1; + VariantValue Ntest(NtestValue); + int64_t result; + result = Ntest.GetInt64(); + EXPECT_EQ(DEFAULT_VALUE, result); + int64_t TestValue = 1; + VariantValue Test(TestValue); + result = Test.GetInt(); + EXPECT_EQ(DEFAULT_VALUE, result); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file