From 983ee2a24a075a7e3d9029a0b8e71d372ef61c3b Mon Sep 17 00:00:00 2001 From: Bobie Date: Tue, 9 May 2023 20:03:42 +0800 Subject: [PATCH 1/6] add json param check Signed-off-by: Bobie --- .../audiomanager/managersource/src/daudio_source_dev.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/audiomanager/managersource/src/daudio_source_dev.cpp b/services/audiomanager/managersource/src/daudio_source_dev.cpp index 6fe25ea2..93e81951 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -92,7 +92,10 @@ int32_t DAudioSourceDev::EnableDAudio(const std::string &dhId, const std::string DHLOGE("Task queue is null."); return ERR_DH_AUDIO_NULLPTR; } - + if (devId_.empty() || dhId.empty()) { + DHLOGE("Json param is incorrect."); + return ERR_DH_AUDIO_FAILED; + } json jParam = { { KEY_DEV_ID, devId_ }, { KEY_DH_ID, dhId }, { KEY_ATTRS, attrs } }; auto task = GenerateTask(this, &DAudioSourceDev::TaskEnableDAudio, jParam.dump(), "", &DAudioSourceDev::OnEnableTaskResult); -- Gitee From 94cdce0c8a34d9e12d79ce8a6b7322e98e7416d4 Mon Sep 17 00:00:00 2001 From: Bobie Date: Wed, 10 May 2023 14:05:42 +0800 Subject: [PATCH 2/6] add json param check Signed-off-by: Bobie --- .../managersource/src/daudio_source_dev.cpp | 5 +---- .../src/daudio_source_manager.cpp | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/services/audiomanager/managersource/src/daudio_source_dev.cpp b/services/audiomanager/managersource/src/daudio_source_dev.cpp index 93e81951..6fe25ea2 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -92,10 +92,7 @@ int32_t DAudioSourceDev::EnableDAudio(const std::string &dhId, const std::string DHLOGE("Task queue is null."); return ERR_DH_AUDIO_NULLPTR; } - if (devId_.empty() || dhId.empty()) { - DHLOGE("Json param is incorrect."); - return ERR_DH_AUDIO_FAILED; - } + json jParam = { { KEY_DEV_ID, devId_ }, { KEY_DH_ID, dhId }, { KEY_ATTRS, attrs } }; auto task = GenerateTask(this, &DAudioSourceDev::TaskEnableDAudio, jParam.dump(), "", &DAudioSourceDev::OnEnableTaskResult); diff --git a/services/audiomanager/managersource/src/daudio_source_manager.cpp b/services/audiomanager/managersource/src/daudio_source_manager.cpp index de960d55..efc45c4e 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,27 @@ int32_t DAudioSourceManager::UnInit() return DH_SUCCESS; } +static bool CheckEnableParams(const std::string &devId, const std::string &dhId, const std::string &attrs) +{ + DHLOGI("Checking oarams of daudio."); + if (devId.empty() || dhId.empty() || attrs.empty()) { + return false; + } + if (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 (!CheckEnableParams(devId, dhId, attrs)) { + DHLOGE("Enable params are incorrect."); + return ERR_DH_AUDIO_FAILED; + } std::lock_guard lock(devMapMtx_); auto dev = audioDevMap_.find(devId); if (dev == audioDevMap_.end()) { -- Gitee From f3338f32c7e3985a8d52ee010d401da85cbbe636 Mon Sep 17 00:00:00 2001 From: Bobie Date: Wed, 10 May 2023 15:01:02 +0800 Subject: [PATCH 3/6] add UT Signed-off-by: Bobie --- .../src/daudio_source_mgr_test.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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 297a9238..c3379e52 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. -- Gitee From 32423cda9499a459c67321bbcf430570e26563a4 Mon Sep 17 00:00:00 2001 From: Bobie Date: Wed, 10 May 2023 15:16:38 +0800 Subject: [PATCH 4/6] add UT Signed-off-by: Bobie --- .../unittest/sourcemanager/src/daudio_source_mgr_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 c3379e52..a01f9fef 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 @@ -136,13 +136,13 @@ HWTEST_F(DAudioSourceMgrTest, EnableDAudio_004, TestSize.Level1) 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"); + 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"); + 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 = ""; -- Gitee From 9931a2331d0fd847424a7d1b15cd348f63cd8aa6 Mon Sep 17 00:00:00 2001 From: Bobie Date: Wed, 10 May 2023 15:58:38 +0800 Subject: [PATCH 5/6] add json param check Signed-off-by: Bobie --- .../managersource/src/daudio_source_manager.cpp | 14 ++++++++------ .../sourcemanager/src/daudio_source_mgr_test.cpp | 9 +++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/services/audiomanager/managersource/src/daudio_source_manager.cpp b/services/audiomanager/managersource/src/daudio_source_manager.cpp index efc45c4e..4502de7b 100644 --- a/services/audiomanager/managersource/src/daudio_source_manager.cpp +++ b/services/audiomanager/managersource/src/daudio_source_manager.cpp @@ -94,13 +94,11 @@ int32_t DAudioSourceManager::UnInit() return DH_SUCCESS; } -static bool CheckEnableParams(const std::string &devId, const std::string &dhId, const std::string &attrs) +static bool CheckEnableParams(const std::string &devId, const std::string &dhId) { DHLOGI("Checking oarams of daudio."); - if (devId.empty() || dhId.empty() || attrs.empty()) { - return false; - } - if (devId.size() > MAX_DEVICE_ID_LENGTH || dhId.size() > MAX_DISTRIBUTED_HAREWARE_ID_LENGTH) { + if (devId.empty() || dhId.empty() || + devId.size() > MAX_DEVICE_ID_LENGTH || dhId.size() > MAX_DISTRIBUTED_HAREWARE_ID_LENGTH) { return false; } return true; @@ -111,7 +109,7 @@ int32_t DAudioSourceManager::EnableDAudio(const std::string &devId, const std::s { 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 (!CheckEnableParams(devId, dhId, attrs)) { + if (!CheckEnableParams(devId, dhId) || attrs.empty()) { DHLOGE("Enable params are incorrect."); return ERR_DH_AUDIO_FAILED; } @@ -130,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 (!CheckEnableParams(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 a01f9fef..0a0c6ac3 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 @@ -160,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()); } -- Gitee From 4e46e7356e3d8ce499758e50ccf79f83de0adb9a Mon Sep 17 00:00:00 2001 From: Bobie Date: Wed, 10 May 2023 17:18:54 +0800 Subject: [PATCH 6/6] add json param check Signed-off-by: Bobie --- .../managersource/src/daudio_source_manager.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/audiomanager/managersource/src/daudio_source_manager.cpp b/services/audiomanager/managersource/src/daudio_source_manager.cpp index 4502de7b..3b142a54 100644 --- a/services/audiomanager/managersource/src/daudio_source_manager.cpp +++ b/services/audiomanager/managersource/src/daudio_source_manager.cpp @@ -94,7 +94,7 @@ int32_t DAudioSourceManager::UnInit() return DH_SUCCESS; } -static bool CheckEnableParams(const std::string &devId, const std::string &dhId) +static bool CheckParams(const std::string &devId, const std::string &dhId) { DHLOGI("Checking oarams of daudio."); if (devId.empty() || dhId.empty() || @@ -109,7 +109,7 @@ int32_t DAudioSourceManager::EnableDAudio(const std::string &devId, const std::s { 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 (!CheckEnableParams(devId, dhId) || attrs.empty()) { + if (!CheckParams(devId, dhId) || attrs.empty()) { DHLOGE("Enable params are incorrect."); return ERR_DH_AUDIO_FAILED; } @@ -128,7 +128,7 @@ 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 (!CheckEnableParams(devId, dhId)) { + if (!CheckParams(devId, dhId)) { DHLOGE("Enable params are incorrect."); return ERR_DH_AUDIO_FAILED; } -- Gitee