codecheck fix

Signed-off-by: 王达 <wangda20@huawei.com>
This commit is contained in:
王达 2024-04-03 20:06:23 +08:00
parent 7e09215ace
commit 37929fc18c
12 changed files with 232 additions and 246 deletions

View File

@ -141,56 +141,50 @@ enum {
// 0x80110d 获取文件属性失败
#define HNP_ERRNO_GET_FILE_ATTR_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xd)
// 0x80110e 魔术字校验失败
#define HNP_ERRNO_BASE_MAGIC_CHECK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xe)
// 0x80110e 解压缩打开文件失败
#define HNP_ERRNO_BASE_UNZIP_OPEN_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xe)
// 0x80110f 解压缩打开文件失败
#define HNP_ERRNO_BASE_UNZIP_OPEN_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xf)
// 0x80110f 解压缩获取文件信息失败
#define HNP_ERRNO_BASE_UNZIP_GET_INFO_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xf)
// 0x801110 解压缩获取文件信息失败
#define HNP_ERRNO_BASE_UNZIP_GET_INFO_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x10)
#define HNP_ERRNO_BASE_UNZIP_READ_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x10)
// 0x801111 解压缩获取文件信息失败
#define HNP_ERRNO_BASE_UNZIP_READ_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x11)
// 0x801111 生成软链接失败
#define HNP_ERRNO_GENERATE_SOFT_LINK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x11)
// 0x801112 生成软链接失败
#define HNP_ERRNO_GENERATE_SOFT_LINK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x12)
// 0x801112 进程正在运行
#define HNP_ERRNO_PROCESS_RUNNING HNP_ERRNO_COMMON(HNP_MID_BASE, 0x12)
// 0x801113 进程正在运行
#define HNP_ERRNO_PROCESS_RUNNING HNP_ERRNO_COMMON(HNP_MID_BASE, 0x13)
// 0x801113 入参失败
#define HNP_ERRNO_BASE_PARAMS_INVALID HNP_ERRNO_COMMON(HNP_MID_BASE, 0x13)
// 0x801114 打开管道失败
#define HNP_ERRNO_BASE_POPEN_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x14)
// 0x801114 strdup失败
#define HNP_ERRNO_BASE_STRDUP_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x14)
// 0x801115 入参失败
#define HNP_ERRNO_BASE_PARAMS_INVALID HNP_ERRNO_COMMON(HNP_MID_BASE, 0x15)
// 0x801115 设置权限失败
#define HNP_ERRNO_BASE_CHMOD_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x15)
// 0x801116 strdup失败
#define HNP_ERRNO_BASE_STRDUP_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x16)
// 0x801116 删除目录失败
#define HNP_ERRNO_BASE_UNLINK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x16)
// 0x801117 设置权限失败
#define HNP_ERRNO_BASE_CHMOD_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x17)
// 0x801117 对应进程不存在
#define HNP_ERRNO_BASE_PROCESS_NOT_FOUND HNP_ERRNO_COMMON(HNP_MID_BASE, 0x17)
// 0x801118 删除目录失败
#define HNP_ERRNO_BASE_UNLINK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x18)
// 0x801118 创建路径失败
#define HNP_ERRNO_BASE_MKDIR_PATH_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x18)
// 0x801119 对应进程不存在
#define HNP_ERRNO_BASE_PROCESS_NOT_FOUND HNP_ERRNO_COMMON(HNP_MID_BASE, 0x19)
// 0x801119 读取配置文件流失败
#define HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x19)
// 0x80111a 创建路径失败
#define HNP_ERRNO_BASE_MKDIR_PATH_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1a)
// 0x80111a 解析json信息失败
#define HNP_ERRNO_BASE_PARSE_JSON_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1a)
// 0x80111b 读取配置文件流失败
#define HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1b)
// 0x80111b 未找到json项
#define HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1b)
// 0x80111c 解析json信息失败
#define HNP_ERRNO_BASE_PARSE_JSON_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1c)
// 0x80111d 未找到json项
#define HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1d)
// 0x80111e 解析json数组失败
#define HNP_ERRNO_BASE_GET_ARRAY_ITRM_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1e)
// 0x80111c 解析json数组失败
#define HNP_ERRNO_BASE_GET_ARRAY_ITRM_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1c)
int GetFileSizeByHandle(FILE *file, int *size);

View File

@ -30,7 +30,7 @@ static int ParseLinksJsonToCfgInfo(cJSON *linksItem, HnpCfgInfo *hnpCfg)
NativeBinLink *linkArray = NULL;
int linkArrayNum = cJSON_GetArraySize(linksItem);
if (linkArrayNum != 0) {
if (linkArrayNum > 0) {
hnpCfg->linkNum = linkArrayNum;
linkArray = (NativeBinLink*)malloc(sizeof(NativeBinLink) * linkArrayNum);
if (linkArray == NULL) {

View File

@ -33,8 +33,8 @@
extern "C" {
#endif
pid_t *gChildPid = NULL;
int gMax = 0;
pid_t *g_hnpPopenChildPid = NULL;
int g_hnpPopenMax = 0;
static void HnpPopenForChild(int pipefd[], const char *command, const char *mode)
{
@ -51,8 +51,8 @@ static void HnpPopenForChild(int pipefd[], const char *command, const char *mode
}
}
for (int i = 0; i < gMax; i++) {
if (gChildPid[i] > 0) {
for (int i = 0; i < g_hnpPopenMax; i++) {
if (g_hnpPopenChildPid[i] > 0) {
close(i);
}
}
@ -71,10 +71,14 @@ static FILE* HnpPopen(const char *command, const char *mode)
return NULL;
}
if (gChildPid == NULL) {
gMax = sysconf(_SC_OPEN_MAX);
gChildPid = (pid_t *)calloc(gMax, sizeof(pid_t));
if (gChildPid == NULL) {
if (g_hnpPopenChildPid == NULL) {
g_hnpPopenMax = sysconf(_SC_OPEN_MAX);
if (g_hnpPopenMax > 0) {
g_hnpPopenChildPid = (pid_t *)calloc(g_hnpPopenMax, sizeof(pid_t));
if (g_hnpPopenChildPid == NULL) {
return NULL;
}
} else {
return NULL;
}
}
@ -83,7 +87,7 @@ static FILE* HnpPopen(const char *command, const char *mode)
return NULL;
}
if ((pipefd[READ] >= gMax) || (pipefd[WRITE] >= gMax)) {
if ((pipefd[READ] >= g_hnpPopenMax) || (pipefd[WRITE] >= g_hnpPopenMax)) {
close(pipefd[READ]);
close(pipefd[WRITE]);
return NULL;
@ -105,7 +109,7 @@ static FILE* HnpPopen(const char *command, const char *mode)
stream = fdopen(pipefd[WRITE], mode);
}
if (stream != NULL) {
gChildPid[fileno(stream)] = pid;
g_hnpPopenChildPid[fileno(stream)] = pid;
}
return stream;
}
@ -119,16 +123,16 @@ static void HnpPclose(FILE *stream)
return;
}
if (gChildPid == NULL) {
if (g_hnpPopenChildPid == NULL) {
return;
}
pid = gChildPid[fileno(stream)];
pid = g_hnpPopenChildPid[fileno(stream)];
if (pid <= 0) {
return;
}
gChildPid[fileno(stream)] = 0;
g_hnpPopenChildPid[fileno(stream)] = 0;
waitpid(pid, &status, 0);
return;
}
@ -175,12 +179,6 @@ int HnpProcessRunCheck(const char *binName, const char *runPath)
HNP_LOGI("process[%s] running check", binName);
/* 对programName进行空格过滤防止外部命令注入 */
if (strchr(binName, ' ') != NULL) {
HNP_LOGE("hnp uninstall process name[%s] invalid", binName);
return HNP_ERRNO_BASE_PARAMS_INVALID;
}
ret = HnpPidGetByBinName(binName, pids, &count);
if ((ret != 0) || (count == 0)) { // 返回非0代表未找到进程对应的pid
return 0;

View File

@ -328,7 +328,7 @@ int HnpCfgGetFromZip(const char *inputFile, HnpCfgInfo *hnpCfg)
ret = unzGetCurrentFileInfo(zipFile, &fileInfo, fileName, sizeof(fileName), NULL, 0, NULL, 0);
if (ret != UNZ_OK) {
HNP_LOGE("unzip get zip:%s info unsuccess!", inputFile);
unzClose(zipFile);
unzClose(zipFile);
return HNP_ERRNO_BASE_UNZIP_GET_INFO_FAILED;
}
if (strstr(fileName, HNP_CFG_FILE_NAME) == NULL) {
@ -340,6 +340,7 @@ int HnpCfgGetFromZip(const char *inputFile, HnpCfgInfo *hnpCfg)
cfgStream = malloc(fileInfo.uncompressed_size);
if (cfgStream == NULL) {
HNP_LOGE("malloc unsuccess. size=%d, errno=%d", fileInfo.uncompressed_size, errno);
unzClose(zipFile);
return HNP_ERRNO_NOMEM;
}
uLong readSize = unzReadCurrentFile(zipFile, cfgStream, fileInfo.uncompressed_size);

View File

@ -48,20 +48,20 @@ int HnpShowHelp(int argc, char *argv[])
"[-i <private path>][-f]\r\n"
"\r\nThese are common hnp commands used in various situations:\r\n"
"\r\ninstall: install native software"
"\r\n hnp install -u [user id] -i [hnp package dir] -p [package name] -f\r\n"
"\r\n hnp install -u [user id] -i [hnp install dir] -p [package name] -f\r\n"
"\r\n -u : [required] user id \r\n"
"\r\n -p : [required] input path of software package dir, is support multiple\r\n"
"\r\n -i : [optional] hnp install dir, if not input, it will be use public path\r\n"
"\r\n -f : [optional] is force install\r\n"
"\r\nuninstall: uninstall native software"
"\r\n hnp uninstall -u [user id] -n [software name] -v [software version] -i [package name]\r\n"
"\r\n hnp uninstall -u [user id] -n [software name] -v [software version] -i [uninstall dir]\r\n"
"\r\n -u : [required] user id \r\n"
"\r\n -n : [required] software name\r\n"
"\r\n -v : [required] software version\r\n"
"\r\n -i : [optional] hnp install dir, it will be use public path\r\n"
"\r\nfor example:\r\n"
"\r\n hnp install -u 1000 -p /usr1/hnp/sample.hnp -p /usr1/hnp/sample2.hnp -i wechat -f\r\n"
" hnp uninstall -u 1000 -n native_sample -v 1.1 -i wechat\r\n");
"\r\n hnp install -u 1000 -p /usr1/hnp/sample.hnp -p /usr1/hnp/sample2.hnp -i /data/app/el1/bundle/ -f\r\n"
" hnp uninstall -u 1000 -n native_sample -v 1.1 -i /data/app/el1/bundle/\r\n");
return 0;
}

View File

@ -47,14 +47,14 @@ int HnpCliShowHelp(int argc, char *argv[])
"[-n <native package name>][-v <native package version>]\r\n"
"\r\nThese are common hnpcli commands used in various situations:\r\n"
"\r\npack: packet native software package to .hnp file"
"\r\n hnpcli pack -i [source path] <-o [dst path]> <-name [software name]> <-v [software version]>"
"\r\n hnpcli pack -i [source path] <-o [dst path]> <-n [software name]> <-v [software version]>"
"\r\n -i : [required] input path of software package dir"
"\r\n -o : [optional] output path of hnp file. if not set then ouput to current directory"
"\r\n -n : [optional] software name. if not hnp.json in input dir then must set"
"\r\n -v : [optional] software version. if not hnp.json in input dir then must set\r\n"
"\r\nfor example:\r\n"
"\r\n hnpcli pack -i /usr1/native_sample -o /usr1/output -n native_sample -v 1.1\r\n");
return 0;
}

View File

@ -29,35 +29,29 @@ typedef struct NativeHnpPathStru {
char hnpVersionPath[MAX_FILE_PATH_LEN];
} NativeHnpPath;
// 0x801301 安装命令参数错误
#define HNP_ERRNO_INSTALLER_ARGV_NUM_INVALID HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x1)
// 0x801301 组装安装路径失败
#define HNP_ERRNO_INSTALLER_GET_HNP_PATH_FAILED HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x1)
// 0x801302 组装安装路径失败
#define HNP_ERRNO_INSTALLER_GET_HNP_PATH_FAILED HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x2)
// 0x801302 获取安装绝对路径失败
#define HNP_ERRNO_INSTALLER_GET_REALPATH_FAILED HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x2)
// 0x801303 获取安装绝对路径失败
#define HNP_ERRNO_INSTALLER_GET_REALPATH_FAILED HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x3)
// 0x801303 获取Hnp安装包名称失败
#define HNP_ERRNO_INSTALLER_GET_HNP_NAME_FAILED HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x3)
// 0x801304 获取Hnp安装包名称失败
#define HNP_ERRNO_INSTALLER_GET_HNP_NAME_FAILED HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x4)
// 0x801304 安装的包已存在
#define HNP_ERRNO_INSTALLER_PATH_IS_EXIST HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x4)
// 0x801305 安装的包已存在
#define HNP_ERRNO_INSTALLER_PATH_IS_EXIST HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x5)
// 0x801305 获取卸载路径失败
#define HNP_ERRNO_UNINSTALLER_HNP_PATH_NOT_EXIST HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x5)
// 0x801306 卸载命令参数错误
#define HNP_ERRNO_UNINSTALLER_ARGV_NUM_INVALID HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x6)
// 0x801306 安装命令参数uid错误
#define HNP_ERRNO_INSTALLER_ARGV_UID_INVALID HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x6)
// 0x801307 获取卸载路径失败
#define HNP_ERRNO_UNINSTALLER_HNP_PATH_NOT_EXIST HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x7)
// 0x801307 获取版本目录失败
#define HNP_ERRNO_INSTALLER_VERSION_FILE_GET_FAILED HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x7)
// 0x801308 安装命令参数uid错误
#define HNP_ERRNO_INSTALLER_ARGV_UID_INVALID HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x8)
// 0x801309 获取版本目录失败
#define HNP_ERRNO_INSTALLER_VERSION_FILE_GET_FAILED HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x9)
// 0x80130a 安装包超过最大值
#define HNP_ERRNO_INSTALLER_SOFTWARE_NUM_OVERSIZE HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x1a)
// 0x801308 安装包超过最大值
#define HNP_ERRNO_INSTALLER_SOFTWARE_NUM_OVERSIZE HNP_ERRNO_COMMON(HNP_MID_INSTALLER, 0x8)
#define HNP_DEFAULT_INSTALL_ROOT_PATH "/data/app/el1/bundle/"

View File

@ -21,6 +21,7 @@
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include "hnp_installer.h"
@ -60,7 +61,7 @@ static int HnpGenerateSoftLinkAllByJson(const char *installPath, const char *dst
}
for (unsigned int i = 0; i < hnpCfg->linkNum; i++) {
int ret = sprintf_s(srcFile, MAX_FILE_PATH_LEN, "%s%s", installPath, currentLink->source);
int ret = sprintf_s(srcFile, MAX_FILE_PATH_LEN, "%s/%s", installPath, currentLink->source);
if (ret < 0) {
HNP_LOGE("sprintf install bin src file unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
@ -77,7 +78,7 @@ static int HnpGenerateSoftLinkAllByJson(const char *installPath, const char *dst
} else {
fileName++;
}
ret = sprintf_s(dstFile, MAX_FILE_PATH_LEN, "%s%s", dstPath, fileName);
ret = sprintf_s(dstFile, MAX_FILE_PATH_LEN, "%s/%s", dstPath, fileName);
if (ret < 0) {
HNP_LOGE("sprintf install bin dst file unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
@ -103,7 +104,7 @@ static int HnpGenerateSoftLinkAll(const char *installPath, const char *dstPath)
DIR *dir;
struct dirent *entry;
ret = sprintf_s(srcPath, MAX_FILE_PATH_LEN, "%sbin/", installPath);
ret = sprintf_s(srcPath, MAX_FILE_PATH_LEN, "%s/bin/", installPath);
if (ret < 0) {
HNP_LOGE("sprintf install bin path unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
@ -127,14 +128,14 @@ static int HnpGenerateSoftLinkAll(const char *installPath, const char *dstPath)
if (entry->d_type != DT_REG) {
continue;
}
ret = sprintf_s(srcFile, MAX_FILE_PATH_LEN, "%s%s", srcPath, entry->d_name);
ret = sprintf_s(srcFile, MAX_FILE_PATH_LEN, "%s/%s", srcPath, entry->d_name);
if (ret < 0) {
closedir(dir);
HNP_LOGE("sprintf install bin src file unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
ret = sprintf_s(dstFile, MAX_FILE_PATH_LEN, "%s%s", dstPath, entry->d_name);
ret = sprintf_s(dstFile, MAX_FILE_PATH_LEN, "%s/%s", dstPath, entry->d_name);
if (ret < 0) {
closedir(dir);
HNP_LOGE("sprintf install bin dst file unsuccess.");
@ -157,7 +158,7 @@ static int HnpGenerateSoftLink(const char *installPath, const char *hnpBasePath,
int ret = 0;
char binPath[MAX_FILE_PATH_LEN];
ret = sprintf_s(binPath, MAX_FILE_PATH_LEN, "%sbin/", hnpBasePath);
ret = sprintf_s(binPath, MAX_FILE_PATH_LEN, "%s/bin/", hnpBasePath);
if (ret < 0) {
HNP_LOGE("sprintf install bin path unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
@ -334,14 +335,14 @@ static int HnpInstallPathGet(const char *fileName, bool isForce, char* hnpVersio
return HNP_ERRNO_BASE_COPY_FAILED;
}
hnpNameTmp = strrchr(hnpDstPath->hnpSoftwareName, '.');
if (hnpNameTmp && !strcmp(hnpNameTmp, ".hnp")) {
if (hnpNameTmp) {
*hnpNameTmp = '\0';
} else {
return HNP_ERRNO_INSTALLER_GET_HNP_NAME_FAILED;
}
/* 拼接安装路径 */
ret = sprintf_s(hnpDstPath->hnpSoftwarePath, MAX_FILE_PATH_LEN, "%s%s.org/", hnpDstPath->hnpBasePath,
ret = sprintf_s(hnpDstPath->hnpSoftwarePath, MAX_FILE_PATH_LEN, "%s/%s.org/", hnpDstPath->hnpBasePath,
hnpDstPath->hnpSoftwareName);
if (ret < 0) {
HNP_LOGE("hnp install sprintf hnp base path unsuccess.");
@ -349,7 +350,7 @@ static int HnpInstallPathGet(const char *fileName, bool isForce, char* hnpVersio
}
/* 拼接安装路径 */
ret = sprintf_s(hnpDstPath->hnpVersionPath, MAX_FILE_PATH_LEN, "%s%s_%s/", hnpDstPath->hnpSoftwarePath,
ret = sprintf_s(hnpDstPath->hnpVersionPath, MAX_FILE_PATH_LEN, "%s/%s_%s/", hnpDstPath->hnpSoftwarePath,
hnpDstPath->hnpSoftwareName, hnpVersion);
if (ret < 0) {
HNP_LOGE("hnp install sprintf install path unsuccess.");
@ -401,6 +402,11 @@ static int HnpReadAndInstall(char *srcFile, NativeHnpPath *hnpDstPath, bool isFo
}
ret = HnpInstallPathGet(fileName, isForce, hnpCfg.version, hnpDstPath);
if (ret != 0) {
// 释放软链接占用的内存
if (hnpCfg.links != NULL) {
free(hnpCfg.links);
hnpCfg.links = NULL;
}
return ret; /* 内部已打印日志 */
}
@ -428,18 +434,6 @@ static int HnpInsatllPre(unsigned long uid, char *softwarePath[], int count, con
int ret;
int i;
/* 拼接安装路径 */
if (sprintf_s(dstPath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/", uid) < 0) {
HNP_LOGE("hnp install sprintf unsuccess, uid:%lu", uid);
return HNP_ERRNO_INSTALLER_GET_HNP_PATH_FAILED;
}
/* 验证安装路径是否存在 */
if (access(dstPath, F_OK) != 0) {
HNP_LOGE("hnp install uid path[%s] is not exist", dstPath);
return HNP_ERRNO_INSTALLER_GET_REALPATH_FAILED;
}
/* 验证native软件包路径是否存在 */
for (i = 0; i < count; i++) {
if (GetRealPath(softwarePath[i], srcFile[i]) != 0) {
@ -448,10 +442,33 @@ static int HnpInsatllPre(unsigned long uid, char *softwarePath[], int count, con
}
}
ret = sprintf_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, "%shnp/%s/", dstPath, installPath);
if (ret < 0) {
HNP_LOGE("hnp install public base path sprintf unsuccess.");
return HNP_ERRNO_INSTALLER_GET_HNP_PATH_FAILED;
if (installPath == NULL) {
/* 拼接安装路径 */
if (sprintf_s(dstPath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/", uid) < 0) {
HNP_LOGE("hnp install sprintf unsuccess, uid:%lu", uid);
return HNP_ERRNO_INSTALLER_GET_HNP_PATH_FAILED;
}
/* 验证安装路径是否存在 */
if (access(dstPath, F_OK) != 0) {
HNP_LOGE("hnp install uid path[%s] is not exist", dstPath);
return HNP_ERRNO_INSTALLER_GET_REALPATH_FAILED;
}
ret = sprintf_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, "%shnp/hnppublic/", dstPath);
if (ret < 0) {
HNP_LOGE("hnp install public base path sprintf unsuccess.");
return HNP_ERRNO_INSTALLER_GET_HNP_PATH_FAILED;
}
} else {
if (access(installPath, F_OK) != 0) {
HNP_LOGE("hnp install path[%s] is not exist", installPath);
return HNP_ERRNO_INSTALLER_GET_REALPATH_FAILED;
}
if (strcpy_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, installPath) != EOK) {
HNP_LOGE("strcpy install base path unsuccess.");
return HNP_ERRNO_BASE_COPY_FAILED;
}
}
for (i = 0; i < count; i++) {
@ -469,7 +486,7 @@ static int HnpInsatllPre(unsigned long uid, char *softwarePath[], int count, con
int HnpCmdInstall(int argc, char *argv[])
{
char *installPath = "hnppublic";
char *installPath = NULL;
char *softwarePath[MAX_SOFTWARE_NUM] = {0};
int count = 0;
unsigned long uid = 0;
@ -477,9 +494,10 @@ int HnpCmdInstall(int argc, char *argv[])
bool isForce = false;
int ret;
int ch;
char *ptr = NULL;
optind = 1; // 从头开始遍历参数
while ((ch = getopt(argc, argv, "hu:p:if")) != -1) {
while ((ch = getopt_long(argc, argv, "hu:p:i:f", NULL, NULL)) != -1) {
switch (ch) {
case 'h' :
return HNP_ERRNO_OPERATOR_ARGV_MISS;
@ -492,6 +510,11 @@ int HnpCmdInstall(int argc, char *argv[])
}
break;
case 'p': //hnp software path
ptr = strstr(optarg, ".hnp");
if ((ptr == NULL) || (strcmp(ptr, ".hnp") != 0)) {
HNP_LOGE("package name[%s] invalid", optarg);
return HNP_ERRNO_INSTALLER_GET_HNP_NAME_FAILED;
}
if ((count + 1) >= MAX_SOFTWARE_NUM) {
HNP_LOGE("hnp install software must less than %d", MAX_SOFTWARE_NUM);
return HNP_ERRNO_INSTALLER_SOFTWARE_NUM_OVERSIZE;
@ -520,14 +543,33 @@ int HnpCmdInstall(int argc, char *argv[])
return HnpInsatllPre(uid, softwarePath, count, installPath, isForce);
}
static int HnpUnInstallPre(unsigned int uid, const char *hnpName, const char *version, const char *packagePath)
static int HnpUnInstallPre(unsigned int uid, const char *hnpName, const char *version, const char *uninstallPath)
{
NativeHnpPath hnpDstPath = {0};
if (sprintf_s(hnpDstPath.hnpVersionPath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/%s/%s.org/%s_%s/",
uid, packagePath, hnpName, hnpName, version) < 0) {
HNP_LOGE("hnp uninstall path sprintf unsuccess, uid:%lu process name[%s], version[%s]", uid, hnpName,
version);
if (uninstallPath == NULL) {
/* 拼接基本路径 */
if (sprintf_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/hnp/hnppublic/",
uid) < 0) {
HNP_LOGE("hnp uninstall base path sprintf unsuccess,uid:%lu, process name[%s]", uid, hnpName);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
} else {
if (strcpy_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, uninstallPath) != EOK) {
HNP_LOGE("strcpy uninstall base path unsuccess.");
return HNP_ERRNO_BASE_COPY_FAILED;
}
}
if (sprintf_s(hnpDstPath.hnpSoftwarePath, MAX_FILE_PATH_LEN, "%s/%s.org/", hnpDstPath.hnpBasePath, hnpName) < 0) {
HNP_LOGE("hnp uninstall path sprintf unsuccess,base path:%s,process name[%s]", hnpDstPath.hnpBasePath, hnpName);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
if (sprintf_s(hnpDstPath.hnpVersionPath, MAX_FILE_PATH_LEN, "%s/%s_%s/", hnpDstPath.hnpSoftwarePath, hnpName,
version) < 0) {
HNP_LOGE("hnp uninstall path sprintf unsuccess,software path:%s process name[%s],version[%s]",
hnpDstPath.hnpSoftwarePath, hnpName, version);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
@ -537,17 +579,6 @@ static int HnpUnInstallPre(unsigned int uid, const char *hnpName, const char *ve
return HNP_ERRNO_UNINSTALLER_HNP_PATH_NOT_EXIST;
}
/* 拼接基本路径 */
if (sprintf_s(hnpDstPath.hnpSoftwarePath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/%s/%s.org/", uid,
packagePath, hnpName) < 0) {
HNP_LOGE("hnp uninstall pro path sprintf unsuccess, uid:%lu, process name[%s]", uid, hnpName);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
if (sprintf_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/%s/", uid,
packagePath) < 0) {
HNP_LOGE("hnp uninstall base path sprintf unsuccess, uid:%lu, process name[%s]", uid, hnpName);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
if (strcpy_s(hnpDstPath.hnpSoftwareName, MAX_FILE_PATH_LEN, hnpName) != EOK) {
HNP_LOGE("hnp uninstall strcpy unsuccess.");
return HNP_ERRNO_BASE_COPY_FAILED;
@ -558,7 +589,7 @@ static int HnpUnInstallPre(unsigned int uid, const char *hnpName, const char *ve
int HnpCmdUnInstall(int argc, char *argv[])
{
char *installPath = "hnppublic";
char *uninstallPath = NULL;
unsigned long uid;
char *uidArg = NULL;
char *hnpName = NULL;
@ -567,7 +598,7 @@ int HnpCmdUnInstall(int argc, char *argv[])
int ch;
optind = 1; // 从头开始遍历参数
while ((ch = getopt(argc, argv, "hu:n:v:i")) != -1) {
while ((ch = getopt_long(argc, argv, "hu:n:v:i:", NULL, NULL)) != -1) {
switch (ch) {
case 'h' :
return HNP_ERRNO_OPERATOR_ARGV_MISS;
@ -589,7 +620,7 @@ int HnpCmdUnInstall(int argc, char *argv[])
if (optarg == NULL) {
break;
}
installPath = (char *)optarg;
uninstallPath = (char *)optarg;
break;
default:
@ -602,7 +633,7 @@ int HnpCmdUnInstall(int argc, char *argv[])
return HNP_ERRNO_OPERATOR_ARGV_MISS;
}
return HnpUnInstallPre(uid, hnpName, version, installPath);
return HnpUnInstallPre(uid, hnpName, version, uninstallPath);
}
#ifdef __cplusplus

View File

@ -22,20 +22,14 @@
extern "C" {
#endif
// 0x801201 打包命令参数错误
#define HNP_ERRNO_PACK_ARGV_NUM_INVALID HNP_ERRNO_COMMON(HNP_MID_PACK, 0x1)
// 0x801201 获取绝对路径失败
#define HNP_ERRNO_PACK_GET_REALPATH_FAILED HNP_ERRNO_COMMON(HNP_MID_PACK, 0x1)
// 0x801202 获取绝对路径失败
#define HNP_ERRNO_PACK_GET_REALPATH_FAILED HNP_ERRNO_COMMON(HNP_MID_PACK, 0x2)
// 0x801202 组装hnp输出路径失败
#define HNP_ERRNO_PACK_GET_HNP_PATH_FAILED HNP_ERRNO_COMMON(HNP_MID_PACK, 0x2)
// 0x801203 缺少操作参数
#define HNP_ERRNO_PACK_MISS_OPERATOR_PARAM HNP_ERRNO_COMMON(HNP_MID_PACK, 0x3)
// 0x801208 组装hnp输出路径失败
#define HNP_ERRNO_PACK_GET_HNP_PATH_FAILED HNP_ERRNO_COMMON(HNP_MID_PACK, 0x8)
// 0x801209 压缩目录失败
#define HNP_ERRNO_PACK_ZIP_DIR_FAILED HNP_ERRNO_COMMON(HNP_MID_PACK, 0x9)
// 0x801203 压缩目录失败
#define HNP_ERRNO_PACK_ZIP_DIR_FAILED HNP_ERRNO_COMMON(HNP_MID_PACK, 0x3)
/* hnp打包参数 */
typedef struct HnpPackArgvStru {

View File

@ -105,7 +105,7 @@ static int PackHnp(const char *hnpSrcPath, const char *hnpDstPath, HnpPackInfo *
return ret;
}
int GetHnpCfgInfo(const char *hnpCfgPath, const char *sourcePath, HnpCfgInfo *hnpCfg)
static int GetHnpCfgInfo(const char *hnpCfgPath, const char *sourcePath, HnpCfgInfo *hnpCfg)
{
NativeBinLink *linkArr = NULL;
char linksource[MAX_FILE_PATH_LEN] = {0};
@ -135,7 +135,7 @@ int GetHnpCfgInfo(const char *hnpCfgPath, const char *sourcePath, HnpCfgInfo *hn
return 0;
}
int ParsePackArgs(HnpPackArgv *packArgv, HnpPackInfo *packInfo)
static int ParsePackArgs(HnpPackArgv *packArgv, HnpPackInfo *packInfo)
{
char cfgPath[MAX_FILE_PATH_LEN];
@ -192,6 +192,7 @@ int HnpCmdPack(int argc, char *argv[])
HnpPackInfo packInfo = {0};
int opt;
optind = 1; // 从头开始遍历参数
while ((opt = getopt_long(argc, argv, "hi:o:n:v:", NULL, NULL)) != -1) {
switch (opt) {
case 'h' :

View File

@ -36,6 +36,7 @@ if (!defined(ohos_lite)) {
sources = [
"${appspawn_path}/service/hnp/base/hnp_file.c",
"${appspawn_path}/service/hnp/base/hnp_json.c",
"${appspawn_path}/service/hnp/base/hnp_log.c",
"${appspawn_path}/service/hnp/base/hnp_sal.c",
"${appspawn_path}/service/hnp/base/hnp_zip.c",

View File

@ -85,45 +85,38 @@ HWTEST(HnpPackTest, Hnp_Pack_001, TestSize.Level0)
// clear resource before test
remove("./hnp_out/sample.hnp");
rmdir("hnp_out");
remove("hnp_sample/hnp.json");
rmdir("hnp_sample");
remove("hnp.cfg");
EXPECT_EQ(mkdir("hnp_sample", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_out", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
char arg1[] = "hnp";
char arg2[] = "pack";
char arg1[] = "hnp", arg2[] = "pack";
{ // param num not enough
char* argv[] = {arg1, arg2};
char *argv[] = {arg1, arg2};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_ARGV_NUM_INVALID);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_OPERATOR_ARGV_MISS);
}
{ // src dir path is invalid
char arg3[] = "./hnp_sample2";
char arg4[] = "./hnp_out";
char* argv[] = {arg1, arg2, arg3, arg4};
char arg3[] = "-i", arg4[] = "./hnp_sample2", arg5[] = "-o", arg6[] = "./hnp_out";
char *argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_GET_REALPATH_FAILED);
}
{ // no name and version
char arg3[] = "./hnp_sample";
char arg4[] = "./hnp_out";
char* argv[] = {arg1, arg2, arg3, arg4};
char arg3[] = "-i", arg4[] = "./hnp_sample", arg5[] = "-o", arg6[] = "./hnp_out";
char *argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_MISS_OPERATOR_PARAM);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_OPERATOR_ARGV_MISS);
}
{ // ok
char arg3[] = "./hnp_sample";
char arg4[] = "./hnp_out";
char arg5[] = "-name";
char arg6[] = "sample";
char arg7[] = "-v";
char arg8[] = "1.1";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8};
char arg3[] = "-i", arg4[] = "./hnp_sample", arg5[] = "-o", arg6[] = "./hnp_out";
char arg7[] = "-n", arg8[] = "sample", arg9[] = "-v", arg10[] = "1.1";
char *argv[] = {arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), 0);
@ -149,46 +142,29 @@ HWTEST(HnpPackTest, Hnp_Pack_002, TestSize.Level0)
EXPECT_EQ(mkdir("hnp_sample", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_out", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
FILE *fp = fopen("hnp.cfg", "w");
FILE *fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_NE(fp, nullptr);
fclose(fp);
char arg1[] = "hnp";
char arg2[] = "pack";
char arg3[] = "./hnp_sample";
char arg4[] = "./hnp_out";
char arg5[] = "-cfg";
{ // cfg file path is invalid
char arg6[] = "./hnp2.cfg";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
char arg1[] = "hnp", arg2[] = "pack";
char arg3[] = "-i", arg4[] = "./hnp_sample", arg5[] = "-o", arg6[] = "./hnp_out";
char *argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_GET_REALPATH_FAILED);
}
{ // cfg file content is empty
char arg6[] = "./hnp.cfg";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_READ_FILE_STREAM_FAILED);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED);
}
{ // cfg file not json formaat
char cfg[] = "this is for test!";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
char arg6[] = "./hnp.cfg";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_PARSE_JSON_FAILED);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_BASE_PARSE_JSON_FAILED);
}
EXPECT_EQ(remove("./hnp_sample/hnp.json"), 0);
EXPECT_EQ(rmdir("hnp_sample"), 0);
EXPECT_EQ(rmdir("hnp_out"), 0);
EXPECT_EQ(remove("hnp.cfg"), 0);
GTEST_LOG_(INFO) << "Hnp_Pack_002 end";
}
@ -206,43 +182,39 @@ HWTEST(HnpPackTest, Hnp_Pack_003, TestSize.Level0)
EXPECT_EQ(mkdir("hnp_sample", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_out", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
FILE *fp = fopen("hnp.cfg", "w");
FILE *fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_NE(fp, nullptr);
fclose(fp);
char arg1[] = "hnp";
char arg2[] = "pack";
char arg3[] = "./hnp_sample";
char arg4[] = "./hnp_out";
char arg5[] = "-cfg";
char arg6[] = "./hnp.cfg";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
char arg1[] = "hnp", arg2[] = "pack";
char arg3[] = "-i", arg4[] = "./hnp_sample", arg5[] = "-o", arg6[] = "./hnp_out";
char *argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
{ // cfg file content has not type item
char cfg[] = "{\"typ\":\"hnp-config\",\"name\":\"sample\",\"version\":\"1.1\",\"install\":"
"{\"links\":[{\"source\":\"bin/out\",\"target\":\"out\"},{\"source\":\"bin/out2\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_PARSE_ITEM_NO_FOUND);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND);
}
{ // type item value is not expected
char cfg[] = "{\"type\":\"hnpconfig\",\"name\":\"sample\",\"version\":\"1.1\",\"install\":"
"{\"links\":[{\"source\":\"bin/out\",\"target\":\"out\"},{\"source\":\"bin/out2\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_PARSE_ITEM_NO_FOUND);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND);
}
EXPECT_EQ(remove("./hnp_sample/hnp.json"), 0);
EXPECT_EQ(rmdir("hnp_sample"), 0);
EXPECT_EQ(rmdir("hnp_out"), 0);
EXPECT_EQ(remove("hnp.cfg"), 0);
GTEST_LOG_(INFO) << "Hnp_Pack_003 end";
}
@ -260,53 +232,49 @@ HWTEST(HnpPackTest, Hnp_Pack_004, TestSize.Level0)
EXPECT_EQ(mkdir("hnp_sample", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_out", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
FILE *fp = fopen("hnp.cfg", "w");
FILE *fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_NE(fp, nullptr);
fclose(fp);
char arg1[] = "hnp";
char arg2[] = "pack";
char arg3[] = "./hnp_sample";
char arg4[] = "./hnp_out";
char arg5[] = "-cfg";
char arg6[] = "./hnp.cfg";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
char arg1[] = "hnp", arg2[] = "pack";
char arg3[] = "-i", arg4[] = "./hnp_sample", arg5[] = "-o", arg6[] = "./hnp_out";
char *argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
{ // cfg file content has not name item
char cfg[] = "{\"type\":\"hnp-config\",\"version\":\"1.1\",\"install\":"
"{\"links\":[{\"source\":\"bin/out\",\"target\":\"out\"},{\"source\":\"bin/out2\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_PARSE_ITEM_NO_FOUND);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND);
}
{ // cfg file content has not version item
char cfg[] = "{\"type\":\"hnp-config\",\"name\":\"sample\",\"install\":"
"{\"links\":[{\"source\":\"bin/out\",\"target\":\"out\"},{\"source\":\"bin/out2\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_PARSE_ITEM_NO_FOUND);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND);
}
{ // cfg file content has not install item
char cfg[] = "{\"type\":\"hnp-config\",\"name\":\"sample\",\"version\":\"1.1\",\"uninstall\":"
"{\"links\":[{\"source\":\"bin/out\",\"target\":\"out\"},{\"source\":\"bin/out2\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_PARSE_ITEM_NO_FOUND);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND);
}
EXPECT_EQ(remove("./hnp_sample/hnp.json"), 0);
EXPECT_EQ(rmdir("hnp_sample"), 0);
EXPECT_EQ(rmdir("hnp_out"), 0);
EXPECT_EQ(remove("hnp.cfg"), 0);
GTEST_LOG_(INFO) << "Hnp_Pack_004 end";
}
@ -324,44 +292,46 @@ HWTEST(HnpPackTest, Hnp_Pack_005, TestSize.Level0)
EXPECT_EQ(mkdir("hnp_sample", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_out", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
FILE *fp = fopen("hnp.cfg", "w");
FILE *fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_NE(fp, nullptr);
fclose(fp);
char arg1[] = "hnp";
char arg2[] = "pack";
char arg3[] = "./hnp_sample";
char arg4[] = "./hnp_out";
char arg5[] = "-cfg";
char arg6[] = "./hnp.cfg";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
char arg1[] = "hnp", arg2[] = "pack";
char arg3[] = "-i", arg4[] = "./hnp_sample", arg5[] = "-o", arg6[] = "./hnp_out";
char *argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
{ // link arry item has not source item
char cfg[] = "{\"type\":\"hnp-config\",\"name\":\"sample\",\"version\":\"1.1\",\"install\":"
"{\"links\":[{\"src\":\"bin/out\",\"target\":\"out\"},{\"source\":\"bin/out2\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_PARSE_ITEM_NO_FOUND);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND);
}
{ // ok, link arry item has not target item
char cfg[] = "{\"type\":\"hnp-config\",\"name\":\"sample\",\"version\":\"1.1\",\"install\":"
"{\"links\":[{\"source\":\"bin/out\",\"tar\":\"out\"},{\"source\":\"bin/out2\","
"{\"links\":[{\"source\":\"a.out\",\"tar\":\"out\"},{\"source\":\"a.out\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_GET_REALPATH_FAILED);
FILE *fp = fopen("./hnp_sample/a.out", "w");
EXPECT_NE(fp, nullptr);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), 0);
EXPECT_EQ(remove("./hnp_sample/a.out"), 0);
EXPECT_EQ(remove("./hnp_out/sample.hnp"), 0);
}
EXPECT_EQ(remove("./hnp_sample/hnp.json"), 0);
EXPECT_EQ(rmdir("hnp_sample"), 0);
EXPECT_EQ(rmdir("hnp_out"), 0);
EXPECT_EQ(remove("hnp.cfg"), 0);
GTEST_LOG_(INFO) << "Hnp_Pack_005 end";
}
@ -379,22 +349,18 @@ HWTEST(HnpPackTest, Hnp_Pack_006, TestSize.Level0)
EXPECT_EQ(mkdir("hnp_sample", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_out", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
FILE *fp = fopen("hnp.cfg", "w");
FILE *fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_NE(fp, nullptr);
fclose(fp);
char arg1[] = "hnp";
char arg2[] = "pack";
char arg3[] = "./hnp_sample";
char arg4[] = "./hnp_out";
char arg5[] = "-cfg";
char arg6[] = "./hnp.cfg";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
char arg1[] = "hnp", arg2[] = "pack";
char arg3[] = "-i", arg4[] = "./hnp_sample", arg5[] = "-o", arg6[] = "./hnp_out";
char *argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
{ // ok. no links item in install item
char cfg[] = "{\"type\":\"hnp-config\",\"name\":\"sample\",\"version\":\"1.1\",\"install\":{}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
@ -404,7 +370,7 @@ HWTEST(HnpPackTest, Hnp_Pack_006, TestSize.Level0)
{ // ok. no array in links item
char cfg[] = "{\"type\":\"hnp-config\",\"name\":\"sample\",\"version\":\"1.1\",\"install\":"
"{\"links\":[]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
@ -413,19 +379,25 @@ HWTEST(HnpPackTest, Hnp_Pack_006, TestSize.Level0)
}
{ // ok. 2 links
char cfg[] = "{\"type\":\"hnp-config\",\"name\":\"sample\",\"version\":\"1.1\",\"install\":"
"{\"links\":[{\"source\":\"bin/out\",\"target\":\"out\"},{\"source\":\"bin/out2\","
"{\"links\":[{\"source\":\"a.out\",\"target\":\"out\"},{\"source\":\"a.out\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
fp = fopen("./hnp_sample/hnp.json", "w");
EXPECT_EQ(fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp), strlen(cfg) + 1);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), HNP_ERRNO_PACK_GET_REALPATH_FAILED);
FILE *fp = fopen("./hnp_sample/a.out", "w");
EXPECT_NE(fp, nullptr);
fclose(fp);
EXPECT_EQ(HnpCmdPack(argc, argv), 0);
EXPECT_EQ(remove("./hnp_sample/a.out"), 0);
EXPECT_EQ(remove("./hnp_out/sample.hnp"), 0);
}
EXPECT_EQ(remove("./hnp_sample/hnp.json"), 0);
EXPECT_EQ(rmdir("hnp_sample"), 0);
EXPECT_EQ(rmdir("hnp_out"), 0);
EXPECT_EQ(remove("hnp.cfg"), 0);
GTEST_LOG_(INFO) << "Hnp_Pack_006 end";
}