!1057 HNP代码告警处理以及UT优化

Merge pull request !1057 from 王达/codecheck_20240418
This commit is contained in:
openharmony_ci 2024-04-25 04:20:42 +00:00 committed by Gitee
commit 36c76f10d7
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 40 additions and 8 deletions

View File

@ -110,6 +110,7 @@ int ReadFileToStreamBySize(const char *filePath, char **stream, int readSize)
}
if (readSize <= 0) {
(void)fclose(file);
HNP_LOGE("read size(%d) is invalid.", readSize);
return HNP_ERRNO_BASE_PARAMS_INVALID;
}
@ -213,6 +214,7 @@ int HnpDeleteFolder(const char *path)
/* 如果是文件夹,递归删除 */
ret = HnpDeleteFolder(filePath);
if (ret != 0) {
closedir(dir);
return ret;
}
} else {

View File

@ -227,10 +227,7 @@ static int HnpUnZipForFile(const char *fileName, const char *outputDir, unzFile
return 0;
#else
int ret;
FILE *outFile;
char filePath[MAX_FILE_PATH_LEN];
char buffer[BUFFER_SIZE];
int readSize = 0;
ret = sprintf_s(filePath, MAX_FILE_PATH_LEN, "%s/%s", outputDir, fileName);
if (ret < 0) {
@ -242,13 +239,15 @@ static int HnpUnZipForFile(const char *fileName, const char *outputDir, unzFile
if (filePath[strlen(filePath) - 1] == '/') {
mkdir(filePath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
} else {
outFile = fopen(filePath, "wb");
FILE *outFile = fopen(filePath, "wb");
if (outFile == NULL) {
HNP_LOGE("unzip open file:%s unsuccess!", filePath);
return HNP_ERRNO_BASE_FILE_OPEN_FAILED;
}
unzOpenCurrentFile(zipFile);
int readSize = 0;
do {
char buffer[BUFFER_SIZE];
readSize = unzReadCurrentFile(zipFile, buffer, sizeof(buffer));
if (readSize < 0) {
HNP_LOGE("unzip read zip:%s file unsuccess", zipFile);
@ -273,7 +272,6 @@ int HnpUnZip(const char *inputFile, const char *outputDir)
int result;
char fileName[MAX_FILE_PATH_LEN];
unz_file_info fileInfo;
char *slash;
HNP_LOGI("HnpUnZip zip=%s, output=%s", inputFile, outputDir);
@ -291,7 +289,7 @@ int HnpUnZip(const char *inputFile, const char *outputDir)
unzClose(zipFile);
return HNP_ERRNO_BASE_UNZIP_GET_INFO_FAILED;
}
slash = strchr(fileName, '/');
char *slash = strchr(fileName, '/');
if (slash != NULL) {
slash++;
} else {

View File

@ -48,7 +48,6 @@ static int HnpGenerateSoftLinkAllByJson(const char *installPath, const char *dst
char srcFile[MAX_FILE_PATH_LEN];
char dstFile[MAX_FILE_PATH_LEN];
NativeBinLink *currentLink = hnpCfg->links;
char *fileName;
char *fileNameTmp;
if (access(dstPath, F_OK) != 0) {
@ -61,6 +60,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);
char *fileName;
if (ret < 0) {
HNP_LOGE("sprintf install bin src file unsuccess.");
return HNP_ERRNO_BASE_SPRINTF_FAILED;
@ -117,6 +117,7 @@ static int HnpGenerateSoftLinkAll(const char *installPath, const char *dstPath)
if (access(dstPath, F_OK) != 0) {
ret = mkdir(dstPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if ((ret != 0) && (errno != EEXIST)) {
closedir(dir);
HNP_LOGE("mkdir [%s] unsuccess, ret=%d, errno:%d", dstPath, ret, errno);
return HNP_ERRNO_BASE_MKDIR_PATH_FAILED;
}

View File

@ -29,7 +29,6 @@ extern "C" {
int main(int argc, char *argv[])
{
int sectime = 0;
int ch;
printf("\nstart hnp sample");
@ -44,6 +43,7 @@ int main(int argc, char *argv[])
FILE *file = fopen("../cfg/hnpsample.cfg", "r");
if (file != NULL) {
printf("\ncfg file content:\n");
int ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}

View File

@ -36,6 +36,7 @@
using namespace testing;
using namespace testing::ext;
#define HNP_UID_PATH "/data/app/el1/bundle/10000"
#define HNP_BASE_PATH "/data/app/el1/bundle/10000/hnp"
#ifdef __cplusplus
@ -319,6 +320,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_001, TestSize.Level0)
rmdir("hnp_out");
HnpDeleteFolder(HNP_BASE_PATH);
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
@ -354,6 +356,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_001, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_Installer_001 end";
@ -370,6 +373,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_002, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Installer_002 start";
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
@ -400,6 +404,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_002, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_Installer_002 end";
@ -418,6 +423,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_003, TestSize.Level0)
char arg1[] = "hnp";
char arg2[] = "install";
HnpCreateFolder(HNP_UID_PATH);
{ // scr path bin not exist
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
@ -449,6 +455,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_003, TestSize.Level0)
HnpPackWithBinDelete();
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
GTEST_LOG_(INFO) << "Hnp_Installer_003 end";
}
@ -464,6 +471,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_004, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Installer_004 start";
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
@ -511,6 +519,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_004, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_Installer_004 end";
@ -527,6 +536,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_005, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Installer_005 start";
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithCfg();
@ -547,6 +557,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_005, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_Installer_005 end";
@ -563,6 +574,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_006, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_Install_006 start";
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir(HNP_BASE_PATH"/test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
@ -587,6 +599,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_006, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_Install_006 end";
@ -611,6 +624,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_007, TestSize.Level0)
rmdir("hnp_out");
HnpDeleteFolder(HNP_BASE_PATH);
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
@ -650,6 +664,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_007, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_Install_007 end";
@ -669,6 +684,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_API_001, TestSize.Level0)
int ret;
const char *packages[1] = {"./hnp_out/sample.hnp"};
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir(HNP_BASE_PATH"/test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
@ -686,6 +702,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_API_001, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_Install_API_001 end";
@ -705,6 +722,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_API_002, TestSize.Level0)
int ret;
const char *packages[1] = {"./hnp_out/sample.hnp"};
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir(HNP_BASE_PATH"/test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
@ -716,6 +734,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_API_002, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_Install_API_002 end";
@ -735,6 +754,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_API_003, TestSize.Level0)
int ret;
const char *packages[2] = {"./hnp_out/sample.hnp", "./hnp_out2/sample2.hnp"};
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir(HNP_BASE_PATH"/test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
@ -750,6 +770,7 @@ HWTEST(HnpInstallerTest, Hnp_Install_API_003, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithCfgDelete();
HnpPackWithBinSimple2Delete();
@ -767,6 +788,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_001, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_UnInstall_001 start";
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
HnpInstall();
@ -806,6 +828,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_001, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_UnInstall_001 end";
@ -822,6 +845,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_002, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_UnInstall_002 start";
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir(HNP_BASE_PATH"/test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithCfg();
@ -859,6 +883,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_002, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_UnInstall_002 end";
@ -875,6 +900,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_003, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Hnp_UnInstall_003 start";
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithBin();
HnpInstall();
@ -920,6 +946,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_003, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithBinDelete();
GTEST_LOG_(INFO) << "Hnp_UnInstall_003 end";
@ -938,6 +965,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_API_001, TestSize.Level0)
int ret;
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir(HNP_BASE_PATH"/test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithCfg();
@ -953,6 +981,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_API_001, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_UnInstall_API_001 end";
@ -971,6 +1000,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_API_002, TestSize.Level0)
int ret;
HnpCreateFolder(HNP_UID_PATH);
EXPECT_EQ(mkdir(HNP_BASE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
EXPECT_EQ(mkdir(HNP_BASE_PATH"/test", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH), 0);
HnpPackWithCfg();
@ -982,6 +1012,7 @@ HWTEST(HnpInstallerTest, Hnp_UnInstall_API_002, TestSize.Level0)
}
HnpDeleteFolder(HNP_BASE_PATH);
HnpDeleteFolder(HNP_UID_PATH);
HnpPackWithCfgDelete();
GTEST_LOG_(INFO) << "Hnp_UnInstall_API_002 end";