diff --git a/test/dslm_unit_test/BUILD.gn b/test/dslm_unit_test/BUILD.gn index da4680cee8bdb7b825f091beeb6b27bfed5c387b..ed44d921f23383292ebe4951692db4c9c942a9a7 100644 --- a/test/dslm_unit_test/BUILD.gn +++ b/test/dslm_unit_test/BUILD.gn @@ -23,7 +23,10 @@ group("dslm_test") { deps = [] if (os_level == "standard") { - deps += [ ":DslmUnitTest" ] + deps += [ + ":DslmUnitTest", + ":DslmUnitTest2", + ] } } @@ -114,4 +117,86 @@ if (os_level == "standard") { "huks:libhukssdk", ] } + + ohos_unittest("DslmUnitTest2") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + blocklist = "../../cfi_blocklist.txt" + } + branch_protector_ret = "pac_ret" + + install_enable = true + testonly = true + part_name = "device_security_level" + subsystem_name = "security" + module_out_path = "${part_name}/${target_name}" + + defines = [ "UT_ENABLE=1" ] + + sources = [ + "../../baselib/msglib/src/common/messenger.c", + "../../baselib/msglib/src/standard/messenger_device_socket_manager.c", + "../../baselib/msglib/src/standard/messenger_device_status_manager.cpp", + "../../baselib/msglib/src/standard/messenger_impl.c", + "../../baselib/msglib/src/utils/messenger_utils.c", + "../../baselib/utils/src/utils_work_queue.c", + "../../oem_property/ohos/standard/dslm_ohos_credential.c", + "../../services/dfx/dslm_bigdata.cpp", + "../../services/dfx/dslm_hidumper.c", + "../../services/dfx/dslm_hitrace.cpp", + "../../services/msg/dslm_messenger_wrapper.c", + "../../services/sa/common/dslm_rpc_process.c", + "device_manager.cpp", + "device_manager_mock.cpp", + "dslm_memory_mock.cpp", + "dslm_request_callback_mock.cpp", + "dslm_second_test.cpp", + ] + + include_dirs = [ + ".", + "../../baselib/msglib/include", + "../../baselib/msglib/src/common", + "../../baselib/msglib/src/standard", + "../../baselib/msglib/src/utils", + "../../common/include", + "../../interfaces/inner_api/include", + "../../interfaces/inner_api/src/standard", + "../../oem_property/common", + "../../oem_property/include", + "../../oem_property/ohos/common", + "../../oem_property/ohos/standard/impl", + "../../services/include", + "../../services/dslm", + "../../services/sa/common", + ] + + configs = [ "../../common:common_configs" ] + + configs += [ ":dslm_cmoker" ] + + deps = [ + "../../baselib/utils:utils_static", + "../../oem_property/ohos/standard:dslm_ohos_cred_obj", + "../../services/common:service_common_obj", + "../../services/dslm:service_dslm_test_obj", + ] + + external_deps = [ + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "device_security_level:dslm_sdk", + "dsoftbus:softbus_client", + "googletest:gmock", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "huks:libhukssdk", + ] + } } diff --git a/test/dslm_unit_test/dslm_oem_property_test.cpp b/test/dslm_unit_test/dslm_oem_property_test.cpp index 2150098a13faff7b142c04fe9d519811c70e5566..e58129a5db9de00e125daae4a04e6c991690b2fb 100644 --- a/test/dslm_unit_test/dslm_oem_property_test.cpp +++ b/test/dslm_unit_test/dslm_oem_property_test.cpp @@ -86,6 +86,9 @@ HWTEST_F(DslmOemPropertyTest, VerifyDslmCredential_case1, TestSize.Level0) ret = VerifyDslmCredential(cred, &info, &list); EXPECT_EQ(ERR_PARSE_CLOUD_CRED_DATA, ret); + + ret = VerifyDslmCredential(nullptr, &info, &list); + EXPECT_EQ(ERR_PARSE_CLOUD_CRED_DATA, ret); } /** @@ -690,6 +693,12 @@ HWTEST_F(DslmOemPropertyTest, EcdsaVerify_case1, TestSize.Level0) int32_t ret = EcdsaVerify(&srcData, &sigData, pbkData, algorithm); EXPECT_EQ(ERR_INVALID_PARA, ret); + + ret = EcdsaVerify(nullptr, &sigData, pbkData, algorithm); + EXPECT_EQ(ERR_INVALID_PARA, ret); + + ret = EcdsaVerify(&srcData, nullptr, pbkData, algorithm); + EXPECT_EQ(ERR_INVALID_PARA, ret); } HWTEST_F(DslmOemPropertyTest, EcdsaVerify_case2, TestSize.Level0) diff --git a/test/dslm_unit_test/dslm_second_test.cpp b/test/dslm_unit_test/dslm_second_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7aedf9178df3aa8df003796304e5b4171df27c5d --- /dev/null +++ b/test/dslm_unit_test/dslm_second_test.cpp @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2025 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 "dslm_test.h" + +#include +#include +#include +#include +#include +#include + +#include "file_ex.h" +#include "nativetoken_kit.h" +#include "securec.h" +#include "token_setproc.h" + +#include "device_security_defines.h" +#include "device_security_info.h" +#include "device_security_level_defines.h" +#include "dslm_bigdata.h" +#include "dslm_core_defines.h" +#include "dslm_core_process.h" +#include "dslm_credential.h" +#include "dslm_crypto.h" +#include "dslm_device_list.h" +#include "dslm_fsm_process.h" +#include "dslm_hidumper.h" +#include "dslm_hievent.h" +#include "dslm_inner_process.h" +#include "dslm_memory_mock.h" +#include "dslm_messenger_wrapper.h" +#include "dslm_msg_serialize.h" +#include "dslm_msg_utils.h" +#include "dslm_ohos_request.h" +#include "dslm_ohos_verify.h" +#include "dslm_request_callback_mock.h" +#include "dslm_rpc_process.h" +#include "external_interface_adapter.h" +#include "hks_adapter.h" +#include "hks_type.h" +#include "messenger_device_socket_manager.h" +#include "messenger_device_status_manager.h" +#include "messenger_impl.h" +#include "utils_datetime.h" +#include "utils_mem.h" +#include "utils_timer.h" +#include "utils_tlv.h" +#include "utils_work_queue.h" + +using namespace std; +using namespace std::chrono; +using namespace testing; +using namespace testing::ext; + +typedef struct Messenger { + uint32_t magicHead; + WorkQueue *processQueue; +} Messenger; + +#define MESSENGER_MAGIC_HEAD 0x1234abcd + +namespace OHOS { +namespace Security { +namespace DslmUnitTest { +void DslmTest::SetUpTestCase() +{ +} + +void DslmTest::TearDownTestCase() +{ +} + +void DslmTest::SetUp() +{ +} + +void DslmTest::TearDown() +{ +} + +HWTEST_F(DslmTest, CreateMessengerImpl_case1, TestSize.Level0) +{ + const MessengerConfig config = {}; + + Messenger *ret = CreateMessengerImpl(&config); + EXPECT_EQ(nullptr, ret); + + ret = CreateMessengerImpl(nullptr); + EXPECT_EQ(nullptr, ret); +} + +HWTEST_F(DslmTest, IsMessengerReadyImpl_case1, TestSize.Level0) +{ + const Messenger messenger = {MESSENGER_MAGIC_HEAD, nullptr}; + const Messenger failedMessenger = {0, nullptr}; + + bool ret = IsMessengerReadyImpl(&messenger); + EXPECT_EQ(true, ret); + + ret = IsMessengerReadyImpl(&failedMessenger); + EXPECT_EQ(false, ret); + + ret = IsMessengerReadyImpl(nullptr); + EXPECT_EQ(false, ret); +} + +HWTEST_F(DslmTest, SendMsgToImpl_case1, TestSize.Level0) +{ + const Messenger messenger = {MESSENGER_MAGIC_HEAD, nullptr}; + const Messenger failedMessenger = {0, nullptr}; + uint64_t transNo = 0; + const DeviceIdentify devId = {DEVICE_ID_MAX_LEN, {0}}; + const uint8_t msg[] = {'1', '2'}; + uint32_t msgLen = 0; + + SendMsgToImpl(&messenger, transNo, &devId, msg, msgLen); + + SendMsgToImpl(nullptr, transNo, &devId, msg, msgLen); + + SendMsgToImpl(&failedMessenger, transNo, &devId, msg, msgLen); + + uint32_t ret = InitService(); + EXPECT_EQ(ERR_MSG_NOT_INIT, ret); +} + +HWTEST_F(DslmTest, GetDeviceOnlineStatusImpl_case1, TestSize.Level0) +{ + const Messenger messenger = {MESSENGER_MAGIC_HEAD, nullptr}; + const Messenger failedMessenger = {0, nullptr}; + const DeviceIdentify devId = {DEVICE_ID_MAX_LEN, {0}}; + int32_t level = 0; + + bool ret = GetDeviceOnlineStatusImpl(&messenger, &devId, &level); + EXPECT_EQ(false, ret); + + ret = GetDeviceOnlineStatusImpl(nullptr, &devId, &level); + EXPECT_EQ(false, ret); + + ret = GetDeviceOnlineStatusImpl(&messenger, nullptr, &level); + EXPECT_EQ(false, ret); + + ret = GetDeviceOnlineStatusImpl(&failedMessenger, &devId, &level); + EXPECT_EQ(false, ret); +} + +HWTEST_F(DslmTest, GetSelfDeviceIdentifyImpl_case1, TestSize.Level0) +{ + const Messenger messenger = {MESSENGER_MAGIC_HEAD, nullptr}; + const Messenger failedMessenger = {0, nullptr}; + DeviceIdentify devId = {DEVICE_ID_MAX_LEN, {0}}; + int32_t level = 0; + + bool ret = GetSelfDeviceIdentifyImpl(&messenger, &devId, &level); + EXPECT_EQ(true, ret); + + ret = GetSelfDeviceIdentifyImpl(nullptr, &devId, &level); + EXPECT_EQ(false, ret); + + ret = GetSelfDeviceIdentifyImpl(&messenger, nullptr, &level); + EXPECT_EQ(false, ret); + + ret = GetSelfDeviceIdentifyImpl(&failedMessenger, &devId, &level); + EXPECT_EQ(false, ret); +} + +HWTEST_F(DslmTest, GetDeviceStatisticInfoImpl_case1, TestSize.Level0) +{ + const Messenger messenger = {MESSENGER_MAGIC_HEAD, nullptr}; + const Messenger failedMessenger = {0, nullptr}; + DeviceIdentify devId = {DEVICE_ID_MAX_LEN, {0}}; + const DeviceProcessor processor = nullptr; + void *para = nullptr; + StatisticInformation info = {}; + + ForEachDeviceProcessImpl(&messenger, processor, para); + + ForEachDeviceProcessImpl(nullptr, processor, para); + + ForEachDeviceProcessImpl(&failedMessenger, processor, para); + + bool ret = GetDeviceStatisticInfoImpl(&messenger, &devId, &info); + EXPECT_EQ(true, ret); + + ret = GetDeviceStatisticInfoImpl(&failedMessenger, &devId, &info); + EXPECT_EQ(false, ret); + + ret = GetDeviceStatisticInfoImpl(nullptr, &devId, &info); + EXPECT_EQ(false, ret); +} +} // namespace DslmUnitTest +} // namespace Security +} // namespace OHOS \ No newline at end of file