Merge branch 'master' into sandbox-new

This commit is contained in:
zhongning5 2024-04-23 17:25:08 +08:00
commit f04fad1600
50 changed files with 4018 additions and 1486 deletions

View File

@ -130,6 +130,12 @@
}, {
"src-path" : "/data/local/ark-profile/<currentUserId>/<PackageName>",
"sandbox-path" : "/data/storage/ark-profile"
}, {
"src-path" : "/data/local/shader_cache/local/<PackageName>",
"sandbox-path" : "/data/storage/shader_cache/local"
}, {
"src-path" : "/data/local/shader_cache/cloud/<PackageName>",
"sandbox-path" : "/data/storage/shader_cache/cloud"
}, {
"src-path" : "/mnt/share/<currentUserId>/<PackageName_index>",
"sandbox-path" : "/data/storage/el2/share"
@ -243,6 +249,12 @@
"sandbox-path": "/data/medialibrary/database"
}
]
}, {
"name": "ohos.permission.ACCESS_SHADER_CACHE_DIR",
"mount-paths": [{
"sandbox-path": "/data/storage/shader_caches",
"src-path": "/data/local/shader_cache"
}]
}],
"spawn-flag": [{
"name": "START_FLAGS_BACKUP",

View File

@ -193,6 +193,16 @@
"sandbox-path" : "/data/storage/ark-profile",
"sandbox-flags" : [ "bind", "rec" ],
"check-action-status": "false"
}, {
"src-path" : "/data/local/shader_cache/local/<PackageName>",
"sandbox-path" : "/data/storage/shader_cache/local",
"sandbox-flags" : [ "bind", "rec" ],
"check-action-status": "false"
}, {
"src-path" : "/data/local/shader_cache/cloud/<PackageName>",
"sandbox-path" : "/data/storage/shader_cache/cloud",
"sandbox-flags" : [ "bind", "rec" ],
"check-action-status": "false"
}, {
"src-path" : "/mnt/share/<currentUserId>/<PackageName_index>",
"sandbox-path" : "/data/storage/el2/share",
@ -786,6 +796,15 @@
"sandbox-flags": [ "bind", "rec" ]
}
]
}],
"ohos.permission.ACCESS_SHADER_CACHE_DIR":[{
"sandbox-switch": "ON",
"mount-paths": [{
"src-path": "/data/local/shader_cache",
"sandbox-path": "/data/storage/shader_caches",
"sandbox-flags": [ "bind", "rec" ]
}
]
}]
}]
}

View File

@ -10,6 +10,46 @@
"application.ExtensionContext",
"application.AbilityStageContext",
"application.AbilityStage",
"app.ability.ExtensionAbility"
"app.ability.ExtensionAbility",
"app.ability.UIAbility",
"file.bfs",
"deviceinfo",
"data.preferences",
"window_napi",
"i18n",
"display_napi",
"hilog",
"net.connection",
"app.ability.AbilityStage",
"util",
"commoneventmanager",
"util.HashMap",
"security.cert",
"web.webview_napi",
"multimedia.image",
"security.cryptoFramework_napi",
"buffer",
"data.relationalStore",
"intl",
"fileio",
"util.HashSet",
"telephony.sim",
"systemDateTime",
"net.http",
"worker",
"application.ApplicationContext",
"hms.core.authentication",
"hms.core.hianalytics",
"multimedia.media",
"bundle",
"util.List",
"security.huks",
"account.osAccount",
"url",
"hitracemeter_napi",
"data.dataShare",
"pasteboard_napi",
"util.Queue",
"process"
]
}

View File

@ -66,6 +66,7 @@ APPSPAWN_STATIC int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *clie
int ret = AppSpawnExecuteClearEnvHook(content, client);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
NotifyResToParent(content, client, ret);
AppSpawnEnvClear(content, client);
return 0);
if (client->flags & APP_COLD_START) {
@ -78,11 +79,13 @@ APPSPAWN_STATIC int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *clie
ret = AppSpawnExecuteSpawningHook(content, client);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
NotifyResToParent(content, client, ret);
AppSpawnEnvClear(content, client);
return 0);
ret = AppSpawnExecutePreReplyHook(content, client);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
NotifyResToParent(content, client, ret);
AppSpawnEnvClear(content, client);
return 0);
// notify success to father process and start app process

View File

@ -24,7 +24,7 @@ if (defined(appspawn_sandbox_new) && appspawn_sandbox_new) {
ohos_prebuilt_etc("appdata-sandbox.json") {
source = "../appdata-sandbox-app.json"
output = "appdata-sandbox.json"
symlink_target_name = [ "appdata-sandbox.json" ]
part_name = "${part_name}"
module_install_dir = "etc/sandbox"
}

View File

@ -147,7 +147,7 @@ static int WriteMessage(int socketFd, const uint8_t *buf, ssize_t len)
const uint8_t *offset = buf;
for (ssize_t wLen = 0; remain > 0; offset += wLen, remain -= wLen, written += wLen) {
wLen = send(socketFd, offset, remain, MSG_NOSIGNAL);
APPSPAWN_LOGV("Write msg errno: %{public}d %{public}d", errno, wLen);
APPSPAWN_LOGV("Write msg errno: %{public}d %{public}zd", errno, wLen);
APPSPAWN_CHECK((wLen > 0) || (errno == EINTR), return -errno,
"Failed to write message to fd %{public}d, wLen %{public}zd errno: %{public}d", socketFd, wLen, errno);
}
@ -237,9 +237,7 @@ int AppSpawnClientInit(const char *serviceName, AppSpawnClientHandle *handle)
if (strcmp(serviceName, NWEBSPAWN_SERVER_NAME) == 0 || strstr(serviceName, NWEBSPAWN_SOCKET_NAME) != NULL) {
type = CLIENT_FOR_NWEBSPAWN;
}
int ret = LoadPermission(type);
APPSPAWN_CHECK(ret == 0, return APPSPAWN_SYSTEM_ERROR, "Failed to load permission");
ret = InitClientInstance(type);
int ret = InitClientInstance(type);
APPSPAWN_CHECK(ret == 0, return APPSPAWN_SYSTEM_ERROR, "Failed to create reqMgr");
*handle = (AppSpawnClientHandle)g_clientInstance[type];
return 0;
@ -249,8 +247,6 @@ int AppSpawnClientDestroy(AppSpawnClientHandle handle)
{
AppSpawnReqMsgMgr *reqMgr = (AppSpawnReqMsgMgr *)handle;
APPSPAWN_CHECK(reqMgr != NULL, return APPSPAWN_SYSTEM_ERROR, "Invalid reqMgr");
// delete permission
DeletePermission(reqMgr->type);
pthread_mutex_lock(&g_mutex);
if (reqMgr->type < sizeof(g_clientInstance) / sizeof(g_clientInstance[0])) {
g_clientInstance[reqMgr->type] = NULL;

View File

@ -81,8 +81,6 @@ typedef struct {
uint16_t dataType;
} AppSpawnAppData;
int LoadPermission(AppSpawnClientType type);
void DeletePermission(AppSpawnClientType type);
int32_t GetPermissionMaxCount();
#ifdef __cplusplus

View File

@ -29,10 +29,9 @@ static inline int SetAppSpawnMsgFlags(AppSpawnMsgFlags *msgFlags, uint32_t index
{
uint32_t blockIndex = index / 32; // 32 max bit in int
uint32_t bitIndex = index % 32; // 32 max bit in int
if (blockIndex >= msgFlags->count) {
return -1;
if (blockIndex < msgFlags->count) {
msgFlags->flags[blockIndex] |= (1 << bitIndex);
}
msgFlags->flags[blockIndex] |= (1 << bitIndex);
return 0;
}
@ -62,11 +61,10 @@ static inline int CheckInputString(const char *info, const char *value, uint32_t
static AppSpawnMsgBlock *CreateAppSpawnMsgBlock(AppSpawnReqMsgNode *reqNode)
{
uint32_t realLen = sizeof(AppSpawnMsgBlock) + MAX_MSG_BLOCK_LEN;
AppSpawnMsgBlock *block = (AppSpawnMsgBlock *)calloc(1, realLen);
AppSpawnMsgBlock *block = (AppSpawnMsgBlock *)calloc(1, MAX_MSG_BLOCK_LEN);
APPSPAWN_CHECK(block != NULL, return NULL, "Failed to create block");
OH_ListInit(&block->node);
block->blockSize = MAX_MSG_BLOCK_LEN;
block->blockSize = MAX_MSG_BLOCK_LEN - sizeof(AppSpawnMsgBlock);
block->currentIndex = 0;
OH_ListAddTail(&reqNode->msgBlocks, &block->node);
return block;
@ -89,12 +87,11 @@ static AppSpawnMsgBlock *GetValidMsgBlock(const AppSpawnReqMsgNode *reqNode, uin
static AppSpawnMsgBlock *GetTailMsgBlock(const AppSpawnReqMsgNode *reqNode)
{
AppSpawnMsgBlock *block = NULL;
struct ListNode *node = reqNode->msgBlocks.next;
struct ListNode *node = reqNode->msgBlocks.prev;
if (node != &reqNode->msgBlocks) {
block = ListEntry(node, AppSpawnMsgBlock, node);
return block;
}
return NULL;
return block;
}
static void FreeMsgBlock(ListNode *node)
@ -114,22 +111,22 @@ static int AddAppDataToBlock(AppSpawnMsgBlock *block, const uint8_t *data, uint3
APPSPAWN_CHECK(reminderLen >= realDataLen, return APPSPAWN_BUFFER_NOT_ENOUGH, "Not enough buffer for data");
int ret = memcpy_s(block->buffer + block->currentIndex, reminderLen, data, dataLen);
APPSPAWN_CHECK(ret == EOK, return APPSPAWN_SYSTEM_ERROR, "Failed to copy data");
block->currentIndex += realDataLen;
if (dataType == DATA_TYPE_STRING) {
*((char *)block->buffer + block->currentIndex + dataLen) = '\0';
}
block->currentIndex += realDataLen;
return 0;
}
static int AddAppDataToTail(AppSpawnReqMsgNode *reqNode, const uint8_t *data, uint32_t dataLen, int32_t dataType)
{
// 最后一个block有有效空间, 则保存部分数据剩余的申请新的block保存
uint32_t currLen = 0;
AppSpawnMsgBlock *block = GetTailMsgBlock(reqNode);
APPSPAWN_CHECK(block != NULL, return APPSPAWN_BUFFER_NOT_ENOUGH, "Not block info reqNode");
uint32_t realDataLen = (dataType == DATA_TYPE_STRING) ? dataLen + 1 : dataLen;
do {
uint32_t reminderBufferLen = block->blockSize - block->currentIndex;
uint32_t reminderDataLen = (dataType == DATA_TYPE_STRING) ? dataLen + 1 - currLen : dataLen - currLen;
uint32_t reminderDataLen = realDataLen - currLen;
uint32_t realLen = APPSPAWN_ALIGN(reminderDataLen);
uint32_t realCopy = 0;
if (reminderBufferLen >= realLen) { // 足够存储,直接保存
@ -137,7 +134,7 @@ static int AddAppDataToTail(AppSpawnReqMsgNode *reqNode, const uint8_t *data, ui
APPSPAWN_CHECK(ret == EOK, return APPSPAWN_SYSTEM_ERROR, "Failed to copy data");
block->currentIndex += realLen;
break;
} else if (reminderBufferLen > 0) { // 按实际大小保存
} else if (reminderBufferLen > 0) {
realCopy = reminderDataLen > reminderBufferLen ? reminderBufferLen : reminderDataLen;
int ret = memcpy_s(block->buffer + block->currentIndex, reminderBufferLen, data + currLen, realCopy);
APPSPAWN_CHECK(ret == EOK, return APPSPAWN_SYSTEM_ERROR, "Failed to copy data");
@ -146,10 +143,7 @@ static int AddAppDataToTail(AppSpawnReqMsgNode *reqNode, const uint8_t *data, ui
}
block = CreateAppSpawnMsgBlock(reqNode);
APPSPAWN_CHECK(block != NULL, return APPSPAWN_SYSTEM_ERROR, "Not enough buffer for data");
if (currLen == dataLen) { // 实际数据已经完成,但是需要补齐对齐造成的扩展
block->currentIndex += realLen - realCopy;
}
} while (currLen < dataLen);
} while (currLen < realDataLen);
return 0;
}
@ -334,10 +328,11 @@ static void GetSpecialGid(const char *bundleName, gid_t gidTable[], uint32_t *gi
int AppSpawnReqMsgCreate(AppSpawnMsgType msgType, const char *processName, AppSpawnReqMsgHandle *reqHandle)
{
APPSPAWN_CHECK(processName != NULL, return APPSPAWN_ARG_INVALID, "Invalid process name");
APPSPAWN_CHECK(reqHandle != NULL, return APPSPAWN_ARG_INVALID, "Invalid request handle");
APPSPAWN_CHECK(msgType < MAX_TYPE_INVALID,
return APPSPAWN_MSG_INVALID, "Invalid message type %{public}u %{public}s", msgType, processName);
int ret = CheckInputString("processName", processName, APP_LEN_PROC_NAME);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
AppSpawnReqMsgNode *reqNode = CreateAppSpawnReqMsg(msgType, processName);
APPSPAWN_CHECK(reqNode != NULL, return APPSPAWN_SYSTEM_ERROR,
"Failed to create msg node for %{public}s", processName);
@ -400,9 +395,9 @@ int AppSpawnReqMsgAddExtInfo(AppSpawnReqMsgHandle reqHandle, const char *name, c
{
AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
APPSPAWN_CHECK(name != NULL && strlen(name) < APPSPAWN_TLV_NAME_LEN,
return APPSPAWN_ARG_INVALID, "Invalid ext name ");
APPSPAWN_CHECK(value != NULL && valueLen <= EXTRAINFO_TOTAL_LENGTH_MAX,
int ret = CheckInputString("check name", name, APPSPAWN_TLV_NAME_LEN);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
APPSPAWN_CHECK(value != NULL && valueLen <= EXTRAINFO_TOTAL_LENGTH_MAX && valueLen > 0,
return APPSPAWN_ARG_INVALID, "Invalid ext value ");
APPSPAWN_LOGV("AppSpawnReqMsgAddExtInfo name %{public}s", name);
@ -416,9 +411,9 @@ int AppSpawnReqMsgAddStringInfo(AppSpawnReqMsgHandle reqHandle, const char *name
{
AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
APPSPAWN_CHECK(name != NULL && strlen(name) < APPSPAWN_TLV_NAME_LEN,
return APPSPAWN_ARG_INVALID, "Invalid tlv name ");
int ret = CheckInputString(name, value, EXTRAINFO_TOTAL_LENGTH_MAX);
int ret = CheckInputString("check name", name, APPSPAWN_TLV_NAME_LEN);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
ret = CheckInputString(name, value, EXTRAINFO_TOTAL_LENGTH_MAX);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
APPSPAWN_LOGV("AppSpawnReqMsgAddStringInfo name %{public}s", name);
@ -532,7 +527,7 @@ int AppSpawnClientAddPermission(AppSpawnClientHandle handle, AppSpawnReqMsgHandl
int index = GetPermissionIndex(handle, permission);
APPSPAWN_CHECK(index >= 0 && index < maxIndex,
return APPSPAWN_PERMISSION_NOT_SUPPORT, "Invalid permission %{public}s", permission);
APPSPAWN_LOGV("AetPermission index %{public}d name %{public}s", index, permission);
APPSPAWN_LOGV("add permission index %{public}d name %{public}s", index, permission);
int ret = SetAppSpawnMsgFlags(reqNode->permissionFlags, index);
APPSPAWN_CHECK(ret == 0, return ret, "Invalid permission %{public}s", permission);
return 0;

View File

@ -39,30 +39,20 @@ static PermissionManager g_permissionMgr[CLIENT_MAX] = {};
#ifdef APPSPAWN_SANDBOX_NEW
static int ParseAppSandboxConfig(const cJSON *root, PermissionManager *mgr)
{
APPSPAWN_LOGE("ParseAppSandboxConfig");
// conditional
cJSON *json = cJSON_GetObjectItemCaseSensitive(root, "conditional");
if (json == NULL) {
return 0;
}
APPSPAWN_CHECK(json != NULL, return 0, "No found conditional in config");
// permission
cJSON *config = cJSON_GetObjectItemCaseSensitive(json, "permission");
if (config == NULL || !cJSON_IsArray(config)) {
return 0;
}
APPSPAWN_CHECK(config != NULL && cJSON_IsArray(config), return 0, "No found permission in config");
uint32_t configSize = cJSON_GetArraySize(config);
for (uint32_t i = 0; i < configSize; i++) {
cJSON *json = cJSON_GetArrayItem(config, i);
if (json == NULL) {
continue;
}
char *name = GetStringFromJsonObj(json, "name");
APPSPAWN_LOGV("ParsePermissionConfig %{public}s", name);
if (name == NULL) {
APPSPAWN_LOGE("No name in permission configs");
continue;
}
APPSPAWN_CHECK(name != NULL, break, "No found name in config");
int ret = AddSandboxPermissionNode(name, &mgr->permissionQueue);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
}
@ -83,7 +73,6 @@ static int ParsePermissionConfig(const cJSON *permissionConfigs, PermissionManag
cJSON_ArrayForEach(config, permissionConfigs)
{
const char *name = config->string;
APPSPAWN_LOGV("ParsePermissionConfig %{public}s", name);
int ret = AddSandboxPermissionNode(name, &mgr->permissionQueue);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
}
@ -115,52 +104,47 @@ static PermissionManager *GetPermissionMgrByType(AppSpawnClientType type)
static int LoadPermissionConfig(PermissionManager *mgr)
{
APPSPAWN_CHECK(mgr != NULL, return -1, "Invalid permission mgr");
int ret = ParseJsonConfig("etc/sandbox",
mgr->type == CLIENT_FOR_APPSPAWN ? APP_SANDBOX_FILE_NAME : WEB_SANDBOX_FILE_NAME, ParseAppSandboxConfig, mgr);
if (ret == APPSPAWN_SANDBOX_NONE) {
APPSPAWN_LOGW("No sandbox config");
ret = 0;
}
APPSPAWN_CHECK(ret == 0, return ret, "Load sandbox fail");
APPSPAWN_CHECK(ret == 0, return ret, "Load sandbox fail %{public}d", ret);
mgr->maxPermissionIndex = PermissionRenumber(&mgr->permissionQueue);
return 0;
}
static inline int32_t CheckPermissionManager(PermissionManager *mgr)
{
if (mgr != NULL && mgr->inited) {
return 1;
}
return 0;
}
static int32_t PMGetPermissionIndex(AppSpawnClientType type, const char *permission)
{
PermissionManager *mgr = GetPermissionMgrByType(type);
APPSPAWN_CHECK_ONLY_EXPER(mgr != NULL, return INVALID_PERMISSION_INDEX);
APPSPAWN_CHECK_ONLY_EXPER(mgr->inited && mgr->maxPermissionIndex >= 0, return INVALID_PERMISSION_INDEX);
APPSPAWN_CHECK_ONLY_EXPER(permission != NULL, return INVALID_PERMISSION_INDEX);
APPSPAWN_CHECK_ONLY_EXPER(CheckPermissionManager(mgr), return INVALID_PERMISSION_INDEX);
return GetPermissionIndexInQueue((SandboxQueue *)&mgr->permissionQueue, permission);
}
static int32_t PMGetMaxPermissionIndex(AppSpawnClientType type)
{
PermissionManager *mgr = GetPermissionMgrByType(type);
APPSPAWN_CHECK_ONLY_EXPER(mgr != NULL, return INVALID_PERMISSION_INDEX);
APPSPAWN_CHECK_ONLY_EXPER(mgr->inited && mgr->maxPermissionIndex >= 0, return INVALID_PERMISSION_INDEX);
APPSPAWN_CHECK_ONLY_EXPER(CheckPermissionManager(mgr), return INVALID_PERMISSION_INDEX);
return mgr->maxPermissionIndex;
}
static const char *PMGetPermissionByIndex(AppSpawnClientType type, int32_t index)
{
PermissionManager *mgr = GetPermissionMgrByType(type);
APPSPAWN_CHECK_ONLY_EXPER(mgr != NULL, return NULL);
APPSPAWN_CHECK_ONLY_EXPER(mgr->inited && mgr->maxPermissionIndex >= 0, return NULL);
APPSPAWN_CHECK_ONLY_EXPER(CheckPermissionManager(mgr), return NULL);
if (mgr->maxPermissionIndex <= index) {
return NULL;
}
const SandboxPermissionNode *node = GetPermissionNodeInQueueByIndex((SandboxQueue *)&mgr->permissionQueue, index);
#ifdef APPSPAWN_CLIENT
return node == NULL ? NULL : node->name;
#else
return node == NULL ? NULL : node->section.name;
#endif
return PERMISSION_NAME(node);
}
int LoadPermission(AppSpawnClientType type)
APPSPAWN_STATIC int LoadPermission(AppSpawnClientType type)
{
APPSPAWN_LOGW("LoadPermission %{public}d", type);
pthread_mutex_lock(&g_mutex);
@ -178,25 +162,19 @@ int LoadPermission(AppSpawnClientType type)
mgr->permissionQueue.type = 0;
OH_ListInit(&mgr->permissionQueue.front);
int ret = LoadPermissionConfig(mgr);
if (ret != 0) {
pthread_mutex_unlock(&g_mutex);
return ret;
if (ret == 0) {
mgr->inited = 1;
}
mgr->inited = 1;
pthread_mutex_unlock(&g_mutex);
return 0;
return ret;
}
void DeletePermission(AppSpawnClientType type)
APPSPAWN_STATIC void DeletePermission(AppSpawnClientType type)
{
APPSPAWN_LOGW("DeletePermission %{public}d", type);
pthread_mutex_lock(&g_mutex);
PermissionManager *mgr = GetPermissionMgrByType(type);
if (mgr == NULL) {
pthread_mutex_unlock(&g_mutex);
return;
}
if (!mgr->inited) {
if (mgr == NULL || !mgr->inited) {
pthread_mutex_unlock(&g_mutex);
return;
}

View File

@ -41,6 +41,13 @@ typedef struct TagPermissionNode {
char name[0];
} SandboxPermissionNode;
#endif
#ifdef APPSPAWN_CLIENT
#define PERMISSION_NAME(node) (node) == NULL ? NULL : (node)->name;
#else
#define PERMISSION_NAME(node) (node) == NULL ? NULL : (node)->section.name;
#endif
#ifdef __cplusplus
}
#endif

View File

@ -119,7 +119,7 @@ static void MountAppEl2Dir(const AppSpawningCtx *property)
MakeDirRec(path, DIR_MODE, 1);
if (mount(path, path, NULL, MS_BIND | MS_REC, NULL) != 0) {
free(path);
APPSPAWN_LOGI("mount el2 path failed!");
APPSPAWN_LOGI("mount el2 path failed! error: %{public}d %{public}s", errno, path);
return;
}
if (mount(NULL, path, NULL, MS_SHARED, NULL) != 0) {

View File

@ -12,15 +12,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dirent.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "appspawn_adapter.h"
#include "appspawn_hook.h"
#include "appspawn_manager.h"
#include "appspawn_utils.h"
@ -38,6 +38,7 @@ typedef struct {
int nsInitPidFd; // ns pid fd of pid_ns_init
} AppSpawnNamespace;
static pid_t GetPidByName(const char *name);
static int AppSpawnExtDataCompareDataId(ListNode *node, void *data)
{
AppSpawnExtData *extData = (AppSpawnExtData *)ListEntry(node, AppSpawnExtData, node);
@ -171,13 +172,13 @@ static int GetNsPidFd(pid_t pid)
return nsFd;
}
static int PreLoadEnablePidNs(AppSpawnMgr *content)
APPSPAWN_STATIC int PreLoadEnablePidNs(AppSpawnMgr *content)
{
APPSPAWN_LOGI("Enable pid namespace flags: 0x%{public}x", content->content.sandboxNsFlags);
if (IsColdRunMode(content)) {
return 0;
}
if (IsNWebSpawnMode(content)) { // only for appspawn
if (IsNWebSpawnMode(content)) { // only for appspawn
return 0;
}
if (!(content->content.sandboxNsFlags & CLONE_NEWPID)) {
@ -186,15 +187,11 @@ static int PreLoadEnablePidNs(AppSpawnMgr *content)
AppSpawnNamespace *namespace = CreateAppSpawnNamespace();
APPSPAWN_CHECK(namespace != NULL, return -1, "Failed to create namespace");
#ifndef APPSPAWN_TEST
int ret = 0;
#else
int ret = 0;
#endif
int ret = -1;
// check if process pid_ns_init exists, this is the init process for pid namespace
pid_t pid = GetPidByName("pid_ns_init");
if (pid == -1) {
APPSPAWN_LOGI("Start Create pid_ns_init");
APPSPAWN_LOGI("Start Create pid_ns_init %{public}d", pid);
pid = clone(NsInitFunc, NULL, CLONE_NEWPID, NULL);
if (pid < 0) {
APPSPAWN_LOGE("clone pid ns init failed");
@ -218,7 +215,7 @@ static int PreLoadEnablePidNs(AppSpawnMgr *content)
DeleteAppSpawnNamespace(namespace);
return ret;
}
OH_ListAddTail(&namespace->extData.node, &content->extData);
OH_ListAddTail(&content->extData, &namespace->extData.node);
APPSPAWN_LOGI("Enable pid namespace success.");
return 0;
}
@ -227,10 +224,12 @@ static int PreLoadEnablePidNs(AppSpawnMgr *content)
static int SetPidNamespace(int nsPidFd, int nsType)
{
APPSPAWN_LOGI("SetPidNamespace 0x%{public}x", nsType);
#ifndef APPSPAWN_TEST
if (setns(nsPidFd, nsType) < 0) {
APPSPAWN_LOGE("set pid namespace nsType:%{public}d failed", nsType);
return -1;
}
#endif
return 0;
}

View File

@ -70,7 +70,8 @@ typedef enum TagAppSpawnHookStage {
STAGE_CHILD_EXECUTE,
STAGE_CHILD_PRE_RELY,
STAGE_CHILD_POST_RELY,
STAGE_CHILD_PRE_RUN
STAGE_CHILD_PRE_RUN,
STAGE_MAX
} AppSpawnHookStage;
typedef enum TagAppSpawnHookPrio {

View File

@ -29,6 +29,11 @@ extern "C" {
#endif
#define HOOK_STOP_WHEN_ERROR 0x2
#if defined(__aarch64__) || defined(__x86_64__)
#define ASAN_MODULE_PATH "/system/lib64/appspawn/libappspawn_asan"
#else
#define ASAN_MODULE_PATH "/system/lib/appspawn/libappspawn_asan"
#endif
typedef enum {
MODULE_DEFAULT,

View File

@ -705,7 +705,9 @@ static bool IsSandboxMounted(const AppSpawnSandboxCfg *sandbox, const char *name
FILE *f = fopen(path, "rb");
if (f != NULL) {
fclose(f);
#ifndef APPSPAWN_TEST
return true;
#endif
}
return false;
}
@ -817,6 +819,7 @@ int StagedMountSystemConst(const AppSpawnSandboxCfg *sandbox, const AppSpawningC
if (IsSandboxMounted(sandbox, "system-const", context->rootPath)) {
APPSPAWN_LOGV("Sandbox system-const %{public}s has been mount", context->rootPath);
DeleteSandboxContext(context);
return 0;
}

View File

@ -36,7 +36,6 @@ extern "C" {
#define PHYSICAL_APP_INSTALL_PATH "/data/app/el1/bundle/public/"
#define APL_SYSTEM_CORE "system_core"
#define APL_SYSTEM_BASIC "system_basic"
#define MODULE_TEST_BUNDLE_NAME "moduleTestProcessName"
#define DEFAULT_NWEB_SANDBOX_SEC_PATH "/data/app/el1/bundle/public/com.ohos.nweb" // persist.nweb.sandbox.src_path
#define PARAMETER_PACKAGE_NAME "<PackageName>"

View File

@ -98,11 +98,18 @@ static inline bool GetBoolParameter(const char *param, bool value)
char tmp[32] = {0}; // 32 max
int ret = GetParameter(param, "", tmp, sizeof(tmp));
APPSPAWN_LOGV("GetBoolParameter key %{public}s ret %{public}d result: %{public}s", param, ret, tmp);
return (ret > 0 && (strcmp(tmp, "true") == 0 || strcmp(tmp, "on") == 0 || strcmp(tmp, "1") == 0));
if (ret > 0 && strcmp(tmp, "false") == 0) {
return false;
}
if (ret > 0 && strcmp(tmp, "true") == 0) {
return true;
}
return value;
}
static inline bool AppSandboxPidNsIsSupport(void)
{
// only set false, return false
return GetBoolParameter("const.sandbox.pidns.support", true);
}
@ -651,7 +658,10 @@ int LoadAppSandboxConfig(AppSpawnSandboxCfg *sandbox, int nwebSpawn)
{
APPSPAWN_CHECK_ONLY_EXPER(sandbox != NULL, return APPSPAWN_ARG_INVALID);
const char *sandboxName = nwebSpawn ? WEB_SANDBOX_FILE_NAME : APP_SANDBOX_FILE_NAME;
if (sandbox->depGroupNodes != NULL) {
APPSPAWN_LOGW("Sandbox has been load");
return 0;
}
ParseJsonContext context = {};
context.sandboxCfg = sandbox;
int ret = ParseJsonConfig("etc/sandbox", sandboxName, ParseAppSandboxConfig, &context);

View File

@ -489,7 +489,7 @@ APPSPAWN_STATIC int PreLoadSandboxCfg(AppSpawnMgr *content)
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != NULL, return APPSPAWN_SYSTEM_ERROR);
OH_ListAddTail(&sandbox->extData.node, &content->extData);
OH_ListAddTail(&content->extData, &sandbox->extData.node);
// load app sandbox config
LoadAppSandboxConfig(sandbox, IsNWebSpawnMode(content));

0
modules/sandbox/sandbox_utils.cpp Executable file → Normal file
View File

0
standard/BUILD.gn Executable file → Normal file
View File

45
standard/appspawn_appmgr.c Executable file → Normal file
View File

@ -36,6 +36,7 @@ static AppSpawnMgr *g_appSpawnMgr = NULL;
AppSpawnMgr *CreateAppSpawnMgr(int mode)
{
APPSPAWN_CHECK_ONLY_EXPER(mode < MODE_INVALID, return NULL);
if (g_appSpawnMgr != NULL) {
return g_appSpawnMgr;
}
@ -191,23 +192,25 @@ AppSpawnedProcess *GetSpawnedProcessByName(const char *name)
return ListEntry(node, AppSpawnedProcess, node);
}
int KillAndWaitStatus(pid_t pid, int sig)
int KillAndWaitStatus(pid_t pid, int sig, int *exitStatus)
{
APPSPAWN_CHECK_ONLY_EXPER(exitStatus != NULL, return 0);
*exitStatus = -1;
if (pid <= 0) {
return 0;
}
int exitStatus = 0;
if (kill(pid, sig) != 0) {
APPSPAWN_LOGE("unable to kill process, pid: %{public}d ret %{public}d", pid, errno);
return -1;
}
pid_t exitPid = waitpid(pid, &exitStatus, 0);
pid_t exitPid = waitpid(pid, exitStatus, 0);
if (exitPid != pid) {
APPSPAWN_LOGE("waitpid failed, pid: %{public}d %{public}d, status: %{public}d", exitPid, pid, exitStatus);
APPSPAWN_LOGE("waitpid failed, pid: %{public}d %{public}d, status: %{public}d", exitPid, pid, *exitStatus);
return -1;
}
return exitStatus;
return 0;
}
static int GetProcessTerminationStatus(pid_t pid)
@ -226,7 +229,17 @@ static int GetProcessTerminationStatus(pid_t pid)
free(info);
return exitStatus;
}
return KillAndWaitStatus(pid, SIGKILL);
AppSpawnedProcess *app = GetSpawnedProcess(pid);
if (app == NULL) {
APPSPAWN_LOGE("unable to get process, pid: %{public}d ", pid);
return -1;
}
if (KillAndWaitStatus(pid, SIGKILL, &exitStatus) == 0) { // kill success, delete app
OH_ListRemove(&app->node);
free(app);
}
return exitStatus;
}
AppSpawningCtx *CreateAppSpawningCtx(void)
@ -323,26 +336,6 @@ void AppSpawningCtxTraversal(ProcessTraversal traversal, void *data)
}
}
static inline int SetSpawnMsgFlags(AppSpawnMsgFlags *msgFlags, uint32_t index)
{
uint32_t blockIndex = index / 32; // 32 max bit in int
uint32_t bitIndex = index % 32; // 32 max bit in int
if (blockIndex >= msgFlags->count) {
return -1;
}
msgFlags->flags[blockIndex] |= (1 << bitIndex);
return 0;
}
int SetAppSpawnMsgFlag(const AppSpawnMsgNode *message, uint32_t type, uint32_t index)
{
APPSPAWN_CHECK_ONLY_EXPER(message != NULL, return -1);
AppSpawnMsgFlags *msgFlags = (AppSpawnMsgFlags *)GetAppSpawnMsgInfo(message, type);
APPSPAWN_CHECK(msgFlags != NULL, return -1,
"No tlv %{public}d in msg %{public}s", type, message->msgHeader.processName);
return SetSpawnMsgFlags(msgFlags, index);
}
static int DumpAppSpawnQueue(ListNode *node, void *data)
{
AppSpawningCtx *property = ListEntry(node, AppSpawningCtx, node);

0
standard/appspawn_main.c Executable file → Normal file
View File

2
standard/appspawn_manager.h Executable file → Normal file
View File

@ -137,7 +137,7 @@ void AppSpawningCtxTraversal(ProcessTraversal traversal, void *data);
AppSpawningCtx *GetAppSpawningCtxByPid(pid_t pid);
AppSpawningCtx *CreateAppSpawningCtx();
void DeleteAppSpawningCtx(AppSpawningCtx *property);
int KillAndWaitStatus(pid_t pid, int sig);
int KillAndWaitStatus(pid_t pid, int sig, int *exitStatus);
/**
* @brief

29
standard/appspawn_msgmgr.c Executable file → Normal file
View File

@ -62,9 +62,9 @@ void *GetAppSpawnMsgExtInfo(const AppSpawnMsgNode *message, const char *name, ui
int CheckAppSpawnMsgFlag(const AppSpawnMsgNode *message, uint32_t type, uint32_t index)
{
APPSPAWN_CHECK(type == TLV_MSG_FLAGS || type == TLV_PERMISSION, return 0, "Invalid tlv %{public}u ", type);
AppSpawnMsgFlags *msgFlags = (AppSpawnMsgFlags *)GetAppSpawnMsgInfo(message, type);
APPSPAWN_CHECK(msgFlags != NULL,
return 0, "No tlv %{public}u in msg %{public}s", type, message->msgHeader.processName);
APPSPAWN_CHECK(msgFlags != NULL, return 0, "No tlv %{public}u in msg", type);
uint32_t blockIndex = index / 32; // 32 max bit in int
uint32_t bitIndex = index % 32; // 32 max bit in int
APPSPAWN_CHECK(blockIndex < msgFlags->count, return 0,
@ -72,6 +72,25 @@ int CheckAppSpawnMsgFlag(const AppSpawnMsgNode *message, uint32_t type, uint32_t
return CHECK_FLAGS_BY_INDEX(msgFlags->flags[blockIndex], bitIndex);
}
static inline int SetSpawnMsgFlags(AppSpawnMsgFlags *msgFlags, uint32_t index)
{
uint32_t blockIndex = index / 32; // 32 max bit in int
uint32_t bitIndex = index % 32; // 32 max bit in int
APPSPAWN_CHECK(blockIndex < msgFlags->count, return APPSPAWN_ARG_INVALID,
"Invalid index %{public}u blockIndex %{public}u %{public}u ", index, blockIndex, msgFlags->count);
msgFlags->flags[blockIndex] |= (1 << bitIndex);
return 0;
}
int SetAppSpawnMsgFlag(const AppSpawnMsgNode *message, uint32_t type, uint32_t index)
{
APPSPAWN_CHECK(type == TLV_MSG_FLAGS || type == TLV_PERMISSION,
return APPSPAWN_ARG_INVALID, "Invalid tlv %{public}u ", type);
AppSpawnMsgFlags *msgFlags = (AppSpawnMsgFlags *)GetAppSpawnMsgInfo(message, type);
APPSPAWN_CHECK(msgFlags != NULL, return APPSPAWN_ARG_INVALID, "No tlv %{public}d in msg", type);
return SetSpawnMsgFlags(msgFlags, index);
}
AppSpawnMsgNode *CreateAppSpawnMsg(void)
{
AppSpawnMsgNode *message = (AppSpawnMsgNode *)calloc(1, sizeof(AppSpawnMsgNode));
@ -167,7 +186,7 @@ static int CheckExtTlvInfo(const AppSpawnTlv *tlv, uint32_t remainLen)
APPSPAWN_LOGV("Recv type [%{public}s %{public}u] real len: %{public}u",
tlvExt->tlvName, tlvExt->tlvLen, tlvExt->dataLen);
if (tlvExt->dataLen > tlvExt->tlvLen - sizeof(AppSpawnTlvExt)) {
APPSPAWN_LOGE("Invalid tlv [%{public}s %{public}u] real len: %{public}u %{public}u",
APPSPAWN_LOGE("Invalid tlv [%{public}s %{public}u] real len: %{public}u %{public}zu",
tlvExt->tlvName, tlvExt->tlvLen, tlvExt->dataLen, sizeof(AppSpawnTlvExt));
return APPSPAWN_MSG_INVALID;
}
@ -219,10 +238,10 @@ int DecodeAppSpawnMsg(AppSpawnMsgNode *message)
while (currLen < bufferLen) {
AppSpawnTlv *tlv = (AppSpawnTlv *)(message->buffer + currLen);
APPSPAWN_CHECK(tlv->tlvLen <= (bufferLen - currLen), break,
"Invalid tlv [%{public}d %{public}d] curr: %{public}u",
"Invalid tlv [%{public}d %{public}d] curr: %{public}zu",
tlv->tlvType, tlv->tlvLen, currLen + sizeof(AppSpawnMsg));
APPSPAWN_LOGV("DecodeAppSpawnMsg tlv %{public}u %{public}u start: %{public}u ",
APPSPAWN_LOGV("DecodeAppSpawnMsg tlv %{public}u %{public}u start: %{public}zu ",
tlv->tlvType, tlv->tlvLen, currLen + sizeof(AppSpawnMsg)); // show in msg offset
ret = CheckMsgTlv(tlv, bufferLen - currLen);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);

View File

@ -73,7 +73,8 @@ static void StopAppSpawn(void)
AppSpawnedProcess *appInfo = GetSpawnedProcessByName(NWEBSPAWN_SERVER_NAME);
if (appInfo != NULL) {
APPSPAWN_LOGI("kill %{public}s pid: %{public}d", appInfo->name, appInfo->pid);
KillAndWaitStatus(appInfo->pid, SIGTERM);
int exitStatus = 0;
KillAndWaitStatus(appInfo->pid, SIGTERM, &exitStatus);
OH_ListRemove(&appInfo->node);
OH_ListInit(&appInfo->node);
free(appInfo);
@ -160,6 +161,10 @@ static void OnClose(const TaskHandle taskHandle)
}
AppSpawnConnection *connection = (AppSpawnConnection *)LE_GetUserData(taskHandle);
APPSPAWN_CHECK(connection != NULL, return, "Invalid connection");
if (connection->receiverCtx.timer) {
LE_StopTimer(LE_GetDefaultLoop(), connection->receiverCtx.timer);
connection->receiverCtx.timer = NULL;
}
APPSPAWN_LOGI("OnClose connectionId: %{public}u socket %{public}d",
connection->connectionId, LE_GetSocketFd(taskHandle));
DeleteAppSpawnMsg(connection->receiverCtx.incompleteMsg);
@ -168,6 +173,15 @@ static void OnClose(const TaskHandle taskHandle)
AppSpawningCtxTraversal(AppSpawningCtxOnClose, connection);
}
static void OnDisConnect(const TaskHandle taskHandle)
{
AppSpawnConnection *connection = (AppSpawnConnection *)LE_GetUserData(taskHandle);
APPSPAWN_CHECK(connection != NULL, return, "Invalid connection");
APPSPAWN_LOGI("OnDisConnect connectionId: %{public}u socket %{public}d",
connection->connectionId, LE_GetSocketFd(taskHandle));
OnClose(taskHandle);
}
static void SendMessageComplete(const TaskHandle taskHandle, BufferHandle handle)
{
AppSpawnConnection *connection = (AppSpawnConnection *)LE_GetUserData(taskHandle);
@ -233,7 +247,7 @@ static int OnConnection(const LoopHandle loopHandle, const TaskHandle server)
info.baseInfo.flags = TASK_STREAM | TASK_PIPE | TASK_CONNECT;
info.baseInfo.close = OnClose;
info.baseInfo.userDataSize = sizeof(AppSpawnConnection);
info.disConnectComplete = NULL;
info.disConnectComplete = OnDisConnect;
info.sendMessageComplete = SendMessageComplete;
info.recvMessage = OnReceiveRequest;
LE_STATUS ret = LE_AcceptStreamClient(loopHandle, server, &stream, &info);
@ -316,13 +330,13 @@ static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer,
return;
}
static char *GetMapMem(AppSpawnMgr *content, uint32_t clientId, const char *processName, uint32_t size, bool readOnly)
static char *GetMapMem(uint32_t clientId, const char *processName, uint32_t size, bool readOnly, bool isNweb)
{
char path[PATH_MAX] = {};
int len = sprintf_s(path, sizeof(path), APPSPAWN_MSG_DIR "%s/%s_%d",
IsNWebSpawnMode(content) ? "nwebspawn" : "appspawn", processName, clientId);
isNweb ? "nwebspawn" : "appspawn", processName, clientId);
APPSPAWN_CHECK(len > 0, return NULL, "Failed to format path %{public}s", processName);
APPSPAWN_LOGV("GetMapMem for child %{public}s memSize %{public}u", path, size);
int prot = PROT_READ;
int mode = O_RDONLY;
if (!readOnly) {
@ -341,13 +355,12 @@ static char *GetMapMem(AppSpawnMgr *content, uint32_t clientId, const char *proc
return (char *)areaAddr;
}
APPSPAWN_STATIC int WriteMsgToChild(AppSpawningCtx *property)
APPSPAWN_STATIC int WriteMsgToChild(AppSpawningCtx *property, bool isNweb)
{
const uint32_t memSize = (property->message->msgHeader.msgLen % 4096 + 1) * 4096; // 4096 4K
char *buffer = GetMapMem(GetAppSpawnMgr(), property->client.id, GetProcessName(property), memSize, false);
const uint32_t memSize = (property->message->msgHeader.msgLen / 4096 + 1) * 4096; // 4096 4K
char *buffer = GetMapMem(property->client.id, GetProcessName(property), memSize, false, isNweb);
APPSPAWN_CHECK(buffer != NULL, return APPSPAWN_SYSTEM_ERROR,
"Failed to map memory error %{public}d fileName %{public}s ", errno, GetProcessName(property));
// copy msg header
int ret = memcpy_s(buffer, memSize, &property->message->msgHeader, sizeof(AppSpawnMsg));
if (ret == 0) {
@ -361,6 +374,7 @@ APPSPAWN_STATIC int WriteMsgToChild(AppSpawningCtx *property)
}
property->forkCtx.msgSize = memSize;
property->forkCtx.childMsg = buffer;
APPSPAWN_LOGV("Write msg to child: %{public}u success", property->client.id);
return 0;
}
@ -374,16 +388,12 @@ static int InitForkContext(AppSpawningCtx *property)
if (option > 0) {
(void)fcntl(property->forkCtx.fd[0], F_SETFD, option | O_NONBLOCK);
}
if (property->client.flags & APP_COLD_START) { // for cold run, use shared memory to exchange message
APPSPAWN_LOGV("Write msg to child %{public}s", GetProcessName(property));
return WriteMsgToChild(property);
}
return 0;
}
static int AddChildWatcher(AppSpawningCtx *property)
{
uint32_t timeout = WAIT_CHILD_RESPONSE_TIMEOUT;
LE_WatchInfo watchInfo = {};
watchInfo.fd = property->forkCtx.fd[0];
watchInfo.flags = WATCHER_ONCE;
@ -394,7 +404,7 @@ static int AddChildWatcher(AppSpawningCtx *property)
return APPSPAWN_SYSTEM_ERROR, "Failed to watch child %{public}d", property->pid);
status = LE_CreateTimer(LE_GetDefaultLoop(), &property->forkCtx.timer, WaitChildTimeout, property);
if (status == LE_SUCCESS) {
status = LE_StartTimer(LE_GetDefaultLoop(), property->forkCtx.timer, WAIT_CHILD_RESPONSE_TIMEOUT, 0);
status = LE_StartTimer(LE_GetDefaultLoop(), property->forkCtx.timer, timeout * 1000, 0); // 1000 1s
}
if (status != LE_SUCCESS) {
if (property->forkCtx.timer != NULL) {
@ -576,6 +586,11 @@ static int AppSpawnColdStartApp(struct AppSpawnContent *content, AppSpawnClient
char *path = property->forkCtx.coldRunPath != NULL ? property->forkCtx.coldRunPath : "/system/bin/appspawn";
APPSPAWN_LOGI("ColdStartApp::processName: %{public}s path: %{public}s", GetProcessName(property), path);
// for cold run, use shared memory to exchange message
APPSPAWN_LOGV("Write msg to child %{public}s", GetProcessName(property));
int ret = WriteMsgToChild(property, IsNWebSpawnMode((AppSpawnMgr *)content));
APPSPAWN_CHECK(ret == 0, return APPSPAWN_SYSTEM_ERROR, "Failed to write msg to child");
char buffer[4][32] = {0}; // 4 32 buffer for fd
char *mode = IsNWebSpawnMode((AppSpawnMgr *)content) ? "nweb_cold" : "app_cold";
int len = sprintf_s(buffer[0], sizeof(buffer[0]), " %d ", property->forkCtx.fd[1]);
@ -593,7 +608,7 @@ static int AppSpawnColdStartApp(struct AppSpawnContent *content, AppSpawnClient
"-param", GetProcessName(property), buffer[3], NULL
};
int ret = execv(path, (char **)formatCmds);
ret = execv(path, (char **)formatCmds);
if (ret != 0) {
APPSPAWN_LOGE("Failed to execv, errno: %{public}d", errno);
}
@ -611,7 +626,8 @@ static AppSpawningCtx *GetAppSpawningCtxFromArg(AppSpawnMgr *content, int argc,
uint32_t size = atoi(argv[SHM_SIZE_INDEX]);
property->client.id = atoi(argv[CLIENT_ID_INDEX]);
uint8_t *buffer = (uint8_t *)GetMapMem(content, property->client.id, argv[PARAM_VALUE_INDEX], size, true);
uint8_t *buffer = (uint8_t *)GetMapMem(property->client.id,
argv[PARAM_VALUE_INDEX], size, true, IsNWebSpawnMode(content));
if (buffer == NULL) {
APPSPAWN_LOGE("Failed to map errno %{public}d %{public}s", property->client.id, argv[PARAM_VALUE_INDEX]);
NotifyResToParent(&content->content, &property->client, APPSPAWN_SYSTEM_ERROR);
@ -693,7 +709,7 @@ static void AppSpawnRun(AppSpawnContent *content, int argc, char *const argv[])
AppSpawnDestroyContent(content);
}
static int AppSpawnClearEnv(AppSpawnMgr *content, AppSpawningCtx *property)
APPSPAWN_STATIC int AppSpawnClearEnv(AppSpawnMgr *content, AppSpawningCtx *property)
{
APPSPAWN_CHECK(content != NULL, return 0, "Invalid appspawn content");
bool isNweb = IsNWebSpawnMode(content);
@ -762,7 +778,7 @@ AppSpawnContent *StartSpawnService(const AppSpawnStartArg *startArg, uint32_t ar
// load module appspawn/common
AppSpawnLoadAutoRunModules(MODULE_COMMON);
AppSpawnModuleMgrInstall("libappspawn_asan");
AppSpawnModuleMgrInstall(ASAN_MODULE_PATH);
APPSPAWN_CHECK(LE_GetDefaultLoop() != NULL, return NULL, "Invalid default loop");
AppSpawnContent *content = AppSpawnCreateContent(arg->socketName, argv[0], argvSize, arg->mode);

View File

@ -32,8 +32,13 @@
extern "C" {
#endif
#ifdef APPSPAWN_TEST
#define MAX_WAIT_MSG_COMPLETE (5 * 100) // 500ms
#define WAIT_CHILD_RESPONSE_TIMEOUT 3 //3s
#else
#define MAX_WAIT_MSG_COMPLETE (5 * 1000) // 5s
#define WAIT_CHILD_RESPONSE_TIMEOUT 3000 //3s
#define WAIT_CHILD_RESPONSE_TIMEOUT 3 //3s
#endif
typedef struct TagAppSpawnMsgNode AppSpawnMsgNode;
typedef struct TagAppSpawnMsgReceiverCtx {

10
standard/nwebspawn_launcher.c Executable file → Normal file
View File

@ -51,7 +51,7 @@ void NWebSpawnInit(void)
// ownerId must been set before setcon & setuid
(void)SetXpmOwnerId(PROCESS_OWNERID_EXTEND, NULL);
#endif
#ifdef WITH_SELINUX
#ifdef WITH_SELINUX
setcon("u:r:nwebspawn:s0");
#endif
pid_t pid = getpid();
@ -59,9 +59,11 @@ void NWebSpawnInit(void)
struct __user_cap_header_struct capHeader;
capHeader.version = _LINUX_CAPABILITY_VERSION_3;
capHeader.pid = 0;
const uint64_t inheriTable = 0x2000c0;
const uint64_t permitted = 0x2000c0;
const uint64_t effective = 0x2000c0;
// old CAP_SYS_ADMIN | CAP_SETGID | CAP_SETUID
const uint64_t inheriTable = CAP_TO_MASK(CAP_SYS_ADMIN) | CAP_TO_MASK(CAP_SETGID) |
CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_KILL);
const uint64_t permitted = inheriTable;
const uint64_t effective = inheriTable;
struct __user_cap_data_struct capData[CAP_NUM] = {};
for (int j = 0; j < CAP_NUM; ++j) {
capData[0].inheritable = (__u32)(inheriTable);

View File

@ -26,6 +26,11 @@ ohos_fuzztest("AppSpawnClientFuzzTest") {
deps = [ "${appspawn_path}/interfaces/innerkits_new/client:appspawn_client" ]
external_deps = [ "bounds_checking_function:libsec_static" ]
defines = []
if (defined(appspawn_sandbox_new) && appspawn_sandbox_new) {
defines += [ "APPSPAWN_SANDBOX_NEW" ]
}
cflags = [
"-g",
"-O0",

View File

@ -272,18 +272,18 @@ namespace OHOS {
int FuzzGetPermissionIndex(const uint8_t *data, size_t size)
{
std::string permission(reinterpret_cast<const char*>(data), size);
return GetPermissionIndex(permission.c_str());
return GetPermissionIndex(nullptr, permission.c_str());
}
int FuzzGetMaxPermissionIndex(const uint8_t *data, size_t size)
{
return GetMaxPermissionIndex();
return GetMaxPermissionIndex(nullptr);
}
int FuzzGetPermissionByIndex(const uint8_t *data, size_t size)
{
int32_t index = static_cast<int32_t>(size);
if (GetPermissionByIndex(index) == nullptr) {
if (GetPermissionByIndex(nullptr, index) == nullptr) {
return -1;
}
return 0;

View File

@ -125,11 +125,21 @@ void SetDeveloperMode(bool mode)
int GetParameter(const char *key, const char *def, char *value, uint32_t len)
{
static uint32_t count = 0;
count++;
if (strcmp(key, "startup.appspawn.cold.boot") == 0) {
return strcpy_s(value, len, "true") == 0 ? strlen("true") : -1;
}
if (strcmp(key, "persist.appspawn.reqMgr.timeout") == 0) {
return strcpy_s(value, len, "5") == 0 ? 1 : -1;
const char *tmp = def;
if ((count % 3) == 0) { // 3 test
return -1;
} else if ((count % 3) == 1) { //
tmp = "a";
} else {
tmp = "5";
}
return strcpy_s(value, len, tmp) == 0 ? strlen(tmp) : -1;
}
if (strcmp(key, "const.security.developermode.state") == 0) {
return g_developerMode ? (strcpy_s(value, len, "true") == 0 ? strlen("true") : -1) : -1;

View File

@ -24,6 +24,7 @@
#include <sys/wait.h>
#include "cJSON.h"
#include "appspawn_client.h"
#ifdef __cplusplus
extern "C" {
@ -42,6 +43,7 @@ typedef struct TagSandboxContext SandboxContext;
typedef struct TagAppSpawnedProcess AppSpawnedProcess;
typedef struct TagAppSpawnForkArg AppSpawnForkArg;
typedef struct TagAppSpawnMsgNode AppSpawnMsgNode;
typedef struct TagAppSpawnMgr AppSpawnMgr;
void SetHapDomainSetcontextResult(int result);
@ -54,14 +56,18 @@ int ParseAppSandboxConfig(const cJSON *appSandboxConfig, AppSpawnSandboxCfg *san
AppSpawnSandboxCfg *CreateAppSpawnSandbox(void);
void AddDefaultVariable(void);
int AppSpawnClearEnv(AppSpawnMgr *content, AppSpawningCtx *property);
int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client);
int WriteMsgToChild(AppSpawningCtx *property);
int WriteMsgToChild(AppSpawningCtx *property, bool isNweb);
int WriteToFile(const char *path, int truncated, pid_t pids[], uint32_t count);
int GetCgroupPath(const AppSpawnedProcess *appInfo, char *buffer, uint32_t buffLen);
void SetDeveloperMode(bool mode);
int LoadPermission(AppSpawnClientType type);
void DeletePermission(AppSpawnClientType type);
#define STUB_NEED_CHECK 0x01
typedef int (*ExecvFunc)(const char *pathname, char *const argv[]);
enum {

View File

@ -286,6 +286,11 @@ int SetgidStub(gid_t gid)
return 0;
}
int IoctlStub(int fd, unsigned long request, ...)
{
return 0;
}
#ifdef __cplusplus
}
#endif

View File

@ -306,7 +306,7 @@ bool StopAppspawn()
static const std::string defaultAppInfo1 = "{ \
\"msg-type\": \"MSG_APP_SPAWN\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\", \
\"dac-info\" : { \
\"uid\" : 20010041, \
@ -343,7 +343,7 @@ static const std::string defaultAppInfo1 = "{ \
static const std::string defaultAppInfo2 = "{ \
\"msg-type\": \"MSG_SPAWN_NATIVE_PROCESS\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\", \
\"dac-info\" : { \
\"uid\" : 20010043, \
@ -380,7 +380,7 @@ static const std::string defaultAppInfo2 = "{ \
static const std::string defaultAppInfo3 = "{ \
\"msg-type\": \"MSG_SPAWN_NATIVE_PROCESS\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\", \
\"dac-info\" : { \
\"uid\" : 20010045, \
@ -417,7 +417,7 @@ static const std::string defaultAppInfo3 = "{ \
static const std::string defaultAppInfoNoInternetPermission = "{ \
\"msg-type\": \"MSG_APP_SPAWN\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\", \
\"dac-info\" : { \
\"uid\" : 20010045, \
@ -450,7 +450,7 @@ static const std::string defaultAppInfoNoInternetPermission = "{ \
static const std::string defaultAppInfoNoDac = "{ \
\"msg-type\": \"MSG_APP_SPAWN\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\", \
\"access-token\" : {\
\"accessTokenIdEx\" : 537854093\
@ -481,7 +481,7 @@ static const std::string defaultAppInfoNoDac = "{ \
static const std::string defaultAppInfoNoToken = "{ \
\"msg-type\": \"MSG_APP_SPAWN\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\", \
\"dac-info\" : { \
\"uid\" : 20010043, \
@ -515,7 +515,7 @@ static const std::string defaultAppInfoNoToken = "{ \
static const std::string defaultAppInfoNoBundleInfo = "{ \
\"msg-type\": \"MSG_APP_SPAWN\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\", \
\"dac-info\" : { \
\"uid\" : 20010043, \
@ -548,7 +548,7 @@ static const std::string defaultAppInfoNoBundleInfo = "{ \
static const std::string defaultWebInfo1 = "{ \
\"msg-type\": \"MSG_APP_SPAWN\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\", \
\"dac-info\" : { \
\"uid\" : 1000001, \
@ -1712,7 +1712,7 @@ HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_001, TestSize.Level0)
static const std::string dumpInfo = "{ \
\"msg-type\": \"MSG_DUMP\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\" \
}";
@ -1730,7 +1730,7 @@ HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_002, TestSize.Level0)
HILOG_INFO(LOG_CORE, "AppSpawn_Msg_002 start");
static const std::string appInfo = "{ \
\"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\" \
}";
@ -1757,7 +1757,7 @@ HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_003, TestSize.Level0)
HILOG_INFO(LOG_CORE, "AppSpawn_Msg_003 start");
static const std::string appInfo = "{ \
\"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\" \
}";
@ -1784,7 +1784,7 @@ HWTEST_F(AppSpawnModuleTest, AppSpawn_Msg_004, TestSize.Level0)
HILOG_INFO(LOG_CORE, "AppSpawn_Msg_004 start");
static const std::string appInfo = "{ \
\"msg-type\": \"MSG_GET_RENDER_TERMINATION_STATUS\", \
\"msg-flags\": [ 7 ], \
\"msg-flags\": [ 13 ], \
\"process-name\" : \"com.example.myapplication\" \
}";

View File

@ -371,7 +371,7 @@ int AppSpawnTestCommander::CreateMsg(AppSpawnReqMsgHandle &reqHandle,
for (uint32_t j = 0; j < count; j++) {
(void)AppSpawnReqMsgSetAppFlag(reqHandle, static_cast<AppFlagsIndex>(msgFlags[j]));
}
(void)AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NO_SANDBOX);
(void)AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_IGNORE_SANDBOX);
ret = BuildMsgFromJson(appInfoConfig_, reqHandle);
APPSPAWN_CHECK(ret == 0, AppSpawnReqMsgFree(reqHandle);
return ret, "Failed to build req %{public}s", processName_.c_str());

View File

@ -18,9 +18,12 @@ ohos_unittest("AppSpawn_client_ut") {
module_out_path = "${module_output_path}"
deps = []
defines = [
"APPSPAWN_BASE_DIR=\"/data/appspawn_ut\"",
"APPSPAWN_TEST",
"APPSPAWN_CLIENT",
"OHOS_DEBUG",
"USER_TIMER_TO_CHECK",
"APPSPAWN_LABEL=\"APPSPAWN_CLENT_UT\"",
]
include_dirs = [
@ -60,10 +63,15 @@ ohos_unittest("AppSpawn_client_ut") {
]
sources += [
"${appspawn_path}/test/unittest/app_spawn_client_test/app_spawn_client_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_client_test/app_spawn_interface_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_test_helper.cpp",
]
if (defined(appspawn_sandbox_new) && appspawn_sandbox_new) {
defines += [ "APPSPAWN_SANDBOX_NEW" ]
}
if (asan_detector || is_asan) {
defines += [ "ASAN_DETECTOR" ]
}

View File

@ -18,6 +18,7 @@
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <memory>
#include <thread>
#include <string>
@ -123,6 +124,7 @@ HWTEST(AppSpawnClientTest, App_Client_Communication_003, TestSize.Level0)
ASSERT_EQ(ret, 0);
ASSERT_EQ(result.result, 0);
ASSERT_NE(result.pid, 0);
APPSPAWN_LOGV("App_Client_Communication_003 kill pid %{public}d", result.pid);
if (result.pid > 0) {
kill(result.pid, SIGKILL);
}
@ -259,10 +261,11 @@ HWTEST(AppSpawnClientTest, App_Client_Msg_003, TestSize.Level0)
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
do {
g_testHelper.SetProcessName("test222222222222222234512345678test222222222222222234512345678"
std::string processName = "test222222222222222234512345678test222222222222222234512345678"
"test222222222222222234512345678test222222222222222234512345678"
"test222222222222222234512345678test222222222222222234512345678"
"test222222222222222234512345678test2222222222222222345123456781234567");
"test222222222222222234512345678test2222222222222222345123456781234567";
g_testHelper.SetProcessName(processName.c_str());
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
@ -277,7 +280,7 @@ HWTEST(AppSpawnClientTest, App_Client_Msg_003, TestSize.Level0)
APPSPAWN_CHECK(info->bundleIndex == g_testHelper.GetTestBundleIndex(),
break, "Invalid bundleIndex %{public}d", info->bundleIndex);
APPSPAWN_LOGV("info->bundleName %{public}s", info->bundleName);
APPSPAWN_CHECK(strcmp(info->bundleName, g_testHelper.GetDefaultTestAppBundleName()) == 0,
APPSPAWN_CHECK(strcmp(info->bundleName, processName.c_str()) == 0,
break, "Invalid bundleName %{public}s", info->bundleName);
ret = 0;
} while (0);
@ -641,9 +644,9 @@ HWTEST(AppSpawnClientTest, App_Client_Msg_012, TestSize.Level0)
APPSPAWN_CHECK(ret == 0, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
// flags test
int max = GetMaxPermissionIndex();
int max = GetMaxPermissionIndex(clientHandle);
for (int i = 0; i < max; i++) {
ret = AppSpawnReqMsgAddPermission(reqHandle, GetPermissionByIndex(i));
ret = AppSpawnReqMsgAddPermission(reqHandle, GetPermissionByIndex(clientHandle, i));
ASSERT_EQ(ret, 0);
}
ret = APPSPAWN_ARG_INVALID;
@ -653,7 +656,8 @@ HWTEST(AppSpawnClientTest, App_Client_Msg_012, TestSize.Level0)
ret = 0;
for (size_t i = 0; i < max; i++) {
if (!CheckAppPermissionFlagSet(property, (uint32_t)i)) {
APPSPAWN_LOGE("Invalid permission not set %{public}d %{public}s", i, GetPermissionByIndex(i));
APPSPAWN_LOGE("Invalid permission not set %{public}d %{public}s",
i, GetPermissionByIndex(clientHandle, i));
ret = APPSPAWN_ARG_INVALID;
break;
}
@ -688,4 +692,485 @@ HWTEST(AppSpawnClientTest, App_Client_Msg_013, TestSize.Level0)
} while (0);
AppSpawnClientDestroy(clientHandle);
}
/**
* @brief
*
*/
HWTEST(AppSpawnClientTest, App_Client_Msg_014, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata", &reqHandle);
EXPECT_EQ(ret, 0);
AppDacInfo dacInfo = {};
dacInfo.uid = 20010029; // 20010029 test uid
dacInfo.gid = 20010029; // 20010029 test gid
dacInfo.gidCount = 2; // 2 count
dacInfo.gidTable[0] = 20010029; // 20010029 test gid
dacInfo.gidTable[1] = 20010030; // 20010030 test gid
ret = AppSpawnReqMsgSetAppDacInfo(reqHandle, &dacInfo);
EXPECT_EQ(ret, 0);
AppSpawnReqMsgFree(reqHandle);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_015, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
AppDacInfo dacInfo = {};
dacInfo.uid = 20010029; // 20010029 test uid
dacInfo.gid = 20010029; // 20010029 test gid
dacInfo.gidCount = 2; // 2 count
dacInfo.gidTable[0] = 20010029; // 20010029 test gid
dacInfo.gidTable[1] = 20010030; // 20010030 test gid
ret = AppSpawnReqMsgSetAppDacInfo(reqHandle, &dacInfo);
EXPECT_EQ(ret, 0);
AppSpawnReqMsgFree(reqHandle);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_016, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<char> testData(4000, '1'); // 4000 test data len
testData.push_back('1');
testData.push_back('2');
testData.push_back('3');
testData.push_back('4');
testData.push_back('5');
testData.push_back('6');
testData.push_back('7');
testData.push_back('8');
testData.push_back('9');
testData.push_back('\0');
ret = AppSpawnReqMsgAddStringInfo(reqHandle, "test-tlv-0001", testData.data());
EXPECT_EQ(ret, 0);
AppDacInfo dacInfo = {};
dacInfo.uid = 20010029; // 20010029 test uid
dacInfo.gid = 20010029; // 20010029 test gid
dacInfo.gidCount = 2; // 2 count
dacInfo.gidTable[0] = 20010029; // 20010029 test gid
dacInfo.gidTable[1] = 20010030; // 20010030 test gid
for (int i = 0; i < 50; i++) {
dacInfo.uid += i;
ret = AppSpawnReqMsgSetAppDacInfo(reqHandle, &dacInfo);
EXPECT_EQ(ret, 0);
}
AppSpawnReqMsgFree(reqHandle);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_017, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
const uint32_t maxCount = 321;
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<char> testData;
for (int i = 1; i < maxCount; i++) {
testData.assign(i, '1');
testData.push_back('\0');
std::string tlvName = "test-tlv-00";
tlvName += std::to_string(i);
ret = AppSpawnReqMsgAddStringInfo(reqHandle, tlvName.c_str(), testData.data());
EXPECT_EQ(ret, 0);
testData.clear();
}
testData.assign(maxCount, '1');
testData.push_back('\0');
std::string tlvName = "test-tlv-00";
tlvName += std::to_string(maxCount);
ret = AppSpawnReqMsgAddStringInfo(reqHandle, tlvName.c_str(), testData.data());
EXPECT_EQ(ret, APPSPAWN_MSG_INVALID);
testData.clear();
AppSpawningCtx *property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
EXPECT_EQ(property != nullptr, 1);
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
}
static uint32_t GenRandom(void)
{
uint32_t random = 0;
int fd = open("/dev/random", O_RDONLY);
if (fd >= 0) {
read(fd, &random, sizeof(random));
close(fd);
}
return random;
}
HWTEST(AppSpawnClientTest, App_Client_Msg_018, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<char> testData;
for (int i = 0; i < 4096; i++) { // 4096
uint32_t count = GenRandom() % 4096; // 4096
count = count == 0 ? 13 : count; // 13
testData.assign(count, '1');
testData.push_back('\0');
std::string tlvName = "test-tlv-00";
tlvName += std::to_string(i);
ret = AppSpawnReqMsgAddStringInfo(reqHandle, tlvName.c_str(), testData.data());
testData.clear();
EXPECT_EQ(ret == 0 || ret == APPSPAWN_MSG_INVALID, 1);
if (ret == APPSPAWN_MSG_INVALID) {
break;
}
}
AppSpawningCtx *property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
EXPECT_EQ(property != nullptr, 1);
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_019, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<char> testData;
testData.assign(3728 + 4072, '1'); // 3728 data size 4072 block size
testData.push_back('\0');
std::string tlvName = "App_Client_Msg_019";
ret = AppSpawnReqMsgAddStringInfo(reqHandle, tlvName.c_str(), testData.data());
testData.clear();
EXPECT_EQ(ret == 0, 1);
AppSpawningCtx *property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
EXPECT_EQ(property != nullptr, 1);
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_020, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<char> testData;
testData.assign(3727 + 4072, '1'); // 3729 data size 4072 block size
testData.push_back('\0');
std::string tlvName = "App_Client_Msg_020";
ret = AppSpawnReqMsgAddStringInfo(reqHandle, tlvName.c_str(), testData.data());
testData.clear();
EXPECT_EQ(ret == 0, 1);
AppSpawningCtx *property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
EXPECT_EQ(property != nullptr, 1);
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_021, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<char> testData;
testData.assign(3729 + 4072, '1'); // 3729 data size 4072 block size
testData.push_back('\0');
std::string tlvName = "App_Client_Msg_021";
ret = AppSpawnReqMsgAddStringInfo(reqHandle, tlvName.c_str(), testData.data());
testData.clear();
EXPECT_EQ(ret == 0, 1);
AppSpawningCtx *property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
EXPECT_EQ(property != nullptr, 1);
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_022, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<uint8_t> testData;
testData.assign(3727 + 4072, static_cast<uint8_t>('1')); // 3727 data size 4072 block size
std::string tlvName = "App_Client_Msg_022";
ret = AppSpawnReqMsgAddExtInfo(reqHandle, tlvName.c_str(), testData.data(), testData.size());
testData.clear();
EXPECT_EQ(ret == 0, 1);
AppSpawningCtx *property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
EXPECT_EQ(property != nullptr, 1);
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_023, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<uint8_t> testData;
testData.assign(3728 + 4072, static_cast<uint8_t>('1')); // 3728 data size 4072 block size
std::string tlvName = "App_Client_Msg_023";
ret = AppSpawnReqMsgAddExtInfo(reqHandle, tlvName.c_str(), testData.data(), testData.size());
testData.clear();
EXPECT_EQ(ret == 0, 1);
AppSpawningCtx *property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
EXPECT_EQ(property != nullptr, 1);
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Client_Msg_024, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.medialibrary.medialibrarydata:backup", &reqHandle);
EXPECT_EQ(ret, 0);
std::vector<uint8_t> testData;
testData.assign(3729 + 4072, static_cast<uint8_t>('1')); // 3729 data size 4072 block size
std::string tlvName = "App_Client_Msg_024";
ret = AppSpawnReqMsgAddExtInfo(reqHandle, tlvName.c_str(), testData.data(), testData.size());
testData.clear();
EXPECT_EQ(ret == 0, 1);
AppSpawningCtx *property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
EXPECT_EQ(property != nullptr, 1);
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
}
/**
* @brief permission test
*
*/
HWTEST(AppSpawnClientTest, App_Spawn_Permission_001, TestSize.Level0)
{
int ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_MAX);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max >= 0, 1);
DeletePermission(CLIENT_FOR_APPSPAWN);
DeletePermission(CLIENT_FOR_NWEBSPAWN);
DeletePermission(CLIENT_MAX);
DeletePermission(CLIENT_FOR_APPSPAWN);
DeletePermission(CLIENT_FOR_NWEBSPAWN);
}
/**
* @brief test appspawn
*
*/
HWTEST(AppSpawnClientTest, App_Spawn_Permission_002, TestSize.Level0)
{
int ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max >= 0, 1);
AppSpawnClientHandle clientHandle;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
max = GetMaxPermissionIndex(clientHandle);
int32_t index = GetPermissionIndex(clientHandle, "ohos.permission.ACCESS_BUNDLE_DIR");
EXPECT_EQ(index >= 0, 1);
EXPECT_EQ(index < max, 1);
const char *permission = GetPermissionByIndex(clientHandle, index);
EXPECT_EQ(permission != nullptr, 1);
EXPECT_EQ(strcmp(permission, "ohos.permission.ACCESS_BUNDLE_DIR") == 0, 1);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnClientTest, App_Spawn_Permission_003, TestSize.Level0)
{
int ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_MAX);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max >= 0, 1);
max = GetMaxPermissionIndex(nullptr);
int32_t index = GetPermissionIndex(nullptr, "ohos.permission.ACCESS_BUNDLE_DIR");
EXPECT_EQ(index >= 0, 1);
EXPECT_EQ(max >= index, 1);
const char *permission = GetPermissionByIndex(nullptr, index);
EXPECT_EQ(permission != nullptr, 1);
EXPECT_EQ(strcmp(permission, "ohos.permission.ACCESS_BUNDLE_DIR") == 0, 1);
}
/**
* @brief no load permission for appspawn
*
*/
HWTEST(AppSpawnClientTest, App_Spawn_Permission_004, TestSize.Level0)
{
DeletePermission(CLIENT_FOR_APPSPAWN);
DeletePermission(CLIENT_FOR_NWEBSPAWN);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max, 0);
AppSpawnClientHandle clientHandle;
int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
max = GetMaxPermissionIndex(clientHandle);
EXPECT_EQ(max, INVALID_PERMISSION_INDEX);
int32_t index = GetPermissionIndex(clientHandle, nullptr);
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
index = GetPermissionIndex(clientHandle, "ohos.permission.ACCESS_BUNDLE_DIR");
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
index = GetPermissionIndex(clientHandle, nullptr);
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
index = GetPermissionIndex(clientHandle, "");
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
const char *permission = GetPermissionByIndex(clientHandle, INVALID_PERMISSION_INDEX);
EXPECT_EQ(permission == nullptr, 1);
AppSpawnClientDestroy(clientHandle);
}
/**
* @brief for nwebspawn, no load permission
*
*/
HWTEST(AppSpawnClientTest, App_Spawn_Permission_005, TestSize.Level0)
{
DeletePermission(CLIENT_FOR_APPSPAWN);
DeletePermission(CLIENT_FOR_NWEBSPAWN);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max >= 0, 1);
AppSpawnClientHandle clientHandle;
int ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
max = GetMaxPermissionIndex(clientHandle);
EXPECT_EQ(max, INVALID_PERMISSION_INDEX);
int32_t index = GetPermissionIndex(clientHandle, nullptr);
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
index = GetPermissionIndex(clientHandle, "ohos.permission.ACCESS_BUNDLE_DIR");
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
const char *permission = GetPermissionByIndex(clientHandle, 1);
EXPECT_EQ(permission == nullptr, 1);
// reload permission
ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
AppSpawnClientDestroy(clientHandle);
}
/**
* @brief load permission for nwebspawn, but no permssion
*
*/
HWTEST(AppSpawnClientTest, App_Spawn_Permission_006, TestSize.Level0)
{
AppSpawnClientHandle clientHandle;
int ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
// for old sandbox, only one config
#ifdef APPSPAWN_SANDBOX_NEW
int max = GetMaxPermissionIndex(clientHandle);
EXPECT_EQ(max, 0);
int index = GetPermissionIndex(clientHandle, nullptr);
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
index = GetPermissionIndex(clientHandle, "ohos.permission.ACCESS_BUNDLE_DIR");
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
const char *permission = GetPermissionByIndex(clientHandle, 1);
EXPECT_EQ(permission == nullptr, 1);
#endif
AppSpawnClientDestroy(clientHandle);
}
} // namespace OHOS

View File

@ -18,21 +18,24 @@
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
#include <iostream>
#include "appspawn.h"
#include "appspawn_client.h"
#include "appspawn_hook.h"
#include "appspawn_mount_permission.h"
#include "appspawn_service.h"
#include "appspawn_utils.h"
#include "securec.h"
#include "app_spawn_stub.h"
using namespace testing;
using namespace testing::ext;
@ -50,34 +53,23 @@ public:
*
*/
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Init_001, TestSize.Level0)
{
int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, nullptr);
EXPECT_EQ(APPSPAWN_ARG_INVALID, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Init_002, TestSize.Level0)
{
int ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, nullptr);
EXPECT_EQ(APPSPAWN_ARG_INVALID, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Init_003, TestSize.Level0)
{
int ret = AppSpawnClientInit("test", nullptr);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Init_004, TestSize.Level0)
{
int ret = AppSpawnClientInit(nullptr, nullptr);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Init_005, TestSize.Level0)
{
AppSpawnClientHandle handle;
int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &handle);
EXPECT_EQ(0, ret);
const char *serviceName[] = {APPSPAWN_SERVER_NAME, APPSPAWN_SOCKET_NAME,
NWEBSPAWN_SERVER_NAME, NWEBSPAWN_SOCKET_NAME,
"test", "", nullptr};
AppSpawnClientHandle *inputhandle[] = {&handle, nullptr};
for (size_t i = 0; i < ARRAY_LENGTH(serviceName); i++) {
for (size_t j = 0; j < ARRAY_LENGTH(inputhandle); j++) {
printf("App_Spawn_Interface_Init_001 %zu %zu \n", i, j);
int ret = AppSpawnClientInit(serviceName[i], inputhandle[j]);
EXPECT_EQ(((i <= 5) && j == 0 && ret == 0) || (ret != 0), 1); // 3 valid
if (ret == 0) {
AppSpawnClientDestroy(handle);
}
}
}
}
/**
@ -113,38 +105,40 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Destroy_003, TestSize.Level0)
*
*/
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Send_Msg_001, TestSize.Level0)
{
int ret = 0;
AppSpawnReqMsgHandle reqHandle = nullptr;
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(nullptr, reqHandle, &result);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Send_Msg_002, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(0, ret);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, nullptr, &result);
EXPECT_NE(0, ret);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Send_Msg_003, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
AppSpawnClientHandle clientHandle1 = nullptr;
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle1);
EXPECT_EQ(0, ret);
AppSpawnResult result1 = {};
AppSpawnClientHandle handle[] = {clientHandle, clientHandle1, nullptr};
AppSpawnResult *result[] = {&result1, nullptr};
for (size_t i = 0; i < ARRAY_LENGTH(handle); i++) {
for (size_t k = 0; k < ARRAY_LENGTH(result); k++) {
AppSpawnReqMsgHandle reqHandle1 = nullptr;
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle1);
EXPECT_EQ(0, ret);
printf("App_Spawn_Interface_Send_Msg_001 %zu %zu %d \n", i, k, ret);
ret = AppSpawnClientSendMsg(handle[i], reqHandle1, result[k]);
EXPECT_EQ((i <= 1 && k == 0 && ret == 0) || (ret != 0), 1);
}
}
for (size_t i = 0; i < ARRAY_LENGTH(handle); i++) {
for (size_t k = 0; k < ARRAY_LENGTH(result); k++) {
ret = AppSpawnClientSendMsg(handle[i], nullptr, result[k]);
EXPECT_EQ(ret != 0, 1);
}
}
ret = AppSpawnClientDestroy(clientHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnClientDestroy(clientHandle1);
EXPECT_EQ(0, ret);
AppSpawnReqMsgHandle reqHandle = nullptr;
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, nullptr);
EXPECT_NE(0, ret);
AppSpawnClientDestroy(clientHandle);
}
/**
@ -153,103 +147,36 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Send_Msg_003, TestSize.Level0)
*/
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_001, TestSize.Level0)
{
std::vector<char> name1(APP_LEN_PROC_NAME - 1, 'a');
name1.push_back('\0');
std::vector<char> name2(APP_LEN_PROC_NAME, 'b');
name2.push_back('\0');
std::vector<char> name3(APP_LEN_PROC_NAME + 1, 'c');
name3.push_back('\0');
AppSpawnMsgType msgType[] = {MSG_APP_SPAWN, MSG_GET_RENDER_TERMINATION_STATUS,
MSG_SPAWN_NATIVE_PROCESS, MSG_DUMP, MAX_TYPE_INVALID};
const char *processName[] = {"com.ohos.myapplication",
"com.ohos.myapplication::@#$%^&*()test",
name1.data(), name2.data(), name3.data(), nullptr, ""};
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
AppSpawnReqMsgHandle *inputHandle[] = {&reqHandle, nullptr};
for (size_t i = 0; i < ARRAY_LENGTH(msgType); i++) {
for (size_t j = 0; j < ARRAY_LENGTH(processName); j++) {
for (size_t k = 0; k < ARRAY_LENGTH(inputHandle); k++) {
int ret = AppSpawnReqMsgCreate(msgType[i], processName[j], inputHandle[k]);
printf("App_Spawn_Interface_Msg_Create_001 %zu %zu %zu \n", i, j, k);
EXPECT_EQ(((i != 4) && (j < 3) && (k == 0) && ret == 0) || (ret != 0), 1); // 4 3 valid index
if (ret == 0) {
AppSpawnReqMsgFree(reqHandle);
}
}
}
}
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_002, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_GET_RENDER_TERMINATION_STATUS, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_003, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_SPAWN_NATIVE_PROCESS, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_004, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_DUMP, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_005, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MAX_TYPE_INVALID, "com.ohos.myapplication", &reqHandle);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_006, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication::@#$%^&*()test", &reqHandle);
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_008, TestSize.Level0)
{
char buffer[255];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, buffer, &reqHandle);
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_009, TestSize.Level0)
{
char buffer[257];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, buffer, &reqHandle);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_010, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, nullptr, &reqHandle);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_011, TestSize.Level0)
{
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", nullptr);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_012, TestSize.Level0)
{
const char *testMsg = "testMsg";
char *p = const_cast<char *>(testMsg);
AppSpawnReqMsgHandle handle = reinterpret_cast<AppSpawnReqMsgHandle>(p);
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &handle);
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_013, TestSize.Level0)
{
char buffer[128];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, buffer, &reqHandle);
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_Create_014, TestSize.Level0)
{
char buffer[129];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, buffer, &reqHandle);
EXPECT_EQ(0, ret);
}
/**
* @brief AppSpawnReqMsgFree
*
@ -272,83 +199,26 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_ReqMsgSetBundleInfo_001, TestS
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetBundleInfo(reqHandle, 0, "com.ohos.myapplication");
EXPECT_EQ(0, ret);
AppSpawnReqMsgFree(reqHandle);
}
std::vector<char> name1(APP_LEN_BUNDLE_NAME - 1, 'a');
name1.push_back('\0');
std::vector<char> name2(APP_LEN_BUNDLE_NAME, 'b');
name2.push_back('\0');
std::vector<char> name3(APP_LEN_BUNDLE_NAME + 1, 'c');
name3.push_back('\0');
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_ReqMsgSetBundleInfo_002, TestSize.Level0)
{
int ret = AppSpawnReqMsgSetBundleInfo(nullptr, 0, "com.ohos.myapplication");
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_ReqMsgSetBundleInfo_005, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetBundleInfo(reqHandle, 1, "com.ohos.myapplication");
EXPECT_EQ(0, ret);
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_ReqMsgSetBundleInfo_006, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetBundleInfo(reqHandle, -1, "com.ohos.myapplication");
EXPECT_EQ(0, ret);
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_ReqMsgSetBundleInfo_007, TestSize.Level0)
{
char buffer[255];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetBundleInfo(reqHandle, 0, buffer);
EXPECT_EQ(0, ret);
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_ReqMsgSetBundleInfo_008, TestSize.Level0)
{
char buffer[257];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetBundleInfo(reqHandle, 0, buffer);
EXPECT_NE(0, ret);
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_ReqMsgSetBundleInfo_009, TestSize.Level0)
{
char buffer[128];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetBundleInfo(reqHandle, 0, buffer);
EXPECT_EQ(0, ret);
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_ReqMsgSetBundleInfo_010, TestSize.Level0)
{
char buffer[129];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetBundleInfo(reqHandle, 0, buffer);
EXPECT_EQ(0, ret);
const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr};
const uint32_t bundleIndex[] = {0, 1};
const char *bundleName[] = {"com.ohos.myapplication", name1.data(), name2.data(), name3.data(), nullptr};
for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) {
for (size_t j = 0; j < ARRAY_LENGTH(bundleIndex); j++) {
for (size_t k = 0; k < ARRAY_LENGTH(bundleName); k++) {
printf("App_Spawn_Interface_ReqMsgSetBundleInfo_001 %zu %zu %zu %d \n", i, j, k, ret);
ret = AppSpawnReqMsgSetBundleInfo(inputHandle[i], bundleIndex[j], bundleName[k]);
EXPECT_EQ((i == 0 && (k <= 2) && ret == 0) || (ret != 0), 1); // 2 valid index
}
}
}
AppSpawnReqMsgFree(reqHandle);
}
@ -364,32 +234,21 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_App_Dac_001, TestSize.Leve
dacInfo.gidCount = 2; // 2 count
dacInfo.gidTable[0] = 20010029; // 20010029 test data
dacInfo.gidTable[1] = 20010029 + 1; // 20010029 test data
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetAppDacInfo(reqHandle, &dacInfo);
EXPECT_EQ(0, ret);
}
const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr};
const AppDacInfo *dacInfo1[] = {&dacInfo, nullptr};
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_App_Dac_002, TestSize.Level0)
{
AppDacInfo dacInfo;
dacInfo.uid = 20010029; // 20010029 test data
dacInfo.gid = 20010029; // 20010029 test data
dacInfo.gidCount = 2; // 2 count
dacInfo.gidTable[0] = 20010029; // 20010029 test data
dacInfo.gidTable[1] = 20010029 + 1; // 20010029 test data
int ret = AppSpawnReqMsgSetAppDacInfo(nullptr, &dacInfo);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_App_Dac_004, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetAppDacInfo(reqHandle, nullptr);
EXPECT_NE(0, ret);
for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) {
for (size_t j = 0; j < ARRAY_LENGTH(dacInfo1); j++) {
printf("App_Spawn_Interface_Msg_App_Dac_001 %zu %zu %d \n", i, j, ret);
ret = AppSpawnReqMsgSetAppDacInfo(inputHandle[i], dacInfo1[j]);
EXPECT_EQ((i == 0 && j == 0 && ret == 0) || (ret != 0), 1);
}
}
AppSpawnReqMsgFree(reqHandle);
}
/**
@ -399,26 +258,32 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Msg_App_Dac_004, TestSize.Leve
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Domain_001, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
char buffer[33];
(void)memset_s(buffer, sizeof(buffer), 'A', sizeof(buffer));
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetAppDomainInfo(reqHandle, 1, "system_core");
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Domain_002, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgSetAppDomainInfo(reqHandle, 1, "system_core");
EXPECT_NE(0, ret);
}
std::vector<char> name1(APP_APL_MAX_LEN - 1, 'a');
name1.push_back('\0');
std::vector<char> name2(APP_APL_MAX_LEN, 'b');
name2.push_back('\0');
std::vector<char> name3(APP_APL_MAX_LEN + 1, 'c');
name3.push_back('\0');
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Domain_004, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetAppDomainInfo(reqHandle, 1, nullptr);
EXPECT_NE(0, ret);
const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr};
const uint32_t hapFlags[] = {1};
const char *apl[] = {"system_core", name1.data(), name2.data(), name3.data(), "", nullptr};
for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) {
for (size_t j = 0; j < ARRAY_LENGTH(hapFlags); j++) {
for (size_t k = 0; k < ARRAY_LENGTH(apl); k++) {
printf("App_Spawn_Interface_Set_App_Domain_001 %zu %zu %zu %d \n", i, j, k, ret);
ret = AppSpawnReqMsgSetAppDomainInfo(inputHandle[i], hapFlags[j], apl[k]);
EXPECT_EQ((i == 0 && k <= 2 && ret == 0) || (ret != 0), 1); // 2 valid index
}
}
}
AppSpawnReqMsgFree(reqHandle);
}
/**
@ -432,6 +297,7 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Permission_Info_001, T
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetAppInternetPermissionInfo(reqHandle, 102, 102);
EXPECT_EQ(0, ret);
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Permission_Info_002, TestSize.Level0)
@ -451,6 +317,7 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Access_Token_001, Test
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetAppAccessToken(reqHandle, 12345678);
EXPECT_EQ(0, ret);
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Access_Token_002, TestSize.Level0)
@ -468,23 +335,24 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Owner_001, TestSize.Le
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetAppOwnerId(reqHandle, "ohos.permission.FILE_ACCESS_MANAGER");
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Owner_002, TestSize.Level0)
{
int ret = AppSpawnReqMsgSetAppOwnerId(nullptr, "ohos.permission.FILE_ACCESS_MANAGER");
EXPECT_NE(0, ret);
}
std::vector<char> name1(APP_OWNER_ID_LEN - 1, 'a');
name1.push_back('\0');
std::vector<char> name2(APP_OWNER_ID_LEN, 'b');
name2.push_back('\0');
std::vector<char> name3(APP_OWNER_ID_LEN + 1, 'c');
name3.push_back('\0');
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Set_App_Owner_004, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgSetAppOwnerId(reqHandle, nullptr);
EXPECT_NE(0, ret);
const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr};
const char *ownerId[] = {"FILE_ACCESS_MANAGER", name1.data(), name2.data(), name3.data(), "", nullptr};
for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) {
for (size_t j = 0; j < ARRAY_LENGTH(ownerId); j++) {
printf("App_Spawn_Interface_Set_App_Owner_001 %zu %zu %d \n", i, j, ret);
ret = AppSpawnReqMsgSetAppOwnerId(inputHandle[i], ownerId[j]);
EXPECT_EQ((i == 0 && j <= 1 && ret == 0) || (ret != 0), 1);
}
}
AppSpawnReqMsgFree(reqHandle);
}
/**
@ -496,23 +364,17 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Permission_001, TestSize.L
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgAddPermission(reqHandle, "ohos.permission.READ_IMAGEVIDEO");
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Permission_002, TestSize.Level0)
{
int ret = AppSpawnReqMsgAddPermission(nullptr, "ohos.permission.READ_IMAGEVIDEO");
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Permission_004, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgAddPermission(reqHandle, nullptr);
EXPECT_NE(0, ret);
const AppSpawnReqMsgHandle inputHandle[] = {reqHandle, nullptr};
const char *permission[] = {"ohos.permission.READ_IMAGEVIDEO", "", nullptr};
for (size_t i = 0; i < ARRAY_LENGTH(inputHandle); i++) {
for (size_t j = 0; j < ARRAY_LENGTH(permission); j++) {
printf("App_Spawn_Interface_Add_Permission_001 %zu %zu %d \n", i, j, ret);
ret = AppSpawnReqMsgAddPermission(inputHandle[i], permission[j]);
EXPECT_EQ((i == 0 && j == 0 && ret == 0) || (ret != 0), 1);
}
}
AppSpawnReqMsgFree(reqHandle);
}
/**
@ -524,39 +386,40 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Ext_Info_001, TestSize.Lev
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
const char *testData = "ssssssssssssss sssssssss ssssssss";
ret = AppSpawnReqMsgAddExtInfo(reqHandle, "tlv-name-1",
reinterpret_cast<uint8_t *>(const_cast<char *>(testData)), strlen(testData));
EXPECT_EQ(0, ret);
}
const char *inputName[] = {"tlv-name-1",
"234567890123456789012345678901",
"2345678901234567890123456789012",
"23456789012345678901234567890123",
"", nullptr};
std::vector<uint8_t> data(EXTRAINFO_TOTAL_LENGTH_MAX + 1, static_cast<uint8_t>('c'));
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Ext_Info_002, TestSize.Level0)
{
const char *testData = "ssssssssssssss sssssssss ssssssss";
int ret = AppSpawnReqMsgAddExtInfo(nullptr, "tlv-name-1",
reinterpret_cast<uint8_t *>(const_cast<char *>(testData)), strlen(testData));
EXPECT_NE(0, ret);
}
const uint8_t *inputData[] = {data.data(), data.data(), data.data(), data.data(), nullptr};
const size_t inputvalueLen[] = {23,
EXTRAINFO_TOTAL_LENGTH_MAX - 1,
EXTRAINFO_TOTAL_LENGTH_MAX,
EXTRAINFO_TOTAL_LENGTH_MAX + 1,
0};
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Ext_Info_004, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
const char *testData = "ssssssssssssss sssssssss ssssssss";
ret = AppSpawnReqMsgAddExtInfo(reqHandle, nullptr,
reinterpret_cast<uint8_t *>(const_cast<char *>(testData)), strlen(testData));
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_Ext_Info_005, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
const char *testData = "ssssssssssssss sssssssss ssssssss";
ret = AppSpawnReqMsgAddExtInfo(reqHandle, "tlv-name-1", nullptr, strlen(testData));
EXPECT_NE(0, ret);
for (size_t j = 0; j < ARRAY_LENGTH(inputName); j++) {
for (size_t k = 0; k < ARRAY_LENGTH(inputData); k++) {
for (size_t l = 0; l < ARRAY_LENGTH(inputvalueLen); l++) {
ret = AppSpawnReqMsgAddExtInfo(reqHandle,
inputName[j], inputData[k], static_cast<uint32_t>(inputvalueLen[l]));
printf("App_Spawn_Interface_Add_Ext_Info_001 %zu %zu %zu %d \n", j, k, l, ret);
EXPECT_EQ((j <= 2 && k <= 4 && l <= 2 && ret == 0) || (ret != 0), 1); // 2 4 valid index
}
}
}
for (size_t j = 0; j < ARRAY_LENGTH(inputName); j++) {
for (size_t k = 0; k < ARRAY_LENGTH(inputData); k++) {
for (size_t l = 0; l < ARRAY_LENGTH(inputvalueLen); l++) {
ret = AppSpawnReqMsgAddExtInfo(nullptr,
inputName[j], inputData[k], static_cast<uint32_t>(inputvalueLen[l]));
EXPECT_EQ(ret != 0, 1);
}
}
}
AppSpawnReqMsgFree(reqHandle);
}
/**
@ -568,53 +431,143 @@ HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_String_Info_001, TestSize.
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
const char *testData = "ssssssssssssss sssssssss ssssssss";
ret = AppSpawnReqMsgAddStringInfo(reqHandle, "test", testData);
EXPECT_EQ(0, ret);
std::vector<char> name1(23, 'a'); // 23 test data
name1.push_back('\0');
std::vector<char> name2(EXTRAINFO_TOTAL_LENGTH_MAX - 1, 'b');
name2.push_back('\0');
std::vector<char> name3(EXTRAINFO_TOTAL_LENGTH_MAX, 'c');
name3.push_back('\0');
std::vector<char> name4(EXTRAINFO_TOTAL_LENGTH_MAX + 1, 'd');
name4.push_back('\0');
const char *inputName[] = {"tlv-name-1",
"234567890123456789012345678901",
"2345678901234567890123456789012",
"23456789012345678901234567890123",
"", nullptr};
const char *inputData[] = {name1.data(), name2.data(), name3.data(), name4.data(), nullptr};
for (size_t j = 0; j < ARRAY_LENGTH(inputName); j++) {
for (size_t k = 0; k < ARRAY_LENGTH(inputData); k++) {
printf("App_Spawn_Interface_Add_String_Info_001 %zu %zu %d \n", j, k, ret);
ret = AppSpawnReqMsgAddStringInfo(reqHandle, inputName[j], inputData[k]);
EXPECT_EQ((j <= 2 && k <= 1 && ret == 0) || (ret != 0), 1);
}
}
for (size_t j = 0; j < ARRAY_LENGTH(inputName); j++) {
for (size_t k = 0; k < ARRAY_LENGTH(inputData); k++) {
ret = AppSpawnReqMsgAddStringInfo(nullptr, inputName[j], inputData[k]);
EXPECT_EQ(ret != 0, 1);
}
}
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_String_Info_002, TestSize.Level0)
/**
* @brief flags set
*
*/
HWTEST(AppSpawnInterfaceTest, App_Spawn_Set_Msg_Flags_001, TestSize.Level0)
{
const char *testData = "ssssssssssssss sssssssss ssssssss";
int ret = AppSpawnReqMsgAddStringInfo(nullptr, "test", testData);
EXPECT_NE(0, ret);
AppSpawnReqMsgHandle reqHandle = 0;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.example.myapplication", &reqHandle);
EXPECT_EQ(ret, 0);
for (int32_t i = 0; i <= MAX_FLAGS_INDEX; i++) {
ret = AppSpawnReqMsgSetAppFlag(reqHandle, static_cast<AppFlagsIndex>(i));
printf(" App_Spawn_Set_Msg_Flags_001 %d %d \n", i, ret);
EXPECT_EQ((i != MAX_FLAGS_INDEX && ret == 0) || (ret != 0), 1);
}
AppSpawnReqMsgFree(reqHandle);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_String_Info_004, TestSize.Level0)
HWTEST(AppSpawnInterfaceTest, App_Spawn_TerminateMsg_001, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
const char *testData = "ssssssssssssss sssssssss ssssssss";
ret = AppSpawnReqMsgAddStringInfo(reqHandle, nullptr, testData);
EXPECT_NE(0, ret);
AppSpawnReqMsgHandle reqHandle = 0;
int ret = AppSpawnTerminateMsgCreate(0, &reqHandle);
EXPECT_EQ(ret, 0);
AppSpawnReqMsgFree(reqHandle);
ret = AppSpawnTerminateMsgCreate(0, nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_Add_String_Info_005, TestSize.Level0)
/**
* @brief add permission
*
*/
HWTEST(AppSpawnInterfaceTest, App_Spawn_Permission_001, TestSize.Level0)
{
AppSpawnReqMsgHandle reqHandle = nullptr;
int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
EXPECT_EQ(0, ret);
ret = AppSpawnReqMsgAddStringInfo(reqHandle, "test", nullptr);
EXPECT_NE(0, ret);
AppSpawnClientHandle clientHandle;
AppSpawnReqMsgHandle reqHandle = 0;
int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.example.myapplication", &reqHandle);
EXPECT_EQ(ret, 0);
const int32_t inputCount = 2;
const char *permissions[] = {"ohos.permission.ACCESS_BUNDLE_DIR", "", "3333333333", nullptr};
const AppSpawnClientHandle inputClientHandle[inputCount] = {clientHandle, nullptr};
const AppSpawnReqMsgHandle inputReqHandle[inputCount] = {reqHandle, nullptr};
for (int32_t i = 0; i < inputCount; i++) {
for (int32_t j = 0; j < inputCount; j++) {
for (int32_t k = 0; k < 4; k++) { // 4 sizeof permissions
ret = AppSpawnClientAddPermission(inputClientHandle[i], inputReqHandle[j], permissions[k]);
printf(" AppSpawnClientAddPermission %d %d %d %d \n", i, j, k, ret);
EXPECT_EQ(i == 0 && j == 0 && k == 0 && ret == 0 || ret != 0, 1);
}
}
}
AppSpawnReqMsgFree(reqHandle);
AppSpawnClientDestroy(clientHandle);
}
/**
* @brief GetPermissionByIndex
*
*/
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_GetPermissionByIndex_005, TestSize.Level0)
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_GetPermissionByIndex_001, TestSize.Level0)
{
int32_t maxIndex = GetMaxPermissionIndex();
int32_t maxIndex = GetMaxPermissionIndex(nullptr);
for (int i = 0; i < maxIndex; i++) {
EXPECT_NE(GetPermissionByIndex(i), nullptr);
EXPECT_NE(GetPermissionByIndex(nullptr, i), nullptr);
}
EXPECT_EQ(GetPermissionByIndex(maxIndex), nullptr);
EXPECT_EQ(GetPermissionByIndex(-1), nullptr);
EXPECT_EQ(GetPermissionByIndex(nullptr, maxIndex), nullptr);
EXPECT_EQ(GetPermissionByIndex(nullptr, -1), nullptr);
int32_t index = GetPermissionIndex("ohos.permission.ACCESS_BUNDLE_DIR");
int32_t index = GetPermissionIndex(nullptr, "ohos.permission.ACCESS_BUNDLE_DIR");
int ret = index >= 0 && index < maxIndex ? 0 : -1;
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_ClientSocket_001, TestSize.Level0)
{
int socketId = CreateClientSocket(CLIENT_FOR_APPSPAWN, 2);
CloseClientSocket(socketId);
socketId = CreateClientSocket(CLIENT_FOR_NWEBSPAWN, 2);
CloseClientSocket(socketId);
socketId = CreateClientSocket(CLIENT_MAX, 2);
CloseClientSocket(socketId);
CloseClientSocket(-1);
}
HWTEST(AppSpawnInterfaceTest, App_Spawn_Interface_GetSpawnTimeout_001, TestSize.Level0)
{
int timeout = GetSpawnTimeout(6); // 6 test
EXPECT_EQ(timeout >= 6, 1); // 6 test
timeout = GetSpawnTimeout(6); // 6 test
EXPECT_EQ(timeout >= 6, 1); // 6 test
timeout = GetSpawnTimeout(6); // 6 test
EXPECT_EQ(timeout >= 6, 1); // 6 test
timeout = GetSpawnTimeout(2); // 2 test
EXPECT_EQ(timeout >= 2, 1); // 2 test
timeout = GetSpawnTimeout(2); // 2 test
EXPECT_EQ(timeout >= 2, 1); // 2 test
timeout = GetSpawnTimeout(2); // 2 test
EXPECT_EQ(timeout >= 2, 1); // 2 test
}
} // namespace OHOS

View File

@ -45,6 +45,7 @@ ohos_unittest("AppSpawn_ut") {
"setuid=SetuidStub",
"setgid=SetgidStub",
"execvp=ExecvpStub",
"ioctl=IoctlStub",
]
include_dirs = [
@ -108,11 +109,13 @@ ohos_unittest("AppSpawn_ut") {
# add test
include_dirs += [ "${appspawn_path}/test/unittest" ]
sources += [
"${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_appmgr_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_cgroup_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_child_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_cold_run_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_command_lexer_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_module_interface_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_sandboxmgr_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_service_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_standard_test/nweb_spawn_service_test.cpp",
"${appspawn_path}/test/unittest/app_spawn_test_helper.cpp",
@ -120,6 +123,7 @@ ohos_unittest("AppSpawn_ut") {
if (defined(appspawn_sandbox_new) && appspawn_sandbox_new) {
sources += [ "${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_sandbox_new_test.cpp" ]
defines += [ "APPSPAWN_SANDBOX_NEW" ]
} else {
sources += [
"${appspawn_path}/modules/sandbox/sandbox_utils.cpp",

File diff suppressed because it is too large Load Diff

View File

@ -47,6 +47,29 @@ public:
void TearDown() {}
};
static int TestRunChildProcessor(AppSpawnContent *content, AppSpawnClient *client)
{
APPSPAWN_LOGV("TestRunChildProcessor %{public}u", client->id);
AppSpawnEnvClear(content, client);
usleep(1000); // 1000 1ms
return 0;
}
static AppSpawnContent *CreateTestAppSpawnContent(const char *name, uint32_t mode)
{
static char path[PATH_MAX] = {};
AppSpawnContent *content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_APP_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, return nullptr);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content); // 预加载解析sandbox
AddAppSpawnHook(STAGE_CHILD_PRE_RUN, HOOK_PRIO_LOWEST, AppSpawnClearEnv); // clear
return content;
}
/**
* @brief appspanw spawn的后半部分
*
*/
HWTEST(AppSpawnChildTest, App_Spawn_Child_001, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
@ -59,12 +82,9 @@ HWTEST(AppSpawnChildTest, App_Spawn_Child_001, TestSize.Level0)
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_APP_SPAWN);
content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content); // 预加载解析sandbox
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
reqHandle = nullptr;
@ -83,6 +103,10 @@ HWTEST(AppSpawnChildTest, App_Spawn_Child_001, TestSize.Level0)
ASSERT_EQ(ret, 0);
}
/**
* @brief appspanw spawn的后半部分
*
*/
HWTEST(AppSpawnChildTest, App_Spawn_Child_002, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
@ -100,12 +124,9 @@ HWTEST(AppSpawnChildTest, App_Spawn_Child_002, TestSize.Level0)
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_APP_SPAWN);
content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
reqHandle = nullptr;
@ -142,12 +163,9 @@ HWTEST(AppSpawnChildTest, App_Spawn_Child_003, TestSize.Level0)
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_APP_SPAWN);
content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
reqHandle = nullptr;
@ -190,10 +208,9 @@ HWTEST(AppSpawnChildTest, App_Spawn_Child_004, TestSize.Level0)
reqHandle = nullptr;
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_APP_SPAWN);
content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
// spawn
@ -231,10 +248,9 @@ HWTEST(AppSpawnChildTest, App_Spawn_Child_005, TestSize.Level0)
reqHandle = nullptr;
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_APP_SPAWN);
content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
// spawn
@ -276,10 +292,9 @@ HWTEST(AppSpawnChildTest, App_Spawn_Child_006, TestSize.Level0)
reqHandle = nullptr;
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_APP_SPAWN);
content = CreateTestAppSpawnContent(APPSPAWN_SOCKET_NAME, MODE_FOR_APP_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
// spawn
@ -293,6 +308,10 @@ HWTEST(AppSpawnChildTest, App_Spawn_Child_006, TestSize.Level0)
ASSERT_EQ(ret, 0);
}
/**
* @brief nweb后半部分处理
*
*/
HWTEST(AppSpawnChildTest, NWeb_Spawn_Child_001, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
@ -305,11 +324,9 @@ HWTEST(AppSpawnChildTest, NWeb_Spawn_Child_001, TestSize.Level0)
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req ");
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(NWEBSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_NWEB_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content); // 预加载解析sandbox
content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
@ -319,6 +336,7 @@ HWTEST(AppSpawnChildTest, NWeb_Spawn_Child_001, TestSize.Level0)
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
// spawn
property->client.flags &= ~APP_COLD_START;
ret = AppSpawnChild(content, &property->client);
property = nullptr;
content = nullptr;
@ -343,18 +361,15 @@ HWTEST(AppSpawnChildTest, NWeb_Spawn_Child_002, TestSize.Level0)
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req");
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(NWEBSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_NWEB_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content); // 预加载解析sandbox
content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ret = APPSPAWN_ARG_INVALID;
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
@ -395,17 +410,15 @@ HWTEST(AppSpawnChildTest, NWeb_Spawn_Child_004, TestSize.Level0)
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(NWEBSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_NWEB_SPAWN);
content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
// spawn prepare process
property->client.flags &= ~APP_COLD_START;
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
// spawn
ret = AppSpawnChild(content, &property->client);
@ -440,19 +453,18 @@ HWTEST(AppSpawnChildTest, NWeb_Spawn_Child_005, TestSize.Level0)
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
char path[PATH_MAX] = {};
content = AppSpawnCreateContent(NWEBSPAWN_SOCKET_NAME, path, sizeof(path), MODE_FOR_NWEB_SPAWN);
content = CreateTestAppSpawnContent(NWEBSPAWN_SOCKET_NAME, MODE_FOR_NWEB_SPAWN);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ServerStageHookExecute(STAGE_SERVER_PRELOAD, content);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
// spawn
property->client.flags &= ~APP_COLD_START;
ret = AppSpawnChild(content, &property->client);
property = nullptr;
content = nullptr;
@ -464,4 +476,209 @@ HWTEST(AppSpawnChildTest, NWeb_Spawn_Child_005, TestSize.Level0)
LE_CloseLoop(LE_GetDefaultLoop());
ASSERT_EQ(ret, 0);
}
static std::string GetColdRunArgs(AppSpawningCtx *property, bool isNweb, const char *arg)
{
std::string argStr = arg;
int ret = WriteMsgToChild(property, isNweb);
APPSPAWN_CHECK(ret == 0, return nullptr,
"Failed to get shm for %{public}s errno %{public}d", GetProcessName(property), errno);
argStr += " -fd -1 0 ";
argStr += std::to_string(property->forkCtx.msgSize);
argStr += " -param ";
argStr += GetProcessName(property);
argStr += " ";
argStr += std::to_string(property->client.id);
return argStr;
}
/**
* @brief
*
*/
HWTEST(AppSpawnChildTest, App_Spawn_Cold_Run_001, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
AppSpawnContent *content = nullptr;
CmdArgs *args = nullptr;
int ret = -1;
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
// set cold start flags
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
// run in cold mode
// child run in TestRunChildProcessor
RegChildLooper(content, TestRunChildProcessor);
content->runAppSpawn(content, args->argc, args->argv);
ret = 0;
} while (0);
if (args) {
free(args);
}
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(AppSpawnChildTest, App_Spawn_Cold_Run_002, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
AppSpawnContent *content = nullptr;
CmdArgs *args = nullptr;
int ret = -1;
do {
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req ");
// set cold start flags
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
std::string cmd = GetColdRunArgs(property, true, "appspawn -mode nweb_cold ");
content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ASSERT_EQ(content->mode, MODE_FOR_NWEB_COLD_RUN);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
// run in cold mode
// child run in TestRunChildProcessor
RegChildLooper(content, TestRunChildProcessor);
content->runAppSpawn(content, args->argc, args->argv);
ret = 0;
} while (0);
if (args) {
free(args);
}
DeleteAppSpawningCtx(property);
DeleteAppSpawnMgr(GetAppSpawnMgr());
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(AppSpawnChildTest, App_Spawn_Cold_Run_003, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
AppSpawnContent *content = nullptr;
CmdArgs *args = nullptr;
int ret = -1;
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
// asan set cold
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_FORCE);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
// run in cold mode
// child run in TestRunChildProcessor
RegChildLooper(content, TestRunChildProcessor);
content->runAppSpawn(content, args->argc, args->argv);
ret = 0;
} while (0);
if (args) {
free(args);
}
DeleteAppSpawningCtx(property);
DeleteAppSpawnMgr(GetAppSpawnMgr());
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(AppSpawnChildTest, App_Spawn_Cold_Run_004, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
AppSpawnContent *content = nullptr;
CmdArgs *args = nullptr;
int ret = -1;
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
// asan set cold
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
std::string cmd = GetColdRunArgs(property, false, "appspawn -mode app_cold ");
content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
// add property to content
OH_ListAddTail(&GetAppSpawnMgr()->appSpawnQueue, &property->node);
ProcessAppSpawnDumpMsg(nullptr);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
// run in cold mode
// child run in TestRunChildProcessor
RegChildLooper(content, TestRunChildProcessor);
content->runAppSpawn(content, args->argc, args->argv);
property = nullptr;
ret = 0;
} while (0);
if (args) {
free(args);
}
DeleteAppSpawningCtx(property);
DeleteAppSpawnMgr(GetAppSpawnMgr());
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
} // namespace OHOS

View File

@ -13,10 +13,8 @@
* limitations under the License.
*/
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <unistd.h>
@ -41,7 +39,7 @@ using namespace testing::ext;
using namespace OHOS;
namespace OHOS {
static AppSpawnTestHelper g_testHelper;
static OHOS::AppSpawnTestServer *g_testServer = nullptr;
class AppSpawnColdRunTest : public testing::Test {
public:
static void SetUpTestCase() {}
@ -118,8 +116,10 @@ static int HandleExecvStub(const char *pathName, char *const argv[])
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_001, TestSize.Level0)
{
OHOS::AppSpawnTestServer testServer("appspawn -mode appspawn");
testServer.Start(nullptr);
g_testServer = new OHOS::AppSpawnTestServer("appspawn -mode appspawn");
ASSERT_EQ(g_testServer != nullptr, 1);
g_testServer->Start(nullptr);
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
StubNode *node = GetStubNode(STUB_EXECV);
@ -127,7 +127,7 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_001, TestSize.Level0)
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", APPSPAWN_SERVER_NAME);
AppSpawnReqMsgHandle reqHandle = testServer.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
// set cold start flags
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
@ -136,21 +136,18 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_001, TestSize.Level0)
node->arg = reinterpret_cast<void *>(HandleExecvStub);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("App_Spawn_Cold_Run_001 Kill pid %{public}d %{public}d", result.pid, result.result);
if (ret == 0 && result.pid > 0) {
APPSPAWN_LOGI("App_Spawn_Cold_Run_001 Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
}
ret = 0;
} while (0);
testServer.Stop();
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_002, TestSize.Level0)
{
OHOS::AppSpawnTestServer testServer("appspawn -mode appspawn");
testServer.Start(nullptr);
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
StubNode *node = GetStubNode(STUB_EXECV);
@ -158,7 +155,7 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_002, TestSize.Level0)
do {
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
AppSpawnReqMsgHandle reqHandle = testServer.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
// set cold start flags
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
@ -167,213 +164,24 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_002, TestSize.Level0)
node->arg = reinterpret_cast<void *>(HandleExecvStub);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("App_Spawn_Cold_Run_002 Kill pid %{public}d %{public}d", result.pid, result.result);
if (ret == 0 && result.pid > 0) {
APPSPAWN_LOGI("App_Spawn_Cold_Run_002 Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
}
ret = 0;
} while (0);
testServer.Stop();
AppSpawnClientDestroy(clientHandle);
node->flags &= ~STUB_NEED_CHECK;
ASSERT_EQ(ret, 0);
}
static std::string GetColdRunArgs(AppSpawningCtx *property, const char *arg)
{
std::string argStr = arg;
int ret = WriteMsgToChild(property);
APPSPAWN_CHECK(ret == 0, return nullptr,
"Failed to get shm for %{public}s errno %{public}d", GetProcessName(property), errno);
argStr += " -fd -1 0 ";
argStr += std::to_string(property->forkCtx.msgSize);
argStr += " -param ";
argStr += GetProcessName(property);
argStr += " ";
argStr += std::to_string(property->client.id);
return argStr;
}
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_003, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
AppSpawnContent *content = nullptr;
CmdArgs *args = nullptr;
int ret = -1;
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
// set cold start flags
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
std::string cmd = GetColdRunArgs(property, "appspawn -mode app_cold ");
content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
content->runAppSpawn(content, args->argc, args->argv);
ret = 0;
} while (0);
if (args) {
free(args);
}
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_004, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
AppSpawnContent *content = nullptr;
CmdArgs *args = nullptr;
int ret = -1;
do {
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", NWEBSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req ");
// set cold start flags
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
std::string cmd = GetColdRunArgs(property, "appspawn -mode nweb_cold ");
content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ASSERT_EQ(content->mode, MODE_FOR_NWEB_COLD_RUN);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
content->runAppSpawn(content, args->argc, args->argv);
ret = 0;
} while (0);
if (args) {
free(args);
}
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_005, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
AppSpawnContent *content = nullptr;
CmdArgs *args = nullptr;
int ret = -1;
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
// asan set cold
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_FORCE);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
std::string cmd = GetColdRunArgs(property, "appspawn -mode app_cold ");
content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
content->runAppSpawn(content, args->argc, args->argv);
ret = 0;
} while (0);
if (args) {
free(args);
}
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_006, TestSize.Level0)
{
AppSpawnClientHandle clientHandle = nullptr;
AppSpawnReqMsgHandle reqHandle = 0;
AppSpawningCtx *property = nullptr;
AppSpawnContent *content = nullptr;
CmdArgs *args = nullptr;
int ret = -1;
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
// asan set cold
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ASANENABLED);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_GWP_ENABLED_NORMAL);
ret = APPSPAWN_ARG_INVALID;
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
std::string cmd = GetColdRunArgs(property, "appspawn -mode app_cold ");
content = AppSpawnTestHelper::StartSpawnServer(cmd, args);
APPSPAWN_CHECK_ONLY_EXPER(content != nullptr, break);
ASSERT_EQ(content->mode, MODE_FOR_APP_COLD_RUN);
// add property to content
OH_ListAddTail(&GetAppSpawnMgr()->appSpawnQueue, &property->node);
ProcessAppSpawnDumpMsg(nullptr);
// spawn prepare process
AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, content, &property->client);
AppSpawnHookExecute(STAGE_CHILD_PRE_COLDBOOT, 0, content, &property->client);
content->runAppSpawn(content, args->argc, args->argv);
property = nullptr;
ret = 0;
} while (0);
if (args) {
free(args);
}
DeleteAppSpawningCtx(property);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
* @brief abort
*
*/
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_008, TestSize.Level0)
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_003, TestSize.Level0)
{
// child abort
OHOS::AppSpawnTestServer testServer("appspawn -mode appspawn");
testServer.Start(nullptr);
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
StubNode *node = GetStubNode(STUB_EXECV);
@ -381,7 +189,7 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_008, TestSize.Level0)
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", APPSPAWN_SERVER_NAME);
AppSpawnReqMsgHandle reqHandle = testServer.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
// set cold start flags
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
@ -390,14 +198,12 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_008, TestSize.Level0)
node->arg = reinterpret_cast<void *>(ExecvAbortStub);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGI("AppSpawnClientSendMsg result %{public}d ", ret);
APPSPAWN_LOGV("App_Spawn_Cold_Run_003 Kill pid %{public}d %{public}d", result.pid, result.result);
if (ret == 0 && result.pid > 0) {
APPSPAWN_LOGI("Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
}
ret = 0;
} while (0);
testServer.Stop();
AppSpawnClientDestroy(clientHandle);
node->flags &= ~STUB_NEED_CHECK;
ASSERT_EQ(ret, 0);
@ -407,10 +213,8 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_008, TestSize.Level0)
* @brief
*
*/
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_009, TestSize.Level0)
HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_004, TestSize.Level0)
{
OHOS::AppSpawnTestServer testServer("appspawn -mode appspawn");
testServer.Start(nullptr);
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
StubNode *node = GetStubNode(STUB_EXECV);
@ -418,7 +222,7 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_009, TestSize.Level0)
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", APPSPAWN_SERVER_NAME);
AppSpawnReqMsgHandle reqHandle = testServer.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
// set cold start flags
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_COLD_BOOT);
@ -427,16 +231,18 @@ HWTEST(AppSpawnColdRunTest, App_Spawn_Cold_Run_009, TestSize.Level0)
node->arg = reinterpret_cast<void *>(ExecvTimeoutStub);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGI("AppSpawnClientSendMsg result %{public}d ", ret);
APPSPAWN_LOGV("App_Spawn_Cold_Run_004 Kill pid %{public}d %{public}d", result.pid, result.result);
if (ret == 0 && result.pid > 0) {
APPSPAWN_LOGI("Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
}
ret = 0;
} while (0);
testServer.Stop();
AppSpawnClientDestroy(clientHandle);
node->flags &= ~STUB_NEED_CHECK;
ASSERT_EQ(ret, 0);
g_testServer->Stop();
delete g_testServer;
}
} // namespace OHOS

View File

@ -27,6 +27,8 @@
#include "appspawn_permission.h"
#include "appspawn_sandbox.h"
#include "appspawn_utils.h"
#include "cJSON.h"
#include "json_utils.h"
#include "securec.h"
#include "app_spawn_stub.h"
@ -45,149 +47,7 @@ public:
};
/**
* @brief test for appspawn_manager.h
*
*/
/**
* @brief IsNWebSpawnMode
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_IsNWebSpawnMode_001, TestSize.Level0)
{
int ret = IsNWebSpawnMode(nullptr);
EXPECT_EQ(0, ret); // false
AppSpawnMgr content;
content.content.mode = MODE_FOR_NWEB_SPAWN;
ret = IsNWebSpawnMode(&content);
EXPECT_EQ(1, ret); // true
content.content.mode = MODE_INVALID;
ret = IsNWebSpawnMode(&content);
EXPECT_EQ(0, ret); // true
}
/**
* @brief IsColdRunMode
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_IsColdRunMode_001, TestSize.Level0)
{
int ret = IsColdRunMode(nullptr);
EXPECT_EQ(0, ret); // false
AppSpawnMgr content;
content.content.mode = MODE_FOR_APP_COLD_RUN;
ret = IsColdRunMode(&content);
EXPECT_EQ(1, ret); // true
content.content.mode = MODE_FOR_NWEB_COLD_RUN;
ret = IsColdRunMode(&content);
EXPECT_EQ(1, ret); // true
content.content.mode = MODE_INVALID;
ret = IsColdRunMode(&content);
EXPECT_EQ(0, ret); // true
}
/**
* @brief GetAppSpawnMsgType
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetAppSpawnMsgType_001, TestSize.Level0)
{
int ret = GetAppSpawnMsgType(nullptr);
EXPECT_EQ(MAX_TYPE_INVALID, ret);
AppSpawningCtx content = {};
AppSpawnMsgNode node = {};
content.message = &node;
ret = GetAppSpawnMsgType(&content);
EXPECT_NE(MAX_TYPE_INVALID, ret);
content.message->msgHeader.msgType = MODE_INVALID;
ret = GetAppSpawnMsgType(&content);
EXPECT_EQ(MAX_TYPE_INVALID, ret);
}
/**
* @brief GetBundleName
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetBundleName_001, TestSize.Level0)
{
const char *bundleName = GetBundleName(nullptr);
EXPECT_EQ(nullptr, bundleName);
}
/**
* @brief GetAppProperty
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetAppProperty_001, TestSize.Level0)
{
void *ret = GetAppProperty(nullptr, 8);
EXPECT_EQ(nullptr, ret); // false
AppSpawningCtx content = {};
AppSpawnMsgNode node = {};
content.message = &node;
ret = GetAppProperty(&content, 8);
EXPECT_EQ(nullptr, ret); // true
ret = GetAppProperty(&content, 9);
EXPECT_EQ(nullptr, ret); // true
ret = GetAppProperty(&content, 10);
EXPECT_EQ(nullptr, ret); // true
}
/**
* @brief GetProcessName
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetProcessName_001, TestSize.Level0)
{
const char *ret = GetProcessName(nullptr);
EXPECT_EQ(nullptr, ret); // false
AppSpawningCtx content = {};
AppSpawnMsgNode node = {};
content.message = &node;
const char *processName = "com.example.myapplication";
int isRst = strncpy_s(content.message->msgHeader.processName, sizeof(content.message->msgHeader.processName) - 1,
processName, strlen(processName));
EXPECT_EQ(0, isRst);
ret = GetProcessName(&content);
EXPECT_NE(nullptr, ret); // true
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_IsDeveloperModeOn_001, TestSize.Level0)
{
int ret = IsDeveloperModeOn(nullptr);
EXPECT_EQ(0, ret); // false
AppSpawningCtx content = {};
content.client.flags = APP_DEVELOPER_MODE;
ret = IsDeveloperModeOn(&content);
EXPECT_EQ(1, ret);
content.client.flags = APP_COLD_START;
ret = IsDeveloperModeOn(&content);
EXPECT_EQ(0, ret);
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetAppPropertyExt_001, TestSize.Level0)
{
uint32_t size = strlen("xxx");
void *ret = GetAppPropertyExt(nullptr, "xxx", &size);
EXPECT_EQ(nullptr, ret); // false
AppSpawningCtx context;
ret = GetAppPropertyExt(&context, nullptr, &size);
EXPECT_EQ(nullptr, ret); // false
context.message = nullptr;
ret = GetAppPropertyExt(&context, "name", &size);
EXPECT_EQ(nullptr, ret); // false
}
/**
* @brief test interface in appspawn_modulemgr.h
* @brief module interface
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_AppSpawnModuleMgrInstall_001, TestSize.Level1)
@ -233,58 +93,8 @@ HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_AppSpawnModuleMgrUnInstall_001, Te
AppSpawnModuleMgrUnInstall(100);
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_DeleteAppSpawnHookMgr_001, TestSize.Level1)
{
DeleteAppSpawnHookMgr();
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_ServerStageHookExecute_001, TestSize.Level1)
{
int ret = ServerStageHookExecute(static_cast<AppSpawnHookStage>(0), nullptr);
EXPECT_NE(0, ret);
AppSpawnContent content;
content.longProcName = nullptr;
content.longProcNameLen = 256;
content.coldStartApp = nullptr;
content.runAppSpawn = nullptr;
ret = ServerStageHookExecute(static_cast<AppSpawnHookStage>(0), &content);
EXPECT_NE(0, ret);
}
/**
* @brief int ProcessMgrHookExecute(AppSpawnHookStage stage, const AppSpawnContent *content,
* const AppSpawnedProcess *appInfo);
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_ProcessMgrHookExecute_001, TestSize.Level1)
{
TagAppSpawnedProcess appInfo;
appInfo.pid = 33; // 33
appInfo.uid = 200000 * 200 + 21; // 200000 200 21
appInfo.max = 0;
appInfo.exitStatus = 0;
AppSpawnContent content;
content.longProcName = nullptr;
content.longProcNameLen = 256;
content.coldStartApp = nullptr;
content.runAppSpawn = nullptr;
int ret = ProcessMgrHookExecute(STAGE_SERVER_APP_ADD, &content, &appInfo);
EXPECT_EQ(0, ret);
ret = ProcessMgrHookExecute(STAGE_SERVER_APP_DIED, &content, &appInfo);
EXPECT_EQ(0, ret);
ret = ProcessMgrHookExecute(STAGE_SERVER_APP_DIED, nullptr, nullptr);
EXPECT_NE(0, ret);
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_AppSpawnHookExecute_001, TestSize.Level1)
{
int ret = AppSpawnHookExecute(STAGE_PARENT_PRE_FORK, 0, nullptr, nullptr);
EXPECT_NE(0, ret);
}
/**
* @brief AddPreloadHook
* @brief hook interface
*
*/
static int TestAppPreload(AppSpawnMgr *content)
@ -292,18 +102,29 @@ static int TestAppPreload(AppSpawnMgr *content)
return 0;
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Add_Preload_Hook_001, TestSize.Level0)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Server_Hook_001, TestSize.Level0)
{
int ret = AddPreloadHook(HOOK_PRIO_HIGHEST, TestAppPreload);
EXPECT_EQ(0, ret);
ret = AddPreloadHook(-1, TestAppPreload);
EXPECT_EQ(0, ret);
ret = AddPreloadHook(1001, TestAppPreload);
EXPECT_EQ(0, ret);
ret = AddPreloadHook(8000, TestAppPreload); // 8000 正常
EXPECT_EQ(0, ret);
ret = AddPreloadHook(8000, nullptr); // 影响功能测试
EXPECT_NE(0, ret);
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
int ret = 0;
for (int i = 0; i < STAGE_MAX; i++) {
AppSpawnContent *content = reinterpret_cast<AppSpawnContent *>(mgr);
for (int k = 0; k <= HOOK_PRIO_LOWEST + 1000; k += 1000) { // 1000
ret = AddServerStageHook(static_cast<AppSpawnHookStage>(i), k, TestAppPreload);
EXPECT_EQ(ret == 0, (i >= STAGE_SERVER_PRELOAD) && (i <= STAGE_SERVER_EXIT));
}
ret = AddServerStageHook(static_cast<AppSpawnHookStage>(i), 0, nullptr);
EXPECT_EQ(ret == 0, 0);
ret = ServerStageHookExecute(static_cast<AppSpawnHookStage>(i), content);
EXPECT_EQ(ret == 0, (i >= STAGE_SERVER_PRELOAD) && (i <= STAGE_SERVER_EXIT));
ret = ServerStageHookExecute(static_cast<AppSpawnHookStage>(i), nullptr);
EXPECT_NE(ret, 0);
}
DeleteAppSpawnMgr(mgr);
}
/**
@ -315,27 +136,43 @@ static int TestAppSpawn(AppSpawnMgr *content, AppSpawningCtx *property)
return 0;
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Add_App_Spawn_Hook_001, TestSize.Level0)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_App_Spawn_Hook_001, TestSize.Level0)
{
int ret = AddAppSpawnHook(STAGE_CHILD_POST_RELY, HOOK_PRIO_HIGHEST, TestAppSpawn);
EXPECT_EQ(0, ret);
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawningCtx *appCtx = CreateAppSpawningCtx();
EXPECT_EQ(appCtx != nullptr, 1);
ret = AddAppSpawnHook(static_cast<AppSpawnHookStage>(-1), HOOK_PRIO_HIGHEST, TestAppSpawn);
EXPECT_NE(0, ret);
ret = AddAppSpawnHook(static_cast<AppSpawnHookStage>(1), HOOK_PRIO_HIGHEST, TestAppSpawn);
EXPECT_NE(0, ret);
ret = AddAppSpawnHook(static_cast<AppSpawnHookStage>(11), HOOK_PRIO_HIGHEST, TestAppSpawn);
EXPECT_NE(0, ret);
ret = AddAppSpawnHook(static_cast<AppSpawnHookStage>(53), HOOK_PRIO_HIGHEST, TestAppSpawn);
EXPECT_NE(0, ret);
ret = AddAppSpawnHook(STAGE_CHILD_POST_RELY, -1, TestAppSpawn);
EXPECT_EQ(0, ret);
ret = AddAppSpawnHook(STAGE_CHILD_POST_RELY, 1001, TestAppSpawn);
EXPECT_EQ(0, ret);
ret = AddAppSpawnHook(STAGE_CHILD_POST_RELY, 8000, TestAppSpawn); // 8000 正常
EXPECT_EQ(0, ret);
ret = AddAppSpawnHook(STAGE_CHILD_POST_RELY, 8000, nullptr); // 影响功能测试
EXPECT_NE(0, ret);
int ret = 0;
for (int i = 0; i < STAGE_MAX; i++) {
AppSpawnClient *client = reinterpret_cast<AppSpawnClient *>(appCtx);
AppSpawnContent *content = reinterpret_cast<AppSpawnContent *>(mgr);
for (int k = 0; k <= HOOK_PRIO_LOWEST + 1000; k += 1000) { // 1000
ret = AddAppSpawnHook(static_cast<AppSpawnHookStage>(i), k, TestAppSpawn);
EXPECT_EQ(ret == 0, (i >= STAGE_PARENT_PRE_FORK) && (i <= STAGE_CHILD_PRE_RUN));
}
ret = AddAppSpawnHook(static_cast<AppSpawnHookStage>(i), 0, nullptr);
EXPECT_EQ(ret == 0, 0);
if (i == STAGE_CHILD_PRE_RUN || i == STAGE_CHILD_EXECUTE) {
printf("App_Spawn_App_Spawn_Hook_001 %d \n", i);
continue;
}
ret = AppSpawnHookExecute(static_cast<AppSpawnHookStage>(i), 0, content, client);
EXPECT_EQ(ret == 0, (i >= STAGE_PARENT_PRE_FORK) && (i <= STAGE_CHILD_PRE_RUN));
ret = AppSpawnHookExecute(static_cast<AppSpawnHookStage>(i), 0, nullptr, client);
EXPECT_NE(ret, 0);
ret = AppSpawnHookExecute(static_cast<AppSpawnHookStage>(i), 0, nullptr, nullptr);
EXPECT_NE(ret, 0);
ret = AppSpawnHookExecute(static_cast<AppSpawnHookStage>(i), 0, content, nullptr);
EXPECT_NE(ret, 0);
}
DeleteAppSpawningCtx(appCtx);
DeleteAppSpawnMgr(mgr);
}
/**
@ -347,26 +184,38 @@ static int ReportProcessExitInfo(const AppSpawnMgr *content, const AppSpawnedPro
return 0;
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Add_App_Change_Hook_001, TestSize.Level0)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Process_Hook_001, TestSize.Level0)
{
int ret = AddProcessMgrHook(STAGE_SERVER_APP_DIED, 0, ReportProcessExitInfo);
EXPECT_EQ(0, ret);
ret = AddProcessMgrHook(static_cast<AppSpawnHookStage>(-1), 0, ReportProcessExitInfo);
EXPECT_NE(0, ret);
ret = AddProcessMgrHook(static_cast<AppSpawnHookStage>(1), 0, ReportProcessExitInfo);
EXPECT_NE(0, ret);
ret = AddProcessMgrHook(static_cast<AppSpawnHookStage>(11), 0, ReportProcessExitInfo);
EXPECT_NE(0, ret);
ret = AddProcessMgrHook(static_cast<AppSpawnHookStage>(53), 0, ReportProcessExitInfo);
EXPECT_NE(0, ret);
ret = AddProcessMgrHook(STAGE_SERVER_APP_DIED, -1, ReportProcessExitInfo);
EXPECT_EQ(0, ret);
ret = AddProcessMgrHook(STAGE_SERVER_APP_DIED, 1001, ReportProcessExitInfo);
EXPECT_EQ(0, ret);
ret = AddProcessMgrHook(STAGE_SERVER_APP_DIED, 8000, ReportProcessExitInfo); // 8000 正常
EXPECT_EQ(0, ret);
ret = AddProcessMgrHook(STAGE_SERVER_APP_ADD, 0, nullptr); // 影响功能测试
EXPECT_NE(0, ret);
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnedProcess *app = AddSpawnedProcess(1000, "test-001");
EXPECT_EQ(app != nullptr, 1);
int ret = 0;
for (int i = 0; i < STAGE_MAX; i++) {
for (int k = 0; k <= HOOK_PRIO_LOWEST + 1000; k += 1000) { // 1000
ret = AddProcessMgrHook(static_cast<AppSpawnHookStage>(i), k, ReportProcessExitInfo);
EXPECT_EQ(ret == 0, (i == STAGE_SERVER_APP_ADD || i == STAGE_SERVER_APP_DIED));
}
ret = AddProcessMgrHook(static_cast<AppSpawnHookStage>(i), 0, nullptr);
EXPECT_EQ(ret == 0, 0);
ret = ProcessMgrHookExecute(static_cast<AppSpawnHookStage>(i), reinterpret_cast<AppSpawnContent *>(mgr), app);
EXPECT_EQ(ret == 0, (i == STAGE_SERVER_APP_ADD || i == STAGE_SERVER_APP_DIED));
ret = ProcessMgrHookExecute(static_cast<AppSpawnHookStage>(i), nullptr, app);
EXPECT_NE(ret, 0);
ret = ProcessMgrHookExecute(static_cast<AppSpawnHookStage>(i), nullptr, nullptr);
EXPECT_NE(ret, 0);
ret = ProcessMgrHookExecute(static_cast<AppSpawnHookStage>(i),
reinterpret_cast<AppSpawnContent *>(mgr), nullptr);
EXPECT_NE(ret, 0);
}
TerminateSpawnedProcess(app);
DeleteAppSpawnMgr(mgr);
}
/**
@ -378,402 +227,209 @@ static int TestChildLoop(AppSpawnContent *content, AppSpawnClient *client)
return 0;
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Add_App_RegChildLooper_001, TestSize.Level0)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_RegChildLooper_001, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
RegChildLooper(reinterpret_cast<AppSpawnContent *>(mgr), TestChildLoop);
RegChildLooper(reinterpret_cast<AppSpawnContent *>(mgr), nullptr);
DeleteAppSpawnMgr(mgr);
RegChildLooper(nullptr, TestChildLoop);
AppSpawnContent content;
content.longProcName = nullptr;
content.longProcNameLen = 256;
content.coldStartApp = nullptr;
content.runAppSpawn = nullptr;
RegChildLooper(&content, nullptr);
RegChildLooper(&content, TestChildLoop);
}
/**
* @brief MakeDirRec
* int MakeDirRec(const char *path, mode_t mode, int lastPath);
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Add_App_MakeDirRec_001, TestSize.Level0)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_MakeDirRec_001, TestSize.Level0)
{
int ret = MakeDirRec(nullptr, 0, -1);
EXPECT_EQ(ret, -1);
ret = MakeDirRec("/data/appspawn/", 0, -1);
EXPECT_NE(ret, -1);
ret = MakeDirRec(APPSPAWN_BASE_DIR "/test_appspawn/3", 0, 0);
EXPECT_EQ(ret, 0);
ret = MakeDirRec(APPSPAWN_BASE_DIR "/test_appspawn/1", 0711, 0); // create file
EXPECT_EQ(ret, 0);
ret = MakeDirRec(APPSPAWN_BASE_DIR "/test_appspawn/2", 0711, 1); // create path
EXPECT_EQ(ret, 0);
}
/**
* @brief SandboxMountPath
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Add_App_SandboxMountPath_001, TestSize.Level0)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_DiffTime_001, TestSize.Level0)
{
MountArg arg = {};
arg.originPath = "/data/";
arg.destinationPath = "/data/appspawn/test";
arg.mountSharedFlag = 1;
struct timespec endTime;
struct timespec startTime;
clock_gettime(CLOCK_MONOTONIC, &startTime);
startTime.tv_sec += 1;
clock_gettime(CLOCK_MONOTONIC, &endTime);
uint64_t diff = DiffTime(&startTime, &endTime);
EXPECT_NE(diff, 0);
diff = DiffTime(nullptr, &endTime);
EXPECT_EQ(diff, 0);
diff = DiffTime(&startTime, nullptr);
EXPECT_EQ(diff, 0);
}
int ret = SandboxMountPath(&arg);
int TestSplitStringHandle(const char *str, void *context)
{
static int index = 0;
if (index == 3) { // 3 test
EXPECT_EQ(strcmp(str, "dddd"), 0);
}
index++;
return 0;
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_StringSplit_001, TestSize.Level0)
{
const char *testStr = "aaaa|bbbb|cccc |dddd| eeee| 1111 | 2222";
int ret = StringSplit(testStr, "|", nullptr, TestSplitStringHandle);
EXPECT_EQ(ret, 0);
ret = SandboxMountPath(&arg);
EXPECT_EQ(ret, 0);
ret = SandboxMountPath(nullptr);
ret = StringSplit(testStr, "|", nullptr, nullptr);
EXPECT_NE(ret, 0);
ret = StringSplit(testStr, nullptr, nullptr, TestSplitStringHandle);
EXPECT_NE(ret, 0);
ret = StringSplit(nullptr, "|", nullptr, TestSplitStringHandle);
EXPECT_NE(ret, 0);
ret = StringSplit(nullptr, "|", nullptr, nullptr);
EXPECT_NE(ret, 0);
ret = StringSplit(nullptr, nullptr, nullptr, nullptr);
EXPECT_NE(ret, 0);
arg.mountSharedFlag = -1;
ret = SandboxMountPath(&arg);
EXPECT_EQ(ret, 0);
}
/**
* @brief AddVariableReplaceHandler
*
*/
static int TestReplaceVarHandler(const SandboxContext *context, const char *buffer, uint32_t bufferLen,
uint32_t *realLen, const VarExtraData *extraData)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetLastStr_001, TestSize.Level0)
{
const char *testStr = "aaaa|bbbb|cccc |dddd| eeee| 1111 | 2222";
char *tmp = GetLastStr(testStr, "2222");
EXPECT_EQ(tmp != nullptr, 1);
tmp = GetLastStr(testStr, nullptr);
EXPECT_EQ(tmp != nullptr, 0);
tmp = GetLastStr(nullptr, nullptr);
EXPECT_EQ(tmp != nullptr, 0);
tmp = GetLastStr(nullptr, "2222");
EXPECT_EQ(tmp != nullptr, 0);
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_DumpCurrentDir_001, TestSize.Level0)
{
char path[PATH_MAX] = {};
DumpCurrentDir(path, sizeof(path), APPSPAWN_BASE_DIR "/test_appspawn/");
DumpCurrentDir(path, sizeof(path), nullptr);
DumpCurrentDir(path, 0, nullptr);
DumpCurrentDir(path, 0, APPSPAWN_BASE_DIR "/test_appspawn/");
DumpCurrentDir(nullptr, sizeof(path), APPSPAWN_BASE_DIR "/test_appspawn/");
DumpCurrentDir(nullptr, sizeof(path), nullptr);
}
static int TestParseAppSandboxConfig(const cJSON *root, ParseJsonContext *context)
{
return 0;
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_AddVariableReplaceHandler_001, TestSize.Level0)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_ParseJsonConfig_001, TestSize.Level0)
{
int ret = AddVariableReplaceHandler(nullptr, nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = AddVariableReplaceHandler("xxx", nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = AddVariableReplaceHandler(nullptr, TestReplaceVarHandler);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = AddVariableReplaceHandler("global", TestReplaceVarHandler);
EXPECT_EQ(ret, 0);
ret = AddVariableReplaceHandler("<Test-Var-001>", TestReplaceVarHandler);
EXPECT_EQ(ret, 0);
}
/**
* @brief RegisterExpandSandboxCfgHandler
*
*/
static int TestProcessExpandSandboxCfg(const SandboxContext *context, const AppSpawnSandboxCfg *appSandBox,
const char *name)
{
return 0;
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_RegisterExpandSandboxCfgHandler_001, TestSize.Level0)
{
int ret = RegisterExpandSandboxCfgHandler(nullptr, 0, nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = RegisterExpandSandboxCfgHandler("test", 0, nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = RegisterExpandSandboxCfgHandler(nullptr, 0, TestProcessExpandSandboxCfg);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
int ret = 0;
#ifdef APPSPAWN_SANDBOX_NEW
ret = RegisterExpandSandboxCfgHandler("HspList", 0, TestProcessExpandSandboxCfg);
EXPECT_EQ(ret, APPSPAWN_NODE_EXIST);
ret = ParseJsonConfig("etc/sandbox", WEB_SANDBOX_FILE_NAME, TestParseAppSandboxConfig, nullptr);
EXPECT_EQ(ret, 0);
ret = ParseJsonConfig("etc/sandbox", APP_SANDBOX_FILE_NAME, TestParseAppSandboxConfig, nullptr);
EXPECT_EQ(ret, 0);
#else
ret = RegisterExpandSandboxCfgHandler("HspList", 0, TestProcessExpandSandboxCfg);
ret = ParseJsonConfig("etc/sandbox", APP_SANDBOX_FILE_NAME, TestParseAppSandboxConfig, nullptr);
EXPECT_EQ(ret, 0);
#endif
ret = RegisterExpandSandboxCfgHandler("HspList", -1, TestProcessExpandSandboxCfg);
EXPECT_EQ(ret, APPSPAWN_NODE_EXIST);
ret = ParseJsonConfig("etc/sandbox", APP_SANDBOX_FILE_NAME, nullptr, nullptr);
EXPECT_NE(ret, 0);
ret = ParseJsonConfig("etc/sandbox", nullptr, TestParseAppSandboxConfig, nullptr);
EXPECT_NE(ret, 0);
ret = ParseJsonConfig("etc/sandbox", nullptr, nullptr, nullptr);
EXPECT_NE(ret, 0);
ret = ParseJsonConfig(nullptr, APP_SANDBOX_FILE_NAME, TestParseAppSandboxConfig, nullptr);
EXPECT_NE(ret, 0);
ret = ParseJsonConfig(nullptr, nullptr, TestParseAppSandboxConfig, nullptr);
EXPECT_NE(ret, 0);
ret = ParseJsonConfig(nullptr, nullptr, nullptr, nullptr);
EXPECT_NE(ret, 0);
}
/**
* @brief AddSandboxPermissionNode
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_AddSandboxPermissionNode_001, TestSize.Level1)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetJsonObjFromFile_001, TestSize.Level0)
{
int ret = AddSandboxPermissionNode(nullptr, nullptr);
EXPECT_NE(0, ret);
ret = AddSandboxPermissionNode("xxx", nullptr);
EXPECT_NE(0, ret);
SandboxQueue queue;
OH_ListInit(&queue.front);
EXPECT_NE(nullptr, &queue.front);
ret = AddSandboxPermissionNode(nullptr, &queue);
EXPECT_NE(0, ret);
ret = AddSandboxPermissionNode("xxx", &queue);
EXPECT_EQ(0, ret);
cJSON *json = GetJsonObjFromFile("/etc/sandbox/appdata-sandbox.json");
EXPECT_EQ(json != nullptr, 1);
cJSON_Delete(json);
json = GetJsonObjFromFile(nullptr);
EXPECT_EQ(json == nullptr, 1);
}
/**
* @brief GetPermissionIndexInQueue
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetPermissionIndexInQueue_001, TestSize.Level1)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetStringFromJsonObj_001, TestSize.Level0)
{
int ret = GetPermissionIndexInQueue(nullptr, nullptr);
EXPECT_NE(0, ret);
ret = GetPermissionIndexInQueue(nullptr, "xxx");
EXPECT_NE(0, ret);
const std::string buffer = "{ \
\"global\": { \
\"sandbox-root\": \"/mnt/sandbox/<currentUserId>/app-root\", \
\"sandbox-ns-flags\": [ \"pid\", \"net\" ], \
\"sandbox-flags\": 100, \
\"top-sandbox-switch\": \"ON\" \
} \
}";
SandboxQueue queue;
OH_ListInit(&queue.front);
ret = GetPermissionIndexInQueue(&queue, nullptr);
EXPECT_NE(0, ret);
ret = AddSandboxPermissionNode("test", &queue);
EXPECT_EQ(0, ret);
ret = GetPermissionIndexInQueue(&queue, "test1");
EXPECT_NE(0, ret);
ret = GetPermissionIndexInQueue(&queue, "xxx");
EXPECT_NE(0, ret);
cJSON *json = cJSON_Parse(buffer.c_str());
EXPECT_EQ(json != nullptr, 1);
char *tmp = GetStringFromJsonObj(cJSON_GetObjectItemCaseSensitive(json, "global"), "sandbox-root");
EXPECT_EQ(tmp != nullptr, 1);
tmp = GetStringFromJsonObj(cJSON_GetObjectItemCaseSensitive(json, "global"), nullptr);
EXPECT_EQ(tmp == nullptr, 1);
tmp = GetStringFromJsonObj(nullptr, "sandbox-root");
EXPECT_EQ(tmp == nullptr, 1);
tmp = GetStringFromJsonObj(nullptr, nullptr);
EXPECT_EQ(tmp == nullptr, 1);
cJSON_Delete(json);
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetPermissionNodeInQueue_001, TestSize.Level1)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetBoolValueFromJsonObj_001, TestSize.Level0)
{
EXPECT_EQ(nullptr, GetPermissionNodeInQueue(nullptr, nullptr));
EXPECT_EQ(nullptr, GetPermissionNodeInQueue(nullptr, "xxx"));
const std::string buffer = "{ \
\"global\": { \
\"sandbox-root\": \"/mnt/sandbox/<currentUserId>/app-root\", \
\"sandbox-ns-flags\": [ \"pid\", \"net\" ], \
\"sandbox-flags\": 100, \
\"top-sandbox-switch\": \"ON\" \
} \
}";
SandboxQueue queue;
OH_ListInit(&queue.front);
EXPECT_EQ(nullptr, GetPermissionNodeInQueue(&queue, nullptr));
EXPECT_EQ(nullptr, GetPermissionNodeInQueue(&queue, "xxxxx"));
EXPECT_EQ(nullptr, GetPermissionNodeInQueue(&queue, "xxx"));
cJSON *json = cJSON_Parse(buffer.c_str());
EXPECT_EQ(json != nullptr, 1);
bool ret = GetBoolValueFromJsonObj(cJSON_GetObjectItemCaseSensitive(json, "global"), "top-sandbox-switch", false);
EXPECT_EQ(ret, true);
ret = GetBoolValueFromJsonObj(cJSON_GetObjectItemCaseSensitive(json, "global"), nullptr, false);
EXPECT_EQ(ret, 0);
ret = GetBoolValueFromJsonObj(nullptr, "top-sandbox-switch", false);
EXPECT_EQ(ret, 0);
ret = GetBoolValueFromJsonObj(nullptr, nullptr, false);
EXPECT_EQ(ret, 0);
cJSON_Delete(json);
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetPermissionNodeInQueueByIndex_001, TestSize.Level1)
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetIntValueFromJsonObj_001, TestSize.Level0)
{
const SandboxPermissionNode *node = GetPermissionNodeInQueueByIndex(nullptr, 0);
EXPECT_EQ(nullptr, node);
node = GetPermissionNodeInQueueByIndex(nullptr, 0);
EXPECT_EQ(nullptr, node);
SandboxQueue queue;
OH_ListInit(&queue.front);
int ret = AddSandboxPermissionNode("xxx", &queue);
EXPECT_EQ(0, ret);
node = GetPermissionNodeInQueueByIndex(&queue, 0);
EXPECT_NE(nullptr, node);
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_PermissionRenumber_001, TestSize.Level1)
{
int ret = PermissionRenumber(nullptr);
EXPECT_NE(0, ret);
SandboxQueue queue;
OH_ListInit(&queue.front);
ret = PermissionRenumber(&queue);
EXPECT_EQ(0, ret);
}
/**
* @brief AppSpawnSandboxCfg op
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetAppSpawnSandbox_001, TestSize.Level1)
{
AppSpawnSandboxCfg *cfg = CreateAppSpawnSandbox();
EXPECT_NE(nullptr, cfg);
AppSpawnSandboxCfg *sandboxCfg = GetAppSpawnSandbox(nullptr);
EXPECT_EQ(nullptr, sandboxCfg);
AppSpawnMgr content;
OH_ListInit(&content.extData);
sandboxCfg = GetAppSpawnSandbox(&content);
EXPECT_NE(nullptr, cfg);
int ret = LoadAppSandboxConfig(nullptr, 0);
EXPECT_EQ(APPSPAWN_ARG_INVALID, ret);
DumpAppSpawnSandboxCfg(nullptr);
DumpAppSpawnSandboxCfg(cfg);
DeleteAppSpawnSandbox(cfg);
DeleteAppSpawnSandbox(nullptr);
}
/**
* @brief SandboxSection op
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_CreateSandboxSection_001, TestSize.Level1)
{
EXPECT_EQ(nullptr, CreateSandboxSection(nullptr, 0, 0));
EXPECT_EQ(nullptr, CreateSandboxSection("xxx", 99999, 0));
EXPECT_EQ(nullptr, CreateSandboxSection("xxx", 0, 0));
EXPECT_EQ(nullptr, CreateSandboxSection("xxx", 111, 0));
SandboxQueue queue;
OH_ListInit(&queue.front);
EXPECT_EQ(nullptr, GetSandboxSection(nullptr, nullptr));
EXPECT_EQ(nullptr, GetSandboxSection(nullptr, "xxx"));
EXPECT_EQ(nullptr, GetSandboxSection(&queue, nullptr));
EXPECT_EQ(nullptr, GetSandboxSection(&queue, ""));
EXPECT_EQ(nullptr, GetSandboxSection(&queue, "xxx"));
SandboxSection node;
OH_ListInit(&node.front);
OH_ListInit(&node.sandboxNode.node);
SandboxMountNode *mountNode = (SandboxMountNode *)malloc(sizeof(SandboxMountNode));
OH_ListInit(&mountNode->node);
node.nameGroups = &mountNode;
AddSandboxSection(nullptr, nullptr);
AddSandboxSection(nullptr, &queue);
AddSandboxSection(&node, nullptr);
AddSandboxSection(&node, &queue);
EXPECT_EQ(SANDBOX_TAG_INVALID, GetSectionType(nullptr));
EXPECT_NE(SANDBOX_TAG_INVALID, GetSectionType(&node));
DeleteSandboxSection(nullptr);
}
/**
* @brief SandboxMountNode op
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_CreateAppSpawnSandbox_001, TestSize.Level1)
{
EXPECT_EQ(nullptr, CreateSandboxMountNode(0, 0));
EXPECT_EQ(nullptr, CreateSandboxMountNode(sizeof(SandboxMountNode) - 1, 0));
EXPECT_EQ(nullptr, CreateSandboxMountNode(sizeof(PathMountNode) + 1, 0));
SandboxMountNode *node = CreateSandboxMountNode(sizeof(PathMountNode), 0);
EXPECT_NE(nullptr, node);
OH_ListInit(&node->node);
SandboxSection *section = CreateSandboxSection("xxx", sizeof(SandboxSection), 0);
OH_ListInit(&section->front);
ListNode item;
OH_ListInit(&item);
OH_ListAddTail(&section->front, &item);
EXPECT_EQ(nullptr, GetFirstSandboxMountNode(nullptr));
EXPECT_NE(nullptr, GetFirstSandboxMountNode(section));
SandboxSection section1;
OH_ListInit(&section1.front);
EXPECT_EQ(nullptr, GetFirstSandboxMountNode(&section1));
OH_ListInit(&section->front);
OH_ListInit(&section->sandboxNode.node);
AddSandboxMountNode(node, section);
AddSandboxMountNode(nullptr, nullptr);
AddSandboxMountNode(node, nullptr);
AddSandboxMountNode(nullptr, section);
DeleteSandboxMountNode(nullptr);
DeleteSandboxMountNode(node);
SandboxMountNode *node1 = CreateSandboxMountNode(sizeof(PathMountNode), 0);
EXPECT_NE(nullptr, node1);
OH_ListInit(&node1->node);
node1->type = SANDBOX_TAG_INVALID;
DeleteSandboxMountNode(node1);
}
/**
* @brief sandbox mount interface
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_MountSandboxConfigs_001, TestSize.Level1)
{
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
AppSpawnClientHandle clientHandle;
int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(0, ret);
AppSpawnReqMsgHandle reqHandle;
ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, "com.ohos.myapplication", &reqHandle);
AppSpawningCtx property;
OH_ListInit(&property.node);
EXPECT_NE(0, MountSandboxConfigs(nullptr, nullptr, 0));
EXPECT_NE(0, MountSandboxConfigs(sandbox, nullptr, 0));
EXPECT_NE(0, StagedMountSystemConst(nullptr, nullptr, 0));
EXPECT_NE(0, StagedMountSystemConst(sandbox, nullptr, 0));
SandboxContext content;
EXPECT_EQ(0, StagedMountPreUnShare(&content, sandbox));
EXPECT_NE(0, StagedMountPreUnShare(nullptr, nullptr));
EXPECT_NE(0, StagedMountPreUnShare(&content, nullptr));
EXPECT_NE(0, StagedMountPreUnShare(nullptr, sandbox));
EXPECT_NE(0, UnmountSandboxConfigs(sandbox, 0, nullptr));
EXPECT_NE(0, UnmountSandboxConfigs(nullptr, 0, nullptr));
}
/**
* @brief Variable op
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_GetSandboxRealVar_001, TestSize.Level1)
{
AddDefaultVariable();
const char *var = GetSandboxRealVar(nullptr, 0, "", "", nullptr);
EXPECT_EQ(nullptr, var);
SandboxContext content;
var = GetSandboxRealVar(&content, 4, "", "", nullptr);
EXPECT_EQ(nullptr, var);
ClearVariable();
}
/**
* @brief expand config
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_expand_config_001, TestSize.Level1)
{
SandboxContext content;
AppSpawnSandboxCfg *cfg = CreateAppSpawnSandbox();
EXPECT_EQ(APPSPAWN_ARG_INVALID, ProcessExpandAppSandboxConfig(nullptr, nullptr, nullptr));
EXPECT_EQ(APPSPAWN_ARG_INVALID, ProcessExpandAppSandboxConfig(nullptr, cfg, nullptr));
EXPECT_EQ(APPSPAWN_ARG_INVALID, ProcessExpandAppSandboxConfig(nullptr, cfg, "name"));
EXPECT_EQ(APPSPAWN_ARG_INVALID, ProcessExpandAppSandboxConfig(&content, nullptr, nullptr));
EXPECT_EQ(APPSPAWN_ARG_INVALID, ProcessExpandAppSandboxConfig(&content, cfg, nullptr));
EXPECT_NE(APPSPAWN_ARG_INVALID, ProcessExpandAppSandboxConfig(&content, cfg, "test"));
EXPECT_EQ(APPSPAWN_ARG_INVALID, ProcessExpandAppSandboxConfig(&content, cfg, nullptr));
EXPECT_NE(APPSPAWN_ARG_INVALID, ProcessExpandAppSandboxConfig(&content, cfg, "test"));
AddDefaultExpandAppSandboxConfigHandle();
ClearExpandAppSandboxConfigHandle();
}
/**
* @brief Sandbox Context op
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Sandbox_Content_Op_001, TestSize.Level1)
{
SandboxContext *sandboxContent = GetSandboxContext();
EXPECT_NE(nullptr, sandboxContent);
DeleteSandboxContext(nullptr);
DeleteSandboxContext(sandboxContent);
}
/**
* @brief Sandbox Context op
*
*/
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Sandbox_Op_001, TestSize.Level1)
{
EXPECT_EQ(MOUNT_TMP_DEFAULT, GetMountCategory(""));
EXPECT_EQ(MOUNT_TMP_DEFAULT, GetMountCategory(nullptr));
EXPECT_EQ(MOUNT_TMP_DEFAULT, GetMountCategory("name"));
}
HWTEST(AppSpawnModuleInterfaceTest, App_Spawn_Sandbox_Op_002, TestSize.Level1)
{
PathMountNode pathNode;
pathNode.category = 123456;
DumpMountPathMountNode(&pathNode);
EXPECT_EQ(0, GetPathMode("test"));
EXPECT_EQ(0, GetPathMode(nullptr));
EXPECT_EQ(nullptr, GetSandboxFlagInfo(nullptr, nullptr, 0));
EXPECT_EQ(nullptr, GetSandboxFlagInfo("key", nullptr, 0));
EXPECT_EQ(nullptr, GetMountArgTemplate(123456));
const std::string buffer = "{ \
\"global\": { \
\"sandbox-root\": \"/mnt/sandbox/<currentUserId>/app-root\", \
\"sandbox-ns-flags\": [ \"pid\", \"net\" ], \
\"sandbox-flags\": 100, \
\"top-sandbox-switch\": \"ON\" \
} \
}";
cJSON *json = cJSON_Parse(buffer.c_str());
EXPECT_EQ(json != nullptr, 1);
int tmp = GetIntValueFromJsonObj(cJSON_GetObjectItemCaseSensitive(json, "global"), "sandbox-flags", 0);
EXPECT_EQ(tmp, 100);
tmp = GetIntValueFromJsonObj(cJSON_GetObjectItemCaseSensitive(json, "global"), nullptr, 0);
EXPECT_EQ(tmp, 0);
tmp = GetIntValueFromJsonObj(nullptr, "sandbox-flags", 0);
EXPECT_EQ(tmp, 0);
tmp = GetIntValueFromJsonObj(nullptr, nullptr, 0);
EXPECT_EQ(tmp, 0);
cJSON_Delete(json);
}
} // namespace OHOS

View File

@ -345,7 +345,27 @@ static int TestParseAppSandboxConfig(AppSpawnSandboxCfg *sandbox, const char *bu
APPSPAWN_LOGE("TestParseAppSandboxConfig config %s", buffer);
return -1;
}
int ret = ParseAppSandboxConfig(config, sandbox);
int ret = 0;
do {
ret = ParseAppSandboxConfig(config, sandbox);
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
uint32_t depNodeCount = sandbox->depNodeCount;
APPSPAWN_CHECK_ONLY_EXPER(depNodeCount > 0, break);
sandbox->depGroupNodes = (SandboxNameGroupNode **)calloc(1, sizeof(SandboxNameGroupNode *) * depNodeCount);
APPSPAWN_CHECK(sandbox->depGroupNodes != NULL, break, "Failed alloc memory ");
sandbox->depNodeCount = 0;
ListNode *node = sandbox->nameGroupsQueue.front.next;
while (node != &sandbox->nameGroupsQueue.front) {
SandboxNameGroupNode *groupNode = (SandboxNameGroupNode *)ListEntry(node, SandboxMountNode, node);
if (groupNode->depNode) {
sandbox->depGroupNodes[sandbox->depNodeCount++] = groupNode;
}
node = node->next;
}
APPSPAWN_LOGI("LoadAppSandboxConfig depNodeCount %{public}d", sandbox->depNodeCount);
} while (0);
cJSON_Delete(config);
return ret;
}
@ -434,6 +454,7 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_JsonUtil_007, TestSize.Level0)
*/
HWTEST(AppSpawnSandboxTest, App_Spawn_Variable_001, TestSize.Level0)
{
AddDefaultVariable();
AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
ASSERT_EQ(context != nullptr, 1);
@ -1148,7 +1169,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_001, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1202,7 +1222,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_002, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1255,7 +1274,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_003, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1316,7 +1334,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_004, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1368,7 +1385,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_005, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1421,13 +1437,15 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_006, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
ret = TestParseAppSandboxConfig(sandbox, g_permissionConfig.c_str());
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
// set permission flags
int index = GetPermissionIndexInQueue(&sandbox->permissionQueue, "ohos.permission.FILE_ACCESS_MANAGER");
SetAppPermissionFlags(property, index);
// set check point
MountArg args = {};
@ -1442,7 +1460,7 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_006, TestSize.Level0)
stub->result = 0;
ret = MountSandboxConfigs(sandbox, property, 0);
ASSERT_EQ(ret, 0); // do not check result
ASSERT_EQ(stub->result != 0, 1);
ASSERT_EQ(stub->result, 0);
ret = 0;
} while (0);
if (sandbox) {
@ -1477,13 +1495,15 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_007, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
ret = TestParseAppSandboxConfig(sandbox, g_permissionConfig.c_str());
APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
// set permission flags
int index = GetPermissionIndexInQueue(&sandbox->permissionQueue, "ohos.permission.FILE_ACCESS_MANAGER");
SetAppPermissionFlags(property, index);
// set check point
MountArg args = {};
@ -1532,7 +1552,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_001, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1586,7 +1605,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_002, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1640,7 +1658,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_003, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1694,7 +1711,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_004, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1747,7 +1763,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_001, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1800,7 +1815,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_002, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1852,7 +1866,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_003, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1905,7 +1918,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_004, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;
@ -1958,7 +1970,6 @@ HWTEST(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_005, TestSize.Level0)
property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
AddDefaultVariable();
sandbox = CreateAppSpawnSandbox();
APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
sandbox->appFullMountEnable = 1;

View File

@ -0,0 +1,760 @@
/*
* Copyright (c) 2024-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 <cstring>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "appspawn.h"
#include "appspawn_hook.h"
#include "appspawn_manager.h"
#include "appspawn_modulemgr.h"
#include "appspawn_permission.h"
#include "appspawn_sandbox.h"
#include "appspawn_utils.h"
#include "securec.h"
#include "app_spawn_stub.h"
#include "app_spawn_test_helper.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS {
class AppSpawnSandboxMgrTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void SetUp() {}
void TearDown() {}
};
/**
* @brief AppSpawnSandboxCfg
*
*/
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_AppSpawnSandboxCfg_001, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = GetAppSpawnSandbox(mgr);
EXPECT_EQ(sandbox == nullptr, 1);
sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
sandbox = GetAppSpawnSandbox(mgr);
EXPECT_EQ(sandbox != nullptr, 1);
// dump
DumpAppSpawnSandboxCfg(sandbox);
// delete
DeleteAppSpawnSandbox(sandbox);
// get none
sandbox = GetAppSpawnSandbox(mgr);
EXPECT_EQ(sandbox == nullptr, 1);
DumpAppSpawnSandboxCfg(sandbox);
DeleteAppSpawnMgr(mgr);
sandbox = GetAppSpawnSandbox(nullptr);
EXPECT_EQ(sandbox == nullptr, 1);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_AppSpawnSandboxCfg_002, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
// for appspawn
int ret = LoadAppSandboxConfig(sandbox, 0);
EXPECT_EQ(ret, 0);
ret = LoadAppSandboxConfig(sandbox, 0); // 重复load
EXPECT_EQ(ret, 0);
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
ret = LoadAppSandboxConfig(nullptr, 0);
EXPECT_NE(ret, 0);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_AppSpawnSandboxCfg_003, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
int ret = 0;
#ifdef APPSPAWN_SANDBOX_NEW
// for nwebspawn
ret = LoadAppSandboxConfig(sandbox, 1);
EXPECT_EQ(ret, 0);
ret = LoadAppSandboxConfig(sandbox, 1); // 重复load
EXPECT_EQ(ret, 0);
ret = LoadAppSandboxConfig(sandbox, 2); // 重复load
EXPECT_EQ(ret, 0);
#else
// for nwebspawn
ret = LoadAppSandboxConfig(sandbox, 0);
EXPECT_EQ(ret, 0);
ret = LoadAppSandboxConfig(sandbox, 0); // 重复load
EXPECT_EQ(ret, 0);
#endif
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
ret = LoadAppSandboxConfig(nullptr, 1);
EXPECT_NE(ret, 0);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_SandboxSection_001, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
const uint32_t nameCount = 3;
const uint32_t lenCount = 7;
const char *inputName[nameCount] = {"test-001", nullptr, ""};
const uint32_t inputDataLen[lenCount] = {
0,
sizeof(SandboxSection),
sizeof(SandboxPackageNameNode),
sizeof(SandboxFlagsNode),
sizeof(SandboxNameGroupNode),
sizeof(SandboxPermissionNode),
sizeof(SandboxNameGroupNode) + 100
};
int result[lenCount] = {0};
result[1] = 1;
result[2] = 1;
result[3] = 1;
result[4] = 1;
result[5] = 1;
auto testFunc = [result](const char *name, uint32_t len, size_t nameIndex, size_t lenIndex) {
for (size_t type = SANDBOX_TAG_PERMISSION; type <= SANDBOX_TAG_REQUIRED; type++) {
SandboxSection *section = CreateSandboxSection(name, len, type);
EXPECT_EQ(section != nullptr, nameIndex == 0 && result[lenIndex]);
if (section) {
EXPECT_EQ(GetSectionType(section), type);
free(section->name);
free(section);
}
}
};
for (size_t i = 0; i < nameCount; i++) {
for (size_t j = 0; j < lenCount; j++) {
testFunc(inputName[i], inputDataLen[j], i, j);
}
}
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_SandboxSection_002, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
const uint32_t nameCount = 3;
const uint32_t lenCount = 7;
const char *inputName[nameCount] = {"test-001", nullptr, ""};
const uint32_t inputDataLen[lenCount] = {
0,
sizeof(SandboxSection),
sizeof(SandboxPackageNameNode),
sizeof(SandboxFlagsNode),
sizeof(SandboxNameGroupNode),
sizeof(SandboxPermissionNode),
sizeof(SandboxNameGroupNode) + 100
};
for (size_t i = 0; i < nameCount; i++) {
for (size_t j = 0; j < lenCount; j++) {
SandboxSection *section = CreateSandboxSection(inputName[i], inputDataLen[j], 0);
EXPECT_EQ(section == nullptr, 1);
section = CreateSandboxSection(inputName[i], inputDataLen[j], 1);
EXPECT_EQ(section == nullptr, 1);
section = CreateSandboxSection(inputName[i], inputDataLen[j], SANDBOX_TAG_INVALID);
EXPECT_EQ(section == nullptr, 1);
section = CreateSandboxSection(inputName[i], inputDataLen[j], SANDBOX_TAG_INVALID + 1);
EXPECT_EQ(section == nullptr, 1);
}
}
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_SandboxSection_003, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
SandboxSection *section = CreateSandboxSection("system-const", sizeof(SandboxSection), SANDBOX_TAG_SYSTEM_CONST);
EXPECT_EQ(section != nullptr, 1);
AddSandboxSection(section, &sandbox->requiredQueue);
// GetSandboxSection
section = GetSandboxSection(&sandbox->requiredQueue, "system-const");
EXPECT_EQ(section != nullptr, 1);
// DeleteSandboxSection
DeleteSandboxSection(section);
// GetSandboxSection has deleted
section = GetSandboxSection(&sandbox->requiredQueue, "system-const");
EXPECT_EQ(section != nullptr, 0);
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_SandboxSection_004, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
AddSandboxSection(nullptr, &sandbox->requiredQueue);
AddSandboxSection(nullptr, nullptr);
// GetSandboxSection
SandboxSection *section = GetSandboxSection(nullptr, "system-const");
EXPECT_EQ(section == nullptr, 1);
section = GetSandboxSection(nullptr, "");
EXPECT_EQ(section == nullptr, 1);
section = GetSandboxSection(nullptr, nullptr);
EXPECT_EQ(section == nullptr, 1);
section = GetSandboxSection(&sandbox->requiredQueue, "");
EXPECT_EQ(section == nullptr, 1);
section = GetSandboxSection(&sandbox->requiredQueue, nullptr);
EXPECT_EQ(section == nullptr, 1);
// DeleteSandboxSection
DeleteSandboxSection(section);
DeleteSandboxSection(nullptr);
// GetSandboxSection has deleted
section = GetSandboxSection(&sandbox->requiredQueue, "system-const");
EXPECT_EQ(section == nullptr, 1);
EXPECT_EQ(GetSectionType(nullptr), SANDBOX_TAG_INVALID);
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
/**
* @brief SandboxMountNode
*
*/
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_SandboxMountNode_001, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
SandboxSection *section = CreateSandboxSection("system-const", sizeof(SandboxSection), SANDBOX_TAG_SYSTEM_CONST);
EXPECT_EQ(section != nullptr, 1);
AddSandboxSection(section, &sandbox->requiredQueue);
const uint32_t lenCount = 4;
const uint32_t inputDataLen[lenCount] = {
0,
sizeof(PathMountNode),
sizeof(SymbolLinkNode),
sizeof(SymbolLinkNode) + 100
};
int result[lenCount] = {0, 1, 1, 0};
for (size_t i = 0; i < lenCount; i++) {
for (size_t j = 0; j < SANDBOX_TAG_INVALID; j++) {
SandboxMountNode *path = CreateSandboxMountNode(inputDataLen[i], j);
EXPECT_EQ(path != nullptr, result[i]);
if (path) {
free(path);
}
}
}
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_SandboxMountNode_002, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
SandboxSection *section = CreateSandboxSection("system-const", sizeof(SandboxSection), SANDBOX_TAG_SYSTEM_CONST);
EXPECT_EQ(section != nullptr, 1);
AddSandboxSection(section, &sandbox->requiredQueue);
SandboxMountNode *path = CreateSandboxMountNode(sizeof(PathMountNode), SANDBOX_TAG_MOUNT_PATH);
EXPECT_EQ(path != nullptr, 1);
AddSandboxMountNode(path, section);
path = GetFirstSandboxMountNode(section);
EXPECT_EQ(path != nullptr, 1);
DeleteSandboxMountNode(path);
path = GetFirstSandboxMountNode(section);
EXPECT_EQ(path == nullptr, 1);
path = GetFirstSandboxMountNode(nullptr);
EXPECT_EQ(path == nullptr, 1);
DeleteSandboxMountNode(nullptr);
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_SandboxMountNode_003, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
SandboxSection *section = CreateSandboxSection("system-const", sizeof(SandboxSection), SANDBOX_TAG_SYSTEM_CONST);
EXPECT_EQ(section != nullptr, 1);
AddSandboxSection(section, &sandbox->requiredQueue);
SandboxMountNode *path = CreateSandboxMountNode(sizeof(PathMountNode), SANDBOX_TAG_MOUNT_PATH);
EXPECT_EQ(path != nullptr, 1);
PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(path);
const char *testPath = "xxx/xxx/xxx";
pathNode->source = strdup(testPath);
pathNode->target = strdup(testPath);
AddSandboxMountNode(path, section);
pathNode = GetPathMountNode(section, SANDBOX_TAG_MOUNT_PATH, testPath, testPath);
EXPECT_EQ(pathNode != nullptr, 1);
// 异常
for (size_t j = 0; j < SANDBOX_TAG_INVALID; j++) {
pathNode = GetPathMountNode(section, j, testPath, testPath);
EXPECT_EQ(pathNode != nullptr, j == SANDBOX_TAG_MOUNT_PATH);
pathNode = GetPathMountNode(section, j, nullptr, testPath);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetPathMountNode(section, j, nullptr, nullptr);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetPathMountNode(section, j, testPath, nullptr);
EXPECT_EQ(pathNode != nullptr, 0);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetPathMountNode(nullptr, j, nullptr, testPath);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetPathMountNode(nullptr, j, nullptr, nullptr);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetPathMountNode(nullptr, j, testPath, nullptr);
EXPECT_EQ(pathNode != nullptr, 0);
}
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_SandboxMountNode_004, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
SandboxSection *section = CreateSandboxSection("system-const", sizeof(SandboxSection), SANDBOX_TAG_SYSTEM_CONST);
EXPECT_EQ(section != nullptr, 1);
AddSandboxSection(section, &sandbox->requiredQueue);
SandboxMountNode *path = CreateSandboxMountNode(sizeof(SymbolLinkNode), SANDBOX_TAG_SYMLINK);
EXPECT_EQ(path != nullptr, 1);
SymbolLinkNode *pathNode = reinterpret_cast<SymbolLinkNode *>(path);
const char *testPath = "xxx/xxx/xxx";
pathNode->linkName = strdup(testPath);
pathNode->target = strdup(testPath);
AddSandboxMountNode(path, section);
pathNode = GetSymbolLinkNode(section, testPath, testPath);
EXPECT_EQ(pathNode != nullptr, 1);
// 异常
pathNode = GetSymbolLinkNode(section, testPath, testPath);
EXPECT_EQ(pathNode != nullptr, 1);
pathNode = GetSymbolLinkNode(section, nullptr, testPath);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetSymbolLinkNode(section, nullptr, nullptr);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetSymbolLinkNode(section, testPath, nullptr);
EXPECT_EQ(pathNode != nullptr, 0);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetSymbolLinkNode(nullptr, nullptr, testPath);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetSymbolLinkNode(nullptr, nullptr, nullptr);
EXPECT_EQ(pathNode != nullptr, 0);
pathNode = GetSymbolLinkNode(nullptr, testPath, nullptr);
EXPECT_EQ(pathNode != nullptr, 0);
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
/**
* @brief mount
*
*/
static AppSpawningCtx *TestCreateAppSpawningCtx()
{
// get from buffer
AppSpawnTestHelper testHelper;
std::vector<uint8_t> buffer(1024 * 2); // 1024 * 2 max buffer
uint32_t msgLen = 0;
int ret = testHelper.CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
EXPECT_EQ(0, ret);
AppSpawnMsgNode *outMsg = nullptr;
uint32_t msgRecvLen = 0;
uint32_t reminder = 0;
ret = GetAppSpawnMsgFromBuffer(buffer.data(), msgLen, &outMsg, &msgRecvLen, &reminder);
EXPECT_EQ(0, ret);
EXPECT_EQ(msgLen, msgRecvLen);
EXPECT_EQ(memcmp(buffer.data() + sizeof(AppSpawnMsg), outMsg->buffer, msgLen - sizeof(AppSpawnMsg)), 0);
EXPECT_EQ(0, reminder);
ret = DecodeAppSpawnMsg(outMsg);
EXPECT_EQ(0, ret);
AppSpawningCtx *appCtx = CreateAppSpawningCtx();
EXPECT_EQ(appCtx != nullptr, 1);
appCtx->message = outMsg;
return appCtx;
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_Mount_001, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
AppSpawningCtx *property = TestCreateAppSpawningCtx();
EXPECT_EQ(property != nullptr, 1);
// 只做异常测试,正常流程需要基于业务流进行测试
const AppSpawningCtx *inputAppSpawningCtx[2] = {property, nullptr};
const AppSpawnSandboxCfg *inputAppSpawnSandboxCfg[2] = {sandbox, nullptr};
uint32_t inputSpawn[3] = {0, 1, 2};
int ret = 0;
for (uint32_t i = 0; i < 2; i++) { // 2
for (uint32_t j = 0; j < 2; j++) { // 2
for (uint32_t k = 0; k < 2; k++) { // 2
ret = MountSandboxConfigs(inputAppSpawnSandboxCfg[i], inputAppSpawningCtx[j], inputSpawn[k]);
EXPECT_EQ(ret == 0, i == 0 && j == 0);
}
}
}
for (uint32_t i = 0; i < 2; i++) { // 2
for (uint32_t j = 0; j < 2; j++) { // 2
for (uint32_t k = 0; k < 2; k++) { // 2
ret = StagedMountSystemConst(inputAppSpawnSandboxCfg[i], inputAppSpawningCtx[j], inputSpawn[k]);
EXPECT_EQ(ret == 0, i == 0 && j == 0);
}
}
}
DeleteAppSpawningCtx(property);
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_Mount_002, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
SandboxContext context = {};
// 只做异常测试,正常流程需要基于业务流进行测试
const SandboxContext *inputContext[2] = {&context, nullptr};
const AppSpawnSandboxCfg *inputAppSpawnSandboxCfg[2] = {sandbox, nullptr};
int ret = 0;
for (uint32_t i = 0; i < 2; i++) { // 2
for (uint32_t j = 0; j < 2; j++) { // 2
ret = StagedMountPreUnShare(inputContext[i], inputAppSpawnSandboxCfg[j]);
EXPECT_EQ(ret == 0, i == 0 && j == 0);
}
}
for (uint32_t i = 0; i < 2; i++) { // 2
for (uint32_t j = 0; j < 2; j++) { // 2
ret = StagedMountPostUnshare(inputContext[i], inputAppSpawnSandboxCfg[j]);
EXPECT_EQ(ret == 0, i == 0 && j == 0);
}
}
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_Mount_003, TestSize.Level0)
{
AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_NWEB_SPAWN);
EXPECT_EQ(mgr != nullptr, 1);
AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
EXPECT_EQ(sandbox != nullptr, 1);
sandbox->rootPath = strdup("/data/appspawn_ut/sandbox/");
OH_ListAddTail(&sandbox->extData.node, &mgr->extData);
// 只做异常测试,正常流程需要基于业务流进行测试
const AppSpawnSandboxCfg *inputAppSpawnSandboxCfg[2] = {sandbox, nullptr};
const char *inputName[2] = {"test", nullptr};
for (uint32_t k = 0; k < 2; k++) { // 2
int ret = UnmountDepPaths(inputAppSpawnSandboxCfg[k], 0);
EXPECT_EQ(ret == 0, k == 0);
}
for (uint32_t i = 0; i < 2; i++) { // 2
for (uint32_t k = 0; k < 2; k++) { // 2
int ret = UnmountSandboxConfigs(inputAppSpawnSandboxCfg[k], 0, inputName[i]);
EXPECT_EQ(ret == 0, k == 0 && i == 0);
}
}
DeleteAppSpawnSandbox(sandbox);
DeleteAppSpawnMgr(mgr);
}
/**
* @brief SandboxMountPath
*
*/
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_Add_App_SandboxMountPath_001, TestSize.Level0)
{
MountArg arg = {};
arg.originPath = "/data/";
arg.destinationPath = "/data/appspawn/test";
arg.mountSharedFlag = 1;
int ret = SandboxMountPath(&arg);
EXPECT_EQ(ret, 0);
ret = SandboxMountPath(&arg);
EXPECT_EQ(ret, 0);
ret = SandboxMountPath(nullptr);
EXPECT_NE(ret, 0);
arg.mountSharedFlag = -1;
ret = SandboxMountPath(&arg);
EXPECT_EQ(ret, 0);
}
/**
* @brief AddVariableReplaceHandler
*
*/
static int TestReplaceVarHandler(const SandboxContext *context,
const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData)
{
return 0;
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_AddVariableReplaceHandler_001, TestSize.Level0)
{
int ret = AddVariableReplaceHandler(nullptr, nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = AddVariableReplaceHandler("xxx", nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = AddVariableReplaceHandler(nullptr, TestReplaceVarHandler);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = AddVariableReplaceHandler("global", TestReplaceVarHandler);
EXPECT_EQ(ret, 0);
ret = AddVariableReplaceHandler("global", TestReplaceVarHandler);
EXPECT_EQ(ret, APPSPAWN_NODE_EXIST);
ret = AddVariableReplaceHandler("<Test-Var-005>", TestReplaceVarHandler);
EXPECT_EQ(ret, 0);
}
/**
* @brief RegisterExpandSandboxCfgHandler
*
*/
static int TestProcessExpandSandboxCfg(const SandboxContext *context,
const AppSpawnSandboxCfg *appSandBox, const char *name)
{
return 0;
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_RegisterExpandSandboxCfgHandler_001, TestSize.Level0)
{
int ret = RegisterExpandSandboxCfgHandler(nullptr, 0, nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = RegisterExpandSandboxCfgHandler("test", 0, nullptr);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = RegisterExpandSandboxCfgHandler(nullptr, 0, TestProcessExpandSandboxCfg);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
ret = RegisterExpandSandboxCfgHandler("test-001", 0, TestProcessExpandSandboxCfg);
EXPECT_EQ(ret, 0);
ret = RegisterExpandSandboxCfgHandler("test-001", 0, TestProcessExpandSandboxCfg);
EXPECT_EQ(ret, APPSPAWN_NODE_EXIST);
ret = RegisterExpandSandboxCfgHandler("test-001", -1, TestProcessExpandSandboxCfg);
EXPECT_EQ(ret, APPSPAWN_NODE_EXIST);
}
/**
* @brief permission test
*
*/
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_Permission_001, TestSize.Level0)
{
int ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_MAX);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max >= 0, 1);
DeletePermission(CLIENT_FOR_APPSPAWN);
DeletePermission(CLIENT_FOR_NWEBSPAWN);
DeletePermission(CLIENT_MAX);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_Permission_002, TestSize.Level0)
{
int ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max >= 0, 1);
AppSpawnClientHandle clientHandle;
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
max = GetMaxPermissionIndex(clientHandle);
int32_t index = GetPermissionIndex(clientHandle, "ohos.permission.ACCESS_BUNDLE_DIR");
EXPECT_EQ(index >= 0, 1);
EXPECT_EQ(index < max, 1);
const char *permission = GetPermissionByIndex(clientHandle, index);
EXPECT_EQ(permission != nullptr, 1);
EXPECT_EQ(strcmp(permission, "ohos.permission.ACCESS_BUNDLE_DIR") == 0, 1);
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_Permission_003, TestSize.Level0)
{
int ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_MAX);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max >= 0, 1);
AppSpawnClientHandle clientHandle;
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
#ifndef APPSPAWN_SANDBOX_NEW
max = GetMaxPermissionIndex(clientHandle);
int32_t index = GetPermissionIndex(clientHandle, "ohos.permission.ACCESS_BUNDLE_DIR");
EXPECT_EQ(index >= 0, 1);
EXPECT_EQ(max >= index, 1);
const char *permission = GetPermissionByIndex(clientHandle, index);
EXPECT_EQ(permission != nullptr, 1);
EXPECT_EQ(strcmp(permission, "ohos.permission.ACCESS_BUNDLE_DIR") == 0, 1);
#else
// nweb no permission, so max = 0
max = GetMaxPermissionIndex(clientHandle);
EXPECT_EQ(max, 0);
#endif
AppSpawnClientDestroy(clientHandle);
}
HWTEST(AppSpawnSandboxMgrTest, App_Spawn_Permission_004, TestSize.Level0)
{
int ret = LoadPermission(CLIENT_FOR_APPSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_FOR_NWEBSPAWN);
EXPECT_EQ(ret, 0);
ret = LoadPermission(CLIENT_MAX);
EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
int32_t max = GetPermissionMaxCount();
EXPECT_EQ(max >= 0, 1);
AppSpawnClientHandle clientHandle;
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
EXPECT_EQ(ret, 0);
max = GetMaxPermissionIndex(nullptr);
EXPECT_EQ(max >= 0, 1);
int32_t index = GetPermissionIndex(clientHandle, nullptr);
EXPECT_EQ(index, INVALID_PERMISSION_INDEX);
index = GetPermissionIndex(nullptr, "ohos.permission.ACCESS_BUNDLE_DIR");
EXPECT_EQ(max >= index, 1);
const char *permission = GetPermissionByIndex(clientHandle, INVALID_PERMISSION_INDEX);
EXPECT_EQ(permission == nullptr, 1);
AppSpawnClientDestroy(clientHandle);
}
} // namespace OHOS

View File

@ -17,6 +17,7 @@
#include <cstring>
#include <memory>
#include <string>
#include <thread>
#include <unistd.h>
#include <gtest/gtest.h>
@ -76,12 +77,13 @@ HWTEST(AppSpawnServiceTest, App_Spawn_001, TestSize.Level0)
APPSPAWN_LOGV("App_Spawn_001 recv result %{public}d %{public}d", result.result, result.pid);
APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret);
if (ret == 0 && result.pid > 0) {
APPSPAWN_LOGI("App_Spawn_Msg_001 Kill pid %{public}d ", result.pid);
APPSPAWN_LOGV("App_Spawn_001 Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
}
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
@ -105,6 +107,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_002, TestSize.Level0)
break;
}
// stop child and termination
APPSPAWN_LOGI("App_Spawn_002 Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
// MSG_GET_RENDER_TERMINATION_STATUS
ret = AppSpawnTerminateMsgCreate(result.pid, &reqHandle);
@ -114,6 +117,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_002, TestSize.Level0)
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
@ -143,6 +147,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_003, TestSize.Level0)
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
@ -163,6 +168,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_004, TestSize.Level0)
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
@ -187,12 +193,57 @@ HWTEST(AppSpawnServiceTest, App_Spawn_005, TestSize.Level0)
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret);
if (ret == 0 && result.pid > 0) {
APPSPAWN_LOGI("App_Spawn_Msg_001 Kill pid %{public}d ", result.pid);
APPSPAWN_LOGI("App_Spawn_005 Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
}
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
* @brief 线
*
*/
HWTEST(AppSpawnServiceTest, App_Spawn_006, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
do {
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", APPSPAWN_SERVER_NAME);
auto sendMsg = [](AppSpawnClientHandle clientHandle) {
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnResult result = {};
int ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_CHECK(ret == 0, return, "Failed to send msg %{public}d", ret);
if (ret == 0 && result.pid > 0) {
printf("App_Spawn_006 Kill pid %d \n", result.pid);
kill(result.pid, SIGKILL);
}
ASSERT_EQ(ret, 0);
};
std::thread thread1(sendMsg, clientHandle);
std::thread thread2(sendMsg, clientHandle);
std::thread thread3(sendMsg, clientHandle);
std::thread thread4(sendMsg, clientHandle);
std::thread thread5(sendMsg, clientHandle);
thread1.join();
thread2.join();
thread3.join();
thread4.join();
thread5.join();
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(AppSpawnServiceTest, App_Spawn_Msg_001, TestSize.Level0)
@ -232,33 +283,41 @@ static int RecvMsg(int socketId, uint8_t *buffer, uint32_t buffSize)
return static_cast<int>(rLen);
}
/**
* @brief
*
*/
HWTEST(AppSpawnServiceTest, App_Spawn_Msg_002, TestSize.Level0)
{
int ret = -1;
int ret = 0;
int socketId = -1;
do {
socketId = g_testServer->CreateSocket();
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", APPSPAWN_SERVER_NAME);
// 消息不完整,断开连接
std::vector<uint8_t> buffer(sizeof(AppSpawnResponseMsg));
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
ret = -1;
int len = write(socketId, buffer.data(), msgLen - 10); // 10
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// recv timeout
len = RecvMsg(socketId, buffer.data(), buffer.size());
APPSPAWN_CHECK(len == 0, ret = -1;
break, "Failed to recv msg len: %{public}d", len);
APPSPAWN_CHECK(len <= 0, break, "Failed to recv msg len: %{public}d", len);
ret = 0;
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_NE(ret, 0);
ASSERT_EQ(ret, 0);
}
/**
* @brief tlv
*
*/
HWTEST(AppSpawnServiceTest, App_Spawn_Msg_003, TestSize.Level0)
{
int ret = -1;
@ -267,7 +326,6 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_003, TestSize.Level0)
socketId = g_testServer->CreateSocket();
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", APPSPAWN_SERVER_NAME);
// 测试异常tlv
std::vector<uint8_t> buffer(sizeof(AppSpawnResponseMsg));
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {});
@ -281,12 +339,12 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_003, TestSize.Level0)
break, "Failed to recv msg %{public}s", APPSPAWN_SERVER_NAME);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
APPSPAWN_LOGV("Recv msg %{public}s result: %{public}d", respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result;
ret = respMsg->result.result == APPSPAWN_MSG_INVALID ? 0 : -1;
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_NE(ret, 0);
ASSERT_EQ(ret, 0);
}
/**
@ -301,7 +359,6 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_004, TestSize.Level0)
socketId = g_testServer->CreateSocket();
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", APPSPAWN_SERVER_NAME);
// 测试小包发送
std::vector<uint8_t> buffer(1024, 0); // 1024 1k
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
@ -310,7 +367,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_004, TestSize.Level0)
// 分片发送
uint32_t sendStep = OHOS::AppSpawnTestHelper::GenRandom() % 70; // 70 一次发送的字节数
sendStep = (sendStep < 20) ? 33 : sendStep; // 20 33 一次发送的字节数
APPSPAWN_LOGV("App_Spawn_Msg_005 msgLen %{public}u sendStep: %{public}u", msgLen, sendStep);
APPSPAWN_LOGV("App_Spawn_Msg_004 msgLen %{public}u sendStep: %{public}u", msgLen, sendStep);
uint32_t currIndex = 0;
int len = 0;
do {
@ -333,13 +390,14 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_004, TestSize.Level0)
APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
break, "Failed to recv msg %{public}s", APPSPAWN_SERVER_NAME);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
APPSPAWN_LOGV("App_Spawn_Msg_005 recv msg %{public}s result: %{public}d",
APPSPAWN_LOGV("App_Spawn_Msg_004 recv msg %{public}s result: %{public}d",
respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result;
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
@ -371,7 +429,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_005, TestSize.Level0)
APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
break, "Failed to recv msg %{public}s", APPSPAWN_SERVER_NAME);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer2.data());
APPSPAWN_LOGV("App_Spawn_Msg_006 Recv msg %{public}s result: %{public}d",
APPSPAWN_LOGV("App_Spawn_Msg_005 Recv msg %{public}s result: %{public}d",
respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result;
(void)RecvMsg(socketId, buffer2.data(), buffer2.size());
@ -379,6 +437,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_005, TestSize.Level0)
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
@ -393,7 +452,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_006, TestSize.Level0)
socketId = g_testServer->CreateSocket();
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", APPSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer1(1024); // 1024
std::vector<uint8_t> buffer1(2 * 1024); // 2 * 1024
std::vector<uint8_t> buffer2(1024); // 1024
uint32_t msgLen1 = 0;
uint32_t msgLen2 = 0;
@ -410,7 +469,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_006, TestSize.Level0)
APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
break, "Failed to recv msg %{public}s", APPSPAWN_SERVER_NAME);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer2.data());
APPSPAWN_LOGV("App_Spawn_Msg_007 recv msg %{public}s result: %{public}d",
APPSPAWN_LOGV("App_Spawn_Msg_006 recv msg %{public}s result: %{public}d",
respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result;
(void)RecvMsg(socketId, buffer2.data(), buffer2.size());
@ -418,6 +477,7 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_006, TestSize.Level0)
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
@ -442,12 +502,10 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_007, TestSize.Level0)
APPSPAWN_LOGV("CloseClientSocket");
CloseClientSocket(socketId);
socketId = -1;
usleep(20000); // 20000 20ms wait conn close
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
@ -468,7 +526,6 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_008, TestSize.Level0)
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
int len = write(socketId, buffer.data(), msgLen - 20); // 20 test
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
usleep(500000); // 500000 need to wait server timeout
// recv
len = read(socketId, buffer.data(), buffer.size()); // timeout EAGAIN
APPSPAWN_CHECK(len <= 0, ret = -1; break, "Can not receive timeout %{public}d", errno);
@ -476,7 +533,6 @@ HWTEST(AppSpawnServiceTest, App_Spawn_Msg_008, TestSize.Level0)
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
@ -489,7 +545,6 @@ HWTEST(AppSpawnServiceTest, App_Spawn_NWebSpawn_001, TestSize.Level0)
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
do {
usleep(2000);
ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", APPSPAWN_SERVER_NAME);
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
@ -501,6 +556,8 @@ HWTEST(AppSpawnServiceTest, App_Spawn_NWebSpawn_001, TestSize.Level0)
g_testServer->KillNWebSpawnServer();
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
g_testServer->Stop();
delete g_testServer;
}

View File

@ -23,7 +23,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "appspawn_client.h"
#include "appspawn_manager.h"
#include "appspawn_modulemgr.h"
#include "appspawn_server.h"
@ -40,7 +39,6 @@ using namespace OHOS;
namespace OHOS {
static OHOS::AppSpawnTestServer *g_testServer = nullptr;
using AddTlvFunction = std::function<int(uint8_t *buffer, uint32_t bufferLen, uint32_t &realLen, uint32_t &tlvCount)>;
class NWebSpawnServiceTest : public testing::Test {
public:
static void SetUpTestCase() {}
@ -50,7 +48,7 @@ public:
};
/**
* @brief nwebspawn
* @brief
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_001, TestSize.Level0)
@ -58,66 +56,103 @@ HWTEST(NWebSpawnServiceTest, NWeb_Spawn_001, TestSize.Level0)
g_testServer = new OHOS::AppSpawnTestServer("appspawn -mode nwebspawn");
ASSERT_EQ(g_testServer != nullptr, 1);
g_testServer->Start(nullptr);
usleep(2000); // 2000
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
do {
APPSPAWN_LOGV("NWeb_Spawn_001 start");
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NO_SANDBOX);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("NWeb_Spawn_001 recv result %{public}d", ret);
if (ret != 0 || result.pid == 0) {
ret = -1;
break;
APPSPAWN_LOGV("NWeb_Spawn_001 recv result %{public}d %{public}d", result.result, result.pid);
APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret);
if (ret == 0 && result.pid > 0) {
APPSPAWN_LOGV("NWeb_Spawn_001 Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
}
// stop child and termination
APPSPAWN_LOGI("NWeb_Spawn_001 Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
usleep(2000); // waitto get exit status
// MSG_GET_RENDER_TERMINATION_STATUS
AppSpawnReqMsgHandle reqHandle2 = nullptr;
ret = AppSpawnTerminateMsgCreate(result.pid, &reqHandle2);
APPSPAWN_CHECK(ret == 0, break, "Failed to create req %{public}s", NWEBSPAWN_SERVER_NAME);
ret = AppSpawnClientSendMsg(clientHandle, reqHandle2, &result);
APPSPAWN_LOGV("Send MSG_GET_RENDER_TERMINATION_STATUS %{public}d", ret);
ret = 0;
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
* @brief 退MSG_GET_RENDER_TERMINATION_STATUS
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_002, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
do {
// 发送消息到nweb spawn
APPSPAWN_LOGV("NWeb_Spawn_002 start");
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("NWeb_Spawn_002 recv result %{public}d %{public}d", ret, result.pid);
APPSPAWN_LOGV("NWeb_Spawn_002 recv result %{public}d %{public}d", result.result, result.pid);
if (ret != 0 || result.pid == 0) {
ret = -1;
break;
}
// stop child and termination
APPSPAWN_LOGV("NWeb_Spawn_002 kill pid %{public}d", result.pid);
kill(result.pid, SIGKILL);
// MSG_GET_RENDER_TERMINATION_STATUS
ret = AppSpawnTerminateMsgCreate(result.pid, &reqHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create termination msg %{public}s", NWEBSPAWN_SERVER_NAME);
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("Send MSG_GET_RENDER_TERMINATION_STATUS %{public}d", ret);
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
* @brief MSG_GET_RENDER_TERMINATION_STATUS
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_003, TestSize.Level0)
{
int ret = 0;
AppSpawnClientHandle clientHandle = nullptr;
do {
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("NWeb_Spawn_003 recv result %{public}d %{public}d", result.result, result.pid);
if (ret != 0 || result.pid == 0) {
ret = -1;
break;
}
// MSG_GET_RENDER_TERMINATION_STATUS
AppSpawnReqMsgHandle reqHandle2 = nullptr;
ret = AppSpawnTerminateMsgCreate(result.pid, &reqHandle2);
APPSPAWN_CHECK(ret == 0, break, "Failed to set pid %{public}s", NWEBSPAWN_SERVER_NAME);
ret = AppSpawnClientSendMsg(clientHandle, reqHandle2, &result);
ret = AppSpawnTerminateMsgCreate(result.pid, &reqHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create termination msg %{public}s", NWEBSPAWN_SERVER_NAME);
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("Send MSG_GET_RENDER_TERMINATION_STATUS %{public}d", ret);
ret = 0; // ut can not get result
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
* @brief dump
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_004, TestSize.Level0)
{
int ret = 0;
@ -125,16 +160,20 @@ HWTEST(NWebSpawnServiceTest, NWeb_Spawn_004, TestSize.Level0)
do {
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
// MSG_DUMP
AppSpawnResult result = {};
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_DUMP, 0);
AppSpawnResult result = {};
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("Send MSG_DUMP %{public}d", ret);
APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret);
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
/**
* @brief MSG_SPAWN_NATIVE_PROCESS
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_005, TestSize.Level0)
{
int ret = 0;
@ -142,15 +181,315 @@ HWTEST(NWebSpawnServiceTest, NWeb_Spawn_005, TestSize.Level0)
do {
ret = AppSpawnClientInit(NWEBSPAWN_SERVER_NAME, &clientHandle);
APPSPAWN_CHECK(ret == 0, break, "Failed to create client %{public}s", NWEBSPAWN_SERVER_NAME);
// MAX_TYPE_INVALID
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_DEBUGGABLE);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_NATIVEDEBUG);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_BUNDLE_RESOURCES);
AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ACCESS_BUNDLE_DIR);
AppSpawnResult result = {};
AppSpawnReqMsgHandle reqHandle = g_testServer->CreateMsg(clientHandle, MAX_TYPE_INVALID, 0);
ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
APPSPAWN_LOGV("Send MAX_TYPE_INVALID %{public}d", ret);
APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret);
if (ret == 0 && result.pid > 0) {
APPSPAWN_LOGI("NWeb_Spawn_Msg_001 Kill pid %{public}d ", result.pid);
kill(result.pid, SIGKILL);
}
} while (0);
AppSpawnClientDestroy(clientHandle);
ASSERT_EQ(ret, 0);
}
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_Msg_001, TestSize.Level0)
{
int ret = -1;
int socketId = -1;
// 没有tlv的消息返回错误
do {
socketId = g_testServer->CreateSocket(1);
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer(sizeof(AppSpawnResponseMsg));
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
int len = write(socketId, buffer.data(), msgLen);
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// recv
APPSPAWN_LOGV("Start recv ... ");
len = read(socketId, buffer.data(), buffer.size());
APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
break, "Failed to recv msg errno: %{public}d", errno);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
APPSPAWN_LOGV("Recv msg %{public}s result: %{public}d", respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result;
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_NE(ret, 0);
}
static int RecvMsg(int socketId, uint8_t *buffer, uint32_t buffSize)
{
ssize_t rLen = TEMP_FAILURE_RETRY(read(socketId, buffer, buffSize));
return static_cast<int>(rLen);
}
/**
* @brief
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_Msg_002, TestSize.Level0)
{
int ret = 0;
int socketId = -1;
do {
socketId = g_testServer->CreateSocket(1);
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer(sizeof(AppSpawnResponseMsg));
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
ret = -1;
int len = write(socketId, buffer.data(), msgLen - 10); // 10
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// recv timeout
len = RecvMsg(socketId, buffer.data(), buffer.size());
APPSPAWN_CHECK(len <= 0, break, "Failed to recv msg len: %{public}d", len);
ret = 0;
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
* @brief tlv
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_Msg_003, TestSize.Level0)
{
int ret = -1;
int socketId = -1;
do {
socketId = g_testServer->CreateSocket(1);
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer(sizeof(AppSpawnResponseMsg));
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
int len = write(socketId, buffer.data(), msgLen);
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// recv
len = RecvMsg(socketId, buffer.data(), buffer.size());
APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
APPSPAWN_LOGV("Recv msg %{public}s result: %{public}d", respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result == APPSPAWN_MSG_INVALID ? 0 : -1;
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
* @brief 2033
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_Msg_004, TestSize.Level0)
{
int ret = -1;
int socketId = -1;
do {
socketId = g_testServer->CreateSocket(1);
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer(1024, 0); // 1024 1k
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// 分片发送
uint32_t sendStep = OHOS::AppSpawnTestHelper::GenRandom() % 70; // 70 一次发送的字节数
sendStep = (sendStep < 20) ? 33 : sendStep; // 20 33 一次发送的字节数
APPSPAWN_LOGV("NWeb_Spawn_Msg_005 msgLen %{public}u sendStep: %{public}u", msgLen, sendStep);
uint32_t currIndex = 0;
int len = 0;
do {
if ((currIndex + sendStep) > msgLen) {
break;
}
len = write(socketId, buffer.data() + currIndex, sendStep);
APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", g_testServer->GetDefaultTestAppBundleName());
usleep(2000); // wait recv
currIndex += sendStep;
} while (1);
APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", g_testServer->GetDefaultTestAppBundleName());
if (msgLen > currIndex) {
len = write(socketId, buffer.data() + currIndex, msgLen - currIndex);
APPSPAWN_CHECK(len > 0, break, "Failed to send %{public}s", g_testServer->GetDefaultTestAppBundleName());
}
// recv
len = RecvMsg(socketId, buffer.data(), buffer.size());
APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer.data());
APPSPAWN_LOGV("NWeb_Spawn_Msg_005 recv msg %{public}s result: %{public}d",
respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result;
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
* @brief 2
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_Msg_005, TestSize.Level0)
{
int ret = -1;
int socketId = -1;
do {
socketId = g_testServer->CreateSocket(1);
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer1(1024); // 1024
std::vector<uint8_t> buffer2(1024); // 1024
uint32_t msgLen1 = 0;
uint32_t msgLen2 = 0;
ret = g_testServer->CreateSendMsg(buffer1, MSG_APP_SPAWN, msgLen1, {AppSpawnTestHelper::AddBaseTlv});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
ret = g_testServer->CreateSendMsg(buffer2, MSG_APP_SPAWN, msgLen2, {AppSpawnTestHelper::AddBaseTlv});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
buffer1.insert(buffer1.begin() + msgLen1, buffer2.begin(), buffer2.end());
int len = write(socketId, buffer1.data(), msgLen1 + msgLen2);
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// recv
len = RecvMsg(socketId, buffer2.data(), buffer2.size());
APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer2.data());
APPSPAWN_LOGV("NWeb_Spawn_Msg_005 Recv msg %{public}s result: %{public}d",
respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result;
(void)RecvMsg(socketId, buffer2.data(), buffer2.size());
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
* @brief 2spawn和dump
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_Msg_006, TestSize.Level0)
{
int ret = -1;
int socketId = -1;
do {
socketId = g_testServer->CreateSocket(1);
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer1(2 * 1024); // 2 * 1024
std::vector<uint8_t> buffer2(1024); // 1024
uint32_t msgLen1 = 0;
uint32_t msgLen2 = 0;
ret = g_testServer->CreateSendMsg(buffer1, MSG_APP_SPAWN, msgLen1, {AppSpawnTestHelper::AddBaseTlv});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
ret = g_testServer->CreateSendMsg(buffer2, MSG_DUMP, msgLen2, {});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
buffer1.insert(buffer1.begin() + msgLen1, buffer2.begin(), buffer2.end());
int len = write(socketId, buffer1.data(), msgLen1 + msgLen2);
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// recv
len = RecvMsg(socketId, buffer2.data(), buffer2.size());
APPSPAWN_CHECK(len >= static_cast<int>(sizeof(AppSpawnResponseMsg)), ret = -1;
break, "Failed to recv msg %{public}s", NWEBSPAWN_SERVER_NAME);
AppSpawnResponseMsg *respMsg = reinterpret_cast<AppSpawnResponseMsg *>(buffer2.data());
APPSPAWN_LOGV("NWeb_Spawn_Msg_006 recv msg %{public}s result: %{public}d",
respMsg->msgHdr.processName, respMsg->result.result);
ret = respMsg->result.result;
(void)RecvMsg(socketId, buffer2.data(), buffer2.size());
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
* @brief
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_Msg_007, TestSize.Level0)
{
int ret = -1;
int socketId = -1;
do {
socketId = g_testServer->CreateSocket(1);
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer(1024, 0); // 1024 1k
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
int len = write(socketId, buffer.data(), msgLen);
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// close socket
APPSPAWN_LOGV("CloseClientSocket");
CloseClientSocket(socketId);
socketId = -1;
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
}
/**
* @brief
*
*/
HWTEST(NWebSpawnServiceTest, NWeb_Spawn_Msg_008, TestSize.Level0)
{
int ret = 0;
int socketId = -1;
do {
socketId = g_testServer->CreateSocket(1);
APPSPAWN_CHECK(socketId >= 0, break, "Failed to create socket %{public}s", NWEBSPAWN_SERVER_NAME);
std::vector<uint8_t> buffer(1024, 0); // 1024 1k
uint32_t msgLen = 0;
ret = g_testServer->CreateSendMsg(buffer, MSG_APP_SPAWN, msgLen, {AppSpawnTestHelper::AddBaseTlv});
APPSPAWN_CHECK(ret == 0, break, "Failed to create msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
int len = write(socketId, buffer.data(), msgLen - 20); // 20 test
APPSPAWN_CHECK(len > 0, break, "Failed to send msg %{public}s", g_testServer->GetDefaultTestAppBundleName());
// recv
len = read(socketId, buffer.data(), buffer.size()); // timeout EAGAIN
APPSPAWN_CHECK(len <= 0, ret = -1; break, "Can not receive timeout %{public}d", errno);
} while (0);
if (socketId >= 0) {
CloseClientSocket(socketId);
}
ASSERT_EQ(ret, 0);
g_testServer->Stop();
delete g_testServer;
}

View File

@ -25,6 +25,7 @@
#include <sys/eventfd.h>
#include <sys/wait.h>
#include <unistd.h>
#include <inttypes.h>
#include "appspawn.h"
#include "appspawn_client.h"
@ -97,48 +98,48 @@ void AppSpawnTestServer::StartCheckHandler(void)
#endif
}
void *AppSpawnTestServer::ServiceThread(void *arg)
void AppSpawnTestServer::ServiceThread()
{
CmdArgs *args = nullptr;
pid_t pid = getpid();
AppSpawnTestServer *server = reinterpret_cast<AppSpawnTestServer *>(arg);
APPSPAWN_LOGV("serviceCmd_ %{public}s", server->serviceCmd_.c_str());
APPSPAWN_LOGV("AppSpawnTestServer::ServiceThread %{public}s", serviceCmd_.c_str());
running_ = true;
// 测试server时使用appspawn的server
if (server->testServer_) {
server->content_ = AppSpawnTestHelper::StartSpawnServer(server->serviceCmd_, args);
if (server->content_ == nullptr) {
return nullptr;
if (testServer_) {
content_ = AppSpawnTestHelper::StartSpawnServer(serviceCmd_, args);
if (content_ == nullptr) {
return;
}
if (pid == getpid()) { // 主进程进行处理
APPSPAWN_LOGV("Service start timer %{public}s ", server->serviceCmd_.c_str());
server->StartCheckHandler();
AppSpawnMgr *content = reinterpret_cast<AppSpawnMgr *>(server->content_);
APPSPAWN_CHECK_ONLY_EXPER(content != NULL, return nullptr);
APPSPAWN_LOGV("Service start timer %{public}s ", serviceCmd_.c_str());
StartCheckHandler();
AppSpawnMgr *content = reinterpret_cast<AppSpawnMgr *>(content_);
APPSPAWN_CHECK_ONLY_EXPER(content != NULL, return);
AppSpawnedProcess *info = GetSpawnedProcessByName(NWEBSPAWN_SERVER_NAME);
if (info != NULL) {
APPSPAWN_LOGV("Save nwebspawn pid: %{public}d %{public}d", info->pid, server->serverId_);
server->appPid_.store(info->pid);
APPSPAWN_LOGV("Save nwebspawn pid: %{public}d %{public}d", info->pid, serverId_);
appPid_.store(info->pid);
}
// register
RegChildLooper(&content->content, TestChildLoopRun);
}
server->content_->runAppSpawn(server->content_, args->argc, args->argv);
content_->runAppSpawn(content_, args->argc, args->argv);
if (pid != getpid()) { // 子进程退出
exit(0);
} else {
server->content_ = nullptr;
content_ = nullptr;
}
} else {
server->StartCheckHandler();
server->localServer_ = new LocalTestServer();
server->localServer_->Run(APPSPAWN_SOCKET_NAME, server->recvMsgProcess_);
StartCheckHandler();
localServer_ = new LocalTestServer();
localServer_->Run(APPSPAWN_SOCKET_NAME, recvMsgProcess_);
}
APPSPAWN_LOGV("Service thread finish %{public}s ", server->serviceCmd_.c_str());
APPSPAWN_LOGV("AppSpawnTestServer::ServiceThread finish %{public}s ", serviceCmd_.c_str());
if (args) {
free(args);
}
return nullptr;
return;
}
void AppSpawnTestServer::Start(void)
@ -146,22 +147,49 @@ void AppSpawnTestServer::Start(void)
Start(nullptr);
}
static void *ServiceHelperThread(void *arg)
{
AppSpawnTestServer *server = reinterpret_cast<AppSpawnTestServer *>(arg);
APPSPAWN_LOGV("AppSpawnTestServer::thread ");
server->ServiceThread();
return nullptr;
}
void AppSpawnTestServer::Start(RecvMsgProcess process, uint32_t time)
{
APPSPAWN_LOGV("AppSpawnTestServer::Start serverId %{public}u", AppSpawnTestServer::serverId);
protectTime_ = time;
if (threadId_ == 0) {
clock_gettime(CLOCK_MONOTONIC, &startTime_);
recvMsgProcess_ = process;
int ret = pthread_create(&threadId_, nullptr, ServiceThread, static_cast<void *>(this));
if (ret != 0) {
return;
}
uint32_t retry = 0;
if (threadId_ != 0) {
return;
}
clock_gettime(CLOCK_MONOTONIC, &startTime_);
recvMsgProcess_ = process;
errno = 0;
int ret = 0;
do {
threadId_ = 0;
ret = pthread_create(&threadId_, nullptr, ServiceHelperThread, static_cast<void *>(this));
if (ret == 0) {
break;
}
APPSPAWN_LOGE("AppSpawnTestServer::Start create thread fail %{public}d %{public}d", ret, errno);
usleep(20000); // 20000 20ms
retry++;
} while (ret == EAGAIN && retry < 10); // 10 max retry
// wait server thread run
retry = 0;
while (!running_ && (retry < 10)) { // 10 max retry
usleep(20000); // 20000 20ms
retry++;
}
APPSPAWN_LOGV("AppSpawnTestServer::Start retry %{public}u", retry);
}
void AppSpawnTestServer::Stop()
{
APPSPAWN_LOGV("AppSpawnTestServer::Stop");
APPSPAWN_LOGV("AppSpawnTestServer::Stop serverId %{public}u", AppSpawnTestServer::serverId);
if (threadId_ != 0) {
stop_ = true;
pthread_join(threadId_, nullptr);
@ -212,7 +240,7 @@ void AppSpawnTestServer::ProcessIdle(const IdleHandle taskHandle, void *context)
clock_gettime(CLOCK_MONOTONIC, &end);
uint64_t diff = DiffTime(&server->startTime_, &end);
if (diff >= (server->protectTime_ * 1000)) { // 1000 ms -> us
APPSPAWN_LOGV("AppSpawnTestServer:: timeout %{public}u %{public}llu", server->protectTime_, diff);
APPSPAWN_LOGV("AppSpawnTestServer:: timeout %{public}u %{public}" PRIu64 "", server->protectTime_, diff);
server->StopSpawnService();
return;
}
@ -507,14 +535,14 @@ void AppSpawnTestHelper::SetDefaultTestData()
defaultMsgFlags_ = 0;
}
int AppSpawnTestHelper::CreateSocket(void)
int AppSpawnTestHelper::CreateSocket(int type)
{
const uint32_t maxCount = 10;
uint32_t count = 0;
int socketId = -1;
while ((socketId < 0) && (count < maxCount)) {
usleep(20000); // 20000 20ms
socketId = CreateClientSocket(0, 2); // 2s
socketId = CreateClientSocket(type, 2); // 2s
if (socketId > 0) {
return socketId;
}
@ -580,7 +608,7 @@ int AppSpawnTestHelper::AddBaseTlv(uint8_t *buffer, uint32_t bufferLen, uint32_t
{
// add app flage
uint32_t currLen = 0;
uint32_t flags[2] = {1, 0};
uint32_t flags[2] = {1, 0b1010};
AppSpawnTlv tlv = {};
tlv.tlvType = TLV_MSG_FLAGS;
tlv.tlvLen = sizeof(AppSpawnTlv) + sizeof(flags);
@ -660,11 +688,10 @@ AppSpawnContent *AppSpawnTestHelper::StartSpawnServer(std::string &cmd, CmdArgs
}
return content;
}
} // namespace OHOS
MODULE_CONSTRUCTOR(void)
{
MakeDirRec(APPSPAWN_MSG_DIR "appspawn", 0771, 1);
MakeDirRec(APPSPAWN_MSG_DIR "nwebspawn", 0771, 1);
}
} // namespace OHOS

View File

@ -105,7 +105,7 @@ public:
AppSpawnReqMsgHandle CreateMsg(AppSpawnClientHandle handle, uint32_t msgType = MSG_APP_SPAWN, int base = 0);
AppSpawningCtx *GetAppProperty(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle);
int CreateSocket(void);
int CreateSocket(int type = 0);
int CreateSendMsg(std::vector<uint8_t> &buffer, uint32_t msgType, uint32_t &msgLen,
const std::vector<AddTlvFunction> &addTlvFuncs);
const std::vector<const char *> &GetPermissions()
@ -170,6 +170,7 @@ public:
void Start(void);
void Start(RecvMsgProcess process, uint32_t time = defaultProtectTime);
void Stop();
void ServiceThread();
void KillNWebSpawnServer();
static const uint32_t defaultProtectTime;
@ -179,7 +180,6 @@ private:
void StopSpawnService(void);
static uint32_t serverId;
static void *ServiceThread(void *arg);
#ifdef USER_TIMER_TO_CHECK
static void ProcessIdle(const TimerHandle taskHandle, void *context);
#else
@ -189,6 +189,7 @@ private:
AppSpawnContent *content_ = nullptr;
std::atomic<long> appPid_{-1};
std::string serviceCmd_{};
bool running_{false};
#ifdef USER_TIMER_TO_CHECK
TimerHandle timer_;
#else

View File

@ -105,7 +105,7 @@ void SetDumpToStream(FILE *stream);
typedef int (*SplitStringHandle)(const char *str, void *context);
int32_t StringSplit(const char *str, const char *separator, void *context, SplitStringHandle handle);
char *GetLastStr(const char *str, const char *dst);
uint32_t GetSpawnTimeout(uint32_t def);
void DumpCurrentDir(char *buffer, uint32_t bufferLen, const char *dirPath);
#ifndef APP_FILE_NAME

View File

@ -34,6 +34,9 @@
uint64_t DiffTime(const struct timespec *startTime, const struct timespec *endTime)
{
APPSPAWN_CHECK_ONLY_EXPER(startTime != NULL, return 0);
APPSPAWN_CHECK_ONLY_EXPER(endTime != NULL, return 0);
uint64_t diff = (uint64_t)((endTime->tv_sec - startTime->tv_sec) * 1000000); // 1000000 s-us
if (endTime->tv_nsec > startTime->tv_nsec) {
diff += (endTime->tv_nsec - startTime->tv_nsec) / 1000; // 1000 ns - us
@ -132,6 +135,9 @@ int32_t StringSplit(const char *str, const char *separator, void *context, Split
char *GetLastStr(const char *str, const char *dst)
{
APPSPAWN_CHECK_ONLY_EXPER(str != NULL , return NULL);
APPSPAWN_CHECK_ONLY_EXPER(dst != NULL , return NULL);
char *end = (char *)str + strlen(str);
size_t len = strlen(dst);
while (end != str) {
@ -194,6 +200,10 @@ cJSON *GetJsonObjFromFile(const char *jsonPath)
int ParseJsonConfig(const char *basePath, const char *fileName, ParseConfig parseConfig, ParseJsonContext *context)
{
APPSPAWN_CHECK_ONLY_EXPER(basePath != NULL , return APPSPAWN_ARG_INVALID);
APPSPAWN_CHECK_ONLY_EXPER(fileName != NULL , return APPSPAWN_ARG_INVALID);
APPSPAWN_CHECK_ONLY_EXPER(parseConfig != NULL , return APPSPAWN_ARG_INVALID);
// load sandbox config
char path[PATH_MAX] = {};
CfgFiles *files = GetCfgFiles(basePath);
@ -223,6 +233,10 @@ int ParseJsonConfig(const char *basePath, const char *fileName, ParseConfig pars
void DumpCurrentDir(char *buffer, uint32_t bufferLen, const char *dirPath)
{
APPSPAWN_CHECK_ONLY_EXPER(buffer != NULL , return);
APPSPAWN_CHECK_ONLY_EXPER(dirPath != NULL , return);
APPSPAWN_CHECK_ONLY_EXPER(bufferLen > 0 , return);
char tmp[32] = {0}; // 32 max
int ret = GetParameter("startup.appspawn.cold.boot", "", tmp, sizeof(tmp));
if (ret <= 0 || strcmp(tmp, "1") != 0) {
@ -308,3 +322,16 @@ void AppSpawnDump(const char *fmt, ...)
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif
uint32_t GetSpawnTimeout(uint32_t def)
{
uint32_t value = def;
char data[32] = {}; // 32 length
int ret = GetParameter("persist.appspawn.reqMgr.timeout", "0", data, sizeof(data));
if (ret > 0 && strcmp(data, "0") != 0) {
errno = 0;
value = atoi(data);
return (errno != 0) ? def : ((value < def) ? def : value);
}
return value;
}