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 
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         ret = context.WritePtableToDevice();
120         ASSERT_EQ(ret, false);
121     }
122 
TestInitPtableManagerAndSetDeviceStorageType()123     void TestInitPtableManagerAndSetDeviceStorageType()
124     {
125         PtableManagerTest context {};
126         bool ret = context.TestInitPtableManager();
127         ASSERT_EQ(ret, false);
128         context.TestSetDeviceStorageType();
129         ASSERT_EQ(context.storage_, PtableManagerTest::StorageType::STORAGE_UFS);
130         context.storage_ = PtableManagerTest::StorageType::STORAGE_EMMC;
131         context.TestSetDeviceStorageType();
132         ASSERT_EQ(context.storage_, PtableManagerTest::StorageType::STORAGE_EMMC);
133     }
134 
TestGetPartionInfoByName()135     void TestGetPartionInfoByName()
136     {
137         PtableManagerTest context {};
138         std::string partitionName = "";
139         Ptable::PtnInfo ptnInfo;
140         context.pPtable_ = nullptr;
141         bool ret = context.GetPartionInfoByName(partitionName, ptnInfo);
142         ASSERT_EQ(ret, false);
143         context.pPtable_ = std::make_unique<UfsPtable>();
144         ret = context.GetPartionInfoByName(partitionName, ptnInfo);
145         ASSERT_EQ(ret, false);
146         ptnInfo.dispName = "testPartition";
147         ret = context.GetPartionInfoByName(partitionName, ptnInfo);
148         ASSERT_EQ(ret, false);
149         partitionName = "testPartition";
150         ret = context.GetPartionInfoByName(partitionName, ptnInfo);
151         ASSERT_EQ(ret, false);
152         ret = context.WritePartitionTable();
153         ASSERT_EQ(ret, false);
154     }
155 
TestIsPartitionChanged()156     void TestIsPartitionChanged()
157     {
158         PtableManagerTest context {};
159         std::vector<Ptable::PtnInfo> devicePtnInfo;
160         std::vector<Ptable::PtnInfo> pkgPtnInfo;
161         std::string partitionName;
162         bool ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
163         ASSERT_EQ(ret, false);
164         Ptable::PtnInfo ptnInfo = {0, 0, {0}, 0, "TestIsPartitionChangedForCheck", "", ""};
165         pkgPtnInfo.push_back(ptnInfo);
166         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
167         ASSERT_EQ(ret, true);
168         devicePtnInfo.push_back(ptnInfo);
169         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
170         ASSERT_EQ(ret, true);
171         partitionName = "TestIsPartitionChanged";
172         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
173         ASSERT_EQ(ret, true);
174         ptnInfo.dispName = "TestIsPartitionChanged";
175         devicePtnInfo.push_back(ptnInfo);
176         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
177         ASSERT_EQ(ret, true);
178         pkgPtnInfo.push_back(ptnInfo);
179         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
180         ASSERT_EQ(ret, false);
181         devicePtnInfo[1].startAddr = 1;
182         devicePtnInfo[1].partitionSize = 1;
183         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
184         ASSERT_EQ(ret, true);
185         devicePtnInfo[1].startAddr = 0;
186         ret = context.TestIsPartitionChanged(devicePtnInfo, pkgPtnInfo, partitionName);
187         ASSERT_EQ(ret, true);
188     }
189 protected:
SetUp()190     void SetUp() {}
TearDown()191     void TearDown() {}
TestBody()192     void TestBody() {}
193 };
194 
195 HWTEST_F(UTestPtableManager, TestGetPartitionInfoIndexByName, TestSize.Level1)
196 {
197     UTestPtableManager {}.TestGetPartitionInfoIndexByName();
198 }
199 
200 HWTEST_F(UTestPtableManager, TestIsPtableChanged, TestSize.Level1)
201 {
202     UTestPtableManager {}.TestIsPtableChanged();
203 }
204 
205 HWTEST_F(UTestPtableManager, TestInitPtableManagerAndSetDeviceStorageType, TestSize.Level1)
206 {
207     UTestPtableManager {}.TestInitPtableManagerAndSetDeviceStorageType();
208 }
209 
210 HWTEST_F(UTestPtableManager, TestGetPartionInfoByName, TestSize.Level1)
211 {
212     UTestPtableManager {}.TestGetPartionInfoByName();
213 }
214 
215 HWTEST_F(UTestPtableManager, TestIsPartitionChanged, TestSize.Level1)
216 {
217     UTestPtableManager {}.TestIsPartitionChanged();
218 }
219 }