!1017 Native软件包管理安装卸载功能ut测试 + bugfix

Merge pull request !1017 from 王达/native_test_fix
This commit is contained in:
openharmony_ci 2024-03-30 10:10:50 +00:00 committed by Gitee
commit ef6682abf1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
10 changed files with 1098 additions and 95 deletions

View File

@ -45,7 +45,7 @@ typedef enum {
*
* @return 0:success;other means failure.
*/
int NativeInstallHnp(const char *userId, const char *hnpPath, Bool isForce);
int NativeInstallHnp(const char *userId, const char *hnpPath, const char *packageName, Bool isForce);
/**
* Uninstall native software package.
@ -56,7 +56,7 @@ int NativeInstallHnp(const char *userId, const char *hnpPath, Bool isForce);
*
* @return 0:success;other means failure.
*/
int NativeUnInstallHnp(const char *userId, const char *hnpName, const char *hnpVersion);
int NativeUnInstallHnp(const char *userId, const char *hnpName, const char *hnpVersion, const char *packageName);
#ifdef __cplusplus
}

View File

@ -81,12 +81,12 @@ static int StartHnpProcess(char *const argv[], char *const apcEnv[])
return exitVal;
}
int NativeInstallHnp(const char *userId, const char *hnpPath, Bool isForce)
int NativeInstallHnp(const char *userId, const char *hnpPath, const char *packageName, Bool isForce)
{
char *argv[MAX_ARGV_NUM] = {0};
char *apcEnv[MAX_ENV_NUM] = {0};
if ((userId == NULL) || (hnpPath == NULL)) {
if ((userId == NULL) || (hnpPath == NULL))) {
return HNP_API_ERRNO_PARAM_INVALID;
}
@ -98,10 +98,20 @@ int NativeInstallHnp(const char *userId, const char *hnpPath, Bool isForce)
argv[HNP_INDEX_2] = (char*)userId;
argv[HNP_INDEX_3] = (char*)hnpPath;
if (packageName == NULL) {
argv[HNP_INDEX_4] = "null";
} else {
argv[HNP_INDEX_4] = (char*)packageName;
}
if (isForce == TRUE) {
argv[HNP_INDEX_5] = "-f";
}
return StartHnpProcess(argv, apcEnv);
}
int NativeUnInstallHnp(const char *userId, const char *hnpName, const char *hnpVersion)
int NativeUnInstallHnp(const char *userId, const char *hnpName, const char *hnpVersion, const char *packageName)
{
char *argv[MAX_ARGV_NUM] = {0};
char *apcEnv[MAX_ENV_NUM] = {0};
@ -118,7 +128,13 @@ int NativeUnInstallHnp(const char *userId, const char *hnpName, const char *hnpV
argv[HNP_INDEX_2] = (char*)userId;
argv[HNP_INDEX_3] = (char*)hnpName;
argv[HNP_INDEX_4] = (char*)hnpVersion;
if (packageName == NULL) {
argv[HNP_INDEX_5] = "null";
} else {
argv[HNP_INDEX_5] = (char*)packageName;
}
return StartHnpProcess(argv, apcEnv);
}

View File

@ -34,6 +34,7 @@ extern "C" {
#define HNP_VERSION_LEN 32
#define BUFFER_SIZE 1024
#define HNP_COMMAND_LEN 128
#define MAX_PROCESSES 32
#ifdef _WIN32
#define DIR_SPLIT_SYMBOL '\\'
@ -172,6 +173,15 @@ enum {
// 0x801118 删除目录失败
#define HNP_ERRNO_BASE_UNLINK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x18)
// 0x801119 对应进程不存在
#define HNP_ERRNO_BASE_PROGRAM_NOT_FOUND HNP_ERRNO_COMMON(HNP_MID_BASE, 0x19)
// 0x80111a 进程超过最大值
#define HNP_ERRNO_BASE_PROGRAM_NUM_OVERSIZE HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1a)
// 0x80111b 创建路径失败
#define HNP_ERRNO_BASE_MKDIR_PATH_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1b)
int GetFileSizeByHandle(FILE *file, int *size);
int ReadFileToStream(const char *filePath, char **stream, int *streamLen);
@ -192,12 +202,14 @@ int HnpReadFromZipHead(const char *zipFile, NativeHnpHead **hnpHead);
int HnpSymlink(const char *srcFile, const char *dstFile);
int HnpProgramRunCheck(const char *programName);
int HnpProgramRunCheck(const char *binName, const char *runPath);
int HnpDeleteFolder(const char *path);
int HnpCreateFolder(const char* path);
int HnpWriteInfoToFile(const char* filePath, char *buff, int len);
#define HNP_LOGI(args...) \
HnpLogPrintf(HNP_LOG_INFO, "HNP", ##args)

View File

@ -130,6 +130,25 @@ int ReadFileToStreamBySize(const char *filePath, char **stream, int readSize)
return 0;
}
int HnpWriteInfoToFile(const char* filePath, char *buff, int len)
{
FILE *fp = fopen(filePath, "w");
if (fp == NULL) {
HNP_LOGE("open file:%s unsuccess!", filePath);
return HNP_ERRNO_BASE_FILE_OPEN_FAILED;
}
int writeLen = fwrite(buff, sizeof(char), len, fp);
if (writeLen != len) {
HNP_LOGE("write file:%s unsuccess! len=%d, write=%d", filePath, len, writeLen);
(void)fclose(fp);
return HNP_ERRNO_BASE_FILE_WRITE_FAILED;
}
(void)fclose(fp);
return 0;
}
int GetRealPath(char *srcPath, char *realPath)
{
char dstTmpPath[PATH_MAX];

View File

@ -25,28 +25,78 @@
extern "C" {
#endif
int HnpProgramRunCheck(const char *programName)
static int HnpPidGetByBinName(const char *programName, int *pids, int *count)
{
FILE *cmdOutput;
char cmdBuffer[BUFFER_SIZE];
char command[HNP_COMMAND_LEN];
int ret;
/* 对programName进行空格过滤防止外部命令注入 */
if (strchr(programName, ' ') != NULL) {
HNP_LOGE("hnp install program name[%s] inval", programName);
return HNP_ERRNO_BASE_PARAMS_INVALID;
}
int pidNum = 0;
/* programName为卸载命令输入的进程名拼接命令command */
if (sprintf_s(command, HNP_COMMAND_LEN, "pgrep -x %s", programName) < 0) {
HNP_LOGE("hnp install program[%s] run command sprintf unsuccess", programName);
HNP_LOGE("hnp uninstall program[%s] run command sprintf unsuccess", programName);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
cmdOutput = popen(command, "rb");
if (cmdOutput == NULL) {
HNP_LOGE("hnp uninstall program[%s] not found", programName);
return HNP_ERRNO_BASE_PROGRAM_NOT_FOUND;
}
while (fgets(cmdBuffer, sizeof(cmdBuffer), cmdOutput) != NULL) {
pids[pidNum++] = atoi(cmdBuffer);
if (pidNum >= MAX_PROCESSES) {
HNP_LOGI("hnp uninstall program[%s] num over size", programName);
break;
}
}
pclose(cmdOutput);
*count = pidNum;
return 0;
}
int HnpProgramRunCheck(const char *binName, const char *runPath)
{
int ret;
int pids[MAX_PROCESSES];
int count = 0;
char command[HNP_COMMAND_LEN];
char cmdBuffer[BUFFER_SIZE];
HNP_LOGI("process[%s] running check", binName);
/* 对programName进行空格过滤防止外部命令注入 */
if (strchr(binName, ' ') != NULL) {
HNP_LOGE("hnp uninstall program name[%s] inval", binName);
return HNP_ERRNO_BASE_PARAMS_INVALID;
}
ret = HnpPidGetByBinName(binName, pids, &count);
if ((ret != 0) || (count == 0)) { // 返回非0代表未找到进程对应的pid
return 0;
}
/* 判断进程是否运行 */
ret = system(command);
if (ret == 0) {
HNP_LOGE("hnp install program[%s] is running now", programName);
return HNP_ERRNO_PROGRAM_RUNNING;
for (int index = 0; index < count; index++) {
if (sprintf_s(command, HNP_COMMAND_LEN, "lsof -p %d", pids[index]) < 0) {
HNP_LOGE("hnp uninstall pid[%d] run check command sprintf unsuccess", pids[index]);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
FILE *cmdOutput = popen(command, "rb");
if (cmdOutput == NULL) {
HNP_LOGE("hnp uninstall pid[%d] not found", pids[index]);
continue;
}
while (fgets(cmdBuffer, sizeof(cmdBuffer), cmdOutput) != NULL) {
if (strstr(cmdBuffer, runPath) != NULL) {
pclose(cmdOutput);
HNP_LOGE("hnp install process[%s] is running now", binName);
return HNP_ERRNO_PROGRAM_RUNNING;
}
}
pclose(cmdOutput);
}
return 0;

View File

@ -44,13 +44,15 @@ int HnpShowHelp(int argc, char *argv[])
HNP_LOGI("\r\nusage:hnp <command> <args>\r\n"
"\r\nThese are common hnp commands used in various situations:\r\n"
"\r\nIf the [package name] is null, it represents a public install/nuninstall,\r\n"
" or it represents a private install/nuninstall\r\n"
"\r\ninstall: install native software"
"\r\n hnp install [user id] [hnp package dir] <-f>\r\n"
"\r\n hnp install [user id] [hnp package dir] [package name] <-f>\r\n"
"\r\nuninstall: uninstall native software"
"\r\n hnp uninstall [user id] [software name] [software version]\r\n"
"\r\n hnp uninstall [user id] [software name] [software version] [package name]\r\n"
"\r\nfor example:\r\n"
"\r\n hnp install 1000 /usr1/hnp -f\r\n"
" hnp uninstall 1000 native_sample 1.1\r\n");
"\r\n hnp install 1000 /usr1/hnp wechat -f\r\n"
" hnp uninstall 1000 native_sample 1.1 wechat\r\n");
return 0;
}

View File

@ -24,6 +24,7 @@ extern "C" {
typedef struct NativeHnpPathStru {
char hnpProgramName[MAX_FILE_PATH_LEN];
char hnpBasePath[MAX_FILE_PATH_LEN];
char hnpProgramPath[MAX_FILE_PATH_LEN];
char hnpVersionPath[MAX_FILE_PATH_LEN];
} NativeHnpPath;
@ -52,8 +53,13 @@ typedef struct NativeHnpPathStru {
// 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)
#define HNP_DEFAULT_INSTALL_ROOT_PATH "/data/app/el1/bundle/"
#define HNP_UNSTALL_INFO_FILE "hnp_uninstall.txt"
int HnpCmdInstall(int argc, char *argv[]);
int HnpCmdUnInstall(int argc, char *argv[]);

View File

@ -47,28 +47,38 @@ static int HnpGenerateSoftLinkAllByJson(const char *installPath, const char *dst
{
char srcFile[MAX_FILE_PATH_LEN];
char dstFile[MAX_FILE_PATH_LEN];
char uninstallFile[MAX_FILE_PATH_LEN];
NativeBinLink *currentLink = hnpHead->links;
char *fileName;
int ret;
char *fileNameTmp;
if (access(dstPath, F_OK) != 0) {
int ret = mkdir(dstPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if ((ret != 0) && (errno != EEXIST)) {
HNP_LOGE("mkdir [%s] unsuccess, ret=%d, errno:%d", dstPath, ret, errno);
return HNP_ERRNO_BASE_MKDIR_PATH_FAILED;
}
}
for (unsigned int i = 0; i < hnpHead->linkNum; i++) {
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;
}
/* 如果target为空则使用源二进制名称 */
if (strcmp(currentLink->target, "") == 0) {
fileName = strrchr(currentLink->source, '/');
if (fileName == NULL) {
fileName = currentLink->source;
} else {
fileName++;
}
ret = sprintf_s(dstFile, MAX_FILE_PATH_LEN, "%s%s", dstPath, fileName);
fileNameTmp = currentLink->source;
} else {
ret = sprintf_s(dstFile, MAX_FILE_PATH_LEN, "%s%s", dstPath, currentLink->target);
fileNameTmp = currentLink->target;
}
fileName = strrchr(fileNameTmp, '/');
if (fileName == NULL) {
fileName = fileNameTmp;
} else {
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;
@ -82,7 +92,15 @@ static int HnpGenerateSoftLinkAllByJson(const char *installPath, const char *dst
currentLink++;
}
return 0;
int ret = sprintf_s(uninstallFile, MAX_FILE_PATH_LEN, "%s"HNP_UNSTALL_INFO_FILE, installPath);
if (ret < 0) {
HNP_LOGE("sprintf install info file unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
/* 向生成的hnp_unstall.txt文件头写入配置信息便于卸载 */
ret = HnpWriteInfoToFile(uninstallFile, (char*)hnpHead, hnpHead->headLen);
return ret;
}
static int HnpGenerateSoftLinkAll(const char *installPath, const char *dstPath)
@ -101,9 +119,18 @@ static int HnpGenerateSoftLinkAll(const char *installPath, const char *dstPath)
}
if ((dir = opendir(srcPath)) == NULL) {
HNP_LOGE("generate soft link opendir:%s unsuccess", srcPath);
return HNP_ERRNO_BASE_DIR_OPEN_FAILED;
HNP_LOGI("soft link bin file:%s not exist", srcPath);
return 0;
}
if (access(dstPath, F_OK) != 0) {
ret = mkdir(dstPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if ((ret != 0) && (errno != EEXIST)) {
HNP_LOGE("mkdir [%s] unsuccess, ret=%d, errno:%d", dstPath, ret, errno);
return HNP_ERRNO_BASE_MKDIR_PATH_FAILED;
}
}
while (((entry = readdir(dir)) != NULL)) {
/* 非二进制文件跳过 */
if (entry->d_type != DT_REG) {
@ -145,13 +172,6 @@ static int HnpGenerateSoftLink(const char *installPath, const char *hnpBasePath,
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
if (access(binPath, F_OK) != 0) {
ret = mkdir(binPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if ((ret != 0) && (errno != EEXIST)) {
HNP_LOGE("mkdir [%s] unsuccess, ret=%d, errno:%d", binPath, ret, errno);
}
}
if (hnpHead->linkNum == 0) {
ret = HnpGenerateSoftLinkAll(installPath, binPath);
} else {
@ -161,42 +181,158 @@ static int HnpGenerateSoftLink(const char *installPath, const char *hnpBasePath,
return ret;
}
static int HnpInstall(const char *hnpFile, const char *installPath, const char *hnpBasePath, NativeHnpHead *hnpHead)
static int HnpInstall(const char *hnpFile, NativeHnpPath *hnpDstPath, NativeHnpHead *hnpHead)
{
int ret;
/* 解压hnp文件 */
ret = HnpUnZip(hnpFile, installPath);
ret = HnpUnZip(hnpFile, hnpDstPath->hnpVersionPath);
if (ret != 0) {
return ret; /* 内部已打印日志 */
}
/* 生成软链 */
return HnpGenerateSoftLink(installPath, hnpBasePath, hnpHead);
return HnpGenerateSoftLink(hnpDstPath->hnpVersionPath, hnpDstPath->hnpBasePath, hnpHead);
}
static int HnpUnInstall(const char *uninstallPath, const char *programName)
static int HnpProgramRunCheckWithFile(const char *file, NativeHnpPath *hnpDstPath, const char *versionPath)
{
int ret;
NativeHnpHead *hnpHead;
NativeBinLink *currentLink;
char *fileName;
HNP_LOGI("\r\n hnp uninstall start now! path=%s program name[%s]", uninstallPath, programName);
ret = HnpReadFromZipHead(file, &hnpHead);
if (ret != 0) {
return ret; /* 内部已打印日志 */
}
currentLink = hnpHead->links;
for (unsigned int i = 0; i < hnpHead->linkNum; i++) {
/* 如果target为空则使用源二进制名称 */
if (strcmp(currentLink->target, "") == 0) {
fileName = strrchr(currentLink->source, '/');
if (fileName == NULL) {
fileName = currentLink->source;
} else {
fileName++;
}
ret = HnpProgramRunCheck(fileName, versionPath);
} else {
ret = HnpProgramRunCheck(currentLink->target, versionPath);
}
if (ret != 0) {
free(hnpHead);
return ret;
}
currentLink++;
}
free(hnpHead);
return 0;
}
static int HnpProgramRunCheckWithPath(const char *path, NativeHnpPath *hnpDstPath, const char *versionPath)
{
DIR *dir;
struct dirent *entry;
int ret;
if ((dir = opendir(path)) == NULL) {
HNP_LOGE("uninstall run check opendir:%s unsuccess", path);
return 0; /* 无bin文件继续删除目录 */
}
while (((entry = readdir(dir)) != NULL)) {
/* 非二进制文件跳过 */
if (entry->d_type != DT_REG) {
continue;
}
/* 查询软件是否正在运行 */
ret = HnpProgramRunCheck(entry->d_name, versionPath);
if (ret != 0) {
closedir(dir);
return ret;
}
}
closedir(dir);
return 0;
}
static int HnpUnInstall(NativeHnpPath *hnpDstPath, const char *versionPath, bool runCheck)
{
int ret;
char uninstallFile[MAX_FILE_PATH_LEN];
char binPath[MAX_FILE_PATH_LEN];
HNP_LOGI("hnp uninstall start now! path=%s program name[%s], run check=%d", hnpDstPath->hnpProgramPath,
hnpDstPath->hnpProgramName, runCheck);
if (runCheck == false) {
ret = HnpDeleteFolder(hnpDstPath->hnpProgramPath);
HNP_LOGI("hnp uninstall end! ret=%d", ret);
return ret;
}
ret = sprintf_s(uninstallFile, MAX_FILE_PATH_LEN, "%s/"HNP_UNSTALL_INFO_FILE, versionPath);
if (ret < 0) {
HNP_LOGE("sprintf uninstall info file unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
if (access(uninstallFile, F_OK) == 0) {
ret = HnpProgramRunCheckWithFile(uninstallFile, hnpDstPath, versionPath);
} else {
ret = sprintf_s(binPath, MAX_FILE_PATH_LEN, "%s/bin", versionPath);
if (ret < 0) {
HNP_LOGE("sprintf uninstall info file unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
ret = HnpProgramRunCheckWithPath(binPath, hnpDstPath, versionPath);
}
/* 查询软件是否正在运行 */
ret = HnpProgramRunCheck(programName);
if (ret != 0) {
return ret;
}
ret = HnpDeleteFolder(uninstallPath);
HNP_LOGI("\r\n hnp uninstall end! ret=%d", ret);
ret = HnpDeleteFolder(hnpDstPath->hnpProgramPath);
HNP_LOGI("hnp uninstall end! ret=%d", ret);
return ret;
}
static int HnpInstallPathGet(const char *fileName, const char *basePath, bool isForce, char* hnpVersion,
NativeHnpPath *hnpDstPath)
static int HnpGetVersionPathInProgramPath(const char *programPath, char *versionPath)
{
DIR *dir;
struct dirent *entry;
int ret;
if ((dir = opendir(programPath)) == NULL) {
HNP_LOGE("get version file opendir:%s unsuccess", programPath);
return HNP_ERRNO_BASE_FILE_OPEN_FAILED;
}
while (((entry = readdir(dir)) != NULL)) {
if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) {
continue;
}
ret = sprintf_s(versionPath, MAX_FILE_PATH_LEN, "%s/%s", programPath, entry->d_name);
if (ret < 0) {
closedir(dir);
HNP_LOGE("get version file sprintf_s unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
HNP_LOGI("get version file:%s success", versionPath);
closedir(dir);
return 0;
}
closedir(dir);
return HNP_ERRNO_INSTALLER_VERSION_FILE_GET_FAILED;
}
static int HnpInstallPathGet(const char *fileName, bool isForce, char* hnpVersion, NativeHnpPath *hnpDstPath)
{
int ret;
char *hnpNameTmp;
char versionOldPath[MAX_FILE_PATH_LEN];
/* 裁剪获取文件名使用 */
ret = strcpy_s(hnpDstPath->hnpProgramName, MAX_FILE_PATH_LEN, fileName);
@ -212,25 +348,13 @@ static int HnpInstallPathGet(const char *fileName, const char *basePath, bool is
}
/* 拼接安装路径 */
ret = sprintf_s(hnpDstPath->hnpProgramPath, MAX_FILE_PATH_LEN, "%s%s.org/", basePath, hnpDstPath->hnpProgramName);
ret = sprintf_s(hnpDstPath->hnpProgramPath, MAX_FILE_PATH_LEN, "%s%s.org/", hnpDstPath->hnpBasePath,
hnpDstPath->hnpProgramName);
if (ret < 0) {
HNP_LOGE("hnp install sprintf hnp base path unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
/* 判断安装目录是否存在,存在判断是否是强制安装,如果是则走卸载流程,否则返回错误 */
if (access(hnpDstPath->hnpProgramPath, F_OK) == 0) {
if (isForce == false) {
HNP_LOGE("hnp install path[%s] exist, but force is false", hnpDstPath->hnpProgramPath);
return HNP_ERRNO_INSTALLER_PATH_IS_EXIST;
} else {
ret = HnpUnInstall(hnpDstPath->hnpProgramPath, hnpDstPath->hnpProgramName);
if (ret != 0) {
return ret;
}
}
}
/* 拼接安装路径 */
ret = sprintf_s(hnpDstPath->hnpVersionPath, MAX_FILE_PATH_LEN, "%s%s_%s/", hnpDstPath->hnpProgramPath,
hnpDstPath->hnpProgramName, hnpVersion);
@ -239,6 +363,23 @@ static int HnpInstallPathGet(const char *fileName, const char *basePath, bool is
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
/* 判断安装目录是否存在,存在判断是否是强制安装,如果是则走卸载流程,否则返回错误 */
if (access(hnpDstPath->hnpProgramPath, F_OK) == 0) {
if (isForce == false) {
HNP_LOGE("hnp install path[%s] exist, but force is false", hnpDstPath->hnpProgramPath);
return HNP_ERRNO_INSTALLER_PATH_IS_EXIST;
} else {
ret = HnpGetVersionPathInProgramPath(hnpDstPath->hnpProgramPath, versionOldPath);
if (ret != 0) {
return ret;
}
ret = HnpUnInstall(hnpDstPath, versionOldPath, true);
if (ret != 0) {
return ret;
}
}
}
ret = HnpCreateFolder(hnpDstPath->hnpVersionPath);
if (ret != 0) {
return HnpDeleteFolder(hnpDstPath->hnpVersionPath);
@ -246,11 +387,10 @@ static int HnpInstallPathGet(const char *fileName, const char *basePath, bool is
return ret;
}
static int HnpDirReadAndInstall(const char *srcPath, const char *basePath, bool isForce)
static int HnpDirReadAndInstall(const char *srcPath, NativeHnpPath *hnpDstPath, bool isForce)
{
struct dirent *entry;
char hnpFile[MAX_FILE_PATH_LEN];
NativeHnpPath hnpDstPath;
NativeHnpHead *hnpHead;
int count = 0;
@ -280,7 +420,7 @@ static int HnpDirReadAndInstall(const char *srcPath, const char *basePath, bool
}
/* 获取安装路径 */
ret = HnpInstallPathGet(entry->d_name, basePath, isForce, hnpHead->hnpVersion, &hnpDstPath);
ret = HnpInstallPathGet(entry->d_name, isForce, hnpHead->hnpVersion, hnpDstPath);
if (ret != 0) {
closedir(dir);
free(hnpHead);
@ -288,17 +428,17 @@ static int HnpDirReadAndInstall(const char *srcPath, const char *basePath, bool
}
/* hnp安装 */
ret = HnpInstall(hnpFile, hnpDstPath.hnpVersionPath, basePath, hnpHead);
ret = HnpInstall(hnpFile, hnpDstPath, hnpHead);
if (ret != 0) {
closedir(dir);
free(hnpHead);
/* 安装失败卸载当前包 */
HnpUnInstall(hnpDstPath.hnpProgramPath, hnpDstPath.hnpProgramName);
HnpUnInstall(hnpDstPath, hnpDstPath->hnpVersionPath, false);
return ret;
}
free(hnpHead);
hnpHead = NULL;
HNP_LOGI("install hnp path[%s], dst path[%s]", srcPath, hnpDstPath.hnpProgramName, hnpDstPath.hnpVersionPath);
HNP_LOGI("install hnp path[%s], dst path[%s]", srcPath, hnpDstPath->hnpVersionPath);
count++;
}
closedir(dir);
@ -312,12 +452,12 @@ int HnpCmdInstall(int argc, char *argv[])
{
char srcPath[MAX_FILE_PATH_LEN];
char dstPath[MAX_FILE_PATH_LEN];
char basePath[MAX_FILE_PATH_LEN];
unsigned long uid;
bool isForce = false;
int ret;
NativeHnpPath hnpDstPath = {0};
if (argc < HNP_INDEX_4) {
if (argc < HNP_INDEX_5) {
HNP_LOGE("hnp install args num[%u] unsuccess!", argc);
return HNP_ERRNO_INSTALLER_ARGV_NUM_INVALID;
}
@ -341,29 +481,35 @@ int HnpCmdInstall(int argc, char *argv[])
}
/* 获取参数是否需要强制覆盖 */
if ((argc == HNP_INDEX_5) && (strcmp(argv[HNP_INDEX_4], "-f") == 0)) {
if ((argc == HNP_INDEX_6) && (strcmp(argv[HNP_INDEX_5], "-f") == 0)) {
isForce = true;
}
if (sprintf_s(basePath, MAX_FILE_PATH_LEN, "%shnp/", dstPath) < 0) {
HNP_LOGE("hnp install base path sprintf unsuccess.");
if (strcmp(argv[HNP_INDEX_4], "null") == 0) {
ret = sprintf_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, "%shnppublic/", dstPath);
} else {
ret = sprintf_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, "%shnp/%s/", dstPath, argv[HNP_INDEX_4]);
}
if (ret < 0) {
HNP_LOGE("hnp install public base path sprintf unsuccess.");
return HNP_ERRNO_INSTALLER_GET_HNP_PATH_FAILED;
}
HNP_LOGI("\r\n hnp install start now! src path=%s, dst path=%s, is force=%d", argv[HNP_INDEX_3], dstPath, isForce);
ret = HnpDirReadAndInstall(srcPath, basePath, isForce);
HNP_LOGI("\r\n hnp install end, ret=%d", ret);
HNP_LOGI("hnp install start now! src path=%s, dst path=%s, is force=%d", argv[HNP_INDEX_3], hnpDstPath.hnpBasePath,
isForce);
ret = HnpDirReadAndInstall(srcPath, &hnpDstPath, isForce);
HNP_LOGI("hnp install end, ret=%d", ret);
return ret;
}
int HnpCmdUnInstall(int argc, char *argv[])
{
unsigned long uid;
char uninstallPath[MAX_FILE_PATH_LEN];
char basePath[MAX_FILE_PATH_LEN];
char pathTmp[MAX_FILE_PATH_LEN];
NativeHnpPath hnpDstPath = {0};
int ret;
if (argc < HNP_INDEX_5) {
if (argc < HNP_INDEX_6) {
HNP_LOGE("hnp uninstall args num[%u] unsuccess!", argc);
return HNP_ERRNO_UNINSTALLER_ARGV_NUM_INVALID;
}
@ -374,28 +520,46 @@ int HnpCmdUnInstall(int argc, char *argv[])
return HNP_ERRNO_INSTALLER_ARGV_UID_INVALID;
}
/* 拼接卸载路径 */
if (sprintf_s(uninstallPath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/hnp/%s.org/%s_%s/", uid,
argv[HNP_INDEX_3], argv[HNP_INDEX_3], argv[HNP_INDEX_4]) < 0) {
HNP_LOGE("hnp uninstall path sprintf unsuccess, uid:%lu program name[%s], version[%s]", uid,
if (strcmp(argv[HNP_INDEX_5], "null") == 0) {
ret = sprintf_s(pathTmp, MAX_FILE_PATH_LEN, "hnppublic");
} else {
ret = sprintf_s(pathTmp, MAX_FILE_PATH_LEN, "hnp/%s", argv[HNP_INDEX_5]);
}
if (ret < 0) {
HNP_LOGE("hnp uninstall path sprintf unsuccess.");
return HNP_ERRNO_BASE_COPY_FAILED;
}
if (sprintf_s(hnpDstPath.hnpVersionPath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/%s/%s.org/%s_%s/",
uid, pathTmp, argv[HNP_INDEX_3], argv[HNP_INDEX_3], argv[HNP_INDEX_4]) < 0) {
HNP_LOGE("hnp uninstall path sprintf unsuccess, uid:%lu program name[%s], version[%s]", uid,
argv[HNP_INDEX_3], argv[HNP_INDEX_4]);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
/* 校验目标目录是否存在判断是否安装 */
if (access(uninstallPath, F_OK) != 0) {
HNP_LOGE("hnp uninstall path:%s is not exist", uninstallPath);
if (access(hnpDstPath.hnpVersionPath, F_OK) != 0) {
HNP_LOGE("hnp uninstall path:%s is not exist", hnpDstPath.hnpVersionPath);
return HNP_ERRNO_UNINSTALLER_HNP_PATH_NOT_EXIST;
}
/* 拼接基本路径 */
if (sprintf_s(basePath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/hnp/%s.org/", uid,
argv[HNP_INDEX_3]) < 0) {
if (sprintf_s(hnpDstPath.hnpProgramPath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/%s/%s.org/", uid,
pathTmp, argv[HNP_INDEX_3]) < 0) {
HNP_LOGE("hnp uninstall pro path sprintf unsuccess, uid:%lu, program name[%s]", uid, argv[HNP_INDEX_3]);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
if (sprintf_s(hnpDstPath.hnpBasePath, MAX_FILE_PATH_LEN, HNP_DEFAULT_INSTALL_ROOT_PATH"%lu/%s/", uid,
pathTmp) < 0) {
HNP_LOGE("hnp uninstall base path sprintf unsuccess, uid:%lu, program name[%s]", uid, argv[HNP_INDEX_3]);
return HNP_ERRNO_BASE_SPRINTF_FAILED;
}
if (strcpy_s(hnpDstPath.hnpProgramName, MAX_FILE_PATH_LEN, argv[HNP_INDEX_3]) != EOK) {
HNP_LOGE("hnp uninstall strcpy unsuccess.");
return HNP_ERRNO_BASE_COPY_FAILED;
}
return HnpUnInstall(basePath, argv[HNP_INDEX_3]);
return HnpUnInstall(&hnpDstPath, hnpDstPath.hnpVersionPath, true);
}
#ifdef __cplusplus

View File

@ -31,13 +31,17 @@ if (!defined(ohos_lite)) {
include_dirs = [
"${appspawn_path}/service/hnp/base",
"${appspawn_path}/service/hnp/pack/include",
"${appspawn_path}/service/hnp/installer/include",
]
sources = [
"${appspawn_path}/service/hnp/base/hnp_file.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",
"${appspawn_path}/service/hnp/installer/src/hnp_installer.c",
"${appspawn_path}/service/hnp/pack/src/hnp_pack.c",
"hnp_installer_test.cpp",
"hnp_pack_test.cpp",
]

View File

@ -0,0 +1,730 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdio>
#include <cerrno>
#include "hnp_base.h"
#include "hnp_pack.h"
#include "hnp_installer.h"
#include "securec.h"
using namespace testing;
using namespace testing::ext;
#define HNP_BASE_PATH "/data/app/el1/bundle/10000"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
namespace OHOS {
class HnpInstallerTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
};
void HnpInstallerTest::SetUpTestCase()
{
GTEST_LOG_(INFO) << "Hnp_Installer_TEST SetUpTestCase";
}
void HnpInstallerTest::TearDownTestCase()
{
GTEST_LOG_(INFO) << "Hnp_Installer_TEST TearDownTestCase";
}
void HnpInstallerTest::SetUp()
{
GTEST_LOG_(INFO) << "Hnp_Installer_TEST SetUp";
}
void HnpInstallerTest::TearDown()
{
GTEST_LOG_(INFO) << "Hnp_Installer_TEST TearDown";
}
void HnpPackWithoutBin(void)
{
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[] = "hnpcli";
char arg2[] = "pack";
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};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), 0);
}
void HnpPackWithoutBinDelete(void)
{
int ret;
ret = remove("hnp_out/sample.hnp");
EXPECT_EQ(ret, 0);
EXPECT_EQ(rmdir("hnp_sample"), 0);
EXPECT_EQ(rmdir("hnp_out"), 0);
}
void HnpPackWithBin(void)
{
EXPECT_EQ(mkdir("hnp_sample", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_sample/bin", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
FILE *fp = fopen("hnp_sample/bin/out", "wb");
EXPECT_NE(fp, nullptr);
(void)fclose(fp);
EXPECT_EQ(mkdir("hnp_out", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
char arg1[] = "hnpcli";
char arg2[] = "pack";
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};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdPack(argc, argv), 0);
}
void HnpPackWithBinDelete(void)
{
int ret;
ret = remove("hnp_out/sample.hnp");
EXPECT_EQ(ret, 0);
ret = remove("hnp_sample/bin/out");
EXPECT_EQ(ret, 0);
EXPECT_EQ(rmdir("hnp_sample/bin"), 0);
EXPECT_EQ(rmdir("hnp_sample"), 0);
EXPECT_EQ(rmdir("hnp_out"), 0);
}
void HnpPackWithCfg(void)
{
FILE *fp;
int whiteLen;
EXPECT_EQ(mkdir("hnp_sample", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_sample/bin", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir("hnp_out", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
fp = fopen("hnp_sample/bin/out", "wb");
EXPECT_NE(fp, NULL);
(void)fclose(fp);
fp = fopen("hnp_sample/bin/out2", "wb");
EXPECT_NE(fp, NULL);
(void)fclose(fp);
fp = fopen("hnp.cfg", "w");
EXPECT_NE(fp, nullptr);
(void)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};
int argc = sizeof(argv) / sizeof(argv[0]);
char cfg[] = "{\"type\":\"hnp-config\",\"name\":\"sample\",\"version\":\"1.1\",\"install\":"
"{\"links\":[{\"source\":\"bin/out\",\"target\":\"outt\"},{\"source\":\"bin/out2\","
"\"target\":\"out2\"}]}}";
fp = fopen("./hnp.cfg", "w");
whiteLen = fwrite(cfg, sizeof(char), strlen(cfg) + 1, fp);
(void)fclose(fp);
EXPECT_EQ(whiteLen, strlen(cfg) + 1);
EXPECT_EQ(HnpCmdPack(argc, argv), 0);
}
void HnpPackWithCfgDelete(void)
{
int ret;
ret = remove("hnp_out/sample.hnp");
EXPECT_EQ(ret, 0);
ret = remove("hnp_sample/bin/out");
EXPECT_EQ(ret, 0);
ret = remove("hnp_sample/bin/out2");
EXPECT_EQ(ret, 0);
ret = remove("hnp.cfg");
EXPECT_EQ(ret, 0);
EXPECT_EQ(rmdir("hnp_sample/bin"), 0);
EXPECT_EQ(rmdir("hnp_sample"), 0);
EXPECT_EQ(rmdir("hnp_out"), 0);
}
void HnpInstall(void)
{
char arg1[] = "hnp";
char arg2[] = "install";
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char arg6[] = "-f";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
}
void HnpUnInstall(void)
{
char arg1[] = "hnp";
char arg2[] = "uninstall";
char arg3[] = "10000";
char arg4[] = "hnp_sample";
char arg5[] = "1.1";
char arg6[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), 0);
}
void HnpInstallPrivate(void)
{
char arg1[] = "hnp";
char arg2[] = "uninstall";
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "test";
char arg6[] = "-f";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
}
void HnpUnInstallPrivate(void)
{
char arg1[] = "hnp";
char arg2[] = "uninstall";
char arg3[] = "10000";
char arg4[] = "hnp_sample";
char arg5[] = "1.1";
char arg6[] = "test";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), 0);
}
/**
* @tc.name: Hnp_Install_001
* @tc.desc: Verify set Arg if HnpCmdInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_Install_001, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Installer_001 start";
// clear resource before test
remove("hnp_out/sample.hnp");
remove("hnp_sample/bin/out");
rmdir("hnp_sample/bin");
rmdir("hnp_sample");
rmdir("hnp_out");
HnpDeleteFolder(HNP_BASE_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
char arg1[] = "hnp";
char arg2[] = "install";
{ // param num not enough
char* argv[] = {arg1, arg2};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), HNP_ERRNO_INSTALLER_ARGV_NUM_INVALID);
}
{ // param uid is invalid
char arg3[] = "asd1231";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), HNP_ERRNO_INSTALLER_ARGV_UID_INVALID);
}
{ // ok
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnppublic/bin/out", F_OK), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_Installer_001 end";
}
/**
* @tc.name: Hnp_Install_002
* @tc.desc: Verify install path get if HnpCmdInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_Install_002, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Installer_002 start";
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
char arg1[] = "hnp";
char arg2[] = "install";
{ // dir exist but force is false
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(HnpCmdInstall(argc, argv), HNP_ERRNO_INSTALLER_PATH_IS_EXIST);
}
{ //ok
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char arg6[] = "-f";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnppublic/bin/out", F_OK), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_Installer_002 end";
}
/**
* @tc.name: Hnp_Install_003
* @tc.desc: Verify scr path bin not exist HnpCmdInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_Install_003, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Installer_003 start";
char arg1[] = "hnp";
char arg2[] = "install";
{ // scr path bin not exist
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithoutBin();
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnppublic/bin/out", F_OK), -1);
HnpPackWithoutBinDelete();
HnpDeleteFolder(HNP_BASE_PATH);
}
{ //ok
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnppublic/bin/out", F_OK), 0);
HnpPackWithBinDelete();
}
HnpDeleteFolder(HNP_BASE_PATH);
GTEST_LOG_(INFO) << "Hnp_Installer_003 end";
}
/**
* @tc.name: Hnp_Install_004
* @tc.desc: Verify src path file is not hnp cli generate if HnpCmdInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_Install_004, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Installer_004 start";
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
char arg1[] = "hnp";
char arg2[] = "install";
{ //src path file is not hnp and size is small than head
FILE *fp = fopen("./hnp_out/example.zip", "wb");
int data[5] = {1, 2, 3, 4, 5};
EXPECT_NE(fp, NULL);
fwrite(data, sizeof(int), 5, fp);
(void)fclose(fp);
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char arg6[] = "-f";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), HNP_ERRNO_BASE_FILE_READ_FAILED);
remove("./hnp_out/example.zip");
}
{ //src path file is not hnp
FILE *fp = fopen("./hnp_out/example.zip", "wb");
int data[15] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
EXPECT_NE(fp, NULL);
fwrite(data, sizeof(int), 15, fp);
(void)fclose(fp);
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char arg6[] = "-f";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), HNP_ERRNO_BASE_MAGIC_CHECK_FAILED);
remove("./hnp_out/example.zip");
}
{ //ok
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char arg6[] = "-f";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnppublic/bin/out", F_OK), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_Installer_004 end";
}
/**
* @tc.name: Hnp_Install_005
* @tc.desc: Verify more than 2 link if HnpCmdInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_Install_005, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Installer_005 start";
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithCfg();
char arg1[] = "hnp";
char arg2[] = "install";
{ //ok
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char arg6[] = "-f";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnppublic/bin/outt", F_OK), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnppublic/bin/out2", F_OK), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_Installer_005 end";
}
/**
* @tc.name: Hnp_Install_006
* @tc.desc: Verify private HnpCmdInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_Install_006, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Install_006 start";
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithCfg();
char arg1[] = "hnp";
char arg2[] = "install";
{ //ok
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "test";
char arg6[] = "-f";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnp/test/bin/outt", F_OK), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnp/test/bin/out2", F_OK), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_Install_006 end";
}
/**
* @tc.name: Hnp_Install_007
* @tc.desc: Verify set Arg if HnpCmdInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_Install_007, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Install_007 start";
// clear resource before test
remove("hnp_out/sample.hnp");
remove("hnp_sample/bin/out");
rmdir("hnp_sample/bin");
rmdir("hnp_sample");
rmdir("hnp_out");
HnpDeleteFolder(HNP_BASE_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
char arg1[] = "hnp";
char arg2[] = "install";
{ // src dir path is invalid
char arg3[] = "10000";
char arg4[] = "./hnp_in";
char arg5[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), HNP_ERRNO_INSTALLER_GET_REALPATH_FAILED);
}
{ // dst dir path is invalid
char arg3[] = "10001";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), HNP_ERRNO_INSTALLER_GET_REALPATH_FAILED);
}
{ // ok
char arg3[] = "10000";
char arg4[] = "./hnp_out";
char arg5[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdInstall(argc, argv), 0);
EXPECT_EQ(access(HNP_BASE_PATH"/hnppublic/bin/out", F_OK), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_Install_007 end";
}
/**
* @tc.name: Hnp_UnInstall_001
* @tc.desc: Verify set Arg if HnpCmdUnInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_UnInstall_001, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_UnInstall_001 start";
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
HnpInstall();
char arg1[] = "hnp";
char arg2[] = "uninstall";
{ // param num not enough
char* argv[] = {arg1, arg2};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), HNP_ERRNO_UNINSTALLER_ARGV_NUM_INVALID);
}
{ // param uid is invalid
char arg3[] = "asd1231";
char arg4[] = "sample";
char arg5[] = "1.1";
char arg6[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), HNP_ERRNO_INSTALLER_ARGV_UID_INVALID);
}
{ // ok
char arg3[] = "10000";
char arg4[] = "sample";
char arg5[] = "1.1";
char arg6[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_UnInstall_001 end";
}
/**
* @tc.name: Hnp_UnInstall_002
* @tc.desc: Verify cfg pack HnpCmdUnInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_UnInstall_002, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_UnInstall_002 start";
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithCfg();
HnpInstallPrivate();
char arg1[] = "hnp";
char arg2[] = "uninstall";
{ // ok
char arg3[] = "10000";
char arg4[] = "sample";
char arg5[] = "1.1";
char arg6[] = "test";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_UnInstall_002 end";
}
/**
* @tc.name: Hnp_UnInstall_003
* @tc.desc: Verify set Arg if HnpCmdUnInstall succeed.
* @tc.type: FUNC
* @tc.require:issueI9BU5F
* @tc.author:
*/
HWTEST(HnpInstallerTest, Hnp_UnInstall_003, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_UnInstall_003 start";
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
HnpInstall();
char arg1[] = "hnp";
char arg2[] = "uninstall";
{ // param software name is invalid
char arg3[] = "10000";
char arg4[] = "sample2";
char arg5[] = "1.1";
char arg6[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), HNP_ERRNO_UNINSTALLER_HNP_PATH_NOT_EXIST);
}
{ // param software version is invalid
char arg3[] = "10001";
char arg4[] = "sample";
char arg5[] = "1.3";
char arg6[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), HNP_ERRNO_UNINSTALLER_HNP_PATH_NOT_EXIST);
}
{ // ok
char arg3[] = "10000";
char arg4[] = "sample";
char arg5[] = "1.1";
char arg6[] = "null";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv) / sizeof(argv[0]);
EXPECT_EQ(HnpCmdUnInstall(argc, argv), 0);
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_UnInstall_003 end";
}
}