diff --git a/services/audiomanager/managersource/src/daudio_source_manager.cpp b/services/audiomanager/managersource/src/daudio_source_manager.cpp index de960d552703e26cbfc300e0c8d35a2b3297626e..3b142a543ebe2f3597ce307aec6979316dfbe22c 100644 --- a/services/audiomanager/managersource/src/daudio_source_manager.cpp +++ b/services/audiomanager/managersource/src/daudio_source_manager.cpp @@ -28,6 +28,11 @@ namespace OHOS { namespace DistributedHardware { +namespace { +constexpr uint32_t MAX_DEVICE_ID_LENGTH = 200; +constexpr uint32_t MAX_DISTRIBUTED_HAREWARE_ID_LENGTH = 100; +} + IMPLEMENT_SINGLE_INSTANCE(DAudioSourceManager); DAudioSourceManager::DAudioSourceManager() { @@ -89,11 +94,25 @@ int32_t DAudioSourceManager::UnInit() return DH_SUCCESS; } +static bool CheckParams(const std::string &devId, const std::string &dhId) +{ + DHLOGI("Checking oarams of daudio."); + if (devId.empty() || dhId.empty() || + devId.size() > MAX_DEVICE_ID_LENGTH || dhId.size() > MAX_DISTRIBUTED_HAREWARE_ID_LENGTH) { + return false; + } + return true; +} + int32_t DAudioSourceManager::EnableDAudio(const std::string &devId, const std::string &dhId, const std::string &version, const std::string &attrs, const std::string &reqId) { DHLOGI("Enable distributed audio, devId: %s, dhId: %s, version: %s, reqId: %s.", GetAnonyString(devId).c_str(), dhId.c_str(), version.c_str(), reqId.c_str()); + if (!CheckParams(devId, dhId) || attrs.empty()) { + DHLOGE("Enable params are incorrect."); + return ERR_DH_AUDIO_FAILED; + } std::lock_guard lock(devMapMtx_); auto dev = audioDevMap_.find(devId); if (dev == audioDevMap_.end()) { @@ -109,6 +128,10 @@ int32_t DAudioSourceManager::DisableDAudio(const std::string &devId, const std:: { DHLOGI("Disable distributed audio, devId: %s, dhId: %s, reqId: %s.", GetAnonyString(devId).c_str(), dhId.c_str(), reqId.c_str()); + if (!CheckParams(devId, dhId)) { + DHLOGE("Enable params are incorrect."); + return ERR_DH_AUDIO_FAILED; + } std::lock_guard lock(devMapMtx_); auto dev = audioDevMap_.find(devId); if (dev == audioDevMap_.end()) { diff --git a/services/audiomanager/test/unittest/sourcemanager/src/daudio_source_mgr_test.cpp b/services/audiomanager/test/unittest/sourcemanager/src/daudio_source_mgr_test.cpp index 297a923802342f977c3179e785bc0f8291b27f50..0a0c6ac3282c904eeb45093e907c37f54d5c20dd 100644 --- a/services/audiomanager/test/unittest/sourcemanager/src/daudio_source_mgr_test.cpp +++ b/services/audiomanager/test/unittest/sourcemanager/src/daudio_source_mgr_test.cpp @@ -124,6 +124,32 @@ HWTEST_F(DAudioSourceMgrTest, EnableDAudio_003, TestSize.Level1) EXPECT_EQ(DH_SUCCESS, sourceMgr.UnInit()); } +/** + * @tc.name: EnableDAudio_004 + * @tc.desc: Verify the EnableDAudio and DisableDAudio function. + * @tc.type: FUNC + * @tc.require: AR000H0E5F + */ +HWTEST_F(DAudioSourceMgrTest, EnableDAudio_004, TestSize.Level1) +{ + std::string reqId1 = GetRandomID(); + std::string dhId = ""; + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.EnableDAudio(DEV_ID, dhId, "", ATTRS, reqId1)); + EXPECT_EQ(DH_SUCCESS, sourceMgr.UnInit()); + dhId = std::string(105, '1'); + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.EnableDAudio(DEV_ID, dhId, "", ATTRS, reqId1)); + EXPECT_EQ(DH_SUCCESS, sourceMgr.UnInit()); + std::string devId = ""; + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.EnableDAudio(devId, DH_ID, "", ATTRS, reqId1)); + EXPECT_EQ(DH_SUCCESS, sourceMgr.UnInit()); + devId = std::string(205, 'a'); + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.EnableDAudio(devId, DH_ID, "", ATTRS, reqId1)); + EXPECT_EQ(DH_SUCCESS, sourceMgr.UnInit()); + std::string attrs = ""; + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.EnableDAudio(DEV_ID, DH_ID, "", attrs, reqId1)); + EXPECT_EQ(DH_SUCCESS, sourceMgr.UnInit()); +} + /** * @tc.name: DisableDAudio_001 * @tc.desc: Verify the DisableDAudio function. @@ -134,6 +160,15 @@ HWTEST_F(DAudioSourceMgrTest, DisableDAudio_001, TestSize.Level1) { std::string reqId1 = GetRandomID(); EXPECT_EQ(DH_SUCCESS, sourceMgr.EnableDAudio(DEV_ID, DH_ID, "", ATTRS, reqId1)); + + std::string dhId = ""; + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.DisableDAudio(DEV_ID, dhId, reqId1)); + dhId = std::string(105, '1'); + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.DisableDAudio(DEV_ID, dhId, reqId1)); + std::string devId = ""; + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.DisableDAudio(devId, DH_ID, reqId1)); + devId = std::string(205, 'a'); + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.DisableDAudio(devId, DH_ID, reqId1)); EXPECT_EQ(ERR_DH_AUDIO_SA_DEVICE_NOT_EXIST, sourceMgr.DisableDAudio("Unknown", DH_ID, reqId1)); EXPECT_EQ(DH_SUCCESS, sourceMgr.UnInit()); }