diff --git a/services/updater_binary/update_partitions.h b/services/updater_binary/update_partitions.h index a8128f5d8ab1ed142b640c295f52d68307f63b0f..8a583bbcf008df126e175a67c78efb919c9ba26f 100644 --- a/services/updater_binary/update_partitions.h +++ b/services/updater_binary/update_partitions.h @@ -27,7 +27,11 @@ public: UpdatePartitions() {} virtual ~UpdatePartitions() {} int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override; +#ifndef UPDATER_UT private: +#else +public: +#endif int ParsePartitionInfo(const std::string &partitionInfo, PartitonList &newPartList) const; int DoNewPartitions(PartitonList &newPartList); bool SetPartitionInfo(const cJSON* partitions, int idx, struct Partition* myPartition) const; diff --git a/test/unittest/updater_binary/update_partitions_unittest.cpp b/test/unittest/updater_binary/update_partitions_unittest.cpp index 157c727c0beb0dcce14e718fc8d9df8326acbcc2..8b17113c986ecb0af97c5d33ba07c3e7112c1c29 100755 --- a/test/unittest/updater_binary/update_partitions_unittest.cpp +++ b/test/unittest/updater_binary/update_partitions_unittest.cpp @@ -28,6 +28,9 @@ #include "unittest_comm.h" #include "update_processor.h" #include "utils.h" +#include "cJSON.h" +#include "fs_manager/partitions.h" +#include "update_partitions.h" using namespace Updater; using namespace testing::ext; @@ -99,4 +102,104 @@ HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest02, TestSize.Level1) PkgManager::ReleasePackageInstance(pkgManager); EXPECT_EQ(partRet, USCRIPT_SUCCESS); } + +HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest04, TestSize.Level1) +{ + const char *partitionInfo = R"( + { + "disk": "sda", + "partition": [ + { + "nostart": 512 + }, + { + "start": 32 + }, + { + "start": 32, + "length": 1 + }, + { + "start": 32, + "length": 1, + "partName": "cust" + } + ], + "sector_size": 4096 + } + )"; + cJSON *root = cJSON_Parse(partitionInfo); + if (root == nullptr) { + cout << "cJSON_Parse error" << endl; + return; + } + cJSON *partitions = cJSON_GetObjectItem(root, "Partition"); + if (partitions == nullptr) { + cout << "cJSON_GetObjectItem error" << endl; + cJSON_Delete(root); + return; + } + struct Partition *myPartition = static_cast(calloc(1, sizeof(struct Partition))); + if (!myPartition) { + cout << "calloc error" << endl; + cJSON_Delete(root); + return; + } + + UpdatePartitions partition; + EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 10, myPartition)); // 10: error index + EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 0, myPartition)); // 0: error start + EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 1, myPartition)); // 1: error length + EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 2, myPartition)); // 2: error partName + EXPECT_TRUE(!partition.SetPartitionInfo(partitions, 3, myPartition)); // 2: error fstype + free(myPartition); + cJSON_Delete(root); +} + +HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest05, TestSize.Level1) +{ + const char *noPartitionInfo = R"( + { + "disk": "sda", + "partitionx": [ + { + "nostart": 512 + } + ], + "sector_size": 4096 + } + )"; + const char *partitionInfo = R"( + { + "disk": "sda", + "partition": [ + { + "nostart": 512 + } + ], + "sector_size": 4096 + } + )"; + const char *partitionInfoNew = R"( + { + "disk": "sda", + "partition": [ + { + "nostart": 512 + }, + { + "start": 512 + } + ], + "sector_size": 4096 + } + )"; + + PartitonList newPartList {}; + UpdatePartitions partition; + EXPECT_TRUE(partition.ParsePartitionInfo("test", newPartList) != 1); // error root + EXPECT_TRUE(partition.ParsePartitionInfo(std::string(noPartitionInfo), newPartList) != 1); // error partitions + EXPECT_TRUE(partition.ParsePartitionInfo(std::string(partitionInfo), newPartList) != 1); // error number + EXPECT_TRUE(partition.ParsePartitionInfo(std::string(partitionInfoNew), newPartList) != 1); // error number +} } // namespace updater_ut