!5078 socketName和networkId长度校验修改

Merge pull request !5078 from yue/master
This commit is contained in:
openharmony_ci 2024-01-18 03:30:23 +00:00 committed by Gitee
commit e64335e463
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 206 additions and 8 deletions

View File

@ -1697,8 +1697,8 @@ int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *l
static int32_t CheckBindSocketInfo(const SessionInfo *session)
{
if (!IsValidString(session->info.peerSessionName, SESSION_NAME_SIZE_MAX) ||
!IsValidString(session->info.peerDeviceId, DEVICE_ID_SIZE_MAX)) {
if (!IsValidString(session->info.peerSessionName, SESSION_NAME_SIZE_MAX - 1) ||
!IsValidString(session->info.peerDeviceId, DEVICE_ID_SIZE_MAX - 1)) {
char *anonySessionName = NULL;
char *anonyNetworkId = NULL;
Anonymize(session->info.peerSessionName, &anonySessionName);

View File

@ -173,21 +173,21 @@ int RemoveSessionServer(const char *pkgName, const char *sessionName)
static int32_t CheckParamIsValid(const char *mySessionName, const char *peerSessionName,
const char *peerNetworkId, const char *groupId, const SessionAttribute *attr)
{
if (!IsValidString(mySessionName, SESSION_NAME_SIZE_MAX)) {
if (!IsValidString(mySessionName, SESSION_NAME_SIZE_MAX - 1)) {
char *tmpMyName = NULL;
Anonymize(mySessionName, &tmpMyName);
TRANS_LOGE(TRANS_SDK, "invalid mySessionName. tmpMyName=%{public}s", tmpMyName);
AnonymizeFree(tmpMyName);
return SOFTBUS_INVALID_PARAM;
}
if (!IsValidString(peerSessionName, SESSION_NAME_SIZE_MAX)) {
if (!IsValidString(peerSessionName, SESSION_NAME_SIZE_MAX - 1)) {
char *tmpPeerName = NULL;
Anonymize(peerSessionName, &tmpPeerName);
TRANS_LOGE(TRANS_SDK, "invalid peerSessionName. tmpPeerName=%{public}s", tmpPeerName);
AnonymizeFree(tmpPeerName);
return SOFTBUS_INVALID_PARAM;
}
if (!IsValidString(peerNetworkId, DEVICE_ID_SIZE_MAX)) {
if (!IsValidString(peerNetworkId, DEVICE_ID_SIZE_MAX - 1)) {
char *tmpPeerNetworkId = NULL;
Anonymize(peerNetworkId, &tmpPeerNetworkId);
TRANS_LOGE(TRANS_SDK, "invalid peerNetworkId. tmpPeerNetworkId=%{public}s", tmpPeerNetworkId);

View File

@ -28,12 +28,13 @@
static int32_t CheckSocketInfoIsValid(const SocketInfo *info)
{
if (!IsValidString(info->name, SESSION_NAME_SIZE_MAX) || !IsValidString(info->pkgName, PKG_NAME_SIZE_MAX)) {
if (!IsValidString(info->name, SESSION_NAME_SIZE_MAX - 1) ||
!IsValidString(info->pkgName, PKG_NAME_SIZE_MAX - 1)) {
TRANS_LOGE(TRANS_SDK, "invalid name or package name of socket");
return SOFTBUS_INVALID_PARAM;
}
if (info->peerName != NULL && !IsValidString(info->peerName, SESSION_NAME_SIZE_MAX)) {
if (info->peerName != NULL && !IsValidString(info->peerName, SESSION_NAME_SIZE_MAX - 1)) {
char *anonySessionName = NULL;
Anonymize(info->peerName, &anonySessionName);
TRANS_LOGI(TRANS_SDK, "strcpy peerName failed, peerName=%{public}s, peerNameLen=%{public}zu",
@ -42,7 +43,7 @@ static int32_t CheckSocketInfoIsValid(const SocketInfo *info)
return SOFTBUS_INVALID_PARAM;
}
if (info->peerNetworkId != NULL && !IsValidString(info->peerNetworkId, DEVICE_ID_SIZE_MAX)) {
if (info->peerNetworkId != NULL && !IsValidString(info->peerNetworkId, DEVICE_ID_SIZE_MAX - 1)) {
char *anonyNetworkId = NULL;
Anonymize(info->peerNetworkId, &anonyNetworkId);
TRANS_LOGI(TRANS_SDK, "strcpy peerNetworkId failed, peerNetworkId=%{public}s, peerNetworkIdLen=%{public}zu",

View File

@ -202,6 +202,28 @@ ohos_unittest("TransClientSessionTest") {
}
}
ohos_unittest("TransClientSocketServiceTest") {
module_out_path = module_output_path
sources = [ "client_trans_socket_service_test.cpp" ]
deps = [
"$dsoftbus_root_path/sdk:softbus_client",
"//third_party/googletest:gtest_main",
]
if (is_standard_system) {
external_deps = [
"c_utils:utils",
"hilog:libhilog",
]
} else {
external_deps = [
"c_utils:utils",
"hilog:libhilog",
]
}
}
group("unittest") {
testonly = true
deps = [
@ -210,5 +232,6 @@ group("unittest") {
":TransClientSessionManagerTest",
":TransClientSessionServiceTest",
":TransClientSessionTest",
":TransClientSocketServiceTest",
]
}

View File

@ -0,0 +1,174 @@
/*
* Copyright (c) 2024 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 <string>
#include <gtest/gtest.h>
#include <securec.h>
#include "socket.h"
#include "softbus_error_code.h"
#define SOCKET_NAME_MAX_LEN 255
#define SOCKET_NAME_INVALID_LEN (SOCKET_NAME_MAX_LEN + 1)
#define SOCKET_PKG_NAME_MAX_LEN 64
#define SOCKET_PKG_NAME_INVALID_LEN (SOCKET_PKG_NAME_MAX_LEN + 1)
#define SOCKET_NETWORKID_MAX_LEN 64
#define SOCKET_NETWORKID_INVALID_LEN (SOCKET_NETWORKID_MAX_LEN + 1)
using namespace testing::ext;
namespace OHOS {
static std::string g_pkgName = "dms";
static std::string g_socketName = "ohos.distributedschedule.dms.test.client";
static std::string g_socketPeerName = "ohos.distributedschedule.dms.test.server";
static std::string g_networkId = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF0";
class TransClientSocketServiceTest : public testing::Test {
public:
TransClientSocketServiceTest() { }
~TransClientSocketServiceTest() { }
static void SetUpTestCase(void) {}
static void TearDownTestCase(void) {}
void SetUp() override { }
void TearDown() override { }
};
/**
* @tc.name: SocketName001
* @tc.desc: call Socket function with different socket name.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TransClientSocketServiceTest, SocketName001, TestSize.Level1)
{
SocketInfo info;
info.peerName = const_cast<char *>(g_socketPeerName.c_str());
info.peerNetworkId = const_cast<char *>(g_networkId.c_str());
info.pkgName = const_cast<char *>(g_pkgName.c_str());
info.dataType = DATA_TYPE_MESSAGE;
int32_t socketId = -1;
// socket name is null pointer
info.name = nullptr;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
// the length of socket name is zero
char socketName[SOCKET_NAME_INVALID_LEN + 1];
memset_s(socketName, SOCKET_NAME_INVALID_LEN + 1, 0, SOCKET_NAME_INVALID_LEN + 1);
info.name = socketName;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
// the length of socket name greater than 255
memset_s(socketName, SOCKET_NAME_INVALID_LEN + 1, 'a', SOCKET_NAME_INVALID_LEN);
info.name = socketName;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
}
/**
* @tc.name: SocketPeerName001
* @tc.desc: call Socket function with different socket peerName.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TransClientSocketServiceTest, SocketPeerName001, TestSize.Level1)
{
SocketInfo info;
info.name = const_cast<char *>(g_socketName.c_str());
info.peerNetworkId = const_cast<char *>(g_networkId.c_str());
info.pkgName = const_cast<char *>(g_pkgName.c_str());
info.dataType = DATA_TYPE_MESSAGE;
int32_t socketId = -1;
// the length of socket peerName is zero
char socketName[SOCKET_NAME_INVALID_LEN + 1];
memset_s(socketName, SOCKET_NAME_INVALID_LEN + 1, 0, SOCKET_NAME_INVALID_LEN + 1);
info.peerName = socketName;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
// the length of socket name greater than 255
memset_s(socketName, SOCKET_NAME_INVALID_LEN + 1, 'a', SOCKET_NAME_INVALID_LEN);
info.peerName = socketName;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
}
/**
* @tc.name: SocketPeerNetworkId001
* @tc.desc: call Socket function with different socket peerNetworkId.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TransClientSocketServiceTest, SocketPeerNetworkId001, TestSize.Level1)
{
SocketInfo info;
info.name = const_cast<char *>(g_socketName.c_str());
info.peerName = const_cast<char *>(g_socketPeerName.c_str());
info.pkgName = const_cast<char *>(g_pkgName.c_str());
info.dataType = DATA_TYPE_MESSAGE;
int32_t socketId = -1;
// the length of socket peerNetworkId is zero
char networkId[SOCKET_NETWORKID_INVALID_LEN + 1];
memset_s(networkId, SOCKET_NETWORKID_INVALID_LEN + 1, 0, SOCKET_NETWORKID_INVALID_LEN + 1);
info.peerNetworkId = networkId;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
// the length of socket peerNetworkId greater than 65
memset_s(networkId, SOCKET_NETWORKID_INVALID_LEN + 1, 'a', SOCKET_NETWORKID_INVALID_LEN);
info.peerNetworkId = networkId;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
}
/**
* @tc.name: SocketPkgName001
* @tc.desc: call Socket function with different socket pkgName.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TransClientSocketServiceTest, SocketPkgName001, TestSize.Level1)
{
SocketInfo info;
info.name = const_cast<char *>(g_socketName.c_str());
info.peerName = const_cast<char *>(g_socketPeerName.c_str());
info.peerNetworkId = const_cast<char *>(g_networkId.c_str());
info.dataType = DATA_TYPE_MESSAGE;
int32_t socketId = -1;
// socket name is null pointer
info.pkgName = nullptr;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
// the length of socket name is zero
char pkgName[SOCKET_PKG_NAME_INVALID_LEN + 1];
memset_s(pkgName, SOCKET_PKG_NAME_INVALID_LEN + 1, 0, SOCKET_PKG_NAME_INVALID_LEN + 1);
info.pkgName = pkgName;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
// the length of socket name greater than 255
memset_s(pkgName, SOCKET_PKG_NAME_INVALID_LEN + 1, 'a', SOCKET_PKG_NAME_INVALID_LEN);
info.name = pkgName;
socketId = Socket(info);
ASSERT_EQ(socketId, SOFTBUS_INVALID_PARAM);
}
} // namespace OHOS