• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager = nullptr) override
32     {
33         return true;
34     }
35 
TestGetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> & ptnInfo,const std::string & name)36     int32_t TestGetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> &ptnInfo, const std::string &name)
37     {
38         return GetPartitionInfoIndexByName(ptnInfo, name);
39     }
40 
TestIsPtableChanged(const std::vector<Ptable::PtnInfo> & devicePtnInfo,const std::vector<Ptable::PtnInfo> & pkgPtnInfo)41     bool TestIsPtableChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
42         const std::vector<Ptable::PtnInfo> &pkgPtnInfo)
43     {
44         return IsPtableChanged(devicePtnInfo, pkgPtnInfo);
45     }
46 
TestInitPtableManager()47     bool TestInitPtableManager()
48     {
49         return InitPtableManager();
50     }
51 
TestSetDeviceStorageType()52     void TestSetDeviceStorageType()
53     {
54         SetDeviceStorageType();
55     }
TestIsPartitionChanged(const std::vector<Ptable::PtnInfo> & devicePtnInfo,const std::vector<Ptable::PtnInfo> & pkgPtnInfo,const std::string & partitionName)56     bool TestIsPartitionChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
57         const std::vector<Ptable::PtnInfo> &pkgPtnInfo, const std::string &partitionName)
58     {
59         return IsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
60     }
61 };
62 
63 class UTestPtableManager : public ::testing::Test {
64 public:
UTestPtableManager()65     UTestPtableManager() {}
66 
67     ~UTestPtableManager() = default;
68 
TestGetPartitionInfoIndexByName()69     void TestGetPartitionInfoIndexByName()
70     {
71         PtableManagerTest context {};
72         std::vector<Ptable::PtnInfo> ptnInfo;
73         Ptable::PtnInfo tmp;
74         std::string name;
75         int32_t ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name);
76         ASSERT_EQ(ret, -1);
77         name = "TestGetPartitionInfoIndexByName";
78         ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name);
79         ASSERT_EQ(ret, -1);
80         tmp.dispName = name;
81         ptnInfo.push_back(tmp);
82         name = "";
83         ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name);
84         ASSERT_EQ(ret, -1);
85         name = "TestGetPartitionInfoIndexByName1";
86         ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name);
87         ASSERT_EQ(ret, -1);
88         name = "TestGetPartitionInfoIndexByName";
89         ret = context.TestGetPartitionInfoIndexByName(ptnInfo, name);
90         ASSERT_NE(ret, -1);
91     }
92 
TestIsPtableChanged()93     void TestIsPtableChanged()
94     {
95         PtableManagerTest context {};
96         std::vector<Ptable::PtnInfo> devicePtnInfo;
97         std::vector<Ptable::PtnInfo> pkgPtnInfo;
98         bool ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo);
99         ASSERT_EQ(ret, false);
100         Ptable::PtnInfo tmp = {1, 1, {1}, 1, "TestIsPtableChanged", "", ""};
101         pkgPtnInfo.push_back(tmp);
102         ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo);
103         ASSERT_EQ(ret, true);
104         devicePtnInfo.push_back(tmp);
105         ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo);
106         ASSERT_EQ(ret, false);
107         devicePtnInfo.push_back(tmp);
108         ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo);
109         ASSERT_EQ(ret, true);
110         devicePtnInfo.pop_back();
111         devicePtnInfo[0].startAddr = 0;
112         ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo);
113         ASSERT_EQ(ret, true);
114         devicePtnInfo[0].startAddr = 1;
115         devicePtnInfo[0].partitionSize = 0;
116         ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo);
117         ASSERT_EQ(ret, true);
118         devicePtnInfo[0].partitionSize = 1;
119         devicePtnInfo[0].dispName = "TestIsPtableChanged1";
120         ret = context.TestIsPtableChanged(devicePtnInfo, pkgPtnInfo);
121         ASSERT_EQ(ret, true);
122         ret = context.WritePtableToDevice();
123         ASSERT_EQ(ret, false);
124     }
125 
TestInitPtableManagerAndSetDeviceStorageType()126     void TestInitPtableManagerAndSetDeviceStorageType()
127     {
128         PtableManagerTest context {};
129         bool ret = context.TestInitPtableManager();
130         ASSERT_EQ(ret, false);
131         context.TestSetDeviceStorageType();
132         ASSERT_EQ(context.storage_, PtableManagerTest::StorageType::STORAGE_UFS);
133         context.storage_ = PtableManagerTest::StorageType::STORAGE_EMMC;
134         context.TestSetDeviceStorageType();
135         ASSERT_EQ(context.storage_, PtableManagerTest::StorageType::STORAGE_EMMC);
136     }
137 
TestGetPartionInfoByName()138     void TestGetPartionInfoByName()
139     {
140         PtableManagerTest context {};
141         std::string partitionName = "";
142         Ptable::PtnInfo ptnInfo;
143         context.pPtable_ = nullptr;
144         bool ret = context.GetPartionInfoByName(partitionName, ptnInfo);
145         ASSERT_EQ(ret, false);
146         context.pPtable_ = std::make_unique<UfsPtable>();
147         ret = context.GetPartionInfoByName(partitionName, ptnInfo);
148         ASSERT_EQ(ret, false);
149         ptnInfo.dispName = "testPartition";
150         ret = context.GetPartionInfoByName(partitionName, ptnInfo);
151         ASSERT_EQ(ret, false);
152         partitionName = "testPartition";
153         ret = context.GetPartionInfoByName(partitionName, ptnInfo);
154         ASSERT_EQ(ret, false);
155         ret = context.WritePartitionTable();
156         ASSERT_EQ(ret, false);
157     }
158 
TestIsPartitionChanged()159     void TestIsPartitionChanged()
160     {
161         PtableManagerTest context {};
162         std::vector<Ptable::PtnInfo> devicePtnInfo;
163         std::vector<Ptable::PtnInfo> pkgPtnInfo;
164         std::string partitionName;
165         bool ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
166         ASSERT_EQ(ret, false);
167         Ptable::PtnInfo ptnInfo = {0, 0, {0}, 0, "TestIsPartitionChangedForCheck", "", ""};
168         pkgPtnInfo.push_back(ptnInfo);
169         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
170         ASSERT_EQ(ret, true);
171         devicePtnInfo.push_back(ptnInfo);
172         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
173         ASSERT_EQ(ret, true);
174         partitionName = "TestIsPartitionChanged";
175         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
176         ASSERT_EQ(ret, true);
177         ptnInfo.dispName = "TestIsPartitionChanged";
178         devicePtnInfo.push_back(ptnInfo);
179         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
180         ASSERT_EQ(ret, true);
181         pkgPtnInfo.push_back(ptnInfo);
182         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
183         ASSERT_EQ(ret, false);
184         devicePtnInfo[1].startAddr = 1;
185         devicePtnInfo[1].partitionSize = 1;
186         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
187         ASSERT_EQ(ret, true);
188         devicePtnInfo[1].startAddr = 0;
189         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
190         ASSERT_EQ(ret, true);
191     }
192 protected:
SetUp()193     void SetUp() {}
TearDown()194     void TearDown() {}
TestBody()195     void TestBody() {}
196 };
197 
198 HWTEST_F(UTestPtableManager, TestGetPartitionInfoIndexByName, TestSize.Level1)
199 {
200     UTestPtableManager {}.TestGetPartitionInfoIndexByName();
201 }
202 
203 HWTEST_F(UTestPtableManager, TestIsPtableChanged, TestSize.Level1)
204 {
205     UTestPtableManager {}.TestIsPtableChanged();
206 }
207 
208 HWTEST_F(UTestPtableManager, TestInitPtableManagerAndSetDeviceStorageType, TestSize.Level1)
209 {
210     UTestPtableManager {}.TestInitPtableManagerAndSetDeviceStorageType();
211 }
212 
213 HWTEST_F(UTestPtableManager, TestGetPartionInfoByName, TestSize.Level1)
214 {
215     UTestPtableManager {}.TestGetPartionInfoByName();
216 }
217 
218 HWTEST_F(UTestPtableManager, TestIsPartitionChanged, TestSize.Level1)
219 {
220     UTestPtableManager {}.TestIsPartitionChanged();
221 }
222 }