diff --git a/BUILD.gn b/BUILD.gn index 32147476ac439751c154179ce6868d565f80159d..db3edd2f578775464e89359fa475b97596ac3b76 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -19,8 +19,7 @@ group("accesstoken_build_module_test") { deps = [] if (is_standard_system) { deps += [ - "//base/security/access_token/frameworks/accesstoken/test:unittest", - "//base/security/access_token/frameworks/privacy/test:unittest", + "//base/security/access_token/frameworks/test/unittest:unittest", "//base/security/access_token/interfaces/innerkits/accesstoken/test:unittest", "//base/security/access_token/interfaces/innerkits/nativetoken/test:unittest", "//base/security/access_token/interfaces/innerkits/privacy/test:unittest", diff --git a/frameworks/accesstoken/test/BUILD.gn b/frameworks/accesstoken/test/BUILD.gn deleted file mode 100644 index ae8260e81de44d87e46886cf0008943bcd9f2a64..0000000000000000000000000000000000000000 --- a/frameworks/accesstoken/test/BUILD.gn +++ /dev/null @@ -1,116 +0,0 @@ -# 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("//base/security/access_token/access_token.gni") -import("//build/test.gni") - -config("accesstoken_test_config") { - include_dirs = [ - "//commonlibrary/c_utils/base/include", - "//third_party/googletest/include", - "//base/security/access_token/frameworks/accesstoken/include", - "//base/security/access_token/frameworks/common/include", - ] -} -ohos_unittest("libpermission_state_change_info_parcel_test") { - subsystem_name = "security" - part_name = "access_token" - module_out_path = part_name + "/" + part_name - - sources = [ "unittest/src/permission_state_change_info_parcel_test.cpp" ] - - configs = [ - ":accesstoken_test_config", - "//base/security/access_token/config:coverage_flags", - ] - - deps = [ "//base/security/access_token/frameworks/accesstoken:accesstoken_communication_adapter_cxx" ] - - external_deps = [ - "c_utils:utils", - "dsoftbus:softbus_client", - "ipc:ipc_single", - ] -} - -ohos_unittest("libpermission_state_change_scope_parcel_test") { - subsystem_name = "security" - part_name = "access_token" - module_out_path = part_name + "/" + part_name - - sources = [ "unittest/src/permission_state_change_scope_parcel_test.cpp" ] - - configs = [ - ":accesstoken_test_config", - "//base/security/access_token/config:coverage_flags", - ] - - deps = [ "//base/security/access_token/frameworks/accesstoken:accesstoken_communication_adapter_cxx" ] - - external_deps = [ - "c_utils:utils", - "dsoftbus:softbus_client", - "ipc:ipc_single", - ] -} - -ohos_unittest("libhap_info_parcel_test") { - subsystem_name = "security" - part_name = "access_token" - module_out_path = part_name + "/" + part_name - - sources = [ "unittest/src/hap_info_parcel_test.cpp" ] - - configs = [ - ":accesstoken_test_config", - "//base/security/access_token/config:coverage_flags", - ] - - deps = [ "//base/security/access_token/frameworks/accesstoken:accesstoken_communication_adapter_cxx" ] - - external_deps = [ - "c_utils:utils", - "dsoftbus:softbus_client", - "ipc:ipc_single", - ] -} - -ohos_unittest("libhap_policy_parcel_test") { - subsystem_name = "security" - part_name = "access_token" - module_out_path = part_name + "/" + part_name - - sources = [ "unittest/src/hap_policy_parcel_test.cpp" ] - - configs = [ - ":accesstoken_test_config", - "//base/security/access_token/config:coverage_flags", - ] - - deps = [ "//base/security/access_token/frameworks/accesstoken:accesstoken_communication_adapter_cxx" ] - - external_deps = [ - "c_utils:utils", - "dsoftbus:softbus_client", - "ipc:ipc_single", - ] -} -group("unittest") { - testonly = true - deps = [ - ":libhap_info_parcel_test", - ":libhap_policy_parcel_test", - ":libpermission_state_change_info_parcel_test", - ":libpermission_state_change_scope_parcel_test", - ] -} diff --git a/frameworks/accesstoken/test/unittest/src/hap_info_parcel_test.cpp b/frameworks/accesstoken/test/unittest/src/hap_info_parcel_test.cpp deleted file mode 100644 index 58e4cb85ad9097bd1364978a859e82ac9de7c660..0000000000000000000000000000000000000000 --- a/frameworks/accesstoken/test/unittest/src/hap_info_parcel_test.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 "access_token.h" -#include "hap_info_parcel.h" -#include "parcel.h" - -using namespace testing::ext; - -namespace OHOS { -namespace Security { -namespace AccessToken { -namespace { -static constexpr int32_t DEFAULT_API_VERSION = 8; -} -class HapInfoParcelTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void HapInfoParcelTest::SetUpTestCase(void) {} -void HapInfoParcelTest::TearDownTestCase(void) {} -void HapInfoParcelTest::SetUp(void) {} -void HapInfoParcelTest::TearDown(void) {} - -/** - * @tc.name: HapInfoParcel001 - * @tc.desc: Verify the CreateHapTokenInfo add one hap token function. - * @tc.type: FUNC - * @tc.require: I5QKZF - */ -HWTEST_F(HapInfoParcelTest, HapInfoParcel001, TestSize.Level1) -{ - HapInfoParcel hapinfoParcel; - hapinfoParcel.hapInfoParameter = { - .userID = 1, - .bundleName = "accesstoken_test", - .instIndex = 0, - .appIDDesc = "testtesttesttest", - .apiVersion = DEFAULT_API_VERSION, - }; - - Parcel parcel; - EXPECT_EQ(true, hapinfoParcel.Marshalling(parcel)); - - std::shared_ptr readedData(HapInfoParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(hapinfoParcel.hapInfoParameter.userID, readedData->hapInfoParameter.userID); - EXPECT_EQ(true, hapinfoParcel.hapInfoParameter.bundleName == readedData->hapInfoParameter.bundleName); - EXPECT_EQ(hapinfoParcel.hapInfoParameter.instIndex, readedData->hapInfoParameter.instIndex); - EXPECT_EQ(true, hapinfoParcel.hapInfoParameter.appIDDesc == readedData->hapInfoParameter.appIDDesc); - EXPECT_EQ(hapinfoParcel.hapInfoParameter.apiVersion, readedData->hapInfoParameter.apiVersion); -} -} -} -} \ No newline at end of file diff --git a/frameworks/accesstoken/test/unittest/src/permission_state_change_info_parcel_test.cpp b/frameworks/accesstoken/test/unittest/src/permission_state_change_info_parcel_test.cpp deleted file mode 100644 index a0e69baab53ba0b05035ea49a267b876f7c37d79..0000000000000000000000000000000000000000 --- a/frameworks/accesstoken/test/unittest/src/permission_state_change_info_parcel_test.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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 "parcel.h" -#include "permission_state_change_info_parcel.h" -#include "permission_state_change_info.h" - -using namespace testing::ext; - -namespace OHOS { -namespace Security { -namespace AccessToken { -namespace { -static constexpr int32_t TEST_PERMSTATE_CHANGE_TYPE = 10001; -static constexpr AccessTokenID TEST_TOKEN_ID = 10002; -static const std::string TEST_PERMISSION_NAME = "ohos.permission.PERMISSION_STATE_CHANGE_INFO"; -} -class PermissionStateChangeInfoParcelTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void PermissionStateChangeInfoParcelTest::SetUpTestCase(void) {} -void PermissionStateChangeInfoParcelTest::TearDownTestCase(void) {} -void PermissionStateChangeInfoParcelTest::SetUp(void) {} -void PermissionStateChangeInfoParcelTest::TearDown(void) {} - -/** - * @tc.name: PermissionStateChangeInfoParcel001 - * @tc.desc: Verify the CreateHapTokenInfo add one hap token function. - * @tc.type: FUNC - * @tc.require: I5QKZF - */ -HWTEST_F(PermissionStateChangeInfoParcelTest, PermissionStateChangeInfoParcel001, TestSize.Level1) -{ - PermissionStateChangeInfoParcel permissionStateParcel; - permissionStateParcel.changeInfo.PermStateChangeType = TEST_PERMSTATE_CHANGE_TYPE; - permissionStateParcel.changeInfo.tokenID = TEST_TOKEN_ID; - permissionStateParcel.changeInfo.permissionName = TEST_PERMISSION_NAME; - - Parcel parcel; - EXPECT_EQ(true, permissionStateParcel.Marshalling(parcel)); - - std::shared_ptr readedData(PermissionStateChangeInfoParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - EXPECT_EQ(permissionStateParcel.changeInfo.PermStateChangeType, readedData->changeInfo.PermStateChangeType); - EXPECT_EQ(permissionStateParcel.changeInfo.tokenID, readedData->changeInfo.tokenID); - EXPECT_EQ(permissionStateParcel.changeInfo.permissionName, readedData->changeInfo.permissionName); -} -} -} -} \ No newline at end of file diff --git a/frameworks/accesstoken/test/unittest/src/permission_state_change_scope_parcel_test.cpp b/frameworks/accesstoken/test/unittest/src/permission_state_change_scope_parcel_test.cpp deleted file mode 100644 index f4f812a766ecec0e19babd635310535919137195..0000000000000000000000000000000000000000 --- a/frameworks/accesstoken/test/unittest/src/permission_state_change_scope_parcel_test.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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 "parcel.h" -#include "permission_state_change_scope_parcel.h" - -using namespace testing::ext; - -namespace OHOS { -namespace Security { -namespace AccessToken { -namespace { -static constexpr AccessTokenID TEST_TOKEN_ID = 10002; -static const std::string TEST_PERMISSION_NAME = "ohos.permission.PERMISSION_STATE_CHANGE_INFO"; -} -class PermStateChangeScopeParcelTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void PermStateChangeScopeParcelTest::SetUpTestCase(void) {} -void PermStateChangeScopeParcelTest::TearDownTestCase(void) {} -void PermStateChangeScopeParcelTest::SetUp(void) {} -void PermStateChangeScopeParcelTest::TearDown(void) {} - -/** - * @tc.name: PermStateChangeScopeParcel001 - * @tc.desc: Verify the CreateHapTokenInfo add one hap token function. - * @tc.type: FUNC - * @tc.require: I5QKZF - */ -HWTEST_F(PermStateChangeScopeParcelTest, PermStateChangeScopeParcel001, TestSize.Level1) -{ - PermStateChangeScopeParcel permStateChangeScopeParcel; - permStateChangeScopeParcel.scope.tokenIDs.emplace_back(TEST_TOKEN_ID); - permStateChangeScopeParcel.scope.permList.emplace_back(TEST_PERMISSION_NAME); - - Parcel parcel; - EXPECT_EQ(true, permStateChangeScopeParcel.Marshalling(parcel)); - - std::shared_ptr readedData(PermStateChangeScopeParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(true, permStateChangeScopeParcel.scope.tokenIDs.size() == readedData->scope.tokenIDs.size()); - EXPECT_EQ(true, permStateChangeScopeParcel.scope.permList.size() == readedData->scope.permList.size()); - - for(int32_t i = 0; i < readedData->scope.tokenIDs.size(); i++) { - EXPECT_EQ(permStateChangeScopeParcel.scope.tokenIDs[i], readedData->scope.tokenIDs[i]); - } - for(int32_t i = 0; i < readedData->scope.permList.size(); i++) { - EXPECT_EQ(true, permStateChangeScopeParcel.scope.permList[i] == readedData->scope.permList[i]); - } -} -} -} -} \ No newline at end of file diff --git a/frameworks/privacy/test/BUILD.gn b/frameworks/test/unittest/BUILD.gn similarity index 64% rename from frameworks/privacy/test/BUILD.gn rename to frameworks/test/unittest/BUILD.gn index b749dbe5c67eaeaad74248218f0cbc31fdd09fdf..050ac39ae54f5bf6a9972789a05ff1f0a756c58b 100644 --- a/frameworks/privacy/test/BUILD.gn +++ b/frameworks/test/unittest/BUILD.gn @@ -1,49 +1,61 @@ -# 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("//base/security/access_token/access_token.gni") -import("//build/test.gni") - -config("accesstoken_test_config") { - include_dirs = [ - "//third_party/googletest/include", - "//base/security/access_token/frameworks/privacy/include", - "//base/security/access_token/interfaces/innerkits/accesstoken/include", - "//base/security/access_token/interfaces/innerkits/privacy/include", - ] -} - -ohos_unittest("libprivacy_parcel_test") { - subsystem_name = "security" - part_name = "access_token" - module_out_path = part_name + "/" + part_name - - sources = [ "unittest/src/privacy_parcel_test.cpp" ] - - configs = [ - ":accesstoken_test_config", - "//base/security/access_token/config:coverage_flags", - ] - - deps = [ "//base/security/access_token/frameworks/privacy:privacy_communication_adapter_cxx" ] - - external_deps = [ - "c_utils:utils", - "ipc:ipc_single", - ] -} - -group("unittest") { - testonly = true - deps = [ ":libprivacy_parcel_test" ] -} +# 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("//base/security/access_token/access_token.gni") +import("//build/test.gni") + +config("accesstoken_test_config") { + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "//third_party/googletest/include", + "//base/security/access_token/frameworks/accesstoken/include", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/frameworks/privacy/include", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/innerkits/privacy/include", + ] +} + +ohos_unittest("libaccesstoken_framework_test") { + subsystem_name = "security" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + + sources = [ + "accesstoken_parcel_test.cpp", + "common_test.cpp", + "privacy_parcel_test.cpp", + ] + + configs = [ + ":accesstoken_test_config", + "//base/security/access_token/config:coverage_flags", + ] + + deps = [ + "//base/security/access_token/frameworks/accesstoken:accesstoken_communication_adapter_cxx", + "//base/security/access_token/frameworks/common:accesstoken_common_cxx", + "//base/security/access_token/frameworks/privacy:privacy_communication_adapter_cxx" + ] + + external_deps = [ + "c_utils:utils", + "ipc:ipc_single", + ] +} + +group("unittest") { + testonly = true + deps = [ ":libaccesstoken_framework_test" ] +} + diff --git a/frameworks/accesstoken/test/unittest/src/hap_policy_parcel_test.cpp b/frameworks/test/unittest/accesstoken_parcel_test.cpp similarity index 55% rename from frameworks/accesstoken/test/unittest/src/hap_policy_parcel_test.cpp rename to frameworks/test/unittest/accesstoken_parcel_test.cpp index f232e6700a3c92ca84679868a159e0bfdb78d3c0..9003c49b50c34d280529f4cd627273edf116bc06 100644 --- a/frameworks/accesstoken/test/unittest/src/hap_policy_parcel_test.cpp +++ b/frameworks/test/unittest/accesstoken_parcel_test.cpp @@ -1,146 +1,234 @@ -/* - * 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 "access_token.h" -#include "hap_policy_parcel.h" -#include "hap_token_info.h" -#include "parcel.h" -#include "permission_state_full.h" - -using namespace testing::ext; - -namespace OHOS { -namespace Security { -namespace AccessToken { -namespace { -static const std::string TEST_PERMISSION_NAME_ALPHA = "ohos.permission.ALPHA"; -static const std::string TEST_PERMISSION_NAME_BETA = "ohos.permission.BETA"; - -PermissionDef g_permDefAlpha = { - .permissionName = TEST_PERMISSION_NAME_ALPHA, - .bundleName = "accesstoken_test", - .grantMode = 1, - .availableLevel = APL_NORMAL, - .label = "label", - .labelId = 1, - .description = "annoying", - .descriptionId = 1 -}; -PermissionDef g_permDefBeta = { - .permissionName = TEST_PERMISSION_NAME_BETA, - .bundleName = "accesstoken_test", - .grantMode = 1, - .availableLevel = APL_NORMAL, - .label = "label", - .labelId = 1, - .description = "so trouble", - .descriptionId = 1 -}; - -PermissionStateFull g_permStatAlpha = { - .permissionName = TEST_PERMISSION_NAME_ALPHA, - .isGeneral = true, - .resDeviceID = {"device"}, - .grantStatus = {PermissionState::PERMISSION_DENIED}, - .grantFlags = {PermissionFlag::PERMISSION_USER_SET} -}; -PermissionStateFull g_permStatBeta = { - .permissionName = TEST_PERMISSION_NAME_BETA, - .isGeneral = true, - .resDeviceID = {"device"}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED} -}; -} -class HapPolicyParcelTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void HapPolicyParcelTest::SetUpTestCase(void) {} -void HapPolicyParcelTest::TearDownTestCase(void) {} -void HapPolicyParcelTest::SetUp(void) {} -void HapPolicyParcelTest::TearDown(void) {} - -/** - * @tc.name: HapPolicyParcel001 - * @tc.desc: Verify the CreateHapTokenInfo add one hap token function. - * @tc.type: FUNC - * @tc.require: I5QKZF - */ -HWTEST_F(HapPolicyParcelTest, HapPolicyParcel001, TestSize.Level1) -{ - HapPolicyParcel hapPolicyParcel; - - hapPolicyParcel.hapPolicyParameter.apl = ATokenAplEnum::APL_NORMAL; - hapPolicyParcel.hapPolicyParameter.domain = "test.domain"; - hapPolicyParcel.hapPolicyParameter.permList.emplace_back(g_permDefAlpha); - hapPolicyParcel.hapPolicyParameter.permList.emplace_back(g_permDefBeta); - hapPolicyParcel.hapPolicyParameter.permStateList.emplace_back(g_permStatAlpha); - hapPolicyParcel.hapPolicyParameter.permStateList.emplace_back(g_permStatBeta); - - Parcel parcel; - EXPECT_EQ(true, hapPolicyParcel.Marshalling(parcel)); - - std::shared_ptr readedData(HapPolicyParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.apl, readedData->hapPolicyParameter.apl); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.domain, readedData->hapPolicyParameter.domain); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList.size(), readedData->hapPolicyParameter.permList.size()); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList.size(), - readedData->hapPolicyParameter.permStateList.size()); - - for(int32_t i = 0; i < hapPolicyParcel.hapPolicyParameter.permList.size(); i++) { - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].permissionName, - readedData->hapPolicyParameter.permList[i].permissionName); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].bundleName, - readedData->hapPolicyParameter.permList[i].bundleName); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].grantMode, - readedData->hapPolicyParameter.permList[i].grantMode); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].availableLevel, - readedData->hapPolicyParameter.permList[i].availableLevel); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].label, - readedData->hapPolicyParameter.permList[i].label); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].labelId, - readedData->hapPolicyParameter.permList[i].labelId); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].description, - readedData->hapPolicyParameter.permList[i].description); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].descriptionId, - readedData->hapPolicyParameter.permList[i].descriptionId); - } - - for(int32_t i = 0; i < hapPolicyParcel.hapPolicyParameter.permStateList.size(); i++) { - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].permissionName, - readedData->hapPolicyParameter.permStateList[i].permissionName); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].isGeneral, - readedData->hapPolicyParameter.permStateList[i].isGeneral); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].resDeviceID, - readedData->hapPolicyParameter.permStateList[i].resDeviceID); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].grantStatus, - readedData->hapPolicyParameter.permStateList[i].grantStatus); - EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].grantFlags, - readedData->hapPolicyParameter.permStateList[i].grantFlags); - } -} -} -} -} \ No newline at end of file +/* + * 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 "access_token.h" +#include "hap_info_parcel.h" +#include "hap_policy_parcel.h" +#include "parcel.h" +#include "permission_state_change_scope_parcel.h" +#include "permission_state_change_info_parcel.h" +#include "permission_state_full.h" + +using namespace testing::ext; + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr int32_t DEFAULT_API_VERSION = 8; +static const std::string TEST_PERMISSION_NAME_ALPHA = "ohos.permission.ALPHA"; +static const std::string TEST_PERMISSION_NAME_BETA = "ohos.permission.BETA"; +static constexpr AccessTokenID TEST_TOKEN_ID = 10002; +static constexpr int32_t TEST_PERMSTATE_CHANGE_TYPE = 10001; + +PermissionDef g_permDefAlpha = { + .permissionName = TEST_PERMISSION_NAME_ALPHA, + .bundleName = "accesstoken_test", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "annoying", + .descriptionId = 1 +}; +PermissionDef g_permDefBeta = { + .permissionName = TEST_PERMISSION_NAME_BETA, + .bundleName = "accesstoken_test", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "so trouble", + .descriptionId = 1 +}; + +PermissionStateFull g_permStatAlpha = { + .permissionName = TEST_PERMISSION_NAME_ALPHA, + .isGeneral = true, + .resDeviceID = {"device"}, + .grantStatus = {PermissionState::PERMISSION_DENIED}, + .grantFlags = {PermissionFlag::PERMISSION_USER_SET} +}; +PermissionStateFull g_permStatBeta = { + .permissionName = TEST_PERMISSION_NAME_BETA, + .isGeneral = true, + .resDeviceID = {"device"}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED} +}; +} +class AccessTokenParcelTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void AccessTokenParcelTest::SetUpTestCase(void) {} +void AccessTokenParcelTest::TearDownTestCase(void) {} +void AccessTokenParcelTest::SetUp(void) {} +void AccessTokenParcelTest::TearDown(void) {} + +/** + * @tc.name: HapInfoParcel001 + * @tc.desc: Test HapInfo Marshalling/Unmarshalling. + * @tc.type: FUNC + * @tc.require: issueI5QKZF + */ +HWTEST_F(AccessTokenParcelTest, HapInfoParcel001, TestSize.Level1) +{ + HapInfoParcel hapinfoParcel; + hapinfoParcel.hapInfoParameter = { + .userID = 1, + .bundleName = "accesstoken_test", + .instIndex = 0, + .appIDDesc = "testtesttesttest", + .apiVersion = DEFAULT_API_VERSION, + }; + + Parcel parcel; + EXPECT_EQ(true, hapinfoParcel.Marshalling(parcel)); + + std::shared_ptr readedData(HapInfoParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(hapinfoParcel.hapInfoParameter.userID, readedData->hapInfoParameter.userID); + EXPECT_EQ(true, hapinfoParcel.hapInfoParameter.bundleName == readedData->hapInfoParameter.bundleName); + EXPECT_EQ(hapinfoParcel.hapInfoParameter.instIndex, readedData->hapInfoParameter.instIndex); + EXPECT_EQ(true, hapinfoParcel.hapInfoParameter.appIDDesc == readedData->hapInfoParameter.appIDDesc); + EXPECT_EQ(hapinfoParcel.hapInfoParameter.apiVersion, readedData->hapInfoParameter.apiVersion); +} + +/** + * @tc.name: HapPolicyParcel001 + * @tc.desc: Test HapPolicy Marshalling/Unmarshalling. + * @tc.type: FUNC + * @tc.require: issueI5QKZF + */ +HWTEST_F(AccessTokenParcelTest, HapPolicyParcel001, TestSize.Level1) +{ + HapPolicyParcel hapPolicyParcel; + + hapPolicyParcel.hapPolicyParameter.apl = ATokenAplEnum::APL_NORMAL; + hapPolicyParcel.hapPolicyParameter.domain = "test.domain"; + hapPolicyParcel.hapPolicyParameter.permList.emplace_back(g_permDefAlpha); + hapPolicyParcel.hapPolicyParameter.permList.emplace_back(g_permDefBeta); + hapPolicyParcel.hapPolicyParameter.permStateList.emplace_back(g_permStatAlpha); + hapPolicyParcel.hapPolicyParameter.permStateList.emplace_back(g_permStatBeta); + + Parcel parcel; + EXPECT_EQ(true, hapPolicyParcel.Marshalling(parcel)); + + std::shared_ptr readedData(HapPolicyParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.apl, readedData->hapPolicyParameter.apl); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.domain, readedData->hapPolicyParameter.domain); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList.size(), readedData->hapPolicyParameter.permList.size()); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList.size(), + readedData->hapPolicyParameter.permStateList.size()); + + for(int32_t i = 0; i < hapPolicyParcel.hapPolicyParameter.permList.size(); i++) { + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].permissionName, + readedData->hapPolicyParameter.permList[i].permissionName); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].bundleName, + readedData->hapPolicyParameter.permList[i].bundleName); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].grantMode, + readedData->hapPolicyParameter.permList[i].grantMode); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].availableLevel, + readedData->hapPolicyParameter.permList[i].availableLevel); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].label, + readedData->hapPolicyParameter.permList[i].label); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].labelId, + readedData->hapPolicyParameter.permList[i].labelId); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].description, + readedData->hapPolicyParameter.permList[i].description); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permList[i].descriptionId, + readedData->hapPolicyParameter.permList[i].descriptionId); + } + + for(int32_t i = 0; i < hapPolicyParcel.hapPolicyParameter.permStateList.size(); i++) { + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].permissionName, + readedData->hapPolicyParameter.permStateList[i].permissionName); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].isGeneral, + readedData->hapPolicyParameter.permStateList[i].isGeneral); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].resDeviceID, + readedData->hapPolicyParameter.permStateList[i].resDeviceID); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].grantStatus, + readedData->hapPolicyParameter.permStateList[i].grantStatus); + EXPECT_EQ(hapPolicyParcel.hapPolicyParameter.permStateList[i].grantFlags, + readedData->hapPolicyParameter.permStateList[i].grantFlags); + } +} + +/** + * @tc.name: PermissionStateChangeInfoParcel001 + * @tc.desc: Test PermissionStateChangeInfo Marshalling/Unmarshalling. + * @tc.type: FUNC + * @tc.require: issueI5QKZF + */ +HWTEST_F(AccessTokenParcelTest, PermissionStateChangeInfoParcel001, TestSize.Level1) +{ + PermissionStateChangeInfoParcel permissionStateParcel; + permissionStateParcel.changeInfo.PermStateChangeType = TEST_PERMSTATE_CHANGE_TYPE; + permissionStateParcel.changeInfo.tokenID = TEST_TOKEN_ID; + permissionStateParcel.changeInfo.permissionName = TEST_PERMISSION_NAME_ALPHA; + + Parcel parcel; + EXPECT_EQ(true, permissionStateParcel.Marshalling(parcel)); + + std::shared_ptr readedData(PermissionStateChangeInfoParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + EXPECT_EQ(permissionStateParcel.changeInfo.PermStateChangeType, readedData->changeInfo.PermStateChangeType); + EXPECT_EQ(permissionStateParcel.changeInfo.tokenID, readedData->changeInfo.tokenID); + EXPECT_EQ(permissionStateParcel.changeInfo.permissionName, readedData->changeInfo.permissionName); +} + +/** + * @tc.name: PermStateChangeScopeParcel001 + * @tc.desc: Test PermStateChangeScope Marshalling/Unmarshalling. + * @tc.type: FUNC + * @tc.require: issueI5QKZF + */ +HWTEST_F(AccessTokenParcelTest, PermStateChangeScopeParcel001, TestSize.Level1) +{ + PermStateChangeScopeParcel permStateChangeScopeParcel; + permStateChangeScopeParcel.scope.tokenIDs.emplace_back(TEST_TOKEN_ID); + permStateChangeScopeParcel.scope.permList.emplace_back(TEST_PERMISSION_NAME_ALPHA); + + Parcel parcel; + EXPECT_EQ(true, permStateChangeScopeParcel.Marshalling(parcel)); + + std::shared_ptr readedData(PermStateChangeScopeParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(true, permStateChangeScopeParcel.scope.tokenIDs.size() == readedData->scope.tokenIDs.size()); + EXPECT_EQ(true, permStateChangeScopeParcel.scope.permList.size() == readedData->scope.permList.size()); + + for(int32_t i = 0; i < readedData->scope.tokenIDs.size(); i++) { + EXPECT_EQ(permStateChangeScopeParcel.scope.tokenIDs[i], readedData->scope.tokenIDs[i]); + } + for(int32_t i = 0; i < readedData->scope.permList.size(); i++) { + EXPECT_EQ(true, permStateChangeScopeParcel.scope.permList[i] == readedData->scope.permList[i]); + } +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/privacy/test/unittest/src/privacy_parcel_test.h b/frameworks/test/unittest/common_test.cpp similarity index 51% rename from frameworks/privacy/test/unittest/src/privacy_parcel_test.h rename to frameworks/test/unittest/common_test.cpp index f18f664a9a8b6ddf0e0adca897836c9b1fb52c8a..f6543fdc55ea1e534c9443cff626d5751849753b 100644 --- a/frameworks/privacy/test/unittest/src/privacy_parcel_test.h +++ b/frameworks/test/unittest/common_test.cpp @@ -1,31 +1,61 @@ -/* - * 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 - -namespace OHOS { -namespace Security { -namespace AccessToken { -class PrivacyParcelTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - - void SetUp(); - void TearDown(); -}; -} // namespace AccessToken -} // namespace Security -} // namespace OHOS +/* + * 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 "constant_common.h" + +using namespace testing::ext; + +namespace OHOS { +namespace Security { +namespace AccessToken { +class CommonTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + + void SetUp(); + void TearDown(); +}; + +void CommonTest::SetUpTestCase() {} +void CommonTest::TearDownTestCase() {} +void CommonTest::SetUp() {} +void CommonTest::TearDown() {} + +/** + * @tc.name: BundleUsedRecordParcel001 + * @tc.desc: Verify the CreateHapTokenInfo add one hap token function. + * @tc.type: FUNC + * @tc.require: issueI5RUCC + */ +HWTEST_F(CommonTest, BundleUsedRecordParcel001, TestSize.Level1) +{ + std::string res; + res = ConstantCommon::EncryptDevId(""); + EXPECT_EQ(res, ""); + + res = ConstantCommon::EncryptDevId("12345"); + EXPECT_EQ(res, "1*******"); + + res = ConstantCommon::EncryptDevId("123454321"); + EXPECT_EQ(res, "1234****4321"); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/frameworks/privacy/test/unittest/src/privacy_parcel_test.cpp b/frameworks/test/unittest/privacy_parcel_test.cpp similarity index 94% rename from frameworks/privacy/test/unittest/src/privacy_parcel_test.cpp rename to frameworks/test/unittest/privacy_parcel_test.cpp index 906e902387009f66105a19077c4662126f4228b2..5ba9897fe5759808a91eb775c73ed43140354509 100644 --- a/frameworks/privacy/test/unittest/src/privacy_parcel_test.cpp +++ b/frameworks/test/unittest/privacy_parcel_test.cpp @@ -1,332 +1,329 @@ -/* - * 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 "privacy_parcel_test.h" - -#include -#include - -#include "bundle_used_record_parcel.h" -#include "parcel.h" -#include "perm_active_response_parcel.h" -#include "permission_used_record_parcel.h" -#include "permission_used_request_parcel.h" -#include "permission_used_result_parcel.h" -#include "used_record_detail_parcel.h" - -using namespace testing::ext; - -namespace OHOS { -namespace Security { -namespace AccessToken { -namespace { -UsedRecordDetail g_accessRecord1 = { - .status = 0, - .timestamp = 0L, - .accessDuration = 0L, -}; - -UsedRecordDetail g_accessRecord2 = { - .status = 1, - .timestamp = 1L, - .accessDuration = 1L, -}; - -UsedRecordDetail g_rejectRecord1 = { - .status = 2, - .timestamp = 2L, - .accessDuration = 2L, -}; - -UsedRecordDetail g_rejectRecord2 = { - .status = 3, - .timestamp = 3L, - .accessDuration = 3L, -}; - -PermissionUsedRecord g_permissionRecord1 = { - .permissionName = "ohos.permission.CAMERA", - .accessCount = 2, - .rejectCount = 2, - .lastAccessTime = 0L, - .lastRejectTime = 0L, - .lastAccessDuration = 0L, -}; - -PermissionUsedRecord g_permissionRecord2 = { - .permissionName = "ohos.permission.LOCATION", - .accessCount = 2, - .rejectCount = 2, - .lastAccessTime = 1L, - .lastRejectTime = 1L, - .lastAccessDuration = 1L, -}; - -BundleUsedRecord g_bundleUsedRecord1 = { - .tokenId = 100, - .isRemote = false, - .deviceId = "you guess", - .bundleName = "com.ohos.camera", -}; - -BundleUsedRecord g_bundleUsedRecord2 = { - .tokenId = 101, - .isRemote = false, - .deviceId = "i want to know too", - .bundleName = "com.ohos.permissionmanager", -}; -} - -void PrivacyParcelTest::SetUpTestCase() -{ -} - -void PrivacyParcelTest::TearDownTestCase() -{ -} - -void PrivacyParcelTest::SetUp() -{ -} - -void PrivacyParcelTest::TearDown() -{ -} - -/** - * @tc.name: BundleUsedRecordParcel001 - * @tc.desc: Verify the CreateHapTokenInfo add one hap token function. - * @tc.type: FUNC - * @tc.require: issueI5RUCC - */ -HWTEST_F(PrivacyParcelTest, BundleUsedRecordParcel001, TestSize.Level1) -{ - BundleUsedRecordParcel bundleUsedRecordParcel; - - bundleUsedRecordParcel.bundleRecord = { - .tokenId = 100, - .isRemote = false, - .deviceId = "you guess", - .bundleName = "com.ohos.permissionmanager", - }; - - g_permissionRecord1.accessRecords.emplace_back(g_accessRecord1); - g_permissionRecord1.accessRecords.emplace_back(g_accessRecord2); - g_permissionRecord2.rejectRecords.emplace_back(g_rejectRecord1); - g_permissionRecord2.rejectRecords.emplace_back(g_rejectRecord2); - - bundleUsedRecordParcel.bundleRecord.permissionRecords.emplace_back(g_permissionRecord1); - bundleUsedRecordParcel.bundleRecord.permissionRecords.emplace_back(g_permissionRecord2); - - Parcel parcel; - EXPECT_EQ(true, bundleUsedRecordParcel.Marshalling(parcel)); - - std::shared_ptr readedData(BundleUsedRecordParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(bundleUsedRecordParcel.bundleRecord.tokenId, readedData->bundleRecord.tokenId); - EXPECT_EQ(bundleUsedRecordParcel.bundleRecord.isRemote, readedData->bundleRecord.isRemote); - EXPECT_EQ(bundleUsedRecordParcel.bundleRecord.deviceId, readedData->bundleRecord.deviceId); - EXPECT_EQ(bundleUsedRecordParcel.bundleRecord.bundleName, readedData->bundleRecord.bundleName); -} - -/** - * @tc.name: ActiveChangeResponseParcel001 - * @tc.desc: Verify ActiveChangeResponseParcel Marshalling and Unmarshalling function. - * @tc.type: FUNC - * @tc.require: issueI5RRLJ - */ -HWTEST_F(PrivacyParcelTest, ActiveChangeResponseParcel001, TestSize.Level1) -{ - ActiveChangeResponseParcel activeChangeResponseParcel; - - activeChangeResponseParcel.changeResponse = { - .tokenID = 100, - .permissionName = "ohos.permission.CAMERA", - .deviceId = "you guess", - .type = PERM_INACTIVE, - }; - - Parcel parcel; - EXPECT_EQ(true, activeChangeResponseParcel.Marshalling(parcel)); - - std::shared_ptr readedData(ActiveChangeResponseParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(activeChangeResponseParcel.changeResponse.tokenID, readedData->changeResponse.tokenID); - EXPECT_EQ(activeChangeResponseParcel.changeResponse.permissionName, readedData->changeResponse.permissionName); - EXPECT_EQ(activeChangeResponseParcel.changeResponse.deviceId, readedData->changeResponse.deviceId); - EXPECT_EQ(activeChangeResponseParcel.changeResponse.type, readedData->changeResponse.type); -} - -/** - * @tc.name: PermissionUsedRecordParcel001 - * @tc.desc: Verify the PermissionUsedRecordParcel Marshalling and Unmarshalling function. - * @tc.type: FUNC - * @tc.require: issueI5RUCC - */ -HWTEST_F(PrivacyParcelTest, PermissionUsedRecordParcel001, TestSize.Level1) -{ - PermissionUsedRecordParcel permissionUsedRecordParcel; - - permissionUsedRecordParcel.permissionRecord = { - .permissionName = "ohos.permission.CAMERA", - .accessCount = 2, - .rejectCount = 2, - .lastAccessTime = 0L, - .lastRejectTime = 0L, - .lastAccessDuration = 0L, - }; - permissionUsedRecordParcel.permissionRecord.accessRecords.emplace_back(g_accessRecord1); - permissionUsedRecordParcel.permissionRecord.accessRecords.emplace_back(g_accessRecord2); - permissionUsedRecordParcel.permissionRecord.rejectRecords.emplace_back(g_rejectRecord1); - permissionUsedRecordParcel.permissionRecord.rejectRecords.emplace_back(g_rejectRecord2); - - Parcel parcel; - EXPECT_EQ(true, permissionUsedRecordParcel.Marshalling(parcel)); - - std::shared_ptr readedData(PermissionUsedRecordParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.permissionName, readedData->permissionRecord.permissionName); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.accessCount, readedData->permissionRecord.accessCount); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.rejectCount, readedData->permissionRecord.rejectCount); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.lastAccessTime, readedData->permissionRecord.lastAccessTime); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.lastRejectTime, readedData->permissionRecord.lastRejectTime); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.lastAccessDuration, - readedData->permissionRecord.lastAccessDuration); - - for(uint32_t i = 0; i < permissionUsedRecordParcel.permissionRecord.accessRecords.size(); i++) { - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.accessRecords[i].status, - readedData->permissionRecord.accessRecords[i].status); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.accessRecords[i].timestamp, - readedData->permissionRecord.accessRecords[i].timestamp); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.accessRecords[i].accessDuration, - readedData->permissionRecord.accessRecords[i].accessDuration); - } - - for(uint32_t i = 0; i < permissionUsedRecordParcel.permissionRecord.rejectRecords.size(); i++) { - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.rejectRecords[i].status, - readedData->permissionRecord.rejectRecords[i].status); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.rejectRecords[i].timestamp, - readedData->permissionRecord.rejectRecords[i].timestamp); - EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.rejectRecords[i].accessDuration, - readedData->permissionRecord.rejectRecords[i].accessDuration); - } -} - -/** - * @tc.name: PermissionUsedRequestParcel001 - * @tc.desc: Verify the PermissionUsedRequestParcel Marshalling and Unmarshalling function. - * @tc.type: FUNC - * @tc.require: issueI5RUP1 - */ -HWTEST_F(PrivacyParcelTest, PermissionUsedRequestParcel001, TestSize.Level1) -{ - PermissionUsedRequestParcel permissionUsedRequestParcel; - - permissionUsedRequestParcel.request = { - .tokenId = 100, - .isRemote = false, - .deviceId = "you guess", - .bundleName = "com.ohos.permissionmanager", - .beginTimeMillis = 0L, - .endTimeMillis = 0L, - .flag = FLAG_PERMISSION_USAGE_SUMMARY, - }; - permissionUsedRequestParcel.request.permissionList.emplace_back("ohos.permission.CAMERA"); - permissionUsedRequestParcel.request.permissionList.emplace_back("ohos.permission.LOCATION"); - - Parcel parcel; - EXPECT_EQ(true, permissionUsedRequestParcel.Marshalling(parcel)); - - std::shared_ptr readedData(PermissionUsedRequestParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(permissionUsedRequestParcel.request.tokenId, readedData->request.tokenId); - EXPECT_EQ(permissionUsedRequestParcel.request.isRemote, readedData->request.isRemote); - EXPECT_EQ(permissionUsedRequestParcel.request.deviceId, readedData->request.deviceId); - EXPECT_EQ(permissionUsedRequestParcel.request.bundleName, readedData->request.bundleName); - EXPECT_EQ(permissionUsedRequestParcel.request.beginTimeMillis, readedData->request.beginTimeMillis); - EXPECT_EQ(permissionUsedRequestParcel.request.endTimeMillis, readedData->request.endTimeMillis); - EXPECT_EQ(permissionUsedRequestParcel.request.flag, readedData->request.flag); - - for(uint32_t i = 0; i < permissionUsedRequestParcel.request.permissionList.size(); i++) { - EXPECT_EQ(permissionUsedRequestParcel.request.permissionList[i], readedData->request.permissionList[i]); - } -} - -/** - * @tc.name: PermissionUsedResultParcel001 - * @tc.desc: Verify the PermissionUsedResultParcel Marshalling and Unmarshalling function. - * @tc.type: FUNC - * @tc.require: issueI5RWP4 - */ -HWTEST_F(PrivacyParcelTest, PermissionUsedResultParcel001, TestSize.Level1) -{ - PermissionUsedResultParcel permissionUsedResultParcel; - - permissionUsedResultParcel.result = { - .beginTimeMillis = 0L, - .endTimeMillis = 0L, - }; - - g_bundleUsedRecord1.permissionRecords.emplace_back(g_permissionRecord1); - g_bundleUsedRecord1.permissionRecords.emplace_back(g_permissionRecord2); - g_bundleUsedRecord2.permissionRecords.emplace_back(g_permissionRecord1); - g_bundleUsedRecord2.permissionRecords.emplace_back(g_permissionRecord2); - - permissionUsedResultParcel.result.bundleRecords.emplace_back(g_bundleUsedRecord1); - permissionUsedResultParcel.result.bundleRecords.emplace_back(g_bundleUsedRecord2); - - Parcel parcel; - EXPECT_EQ(true, permissionUsedResultParcel.Marshalling(parcel)); - - std::shared_ptr readedData(PermissionUsedResultParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(permissionUsedResultParcel.result.beginTimeMillis, readedData->result.beginTimeMillis); - EXPECT_EQ(permissionUsedResultParcel.result.endTimeMillis, readedData->result.endTimeMillis); -} - -/** - * @tc.name: UsedRecordDetailParcel001 - * @tc.desc: Verify the UsedRecordDetailParcel Marshalling and Unmarshalling function. - * @tc.type: FUNC - * @tc.require: issueI5RWP4 - */ -HWTEST_F(PrivacyParcelTest, UsedRecordDetailParcel001, TestSize.Level1) -{ - UsedRecordDetailParcel usedRecordDetailParcel; - - usedRecordDetailParcel.detail = { - .status = 0, - .timestamp = 0L, - .accessDuration = 0L, - }; - - Parcel parcel; - EXPECT_EQ(true, usedRecordDetailParcel.Marshalling(parcel)); - - std::shared_ptr readedData(UsedRecordDetailParcel::Unmarshalling(parcel)); - EXPECT_EQ(true, readedData != nullptr); - - EXPECT_EQ(usedRecordDetailParcel.detail.status, readedData->detail.status); - EXPECT_EQ(usedRecordDetailParcel.detail.timestamp, readedData->detail.timestamp); - EXPECT_EQ(usedRecordDetailParcel.detail.accessDuration, readedData->detail.accessDuration); -} -} // namespace AccessToken -} // namespace Security -} // namespace OHOS +/* + * 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 "bundle_used_record_parcel.h" +#include "parcel.h" +#include "perm_active_response_parcel.h" +#include "permission_used_record_parcel.h" +#include "permission_used_request_parcel.h" +#include "permission_used_result_parcel.h" +#include "used_record_detail_parcel.h" + +using namespace testing::ext; + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +UsedRecordDetail g_accessRecord1 = { + .status = 0, + .timestamp = 0L, + .accessDuration = 0L, +}; + +UsedRecordDetail g_accessRecord2 = { + .status = 1, + .timestamp = 1L, + .accessDuration = 1L, +}; + +UsedRecordDetail g_rejectRecord1 = { + .status = 2, + .timestamp = 2L, + .accessDuration = 2L, +}; + +UsedRecordDetail g_rejectRecord2 = { + .status = 3, + .timestamp = 3L, + .accessDuration = 3L, +}; + +PermissionUsedRecord g_permissionRecord1 = { + .permissionName = "ohos.permission.CAMERA", + .accessCount = 2, + .rejectCount = 2, + .lastAccessTime = 0L, + .lastRejectTime = 0L, + .lastAccessDuration = 0L, +}; + +PermissionUsedRecord g_permissionRecord2 = { + .permissionName = "ohos.permission.LOCATION", + .accessCount = 2, + .rejectCount = 2, + .lastAccessTime = 1L, + .lastRejectTime = 1L, + .lastAccessDuration = 1L, +}; + +BundleUsedRecord g_bundleUsedRecord1 = { + .tokenId = 100, + .isRemote = false, + .deviceId = "you guess", + .bundleName = "com.ohos.camera", +}; + +BundleUsedRecord g_bundleUsedRecord2 = { + .tokenId = 101, + .isRemote = false, + .deviceId = "i want to know too", + .bundleName = "com.ohos.permissionmanager", +}; +} + +class PrivacyParcelTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + + void SetUp(); + void TearDown(); +}; + +void PrivacyParcelTest::SetUpTestCase() {} +void PrivacyParcelTest::TearDownTestCase() {} +void PrivacyParcelTest::SetUp() {} +void PrivacyParcelTest::TearDown() {} + +/** + * @tc.name: BundleUsedRecordParcel001 + * @tc.desc: Verify the CreateHapTokenInfo add one hap token function. + * @tc.type: FUNC + * @tc.require: issueI5RUCC + */ +HWTEST_F(PrivacyParcelTest, BundleUsedRecordParcel001, TestSize.Level1) +{ + BundleUsedRecordParcel bundleUsedRecordParcel; + + bundleUsedRecordParcel.bundleRecord = { + .tokenId = 100, + .isRemote = false, + .deviceId = "device", + .bundleName = "com.ohos.permissionmanager", + }; + + g_permissionRecord1.accessRecords.emplace_back(g_accessRecord1); + g_permissionRecord1.accessRecords.emplace_back(g_accessRecord2); + g_permissionRecord2.rejectRecords.emplace_back(g_rejectRecord1); + g_permissionRecord2.rejectRecords.emplace_back(g_rejectRecord2); + + bundleUsedRecordParcel.bundleRecord.permissionRecords.emplace_back(g_permissionRecord1); + bundleUsedRecordParcel.bundleRecord.permissionRecords.emplace_back(g_permissionRecord2); + + Parcel parcel; + EXPECT_EQ(true, bundleUsedRecordParcel.Marshalling(parcel)); + + std::shared_ptr readedData(BundleUsedRecordParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(bundleUsedRecordParcel.bundleRecord.tokenId, readedData->bundleRecord.tokenId); + EXPECT_EQ(bundleUsedRecordParcel.bundleRecord.isRemote, readedData->bundleRecord.isRemote); + EXPECT_EQ(bundleUsedRecordParcel.bundleRecord.deviceId, readedData->bundleRecord.deviceId); + EXPECT_EQ(bundleUsedRecordParcel.bundleRecord.bundleName, readedData->bundleRecord.bundleName); +} + +/** + * @tc.name: ActiveChangeResponseParcel001 + * @tc.desc: Verify ActiveChangeResponseParcel Marshalling and Unmarshalling function. + * @tc.type: FUNC + * @tc.require: issueI5RRLJ + */ +HWTEST_F(PrivacyParcelTest, ActiveChangeResponseParcel001, TestSize.Level1) +{ + ActiveChangeResponseParcel activeChangeResponseParcel; + + activeChangeResponseParcel.changeResponse = { + .tokenID = 100, + .permissionName = "ohos.permission.CAMERA", + .deviceId = "device", + .type = PERM_INACTIVE, + }; + + Parcel parcel; + EXPECT_EQ(true, activeChangeResponseParcel.Marshalling(parcel)); + + std::shared_ptr readedData(ActiveChangeResponseParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(activeChangeResponseParcel.changeResponse.tokenID, readedData->changeResponse.tokenID); + EXPECT_EQ(activeChangeResponseParcel.changeResponse.permissionName, readedData->changeResponse.permissionName); + EXPECT_EQ(activeChangeResponseParcel.changeResponse.deviceId, readedData->changeResponse.deviceId); + EXPECT_EQ(activeChangeResponseParcel.changeResponse.type, readedData->changeResponse.type); +} + +/** + * @tc.name: PermissionUsedRecordParcel001 + * @tc.desc: Verify the PermissionUsedRecordParcel Marshalling and Unmarshalling function. + * @tc.type: FUNC + * @tc.require: issueI5RUCC + */ +HWTEST_F(PrivacyParcelTest, PermissionUsedRecordParcel001, TestSize.Level1) +{ + PermissionUsedRecordParcel permissionUsedRecordParcel; + + permissionUsedRecordParcel.permissionRecord = { + .permissionName = "ohos.permission.CAMERA", + .accessCount = 2, + .rejectCount = 2, + .lastAccessTime = 0L, + .lastRejectTime = 0L, + .lastAccessDuration = 0L, + }; + permissionUsedRecordParcel.permissionRecord.accessRecords.emplace_back(g_accessRecord1); + permissionUsedRecordParcel.permissionRecord.accessRecords.emplace_back(g_accessRecord2); + permissionUsedRecordParcel.permissionRecord.rejectRecords.emplace_back(g_rejectRecord1); + permissionUsedRecordParcel.permissionRecord.rejectRecords.emplace_back(g_rejectRecord2); + + Parcel parcel; + EXPECT_EQ(true, permissionUsedRecordParcel.Marshalling(parcel)); + + std::shared_ptr readedData(PermissionUsedRecordParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.permissionName, readedData->permissionRecord.permissionName); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.accessCount, readedData->permissionRecord.accessCount); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.rejectCount, readedData->permissionRecord.rejectCount); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.lastAccessTime, readedData->permissionRecord.lastAccessTime); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.lastRejectTime, readedData->permissionRecord.lastRejectTime); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.lastAccessDuration, + readedData->permissionRecord.lastAccessDuration); + + for(uint32_t i = 0; i < permissionUsedRecordParcel.permissionRecord.accessRecords.size(); i++) { + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.accessRecords[i].status, + readedData->permissionRecord.accessRecords[i].status); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.accessRecords[i].timestamp, + readedData->permissionRecord.accessRecords[i].timestamp); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.accessRecords[i].accessDuration, + readedData->permissionRecord.accessRecords[i].accessDuration); + } + + for(uint32_t i = 0; i < permissionUsedRecordParcel.permissionRecord.rejectRecords.size(); i++) { + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.rejectRecords[i].status, + readedData->permissionRecord.rejectRecords[i].status); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.rejectRecords[i].timestamp, + readedData->permissionRecord.rejectRecords[i].timestamp); + EXPECT_EQ(permissionUsedRecordParcel.permissionRecord.rejectRecords[i].accessDuration, + readedData->permissionRecord.rejectRecords[i].accessDuration); + } +} + +/** + * @tc.name: PermissionUsedRequestParcel001 + * @tc.desc: Verify the PermissionUsedRequestParcel Marshalling and Unmarshalling function. + * @tc.type: FUNC + * @tc.require: issueI5RUP1 + */ +HWTEST_F(PrivacyParcelTest, PermissionUsedRequestParcel001, TestSize.Level1) +{ + PermissionUsedRequestParcel permissionUsedRequestParcel; + + permissionUsedRequestParcel.request = { + .tokenId = 100, + .isRemote = false, + .deviceId = "you guess", + .bundleName = "com.ohos.permissionmanager", + .beginTimeMillis = 0L, + .endTimeMillis = 0L, + .flag = FLAG_PERMISSION_USAGE_SUMMARY, + }; + permissionUsedRequestParcel.request.permissionList.emplace_back("ohos.permission.CAMERA"); + permissionUsedRequestParcel.request.permissionList.emplace_back("ohos.permission.LOCATION"); + + Parcel parcel; + EXPECT_EQ(true, permissionUsedRequestParcel.Marshalling(parcel)); + + std::shared_ptr readedData(PermissionUsedRequestParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(permissionUsedRequestParcel.request.tokenId, readedData->request.tokenId); + EXPECT_EQ(permissionUsedRequestParcel.request.isRemote, readedData->request.isRemote); + EXPECT_EQ(permissionUsedRequestParcel.request.deviceId, readedData->request.deviceId); + EXPECT_EQ(permissionUsedRequestParcel.request.bundleName, readedData->request.bundleName); + EXPECT_EQ(permissionUsedRequestParcel.request.beginTimeMillis, readedData->request.beginTimeMillis); + EXPECT_EQ(permissionUsedRequestParcel.request.endTimeMillis, readedData->request.endTimeMillis); + EXPECT_EQ(permissionUsedRequestParcel.request.flag, readedData->request.flag); + + for(uint32_t i = 0; i < permissionUsedRequestParcel.request.permissionList.size(); i++) { + EXPECT_EQ(permissionUsedRequestParcel.request.permissionList[i], readedData->request.permissionList[i]); + } +} + +/** + * @tc.name: PermissionUsedResultParcel001 + * @tc.desc: Verify the PermissionUsedResultParcel Marshalling and Unmarshalling function. + * @tc.type: FUNC + * @tc.require: issueI5RWP4 + */ +HWTEST_F(PrivacyParcelTest, PermissionUsedResultParcel001, TestSize.Level1) +{ + PermissionUsedResultParcel permissionUsedResultParcel; + + permissionUsedResultParcel.result = { + .beginTimeMillis = 0L, + .endTimeMillis = 0L, + }; + + g_bundleUsedRecord1.permissionRecords.emplace_back(g_permissionRecord1); + g_bundleUsedRecord1.permissionRecords.emplace_back(g_permissionRecord2); + g_bundleUsedRecord2.permissionRecords.emplace_back(g_permissionRecord1); + g_bundleUsedRecord2.permissionRecords.emplace_back(g_permissionRecord2); + + permissionUsedResultParcel.result.bundleRecords.emplace_back(g_bundleUsedRecord1); + permissionUsedResultParcel.result.bundleRecords.emplace_back(g_bundleUsedRecord2); + + Parcel parcel; + EXPECT_EQ(true, permissionUsedResultParcel.Marshalling(parcel)); + + std::shared_ptr readedData(PermissionUsedResultParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(permissionUsedResultParcel.result.beginTimeMillis, readedData->result.beginTimeMillis); + EXPECT_EQ(permissionUsedResultParcel.result.endTimeMillis, readedData->result.endTimeMillis); +} + +/** + * @tc.name: UsedRecordDetailParcel001 + * @tc.desc: Verify the UsedRecordDetailParcel Marshalling and Unmarshalling function. + * @tc.type: FUNC + * @tc.require: issueI5RWP4 + */ +HWTEST_F(PrivacyParcelTest, UsedRecordDetailParcel001, TestSize.Level1) +{ + UsedRecordDetailParcel usedRecordDetailParcel; + + usedRecordDetailParcel.detail = { + .status = 0, + .timestamp = 0L, + .accessDuration = 0L, + }; + + Parcel parcel; + EXPECT_EQ(true, usedRecordDetailParcel.Marshalling(parcel)); + + std::shared_ptr readedData(UsedRecordDetailParcel::Unmarshalling(parcel)); + EXPECT_EQ(true, readedData != nullptr); + + EXPECT_EQ(usedRecordDetailParcel.detail.status, readedData->detail.status); + EXPECT_EQ(usedRecordDetailParcel.detail.timestamp, readedData->detail.timestamp); + EXPECT_EQ(usedRecordDetailParcel.detail.accessDuration, readedData->detail.accessDuration); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/common/database/test/BUILD.gn b/services/common/database/test/BUILD.gn index 7125ad43a8d8ecfaa1c3d468e72c4e2a4fa7b9b2..3980e870e20dcbcddf152459192b44771b234acb 100644 --- a/services/common/database/test/BUILD.gn +++ b/services/common/database/test/BUILD.gn @@ -13,7 +13,7 @@ import("//build/test.gni") -ohos_unittest("libdatabase_values_test") { +ohos_unittest("libdatabase_test") { subsystem_name = "security" part_name = "access_token" module_out_path = part_name + "/" + part_name @@ -26,7 +26,7 @@ ohos_unittest("libdatabase_values_test") { "//base/security/access_token/services/accesstokenmanager/main/cpp/include/database", ] - sources = [ "unittest/src/generic_values_test.cpp" ] + sources = [ "unittest/database_test.cpp" ] configs = [ "//base/security/access_token/config:coverage_flags" ] @@ -44,5 +44,5 @@ ohos_unittest("libdatabase_values_test") { group("unittest") { testonly = true - deps = [ ":libdatabase_values_test" ] + deps = [ ":libdatabase_test" ] } diff --git a/services/common/database/test/unittest/src/generic_values_test.cpp b/services/common/database/test/unittest/database_test.cpp similarity index 93% rename from services/common/database/test/unittest/src/generic_values_test.cpp rename to services/common/database/test/unittest/database_test.cpp index 777a23721649d833c3d1059bb68e61d2c2fb3822..e3991710071e08cb4137d5dd7fd33514d472c64c 100644 --- a/services/common/database/test/unittest/src/generic_values_test.cpp +++ b/services/common/database/test/unittest/database_test.cpp @@ -1,175 +1,171 @@ -/* - * 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 "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 ROLLBACK_TRANSACTION_RESULT_ABNORMAL = -1; -static constexpr int32_t EXECUTESQL_RESULT_ABNORMAL = -1; -static const int32_t DEFAULT_VALUE = -1; -} // 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, ROLLBACK_TRANSACTION_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(); - EXPECT_EQ(SqliteStorage::GetInstance().RollbackTransaction(), ROLLBACK_TRANSACTION_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) -{ - std::string testSql = "test"; - EXPECT_EQ(SqliteStorage::GetInstance().ExecuteSql(testSql), 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) -{ - std::string testSql = "test"; - SqliteStorage::GetInstance().Close(); - EXPECT_EQ(SqliteStorage::GetInstance().ExecuteSql(testSql), 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) -{ - int64_t testValue = 1; - VariantValue Test(testValue); - EXPECT_EQ(Test.GetInt64(), 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); - EXPECT_EQ(DEFAULT_VALUE, Ntest.GetInt64()); - int64_t testValue = 1; - VariantValue Test(testValue); - EXPECT_EQ(DEFAULT_VALUE, Test.GetInt()); -} -} // namespace AccessToken -} // namespace Security +/* + * 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 "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 ROLLBACK_TRANSACTION_RESULT_ABNORMAL = -1; +static constexpr int32_t EXECUTESQL_RESULT_ABNORMAL = -1; +static const int32_t DEFAULT_VALUE = -1; +} // namespace +class DatabaseTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DatabaseTest::SetUpTestCase() {} +void DatabaseTest::TearDownTestCase() {} +void DatabaseTest::SetUp() {} +void DatabaseTest::TearDown() {} + +/** + * @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, ROLLBACK_TRANSACTION_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(); + EXPECT_EQ(SqliteStorage::GetInstance().RollbackTransaction(), ROLLBACK_TRANSACTION_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) +{ + std::string testSql = "test"; + EXPECT_EQ(SqliteStorage::GetInstance().ExecuteSql(testSql), 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) +{ + std::string testSql = "test"; + SqliteStorage::GetInstance().Close(); + EXPECT_EQ(SqliteStorage::GetInstance().ExecuteSql(testSql), 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) +{ + int64_t testValue = 1; + VariantValue Test(testValue); + EXPECT_EQ(Test.GetInt64(), 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); + EXPECT_EQ(DEFAULT_VALUE, Ntest.GetInt64()); + int64_t testValue = 1; + VariantValue Test(testValue); + EXPECT_EQ(DEFAULT_VALUE, Test.GetInt()); +} +} // namespace AccessToken +} // namespace Security } // namespace OHOS \ No newline at end of file