提交 63147ad1 编写于 作者: M mamingshuai

update openharmony 1.0.1

上级 301442fc
### 该问题是怎么引起的?
### 重现步骤
### 报错信息
### 相关的Issue
### 原因(目的、解决的问题等)
### 描述(做了什么,变更了什么)
### 测试用例(新增、改动、可能影响的功能)
文件模式从 100755 更改为 100644
# appspawn\_lite<a name="EN-US_TOPIC_0000001081995748"></a>
- [Introduction](#section469617221261)
- [Directory Structure](#section15884114210197)
- [Constraints](#section12212842173518)
- [Repositories Involved](#section641143415335)
## Introduction<a name="section469617221261"></a>
The appspawn\_lite module spawns application processes upon receiving commands from the application framework, configures permissions for new processes, and calls the entry function of the application framework.
## Directory Structure<a name="section15884114210197"></a>
```
base/startup/appspawn_lite/ # appspawn_lite module
├── LICENSE
└── services
├── include # Header files for the appspawn_lite module
├── src # Source files for the appspawn_lite module
└── test # Source files of the test cases for the appspawn_lite module
└── unittest
```
## Constraints<a name="section12212842173518"></a>
Currently, the appspawn\_lite module applies only to small-system devices \(reference memory ≥ 1 MB\), for example, Hi3516D V300 and Hi3518E V300.
## Repositories Involved<a name="section641143415335"></a>
Startup subsystem
hmf/startup/syspara\_lite
**hmf/startup/appspawn\_lite**
hmf/startup/bootstrap\_lite
hmf/startup/init\_lite
# appspawn应用孵化器组件<a name="ZH-CN_TOPIC_0000001081995748"></a>
- [简介](#section469617221261)
- [目录](#section15884114210197)
- [约束](#section12212842173518)
- [对应仓库](#section641143415335)
## 简介<a name="section469617221261"></a>
应用孵化器,负责接受应用程序框架的命令孵化应用进程,设置其对应权限,并调用应用程序框架的入口。
## 目录<a name="section15884114210197"></a>
```
base/startup/appspawn_lite/ # 应用孵化器组件
├── LICENSE
└── services
├── include # 应用孵化器组件头文件目录
├── src # 应用孵化器组件源文件目录
└── test # 应用孵化器组件测试用例源文件目录
└── unittest
```
## 约束<a name="section12212842173518"></a>
目前支持小型系统设备(参考内存≥1MB),如Hi3516DV300 、Hi3518EV300。
## 对应仓库<a name="section641143415335"></a>
启动恢复子系统
hmf/startup/syspara\_lite
**hmf/startup/appspawn\_lite**
hmf/startup/bootstrap\_lite
hmf/startup/init\_lite
hmf/startup/startup
hmf/startup/systemrestore
详见:https://gitee.com/openharmony/docs/blob/master/readme/启动恢复README.md
see: https://gitee.com/openharmony/docs/blob/master/docs-en/readme/startup.md
\ No newline at end of file
# Copyright (c) 2020 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.
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/subsystem/aafwk/config.gni")
lite_component("appspawn_lite") {
features = [
":appspawn",
]
}
# feature: appspawn
executable("appspawn") {
sources = [
"src/main.c",
"src/appspawn_process.c",
"src/appspawn_service.c",
"src/appspawn_message.c",
"src/appspawn_adapter.c",
]
ldflags = [
"-lstdc++",
]
cflags = [
"-Wall",
"-Wno-format",
"-Wno-format-extra-args",
]
include_dirs = [
"include",
"//utils/native/lite/include",
"//foundation/distributedschedule/interfaces/kits/samgr_lite/samgr",
"//foundation/distributedschedule/interfaces/kits/samgr_lite/registry",
"//foundation/aafwk/interfaces/innerkits/abilitymgr_lite",
"//third_party/bounds_checking_function/include/",
"//third_party/cJSON",
]
deps = [
"//third_party/bounds_checking_function:libsec_shared",
"//foundation/aafwk/frameworks/ability_lite:aafwk_abilitykit_lite",
"//foundation/distributedschedule/services/samgr_lite/samgr:samgr",
"//foundation/communication/frameworks/ipc_lite:liteipc_adapter",
"//third_party/cJSON:cjson_shared",
"//utils/native/lite/kv_store:kv_store",
]
if (enable_ohos_appexecfwk_feature_ability == true) {
deps += [
"//foundation/ace/frameworks/lite:ace_lite",
"//foundation/graphic/lite/frameworks/ui:ui",
"//foundation/graphic/lite/utils:graphic_utils",
"//foundation/graphic/lite/frameworks/surface",
]
}
if (ohos_kernel_type == "liteos_a") {
include_dirs += [
"//kernel/liteos_a/kernel/include",
"//kernel/liteos_a/kernel/common",
]
deps += [
"//base/hiviewdfx/frameworks/hilog_lite/featured:hilog_shared",
]
}
if (ohos_kernel_type == "linux") {
include_dirs += [
]
}
}
# Copyright (c) 2020 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.
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/subsystem/aafwk/config.gni")
lite_component("appspawn_lite") {
features = [ ":appspawn" ]
}
# feature: appspawn
executable("appspawn") {
sources = [
"src/appspawn_adapter.c",
"src/appspawn_message.c",
"src/appspawn_process.c",
"src/appspawn_service.c",
"src/main.c",
]
ldflags = [
"-lstdc++",
"-ldl",
]
cflags = [
"-Wall",
"-Wno-format",
"-Wno-format-extra-args",
]
include_dirs = [
"include",
"//utils/native/lite/include",
"//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr",
"//foundation/distributedschedule/samgr_lite/interfaces/kits/registry",
"${aafwk_lite_path}/interfaces/innerkits/abilitymgr_lite",
"//third_party/bounds_checking_function/include/",
"//third_party/cJSON",
]
deps = [
"${aafwk_lite_path}/frameworks/ability_lite:aafwk_abilitykit_lite",
"//build/lite/config/component/cJSON:cjson_shared",
"//foundation/communication/ipc_lite:liteipc_adapter",
"//foundation/distributedschedule/samgr_lite/samgr:samgr",
"//third_party/bounds_checking_function:libsec_shared",
"//utils/native/lite/kv_store:kv_store",
]
if (enable_ohos_appexecfwk_feature_ability == true) {
deps += [
"//foundation/ace/ace_engine_lite/frameworks:ace_lite",
"//foundation/graphic/surface:lite_surface",
"//foundation/graphic/ui:lite_ui",
"//foundation/graphic/utils:lite_graphic_utils",
]
}
if (ohos_kernel_type == "liteos_a") {
include_dirs += [
"//kernel/liteos_a/kernel/include",
"//kernel/liteos_a/kernel/common",
]
deps += [ "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared" ]
}
if (ohos_kernel_type == "linux") {
include_dirs += []
}
}
if (ohos_build_type == "debug") {
group("unittest") {
deps = [
"//base/startup/appspawn_lite/services/test/unittest/common:unittest",
]
}
}
......@@ -28,10 +28,27 @@ extern "C" {
#endif /* __cpluscplus */
#ifdef __LINUX__
/* Control the ambient capability set */
#ifndef PR_CAP_AMBIENT
#define PR_CAP_AMBIENT 47
#endif
#ifndef PR_CAP_AMBIENT_IS_SET
#define PR_CAP_AMBIENT_IS_SET 1
#endif
#ifndef PR_CAP_AMBIENT_RAISE
#define PR_CAP_AMBIENT_RAISE 2
#endif
#ifndef PR_CAP_AMBIENT_LOWER
#define PR_CAP_AMBIENT_LOWER 3
#endif
#ifndef PR_CAP_AMBIENT_CLEAR_ALL
#define PR_CAP_AMBIENT_CLEAR_ALL 4
#endif
extern int capset(void *a, void *b);
#endif
int KeepCapability();
int SetAmbientCapability(int cap);
#ifdef __cplusplus
#if __cplusplus
......
......@@ -26,6 +26,8 @@ typedef struct {
char* identityID;
int uID;
int gID;
unsigned int* caps;
unsigned int capsCnt;
} MessageSt;
int SplitMessage(const char* msg, unsigned int msgLen, MessageSt* msgSt);
......
......@@ -15,14 +15,29 @@
#include "appspawn_adapter.h"
#include <stdio.h>
#include <sys/prctl.h>
#ifdef __LINUX__
#include <linux/securebits.h>
#endif
int KeepCapability()
{
#ifdef __LINUX__
if (prctl(PR_SET_KEEPCAPS, 1)) {
if (prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP | SECBIT_NO_SETUID_FIXUP_LOCKED)) {
printf("prctl failed\n");
return -1;
}
#endif
return 0;
}
int SetAmbientCapability(int cap)
{
#ifdef __LINUX__
if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0)) {
printf("[Init] prctl PR_CAP_AMBIENT failed\n");
return -1;
}
#endif
return 0;
}
......@@ -17,6 +17,13 @@
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#ifdef __LINUX__
#include <linux/capability.h>
#else
#include <sys/capability.h>
#endif
#include "cJSON.h"
#include "log.h"
#include "ohos_errno.h"
......@@ -26,6 +33,7 @@ static const size_t MAX_BUNDLE_NAME_LEN = 127;
static const size_t MIN_BUNDLE_NAME_LEN = 7;
static const size_t MAX_IDENTITY_ID_LEN = 24;
static const size_t MIN_IDENTITY_ID_LEN = 1;
static const size_t MAX_CAPABILITY_COUNT = 10;
void FreeMessageSt(MessageSt* targetSt)
{
......@@ -40,8 +48,14 @@ void FreeMessageSt(MessageSt* targetSt)
targetSt->identityID = NULL;
}
if (targetSt->caps != NULL) {
free(targetSt->caps);
targetSt->caps = NULL;
}
targetSt->uID = -1;
targetSt->gID = -1;
targetSt->capsCnt = 0;
}
}
......@@ -86,20 +100,76 @@ static double ReadNumberItem(cJSON* strItem)
return cJSON_GetNumberValue(strItem);
}
static int GetCaps(const cJSON* curItem, MessageSt* msgSt)
{
msgSt->capsCnt = 0;
msgSt->caps = NULL;
cJSON* capItem = cJSON_GetObjectItem(curItem, "capability");
if (!cJSON_IsArray(capItem)) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] GetCaps failed, no caps array found.");
return EC_INVALID;
}
// caps array empty, means do not need any capability
int capsCnt = cJSON_GetArraySize(capItem);
if (capsCnt <= 0) {
return EC_SUCCESS;
}
if (capsCnt > MAX_CAPABILITY_COUNT) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] GetCaps, too many caps[cnt %{public}d], max %{public}d",\
capsCnt, MAX_CAPABILITY_COUNT);
return EC_INVALID;
}
msgSt->caps = (unsigned int*)malloc(sizeof(unsigned int) * capsCnt);
if (msgSt->caps == NULL) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] GetCaps, malloc failed! capsCnt[cnt %{public}d].", capsCnt);
return EC_NOMEMORY;
}
for (int i = 0; i < capsCnt; ++i) {
cJSON* capJ = cJSON_GetArrayItem(capItem, i);
if (!cJSON_IsNumber(capJ) || cJSON_GetNumberValue(capJ) < 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] GetCaps, invalid cap value detected!");
free(msgSt->caps);
msgSt->caps = NULL;
return EC_INVALID;
}
msgSt->caps[i] = (unsigned int)cJSON_GetNumberValue(capJ);
if (msgSt->caps[i] > CAP_LAST_CAP) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] GetCaps, invalid cap value %{public}u detected!",\
msgSt->caps[i]);
free(msgSt->caps);
msgSt->caps = NULL;
return EC_INVALID;
}
}
msgSt->capsCnt = capsCnt;
return EC_SUCCESS;
}
int SplitMessage(const char* msg, unsigned int msgLen, MessageSt* msgSt)
{
if (msg == NULL || msgLen == 0 || msgSt == NULL) {
if (msgSt == NULL) {
return EC_INVALID;
}
if (msg == NULL || msgLen == 0) {
FreeMessageSt(msgSt);
return EC_INVALID;
}
cJSON* rootJ = cJSON_ParseWithLength(msg, msgLen);
if (rootJ == NULL) {
FreeMessageSt(msgSt);
return EC_PROTOCOL;
}
cJSON* bundleNameItem = cJSON_GetObjectItem(rootJ, "bundleName");
int ret = ReadStringItem(bundleNameItem, &(msgSt->bundleName), MAX_BUNDLE_NAME_LEN, MIN_BUNDLE_NAME_LEN);
if (ret != EC_SUCCESS) {
FreeMessageSt(msgSt);
cJSON_Delete(rootJ);
return ret;
}
......@@ -116,6 +186,14 @@ int SplitMessage(const char* msg, unsigned int msgLen, MessageSt* msgSt)
cJSON* gIDItem = cJSON_GetObjectItem(rootJ, "gID");
msgSt->uID = (int)ReadNumberItem(uIDItem);
msgSt->gID = (int)ReadNumberItem(gIDItem);
ret = GetCaps(rootJ, msgSt);
if (ret != EC_SUCCESS) {
FreeMessageSt(msgSt);
cJSON_Delete(rootJ);
return ret;
}
cJSON_Delete(rootJ);
if (msgSt->uID <= 0 || msgSt->gID <= 0 || msgSt->uID == INT_MAX || msgSt->gID == INT_MAX) {
......@@ -123,4 +201,4 @@ int SplitMessage(const char* msg, unsigned int msgLen, MessageSt* msgSt)
return EC_PROTOCOL;
}
return EC_SUCCESS;
}
\ No newline at end of file
}
......@@ -18,12 +18,24 @@
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef OHOS_DEBUG
#include <time.h>
#endif // OHOS_DEBUG
#include <unistd.h>
#include "ability_main.h"
#include "appspawn_adapter.h"
#include "log.h"
#include "securec.h"
#ifdef __LINUX__
#include <sys/time.h>
#include <sys/resource.h>
static const rlim_t DEFAULT_RLIMIT = 40;
#endif // __LINUX__
#define DEFAULT_UMASK 022
#define CAP_NUM 2
#define ENV_TITLE "LD_LIBRARY_PATH="
......@@ -32,18 +44,39 @@
#define GRP_NUM 2
#define DEVMGR_GRP 99
static int SetPerms(uid_t uID, gid_t gID)
static int SetCapability(unsigned int capsCnt, const unsigned int* caps)
{
gid_t groups[GRP_NUM];
struct __user_cap_header_struct capHeader;
capHeader.version = _LINUX_CAPABILITY_VERSION_3;
capHeader.pid = 0;
if (KeepCapability() != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] KeepCapability failed, uID %{public}u, err: %{public}d.",\
uID, errno);
// common user, clear all caps
struct __user_cap_data_struct capData[CAP_NUM] = {0};
for (unsigned int i = 0; i < capsCnt; ++i) {
capData[CAP_TO_INDEX(caps[i])].effective |= CAP_TO_MASK(caps[i]);
capData[CAP_TO_INDEX(caps[i])].permitted |= CAP_TO_MASK(caps[i]);
capData[CAP_TO_INDEX(caps[i])].inheritable |= CAP_TO_MASK(caps[i]);
}
if (capset(&capHeader, capData) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] capset failed, err: %{public}d.", errno);
return -1;
}
for (unsigned int i = 0; i < capsCnt; ++i) {
if (SetAmbientCapability(caps[i]) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] SetAmbientCapability failed, err: %{public}d.", errno);
return -1;
}
}
return 0;
}
if (setuid(uID) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] setuid failed, uID %{public}u, err: %{public}d.",\
static int SetPerms(uid_t uID, gid_t gID, unsigned int capsCnt, const unsigned int* caps)
{
gid_t groups[GRP_NUM];
if (KeepCapability() != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] KeepCapability failed, uID %{public}u, err: %{public}d.",\
uID, errno);
return -1;
}
......@@ -54,6 +87,12 @@ static int SetPerms(uid_t uID, gid_t gID)
return -1;
}
if (setuid(uID) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] setuid failed, uID %{public}u, err: %{public}d.",\
uID, errno);
return -1;
}
// add device groups for system app
if (gID >= LOWER_BOUND_GID && gID <= UPPER_BOUND_GID) {
groups[0] = gID;
......@@ -68,17 +107,27 @@ static int SetPerms(uid_t uID, gid_t gID)
// umask call always succeeds and return the previous mask value which is not needed here
(void)umask(DEFAULT_UMASK);
struct __user_cap_header_struct capHeader;
capHeader.version = _LINUX_CAPABILITY_VERSION_3;
capHeader.pid = 0;
// common user, clear all caps
struct __user_cap_data_struct capData[CAP_NUM] = {0};
// set rlimit
#ifdef __LINUX__
struct rlimit rlim = {0};
rlim.rlim_cur = DEFAULT_RLIMIT;
rlim.rlim_max = DEFAULT_RLIMIT;
if (setrlimit(RLIMIT_NICE, &rlim) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] setrlimit failed, err: %{public}d.", errno);
return -1;
}
if (capset(&capHeader, capData) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] capset failed, err: %{public}d.", errno);
unsigned int tmpCaps[] = {17}; // 17 means CAP_SYS_RAWIO
unsigned int tmpsCapCnt = sizeof(tmpCaps) / sizeof(tmpCaps[0]);
if (SetCapability(tmpsCapCnt, tmpCaps) != 0) {
return -1;
}
#else
if (SetCapability(capsCnt, caps) != 0) {
return -1;
}
#endif // __LINUX__
return 0;
}
......@@ -92,13 +141,34 @@ pid_t CreateProcess(const MessageSt* msgSt)
// in child process
if (newPID == 0) {
#ifdef OHOS_DEBUG
struct timespec tmStart = {0};
if (clock_gettime(CLOCK_REALTIME, &tmStart) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sub-process, pid %{public}d. get time err %{public}d.",\
getpid(), errno);
}
#endif // OHOS_DEBUG
// set permissions
if (SetPerms(msgSt->uID, msgSt->gID) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] process %{public}s exit!", msgSt->bundleName);
if (SetPerms(msgSt->uID, msgSt->gID, msgSt->capsCnt, msgSt->caps) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sub-process %{public}s exit!", msgSt->bundleName);
exit(0x7f); // 0x7f: user specified
}
(void) prctl(PR_SET_NAME, msgSt->bundleName);
(void)prctl(PR_SET_NAME, msgSt->bundleName);
#ifdef OHOS_DEBUG
struct timespec tmEnd = {0};
if (clock_gettime(CLOCK_REALTIME, &tmEnd) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sub-process, pid %{public}d. get time2 err %{public}d.",\
getpid(), errno);
}
// 1s = 1000000000ns
long timeUsed = (tmEnd.tv_sec - tmStart.tv_sec) * 1000000000 + (tmEnd.tv_nsec - tmStart.tv_nsec);
HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] sub-process, pid %{public}d, timeused %ld ns.",\
getpid(), timeUsed);
#endif // OHOS_DEBUG
if (AbilityMain(msgSt->identityID) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] AbilityMain execute failed, pid %{public}d.", getpid());
exit(0x7f); // 0x7f: user specified
......@@ -109,4 +179,5 @@ pid_t CreateProcess(const MessageSt* msgSt)
}
return newPID;
}
\ No newline at end of file
}
......@@ -14,6 +14,12 @@
*/
#include "appspawn_service.h"
#include <stdlib.h>
#ifdef OHOS_DEBUG
#include <errno.h>
#include <time.h>
#endif // OHOS_DEBUG
#include "appspawn_message.h"
#include "appspawn_process.h"
#include "iproxy_server.h"
......@@ -26,6 +32,7 @@
#include "samgr_lite.h"
#include "service.h"
static const int INVALID_PID = -1;
typedef struct AppSpawnFeatureApi {
......@@ -75,51 +82,85 @@ static TaskConfig GetTaskConfig(Service* service)
return config;
}
static int Invoke(IServerProxy* iProxy, int funcId, void* origin, IpcIo* req, IpcIo* reply)
#ifdef OHOS_DEBUG
static void GetCurTime(struct timespec* tmCur)
{
(void)iProxy;
(void)origin;
if (reply == NULL) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, reply NULL!");
return EC_BADPTR;
if (clock_gettime(CLOCK_REALTIME, tmCur) != 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, get time failed! err %{public}d", errno);
}
}
#endif // OHOS_DEBUG
if (funcId != ID_CALL_CREATE_SERVICE || req == NULL) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, funcId %{public}d invalid, reply %{public}d.",\
funcId, INVALID_PID);
IpcIoPushInt64(reply, INVALID_PID);
return EC_BADPTR;
static int GetMessageSt(MessageSt* msgSt, IpcIo* req)
{
#ifdef __LINUX__
size_t len = 0;
char* str = IpcIoPopString(req, &len);
if (str == NULL || len == 0) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, get data failed.");
return EC_FAILURE;
}
int ret = SplitMessage(str, len, msgSt); // after split message, str no need to free(linux version)
#else
BuffPtr* dataPtr = IpcIoPopDataBuff(req);
if (dataPtr == NULL) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, get data failed, reply %{public}d.", INVALID_PID);
IpcIoPushInt64(reply, INVALID_PID);
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, get data failed.");
return EC_FAILURE;
}
MessageSt msgSt = {0};
int ret = SplitMessage((char*)dataPtr->buff, dataPtr->buffSz, &msgSt);
int ret = SplitMessage((char*)dataPtr->buff, dataPtr->buffSz, msgSt);
// release buffer
if (FreeBuffer(NULL, dataPtr->buff) != LITEIPC_OK) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, free buffer failed!");
}
#endif
return ret;
}
if (ret != EC_SUCCESS) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, parse failed! err %{public}d, reply %{public}d.",\
ret, INVALID_PID);
static int Invoke(IServerProxy* iProxy, int funcId, void* origin, IpcIo* req, IpcIo* reply)
{
#ifdef OHOS_DEBUG
struct timespec tmStart = {0};
GetCurTime(&tmStart);
#endif // OHOS_DEBUG
(void)iProxy;
(void)origin;
if (reply == NULL || funcId != ID_CALL_CREATE_SERVICE || req == NULL) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, funcId %{public}d invalid, reply %{public}d.",\
funcId, INVALID_PID);
IpcIoPushInt64(reply, INVALID_PID);
return EC_BADPTR;
}
MessageSt msgSt = {0};
if (GetMessageSt(&msgSt, req) != EC_SUCCESS) {
HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] invoke, parse failed! reply %{public}d.", INVALID_PID);
IpcIoPushInt64(reply, INVALID_PID);
return EC_FAILURE;
}
HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] msg<%{public}s,%{public}s,%{public}d,%{public}d>",\
HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] invoke, msg<%{public}s,%{public}s,%{public}d,%{public}d>",\
msgSt.bundleName, msgSt.identityID, msgSt.uID, msgSt.gID);
pid_t newPid = CreateProcess(&msgSt);
FreeMessageSt(&msgSt);
IpcIoPushInt64(reply, newPid);
#ifdef OHOS_DEBUG
struct timespec tmEnd = {0};
GetCurTime(&tmEnd);
// 1s = 1000000000ns
long timeUsed = (tmEnd.tv_sec - tmStart.tv_sec) * 1000000000 + (tmEnd.tv_nsec - tmStart.tv_nsec);
HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] invoke, reply pid %{public}d, timeused %{public}ld ns.",\
newPid, timeUsed);
#else
HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] invoke, reply pid %{public}d.", newPid);
#endif // OHOS_DEBUG
return ((newPid > 0) ? EC_SUCCESS : EC_FAILURE);
}
......@@ -153,4 +194,5 @@ void AppSpawnInit(void)
HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] register featureapi succeed.");
}
SYSEX_SERVICE_INIT(AppSpawnInit);
\ No newline at end of file
SYSEX_SERVICE_INIT(AppSpawnInit);
......@@ -61,6 +61,7 @@ void SignalRegist()
int main(int argc, char * const argv[])
{
sleep(1);
HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] main, enter.");
// 1. ipc module init
......
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
\ No newline at end of file
# Copyright (c) 2020 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.
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/test.gni")
unittest("appspawn_test") {
output_extension = "bin"
output_dir = "$root_out_dir/test/unittest/startup"
ldflags = [
"-lstdc++",
"-lpthread",
"-lm",
]
include_dirs = [
"//base/startup/appspawn_lite/services/include",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog",
"//third_party/bounds_checking_function/include/",
"//third_party/cJSON",
]
sources = [
"//base/startup/appspawn_lite/services/src/appspawn_message.c",
"message_func_test.cpp",
]
deps = [
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
"//build/lite/config/component/cJSON:cjson_shared",
"//third_party/bounds_checking_function:libsec_shared",
]
}
group("unittest") {
deps = [ ":appspawn_test" ]
}
/*
* Copyright (c) 2020 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/types.h>
#include <time.h>
#include <vector>
#include "gtest/gtest.h"
#include "appspawn_message.h"
using namespace testing::ext;
namespace OHOS {
const unsigned int MALLOC_TEST_LENGTH = 10;
const long NANOSECONDS_PER_SECOND = 1000000000;
const int TEST_UID = 999;
const int TEST_GID = 888;
std::vector<std::string> g_badStrings;
std::vector<std::string> g_goodStrings;
class StartupAppspawnUTest : public testing::Test {
public:
static void SetUpTestCase()
{
// empty
g_badStrings.push_back(std::string(""));
// not json
g_badStrings.push_back(std::string("abcdefghijklmnopqrstuvwxyz"));
g_badStrings.push_back(std::string("0123456789876543210"));
g_badStrings.push_back(std::string("xxxx"));
g_badStrings.push_back(std::string("xxxxssssssssssssssssssss"));
g_badStrings.push_back(std::string("\"\"\"\"\"\"\"\"\"\"\"\"\"\"\""));
g_badStrings.push_back(std::string("............................................."));
g_badStrings.push_back(std::string("....%%%....^..***@##.../*--++......$$&&....."));
// looks like json but format error
g_badStrings.push_back(std::string("{bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName:\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV,\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID:\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1,\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID:10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID:10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability:[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]"));
g_badStrings.push_back(std::string("\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\"\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\"\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\"\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\"\"uID\":10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\"10,\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10\"gID\":10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\"10,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\"[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0]},"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0],}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1\",\"uID\":10,\"gID\":10,\"capability\":[0,]}"));
// json format correct but fields missing
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000}"));
// field value invalid
g_badStrings.push_back(std::string("{\"bundleName\":\"\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"012345978901234597890123459789\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":-1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":-1000,\"capability\":[-7]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":1}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0, 3, -9]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[99999999]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":1234,\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":\"1000\",\"gID\":1000,\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":\"1000\",\"capability\":[0]}"));
g_badStrings.push_back(std::string("{\"bundleName\":\"nameV\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":\"0\"}"));
g_badStrings.push_back(std::string("{\"bundleName\": 250,\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_goodStrings.push_back(std::string("{\"bundleName\":\"testvalid1\",\"identityID\":\"1234\",\"uID\":1000,\"gID\":1000,\"capability\":[0]}"));
g_goodStrings.push_back(std::string("{\"bundleName\":\"testvalid2\",\"identityID\":\"5678\",\"uID\":1001,\"gID\":1001,\"capability\":[3,5]}"));
g_goodStrings.push_back(std::string("{\"bundleName\":\"testvalid3\",\"identityID\":\"91011\",\"uID\":1002,\"gID\":1002,\"capability\":[8,9]}"));
g_goodStrings.push_back(std::string("{\"bundleName\":\"testvalid3\",\"identityID\":\"999\",\"uID\":1002,\"gID\":1002,\"capability\":[]}"));
g_goodStrings.push_back(std::string("{\"bundleName\":\"testvalid3\",\"identityID\":\"3\",\"uID\":1002,\"gID\":1002,\"capability\":[1,2]}"));
printf("[----------] StartupAppspawnUTest, message func test setup.\n");
}
static void TearDownTestCase()
{
g_badStrings.clear();
g_goodStrings.clear();
printf("[----------] StartupAppspawnUTest, message func test teardown.\n");
}
void SetUp() {}
void TearDown() {}
};
/*
** @tc.name: msgFuncFreeTest_001
** @tc.desc: free message struct function, nullptr test
** @tc.type: FUNC
** @tc.require: AR000F733K
** @tc.author: ZAL
**/
HWTEST_F(StartupAppspawnUTest, msgFuncFreeTest_001, TestSize.Level0)
{
// do not crash here
FreeMessageSt(nullptr);
MessageSt msgSt = {0};
FreeMessageSt(&msgSt);
EXPECT_EQ(msgSt.bundleName, nullptr);
EXPECT_EQ(msgSt.identityID, nullptr);
EXPECT_EQ(msgSt.caps, nullptr);
EXPECT_EQ(msgSt.uID, -1);
EXPECT_EQ(msgSt.gID, -1);
EXPECT_EQ(msgSt.capsCnt, 0);
}
/*
** @tc.name: msgFuncFreeTest_002
** @tc.desc: free message struct function, function test
** @tc.type: FUNC
** @tc.require: AR000F733K
** @tc.author: ZAL
**/
HWTEST_F(StartupAppspawnUTest, msgFuncFreeTest_002, TestSize.Level0)
{
MessageSt msgSt = {0};
msgSt.capsCnt = MALLOC_TEST_LENGTH;
FreeMessageSt(&msgSt);
EXPECT_EQ(msgSt.capsCnt, 0);
msgSt.uID = TEST_UID;
FreeMessageSt(&msgSt);
EXPECT_EQ(msgSt.uID, -1);
msgSt.gID = TEST_GID;
FreeMessageSt(&msgSt);
EXPECT_EQ(msgSt.gID, -1);
msgSt.bundleName = (char*)malloc(MALLOC_TEST_LENGTH);
EXPECT_TRUE(msgSt.bundleName != nullptr);
FreeMessageSt(&msgSt);
EXPECT_EQ(msgSt.bundleName, nullptr);
msgSt.identityID = (char*)malloc(MALLOC_TEST_LENGTH);
EXPECT_TRUE(msgSt.identityID != nullptr);
FreeMessageSt(&msgSt);
EXPECT_EQ(msgSt.identityID, nullptr);
msgSt.caps = (unsigned int*)malloc(MALLOC_TEST_LENGTH * sizeof(unsigned int));
EXPECT_TRUE(msgSt.caps != nullptr);
FreeMessageSt(&msgSt);
EXPECT_EQ(msgSt.caps, nullptr);
// full test
msgSt.bundleName = (char*)malloc(MALLOC_TEST_LENGTH);
msgSt.identityID = (char*)malloc(MALLOC_TEST_LENGTH);
msgSt.caps = (unsigned int*)malloc(MALLOC_TEST_LENGTH * sizeof(unsigned int));
EXPECT_TRUE(msgSt.bundleName != nullptr);
EXPECT_TRUE(msgSt.identityID != nullptr);
EXPECT_TRUE(msgSt.caps != nullptr);
msgSt.capsCnt = MALLOC_TEST_LENGTH;
msgSt.uID = TEST_UID;
msgSt.gID = TEST_GID;
FreeMessageSt(&msgSt);
EXPECT_EQ(msgSt.bundleName, nullptr);
EXPECT_EQ(msgSt.identityID, nullptr);
EXPECT_EQ(msgSt.caps, nullptr);
EXPECT_EQ(msgSt.capsCnt, 0);
EXPECT_EQ(msgSt.uID, -1);
EXPECT_EQ(msgSt.gID, -1);
};
static void GetCurrentTime(struct timespec* tmCur)
{
if (clock_gettime(CLOCK_REALTIME, tmCur) != 0) {
printf("[----------] StartupAppspawnUTest, get time failed! err %d.\n", errno);
}
}
/*
** @tc.name: msgFuncSplitTest_001
** @tc.desc: split message function, bad strings test
** @tc.type: FUNC
** @tc.require: AR000F733K
** @tc.author: ZAL
**/
HWTEST_F(StartupAppspawnUTest, msgFuncSplitTest_001, TestSize.Level0)
{
MessageSt msgSt = {0};
EXPECT_NE(SplitMessage(nullptr, 0, nullptr), 0);
EXPECT_NE(SplitMessage(nullptr, 0, &msgSt), 0);
std::string testMsg = "xxxxxxxx";
EXPECT_NE(SplitMessage(testMsg.c_str(), testMsg.length(), nullptr), 0);
struct timespec tmStart = {0};
GetCurrentTime(&tmStart);
for (size_t i = 0; i < g_badStrings.size(); ++i) {
int ret = SplitMessage(g_badStrings[i].c_str(), g_badStrings[i].length(), &msgSt);
EXPECT_NE(ret, 0);
if (ret == 0) {
printf("[----------] StartupAppspawnUTest, msgFuncSplitTest_001 i = %u.\n", i);
FreeMessageSt(&msgSt);
}
}
struct timespec tmEnd = {0};
GetCurrentTime(&tmEnd);
long timeUsed = (tmEnd.tv_sec - tmStart.tv_sec) * NANOSECONDS_PER_SECOND + (tmEnd.tv_nsec - tmStart.tv_nsec);
printf("[----------] StartupAppspawnUTest, msgFuncSplitTest_001, total time %ld ns, strCnt %u.\n",\
timeUsed, g_badStrings.size());
}
/*
** @tc.name: msgFuncSplitTest_002
** @tc.desc: split message function, good strings test
** @tc.type: FUNC
** @tc.require: AR000F733K
** @tc.author: ZAL
**/
HWTEST_F(StartupAppspawnUTest, msgFuncSplitTest_002, TestSize.Level0)
{
MessageSt msgSt = {0};
struct timespec tmStart = {0};
GetCurrentTime(&tmStart);
for (size_t i = 0; i < g_goodStrings.size(); ++i) {
int ret = SplitMessage(g_goodStrings[i].c_str(), g_goodStrings[i].length(), &msgSt);
EXPECT_EQ(ret, 0);
if (ret != 0) {
printf("[----------] StartupAppspawnUTest, msgFuncSplitTest_002 i = %u.\n", i);
} else {
FreeMessageSt(&msgSt);
}
}
struct timespec tmEnd = {0};
GetCurrentTime(&tmEnd);
long timeUsed = (tmEnd.tv_sec - tmStart.tv_sec) * NANOSECONDS_PER_SECOND + (tmEnd.tv_nsec - tmStart.tv_nsec);
printf("[----------] StartupAppspawnUTest, msgFuncSplitTest_002, total time %ld ns, strCnt %u.\n", \
timeUsed, g_goodStrings.size());
// parse one good string and check all results
std::string validStr =
"{\"bundleName\":\"validName\",\"identityID\":\"135\",\"uID\":999,\"gID\":888,\"capability\":[0, 1, 5]}";
int ret = SplitMessage(validStr.c_str(), validStr.length(), &msgSt);
EXPECT_EQ(ret, 0);
std::vector<unsigned int> caps;
caps.push_back(0); // 0, test capability
caps.push_back(1); // 1, test capability
caps.push_back(5); // 5, test capability
EXPECT_EQ(strcmp("validName", msgSt.bundleName), 0);
EXPECT_EQ(strcmp("135", msgSt.identityID), 0);
EXPECT_EQ(TEST_UID, msgSt.uID);
EXPECT_EQ(TEST_GID, msgSt.gID);
EXPECT_EQ(caps.size(), msgSt.capsCnt);
for (size_t i = 0; i < caps.size(); ++i) {
EXPECT_EQ(caps[i], msgSt.caps[i]);
}
FreeMessageSt(&msgSt);
}
} // namespace OHOS
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册