diff --git a/services/legacy/group_manager/src/group_operation/group_operation.c b/services/legacy/group_manager/src/group_operation/group_operation.c index 76b09089fc0f4001b72bce40759736a3568bb6be..ade007aff4dec239e06ec7bd1bd97b0ae16ed8fc 100644 --- a/services/legacy/group_manager/src/group_operation/group_operation.c +++ b/services/legacy/group_manager/src/group_operation/group_operation.c @@ -855,8 +855,11 @@ static int32_t RequestAddMemberToGroup(int32_t osAccountId, int64_t requestId, c static int32_t CreateAppIdJsonString(const char *appId, char **reqParames) { + if ((appId == NULL) || (reqParames == NULL)) { + return HC_ERR_INVALID_PARAMS; + } CJson *reqJson = CreateJson(); - if ((reqJson == NULL) || (reqParames == NULL)) { + if (reqJson == NULL) { LOGE("Failed to create json!"); return HC_ERR_JSON_CREATE; } diff --git a/test/BUILD.gn b/test/BUILD.gn index 3ed68537d251aee6c1f3c4190db03dcb3ec551ec..cae6d3fda223b64ffd01675d18e32aee580acc71 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -72,6 +72,7 @@ group("deviceauth_test_build") { "unittest/deviceauth:deviceauth_llt", "unittest/deviceauth:deviceauth_unit_test", "unittest/deviceauth:identity_service_ipc_test", + "unittest/deviceauth:minisession_test", "unittest/tdd_framework/unit_test/services/creds_manager:creds_manager_test", "unittest/tdd_framework/unit_test/services/frameworks/hiview_adapter:perform_dumper_test", "unittest/tdd_framework/unit_test/services/frameworks/os_account_adapter:os_account_adapter_test_group", diff --git a/test/unittest/deviceauth/BUILD.gn b/test/unittest/deviceauth/BUILD.gn index 56562fcd54d4664f86d961200bee862f36b0ae72..1a0c07343e5473e5d95fa66ea76568e44bf18047 100644 --- a/test/unittest/deviceauth/BUILD.gn +++ b/test/unittest/deviceauth/BUILD.gn @@ -867,3 +867,46 @@ ohos_unittest("identity_service_ipc_test") { "hilog:libhilog", ] } + + +ohos_unittest("minisession_test") { + module_out_path = module_output_path + + include_dirs = hals_inc_path + include_dirs += [ + "${services_path}/session_manager/inc/session/mini_session", + "./include", + "./unit_test/include", + "${services_path}/frameworks/inc", + "${services_path}/mk_agree/inc", + "${services_path}/frameworks/inc/hiview_adapter", + "${frameworks_path}/deviceauth_lite/inc/os_account_adapter", + "${services_path}/privacy_enhancement/inc", + "${services_path}/frameworks/inc/security_label_adapter", + "${services_path}/frameworks/inc/account_task_manager" + ] + + + sources = [ "source/minisession_test.cpp" ] + + sources += session_mini_files + + deps = [ "${deps_adapter_path}:${hal_module_test_name}" ] + + defines = [ "DEV_AUTH_IS_ENABLE" ] + + cflags = [ "-DHILOG_ENABLE" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "selinux_adapter:librestorecon", + "cJSON:cjson", + "c_utils:utils", + "device_auth:deviceauth_sdk", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + ] +} \ No newline at end of file diff --git a/test/unittest/deviceauth/source/minisession_test.cpp b/test/unittest/deviceauth/source/minisession_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49c359baef6afc43e7dddf02221c2c70863a581c --- /dev/null +++ b/test/unittest/deviceauth/source/minisession_test.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2022 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 +#include +#include +#include "securec.h" +#include "common_defs.h" +#include "device_auth_defines.h" +#include "mini_session_manager.h" + +using namespace std; +using namespace testing::ext; + +namespace { +#define RETURN_RANDOM_LEN 16 +static const int32_t TEST_OS_ACCOUNT_ID = 100; +static const int64_t TEST_REQUEST_ID = 1234567890; +static const int64_t INVALID_REQUEST_ID = 0; +static const char* TEST_SERVICE_ID = "TestServiceId"; + +class LightSessionManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +void LightSessionManagerTest::SetUpTestCase() +{ + ASSERT_EQ(InitLightSessionManager(), HC_SUCCESS); +} + +void LightSessionManagerTest::TearDownTestCase() +{ + DestroyLightSessionManager(); +} + +void LightSessionManagerTest::SetUp() +{ + DeleteLightSession(TEST_REQUEST_ID, TEST_OS_ACCOUNT_ID); +} + +void LightSessionManagerTest::TearDown() +{ + DeleteLightSession(TEST_REQUEST_ID, TEST_OS_ACCOUNT_ID); +} + +HWTEST_F(LightSessionManagerTest, LightSessionManagerTest01, TestSize.Level0) +{ + uint8_t testRandomVal[RETURN_RANDOM_LEN] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, + 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 + }; + DataBuff randomBuff = { testRandomVal, RETURN_RANDOM_LEN }; + + EXPECT_EQ(AddLightSession(TEST_REQUEST_ID, TEST_OS_ACCOUNT_ID, TEST_SERVICE_ID, randomBuff), HC_SUCCESS); + + uint8_t *retRandomVal = nullptr; + char *retServiceId = nullptr; + EXPECT_EQ(QueryLightSession(TEST_REQUEST_ID, TEST_OS_ACCOUNT_ID, &retRandomVal, + &retServiceId), HC_SUCCESS); + ASSERT_NE(retRandomVal, nullptr); + ASSERT_NE(retServiceId, nullptr); + + EXPECT_EQ(memcmp(retRandomVal, testRandomVal, RETURN_RANDOM_LEN), 0); + EXPECT_STREQ(retServiceId, TEST_SERVICE_ID); + + HcFree(retRandomVal); + HcFree(retServiceId); + + EXPECT_EQ(DeleteLightSession(TEST_REQUEST_ID, TEST_OS_ACCOUNT_ID), HC_SUCCESS); + + retRandomVal = nullptr; + retServiceId = nullptr; + EXPECT_EQ(QueryLightSession(TEST_REQUEST_ID, TEST_OS_ACCOUNT_ID, &retRandomVal, + &retServiceId), HC_ERR_SESSION_NOT_EXIST); + EXPECT_EQ(retRandomVal, nullptr); + EXPECT_EQ(retServiceId, nullptr); +} + +HWTEST_F(LightSessionManagerTest, LightSessionManagerTest02, TestSize.Level0) +{ + uint8_t testRandomVal[RETURN_RANDOM_LEN] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, + 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 + }; + DataBuff randomBuff = { testRandomVal, RETURN_RANDOM_LEN }; + + EXPECT_EQ(AddLightSession(TEST_REQUEST_ID, TEST_OS_ACCOUNT_ID, TEST_SERVICE_ID, randomBuff), HC_SUCCESS); +} + +HWTEST_F(LightSessionManagerTest, LightSessionManagerTest03, TestSize.Level0) +{ + constexpr uint32_t maxSession = 30; + uint8_t testRandomVal[RETURN_RANDOM_LEN] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, + 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 + }; + DataBuff randomBuff = { testRandomVal, RETURN_RANDOM_LEN }; + + for (uint64_t i = 1; i <= maxSession; ++i) { + EXPECT_EQ(AddLightSession(i, TEST_OS_ACCOUNT_ID, TEST_SERVICE_ID, randomBuff), HC_SUCCESS); + } + + EXPECT_EQ(AddLightSession(maxSession + 1, TEST_OS_ACCOUNT_ID, TEST_SERVICE_ID, randomBuff), HC_ERR_OUT_OF_LIMIT); + + for (uint64_t i = 1; i <= maxSession; ++i) { + EXPECT_EQ(DeleteLightSession(i, TEST_OS_ACCOUNT_ID), HC_SUCCESS); + } +} + +HWTEST_F(LightSessionManagerTest, LightSessionManagerTest04, TestSize.Level0) +{ + uint8_t *randomVal = nullptr; + char *serviceId = nullptr; + EXPECT_EQ(QueryLightSession(INVALID_REQUEST_ID, TEST_OS_ACCOUNT_ID, &randomVal, + &serviceId), HC_ERR_SESSION_NOT_EXIST); + EXPECT_EQ(randomVal, nullptr); + EXPECT_EQ(serviceId, nullptr); +} + +HWTEST_F(LightSessionManagerTest, LightSessionManagerTest05, TestSize.Level0) +{ + EXPECT_EQ(DeleteLightSession(INVALID_REQUEST_ID, TEST_OS_ACCOUNT_ID), HC_SUCCESS); +} +} // namespace \ No newline at end of file