feature(trans):add permission manage interface for RPC

Signed-off-by: wuchengwen <wuchengwen4@huawei.com>
This commit is contained in:
wuchengwen 2024-03-18 17:23:34 +08:00
parent 676a7c6538
commit 8f858f2e56
6 changed files with 174 additions and 6 deletions

View File

@ -54,6 +54,34 @@ extern "C" {
* @version 1.0
*/
int32_t GetMtuSize(int32_t socket, uint32_t *mtuSize);
/**
* @brief Grant permission to socket with uid and pid.
*
* @param uid Indicates the uid of the process.
* @param pid Indicates the pid of the process.
* @param socketName Indicates the name of the socket to grant permission.
*
* @return Returns <b>SOFTBUS_INVALID_PARAM</b> if invalid parameters are detected.
* @return Returns <b>SOFTBUS_OK</b> if the operation is successful; returns an error code otherwise.
*
* @since 1.0
* @version 1.0
*/
int32_t GrantPermission(int32_t uid, int32_t pid, const char *socketName);
/**
* @brief Removes permissions for a specific socket
*
* @param socketName Indicates the name of the socket to remove permission.
*
* @return Returns <b>SOFTBUS_INVALID_PARAM</b> if invalid parameters are detected.
* @return Returns <b>SOFTBUS_OK</b> if the operation is successful; returns an error code otherwise.
*
* @since 1.0
* @version 1.0
*/
int32_t RemovePermission(const char *socketName);
#ifdef __cplusplus
}
#endif

View File

@ -110,13 +110,13 @@ typedef enum {
*/
typedef enum {
QOS_TYPE_MIN_BW, /**< Minimum bandwidth. */
QOS_TYPE_MAX_LATENCY, /**< Maximum latency. */
QOS_TYPE_MIN_LATENCY, /**< Minimum latency. */
QOS_TYPE_MAX_WAIT_TIMEOUT, /**< Maximum timeout of transmission. */
QOS_TYPE_MAX_BUFFER, /**< Maximum cache. */
QOS_TYPE_FIRST_PACKAGE, /**< First packet size. */
QOS_TYPE_MAX_WAIT_TIMEOUT = 1, /**< Maximum timeout of Bind. */
QOS_TYPE_MAX_LATENCY = 1, /**< @deprecated Maximum latency. */
QOS_TYPE_MIN_LATENCY, /**< @deprecated Minimum latency. */
QOS_TYPE_MAX_BUFFER, /**< @reserved Maximum cache. */
QOS_TYPE_FIRST_PACKAGE, /**< @reserved First packet size. */
QOS_TYPE_MAX_IDLE_TIMEOUT, /**< Maximum idle time. */
QOS_TYPE_TRANS_RELIABILITY, /**< Transmission reliability. */
QOS_TYPE_TRANS_RELIABILITY, /**< @reserved Transmission reliability. */
QOS_TYPE_BUTT,
} QosType;

View File

@ -385,6 +385,8 @@
"TransSetSocketFileListener";
"EvaluateQos";
"GetMtuSize";
"GrantPermission";
"RemovePermission";
"ClientAddPolicyReportCallback";
"ClientDeletePolicyReportCallback";
"ServerEnableDiscoveryPolicy";

View File

@ -132,4 +132,14 @@ int32_t GetMtuSize(int32_t socket, uint32_t *mtuSize)
{
TRANS_LOGI(TRANS_SDK, "GetMtuSize: socket=%{public}d", socket);
return GetSocketMtuSize(socket, mtuSize);
}
int32_t GrantPermission(int32_t uid, int32_t pid, const char *socketName)
{
return ClientGrantPermission(uid, pid, socketName);
}
int32_t RemovePermission(const char *socketName)
{
return ClientRemovePermission(socketName);
}

View File

@ -227,6 +227,31 @@ ohos_unittest("TransClientSocketServiceTest") {
}
}
ohos_unittest("TransClientSocketTest") {
module_out_path = module_output_path
sources = [ "client_trans_inner_socket_test.cpp" ]
deps = [
"$dsoftbus_root_path/sdk:softbus_client",
"//third_party/googletest:gtest_main",
]
if (is_standard_system) {
external_deps = [
"access_token:libaccesstoken_sdk",
"access_token:libnativetoken",
"access_token:libtoken_setproc",
"c_utils:utils",
"hilog:libhilog",
]
} else {
external_deps = [
"c_utils:utils",
"hilog:libhilog",
]
}
}
group("unittest") {
testonly = true
deps = [
@ -236,5 +261,6 @@ group("unittest") {
":TransClientSessionServiceTest",
":TransClientSessionTest",
":TransClientSocketServiceTest",
":TransClientSocketTest",
]
}

View File

@ -0,0 +1,102 @@
/*
* 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 <gtest/gtest.h>
#include "inner_socket.h"
#include "nativetoken_kit.h"
#include "softbus_error_code.h"
#include "token_setproc.h"
using namespace testing::ext;
namespace OHOS {
namespace {
void CounterfeitProcess(const char *processName)
{
NativeTokenInfoParams infoTnstance = {
.dcapsNum = 0,
.permsNum = 0,
.aclsNum = 0,
.dcaps = nullptr,
.perms = nullptr,
.acls = nullptr,
.processName = processName,
.aplStr = "system_core",
};
uint64_t tokenId = GetAccessTokenId(&infoTnstance);
SetSelfTokenID(tokenId);
}
} // namespace
class ClientTransSocketTest : public testing::Test { };
/*
* @tc.name: GrantPermissionTest001
* @tc.desc: Grant permission to DBinder test.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ClientTransSocketTest, GrantPermissionTest001, TestSize.Level1)
{
CounterfeitProcess("samgr");
int32_t uid = getuid();
ASSERT_GE(uid, 0);
int32_t pid = getpid();
ASSERT_GT(pid, 0);
std::string socketName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid);
auto ret = GrantPermission(uid, pid, socketName.c_str());
ASSERT_EQ(ret, SOFTBUS_OK);
ret = RemovePermission(socketName.c_str());
ASSERT_EQ(ret, SOFTBUS_OK);
}
/*
* @tc.name: GrantPermissionTest002
* @tc.desc: Other percess call GrantPermission test.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ClientTransSocketTest, GrantPermissionTest002, TestSize.Level1)
{
CounterfeitProcess("msdp");
int32_t uid = getuid();
ASSERT_GE(uid, 0);
int32_t pid = getpid();
ASSERT_GT(pid, 0);
std::string socketName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid);
auto ret = GrantPermission(uid, pid, socketName.c_str());
ASSERT_NE(ret, SOFTBUS_OK);
}
/*
* @tc.name: RemovePermissionTest001
* @tc.desc: Other percess call RemovePermission test.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ClientTransSocketTest, RemovePermissionTest001, TestSize.Level1)
{
CounterfeitProcess("samgr");
int32_t uid = getuid();
ASSERT_GE(uid, 0);
int32_t pid = getpid();
ASSERT_GT(pid, 0);
std::string socketName = "DBinder" + std::to_string(uid) + std::string("_") + std::to_string(pid);
auto ret = GrantPermission(uid, pid, socketName.c_str());
ASSERT_EQ(ret, SOFTBUS_OK);
CounterfeitProcess("msdp");
ret = RemovePermission(socketName.c_str());
ASSERT_NE(ret, SOFTBUS_OK);
}
} // namespace OHOS