diff --git a/test/unittest/applypatch_test/all_cmd_unittest.cpp b/test/unittest/applypatch_test/all_cmd_unittest.cpp index 5e712fa2941302ce75a51996325c9a8bbf1b6ef3..489b5b26c8019a88193ccf9815019c8f363c2243 100755 --- a/test/unittest/applypatch_test/all_cmd_unittest.cpp +++ b/test/unittest/applypatch_test/all_cmd_unittest.cpp @@ -176,11 +176,11 @@ HWTEST_F(AllCmdUnitTest, allCmd_test_002, TestSize.Level1) utils::MkdirRecursive(TransferManager::GetTransferManagerInstance()->GetGlobalParams()->storeBase, dirMode); std::vector buffer(bufferSize, 0); int fd = open(filePath.c_str(), O_RDWR | O_CREAT, dirMode); - lseek64(fd, 0, SEEK_SET); - if (fd == -1) { + if (fd < 0) { printf("Failed to open block %s, errno: %d\n", filePath.c_str(), errno); return; } + lseek64(fd, 0, SEEK_SET); auto res = WriteTestBin(fd, *buffer.data(), bufferSize * count); if (!res) { printf("Write to bin error\n"); diff --git a/test/unittest/applypatch_test/applypatch_unittest.cpp b/test/unittest/applypatch_test/applypatch_unittest.cpp index 6cc3183311a2b3b94f335e254ec5877b3945d3e1..4f70a0ef14aed9042e3b0dd8c6f398847251e768 100755 --- a/test/unittest/applypatch_test/applypatch_unittest.cpp +++ b/test/unittest/applypatch_test/applypatch_unittest.cpp @@ -92,7 +92,7 @@ HWTEST_F(ApplyPatchUnitTest, updater_RawWriter, TestSize.Level1) EXPECT_TRUE(ret); int fd = open(devPath.c_str(), O_RDONLY); - EXPECT_GT(fd, 0); + EXPECT_GE(fd, 0); uint8_t buffer[BUFFER_LEN + 1] = {0}; size_t n = read(fd, buffer, BUFFER_LEN); @@ -116,7 +116,13 @@ HWTEST_F(ApplyPatchUnitTest, updater_DataWriterOpenPartition, TestSize.Level1) partitionName = "/rawwriter"; auto devPath = GetBlockDeviceByMountPoint(partitionName); - close(open(devPath.c_str(), O_CREAT | O_WRONLY | O_EXCL, 0664)); + int fd = open(devPath.c_str(), O_CREAT | O_WRONLY | O_EXCL, 0664); + if (fd < 0) { + printf("Open %s failed", devPath.c_str()); + FAIL(); + return; + } + close(fd); writer = DataWriter::CreateDataWriter(mode, partitionName); EXPECT_NE(writer, nullptr); ret = writer->OpenPartition(partitionName); diff --git a/test/unittest/applypatch_test/blockset_unittest.cpp b/test/unittest/applypatch_test/blockset_unittest.cpp index 6d948e84175a8a84a6b2d2652d55f5f51b23ea15..866765b274c23b058ed49a6bc849a5eee0cfb73c 100755 --- a/test/unittest/applypatch_test/blockset_unittest.cpp +++ b/test/unittest/applypatch_test/blockset_unittest.cpp @@ -87,7 +87,12 @@ HWTEST_F(BlockSetUnitTest, blockset_test_003, TestSize.Level1) std::fill(buffer.begin(), buffer.end(), 0); std::string filename = "/tmp/ut_blockset"; int fd = open(filename.c_str(), O_RDWR); + if (fd < 0) { + printf("Open file failed"); + return; + } blk.WriteDataToBlock(fd, buffer); + close(fd); } HWTEST_F(BlockSetUnitTest, blockset_test_004, TestSize.Level1) @@ -109,6 +114,10 @@ HWTEST_F(BlockSetUnitTest, blockset_test_005, TestSize.Level1) std::string blockInfo = "2,20755,21031 276 2,20306,20582"; std::string cmdLine = std::string("move ") + hashValue + " " + blockInfo; int fd = open("/data/updater/updater/blocksetTest.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO); + if (fd < 0) { + printf("Open file failed"); + return; + } Command *cmd = new Command(); cmd->Init(cmdLine); cmd->SetFileDescriptor(fd); @@ -122,5 +131,6 @@ HWTEST_F(BlockSetUnitTest, blockset_test_005, TestSize.Level1) ret = targetBlock.WriteDiffToBlock(const_cast(*cmd), buffer, tgtBlockSize, isImgDiff); EXPECT_EQ(ret, -1); close(fd); + delete cmd; } } diff --git a/test/unittest/applypatch_test/commands_unittest.cpp b/test/unittest/applypatch_test/commands_unittest.cpp index 59249952a40c2f8fc9f34e4afda5fe0fd98c6eed..809d669b486370d8f86b5146ae85a6d52b8cc480 100755 --- a/test/unittest/applypatch_test/commands_unittest.cpp +++ b/test/unittest/applypatch_test/commands_unittest.cpp @@ -62,6 +62,7 @@ HWTEST_F(CommandsUnitTest, command_test_001, TestSize.Level0) sha256 = cmd->GetArgumentByPos(1); EXPECT_EQ(sha256, "5aa246ebe8e817740f12cc0f6e536c5ea22e5db177563a1caea5a86614275546"); EXPECT_EQ(cmd->GetCommandLine(), cmdLine); + delete cmd; } HWTEST_F(CommandsUnitTest, command_test_002, TestSize.Level0) @@ -91,5 +92,6 @@ HWTEST_F(CommandsUnitTest, command_test_002, TestSize.Level0) EXPECT_EQ(cmd->Init(cmdLine), true); cmdLine = "last 1,1"; EXPECT_EQ(cmd->Init(cmdLine), true); + delete cmd; } } // updater_ut diff --git a/test/unittest/applypatch_test/imagepatch_unittest.cpp b/test/unittest/applypatch_test/imagepatch_unittest.cpp index 7138814703cd1342bba7c239a225fc3ac6a0c9c1..c9a1466c03fe12e300e22a4b19ed4626d693223c 100755 --- a/test/unittest/applypatch_test/imagepatch_unittest.cpp +++ b/test/unittest/applypatch_test/imagepatch_unittest.cpp @@ -53,6 +53,7 @@ bool ImagePatchTest::ReadContentFromFile(const std::string& file, std::string &c while ((n = read(fd, buffer, sizeof(buffer))) > 0) { content.append(buffer, n); } + close(fd); return ((n == 0) ? true : false); } diff --git a/test/unittest/applypatch_test/imagepatch_unittest.h b/test/unittest/applypatch_test/imagepatch_unittest.h index efff589cc95acb30c05964d9e79f4e2856017632..6da5f553f987e8d7f6cc47979d316a75a9f0c473 100755 --- a/test/unittest/applypatch_test/imagepatch_unittest.h +++ b/test/unittest/applypatch_test/imagepatch_unittest.h @@ -75,7 +75,7 @@ public: { mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); int fd = open(target.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, mode); - EXPECT_GT(fd, 0); + EXPECT_GE(fd, 0); BlockSet targetBlk; targetBlk.ParserAndInsert({ "2", "0", "1" diff --git a/test/unittest/fs_manager/do_partition_unittest.cpp b/test/unittest/fs_manager/do_partition_unittest.cpp index cf0a03d732d32e0e5b615bd28c455e3864fd5faa..32c652cbb980c56531e27fbefa20f3051117bf17 100755 --- a/test/unittest/fs_manager/do_partition_unittest.cpp +++ b/test/unittest/fs_manager/do_partition_unittest.cpp @@ -116,7 +116,7 @@ HWTEST_F(DoPartitionUnitTest, do_partition_test_001, TestSize.Level1) ASSERT_GT(ret, 0); PartitonList olist; - size_t xxxPartitionStart = dataPartitionStart + XXX_PARTITION_LEN; + size_t xxxPartitionStart = dataPartitionStart + XXX_PARTITION_LEN; InitEmmcPartition(myPaty[partitionIndex], "xxxxxx", xxxPartitionStart, XXX_PARTITION_LEN); olist.push_back(&myPaty[partitionIndex]); int ret1 = RegisterUpdaterPartitionList(nList, olist); diff --git a/test/unittest/misc_info_test/misc_info_unittest.cpp b/test/unittest/misc_info_test/misc_info_unittest.cpp index dd3b1545dd2ca3c2acbf6008f6c9352f2f76d6c5..0895b703cab520a13fc3d7190327f5bb43e0a466 100755 --- a/test/unittest/misc_info_test/misc_info_unittest.cpp +++ b/test/unittest/misc_info_test/misc_info_unittest.cpp @@ -42,10 +42,11 @@ HWTEST_F(MiscInfoUnitTest, misc_info_test_001, TestSize.Level1) auto fp = std::unique_ptr(fopen(MISC_FILE.c_str(), "wb"), fclose); EXPECT_NE(fp, nullptr); - UpdateMessage boot {}; - EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command), "boot_updater", sizeof(boot.command)), 0); - EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update), - "--update_package=./updater/xxx.zip\n--retry_count=1", sizeof(boot.update)), 0); + UpdateMessage boot {}; + const std::string command1 = "boot_updater"; + EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, command1.c_str(), command1.size()), 0); + const std::string command2 = "--update_package=./updater/xxx.zip\n--retry_count=1"; + EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command2.c_str(), command2.size()), 0); bool ret = WriteUpdaterMessage(MISC_FILE, boot); EXPECT_EQ(ret, true); diff --git a/test/unittest/mount_test/mount_unittest.cpp b/test/unittest/mount_test/mount_unittest.cpp index 832a936d44719a3f588f083a2f01baee4e3b3392..80f80b8d3ec6ae6e827b8f1befcd56e940002e5b 100755 --- a/test/unittest/mount_test/mount_unittest.cpp +++ b/test/unittest/mount_test/mount_unittest.cpp @@ -177,8 +177,9 @@ HWTEST_F(MountUnitTest, GetBlockDeviceByMountPoint_unitest, TestSize.Level1) std::cout << "Cannot re-mount vendor\n"; } auto ret = open(tmpPath.c_str(), O_RDONLY|O_CREAT|O_TRUNC, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); - if (ret != 0 && errno != EEXIST) { + if (ret < 0) { std::cout << "Cannot open \"/vendor/etc/fstab.updater\" file: " << errno << "\n"; + return; } close(ret); LoadFstab(); diff --git a/test/unittest/package/pkg_manager_unittest.cpp b/test/unittest/package/pkg_manager_unittest.cpp index 54bb964a40beae48e3f9e6811382edf7934ac6bf..d968d70173901cbdc14a9feb04c4c35d09cd5ff7 100755 --- a/test/unittest/package/pkg_manager_unittest.cpp +++ b/test/unittest/package/pkg_manager_unittest.cpp @@ -528,7 +528,6 @@ public: (void)start; (void)buffer; size_t oldSize = uncompressedData.size(); - PKG_CHECK(oldSize != 0, return -1, "oldSize is 0"); if ((start + size) > uncompressedData.size()) { uncompressedData.resize(oldSize * ((start + size) / oldSize + 1)); } @@ -540,7 +539,7 @@ public: [&](hpackage::PkgManager::StreamPtr stream) { pkgManager_->ClosePkgStream(stream); }); - PKG_CHECK(outStream != nullptr, return -1, "Can not create stream "); + PKG_CHECK(outStream != nullptr, close(fd); return -1, "Can not create stream "); void* mappedData = mmap(nullptr, (size_t)fileSize, PROT_READ, MAP_SHARED, fd, 0); PKG_CHECK(mappedData != MAP_FAILED, close(fd); return -2, "Can not mmap "); @@ -608,7 +607,7 @@ public: size_t fileSize = GetFileSize(testFileName); size_t uncompressedDataSize = 1024; int32_t fd = open(testFileName.c_str(), O_RDWR); - EXPECT_GT(fd, 0); + EXPECT_GE(fd, 0); uncompressedData.resize(uncompressedDataSize); PkgManager::StreamPtr stream = nullptr; @@ -620,7 +619,6 @@ public: (void)start; (void)buffer; size_t oldSize = uncompressedData.size(); - PKG_CHECK(oldSize != 0, return -1, "oldSize is 0"); if ((start + size) > uncompressedData.size()) { uncompressedData.resize(oldSize * ((start + size) / oldSize + 1)); } @@ -632,7 +630,7 @@ public: [&](hpackage::PkgManager::StreamPtr stream) { pkgManager_->ClosePkgStream(stream); }); - PKG_CHECK(outStream != nullptr, return -1, "Can not create stream "); + PKG_CHECK(outStream != nullptr, close(fd); return -1, "Can not create stream "); void* mappedData = mmap(nullptr, (size_t)fileSize, PROT_READ, MAP_SHARED, fd, 0); PKG_CHECK(mappedData != MAP_FAILED, close(fd); return -2, "Can not mmap "); diff --git a/test/unittest/script/script_interpreter_unittest.cpp b/test/unittest/script/script_interpreter_unittest.cpp index db031a77e8b70d47b18553480056d19e0a851eac..1f0cc7973279db8fdf3e5a237d2fff2bbb2d1798 100755 --- a/test/unittest/script/script_interpreter_unittest.cpp +++ b/test/unittest/script/script_interpreter_unittest.cpp @@ -101,15 +101,15 @@ public: { std::unique_ptr funcContext = std::make_unique(); int intValue = 100; - int ret = funcContext->PushParam(intValue); + int ret1 = funcContext->PushParam(intValue); float floatValue = 100.0; - ret |= funcContext->PushParam(floatValue); + int ret2 = funcContext->PushParam(floatValue); std::string str = std::string("333333333"); - ret |= funcContext->PushParam(str); - EXPECT_EQ(0, ret); + int ret3 = funcContext->PushParam(str); + EXPECT_EQ(0, ret1 && ret2 && ret3); int32_t outOfIndex = 3; - ret = funcContext->GetParamType(outOfIndex); + int ret = funcContext->GetParamType(outOfIndex); EXPECT_EQ(UScriptContext::PARAM_TYPE_INVALID, ret); return 0; } diff --git a/test/unittest/updater_binary/update_processor_unittest.cpp b/test/unittest/updater_binary/update_processor_unittest.cpp index d8ff4857649bcf77184dc3b1e7edd823f22898b4..e86e72ee1f8975e2e64441d15331be331324a1ce 100755 --- a/test/unittest/updater_binary/update_processor_unittest.cpp +++ b/test/unittest/updater_binary/update_processor_unittest.cpp @@ -72,6 +72,11 @@ HWTEST_F(UpdateProcessorUnitTest, UpdateProcessor_001, TestSize.Level1) updater::utils::MkdirRecursive(devDir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); int fd = open(devPath.c_str(), O_CREAT | O_WRONLY | O_EXCL, 0664); printf("@@@ devPath = %s, fd=%d\n", devPath.c_str(), fd); + if (fd < 0) { + printf("Open failed, fd = %d", fd); + FAIL(); + return; + } close(fd); for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) { diff --git a/test/unittest/updater_main_test/updater_main_unittest.cpp b/test/unittest/updater_main_test/updater_main_unittest.cpp index 9f2dad4b3b5c03c622033762c6e39dc8ffbfbbbe..a13bcbb56d420ae1953cfc81022e860e9d7bb80d 100755 --- a/test/unittest/updater_main_test/updater_main_unittest.cpp +++ b/test/unittest/updater_main_test/updater_main_unittest.cpp @@ -26,7 +26,7 @@ #include "updater_ui.h" #include "utils.h" -using namespace updater; +using namespace updater; using namespace testing::ext; using namespace std; using namespace updater::utils; @@ -76,7 +76,8 @@ HWTEST_F(UpdaterMainUnitTest, updater_main_test_001, TestSize.Level1) const std::string commandFile = "/data/updater/command"; auto fp = std::unique_ptr(fopen(commandFile.c_str(), "wb"), fclose); EXPECT_NE(fp, nullptr); - EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command), "boot_updater", sizeof(boot.command)), 0); + const std::string commandMsg = "boot_updater"; + EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, commandMsg.c_str(), commandMsg.size()), 0); EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update), "", sizeof(boot.update)), 0); bool bRet = WriteUpdaterMessage(commandFile, boot); EXPECT_EQ(bRet, true); @@ -95,8 +96,10 @@ HWTEST_F(UpdaterMainUnitTest, updater_main_test_001, TestSize.Level1) HWTEST_F(UpdaterMainUnitTest, updater_main_test_002, TestSize.Level1) { UpdateMessage boot {}; - EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command), "boot_updater", sizeof(boot.command)), 0); - EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update), "--user_wipe_data", sizeof(boot.update)), 0); + const std::string command1 = "boot_updater"; + EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, command1.c_str(), command1.size()), 0); + const std::string command2 = "--user_wipe_data"; + EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command2.c_str(), command2.size()), 0); bool ret = WriteUpdaterMessage(MISC_FILE, boot); EXPECT_EQ(ret, true); @@ -114,7 +117,6 @@ HWTEST_F(UpdaterMainUnitTest, updater_main_test_003, TestSize.Level1) unlink(dLog.c_str()); } - HWTEST_F(UpdaterMainUnitTest, updater_main_test_004, TestSize.Level1) { UpdaterUiInit(); @@ -122,25 +124,29 @@ HWTEST_F(UpdaterMainUnitTest, updater_main_test_004, TestSize.Level1) EXPECT_NE(fp, nullptr); UpdateMessage boot {}; - EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command), "boot_updater", sizeof(boot.command)), 0); - EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update), - "--update_package=/data/updater/updater.zip\n--retry_count=0", sizeof(boot.update)), 0); + const std::string command1 = "boot_updater"; + EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, command1.c_str(), command1.size()), 0); + const std::string command2 = "--update_package=/data/updater/updater.zip\n--retry_count=0"; + EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command2.c_str(), command2.size()), 0); bool ret = WriteUpdaterMessage(MISC_FILE, boot); EXPECT_EQ(ret, true); - - int lRet = 0; - int argc = 1; + + int lRet = 0; + int argc = 1; char **argv = new char*[MAX_ARG_SIZE]; argv[0] = new char[10]; + EXPECT_EQ(memset_s(boot.update, sizeof(boot.update), 0, sizeof(boot.update)), 0); - EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update), "--user_wipe_data", sizeof(boot.update)), 0); + const std::string command3 = "--user_wipe_data"; + EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command3.c_str(), command3.size()), 0); ret = WriteUpdaterMessage(MISC_FILE, boot); EXPECT_EQ(ret, true); lRet = UpdaterMain(argc, argv); EXPECT_EQ(lRet, 0); EXPECT_EQ(memset_s(boot.update, sizeof(boot.update), 0, sizeof(boot.update)), 0); - EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update), "--factory_wipe_data", sizeof(boot.update)), 0); + const std::string command4 = "--factory_wipe_data"; + EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command4.c_str(), command4.size()), 0); ret = WriteUpdaterMessage(MISC_FILE, boot); EXPECT_EQ(ret, true); lRet = UpdaterMain(argc, argv); @@ -154,7 +160,6 @@ HWTEST_F(UpdaterMainUnitTest, updater_main_test_004, TestSize.Level1) DeleteView(); } - HWTEST_F(UpdaterMainUnitTest, updater_main_test_compress, TestSize.Level1) { const std::string testFile = "/data/sdcard/updater/test_compress.txt";