1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 #include "ptable_manager.h" 18 #include "ptable.h" 19 20 using namespace Updater; 21 using namespace testing; 22 using namespace testing::ext; 23 24 namespace { 25 class PtableManagerTest : public PtableManager { 26 public: PtableManagerTest()27 PtableManagerTest() {} 28 ~PtableManagerTest()29 ~PtableManagerTest() {} 30 LoadPartitionInfo(Hpackage::PkgManager * pkgManager=nullptr)31 void LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override {} 32 TestGetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> & ptnInfo,const std::string & name)33 int32_t TestGetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> &ptnInfo, const std::string &name) 34 { 35 return GetPartitionInfoIndexByName(ptnInfo, name); 36 } 37 TestIsPtableChanged(const std::vector<Ptable::PtnInfo> & devicePtnInfo,const std::vector<Ptable::PtnInfo> & pkgPtnInfo)38 bool TestIsPtableChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo, 39 const std::vector<Ptable::PtnInfo> &pkgPtnInfo) 40 { 41 return IsPtableChanged(devicePtnInfo, pkgPtnInfo); 42 } 43 TestInitPtableManager()44 bool TestInitPtableManager() 45 { 46 return InitPtableManager(); 47 } 48 TestSetDeviceStorageType()49 void TestSetDeviceStorageType() 50 { 51 SetDeviceStorageType(); 52 } TestIsPartitionChanged(const std::vector<Ptable::PtnInfo> & devicePtnInfo,const std::vector<Ptable::PtnInfo> & pkgPtnInfo,const std::string & partitionName)53 bool TestIsPartitionChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo, 54 const std::vector<Ptable::PtnInfo> &pkgPtnInfo, const std::string &partitionName) 55 { 56 return IsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 57 } 58 }; 59 60 class UTestPtableManager : public ::testing::Test { 61 public: UTestPtableManager()62 UTestPtableManager() {} 63 64 ~UTestPtableManager() = default; 65 TestGetPartitionInfoIndexByName()66 void TestGetPartitionInfoIndexByName() 67 { 68 PtableManagerTest context {}; 69 std::vector<Ptable::PtnInfo> ptnInfo; 70 Ptable::PtnInfo tmp; 71 std::string name; 72 int32_t ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name); 73 ASSERT_EQ(ret, -1); 74 name = "TestGetPartitionInfoIndexByName"; 75 ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name); 76 ASSERT_EQ(ret, -1); 77 tmp.dispName = name; 78 ptnInfo.push_back(tmp); 79 name = ""; 80 ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name); 81 ASSERT_EQ(ret, -1); 82 name = "TestGetPartitionInfoIndexByName1"; 83 ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name); 84 ASSERT_EQ(ret, -1); 85 name = "TestGetPartitionInfoIndexByName"; 86 ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name); 87 ASSERT_NE(ret, -1); 88 } 89 TestIsPtableChanged()90 void TestIsPtableChanged() 91 { 92 PtableManagerTest context {}; 93 std::vector<Ptable::PtnInfo> devicePtnInfo; 94 std::vector<Ptable::PtnInfo> pkgPtnInfo; 95 bool ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo); 96 ASSERT_EQ(ret, false); 97 Ptable::PtnInfo tmp = {1, 1, {1}, 1, "TestIsPtableChanged"}; 98 pkgPtnInfo.push_back(tmp); 99 ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo); 100 ASSERT_EQ(ret, true); 101 devicePtnInfo.push_back(tmp); 102 ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo); 103 ASSERT_EQ(ret, false); 104 devicePtnInfo.push_back(tmp); 105 ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo); 106 ASSERT_EQ(ret, true); 107 devicePtnInfo.pop_back(); 108 devicePtnInfo[0].startAddr = 0; 109 ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo); 110 ASSERT_EQ(ret, true); 111 devicePtnInfo[0].startAddr = 1; 112 devicePtnInfo[0].partitionSize = 0; 113 ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo); 114 ASSERT_EQ(ret, true); 115 devicePtnInfo[0].partitionSize = 1; 116 devicePtnInfo[0].dispName = "TestIsPtableChanged1"; 117 ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo); 118 ASSERT_EQ(ret, true); 119 } 120 TestInitPtableManagerAndSetDeviceStorageType()121 void TestInitPtableManagerAndSetDeviceStorageType() 122 { 123 PtableManagerTest context {}; 124 bool ret = context.TestInitPtableManager(); 125 ASSERT_EQ(ret, false); 126 context.TestSetDeviceStorageType(); 127 ASSERT_EQ(context.storage_, PtableManagerTest::StorageType::STORAGE_UFS); 128 context.storage_ = PtableManagerTest::StorageType::STORAGE_EMMC; 129 context.TestSetDeviceStorageType(); 130 ASSERT_EQ(context.storage_, PtableManagerTest::StorageType::STORAGE_EMMC); 131 } 132 TestGetPartionInfoByName()133 void TestGetPartionInfoByName() 134 { 135 PtableManagerTest context {}; 136 std::string partitionName = ""; 137 Ptable::PtnInfo ptnInfo; 138 context.pPtable_ = nullptr; 139 bool ret = context.GetPartionInfoByName(partitionName, ptnInfo); 140 ASSERT_EQ(ret, false); 141 context.pPtable_ = std::make_unique<UfsPtable>(); 142 ret = context.GetPartionInfoByName(partitionName, ptnInfo); 143 ASSERT_EQ(ret, false); 144 ptnInfo.dispName = "testPartition"; 145 ret = context.GetPartionInfoByName(partitionName, ptnInfo); 146 ASSERT_EQ(ret, false); 147 partitionName = "testPartition"; 148 ret = context.GetPartionInfoByName(partitionName, ptnInfo); 149 ASSERT_EQ(ret, false); 150 } 151 TestIsPartitionChanged()152 void TestIsPartitionChanged() 153 { 154 PtableManagerTest context {}; 155 std::vector<Ptable::PtnInfo> devicePtnInfo; 156 std::vector<Ptable::PtnInfo> pkgPtnInfo; 157 std::string partitionName; 158 bool ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 159 ASSERT_EQ(ret, false); 160 Ptable::PtnInfo ptnInfo = {0, 0, {0}, 0, "TestIsPartitionChangedForCheck"}; 161 pkgPtnInfo.push_back(ptnInfo); 162 ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 163 ASSERT_EQ(ret, true); 164 devicePtnInfo.push_back(ptnInfo); 165 ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 166 ASSERT_EQ(ret, true); 167 partitionName = "TestIsPartitionChanged"; 168 ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 169 ASSERT_EQ(ret, true); 170 ptnInfo.dispName = "TestIsPartitionChanged"; 171 devicePtnInfo.push_back(ptnInfo); 172 ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 173 ASSERT_EQ(ret, true); 174 pkgPtnInfo.push_back(ptnInfo); 175 ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 176 ASSERT_EQ(ret, false); 177 devicePtnInfo[1].startAddr = 1; 178 devicePtnInfo[1].partitionSize = 1; 179 ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 180 ASSERT_EQ(ret, true); 181 devicePtnInfo[1].startAddr = 0; 182 ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName); 183 ASSERT_EQ(ret, true); 184 } 185 protected: SetUp()186 void SetUp() {} TearDown()187 void TearDown() {} TestBody()188 void TestBody() {} 189 }; 190 191 HWTEST_F(UTestPtableManager, TestGetPartitionInfoIndexByName, TestSize.Level1) 192 { 193 UTestPtableManager {}.TestGetPartitionInfoIndexByName(); 194 } 195 196 HWTEST_F(UTestPtableManager, TestIsPtableChanged, TestSize.Level1) 197 { 198 UTestPtableManager {}.TestIsPtableChanged(); 199 } 200 201 HWTEST_F(UTestPtableManager, TestInitPtableManagerAndSetDeviceStorageType, TestSize.Level1) 202 { 203 UTestPtableManager {}.TestInitPtableManagerAndSetDeviceStorageType(); 204 } 205 206 HWTEST_F(UTestPtableManager, TestGetPartionInfoByName, TestSize.Level1) 207 { 208 UTestPtableManager {}.TestGetPartionInfoByName(); 209 } 210 211 HWTEST_F(UTestPtableManager, TestIsPartitionChanged, TestSize.Level1) 212 { 213 UTestPtableManager {}.TestIsPartitionChanged(); 214 } 215 }