!770 fix : sensint the death of the caller procees

Merge pull request !770 from 何娇/master
This commit is contained in:
openharmony_ci 2024-03-20 02:25:05 +00:00 committed by Gitee
commit f975a7f61e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 306 additions and 2 deletions

View File

@ -33,10 +33,12 @@ class ContextAppStateObserverManager {
public:
ContextAppStateObserverManager() = default;
~ContextAppStateObserverManager() = default;
void SubscribeAppState(std::shared_ptr<ContextCallback> &callback, uint64_t contextId);
void SubscribeAppState(const std::shared_ptr<ContextCallback> &callback, const uint64_t contextId);
void UnSubscribeAppState();
protected:
sptr<ApplicationStateObserverStub> appStateObserver_ = nullptr;
private:
sptr<IAppMgr> GetAppManagerInstance();
};
@ -48,6 +50,8 @@ class ContextAppStateObserver : public ApplicationStateObserverStub {
void OnAppStateChanged(const AppStateData &appStateData) override;
void OnForegroundApplicationChanged(const AppStateData &appStateData) override;
void OnAbilityStateChanged(const AbilityStateData &abilityStateData) override;
void OnProcessDied(const ProcessData &processData) override;
protected:
void ProcAppStateChanged();
const uint64_t contextId_;

View File

@ -46,7 +46,8 @@ sptr<IAppMgr> ContextAppStateObserverManager::GetAppManagerInstance()
return iface_cast<IAppMgr>(object);
}
void ContextAppStateObserverManager::SubscribeAppState(std::shared_ptr<ContextCallback> &callback, uint64_t contextId)
void ContextAppStateObserverManager::SubscribeAppState(const std::shared_ptr<ContextCallback> &callback,
const uint64_t contextId)
{
IAM_LOGI("start");
IF_FALSE_LOGE_AND_RETURN(callback != nullptr);
@ -167,6 +168,18 @@ void ContextAppStateObserver::OnAbilityStateChanged(const AbilityStateData &abil
}
return;
}
void ContextAppStateObserver::OnProcessDied(const ProcessData &processData)
{
IAM_LOGI("start, contextId: ****%{public}hx", static_cast<uint16_t>(contextId_));
auto bundleName = processData.bundleName;
IAM_LOGI("OnProcessDied, bundleName:%{public}s", bundleName.c_str());
if (bundleName.compare(bundleName_) == 0) {
ProcAppStateChanged();
}
return;
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS

View File

@ -92,6 +92,7 @@ ohos_unittest("iam_services_test") {
"src/authentication_impl_test.cpp",
"src/co_auth_service_test.cpp",
"src/co_auth_stub_test.cpp",
"src/context_appstate_observer_test.cpp",
"src/context_callback_impl_test.cpp",
"src/context_factory_test.cpp",
"src/context_pool_test.cpp",
@ -145,6 +146,8 @@ ohos_unittest("iam_services_test") {
"ability_runtime:app_manager",
"ability_runtime:extension_manager",
"access_token:libaccesstoken_sdk",
"access_token:libnativetoken",
"access_token:libtoken_setproc",
"access_token:libtokenid_sdk",
"c_utils:utils",
"drivers_interface_user_auth:libuser_auth_proxy_1.3",

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 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.
*/
#ifndef IAM_CONTEXT_APPSTATE_OBSERVER_TEST_H
#define IAM_CONTEXT_APPSTATE_OBSERVER_TEST_H
#include <gtest/gtest.h>
namespace OHOS {
namespace UserIam {
namespace UserAuth {
class ContextAppStateObserverTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp() override;
void TearDown() override;
void MockNativePermission();
uint64_t tokenId_;
};
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS
#endif // IAM_CONTEXT_APPSTATE_OBSERVER_TEST_H

View File

@ -0,0 +1,248 @@
/*
* Copyright (C) 2024 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 "context_appstate_observer_test.h"
#include <gtest/gtest.h>
#include "accesstoken_kit.h"
#include "app_state_data.h"
#include "context_appstate_observer.h"
#include "mock_context.h"
#include "nativetoken_kit.h"
#include "token_setproc.h"
namespace OHOS {
namespace UserIam {
namespace UserAuth {
using namespace std;
using namespace testing;
using namespace testing::ext;
const std::string ACCESS_AUTH_RESPOOL = "ohos.permission.ACCESS_AUTH_RESPOOL";
const std::string ACROSS_LOCAL_ACCOUNTS_EXTENSION = "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION";
const std::string MANAGE_LOCAL_ACCOUNTS = "ohos.permission.MANAGE_LOCAL_ACCOUNTS";
const std::string VIBRATE = "ohos.permission.VIBRATE";
const std::string GET_RUNNING_INFO = "ohos.permission.GET_RUNNING_INFO";
const std::string START_SYSTEM_DIALOG = "ohos.permission.START_SYSTEM_DIALOG";
const std::string RECEIVER_STARTUP_COMPLETED = "ohos.permission.RECEIVER_STARTUP_COMPLETED";
const std::string RUNNING_STATE_OBSERVER = "ohos.permission.RUNNING_STATE_OBSERVER";
const int32_t LOCATION_PERM_NUM = 8;
void ContextAppStateObserverTest::SetUpTestCase()
{
}
void ContextAppStateObserverTest::TearDownTestCase()
{
}
void ContextAppStateObserverTest::SetUp()
{
MockNativePermission();
}
void ContextAppStateObserverTest::TearDown()
{
}
void ContextAppStateObserverTest::MockNativePermission()
{
const char *perms[] = {
ACCESS_AUTH_RESPOOL.c_str(),
ACROSS_LOCAL_ACCOUNTS_EXTENSION.c_str(),
MANAGE_LOCAL_ACCOUNTS.c_str(),
VIBRATE.c_str(),
GET_RUNNING_INFO.c_str(),
START_SYSTEM_DIALOG.c_str(),
RECEIVER_STARTUP_COMPLETED.c_str(),
RUNNING_STATE_OBSERVER.c_str(),
};
NativeTokenInfoParams infoInstance = {
.dcapsNum = 0,
.permsNum = LOCATION_PERM_NUM,
.aclsNum = 0,
.dcaps = nullptr,
.perms = perms,
.acls = nullptr,
.processName = "ContextAppStateObserverTest",
.aplStr = "system_basic",
};
tokenId_ = GetAccessTokenId(&infoInstance);
SetSelfTokenID(tokenId_);
Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
}
HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_001, TestSize.Level0)
{
std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
ASSERT_NE(contextCallback, nullptr);
uint64_t contextId = 1;
EXPECT_CALL(*contextCallback, GetCallerName())
.WillRepeatedly([]() {
return "com.homs.settings";
}
);
auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
ASSERT_NE(appStateObserverManager, nullptr);
appStateObserverManager->SubscribeAppState(contextCallback, contextId);
appStateObserverManager->UnSubscribeAppState();
}
HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_002, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
ASSERT_NE(appStateObserverManager, nullptr);
appStateObserverManager->SubscribeAppState(nullptr, contextId);
}
HWTEST_F(ContextAppStateObserverTest, SubscribeAppStateTest_003, TestSize.Level0)
{
std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
ASSERT_NE(contextCallback, nullptr);
uint64_t contextId = 1;
auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
ASSERT_NE(appStateObserverManager, nullptr);
appStateObserverManager->SubscribeAppState(contextCallback, contextId);
}
HWTEST_F(ContextAppStateObserverTest, UnSubscribeAppStateTest_001, TestSize.Level0)
{
auto appStateObserverManager = Common::MakeShared<ContextAppStateObserverManager>();
ASSERT_NE(appStateObserverManager, nullptr);
appStateObserverManager->UnSubscribeAppState();
}
HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_001, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
ASSERT_NE(appStateObserver, nullptr);
AppStateData appStateData;
appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
appStateData.bundleName = "com.homs.settings";
appStateObserver->OnAppStateChanged(appStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_002, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
ASSERT_NE(appStateObserver, nullptr);
AppStateData appStateData;
appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND);
appStateData.bundleName = "com.homs.settings";
appStateObserver->OnAppStateChanged(appStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnAppStateChangedTest_003, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
ASSERT_NE(appStateObserver, nullptr);
AppStateData appStateData;
appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
appStateData.bundleName = "com.homs.settings";
appStateObserver->OnAppStateChanged(appStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_001, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
ASSERT_NE(appStateObserver, nullptr);
AppStateData appStateData;
appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
appStateData.bundleName = "com.homs.settings";
appStateObserver->OnForegroundApplicationChanged(appStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_002, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
ASSERT_NE(appStateObserver, nullptr);
AppStateData appStateData;
appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND);
appStateData.bundleName = "com.homs.settings";
appStateObserver->OnForegroundApplicationChanged(appStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnForegroundApplicationChangedTest_003, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
ASSERT_NE(appStateObserver, nullptr);
AppStateData appStateData;
appStateData.state = static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND);
appStateData.bundleName = "com.homs.settings";
appStateObserver->OnForegroundApplicationChanged(appStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnAbilityStateChangedTest_001, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
ASSERT_NE(appStateObserver, nullptr);
AbilityStateData abilityStateData;
abilityStateData.abilityState = static_cast<int32_t>(AbilityState::ABILITY_STATE_BACKGROUND);
abilityStateData.bundleName = "com.homs.settings";
appStateObserver->OnAbilityStateChanged(abilityStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnAbilityStateChangedTest_002, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
ASSERT_NE(appStateObserver, nullptr);
AbilityStateData abilityStateData;
abilityStateData.abilityState = static_cast<int32_t>(AbilityState::ABILITY_STATE_FOREGROUND);
abilityStateData.bundleName = "com.homs.settings";
appStateObserver->OnAbilityStateChanged(abilityStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnAbilityStateChangedTest_003, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
ASSERT_NE(appStateObserver, nullptr);
AbilityStateData abilityStateData;
abilityStateData.abilityState = static_cast<int32_t>(AbilityState::ABILITY_STATE_BACKGROUND);
abilityStateData.bundleName = "com.homs.settings";
appStateObserver->OnAbilityStateChanged(abilityStateData);
}
HWTEST_F(ContextAppStateObserverTest, OnProcessDiedTest_001, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.settings");
ASSERT_NE(appStateObserver, nullptr);
ProcessData processData;
processData.bundleName = "com.homs.settings";
appStateObserver->OnProcessDied(processData);
}
HWTEST_F(ContextAppStateObserverTest, OnProcessDiedTest_002, TestSize.Level0)
{
uint64_t contextId = 1;
auto appStateObserver = new (std::nothrow) ContextAppStateObserver(contextId, "com.homs.setting");
ASSERT_NE(appStateObserver, nullptr);
ProcessData processData;
processData.bundleName = "com.homs.settings";
appStateObserver->OnProcessDied(processData);
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS