!705 Feature:sink端广播来源校验

Merge pull request !705 from zhx/master
This commit is contained in:
openharmony_ci 2023-09-27 02:12:45 +00:00 committed by Gitee
commit 95636dcc58
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 117 additions and 14 deletions

View File

@ -38,6 +38,12 @@ struct currentMissionInfo {
int32_t currentMissionId;
bool currentIsContinuable;
};
struct currentIconInfo {
std::string senderNetworkId;
std::string bundleName;
};
class DistributedSchedContinueManager {
DECLARE_SINGLE_INSTANCE(DistributedSchedContinueManager);
@ -63,7 +69,7 @@ public:
int32_t RegisterOffListener(const std::string& type, const sptr<IRemoteObject>& obj);
int32_t GetMissionId(const std::string& bundleName, int32_t& missionId);
void NotifyDeid(const sptr<IRemoteObject>& obj);
int32_t SetMissionContinueState(const int32_t missionId, const AAFwk::ContinueState &state);
int32_t SetMissionContinueState(const int32_t missionId, const AAFwk::ContinueState& state);
private:
void AddCancelMissionFocusedTimer(const int32_t missionId);
@ -71,15 +77,17 @@ private:
void StartEvent();
int32_t DealFocusedBusiness(const int32_t missionId);
int32_t DealUnfocusedBusiness(const int32_t missionId, bool isUnfocused);
int32_t DealUnBroadcastdBusiness(std::string& senderNetworkId, uint32_t accessTokenId, const int32_t state);
int32_t VerifyBroadcastSource(std::string& senderNetworkId, std::string& bundleName, const int32_t state);
int32_t DealOnBroadcastBusiness(std::string& senderNetworkId, uint32_t accessTokenId, const int32_t state);
void NotifyRecvBroadcast(const sptr<IRemoteObject>& obj, const std::string& networkId,
const std::string& bundleName, const int32_t state);
int32_t GetBundleName(const int32_t missionId, std::string& bundleName);
bool IsContinue(const int32_t& missionId, const std::string& bundleName);
int32_t DealSetMissionContinueStateBusiness(const int32_t missionId, const AAFwk::ContinueState &state);
int32_t DealSetMissionContinueStateBusiness(const int32_t missionId, const AAFwk::ContinueState& state);
int32_t CheckContinueState(const int32_t missionId);
private:
currentMissionInfo info_ = { INVALID_MISSION_ID, false };
currentIconInfo iconInfo_;
sptr<DistributedMissionFocusedListener> missionFocusedListener_;
sptr<DistributedMissionDiedListener> missionDiedListener_;
std::string onType_;
@ -88,6 +96,7 @@ private:
std::thread eventThread_;
std::condition_variable eventCon_;
std::mutex eventMutex_;
std::mutex iconMutex_;
std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_;
};
} // namespace DistributedSchedule

View File

@ -146,7 +146,7 @@ void DistributedSchedContinueManager::NotifyDataRecv(std::string& senderNetworkI
state = INACTIVE;
}
auto feedfunc = [this, senderNetworkId, accessTokenId, state]() mutable {
DealUnBroadcastdBusiness(senderNetworkId, accessTokenId, state);
DealOnBroadcastBusiness(senderNetworkId, accessTokenId, state);
};
if (eventHandler_ != nullptr) {
eventHandler_->PostTask(feedfunc);
@ -364,10 +364,36 @@ int32_t DistributedSchedContinueManager::DealUnfocusedBusiness(const int32_t mis
return ERR_OK;
}
int32_t DistributedSchedContinueManager::DealUnBroadcastdBusiness(std::string& senderNetworkId,
int32_t DistributedSchedContinueManager::VerifyBroadcastSource(std::string& senderNetworkId, std::string& bundleName,
const int32_t state)
{
std::lock_guard<std::mutex> currentIconLock(iconMutex_);
if (state == ACTIVE) {
iconInfo_.senderNetworkId = senderNetworkId;
iconInfo_.bundleName = bundleName;
} else {
if (senderNetworkId != iconInfo_.senderNetworkId) {
HILOGE("Sender not match, task abort. senderNetworkId: %{public}s, saved NetworkId: %{public}s",
DnetworkAdapter::AnonymizeNetworkId(senderNetworkId).c_str(),
DnetworkAdapter::AnonymizeNetworkId(iconInfo_.senderNetworkId).c_str());
return INVALID_PARAMETERS_ERR;
}
if (bundleName != iconInfo_.bundleName) {
HILOGE("BundleName not match, task abort. bundleName: %{public}s, saved bundleName: %{public}s",
bundleName.c_str(), iconInfo_.bundleName.c_str());
return INVALID_PARAMETERS_ERR;
}
iconInfo_.senderNetworkId = "";
iconInfo_.bundleName = "";
}
return ERR_OK;
}
int32_t DistributedSchedContinueManager::DealOnBroadcastBusiness(std::string& senderNetworkId,
uint32_t accessTokenId, const int32_t state)
{
HILOGI("DealUnBroadcastdBusiness start, senderNetworkId: %{public}s, accessTokenId: %{public}d, state: %{public}d",
HILOGI("DealOnBroadcastBusiness start, senderNetworkId: %{public}s, accessTokenId: %{public}d, state: %{public}d",
DnetworkAdapter::AnonymizeNetworkId(senderNetworkId).c_str(), accessTokenId, state);
std::string bundleName;
int32_t ret = BundleManagerInternal::GetBundleNameFromDbms(senderNetworkId, accessTokenId, bundleName);
@ -386,6 +412,10 @@ int32_t DistributedSchedContinueManager::DealUnBroadcastdBusiness(std::string& s
HILOGE("The bundleType must be app, but it is %{public}d", localBundleInfo.applicationInfo.bundleType);
return INVALID_PARAMETERS_ERR;
}
ret = VerifyBroadcastSource(senderNetworkId, bundleName, state);
if (ret != ERR_OK) {
return ret;
}
std::lock_guard<std::mutex> registerOnListenerMapLock(eventMutex_);
auto iterItem = registerOnListener_.find(onType_);
if (iterItem == registerOnListener_.end()) {
@ -396,7 +426,7 @@ int32_t DistributedSchedContinueManager::DealUnBroadcastdBusiness(std::string& s
for (auto iter : objs) {
NotifyRecvBroadcast(iter, senderNetworkId, bundleName, state);
}
HILOGI("DealUnBroadcastdBusiness end");
HILOGI("DealOnBroadcastBusiness end");
return ERR_OK;
}

View File

@ -32,6 +32,8 @@ const std::string BUNDLENAME_01 = "bundleName01";
const std::string BUNDLENAME_02 = "bundleName02";
constexpr int32_t MISSIONID_01 = 1;
constexpr int32_t MISSIONID_02 = 2;
constexpr int32_t ACTIVE = 0;
constexpr int32_t INACTIVE = 1;
}
void DMSContinueManagerTest::SetUpTestCase()
@ -307,22 +309,84 @@ HWTEST_F(DMSContinueManagerTest, testDealUnfocusedBusiness001, TestSize.Level3)
}
/**
* @tc.name: testDealUnBroadcastdBusiness001
* @tc.desc: test DealUnBroadcastdBusiness.
* @tc.name: testVerifyBroadcastSource001
* @tc.desc: test testVerifyBroadcastSource001.
* @tc.type: FUNC
*/
HWTEST_F(DMSContinueManagerTest, testVerifyBroadcastSource001, TestSize.Level3)
{
DTEST_LOG << "DMSContinueManagerTest testVerifyBroadcastSource001 start" << std::endl;
int32_t state = ACTIVE;
std::string networkId = "test networkId";
std::string bundleName = "test bundleName";
int32_t ret = DistributedSchedContinueManager::GetInstance().VerifyBroadcastSource(networkId, bundleName, state);
EXPECT_EQ(ret, ERR_OK);
state = INACTIVE;
ret = DistributedSchedContinueManager::GetInstance().VerifyBroadcastSource(networkId, bundleName, state);
EXPECT_EQ(ret, ERR_OK);
}
/**
* @tc.name: testVerifyBroadcastSource002
* @tc.desc: test testVerifyBroadcastSource002.
* @tc.type: FUNC
*/
HWTEST_F(DMSContinueManagerTest, testVerifyBroadcastSource002, TestSize.Level3)
{
DTEST_LOG << "DMSContinueManagerTest testVerifyBroadcastSource002 start" << std::endl;
int32_t state = ACTIVE;
std::string networkId = "test networkId";
std::string bundleName = "test bundleName";
int32_t ret = DistributedSchedContinueManager::GetInstance().VerifyBroadcastSource(networkId, bundleName, state);
EXPECT_EQ(ret, ERR_OK);
state = INACTIVE;
networkId = "invalid networkId";
ret = DistributedSchedContinueManager::GetInstance().VerifyBroadcastSource(networkId, bundleName, state);
EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
}
/**
* @tc.name: testVerifyBroadcastSource003
* @tc.desc: test testVerifyBroadcastSource003.
* @tc.type: FUNC
*/
HWTEST_F(DMSContinueManagerTest, testVerifyBroadcastSource003, TestSize.Level3)
{
DTEST_LOG << "DMSContinueManagerTest testVerifyBroadcastSource003 start" << std::endl;
int32_t state = ACTIVE;
std::string networkId = "test networkId";
std::string bundleName = "test bundleName";
int32_t ret = DistributedSchedContinueManager::GetInstance().VerifyBroadcastSource(networkId, bundleName, state);
EXPECT_EQ(ret, ERR_OK);
state = INACTIVE;
bundleName = "invalid bundleName";
ret = DistributedSchedContinueManager::GetInstance().VerifyBroadcastSource(networkId, bundleName, state);
EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
}
/**
* @tc.name: testDealOnBroadcastBusiness001
* @tc.desc: test DealOnBroadcastBusiness.
* @tc.type: FUNC
* @tc.require: I7F8KH
*/
HWTEST_F(DMSContinueManagerTest, testDealUnBroadcastdBusiness001, TestSize.Level3)
HWTEST_F(DMSContinueManagerTest, testDealOnBroadcastBusiness001, TestSize.Level3)
{
DTEST_LOG << "DMSContinueManagerTest testDealUnBroadcastdBusiness001 start" << std::endl;
DTEST_LOG << "DMSContinueManagerTest testDealOnBroadcastBusiness001 start" << std::endl;
/**
* @tc.steps: step1. test DealUnBroadcastdBusiness when senderNetworkId is invalid;
* @tc.steps: step1. test DealOnBroadcastBusiness when senderNetworkId is invalid;
*/
std::string senderNetworkId = "invalid senderNetworkId";
uint32_t accessTokenId = 0;
int32_t state = 0;
int32_t ret = DistributedSchedContinueManager::GetInstance().DealUnBroadcastdBusiness(
int32_t ret = DistributedSchedContinueManager::GetInstance().DealOnBroadcastBusiness(
senderNetworkId, accessTokenId, state);
EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
@ -340,7 +404,7 @@ HWTEST_F(DMSContinueManagerTest, testDealUnBroadcastdBusiness001, TestSize.Level
obj = new RemoteOnListenerStubTest();
DistributedSchedContinueManager::GetInstance().NotifyRecvBroadcast(obj, networkId, bundleName, state);
DTEST_LOG << "DMSContinueManagerTest testDealUnBroadcastdBusiness001 end" << std::endl;
DTEST_LOG << "DMSContinueManagerTest testDealOnBroadcastBusiness001 end" << std::endl;
}
/**