mirror of
https://github.com/openharmony/distributedschedule_dms_fwk_lite.git
synced 2026-07-01 06:41:51 -04:00
optimize interfaces to query appId
Signed-off-by: jidong <jidong4@huawei.com>
This commit is contained in:
@@ -40,13 +40,12 @@ char* GetCallerSignature(const char *remoteName, BundleInfo *bundleInfo);
|
||||
char* GetRemoteSignature(const char *remoteName, BundleInfo *bundleInfo);
|
||||
|
||||
/**
|
||||
* @brief Get appId
|
||||
* @brief Get caller bundle info from bms or file
|
||||
* @param callerInfo caller information, which includes uid and bundleName
|
||||
* @param appId application id, which uniquely identifies an application
|
||||
* @param len length of appId
|
||||
* @param bundleInfo bundle information of caller
|
||||
* @return DmsLiteCommonErrorCode
|
||||
*/
|
||||
int32_t GetAppId(const CallerInfo *callerInfo, char *appId, uint32_t len);
|
||||
int32_t GetCallerBundleInfo(const CallerInfo *callerInfo, BundleInfo *bundleInfo);
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
|
||||
@@ -26,8 +26,6 @@ extern "C" {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void InitSoftbusService();
|
||||
|
||||
int32_t CreateDMSSessionServer();
|
||||
int32_t CloseDMSSessionServer();
|
||||
int32_t SendDmsMessage(const char *data, int32_t len, const char *deviceId, IDmsListener *callback);
|
||||
|
||||
@@ -33,7 +33,6 @@ namespace OHOS {
|
||||
namespace DistributedSchedule {
|
||||
namespace {
|
||||
#ifndef WEARABLE_PRODUCT
|
||||
const int32_t MAX_APPID_LEN = 512;
|
||||
const int32_t NON_EXISTENT_UID = 12345;
|
||||
const char NATIVE_APPID_DIR[] = "/system/native_appid/";
|
||||
const char FOUNDATION_APPID[] = "foundation_signature";
|
||||
@@ -55,46 +54,39 @@ protected:
|
||||
|
||||
#ifndef WEARABLE_PRODUCT
|
||||
/**
|
||||
* @tc.name: GetAppId_001
|
||||
* @tc.desc: Get appId failed with invalid appId pointer or length
|
||||
* @tc.name: GetCallerBundleInfo_001
|
||||
* @tc.desc: GetCallerBundleInfo failed with null callerInfo or bundleInfo pointer
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000FU5M6
|
||||
*/
|
||||
HWTEST_F(PermissionTest, GetAppId_001, TestSize.Level1)
|
||||
HWTEST_F(PermissionTest, GetCallerBundleInfo_001, TestSize.Level1)
|
||||
{
|
||||
BundleInfo bundleInfo;
|
||||
EXPECT_EQ(memset_s(&bundleInfo, sizeof(BundleInfo), 0x00, sizeof(BundleInfo)), EOK);
|
||||
EXPECT_EQ(GetBundleInfo(LAUNCHER_BUNDLE_NAME, 0, &bundleInfo), EC_SUCCESS);
|
||||
CallerInfo callerInfo;
|
||||
callerInfo.uid = bundleInfo.uid;
|
||||
char *appId1 = nullptr;
|
||||
EXPECT_EQ(GetAppId(&callerInfo, appId1, MAX_APPID_LEN), DMS_EC_INVALID_PARAMETER);
|
||||
char appId2[MAX_APPID_LEN] = {0};
|
||||
EXPECT_EQ(GetAppId(&callerInfo, appId2, 0), DMS_EC_INVALID_PARAMETER);
|
||||
EXPECT_EQ(GetAppId(nullptr, appId2, MAX_APPID_LEN), DMS_EC_INVALID_PARAMETER);
|
||||
CallerInfo callerInfo = {.uid = SHELL_UID};
|
||||
BundleInfo bundleInfo = {0};
|
||||
EXPECT_EQ(GetCallerBundleInfo(nullptr, &bundleInfo), DMS_EC_INVALID_PARAMETER);
|
||||
EXPECT_EQ(GetCallerBundleInfo(&callerInfo, nullptr), DMS_EC_INVALID_PARAMETER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: GetAppId_002
|
||||
* @tc.desc: Get appId failed with SHELL_UID=2, which is not configured with appId
|
||||
* @tc.name: GetCallerBundleInfo_002
|
||||
* @tc.desc: GetCallerBundleInfo failed with SHELL_UID=0/2, which is not configured with appId
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000FU5M6
|
||||
*/
|
||||
HWTEST_F(PermissionTest, GetAppId_002, TestSize.Level1)
|
||||
HWTEST_F(PermissionTest, GetCallerBundleInfo_002, TestSize.Level1)
|
||||
{
|
||||
char appId[MAX_APPID_LEN] = {0};
|
||||
CallerInfo callerInfo;
|
||||
callerInfo.uid = SHELL_UID;
|
||||
EXPECT_EQ(GetAppId(&callerInfo, appId, MAX_APPID_LEN), DMS_EC_FAILURE);
|
||||
CallerInfo callerInfo = {.uid = SHELL_UID};
|
||||
BundleInfo bundleInfo = {0};
|
||||
EXPECT_EQ(GetCallerBundleInfo(&callerInfo, &bundleInfo), DMS_EC_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: GetAppId_003
|
||||
* @tc.desc: Get appId successfully with FOUNDATION_UID=7, which has been configured with appId
|
||||
* @tc.name: GetCallerBundleInfo_003
|
||||
* @tc.desc: GetCallerBundleInfo successfully with FOUNDATION_UID=7, which has been configured with appId
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000FU5M6
|
||||
*/
|
||||
HWTEST_F(PermissionTest, GetAppId_003, TestSize.Level1)
|
||||
HWTEST_F(PermissionTest, GetCallerBundleInfo_003, TestSize.Level1)
|
||||
{
|
||||
bool isDirExist = true;
|
||||
DIR *dir = opendir(NATIVE_APPID_DIR);
|
||||
@@ -105,17 +97,17 @@ HWTEST_F(PermissionTest, GetAppId_003, TestSize.Level1)
|
||||
} else {
|
||||
closedir(dir);
|
||||
}
|
||||
CallerInfo callerInfo;
|
||||
callerInfo.uid = FOUNDATION_UID;
|
||||
CallerInfo callerInfo = {.uid = FOUNDATION_UID};
|
||||
stringstream filePath;
|
||||
filePath << NATIVE_APPID_DIR << PREFIX << callerInfo.uid << SUFFIX;
|
||||
fstream fs(filePath.str(), ios::out);
|
||||
EXPECT_TRUE(fs.is_open());
|
||||
fs << FOUNDATION_APPID;
|
||||
fs.close();
|
||||
char appId[MAX_APPID_LEN] = {0};
|
||||
EXPECT_EQ(GetAppId(&callerInfo, appId, MAX_APPID_LEN), DMS_EC_SUCCESS);
|
||||
EXPECT_EQ(strcmp(appId, FOUNDATION_APPID), 0);
|
||||
BundleInfo bundleInfo = {0};
|
||||
EXPECT_EQ(GetCallerBundleInfo(&callerInfo, &bundleInfo), DMS_EC_SUCCESS);
|
||||
EXPECT_EQ(strcmp(bundleInfo.appId, FOUNDATION_APPID), 0);
|
||||
ClearBundleInfo(&bundleInfo);
|
||||
remove(filePath.str().c_str());
|
||||
if (!isDirExist) {
|
||||
rmdir(NATIVE_APPID_DIR);
|
||||
@@ -123,35 +115,33 @@ HWTEST_F(PermissionTest, GetAppId_003, TestSize.Level1)
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: GetAppId_004
|
||||
* @tc.desc: Get appId failed with a non-existent uid
|
||||
* @tc.name: GetCallerBundleInfo_004
|
||||
* @tc.desc: GetCallerBundleInfo failed with a non-existent uid
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000FU5M6
|
||||
*/
|
||||
HWTEST_F(PermissionTest, GetAppId_004, TestSize.Level1)
|
||||
HWTEST_F(PermissionTest, GetCallerBundleInfo_004, TestSize.Level1)
|
||||
{
|
||||
char appId[MAX_APPID_LEN] = {0};
|
||||
CallerInfo callerInfo;
|
||||
callerInfo.uid = NON_EXISTENT_UID;
|
||||
EXPECT_EQ(GetAppId(&callerInfo, appId, MAX_APPID_LEN), DMS_EC_FAILURE);
|
||||
CallerInfo callerInfo = {.uid = NON_EXISTENT_UID};
|
||||
BundleInfo bundleInfo = {0};
|
||||
EXPECT_EQ(GetCallerBundleInfo(&callerInfo, &bundleInfo), DMS_EC_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: GetAppId_005
|
||||
* @tc.desc: Get appId successfully with com.huawei.launcher uid
|
||||
* @tc.name: GetCallerBundleInfo_005
|
||||
* @tc.desc: GetCallerBundleInfo successfully with com.huawei.launcher uid
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000FU5M6
|
||||
*/
|
||||
HWTEST_F(PermissionTest, GetAppId_005, TestSize.Level1)
|
||||
HWTEST_F(PermissionTest, GetCallerBundleInfo_005, TestSize.Level1)
|
||||
{
|
||||
BundleInfo bundleInfo;
|
||||
EXPECT_EQ(memset_s(&bundleInfo, sizeof(BundleInfo), 0x00, sizeof(BundleInfo)), EOK);
|
||||
BundleInfo bundleInfo = {0};
|
||||
EXPECT_EQ(GetBundleInfo(LAUNCHER_BUNDLE_NAME, 0, &bundleInfo), EC_SUCCESS);
|
||||
CallerInfo callerInfo;
|
||||
callerInfo.uid = bundleInfo.uid;
|
||||
char appId[MAX_APPID_LEN] = {0};
|
||||
EXPECT_EQ(GetAppId(&callerInfo, appId, MAX_APPID_LEN), DMS_EC_SUCCESS);
|
||||
EXPECT_EQ(strcmp(appId, LAUNCHER_APPID), 0);
|
||||
CallerInfo callerInfo = {.uid = bundleInfo.uid};
|
||||
BundleInfo callerBundleInfo = {0};
|
||||
EXPECT_EQ(GetCallerBundleInfo(&callerInfo, &callerBundleInfo), DMS_EC_SUCCESS);
|
||||
EXPECT_EQ(strcmp(callerBundleInfo.appId, LAUNCHER_APPID), 0);
|
||||
ClearBundleInfo(&callerBundleInfo);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
+10
-6
@@ -30,7 +30,6 @@
|
||||
#include "securec.h"
|
||||
|
||||
#define DMS_VERSION_VALUE 200
|
||||
#define MAX_APPID_LEN 256
|
||||
#define ENDING_SYMBOL_LEN 1
|
||||
|
||||
static int32_t FillRequestData(RequestData *reqdata, const Want *want,
|
||||
@@ -117,13 +116,18 @@ static int32_t MarshallDmsMessage(const Want *want, const CallerInfo *callerInfo
|
||||
PACKET_MARSHALL_HELPER(String, CALLEE_BUNDLE_NAME, want->element->bundleName);
|
||||
PACKET_MARSHALL_HELPER(String, CALLEE_ABILITY_NAME, want->element->abilityName);
|
||||
|
||||
char appId[MAX_APPID_LEN + ENDING_SYMBOL_LEN] = {0};
|
||||
int32_t ret = GetAppId(callerInfo, appId, MAX_APPID_LEN);
|
||||
BundleInfo bundleInfo = {0};
|
||||
int32_t ret = GetCallerBundleInfo(callerInfo, &bundleInfo);
|
||||
if (ret != DMS_EC_SUCCESS) {
|
||||
HILOGI("[StartRemoteAbility GetAppID error = %d]", ret);
|
||||
HILOGE("[StartRemoteAbility GetCallerBundleInfo error = %d]", ret);
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
PACKET_MARSHALL_HELPER(String, CALLER_SIGNATURE, appId);
|
||||
if (!MarshallString(bundleInfo.appId, CALLER_SIGNATURE)) {
|
||||
HILOGE("[StartRemoteAbility Marshall appId failed]");
|
||||
ClearBundleInfo(&bundleInfo);
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
ClearBundleInfo(&bundleInfo);
|
||||
|
||||
if (want->data != NULL && want->dataLength > 0) {
|
||||
RAWDATA_MARSHALL_HELPER(RawData, CALLER_PAYLOAD, want->data, want->dataLength);
|
||||
@@ -158,7 +162,7 @@ static int32_t FillCallerInfo(RequestData *reqdata, const CallerInfo *callerInfo
|
||||
DMS_FREE(data);
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
data[size] = '\0';
|
||||
data[size - ENDING_SYMBOL_LEN] = '\0';
|
||||
callerData->bundleName = data;
|
||||
}
|
||||
return DMS_EC_SUCCESS;
|
||||
|
||||
@@ -63,8 +63,6 @@ static void OnInitialize(Feature *feature, Service *parent, Identity identity)
|
||||
}
|
||||
|
||||
((DmsLite*) feature)->identity = identity;
|
||||
|
||||
InitSoftbusService();
|
||||
}
|
||||
|
||||
static void OnStop(Feature *feature, Identity identity)
|
||||
|
||||
+35
-25
@@ -20,13 +20,13 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dmslite_log.h"
|
||||
|
||||
#ifdef WEARABLE_PRODUCT
|
||||
#include "bundle_manager.h"
|
||||
#else
|
||||
#include "bundle_inner_interface.h"
|
||||
#endif
|
||||
#include "dmslite_log.h"
|
||||
#include "dmslite_utils.h"
|
||||
#include "samgr_lite.h"
|
||||
#include "securec.h"
|
||||
|
||||
@@ -102,6 +102,7 @@ int32_t CheckRemotePermission(const PermissionCheckInfo *permissionCheckInfo)
|
||||
/* appId: bundleName + "_" + signature */
|
||||
const char *calleeSignature = bundleInfo.appId + strlen(permissionCheckInfo->calleeBundleName)
|
||||
+ DELIMITER_LENGTH;
|
||||
ClearBundleInfo(&bundleInfo);
|
||||
if ((permissionCheckInfo->callerSignature == NULL) || (calleeSignature == NULL)) {
|
||||
HILOGE("[Signature is null]");
|
||||
return DMS_EC_FAILURE;
|
||||
@@ -115,7 +116,7 @@ int32_t CheckRemotePermission(const PermissionCheckInfo *permissionCheckInfo)
|
||||
return DMS_EC_SUCCESS;
|
||||
}
|
||||
|
||||
static int32_t GetAppIdFromFile(const char *filePath, char *appId, uint32_t len)
|
||||
static int32_t GetBundleInfoFromFile(const char *filePath, BundleInfo *bundleInfo)
|
||||
{
|
||||
int32_t fd = open(filePath, O_RDONLY, S_IRUSR);
|
||||
if (fd < 0) {
|
||||
@@ -123,8 +124,8 @@ static int32_t GetAppIdFromFile(const char *filePath, char *appId, uint32_t len)
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
int32_t fileLen = lseek(fd, 0, SEEK_END);
|
||||
if ((fileLen <=0) || (fileLen >= len)) {
|
||||
HILOGE("[fileLen is invalid or larger than available space, fileLen=%d]", fileLen);
|
||||
if (fileLen <= 0) {
|
||||
HILOGE("[fileLen is invalid, fileLen=%d]", fileLen);
|
||||
close(fd);
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
@@ -134,23 +135,34 @@ static int32_t GetAppIdFromFile(const char *filePath, char *appId, uint32_t len)
|
||||
close(fd);
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
ret = read(fd, appId, fileLen);
|
||||
if (ret < 0) {
|
||||
HILOGE("[read appId failed, ret=%d]", ret);
|
||||
int32_t appIdLen = fileLen + 1;
|
||||
char *appId = (char *)DMS_ALLOC(appIdLen);
|
||||
if (appId == NULL) {
|
||||
HILOGE("[DMS_ALLOC appId failed]");
|
||||
close(fd);
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
(void)memset_s(appId, appIdLen, 0x00, appIdLen);
|
||||
ret = read(fd, appId, fileLen);
|
||||
if (ret < 0) {
|
||||
HILOGE("[read appId failed, ret=%d]", ret);
|
||||
DMS_FREE(appId);
|
||||
close(fd);
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
for (; fileLen > 0; --fileLen) {
|
||||
if (appId[fileLen - 1] != '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
appId[fileLen] = '\0';
|
||||
bundleInfo->appId = appId;
|
||||
close(fd);
|
||||
return DMS_EC_SUCCESS;
|
||||
}
|
||||
|
||||
static int32_t GetAppIdFromBms(const CallerInfo *callerInfo, char *appId, uint32_t len)
|
||||
static int32_t GetBundleInfoFromBms(const CallerInfo *callerInfo, BundleInfo *bundleInfo)
|
||||
{
|
||||
BundleInfo bundleInfo;
|
||||
if (memset_s(&bundleInfo, sizeof(BundleInfo), 0x00, sizeof(BundleInfo)) != EOK) {
|
||||
HILOGE("[bundleInfo memset failed]");
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
int32_t errCode;
|
||||
#ifndef WEARABLE_PRODUCT
|
||||
char *bundleName = NULL;
|
||||
@@ -166,34 +178,31 @@ static int32_t GetAppIdFromBms(const CallerInfo *callerInfo, char *appId, uint32
|
||||
HILOGE("[GetBundleNameForUid failed]");
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
errCode = bmsServerProxy->GetBundleInfo(bundleName, GET_BUNDLE_WITHOUT_ABILITIES, &bundleInfo);
|
||||
errCode = bmsServerProxy->GetBundleInfo(bundleName, GET_BUNDLE_WITHOUT_ABILITIES, bundleInfo);
|
||||
} else if (callerUid == SHELL_UID) {
|
||||
/* inter-process mode (mainly called in xts testsuit process started by shell) */
|
||||
if (GetBundleNameForUid(callerInfo->uid, &bundleName) != EC_SUCCESS) {
|
||||
HILOGE("[GetBundleNameForUid failed]");
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
errCode = GetBundleInfo(bundleName, GET_BUNDLE_WITHOUT_ABILITIES, &bundleInfo);
|
||||
errCode = GetBundleInfo(bundleName, GET_BUNDLE_WITHOUT_ABILITIES, bundleInfo);
|
||||
} else {
|
||||
errCode = DMS_EC_FAILURE;
|
||||
}
|
||||
DMS_FREE(bundleName);
|
||||
#else
|
||||
errCode = GetBundleInfo(callerInfo->bundleName, GET_BUNDLE_WITHOUT_ABILITIES, &bundleInfo);
|
||||
errCode = GetBundleInfo(callerInfo->bundleName, GET_BUNDLE_WITHOUT_ABILITIES, bundleInfo);
|
||||
#endif
|
||||
if (errCode != EC_SUCCESS) {
|
||||
HILOGE("[GetBundleInfo failed]");
|
||||
return DMS_EC_GET_BUNDLEINFO_FAILURE;
|
||||
}
|
||||
if (strcpy_s(appId, len, bundleInfo.appId) != EOK) {
|
||||
HILOGE("[appId strcpy failed]");
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
return DMS_EC_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t GetAppId(const CallerInfo *callerInfo, char *appId, uint32_t len)
|
||||
int32_t GetCallerBundleInfo(const CallerInfo *callerInfo, BundleInfo *bundleInfo)
|
||||
{
|
||||
if ((callerInfo == NULL) || (appId == NULL) || (len == 0)) {
|
||||
if ((callerInfo == NULL) || (bundleInfo == NULL)) {
|
||||
HILOGE("[invalid parameter]");
|
||||
return DMS_EC_INVALID_PARAMETER;
|
||||
}
|
||||
@@ -206,8 +215,9 @@ int32_t GetAppId(const CallerInfo *callerInfo, char *appId, uint32_t len)
|
||||
HILOGE("[filePath sprintf failed]");
|
||||
return DMS_EC_FAILURE;
|
||||
}
|
||||
return GetAppIdFromFile(filePath, appId, len);
|
||||
bundleInfo->uid = callerInfo->uid;
|
||||
return GetBundleInfoFromFile(filePath, bundleInfo);
|
||||
}
|
||||
#endif
|
||||
return GetAppIdFromBms(callerInfo, appId, len);
|
||||
return GetBundleInfoFromBms(callerInfo, bundleInfo);
|
||||
}
|
||||
|
||||
@@ -69,11 +69,6 @@ void OnStartAbilityDone(int8_t errCode)
|
||||
HILOGD("[onStartAbilityDone errCode = %d]", errCode);
|
||||
}
|
||||
|
||||
void InitSoftbusService()
|
||||
{
|
||||
CreateDMSSessionServer();
|
||||
}
|
||||
|
||||
void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
|
||||
{
|
||||
HILOGD("[OnBytesReceived dataLen = %d]", dataLen);
|
||||
@@ -201,8 +196,8 @@ int32_t SendDmsMessage(const char *data, int32_t len, const char *deviceId, IDms
|
||||
return EC_FAILURE;
|
||||
}
|
||||
|
||||
if (g_curBusy) {
|
||||
HILOGE("[SendDmsMessage busy]");
|
||||
if (CreateDMSSessionServer() != EC_SUCCESS) {
|
||||
HILOGE("[CreateDMSSessionServer error]");
|
||||
return EC_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user