From c14b9b06f0e688164dfad6da306ef71b43c0b4f2 Mon Sep 17 00:00:00 2001 From: laiguizhong Date: Mon, 6 Jun 2022 16:31:16 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laiguizhong --- adapter/appspawn_ace.cpp | 11 +-- adapter/appspawn_sandbox.cpp | 5 +- common/appspawn_server.c | 19 ++-- common/appspawn_server.h | 1 + interfaces/innerkits/BUILD.gn | 11 ++- .../innerkits/client/appspawn_socket.cpp | 54 ++++------- interfaces/innerkits/client/client_socket.cpp | 30 +++--- standard/appspawn_process.c | 92 +++++++------------ standard/appspawn_service.c | 7 +- standard/appspawn_service.h | 6 ++ 10 files changed, 99 insertions(+), 137 deletions(-) diff --git a/adapter/appspawn_ace.cpp b/adapter/appspawn_ace.cpp index 09b5c90c..a8fc6b0f 100644 --- a/adapter/appspawn_ace.cpp +++ b/adapter/appspawn_ace.cpp @@ -26,15 +26,10 @@ void LoadExtendLib(AppSpawnContent *content) #endif void *AceAbilityLib = NULL; APPSPAWN_LOGI("MainThread::LoadAbilityLibrary. Start calling dlopen acelibdir."); -#ifndef APPSPAWN_TEST AceAbilityLib = dlopen(acelibdir, RTLD_NOW | RTLD_GLOBAL); -#endif - if (AceAbilityLib == NULL) { - APPSPAWN_LOGE("Fail to dlopen %s, [%s]", acelibdir, dlerror()); - } else { - APPSPAWN_LOGI("Success to dlopen %s", acelibdir); - } - APPSPAWN_LOGI("MainThread::LoadAbilityLibrary. End calling dlopen."); + APPSPAWN_CHECK(AceAbilityLib != NULL, return, "Fail to dlopen %s, [%s]", acelibdir, dlerror()); + APPSPAWN_LOGI("Success to dlopen %s", acelibdir); + APPSPAWN_LOGI("MainThread::LoadAbilityLibrary. End calling dlopen"); } void RunChildProcessor(AppSpawnContent *content, AppSpawnClient *client) diff --git a/adapter/appspawn_sandbox.cpp b/adapter/appspawn_sandbox.cpp index d89780f6..328cc026 100644 --- a/adapter/appspawn_sandbox.cpp +++ b/adapter/appspawn_sandbox.cpp @@ -35,7 +35,6 @@ using namespace std; using namespace OHOS; using namespace OHOS::HiviewDFX; using namespace OHOS::AppSpawn; -static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "AppSpawn_SandboxUtil"}; bool g_isPrivAppSandboxCreated = false; bool g_isAppSandboxCreated = false; @@ -55,13 +54,13 @@ void LoadAppSandboxConfig(void) nlohmann::json appSandboxConfig; bool rc = JsonUtils::GetJsonObjFromJson(appSandboxConfig, APP_JSON_CONFIG); if (!rc) { - HiLog::Error(LABEL, "AppSpawnServer::Failed to load app private sandbox config"); + APPSPAWN_LOGE("AppSpawnServer::Failed to load app private sandbox config"); } SandboxUtils::StoreJsonConfig(appSandboxConfig); rc = JsonUtils::GetJsonObjFromJson(appSandboxConfig, PRODUCT_JSON_CONFIG); if (!rc) { - HiLog::Error(LABEL, "AppSpawnServer::Failed to load app product sandbox config"); + APPSPAWN_LOGE("AppSpawnServer::Failed to load app product sandbox config"); } SandboxUtils::StoreProductJsonConfig(appSandboxConfig); } diff --git a/common/appspawn_server.c b/common/appspawn_server.c index 3528b095..057e8395 100644 --- a/common/appspawn_server.c +++ b/common/appspawn_server.c @@ -85,13 +85,8 @@ int DoStartApp(struct AppSpawnContent_ *content, AppSpawnClient *client, char *l return 0; } -int AppSpawnProcessMsg(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_t *childPid) +int ForkChildProc(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_t pid) { - APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for appspawn"); - APPSPAWN_CHECK(client != NULL && childPid != NULL, return -1, "Invalid client for appspawn"); - APPSPAWN_LOGI("AppSpawnProcessMsg id %d 0x%x", client->id, client->flags); - - pid_t pid = fork(); if (pid < 0) { return -errno; } else if (pid == 0) { @@ -130,6 +125,18 @@ int AppSpawnProcessMsg(struct AppSpawnContent_ *content, AppSpawnClient *client, } ProcessExit(); } + return 0; +} + +int AppSpawnProcessMsg(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_t *childPid) +{ + APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for appspawn"); + APPSPAWN_CHECK(client != NULL && childPid != NULL, return -1, "Invalid client for appspawn"); + APPSPAWN_LOGI("AppSpawnProcessMsg id %d 0x%x", client->id, client->flags); + + pid_t pid = fork(); + int ret = ForkChildProc(content, client, pid); + APPSPAWN_CHECK(ret == 0, return ret, "fork child process error: %d", ret); *childPid = pid; return 0; } diff --git a/common/appspawn_server.h b/common/appspawn_server.h index c344470d..fa918a12 100644 --- a/common/appspawn_server.h +++ b/common/appspawn_server.h @@ -72,6 +72,7 @@ typedef struct AppSpawnContent_ { AppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcName, uint32_t longProcNameLen, int cold); int AppSpawnProcessMsg(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_t *childPid); int DoStartApp(struct AppSpawnContent_ *content, AppSpawnClient *client, char *longProcName, uint32_t longProcNameLen); +int ForkChildProc(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_t pid); #ifdef OHOS_DEBUG void GetCurTime(struct timespec* tmCur); diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 8d6c1072..7750088c 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -24,9 +24,16 @@ ohos_static_library("appspawn_socket_client") { "client/appspawn_socket.cpp", "client/client_socket.cpp", ] - include_dirs = [ "include" ] + include_dirs = [ + "include", + "//base/startup/appspawn_standard/common", + ] + public_configs = [ ":exported_header_files" ] - deps = [ "//utils/native/base:utils" ] + deps = [ + "//utils/native/base:utils", + "//base/startup/init_lite/interfaces/innerkits:libbegetutil", + ] external_deps = [ "hiviewdfx_hilog_native:libhilog" ] subsystem_name = "${subsystem_name}" diff --git a/interfaces/innerkits/client/appspawn_socket.cpp b/interfaces/innerkits/client/appspawn_socket.cpp index b1521f0b..9574a2d6 100644 --- a/interfaces/innerkits/client/appspawn_socket.cpp +++ b/interfaces/innerkits/client/appspawn_socket.cpp @@ -19,6 +19,7 @@ #include #include +#include "appspawn_server.h" #include "hilog/log.h" #include "pubdef.h" #include "securec.h" @@ -26,7 +27,6 @@ namespace OHOS { namespace AppSpawn { using namespace OHOS::HiviewDFX; -static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "AppSpawnSocket"}; AppSpawnSocket::AppSpawnSocket(const std::string &name) { @@ -48,15 +48,10 @@ int AppSpawnSocket::GetSocketFd() const int AppSpawnSocket::PackSocketAddr() { - if (socketName_.empty()) { - HiLog::Error(LABEL, "Invalid socket name: empty"); - return -EINVAL; - } + APPSPAWN_CHECK(!socketName_.empty(), return -EINVAL, "Invalid socket name: empty"); - if (memset_s(&socketAddr_, sizeof(socketAddr_), 0, sizeof(socketAddr_)) != EOK) { - HiLog::Error(LABEL, "Failed to memset socket addr"); - return -1; - } + APPSPAWN_CHECK(memset_s(&socketAddr_, sizeof(socketAddr_), 0, sizeof(socketAddr_)) == EOK, + return -1, "Failed to memset socket addr"); socklen_t pathLen = 0; if (socketName_[0] == '/') { @@ -66,7 +61,7 @@ int AppSpawnSocket::PackSocketAddr() } socklen_t pathSize = sizeof(socketAddr_.sun_path); if (pathLen >= pathSize) { - HiLog::Error(LABEL, "Invalid socket name: '%{public}s' too long", socketName_.c_str()); + APPSPAWN_LOGE("Invalid socket name: '%s' too long", socketName_.c_str()); return -1; } @@ -77,10 +72,7 @@ int AppSpawnSocket::PackSocketAddr() len = snprintf_s(socketAddr_.sun_path, pathSize, (pathSize - 1), "%s%s", socketDir_.c_str(), socketName_.c_str()); } - if (static_cast(pathLen) != len) { - HiLog::Error(LABEL, "Failed to copy socket path"); - return -1; - } + APPSPAWN_CHECK(static_cast(pathLen) == len, return -1, "Failed to copy socket path"); socketAddr_.sun_family = AF_LOCAL; socketAddrLen_ = offsetof(struct sockaddr_un, sun_path) + pathLen + 1; @@ -91,19 +83,16 @@ int AppSpawnSocket::PackSocketAddr() int AppSpawnSocket::CreateSocket() { int socketFd = socket(AF_UNIX, SOCK_STREAM, 0); // SOCK_SEQPACKET - if (socketFd < 0) { - HiLog::Error(LABEL, "Failed to create socket: %{public}d", errno); - return (-errno); - } + APPSPAWN_CHECK(socketFd >= 0, return -errno, "Failed to create socket: %d", errno); - HiLog::Debug(LABEL, "Created socket with fd %{public}d", socketFd); + APPSPAWN_LOGV("Created socket with fd %d", socketFd); return socketFd; } void AppSpawnSocket::CloseSocket(int &socketFd) { if (socketFd >= 0) { - HiLog::Debug(LABEL, "Closed socket with fd %{public}d", socketFd); + APPSPAWN_LOGV("Closed socket with fd %d", socketFd); close(socketFd); socketFd = -1; } @@ -112,20 +101,15 @@ void AppSpawnSocket::CloseSocket(int &socketFd) int AppSpawnSocket::ReadSocketMessage(int socketFd, void *buf, int len) { if (socketFd < 0 || len <= 0 || buf == nullptr) { - HiLog::Error(LABEL, "Invalid args: socket %{public}d, len %{public}d, buf might be nullptr", socketFd, len); + APPSPAWN_LOGE("Invalid args: socket %d, len %d, buf might be nullptr", socketFd, len); return -1; } - if (memset_s(buf, len, 0, len) != EOK) { - HiLog::Warn(LABEL, "Failed to memset read buf"); - return -1; - } + APPSPAWN_CHECK(memset_s(buf, len, 0, len) == EOK, return -1, "Failed to memset read buf"); ssize_t rLen = TEMP_FAILURE_RETRY(read(socketFd, buf, len)); - if (rLen < 0) { - HiLog::Error(LABEL, "Read message from fd %{public}d error %{public}zd: %{public}d", socketFd, rLen, errno); - return -EFAULT; - } + APPSPAWN_CHECK(rLen >= 0, return -EFAULT, "Read message from fd %d error %zd: %d", + socketFd, rLen, errno); return rLen; } @@ -133,7 +117,7 @@ int AppSpawnSocket::ReadSocketMessage(int socketFd, void *buf, int len) int AppSpawnSocket::WriteSocketMessage(int socketFd, const void *buf, int len) { if (socketFd < 0 || len <= 0 || buf == nullptr) { - HiLog::Error(LABEL, "Invalid args: socket %{public}d, len %{public}d, buf might be nullptr", socketFd, len); + APPSPAWN_LOGE("Invalid args: socket %d, len %d, buf might be nullptr", socketFd, len); return -1; } @@ -142,12 +126,10 @@ int AppSpawnSocket::WriteSocketMessage(int socketFd, const void *buf, int len) const uint8_t *offset = reinterpret_cast(buf); for (ssize_t wLen = 0; remain > 0; offset += wLen, remain -= wLen, written += wLen) { wLen = write(socketFd, offset, remain); - HiLog::Debug(LABEL, "socket fd %{public}d, wLen %zd", socketFd, wLen); - if ((wLen <= 0) && (errno != EINTR)) { - HiLog::Error(LABEL, "Failed to write message to fd %{public}d, error %zd: %{public}d", - socketFd, wLen, errno); - return (-errno); - } + APPSPAWN_LOGV("socket fd %d, wLen %zd", socketFd, wLen); + bool isRet = (wLen <= 0) && (errno != EINTR); + APPSPAWN_CHECK(!isRet, return -errno, "Failed to write message to fd %d, error %zd: %d", + socketFd, wLen, errno); } return written; diff --git a/interfaces/innerkits/client/client_socket.cpp b/interfaces/innerkits/client/client_socket.cpp index 12bea8e1..d6c9da19 100644 --- a/interfaces/innerkits/client/client_socket.cpp +++ b/interfaces/innerkits/client/client_socket.cpp @@ -19,13 +19,13 @@ #include #include +#include "appspawn_server.h" #include "hilog/log.h" #include "securec.h" namespace OHOS { namespace AppSpawn { using namespace OHOS::HiviewDFX; -static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "ClientSocket"}; ClientSocket::ClientSocket(const std::string &client) : AppSpawnSocket(client) {} @@ -34,20 +34,17 @@ int ClientSocket::CreateClient() { if (socketFd_ < 0) { socketFd_ = CreateSocket(); - if (socketFd_ < 0) { - HiLog::Error(LABEL, "Client: Create socket failed"); - return socketFd_; - } + APPSPAWN_CHECK(socketFd_ >= 0, return socketFd_, "Client: Create socket failed"); } - HiLog::Debug(LABEL, "Client: CreateClient socket fd %{public}d", socketFd_); + APPSPAWN_LOGE("Client: CreateClient socket fd %d", socketFd_); return 0; } void ClientSocket::CloseClient() { if (socketFd_ < 0) { - HiLog::Error(LABEL, "Client: Invalid connectFd %{public}d", socketFd_); + APPSPAWN_LOGE("Client: Invalid connectFd %d", socketFd_); return; } @@ -58,27 +55,22 @@ void ClientSocket::CloseClient() int ClientSocket::ConnectSocket(int connectFd) { if (connectFd < 0) { - HiLog::Error(LABEL, "Client: Invalid socket fd: %{public}d", connectFd); + APPSPAWN_LOGE("Client: Invalid socket fd: %d", connectFd); return -1; } - if (PackSocketAddr() != 0) { - return -1; - } + APPSPAWN_CHECK(PackSocketAddr() == 0, return -1, "pack socket failed"); - if ((setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0) || - (setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0)) { - HiLog::Warn(LABEL, "Client: Failed to set opt of socket %{public}d, err %{public}d", connectFd, errno); - return -1; - } + bool isRet = (setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0) || + (setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0); + APPSPAWN_CHECK(!isRet, return (-1), "Client: Failed to set opt of socket %d, err %d", connectFd, errno); if (connect(connectFd, reinterpret_cast(&socketAddr_), socketAddrLen_) < 0) { - HiLog::Warn(LABEL, "Client: Connect on socket fd %{public}d, failed: %{public}d", connectFd, errno); + APPSPAWN_LOGW("Client: Connect on socket fd %d, failed: %d", connectFd, errno); return -1; } - HiLog::Debug(LABEL, "Client: Connected on socket fd %{public}d, name '%{public}s'", - connectFd, socketAddr_.sun_path); + APPSPAWN_LOGV("Client: Connected on socket fd %d, name '%s'", connectFd, socketAddr_.sun_path); return 0; } diff --git a/standard/appspawn_process.c b/standard/appspawn_process.c index 38227397..2d8d49c9 100644 --- a/standard/appspawn_process.c +++ b/standard/appspawn_process.c @@ -40,18 +40,14 @@ static int SetProcessName(struct AppSpawnContent_ *content, AppSpawnClient *clie AppSpawnClientExt *appPropertyExt = (AppSpawnClientExt *)client; AppParameter *appProperty = &appPropertyExt->property; int len = strlen(appProperty->processName); - if (longProcName == NULL || len <= 0) { - APPSPAWN_LOGE("process name is nullptr or length error"); - return -EINVAL; - } + bool isRet = longProcName == NULL || len <= 0; + APPSPAWN_CHECK(!isRet, return -EINVAL, "process name is nullptr or length error"); char shortName[MAX_LEN_SHORT_NAME] = {0}; // process short name max length 16 bytes. if (len >= MAX_LEN_SHORT_NAME) { - if (strncpy_s(shortName, MAX_LEN_SHORT_NAME, appProperty->processName, MAX_LEN_SHORT_NAME - 1) != EOK) { - APPSPAWN_LOGE("strncpy_s short name error: %d", errno); - return -EINVAL; - } + isRet = strncpy_s(shortName, MAX_LEN_SHORT_NAME, appProperty->processName, MAX_LEN_SHORT_NAME - 1) != EOK; + APPSPAWN_CHECK(!isRet, return -EINVAL, "strncpy_s short name error: %d", errno); } else { if (strncpy_s(shortName, MAX_LEN_SHORT_NAME, appProperty->processName, len) != EOK) { APPSPAWN_LOGE("strncpy_s short name error: %d", errno); @@ -60,22 +56,17 @@ static int SetProcessName(struct AppSpawnContent_ *content, AppSpawnClient *clie } // set short name - if (prctl(PR_SET_NAME, shortName) == -1) { - APPSPAWN_LOGE("prctl(PR_SET_NAME) error: %d", errno); - return (-errno); - } + isRet = prctl(PR_SET_NAME, shortName) == -1; + APPSPAWN_CHECK(!isRet, return -errno, "prctl(PR_SET_NAME) error: %d", errno); // reset longProcName - if (memset_s(longProcName, (size_t)longProcNameLen, 0, (size_t)longProcNameLen) != EOK) { - APPSPAWN_LOGE("Failed to memset long process name"); - return -EINVAL; - } + isRet = memset_s(longProcName, (size_t)longProcNameLen, 0, (size_t)longProcNameLen) != EOK; + APPSPAWN_CHECK(!isRet, return -EINVAL, "Failed to memset long process name"); // set long process name - if (strncpy_s(longProcName, sizeof(appProperty->processName) - 1, appProperty->processName, len) != EOK) { - APPSPAWN_LOGE("strncpy_s long name error: %d longProcNameLen %u", errno, longProcNameLen); - return -EINVAL; - } + isRet = strncpy_s(longProcName, sizeof(appProperty->processName) - 1, appProperty->processName, len) != EOK; + APPSPAWN_CHECK(!isRet, return -EINVAL, "strncpy_s long name error: %d longProcNameLen %u", errno, longProcNameLen); + return 0; } @@ -84,10 +75,8 @@ static int SetKeepCapabilities(struct AppSpawnContent_ *content, AppSpawnClient AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client; // set keep capabilities when user not root. if (appProperty->property.uid != 0) { - if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) { - APPSPAWN_LOGE("set keepcaps failed: %d", errno); - return (-errno); - } + bool isRet = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1; + APPSPAWN_CHECK(!isRet, return -errno, "set keepcaps failed: %d", errno); } return 0; } @@ -97,18 +86,15 @@ static int SetCapabilities(struct AppSpawnContent_ *content, AppSpawnClient *cli // init cap struct __user_cap_header_struct cap_header; - if (memset_s(&cap_header, sizeof(cap_header), 0, sizeof(cap_header)) != EOK) { - APPSPAWN_LOGE("Failed to memset cap header"); - return -EINVAL; - } + bool isRet = memset_s(&cap_header, sizeof(cap_header), 0, sizeof(cap_header)) != EOK; + APPSPAWN_CHECK(!isRet, return -EINVAL, "Failed to memset cap header"); + cap_header.version = _LINUX_CAPABILITY_VERSION_3; cap_header.pid = 0; struct __user_cap_data_struct cap_data[2]; - if (memset_s(&cap_data, sizeof(cap_data), 0, sizeof(cap_data)) != EOK) { - APPSPAWN_LOGE("Failed to memset cap data"); - return -EINVAL; - } + isRet = memset_s(&cap_data, sizeof(cap_data), 0, sizeof(cap_data)) != EOK; + APPSPAWN_CHECK(!isRet, return -EINVAL, "Failed to memset cap data"); // init inheritable permitted effective zero #ifdef GRAPHIC_PERMISSION_CHECK @@ -129,10 +115,8 @@ static int SetCapabilities(struct AppSpawnContent_ *content, AppSpawnClient *cli cap_data[1].effective = (__u32)(effective >> BITLEN32); // set capabilities - if (capset(&cap_header, &cap_data[0]) == -1) { - APPSPAWN_LOGE("capset failed: %d", errno); - return (-errno); - } + isRet = capset(&cap_header, &cap_data[0]) == -1; + APPSPAWN_CHECK(!isRet, return -errno, "capset failed: %d", errno); return 0; } @@ -144,21 +128,15 @@ static void InitDebugParams(struct AppSpawnContent_ *content, AppSpawnClient *cl #else const char *debugSoPath = "/system/lib/libhidebug.so"; #endif - if (access(debugSoPath, F_OK) != 0) { - return; - } + bool isRet = access(debugSoPath, F_OK) != 0; + APPSPAWN_CHECK(!isRet, return, "access failed, errno = %d", errno); + void *handle = dlopen(debugSoPath, RTLD_LAZY); - if (handle == NULL) { - APPSPAWN_LOGE("Failed to dlopen libhidebug.so, %s", dlerror()); - return; - } + APPSPAWN_CHECK(handle != NULL, return, "Failed to dlopen libhidebug.so, %s", dlerror()); + bool (*initParam)(const char *name); initParam = (bool (*)(const char *name))dlsym(handle, "InitEnvironmentParam"); - if (initParam == NULL) { - APPSPAWN_LOGE("Failed to dlsym InitEnvironmentParam, %s", dlerror()); - dlclose(handle); - return; - } + APPSPAWN_CHECK(initParam != NULL, dlclose(handle); return, "Failed to dlsym InitEnvironmentParam, %s", dlerror()); (*initParam)(appProperty->property.processName); dlclose(handle); } @@ -183,22 +161,16 @@ static int SetUidGid(struct AppSpawnContent_ *content, AppSpawnClient *client) #ifdef GRAPHIC_PERMISSION_CHECK AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client; // set gids - if (setgroups(appProperty->property.gidCount, (const gid_t *)(&appProperty->property.gidTable[0])) == -1) { - APPSPAWN_LOGE("setgroups failed: %d, gids.size=%u", errno, appProperty->property.gidCount); - return (-errno); - } + bool isRet = setgroups(appProperty->property.gidCount, (const gid_t *)(&appProperty->property.gidTable[0])) == -1; + APPSPAWN_CHECK(!isRet, return -errno, "setgroups failed: %d, gids.size=%u", errno, appProperty->property.gidCount); // set gid - if (setresgid(appProperty->property.gid, appProperty->property.gid, appProperty->property.gid) == -1) { - APPSPAWN_LOGE("setgid(%u) failed: %d", appProperty->property.gid, errno); - return (-errno); - } + isRet = setresgid(appProperty->property.gid, appProperty->property.gid, appProperty->property.gid) == -1; + APPSPAWN_CHECK(!isRet, return -errno, "setgid(%u) failed: %d", appProperty->property.gid, errno); // If the effective user ID is changed from 0 to nonzero, then all capabilities are cleared from the effective set - if (setresuid(appProperty->property.uid, appProperty->property.uid, appProperty->property.uid) == -1) { - APPSPAWN_LOGE("setuid(%u) failed: %d", appProperty->property.uid, errno); - return (-errno); - } + isRet = setresuid(appProperty->property.uid, appProperty->property.uid, appProperty->property.uid) == -1; + APPSPAWN_CHECK(!isRet, return -errno, "setuid(%u) failed: %d", appProperty->property.uid, errno); #endif return 0; } diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index 570d3d9a..3a8be9f0 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -355,7 +355,7 @@ static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, } } -static int OnConnection(const LoopHandle loopHandle, const TaskHandle server) +APPSPAWN_STATIC int OnConnection(const LoopHandle loopHandle, const TaskHandle server) { static uint32_t clientId = 0; APPSPAWN_LOGI("OnConnection "); @@ -431,7 +431,7 @@ void AppSpawnColdRun(AppSpawnContent *content, int argc, char *const argv[]) g_appSpawnContent = NULL; } -static void AppSpawnRun(AppSpawnContent *content, int argc, char *const argv[]) +APPSPAWN_STATIC void AppSpawnRun(AppSpawnContent *content, int argc, char *const argv[]) { APPSPAWN_LOGI("AppSpawnRun"); AppSpawnContentExt *appSpawnContent = (AppSpawnContentExt *)content; @@ -444,8 +444,9 @@ static void AppSpawnRun(AppSpawnContent *content, int argc, char *const argv[]) if (status != 0) { APPSPAWN_LOGE("Failed to add signal %d", status); } - +#ifndef APPSPAWN_TEST LE_RunLoop(LE_GetDefaultLoop()); +#endif APPSPAWN_LOGI("AppSpawnRun exit "); LE_CloseSignalTask(LE_GetDefaultLoop(), appSpawnContent->sigHandler); // release resource diff --git a/standard/appspawn_service.h b/standard/appspawn_service.h index 3ef432fd..7a758ec4 100644 --- a/standard/appspawn_service.h +++ b/standard/appspawn_service.h @@ -25,6 +25,12 @@ extern "C" { #endif +#ifdef APPSPAWN_TEST +#define APPSPAWN_STATIC +#else +#define APPSPAWN_STATIC static +#endif + #define APP_HASH_BUTT 32 #define FLAGS_ON_DEMAND 0x1 #define FLAGS_MODE_COLD 0x2 -- Gitee From a182ea7411da3b9fb2bf30633061e7a9e5dcb5be Mon Sep 17 00:00:00 2001 From: laiguizhong Date: Mon, 6 Jun 2022 19:17:56 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E5=8F=8A=E7=BC=96=E8=AF=91=E5=91=8A=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laiguizhong --- interfaces/innerkits/BUILD.gn | 2 +- test/unittest/app_spawn_socket_test/BUILD.gn | 75 +++---- .../app_spawn_standard_test.cpp | 193 ++++++++++++++++-- test/unittest/client_socket_test/BUILD.gn | 5 +- 4 files changed, 218 insertions(+), 57 deletions(-) diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 7750088c..3426b97c 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -31,8 +31,8 @@ ohos_static_library("appspawn_socket_client") { public_configs = [ ":exported_header_files" ] deps = [ - "//utils/native/base:utils", "//base/startup/init_lite/interfaces/innerkits:libbegetutil", + "//utils/native/base:utils", ] external_deps = [ "hiviewdfx_hilog_native:libhilog" ] diff --git a/test/unittest/app_spawn_socket_test/BUILD.gn b/test/unittest/app_spawn_socket_test/BUILD.gn index c90d9872..b042881c 100644 --- a/test/unittest/app_spawn_socket_test/BUILD.gn +++ b/test/unittest/app_spawn_socket_test/BUILD.gn @@ -1,36 +1,39 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//base/startup/appspawn_standard/appspawn.gni") -import("//build/test.gni") - -ohos_unittest("AppSpawnSocketTest") { - module_out_path = "${module_output_path}" - - sources = - [ "${appspawn_path}/interfaces/innerkits/client/appspawn_socket.cpp" ] - - sources += [ "app_spawn_socket_test.cpp" ] - - configs = [ "${appspawn_path}:appspawn_config" ] - - deps = [ "${appspawn_path}/test:appspawn_test_source" ] - - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] -} - -group("unittest") { - testonly = true - - deps = [ ":AppSpawnSocketTest" ] -} +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/startup/appspawn_standard/appspawn.gni") +import("//build/test.gni") + +ohos_unittest("AppSpawnSocketTest") { + module_out_path = "${module_output_path}" + + sources = + [ "${appspawn_path}/interfaces/innerkits/client/appspawn_socket.cpp" ] + + sources += [ "app_spawn_socket_test.cpp" ] + + configs = [ "${appspawn_path}:appspawn_config" ] + + deps = [ + "${appspawn_path}/test:appspawn_test_source", + "//base/startup/init_lite/interfaces/innerkits:libbegetutil", + ] + + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] +} + +group("unittest") { + testonly = true + + deps = [ ":AppSpawnSocketTest" ] +} diff --git a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp index b68093b0..ead4d9e3 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp @@ -31,6 +31,69 @@ using namespace testing; using namespace testing::ext; +#ifdef __cplusplus + extern "C" { +#endif + int OnConnection(const LoopHandle loopHandle, const TaskHandle server); + void AppSpawnRun(AppSpawnContent *content, int argc, char *const argv[]); +#ifdef __cplusplus + } +#endif + +static void runChildProcessor(struct AppSpawnContent_ *content, AppSpawnClient *client) +{} + +static void initAppSpawn(struct AppSpawnContent_ *content) +{} + +static void loadExtendLib(struct AppSpawnContent_ *content) +{} + +static void runAppSpawn(struct AppSpawnContent_ *content, int argc, char *const argv[]) +{} + +static void clearEnvironment(struct AppSpawnContent_ *content, AppSpawnClient *client) +{} + +static void setAppAccessToken(struct AppSpawnContent_ *content, AppSpawnClient *client) +{} + +static int setAppSandbox(struct AppSpawnContent_ *content, AppSpawnClient *client) +{ + return 0; +} + +static int setKeepCapabilities(struct AppSpawnContent_ *content, AppSpawnClient *client) +{ + return 0; +} + +static int setFileDescriptors(struct AppSpawnContent_ *content, AppSpawnClient *client) +{ + return 0; +} + +static int setProcessName(struct AppSpawnContent_ *content, AppSpawnClient *client, + char *longProcName, uint32_t longProcNameLen) +{ + return 0; +} + +static int setUidGid(struct AppSpawnContent_ *content, AppSpawnClient *client) +{ + return 0; +} + +static int setCapabilities(struct AppSpawnContent_ *content, AppSpawnClient *client) +{ + return 0; +} + +static int notifyResToParent(struct AppSpawnContent_ *content, AppSpawnClient *client, int result) +{ + return 0; +} + namespace OHOS { class AppSpawnStandardTest : public testing::Test { public: @@ -55,25 +118,21 @@ void AppSpawnStandardTest::TearDown() HWTEST(AppSpawnStandardTest, App_Spawn_Standard_001, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_001 start"; - string longProcName = "AppSpawnStandardTest1"; + string longProcName = "ohos.samples.ecg.default"; int64_t longProcNameLen = longProcName.length(); - int cold = 1; + int cold = 0; AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", (char*)longProcName.c_str(), longProcNameLen, cold); EXPECT_TRUE(content); content->loadExtendLib = LoadExtendLib; content->runChildProcessor = RunChildProcessor; - - char *const argv[] = {}; - content->initAppSpawn(content); - AppSpawnColdRun(content, 1, argv); - + AppSpawnRun(content, 0, nullptr); GTEST_LOG_(INFO) << "App_Spawn_Standard_001 end"; } HWTEST(AppSpawnStandardTest, App_Spawn_Standard_002, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_002 start"; - string longProcName = "AppSpawnStandardTest2"; + string longProcName = "ohos.samples.ecg.default"; int64_t longProcNameLen = longProcName.length(); AppSpawnClientExt* client = (AppSpawnClientExt*)malloc(sizeof(AppSpawnClientExt)); @@ -82,12 +141,16 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_002, TestSize.Level0) if (strcpy_s(client->property.apl, APP_APL_MAX_LEN, "system_basic") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - pid_t pid = 100; - AppSpawnContentExt* appSpawnContent = (AppSpawnContentExt*)calloc(1, sizeof(AppSpawnContentExt)); + + pid_t pid = 0; + AppSpawnContentExt* appSpawnContent = (AppSpawnContentExt*)malloc(sizeof(AppSpawnContentExt)); EXPECT_TRUE(appSpawnContent); + if (strcpy_s(appSpawnContent->content.longProcName, longProcNameLen, longProcName.c_str()) != 0) { + GTEST_LOG_(INFO) << "strcpy_s failed"; + }; appSpawnContent->content.longProcNameLen = longProcNameLen; appSpawnContent->timer = NULL; - appSpawnContent->content.runAppSpawn = NULL; + appSpawnContent->content.runAppSpawn = runAppSpawn; appSpawnContent->content.initAppSpawn = NULL; AppSpawnProcessMsg(&appSpawnContent->content, &client->client, &pid); free(appSpawnContent); @@ -98,7 +161,7 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_002, TestSize.Level0) HWTEST(AppSpawnStandardTest, App_Spawn_Standard_003, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_003 start"; - char longProcName[124] = "AppSpawnStandardTest3"; + char longProcName[124] = "ohos.samples.ecg.default"; int64_t longProcNameLen = 124; // 124 is str length std::unique_ptr clientExt = std::make_unique(); @@ -139,6 +202,10 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_003, TestSize.Level0) content->setAppSandbox(content, &clientExt->client); content->setAppAccessToken(content, &clientExt->client); EXPECT_EQ(content->coldStartApp(content, &clientExt->client), 0); + + int ret = ForkChildProc((AppSpawnContent_*)content, &clientExt->client, 0); + EXPECT_EQ(ret, 0); + ForkChildProc((AppSpawnContent_ *)content, &clientExt->client, -1); DoStartApp(content, &clientExt->client, longProcName, longProcNameLen); free(content); GTEST_LOG_(INFO) << "App_Spawn_Standard_003 end"; @@ -147,22 +214,21 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_003, TestSize.Level0) HWTEST(AppSpawnStandardTest, App_Spawn_Standard_004, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_004 start"; - AppSpawnClientExt* client = (AppSpawnClientExt*)malloc(sizeof(AppSpawnClientExt)); - client->client.id = 1; - client->client.flags = 0; + client->client.id = 8; + client->client.flags = 1; client->fd[0] = 1; client->fd[1] = 2; client->property.uid = 10000; client->property.gid = 1000; client->property.gidCount = 1; - if (strcpy_s(client->property.processName, APP_LEN_PROC_NAME, "test4") != 0) { + if (strcpy_s(client->property.processName, APP_LEN_PROC_NAME, "ohos.samples.ecg") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - if (strcpy_s(client->property.bundleName, APP_LEN_BUNDLE_NAME, "test4") != 0) { + if (strcpy_s(client->property.bundleName, APP_LEN_BUNDLE_NAME, "ohos.samples.ecg") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - if (strcpy_s(client->property.soPath, APP_LEN_SO_PATH, "test4") != 0) { + if (strcpy_s(client->property.soPath, APP_LEN_SO_PATH, "default") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } client->property.accessTokenId = 671201800; @@ -174,11 +240,100 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_004, TestSize.Level0) } client->property.flags = 0; - char* argv[] = {const_cast("AppSpawnStandardTest4"), const_cast("test4")}; + char* argv[] = {const_cast("default"), const_cast("ohos.samples.ecg")}; int argc = sizeof(argv)/sizeof(argv[0]); EXPECT_EQ(GetAppSpawnClientFromArg(argc, argv, client), -1); free(client); GTEST_LOG_(INFO) << "App_Spawn_Standard_004 end"; } + +HWTEST(AppSpawnStandardTest, App_Spawn_Standard_005, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Standard_005 start"; + string longProcName = "ohos.samples.ecg.default"; + int64_t longProcNameLen = longProcName.length(); + + AppSpawnClientExt* client = (AppSpawnClientExt*)malloc(sizeof(AppSpawnClientExt)); + client->client.id = 20010013; + client->client.flags = 0; + if (strcpy_s(client->property.apl, APP_APL_MAX_LEN, "system_basic") != 0) { + GTEST_LOG_(INFO) << "strcpy_s failed"; + } + pid_t pid = 100; + AppSpawnContentExt* appSpawnContent = (AppSpawnContentExt*)malloc(sizeof(AppSpawnContentExt)); + EXPECT_TRUE(appSpawnContent); + if (strcpy_s(appSpawnContent->content.longProcName, longProcNameLen, longProcName.c_str()) != 0) { + GTEST_LOG_(INFO) << "strcpy_s failed"; + }; + appSpawnContent->content.longProcNameLen = longProcNameLen; + appSpawnContent->timer = NULL; + appSpawnContent->content.runAppSpawn = NULL; + appSpawnContent->content.initAppSpawn = NULL; + AppSpawnProcessMsg(&appSpawnContent->content, &client->client, &pid); + free(appSpawnContent); + free(client); + GTEST_LOG_(INFO) << "App_Spawn_Standard_005 end"; +} + +HWTEST(AppSpawnStandardTest, App_Spawn_Standard_006, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Standard_006 start"; + string longProcName = "ohos.samples.ecg.default"; + int64_t longProcNameLen = longProcName.length(); + int cold = 1; + AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", (char*)longProcName.c_str(), longProcNameLen, cold); + EXPECT_TRUE(content); + content->loadExtendLib = LoadExtendLib; + content->runChildProcessor = RunChildProcessor; + + char tmp0[] = "/system/bin/appspawn"; + char tmp1[] = "cold-start"; + char tmp2[] = "1"; + char tmp3[] = "1:1:1:1:0:ohos.samples.ecg.default:ohos.samples.ecg:default:671201800:system_core:default"; + char * const argv[] = {tmp0, tmp1, tmp2, tmp3}; + content->initAppSpawn(content); + AppSpawnColdRun(content, 4, argv); + GTEST_LOG_(INFO) << "App_Spawn_Standard_006 end"; +} + +HWTEST(AppSpawnStandardTest, App_Spawn_Standard_007, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Standard_007 start"; + RunChildProcessor(nullptr, nullptr); + GTEST_LOG_(INFO) << "App_Spawn_Standard_007 end"; +} + +HWTEST(AppSpawnStandardTest, App_Spawn_Standard_008, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Standard_008 start"; + string longProcName = "ohos.samples.ecg.default"; + int64_t longProcNameLen = longProcName.length(); + std::unique_ptr clientExt = std::make_unique(); + AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", (char*)longProcName.c_str(), longProcNameLen, 1); + content->setAppSandbox = setAppSandbox; + content->setKeepCapabilities = setKeepCapabilities; + content->setProcessName = setProcessName; + content->setUidGid = setUidGid; + content->setFileDescriptors = setFileDescriptors; + content->setCapabilities = setCapabilities; + int ret = DoStartApp((AppSpawnContent_*)content, &clientExt->client, (char*)"", 0); + EXPECT_EQ(ret, 0); + GTEST_LOG_(INFO) << "App_Spawn_Standard_008 end"; +} + +HWTEST(AppSpawnStandardTest, App_Spawn_Standard_009, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Standard_009 start"; + TaskHandle server = (TaskHandle)malloc(sizeof(TaskHandle)); + EXPECT_TRUE(server); + int ret = OnConnection(nullptr, server); + EXPECT_EQ(ret, -1); + server->flags = 0; + OnConnection(nullptr, server); + server->flags = 1; + OnConnection(nullptr, server); + free(server); + GTEST_LOG_(INFO) << "App_Spawn_Standard_009 end"; +} } // namespace OHOS diff --git a/test/unittest/client_socket_test/BUILD.gn b/test/unittest/client_socket_test/BUILD.gn index d5ba067a..bd99d4e2 100644 --- a/test/unittest/client_socket_test/BUILD.gn +++ b/test/unittest/client_socket_test/BUILD.gn @@ -28,7 +28,10 @@ ohos_unittest("ClientSocketTest") { configs = [ "${appspawn_path}:appspawn_config" ] - deps = [ "${appspawn_path}/test:appspawn_test_source" ] + deps = [ + "${appspawn_path}/test:appspawn_test_source", + "//base/startup/init_lite/interfaces/innerkits:libbegetutil", + ] external_deps = [ "hiviewdfx_hilog_native:libhilog" ] } -- Gitee From d9eb94a7664b92098dece4dc3a32af55caa13ee3 Mon Sep 17 00:00:00 2001 From: laiguizhong Date: Mon, 6 Jun 2022 20:19:29 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9ut=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=91=8A=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laiguizhong --- test/unittest/app_spawn_socket_test/BUILD.gn | 78 +++++++++---------- .../app_spawn_standard_test.cpp | 30 ++----- test/unittest/client_socket_test/BUILD.gn | 2 +- 3 files changed, 48 insertions(+), 62 deletions(-) diff --git a/test/unittest/app_spawn_socket_test/BUILD.gn b/test/unittest/app_spawn_socket_test/BUILD.gn index b042881c..ce226bbf 100644 --- a/test/unittest/app_spawn_socket_test/BUILD.gn +++ b/test/unittest/app_spawn_socket_test/BUILD.gn @@ -1,39 +1,39 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//base/startup/appspawn_standard/appspawn.gni") -import("//build/test.gni") - -ohos_unittest("AppSpawnSocketTest") { - module_out_path = "${module_output_path}" - - sources = - [ "${appspawn_path}/interfaces/innerkits/client/appspawn_socket.cpp" ] - - sources += [ "app_spawn_socket_test.cpp" ] - - configs = [ "${appspawn_path}:appspawn_config" ] - - deps = [ - "${appspawn_path}/test:appspawn_test_source", - "//base/startup/init_lite/interfaces/innerkits:libbegetutil", - ] - - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] -} - -group("unittest") { - testonly = true - - deps = [ ":AppSpawnSocketTest" ] -} +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/startup/appspawn_standard/appspawn.gni") +import("//build/test.gni") + +ohos_unittest("AppSpawnSocketTest") { + module_out_path = "${module_output_path}" + + sources = + [ "${appspawn_path}/interfaces/innerkits/client/appspawn_socket.cpp" ] + + sources += [ "app_spawn_socket_test.cpp" ] + + configs = [ "${appspawn_path}:appspawn_config" ] + + deps = [ + "${appspawn_path}/test:appspawn_test_source", + "//base/startup/init_lite/interfaces/innerkits:libbegetutil", + ] + + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] +} + +group("unittest") { + testonly = true + + deps = [ ":AppSpawnSocketTest" ] +} diff --git a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp index ead4d9e3..3b7a43c2 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp @@ -40,24 +40,16 @@ using namespace testing::ext; } #endif -static void runChildProcessor(struct AppSpawnContent_ *content, AppSpawnClient *client) +namespace OHOS { +static void RunChildProcessor(struct AppSpawnContent_ *content, AppSpawnClient *client) {} static void initAppSpawn(struct AppSpawnContent_ *content) {} -static void loadExtendLib(struct AppSpawnContent_ *content) -{} - static void runAppSpawn(struct AppSpawnContent_ *content, int argc, char *const argv[]) {} -static void clearEnvironment(struct AppSpawnContent_ *content, AppSpawnClient *client) -{} - -static void setAppAccessToken(struct AppSpawnContent_ *content, AppSpawnClient *client) -{} - static int setAppSandbox(struct AppSpawnContent_ *content, AppSpawnClient *client) { return 0; @@ -89,19 +81,13 @@ static int setCapabilities(struct AppSpawnContent_ *content, AppSpawnClient *cli return 0; } -static int notifyResToParent(struct AppSpawnContent_ *content, AppSpawnClient *client, int result) -{ - return 0; -} - -namespace OHOS { class AppSpawnStandardTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp(); void TearDown(); -}; +} void AppSpawnStandardTest::SetUpTestCase() {} @@ -147,10 +133,10 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_002, TestSize.Level0) EXPECT_TRUE(appSpawnContent); if (strcpy_s(appSpawnContent->content.longProcName, longProcNameLen, longProcName.c_str()) != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; - }; + } appSpawnContent->content.longProcNameLen = longProcNameLen; appSpawnContent->timer = NULL; - appSpawnContent->content.runAppSpawn = runAppSpawn; + appSpawnContent->content.runAppSpawn = NULL; appSpawnContent->content.initAppSpawn = NULL; AppSpawnProcessMsg(&appSpawnContent->content, &client->client, &pid); free(appSpawnContent); @@ -265,11 +251,11 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_005, TestSize.Level0) EXPECT_TRUE(appSpawnContent); if (strcpy_s(appSpawnContent->content.longProcName, longProcNameLen, longProcName.c_str()) != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; - }; + } appSpawnContent->content.longProcNameLen = longProcNameLen; appSpawnContent->timer = NULL; - appSpawnContent->content.runAppSpawn = NULL; - appSpawnContent->content.initAppSpawn = NULL; + appSpawnContent->content.runAppSpawn = runAppSpawn; + appSpawnContent->content.initAppSpawn = initAppSpawn; AppSpawnProcessMsg(&appSpawnContent->content, &client->client, &pid); free(appSpawnContent); free(client); diff --git a/test/unittest/client_socket_test/BUILD.gn b/test/unittest/client_socket_test/BUILD.gn index bd99d4e2..06c658fc 100644 --- a/test/unittest/client_socket_test/BUILD.gn +++ b/test/unittest/client_socket_test/BUILD.gn @@ -28,7 +28,7 @@ ohos_unittest("ClientSocketTest") { configs = [ "${appspawn_path}:appspawn_config" ] - deps = [ + deps = [ "${appspawn_path}/test:appspawn_test_source", "//base/startup/init_lite/interfaces/innerkits:libbegetutil", ] -- Gitee From 3bf32a35d29459e3709a86660daa9d0f0737d6f8 Mon Sep 17 00:00:00 2001 From: laiguizhong Date: Mon, 6 Jun 2022 20:28:26 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laiguizhong --- .../app_spawn_standard_test/app_spawn_standard_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp index 3b7a43c2..201e371a 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp @@ -87,7 +87,7 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); -} +}; void AppSpawnStandardTest::SetUpTestCase() {} -- Gitee From ffc0ce15a4275b9bbddf8691306b11232c9e737e Mon Sep 17 00:00:00 2001 From: laiguizhong Date: Wed, 8 Jun 2022 21:14:52 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20ut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laiguizhong --- lite/appspawn_process.c | 2 + standard/appspawn_process.c | 6 +- .../unittest/app_spawn_standard_test/BUILD.gn | 1 + .../app_spawn_standard_test.cpp | 105 ++++++++---------- util/src/sandbox_utils.cpp | 2 + 5 files changed, 56 insertions(+), 60 deletions(-) diff --git a/lite/appspawn_process.c b/lite/appspawn_process.c index c0206f31..bf562f73 100644 --- a/lite/appspawn_process.c +++ b/lite/appspawn_process.c @@ -143,12 +143,14 @@ static int SetCapabilities(struct AppSpawnContent_ *content, AppSpawnClient *cli return -1; } +#ifndef APPSPAWN_TEST unsigned int tmpCaps[] = {17}; // 17 means CAP_SYS_RAWIO unsigned int tmpsCapCnt = sizeof(tmpCaps) / sizeof(tmpCaps[0]); if (SetCapability(tmpsCapCnt, tmpCaps) != 0) { APPSPAWN_LOGE("[appspawn] setrlimit failed, err: %d.", errno); return -1; } +#endif #else if (SetCapability(appProperty->message.capsCnt, appProperty->message.caps) != 0) { APPSPAWN_LOGE("[appspawn] SetCapability failed, err: %d.", errno); diff --git a/standard/appspawn_process.c b/standard/appspawn_process.c index 2d8d49c9..b6834ddd 100644 --- a/standard/appspawn_process.c +++ b/standard/appspawn_process.c @@ -122,6 +122,7 @@ static int SetCapabilities(struct AppSpawnContent_ *content, AppSpawnClient *cli static void InitDebugParams(struct AppSpawnContent_ *content, AppSpawnClient *client) { +#ifndef APPSPAWN_TEST AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client; #ifdef __aarch64__ const char *debugSoPath = "/system/lib64/libhidebug.so"; @@ -139,19 +140,22 @@ static void InitDebugParams(struct AppSpawnContent_ *content, AppSpawnClient *cl APPSPAWN_CHECK(initParam != NULL, dlclose(handle); return, "Failed to dlsym InitEnvironmentParam, %s", dlerror()); (*initParam)(appProperty->property.processName); dlclose(handle); +#endif } static void ClearEnvironment(AppSpawnContent *content, AppSpawnClient *client) { APPSPAWN_LOGI("ClearEnvironment id %d", client->id); - AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client; sigset_t mask; sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigaddset(&mask, SIGTERM); sigprocmask(SIG_UNBLOCK, &mask, NULL); // close child fd +#ifndef APPSPAWN_TEST + AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client; close(appProperty->fd[0]); +#endif InitDebugParams(content, client); return; } diff --git a/test/unittest/app_spawn_standard_test/BUILD.gn b/test/unittest/app_spawn_standard_test/BUILD.gn index 7a0d1ba8..4546567f 100644 --- a/test/unittest/app_spawn_standard_test/BUILD.gn +++ b/test/unittest/app_spawn_standard_test/BUILD.gn @@ -33,6 +33,7 @@ ohos_unittest("AppSpawnStandardTest") { "${appspawn_path}/adapter/appspawn_ace.cpp", "${appspawn_path}/standard/appspawn_process.c", "${appspawn_path}/standard/appspawn_service.c", + "${appspawn_path}/util/src/sandbox_utils.cpp", "app_spawn_standard_test.cpp", ] diff --git a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp index 201e371a..480512fc 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp @@ -14,7 +14,7 @@ */ #include #include -#include +#include #include // redefine private and protected since testcase need to invoke and test private function @@ -44,9 +44,6 @@ namespace OHOS { static void RunChildProcessor(struct AppSpawnContent_ *content, AppSpawnClient *client) {} -static void initAppSpawn(struct AppSpawnContent_ *content) -{} - static void runAppSpawn(struct AppSpawnContent_ *content, int argc, char *const argv[]) {} @@ -104,7 +101,7 @@ void AppSpawnStandardTest::TearDown() HWTEST(AppSpawnStandardTest, App_Spawn_Standard_001, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_001 start"; - string longProcName = "ohos.samples.ecg.default"; + string longProcName = "App_Spawn_Standard_001"; int64_t longProcNameLen = longProcName.length(); int cold = 0; AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", (char*)longProcName.c_str(), longProcNameLen, cold); @@ -118,7 +115,7 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_001, TestSize.Level0) HWTEST(AppSpawnStandardTest, App_Spawn_Standard_002, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_002 start"; - string longProcName = "ohos.samples.ecg.default"; + string longProcName = "App_Spawn_Standard_002"; int64_t longProcNameLen = longProcName.length(); AppSpawnClientExt* client = (AppSpawnClientExt*)malloc(sizeof(AppSpawnClientExt)); @@ -131,13 +128,9 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_002, TestSize.Level0) pid_t pid = 0; AppSpawnContentExt* appSpawnContent = (AppSpawnContentExt*)malloc(sizeof(AppSpawnContentExt)); EXPECT_TRUE(appSpawnContent); - if (strcpy_s(appSpawnContent->content.longProcName, longProcNameLen, longProcName.c_str()) != 0) { - GTEST_LOG_(INFO) << "strcpy_s failed"; - } appSpawnContent->content.longProcNameLen = longProcNameLen; appSpawnContent->timer = NULL; appSpawnContent->content.runAppSpawn = NULL; - appSpawnContent->content.initAppSpawn = NULL; AppSpawnProcessMsg(&appSpawnContent->content, &client->client, &pid); free(appSpawnContent); free(client); @@ -147,53 +140,51 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_002, TestSize.Level0) HWTEST(AppSpawnStandardTest, App_Spawn_Standard_003, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_003 start"; - char longProcName[124] = "ohos.samples.ecg.default"; + char longProcName[124] = "App_Spawn_Standard_003"; int64_t longProcNameLen = 124; // 124 is str length - std::unique_ptr clientExt = std::make_unique(); - - clientExt->client.id = 1; - clientExt->client.flags = 0; - clientExt->fd[0] = 123; - clientExt->fd[1] = 456; - clientExt->property.uid = 10002; - clientExt->property.gid = 1000; - clientExt->property.gidCount = 1; - if (strcpy_s(clientExt->property.processName, APP_LEN_PROC_NAME, "com.ohos.settingsdata") != 0) { - GTEST_LOG_(INFO) << "strcpy_s failed"; - } - if (strcpy_s(clientExt->property.bundleName, APP_LEN_BUNDLE_NAME, "com.ohos.settingsdata") != 0) { + AppSpawnClientExt* client = (AppSpawnClientExt*)malloc(sizeof(AppSpawnClientExt)); + client->client.id = 8; + client->client.flags = 0; + client->fd[0] = 100; + client->fd[1] = 200; + client->property.uid = 10000; + client->property.gid = 1000; + client->property.gidCount = 1; + if (strcpy_s(client->property.bundleName, APP_LEN_BUNDLE_NAME, "xxx.xxx.xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - if (strcpy_s(clientExt->property.soPath, APP_LEN_SO_PATH, "/test") != 0) { + if (strcpy_s(client->property.soPath, APP_LEN_SO_PATH, "xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - clientExt->property.accessTokenId = 671201800; - if (strcpy_s(clientExt->property.apl, APP_APL_MAX_LEN, "system_core") != 0) { + client->property.accessTokenId = 671201800; + if (strcpy_s(client->property.apl, APP_APL_MAX_LEN, "xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - if (strcpy_s(clientExt->property.renderCmd, APP_RENDER_CMD_MAX_LEN, "cmd_test") != 0) { + if (strcpy_s(client->property.renderCmd, APP_RENDER_CMD_MAX_LEN, "xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - clientExt->property.flags = 0; + AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", longProcName, longProcNameLen, 1); content->loadExtendLib = LoadExtendLib; content->runChildProcessor = RunChildProcessor; SetContentFunction(content); - content->clearEnvironment(content, &clientExt->client); - EXPECT_EQ(content->setProcessName(content, &clientExt->client, longProcName, longProcNameLen), 0); - EXPECT_EQ(content->setKeepCapabilities(content, &clientExt->client), 0); - EXPECT_EQ(content->setUidGid(content, &clientExt->client), 0); - EXPECT_EQ(content->setCapabilities(content, &clientExt->client), 0); - content->setAppSandbox(content, &clientExt->client); - content->setAppAccessToken(content, &clientExt->client); - EXPECT_EQ(content->coldStartApp(content, &clientExt->client), 0); - - int ret = ForkChildProc((AppSpawnContent_*)content, &clientExt->client, 0); - EXPECT_EQ(ret, 0); - ForkChildProc((AppSpawnContent_ *)content, &clientExt->client, -1); - DoStartApp(content, &clientExt->client, longProcName, longProcNameLen); - free(content); + + content->clearEnvironment(content, &client->client); + EXPECT_EQ(content->setProcessName(content, &client->client, (char *)longProcName, longProcNameLen), 0); + + EXPECT_EQ(content->setKeepCapabilities(content, &client->client), 0); + + EXPECT_EQ(content->setUidGid(content, &client->client), 0); + + EXPECT_EQ(content->setCapabilities(content, &client->client), 0); + + content->setAppSandbox(content, &client->client); + + content->setAppAccessToken(content, &client->client); + + EXPECT_EQ(content->coldStartApp(content, &client->client), 0); + GTEST_LOG_(INFO) << "App_Spawn_Standard_003 end"; } @@ -203,30 +194,30 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_004, TestSize.Level0) AppSpawnClientExt* client = (AppSpawnClientExt*)malloc(sizeof(AppSpawnClientExt)); client->client.id = 8; client->client.flags = 1; - client->fd[0] = 1; - client->fd[1] = 2; + client->fd[0] = 100; + client->fd[1] = 200; client->property.uid = 10000; client->property.gid = 1000; client->property.gidCount = 1; - if (strcpy_s(client->property.processName, APP_LEN_PROC_NAME, "ohos.samples.ecg") != 0) { + if (strcpy_s(client->property.processName, APP_LEN_PROC_NAME, "xxx.xxx.xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - if (strcpy_s(client->property.bundleName, APP_LEN_BUNDLE_NAME, "ohos.samples.ecg") != 0) { + if (strcpy_s(client->property.bundleName, APP_LEN_BUNDLE_NAME, "xxx.xxx.xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - if (strcpy_s(client->property.soPath, APP_LEN_SO_PATH, "default") != 0) { + if (strcpy_s(client->property.soPath, APP_LEN_SO_PATH, "xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } client->property.accessTokenId = 671201800; - if (strcpy_s(client->property.apl, APP_APL_MAX_LEN, "system_core") != 0) { + if (strcpy_s(client->property.apl, APP_APL_MAX_LEN, "xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } - if (strcpy_s(client->property.renderCmd, APP_RENDER_CMD_MAX_LEN, "test4") != 0) { + if (strcpy_s(client->property.renderCmd, APP_RENDER_CMD_MAX_LEN, "xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } client->property.flags = 0; - char* argv[] = {const_cast("default"), const_cast("ohos.samples.ecg")}; + char* argv[] = {const_cast("xxx"), const_cast("xxx")}; int argc = sizeof(argv)/sizeof(argv[0]); EXPECT_EQ(GetAppSpawnClientFromArg(argc, argv, client), -1); @@ -249,13 +240,9 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_005, TestSize.Level0) pid_t pid = 100; AppSpawnContentExt* appSpawnContent = (AppSpawnContentExt*)malloc(sizeof(AppSpawnContentExt)); EXPECT_TRUE(appSpawnContent); - if (strcpy_s(appSpawnContent->content.longProcName, longProcNameLen, longProcName.c_str()) != 0) { - GTEST_LOG_(INFO) << "strcpy_s failed"; - } appSpawnContent->content.longProcNameLen = longProcNameLen; appSpawnContent->timer = NULL; appSpawnContent->content.runAppSpawn = runAppSpawn; - appSpawnContent->content.initAppSpawn = initAppSpawn; AppSpawnProcessMsg(&appSpawnContent->content, &client->client, &pid); free(appSpawnContent); free(client); @@ -265,20 +252,18 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_005, TestSize.Level0) HWTEST(AppSpawnStandardTest, App_Spawn_Standard_006, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_006 start"; - string longProcName = "ohos.samples.ecg.default"; + string longProcName = "App_Spawn_Standard_006"; int64_t longProcNameLen = longProcName.length(); int cold = 1; AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", (char*)longProcName.c_str(), longProcNameLen, cold); EXPECT_TRUE(content); - content->loadExtendLib = LoadExtendLib; - content->runChildProcessor = RunChildProcessor; char tmp0[] = "/system/bin/appspawn"; char tmp1[] = "cold-start"; char tmp2[] = "1"; char tmp3[] = "1:1:1:1:0:ohos.samples.ecg.default:ohos.samples.ecg:default:671201800:system_core:default"; char * const argv[] = {tmp0, tmp1, tmp2, tmp3}; - content->initAppSpawn(content); + AppSpawnColdRun(content, 4, argv); GTEST_LOG_(INFO) << "App_Spawn_Standard_006 end"; } @@ -305,6 +290,8 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_008, TestSize.Level0) content->setCapabilities = setCapabilities; int ret = DoStartApp((AppSpawnContent_*)content, &clientExt->client, (char*)"", 0); EXPECT_EQ(ret, 0); + + free(content); GTEST_LOG_(INFO) << "App_Spawn_Standard_008 end"; } diff --git a/util/src/sandbox_utils.cpp b/util/src/sandbox_utils.cpp index 3b80bb01..ab1f89ee 100644 --- a/util/src/sandbox_utils.cpp +++ b/util/src/sandbox_utils.cpp @@ -711,12 +711,14 @@ int32_t SandboxUtils::SetAppSandboxProperty(const ClientSocket::AppProperty *app return rc; } +#ifndef APPSPAWN_TEST rc = syscall(SYS_pivot_root, sandboxPackagePath.c_str(), sandboxPackagePath.c_str()); if (rc) { HiLog::Error(LABEL, "pivot root failed, packagename is %{public}s, errno is %{public}d", \ bundleName.c_str(), errno); return rc; } +#endif rc = umount2(".", MNT_DETACH); if (rc) { -- Gitee From e7ba3fd061fe68ba0207c8a0268838dc946c8fdd Mon Sep 17 00:00:00 2001 From: Mupceet Date: Tue, 14 Jun 2022 11:48:01 +0800 Subject: [PATCH 6/7] =?UTF-8?q?ut=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mupceet --- adapter/appspawn_ace.cpp | 4 +- adapter/appspawn_adapter.cpp | 1 + adapter/appspawn_adapter.h | 2 +- adapter/appspawn_nweb.cpp | 2 + adapter/appspawn_sandbox.cpp | 11 +- bundle.json | 2 +- common/appspawn_server.h | 5 + interfaces/innerkits/BUILD.gn | 2 +- .../innerkits/include/appspawn_service.h | 37 ----- lite/appspawn_message.h | 10 +- lite/main.c | 1 - standard/appspawn_service.c | 34 +++-- test/BUILD.gn | 52 ------- test/unittest/BUILD.gn | 94 +++++++++++++ .../app_spawn_lite_test.cpp | 1 - test/unittest/app_spawn_socket_test/BUILD.gn | 39 ------ .../unittest/app_spawn_standard_test/BUILD.gn | 61 --------- .../app_spawn_standard_test.cpp | 128 +++++++++++++++--- test/unittest/client_socket_test/BUILD.gn | 43 ------ util/src/json_utils.cpp | 1 + 20 files changed, 248 insertions(+), 282 deletions(-) delete mode 100644 interfaces/innerkits/include/appspawn_service.h create mode 100755 test/unittest/BUILD.gn delete mode 100644 test/unittest/app_spawn_socket_test/BUILD.gn delete mode 100644 test/unittest/app_spawn_standard_test/BUILD.gn delete mode 100644 test/unittest/client_socket_test/BUILD.gn diff --git a/adapter/appspawn_ace.cpp b/adapter/appspawn_ace.cpp index 65907bba..7bb95d58 100644 --- a/adapter/appspawn_ace.cpp +++ b/adapter/appspawn_ace.cpp @@ -24,10 +24,12 @@ void LoadExtendLib(AppSpawnContent *content) #else const char *acelibdir("/system/lib/libace.z.so"); #endif - void *AceAbilityLib = nullptr; APPSPAWN_LOGI("MainThread::LoadAbilityLibrary. Start calling dlopen acelibdir."); +#ifndef APPSPAWN_TEST + void *AceAbilityLib = NULL; AceAbilityLib = dlopen(acelibdir, RTLD_NOW | RTLD_GLOBAL); APPSPAWN_CHECK(AceAbilityLib != NULL, return, "Fail to dlopen %s, [%s]", acelibdir, dlerror()); +#endif APPSPAWN_LOGI("Success to dlopen %s", acelibdir); APPSPAWN_LOGI("MainThread::LoadAbilityLibrary. End calling dlopen"); } diff --git a/adapter/appspawn_adapter.cpp b/adapter/appspawn_adapter.cpp index 74a6e0d6..a7da57d7 100644 --- a/adapter/appspawn_adapter.cpp +++ b/adapter/appspawn_adapter.cpp @@ -17,6 +17,7 @@ #include +#include "appspawn_service.h" #include "token_setproc.h" #ifdef WITH_SELINUX #include "hap_restorecon.h" diff --git a/adapter/appspawn_adapter.h b/adapter/appspawn_adapter.h index 9925724d..bf0928d4 100644 --- a/adapter/appspawn_adapter.h +++ b/adapter/appspawn_adapter.h @@ -16,7 +16,7 @@ #ifndef APPSPAWN_ADPATER_CPP #define APPSPAWN_ADPATER_CPP -#include "appspawn_service.h" +#include "appspawn_server.h" #include diff --git a/adapter/appspawn_nweb.cpp b/adapter/appspawn_nweb.cpp index c76354ee..21c1db96 100644 --- a/adapter/appspawn_nweb.cpp +++ b/adapter/appspawn_nweb.cpp @@ -21,6 +21,8 @@ #include #include +#include "appspawn_service.h" + struct RenderProcessNode { RenderProcessNode(time_t now, int exit):recordTime_(now), exitStatus_(exit) {} time_t recordTime_; diff --git a/adapter/appspawn_sandbox.cpp b/adapter/appspawn_sandbox.cpp index 51fe065a..4d852000 100644 --- a/adapter/appspawn_sandbox.cpp +++ b/adapter/appspawn_sandbox.cpp @@ -27,6 +27,7 @@ #include #include +#include "appspawn_service.h" #include "json_utils.h" #include "sandbox_utils.h" #include "hilog/log.h" @@ -52,24 +53,18 @@ void LoadAppSandboxConfig(void) { // load sandbox config nlohmann::json appSandboxConfig; - bool rc = JsonUtils::GetJsonObjFromJson(appSandboxConfig, APP_JSON_CONFIG); - if (!rc) { - APPSPAWN_LOGE("AppSpawnServer::Failed to load app private sandbox config"); - } + APPSPAWN_CHECK_ONLY_LOG(rc, "AppSpawnServer::Failed to load app private sandbox config"); SandboxUtils::StoreJsonConfig(appSandboxConfig); rc = JsonUtils::GetJsonObjFromJson(appSandboxConfig, PRODUCT_JSON_CONFIG); - if (!rc) { - APPSPAWN_LOGE("AppSpawnServer::Failed to load app product sandbox config"); - } + APPSPAWN_CHECK_ONLY_LOG(rc, "AppSpawnServer::Failed to load app product sandbox config"); SandboxUtils::StoreProductJsonConfig(appSandboxConfig); } int32_t SetAppSandboxProperty(struct AppSpawnContent_ *content, AppSpawnClient *client) { APPSPAWN_CHECK(client != NULL, return -1, "Invalid appspwn client"); - AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client; return SandboxUtils::SetAppSandboxProperty(&appProperty->property); } diff --git a/bundle.json b/bundle.json index db9a6a01..939b3581 100644 --- a/bundle.json +++ b/bundle.json @@ -59,7 +59,7 @@ ], "test": [ "//base/startup/appspawn/test:moduletest", - "//base/startup/appspawn/test:unittest" + "//base/startup/appspawn/test/unittest:unittest" ] } } diff --git a/common/appspawn_server.h b/common/appspawn_server.h index fa918a12..f91bc28f 100644 --- a/common/appspawn_server.h +++ b/common/appspawn_server.h @@ -110,6 +110,11 @@ void AppspawnLogPrint(AppspawnLogLevel logLevel, const char *file, int line, con exper; \ } +#define APPSPAWN_CHECK_ONLY_LOG(retCode, ...) \ + if (!(retCode)) { \ + APPSPAWN_LOGE(__VA_ARGS__); \ + } + #ifdef __cplusplus } #endif diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 91f519ac..4df8333c 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -26,7 +26,7 @@ ohos_static_library("appspawn_socket_client") { ] include_dirs = [ "include", - "//base/startup/appspawn_standard/common", + "//base/startup/appspawn/common", ] public_configs = [ ":exported_header_files" ] diff --git a/interfaces/innerkits/include/appspawn_service.h b/interfaces/innerkits/include/appspawn_service.h deleted file mode 100644 index 16068f38..00000000 --- a/interfaces/innerkits/include/appspawn_service.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2021-2022 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. - */ -#ifndef BASE_STARTUP_APPSPAWN_SERVICE_H -#define BASE_STARTUP_APPSPAWN_SERVICE_H - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#define APPSPAWN_SERVICE_NAME "appspawn" - -enum APPSPAWN_FUNCID { - ID_CALL_CREATE_SERVICE = 0, - ID_CALL_BUT -}; - -#ifdef __cplusplus -#if __cplusplus -} -#endif -#endif - -#endif // BASE_STARTUP_APPSPAWN_SERVICE_H \ No newline at end of file diff --git a/lite/appspawn_message.h b/lite/appspawn_message.h index e7a1ab2e..0e3739ae 100644 --- a/lite/appspawn_message.h +++ b/lite/appspawn_message.h @@ -18,7 +18,6 @@ #include #include "appspawn_server.h" -#include "appspawn_service.h" #ifdef __cplusplus #if __cplusplus @@ -26,6 +25,13 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ +#define APPSPAWN_SERVICE_NAME "appspawn" + +enum APPSPAWN_FUNCID { + ID_CALL_CREATE_SERVICE = 0, + ID_CALL_BUT +}; + typedef struct { char *bundleName; char *identityID; @@ -54,4 +60,4 @@ void FreeMessageSt(MessageSt *targetSt); #endif #endif -#endif // BASE_STARTUP_APPSPAWN_SERVICE_H +#endif // BASE_STARTUP_APPSPAWN_MESSAGE_H diff --git a/lite/main.c b/lite/main.c index 3c6592fc..df227140 100644 --- a/lite/main.c +++ b/lite/main.c @@ -20,7 +20,6 @@ #include "samgr_lite.h" #include "appspawn_server.h" -#include "appspawn_service.h" void __attribute__((weak)) HOS_SystemInit(void) { diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index 3a8be9f0..5ebcfa62 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -41,20 +41,20 @@ static AppSpawnContentExt *g_appSpawnContent = NULL; static const int TV_SEC = 60; -static int AppInfoHashNodeCompare(const HashNode *node1, const HashNode *node2) +APPSPAWN_STATIC int AppInfoHashNodeCompare(const HashNode *node1, const HashNode *node2) { AppInfo *testNode1 = HASHMAP_ENTRY(node1, AppInfo, node); AppInfo *testNode2 = HASHMAP_ENTRY(node2, AppInfo, node); return testNode1->pid - testNode2->pid; } -static int TestHashKeyCompare(const HashNode *node1, const void *key) +APPSPAWN_STATIC int TestHashKeyCompare(const HashNode *node1, const void *key) { AppInfo *testNode1 = HASHMAP_ENTRY(node1, AppInfo, node); return testNode1->pid - *(pid_t *)key; } -static int AppInfoHashNodeFunction(const HashNode *node) +APPSPAWN_STATIC int AppInfoHashNodeFunction(const HashNode *node) { AppInfo *testNode = HASHMAP_ENTRY(node, AppInfo, node); if (testNode == NULL) { @@ -63,20 +63,20 @@ static int AppInfoHashNodeFunction(const HashNode *node) return testNode->pid % APP_HASH_BUTT; } -static int AppInfoHashKeyFunction(const void *key) +APPSPAWN_STATIC int AppInfoHashKeyFunction(const void *key) { pid_t code = *(pid_t *)key; return code % APP_HASH_BUTT; } -static void AppInfoHashNodeFree(const HashNode *node) +APPSPAWN_STATIC void AppInfoHashNodeFree(const HashNode *node) { AppInfo *testNode = HASHMAP_ENTRY(node, AppInfo, node); APPSPAWN_LOGI("AppInfoHashNodeFree %s\n", testNode->name); free(testNode); } -static void AddAppInfo(pid_t pid, const char *processName) +APPSPAWN_STATIC void AddAppInfo(pid_t pid, const char *processName) { size_t len = strlen(processName) + 1; AppInfo *node = (AppInfo *)malloc(sizeof(AppInfo) + len + 1); @@ -99,7 +99,7 @@ static void ProcessTimer(const TimerHandle taskHandle, void *context) LE_StopLoop(LE_GetDefaultLoop()); } -static void RemoveAppInfo(pid_t pid) +APPSPAWN_STATIC void RemoveAppInfo(pid_t pid) { HashNode *node = HashMapGet(g_appSpawnContent->appMap, (const void *)&pid); APPSPAWN_CHECK(node != NULL, return, "Invalid node %d", pid); @@ -169,7 +169,7 @@ static void PrintProcessExitInfo(pid_t pid, uid_t uid, int status) } #endif -static void SignalHandler(const struct signalfd_siginfo *siginfo) +APPSPAWN_STATIC void SignalHandler(const struct signalfd_siginfo *siginfo) { APPSPAWN_LOGI("SignalHandler signum %d", siginfo->ssi_signo); switch (siginfo->ssi_signo) { @@ -244,7 +244,7 @@ static int WaitChild(int fd, int pid, const AppSpawnClientExt *appProperty) return result; } -static void StartColdApp(AppSpawnClientExt *appProperty) +static void CheckColdAppEnabled(AppSpawnClientExt *appProperty) { if (appProperty == NULL) { return; @@ -299,7 +299,7 @@ static void GetProcessTerminationStatus(AppSpawnClientExt *appProperty) } #endif -static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen) +APPSPAWN_STATIC void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen) { APPSPAWN_CHECK(buffer != NULL && buffLen >= sizeof(AppParameter), LE_CloseTask(LE_GetDefaultLoop(), taskHandle); return, "Invalid buffer buffLen %u", buffLen); @@ -326,7 +326,7 @@ static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, g_appSpawnContent->timer = NULL; } - StartColdApp(appProperty); + CheckColdAppEnabled(appProperty); // create pipe for commication from child if (pipe(appProperty->fd) == -1) { APPSPAWN_LOGE("create pipe fail, errno = %d", errno); @@ -355,6 +355,9 @@ static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, } } +#ifdef APPSPAWN_TEST +TaskHandle g_testClientHandle = NULL; +#endif APPSPAWN_STATIC int OnConnection(const LoopHandle loopHandle, const TaskHandle server) { static uint32_t clientId = 0; @@ -364,6 +367,9 @@ APPSPAWN_STATIC int OnConnection(const LoopHandle loopHandle, const TaskHandle s TaskHandle stream; LE_StreamInfo info = {}; info.baseInfo.flags = TASK_STREAM | TASK_PIPE | TASK_CONNECT; +#ifdef APPSPAWN_TEST + info.baseInfo.flags |= TASK_TEST; +#endif info.baseInfo.close = OnClose; info.baseInfo.userDataSize = sizeof(AppSpawnClientExt); info.disConntectComplete = NULL; @@ -377,6 +383,9 @@ APPSPAWN_STATIC int OnConnection(const LoopHandle loopHandle, const TaskHandle s client->client.id = ++clientId; client->client.flags = 0; APPSPAWN_LOGI("OnConnection client fd %d Id %d", LE_GetSocketFd(stream), client->client.id); +#ifdef APPSPAWN_TEST + g_testClientHandle = stream; +#endif return 0; } @@ -400,6 +409,7 @@ static void AppSpawnInit(AppSpawnContent *content) if (content->loadExtendLib) { content->loadExtendLib(content); } + content->notifyResToParent = NotifyResToParent; // set private function SetContentFunction(content); @@ -431,7 +441,7 @@ void AppSpawnColdRun(AppSpawnContent *content, int argc, char *const argv[]) g_appSpawnContent = NULL; } -APPSPAWN_STATIC void AppSpawnRun(AppSpawnContent *content, int argc, char *const argv[]) +static void AppSpawnRun(AppSpawnContent *content, int argc, char *const argv[]) { APPSPAWN_LOGI("AppSpawnRun"); AppSpawnContentExt *appSpawnContent = (AppSpawnContentExt *)content; diff --git a/test/BUILD.gn b/test/BUILD.gn index 2ec39d88..54b6beea 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -14,59 +14,7 @@ import("//base/startup/appspawn/appspawn.gni") import("//build/test.gni") -group("unittest") { - testonly = true - - deps = [ - "unittest/app_spawn_socket_test:unittest", - "unittest/app_spawn_standard_test:unittest", - "unittest/client_socket_test:unittest", - ] -} - group("moduletest") { testonly = true - deps = [ "moduletest:moduletest" ] } - -config("appspawn_test_config") { - configs = [] - - defines = [ "TEST_EXIT" ] - - include_dirs = [ - "${appspawn_path}/test/mock/include", - "${appspawn_path}/common", - "${appspawn_path}/interfaces/innerkits/include", - "${appspawn_path}/util/include", - ] -} - -ohos_source_set("appspawn_test_source") { - testonly = true - - sources = [] - - include_dirs = [ "//third_party/json/include" ] - - public_configs = [ - "//utils/native/base:utils_config", - ":appspawn_test_config", - ] - - public_deps = [ - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - "//utils/native/base:utils", - ] - - deps = [] - - external_deps = [ - "ability_base:want", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "hiviewdfx_hilog_native:libhilog", - ] -} diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn new file mode 100755 index 00000000..f2d29f6f --- /dev/null +++ b/test/unittest/BUILD.gn @@ -0,0 +1,94 @@ +import("//base/startup/appspawn/appspawn.gni") +import("//build/test.gni") + +config("utest_config") { + cflags = [ + "-fprofile-arcs", + "-ftest-coverage", + "-Wno-implicit-fallthrough", + "-Wno-unused-function", + ] + cflags_cc = [ + "-Wno-implicit-fallthrough", + "-fexceptions", + ] + + ldflags = [ "--coverage" ] + + include_dirs = [ + "${appspawn_path}/test/mock/include", + "${appspawn_path}/common", + "${appspawn_path}/standard", + "${appspawn_path}/adapter", + "${appspawn_path}/interfaces/innerkits/include", + "${appspawn_path}/util/include", + "//utils/native/base/include", + "${aafwk_path}/frameworks/kits/appkit/native/app/include", + "${aafwk_path}/interfaces/innerkits/app_manager/include/appmgr", + "${aafwk_path}/interfaces/innerkits/ability_manager/include", + "${aafwk_path}/frameworks/kits/ability/native/include", + "${aafwk_path}/services/abilitymgr/include", + "//base/global/resource_management/interfaces/inner_api/include", + "//base/security/access_token/interfaces/innerkits/token_setproc/include", + "//base/startup/init_lite/services/log", + "//base/startup/init_lite/services/include", + "//base/startup/init_lite/services/loopevent/include", + "//base/startup/init_lite/interfaces/innerkits/include", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include", + "//third_party/json/include", + ] +} + +ohos_unittest("AppSpawn_ut") { + module_out_path = "${module_output_path}" + + defines = [ + "usleep(time) = MockSleep(time)", + "APPSPAWN_TEST", + ] + + sources = [ + "${appspawn_path}/adapter/appspawn_ace.cpp", + "${appspawn_path}/adapter/appspawn_adapter.cpp", + "${appspawn_path}/adapter/appspawn_sandbox.cpp", + "${appspawn_path}/common/appspawn_server.c", + "${appspawn_path}/interfaces/innerkits/client/appspawn_socket.cpp", + "${appspawn_path}/interfaces/innerkits/client/client_socket.cpp", + "${appspawn_path}/util/src/json_utils.cpp", + "${appspawn_path}/util/src/sandbox_utils.cpp", + "${appspawn_path}/standard/appspawn_process.c", + "${appspawn_path}/standard/appspawn_service.c", + ] + + sources += [ + "${appspawn_path}/test/unittest/app_spawn_socket_test/app_spawn_socket_test.cpp", + "${appspawn_path}/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp", + "${appspawn_path}/test/unittest/client_socket_test/client_socket_test.cpp", + ] + + configs = [ "//base/startup/appspawn/test/unittest:utest_config" ] + + deps = [ + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", + "${aafwk_kits_path}/appkit:appkit_native", + ] + + external_deps = [ + "init:libbegetutil", + "ability_base:want", + "ability_runtime:app_manager", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "utils_base:utils", + ] +} + +group("unittest") { + testonly = true + + deps = [ ":AppSpawn_ut" ] +} diff --git a/test/unittest/app_spawn_lite_test/app_spawn_lite_test.cpp b/test/unittest/app_spawn_lite_test/app_spawn_lite_test.cpp index 72d54c75..b1af6cd7 100644 --- a/test/unittest/app_spawn_lite_test/app_spawn_lite_test.cpp +++ b/test/unittest/app_spawn_lite_test/app_spawn_lite_test.cpp @@ -21,7 +21,6 @@ #include #include "gtest/gtest.h" #include "appspawn_message.h" -#include "appspawn_service.h" #include "appspawn_msg.h" using namespace testing::ext; diff --git a/test/unittest/app_spawn_socket_test/BUILD.gn b/test/unittest/app_spawn_socket_test/BUILD.gn deleted file mode 100644 index d5bd380e..00000000 --- a/test/unittest/app_spawn_socket_test/BUILD.gn +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//base/startup/appspawn/appspawn.gni") -import("//build/test.gni") - -ohos_unittest("AppSpawnSocketTest") { - module_out_path = "${module_output_path}" - - sources = - [ "${appspawn_path}/interfaces/innerkits/client/appspawn_socket.cpp" ] - - sources += [ "app_spawn_socket_test.cpp" ] - - configs = [ "${appspawn_path}:appspawn_config" ] - - deps = [ - "${appspawn_path}/test:appspawn_test_source", - "//base/startup/init_lite/interfaces/innerkits:libbegetutil", - ] - - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] -} - -group("unittest") { - testonly = true - - deps = [ ":AppSpawnSocketTest" ] -} diff --git a/test/unittest/app_spawn_standard_test/BUILD.gn b/test/unittest/app_spawn_standard_test/BUILD.gn deleted file mode 100644 index dd6b5443..00000000 --- a/test/unittest/app_spawn_standard_test/BUILD.gn +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//base/startup/appspawn/appspawn.gni") -import("//build/test.gni") - -ohos_unittest("AppSpawnStandardTest") { - module_out_path = "${module_output_path}" - - defines = [ - "usleep(time) = MockSleep(time)", - "APPSPAWN_TEST", - ] - - include_dirs = [ - "${appspawn_path}/standard/", - "${appspawn_path}/common/", - ] - - configs = [ "${appspawn_path}:appspawn_config" ] - - sources = [ - "${appspawn_path}/adapter/appspawn_ace.cpp", - "${appspawn_path}/standard/appspawn_process.c", - "${appspawn_path}/standard/appspawn_service.c", - "${appspawn_path}/util/src/sandbox_utils.cpp", - "app_spawn_standard_test.cpp", - ] - - deps = [ - "${aafwk_kits_path}/appkit:appkit_native", - "${appspawn_path}:appspawn_server", - "${appspawn_path}/test:appspawn_test_source", - "//base/startup/init_lite/interfaces/innerkits:libbegetutil", - ] - - external_deps = [ - "ability_base:want", - "ability_runtime:app_manager", - "eventhandler:libeventhandler", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "utils_base:utils", - ] -} - -group("unittest") { - testonly = true - - deps = [ ":AppSpawnStandardTest" ] -} diff --git a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp index 480512fc..dbc6d40b 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp @@ -17,33 +17,41 @@ #include #include -// redefine private and protected since testcase need to invoke and test private function -#define private public -#define protected public -#include "appspawn_service.h" -#undef private -#undef protected - #include "securec.h" #include "appspawn_adapter.h" #include "appspawn_server.h" +#include "appspawn_service.h" +#include "json_utils.h" +#include "init_hashmap.h" +#include "loop_event.h" using namespace testing; using namespace testing::ext; +using namespace OHOS::AppSpawn; +using nlohmann::json; #ifdef __cplusplus extern "C" { #endif - int OnConnection(const LoopHandle loopHandle, const TaskHandle server); - void AppSpawnRun(AppSpawnContent *content, int argc, char *const argv[]); +int OnConnection(const LoopHandle loopHandle, const TaskHandle server); +int AppInfoHashNodeCompare(const HashNode *node1, const HashNode *node2); +int AppInfoHashNodeFunction(const HashNode *node); +int AppInfoHashKeyFunction(const void *key); +void AppInfoHashNodeFree(const HashNode *node); +int TestHashKeyCompare(const HashNode *node1, const void *key); +void AddAppInfo(pid_t pid, const char *processName); +void RemoveAppInfo(pid_t pid); +void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen); +extern TaskHandle g_testClientHandle; #ifdef __cplusplus } #endif namespace OHOS { +/* static void RunChildProcessor(struct AppSpawnContent_ *content, AppSpawnClient *client) {} - +*/ static void runAppSpawn(struct AppSpawnContent_ *content, int argc, char *const argv[]) {} @@ -78,6 +86,21 @@ static int setCapabilities(struct AppSpawnContent_ *content, AppSpawnClient *cli return 0; } +static AppInfo *TestCreateHashNode(const char *value, int pid) +{ + AppInfo *node = (AppInfo *)malloc(sizeof(AppInfo) + strlen(value) + 1); + if (node == nullptr) { + return nullptr; + } + node->pid = pid; + int ret = strcpy_s(node->name, strlen(value) + 1, value); + if (ret != 0) { + free(node); + return nullptr; + } + return node; +} + class AppSpawnStandardTest : public testing::Test { public: static void SetUpTestCase(); @@ -108,7 +131,8 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_001, TestSize.Level0) EXPECT_TRUE(content); content->loadExtendLib = LoadExtendLib; content->runChildProcessor = RunChildProcessor; - AppSpawnRun(content, 0, nullptr); + content->initAppSpawn(content); + content->runAppSpawn(content, 0, nullptr); GTEST_LOG_(INFO) << "App_Spawn_Standard_001 end"; } @@ -131,7 +155,9 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_002, TestSize.Level0) appSpawnContent->content.longProcNameLen = longProcNameLen; appSpawnContent->timer = NULL; appSpawnContent->content.runAppSpawn = NULL; + AppSpawnProcessMsg(&appSpawnContent->content, &client->client, &pid); + free(appSpawnContent); free(client); GTEST_LOG_(INFO) << "App_Spawn_Standard_002 end"; @@ -150,6 +176,9 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_003, TestSize.Level0) client->property.uid = 10000; client->property.gid = 1000; client->property.gidCount = 1; + if (strcpy_s(client->property.processName, APP_LEN_PROC_NAME, "xxx.xxx.xxx") != 0) { + GTEST_LOG_(INFO) << "strcpy_s failed"; + } if (strcpy_s(client->property.bundleName, APP_LEN_BUNDLE_NAME, "xxx.xxx.xxx") != 0) { GTEST_LOG_(INFO) << "strcpy_s failed"; } @@ -167,8 +196,9 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_003, TestSize.Level0) AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", longProcName, longProcNameLen, 1); content->loadExtendLib = LoadExtendLib; content->runChildProcessor = RunChildProcessor; - SetContentFunction(content); + EXPECT_EQ(ForkChildProc(content, &client->client, 0), 0); + EXPECT_NE(ForkChildProc(content, &client->client, -1), 0); content->clearEnvironment(content, &client->client); EXPECT_EQ(content->setProcessName(content, &client->client, (char *)longProcName, longProcNameLen), 0); @@ -257,7 +287,10 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_006, TestSize.Level0) int cold = 1; AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", (char*)longProcName.c_str(), longProcNameLen, cold); EXPECT_TRUE(content); + content->loadExtendLib = LoadExtendLib; + content->runChildProcessor = RunChildProcessor; + content->runChildProcessor(content, nullptr); char tmp0[] = "/system/bin/appspawn"; char tmp1[] = "cold-start"; char tmp2[] = "1"; @@ -268,13 +301,14 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_006, TestSize.Level0) GTEST_LOG_(INFO) << "App_Spawn_Standard_006 end"; } +/* HWTEST(AppSpawnStandardTest, App_Spawn_Standard_007, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_007 start"; RunChildProcessor(nullptr, nullptr); GTEST_LOG_(INFO) << "App_Spawn_Standard_007 end"; } - +*/ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_008, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_008 start"; @@ -282,12 +316,15 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_008, TestSize.Level0) int64_t longProcNameLen = longProcName.length(); std::unique_ptr clientExt = std::make_unique(); AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", (char*)longProcName.c_str(), longProcNameLen, 1); + content->loadExtendLib = LoadExtendLib; + content->runChildProcessor = RunChildProcessor; content->setAppSandbox = setAppSandbox; content->setKeepCapabilities = setKeepCapabilities; content->setProcessName = setProcessName; content->setUidGid = setUidGid; content->setFileDescriptors = setFileDescriptors; content->setCapabilities = setCapabilities; + int ret = DoStartApp((AppSpawnContent_*)content, &clientExt->client, (char*)"", 0); EXPECT_EQ(ret, 0); @@ -295,18 +332,65 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_008, TestSize.Level0) GTEST_LOG_(INFO) << "App_Spawn_Standard_008 end"; } +static int TestClient(int cold, AppOperateType code) +{ + char buffer[64] = {0}; // 64 buffer size + AppSpawnContentExt *content = (AppSpawnContentExt *)AppSpawnCreateContent("AppSpawn", buffer, sizeof(buffer), cold); + if (content == NULL) { + return -1; + } + // create connection + OnConnection(LE_GetDefaultLoop(), content->server); + + // process recv message + if (g_testClientHandle == nullptr) { + free(content); + return -1; + } + + AppParameter property = {}; + property.uid = 100; + property.gid = 100; + property.gidCount = 1; + property.gidTable[0] = 101; + const std::string name = "wwwwwwwwwwwww"; + (void)strcpy_s(property.processName, sizeof(property.processName), name.c_str()); + (void)strcpy_s(property.bundleName, sizeof(property.bundleName), name.c_str()); + (void)strcpy_s(property.renderCmd, sizeof(property.renderCmd), name.c_str()); + (void)strcpy_s(property.soPath, sizeof(property.soPath), name.c_str()); + (void)strcpy_s(property.apl, sizeof(property.apl), "system_core"); + property.flags = 0; + property.code = code; + property.accessTokenId = 0; + OnReceiveRequest(g_testClientHandle, (const uint8_t *)&property, sizeof(property)); + LE_CloseTask(LE_GetDefaultLoop(), g_testClientHandle); + free(content); + return 0; +} + HWTEST(AppSpawnStandardTest, App_Spawn_Standard_009, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_009 start"; - TaskHandle server = (TaskHandle)malloc(sizeof(TaskHandle)); - EXPECT_TRUE(server); - int ret = OnConnection(nullptr, server); - EXPECT_EQ(ret, -1); - server->flags = 0; - OnConnection(nullptr, server); - server->flags = 1; - OnConnection(nullptr, server); - free(server); + //int ret = TestClient(0, DEFAULT); + //EXPECT_EQ(ret, 0); GTEST_LOG_(INFO) << "App_Spawn_Standard_009 end"; } + +HWTEST(AppSpawnStandardTest, App_Spawn_Standard_010, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "App_Spawn_Standard_010 start"; + const char *str1 = "Test hash map node 1"; + const char *str2 = "Test hash map node 2"; + AppInfo *node1 = TestCreateHashNode(str1, 11); + AppInfo *node2 = TestCreateHashNode(str2, 12); + + AppInfoHashNodeCompare((const HashNode *)node1, (const HashNode *)node2); + int value = 13; + TestHashKeyCompare((const HashNode *)node1, &value); + AppInfoHashNodeFunction((const HashNode *)node1); + AppInfoHashKeyFunction(&value); + AppInfoHashNodeFree((const HashNode *)node1); + AppInfoHashNodeFree((const HashNode *)node2); + GTEST_LOG_(INFO) << "App_Spawn_Standard_010 end"; +} } // namespace OHOS diff --git a/test/unittest/client_socket_test/BUILD.gn b/test/unittest/client_socket_test/BUILD.gn deleted file mode 100644 index abd50b5a..00000000 --- a/test/unittest/client_socket_test/BUILD.gn +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//base/startup/appspawn/appspawn.gni") -import("//build/test.gni") - -ohos_unittest("ClientSocketTest") { - module_out_path = "${module_output_path}" - - defines = [ "usleep(time) = MockSleep(time)" ] - - sources = [ - "${appspawn_path}/interfaces/innerkits/client/appspawn_socket.cpp", - "${appspawn_path}/interfaces/innerkits/client/client_socket.cpp", - ] - - sources += [ "client_socket_test.cpp" ] - - configs = [ "${appspawn_path}:appspawn_config" ] - - deps = [ - "${appspawn_path}/test:appspawn_test_source", - "//base/startup/init_lite/interfaces/innerkits:libbegetutil", - ] - - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] -} - -group("unittest") { - testonly = true - - deps = [ ":ClientSocketTest" ] -} diff --git a/util/src/json_utils.cpp b/util/src/json_utils.cpp index 37111e28..650df654 100644 --- a/util/src/json_utils.cpp +++ b/util/src/json_utils.cpp @@ -21,6 +21,7 @@ using namespace std; using namespace OHOS; using namespace OHOS::HiviewDFX; +using nlohmann::json; static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "AppSpawn_JsonUtil"}; namespace OHOS { -- Gitee From 1079fe1fd00d7b1e1dde9ae444aefe0cd70a4ff1 Mon Sep 17 00:00:00 2001 From: Mupceet Date: Tue, 14 Jun 2022 12:48:15 +0800 Subject: [PATCH 7/7] =?UTF-8?q?ut=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mupceet --- common/appspawn_server.c | 2 + lite/appspawn_message.h | 8 +--- lite/appspawn_service.h | 37 +++++++++++++++++++ lite/main.c | 1 + standard/appspawn_service.c | 4 ++ .../app_spawn_standard_test.cpp | 15 ++++---- 6 files changed, 52 insertions(+), 15 deletions(-) create mode 100755 lite/appspawn_service.h diff --git a/common/appspawn_server.c b/common/appspawn_server.c index 0f09e9ca..726f7f26 100644 --- a/common/appspawn_server.c +++ b/common/appspawn_server.c @@ -115,7 +115,9 @@ int ForkChildProc(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_ int ret = -1; if (client->flags & APP_COLD_START) { if (content->coldStartApp != NULL && content->coldStartApp(content, client) == 0) { +#ifndef APPSPAWN_TEST _exit(0x7f); // 0x7f user exit +#endif return -1; } else { ret = DoStartApp(content, client, content->longProcName, content->longProcNameLen); diff --git a/lite/appspawn_message.h b/lite/appspawn_message.h index 0e3739ae..718a4453 100644 --- a/lite/appspawn_message.h +++ b/lite/appspawn_message.h @@ -18,6 +18,7 @@ #include #include "appspawn_server.h" +#include "appspawn_service.h" #ifdef __cplusplus #if __cplusplus @@ -25,13 +26,6 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -#define APPSPAWN_SERVICE_NAME "appspawn" - -enum APPSPAWN_FUNCID { - ID_CALL_CREATE_SERVICE = 0, - ID_CALL_BUT -}; - typedef struct { char *bundleName; char *identityID; diff --git a/lite/appspawn_service.h b/lite/appspawn_service.h new file mode 100755 index 00000000..ee8914fa --- /dev/null +++ b/lite/appspawn_service.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 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. + */ +#ifndef BASE_STARTUP_APPSPAWN_SERVICE_H +#define BASE_STARTUP_APPSPAWN_SERVICE_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#define APPSPAWN_SERVICE_NAME "appspawn" + +enum APPSPAWN_FUNCID { + ID_CALL_CREATE_SERVICE = 0, + ID_CALL_BUT +}; + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif + +#endif // BASE_STARTUP_APPSPAWN_SERVICE_H diff --git a/lite/main.c b/lite/main.c index df227140..3c6592fc 100644 --- a/lite/main.c +++ b/lite/main.c @@ -20,6 +20,7 @@ #include "samgr_lite.h" #include "appspawn_server.h" +#include "appspawn_service.h" void __attribute__((weak)) HOS_SystemInit(void) { diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index 5ebcfa62..20533797 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -39,7 +39,11 @@ static AppSpawnContentExt *g_appSpawnContent = NULL; +#ifdef APPSPAWN_TEST +static const int TV_SEC = 1; +#else static const int TV_SEC = 60; +#endif APPSPAWN_STATIC int AppInfoHashNodeCompare(const HashNode *node1, const HashNode *node2) { diff --git a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp index dbc6d40b..7f8b01e0 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_standard_test.cpp @@ -332,7 +332,7 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_008, TestSize.Level0) GTEST_LOG_(INFO) << "App_Spawn_Standard_008 end"; } -static int TestClient(int cold, AppOperateType code) +static int TestClient(int cold, AppOperateType code, const std::string &processName) { char buffer[64] = {0}; // 64 buffer size AppSpawnContentExt *content = (AppSpawnContentExt *)AppSpawnCreateContent("AppSpawn", buffer, sizeof(buffer), cold); @@ -353,11 +353,10 @@ static int TestClient(int cold, AppOperateType code) property.gid = 100; property.gidCount = 1; property.gidTable[0] = 101; - const std::string name = "wwwwwwwwwwwww"; - (void)strcpy_s(property.processName, sizeof(property.processName), name.c_str()); - (void)strcpy_s(property.bundleName, sizeof(property.bundleName), name.c_str()); - (void)strcpy_s(property.renderCmd, sizeof(property.renderCmd), name.c_str()); - (void)strcpy_s(property.soPath, sizeof(property.soPath), name.c_str()); + (void)strcpy_s(property.processName, sizeof(property.processName), processName.c_str()); + (void)strcpy_s(property.bundleName, sizeof(property.bundleName), processName.c_str()); + (void)strcpy_s(property.renderCmd, sizeof(property.renderCmd), processName.c_str()); + (void)strcpy_s(property.soPath, sizeof(property.soPath), processName.c_str()); (void)strcpy_s(property.apl, sizeof(property.apl), "system_core"); property.flags = 0; property.code = code; @@ -371,8 +370,8 @@ static int TestClient(int cold, AppOperateType code) HWTEST(AppSpawnStandardTest, App_Spawn_Standard_009, TestSize.Level0) { GTEST_LOG_(INFO) << "App_Spawn_Standard_009 start"; - //int ret = TestClient(0, DEFAULT); - //EXPECT_EQ(ret, 0); + int ret = TestClient(0, DEFAULT, "ohos.test.testapp"); + EXPECT_EQ(ret, 0); GTEST_LOG_(INFO) << "App_Spawn_Standard_009 end"; } -- Gitee