diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 64ce9ff50e58b2740ec51683a83809a58d93b5d6..2e89e572c11e8bc46afa26b59bcf756b7fcdb6ce 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -1600,6 +1600,10 @@ std::vector CloudServiceImpl::ConvertCursor(std::shared CloudServiceImpl::HapInfo CloudServiceImpl::GetHapInfo(uint32_t tokenId) { + if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { + ZLOGE("TokenType is not TOKEN_HAP, tokenId:0x%{public}x", tokenId); + return { 0, 0, ""}; + } HapTokenInfo tokenInfo; int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); if (errCode != RET_SUCCESS) { diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 8abb69bbd2916d8c02ad8d850a82601e879fa026..66d35fd0c24e758978cf8d3906a190ad23e1ae9d 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -1455,6 +1455,10 @@ int32_t ObjectStoreManager::BindAsset(const uint32_t tokenId, const std::string& return true; }); + if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { + ZLOGE("TokenType is not TOKEN_HAP, token:0x%{public}x, bundleName:%{public}s", tokenId, appId.c_str()); + return GeneralError::E_ERROR; + } HapTokenInfo tokenInfo; auto status = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); if (status != RET_SUCCESS) { diff --git a/services/distributeddataservice/service/object/src/object_service_impl.cpp b/services/distributeddataservice/service/object/src/object_service_impl.cpp index 31d8232e0360295c74a5a0f3a9b418648a0c3e7e..ff6d5ee3af5e31e993f6ab2e0808f161c010aaba 100644 --- a/services/distributeddataservice/service/object/src/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/src/object_service_impl.cpp @@ -117,6 +117,10 @@ int32_t ObjectServiceImpl::BindAssetStore(const std::string &bundleName, const s int32_t ObjectServiceImpl::IsContinue(bool &result) { uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); + if (Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) != Security::AccessToken::TOKEN_HAP) { + ZLOGE("TokenType is not TOKEN_HAP, tokenId: %{public}u", tokenId); + return OBJECT_INNER_ERROR; + } Security::AccessToken::HapTokenInfo tokenInfo; auto status = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); if (status != 0) { diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 2f050490c0e2e16943098b1ecc9ef10444a1cba2..496f55ce891c849b798dcd285e2bd1d9cc356dc3 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -128,6 +128,7 @@ ohos_unittest("CloudDataMockTest") { "${data_service_path}/service/cloud/sync_manager.cpp", "${data_service_path}/service/cloud/sync_strategies/network_sync_strategy.cpp", "${data_service_path}/service/test/mock/checker_mock.cpp", + "mock/access_token_mock.cpp", "mock/account_delegate_mock.cpp", "cloud_data_mock_test.cpp", ] @@ -966,6 +967,7 @@ ohos_unittest("ObjectManagerMockTest") { "../object/src/object_service_stub.cpp", "../object/src/object_snapshot.cpp", "../object/src/object_types_utils.cpp", + "mock/access_token_mock.cpp", "mock/account_delegate_mock.cpp", "mock/device_manager_adapter_mock.cpp", "mock/kv_store_nb_delegate_mock.cpp", diff --git a/services/distributeddataservice/service/test/cloud_data_mock_test.cpp b/services/distributeddataservice/service/test/cloud_data_mock_test.cpp index 0e02096ddfd5fd53f166c903f8397a9416c65682..5f7e68b9da4d31b848c9918b60c254f2429eee94 100644 --- a/services/distributeddataservice/service/test/cloud_data_mock_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_mock_test.cpp @@ -25,6 +25,7 @@ #include "ipc_skeleton.h" #include "log_print.h" #include "metadata/meta_data_manager.h" +#include "mock/access_token_mock.h" #include "mock/account_delegate_mock.h" #include "mock/db_store_mock.h" #include "network_delegate_mock.h" @@ -33,6 +34,7 @@ using namespace testing::ext; using namespace testing; using namespace DistributedDB; using namespace OHOS::DistributedData; +using namespace OHOS::Security::AccessToken; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; namespace OHOS::Test { @@ -45,6 +47,7 @@ static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_1 = "test_cloud_database_ static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_2 = "test_cloud_database_alias_2"; static constexpr const char *TEST_CLOUD_PATH = "/data/app/el2/100/database/test_cloud_bundleName/entry/rdb/" "test_cloud_store"; +static constexpr const int32_t TEST_TOKEN_FLAG_CALL_COUNT = 3; class CloudDataMockTest : public testing::Test { public: static void SetUpTestCase(void); @@ -52,6 +55,7 @@ public: void SetUp(); void TearDown(); + static inline std::shared_ptr accTokenMock = nullptr; static SchemaMeta schemaMeta_; static std::shared_ptr cloudServiceImpl_; @@ -185,6 +189,13 @@ void CloudDataMockTest::SetUpTestCase(void) } // 2 means that the GetUserByToken interface will be called twice EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).Times(2).WillRepeatedly(Return(0)); + + accTokenMock = std::make_shared(); + BAccessTokenKit::accessTokenkit = accTokenMock; + EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(_)) + .Times(TEST_TOKEN_FLAG_CALL_COUNT) + .WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP)); + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); MetaDataManager::GetInstance().SetSyncer( [](const auto &, auto) { DeviceMatrix::GetInstance().OnChanged(DeviceMatrix::META_STORE_MASK); }); @@ -212,6 +223,8 @@ void CloudDataMockTest::TearDownTestCase() delete accountDelegateMock; accountDelegateMock = nullptr; } + accTokenMock = nullptr; + BAccessTokenKit::accessTokenkit = nullptr; } void CloudDataMockTest::SetUp() @@ -327,6 +340,7 @@ HWTEST_F(CloudDataMockTest, OnReadyTest_LoginAccount, TestSize.Level0) { ZLOGI("CloudDataMockTest OnReadyTest_LoginAccount start"); std::string device = "test"; + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillRepeatedly(testing::Return(false)); auto ret = cloudServiceImpl_->OnReady(device); EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); @@ -339,5 +353,51 @@ HWTEST_F(CloudDataMockTest, OnReadyTest_LoginAccount, TestSize.Level0) EXPECT_EQ(ret, CloudData::CloudService::SUCCESS); ZLOGI("CloudDataMockTest OnReadyTest_LoginAccount end"); } + + +/** +* @tc.name: GetHapInfo001 +* @tc.desc: Test GetHapInfo function when GetTokenTypeFlag is not TOKEN_HAP. +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataMockTest, GetHapInfo001, TestSize.Level1) +{ + ZLOGI("CloudDataMockTest GetHapInfo001 start"); + DistributedRdb::PredicatesMemo predicates; + predicates.tables_.push_back(TEST_CLOUD_BUNDLE); + std::vector columns; + CloudData::Participants participants; + EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(_)) + .Times(1) + .WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE)); + auto [ret, _] = cloudServiceImpl_->AllocResourceAndShare(TEST_CLOUD_STORE, predicates, columns, participants); + EXPECT_EQ(ret, E_ERROR); + ZLOGI("CloudDataMockTest GetHapInfo001 end"); +} + +/** +* @tc.name: GetHapInfo002 +* @tc.desc: Test GetHapInfo function when GetTokenTypeFlag is TOKEN_HAP. +* @tc.type: FUNC +* @tc.require: + */ +HWTEST_F(CloudDataMockTest, GetHapInfo002, TestSize.Level1) +{ + ZLOGI("CloudDataMockTest GetHapInfo002 start"); + DistributedRdb::PredicatesMemo predicates; + predicates.tables_.push_back(TEST_CLOUD_BUNDLE); + std::vector columns; + CloudData::Participants participants; + EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(_)) + .Times(1) + .WillOnce(Return(ATokenTypeEnum::TOKEN_HAP)); + EXPECT_CALL(*accTokenMock, GetHapTokenInfo(_, _)) + .Times(1) + .WillOnce(Return(-1)); + auto [ret, _] = cloudServiceImpl_->AllocResourceAndShare(TEST_CLOUD_STORE, predicates, columns, participants); + EXPECT_EQ(ret, E_ERROR); + ZLOGI("CloudDataMockTest GetHapInfo002 end"); +} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/access_token_mock.cpp b/services/distributeddataservice/service/test/mock/access_token_mock.cpp index c0fe454a66674a2997c086ccba971fe1af022243..e3e5addd42222f635eccf12f433818298618295d 100644 --- a/services/distributeddataservice/service/test/mock/access_token_mock.cpp +++ b/services/distributeddataservice/service/test/mock/access_token_mock.cpp @@ -19,6 +19,9 @@ namespace Security { namespace AccessToken { ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) { + if (BAccessTokenKit::accessTokenkit == nullptr) { + return ATokenTypeEnum::TOKEN_INVALID; + } return BAccessTokenKit::accessTokenkit->GetTokenTypeFlag(tokenID); } diff --git a/services/distributeddataservice/service/test/object_manager_mock_test.cpp b/services/distributeddataservice/service/test/object_manager_mock_test.cpp index faf7ca79cb6d74afb3c7a792747562ee955ee7f1..988dbcdae72350e2009d43a56a41d13ac06c8748 100644 --- a/services/distributeddataservice/service/test/object_manager_mock_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_mock_test.cpp @@ -15,21 +15,26 @@ #define LOG_TAG "ObjectManagerMockTest" #include +#include #include "device_manager_adapter_mock.h" #include "device_matrix_mock.h" #include "gtest/gtest.h" +#include "mock/access_token_mock.h" #include "mock/account_delegate_mock.h" #include "mock/distributed_file_daemon_manager_mock.h" #include "mock/meta_data_manager_mock.h" #include "object_manager.h" +#include "object_service_impl.h" using namespace OHOS::DistributedObject; using namespace OHOS::DistributedData; using namespace OHOS::Storage::DistributedFile; +using namespace OHOS::Security::AccessToken; using namespace testing::ext; using namespace testing; +using AssetValue = OHOS::CommonType::AssetValue; using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo; using OnComplete = OHOS::DistributedData::MetaDataManager::OnComplete; @@ -54,6 +59,8 @@ public: AccountDelegate::instance_ = nullptr; AccountDelegate::RegisterAccountInstance(accountDelegateMock); } + accTokenMock = std::make_shared(); + BAccessTokenKit::accessTokenkit = accTokenMock; } static void TearDownTestCase(void) { @@ -71,6 +78,8 @@ public: delete accountDelegateMock; accountDelegateMock = nullptr; } + accTokenMock = nullptr; + BAccessTokenKit::accessTokenkit = nullptr; } static inline std::shared_ptr metaDataManagerMock = nullptr; @@ -79,8 +88,14 @@ public: static inline std::shared_ptr deviceMatrixMock = nullptr; static inline std::shared_ptr fileDaemonMgrMock = nullptr; static inline AccountDelegateMock *accountDelegateMock = nullptr; + static inline std::shared_ptr accTokenMock = nullptr; void SetUp() {}; void TearDown() {}; + +protected: + std::string sessionId_ = "123"; + OHOS::ObjectStore::AssetBindInfo assetBindInfo_; + AssetValue assetValue_; }; /** @@ -391,7 +406,7 @@ HWTEST_F(ObjectManagerMockTest, InitUserMeta001, TestSize.Level1) std::vector users; EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(false))); auto status = manager.InitUserMeta(); - ASSERT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); + EXPECT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); } /** @@ -410,7 +425,7 @@ HWTEST_F(ObjectManagerMockTest, InitUserMeta002, TestSize.Level1) .WillOnce( DoAll(SetArgReferee<0>(users), Invoke([](std::vector &users) { users.clear(); }), Return(true))); auto status = manager.InitUserMeta(); - ASSERT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); + EXPECT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); } /** @@ -428,7 +443,80 @@ HWTEST_F(ObjectManagerMockTest, InitUserMeta003, TestSize.Level1) std::vector users = { 0, 1 }; EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); auto status = manager.InitUserMeta(); - ASSERT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); + EXPECT_EQ(status, DistributedObject::OBJECT_INNER_ERROR); +} + +/** +* @tc.name: BindAsset001 +* @tc.desc: Test BindAsset function when GetTokenTypeFlag is not TOKEN_HAP. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerMockTest, BindAsset001, TestSize.Level1) +{ + auto &manager = ObjectStoreManager::GetInstance(); + std::string bundleName = "BindAsset"; + uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); + EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(_)) + .Times(1) + .WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE)); + auto result = manager.BindAsset(tokenId, bundleName, sessionId_, assetValue_, assetBindInfo_); + EXPECT_EQ(result, DistributedObject::OBJECT_DBSTATUS_ERROR); +} + +/** +* @tc.name: BindAsset002 +* @tc.desc: Test BindAsset function when GetTokenTypeFlag is TOKEN_HAP. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerMockTest, BindAsset002, TestSize.Level1) +{ + auto &manager = ObjectStoreManager::GetInstance(); + std::string bundleName = "BindAsset"; + uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); + EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(_)) + .Times(1) + .WillOnce(Return(ATokenTypeEnum::TOKEN_HAP)); + EXPECT_CALL(*accTokenMock, GetHapTokenInfo(_, _)) + .Times(1) + .WillOnce(Return(0)); + auto result = manager.BindAsset(tokenId, bundleName, sessionId_, assetValue_, assetBindInfo_); + EXPECT_EQ(result, DistributedObject::OBJECT_SUCCESS); +} + +/** +* @tc.name: IsContinue001 +* @tc.desc: Test IsContinue function when GetTokenTypeFlag is not TOKEN_HAP. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerMockTest, IsContinue001, TestSize.Level1) +{ + std::shared_ptr objectServiceImpl = std::make_shared(); + bool isContinue = false; + EXPECT_CALL(*fileDaemonMgrMock, UnRegisterAssetCallback(_)).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(_)) + .Times(1) + .WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE)); + auto ret = objectServiceImpl->IsContinue(isContinue); + EXPECT_EQ(ret, DistributedObject::OBJECT_INNER_ERROR); +} + +/** +* @tc.name: IsContinue002 +* @tc.desc: Test IsContinue function when GetTokenTypeFlag is TOKEN_HAP. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerMockTest, IsContinue002, TestSize.Level1) +{ + std::shared_ptr objectServiceImpl = std::make_shared(); + bool isContinue = false; + EXPECT_CALL(*accTokenMock, GetTokenTypeFlag(_)) + .Times(2) + .WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP)); + EXPECT_CALL(*accTokenMock, GetHapTokenInfo(_, _)) + .Times(1) + .WillOnce(Return(0)); + auto ret = objectServiceImpl->IsContinue(isContinue); + EXPECT_EQ(ret, DistributedObject::OBJECT_SUCCESS); } }; // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file