• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <memory>
19 #include <sys/ioctl.h>
20 
21 #include "common.h"
22 #include "daemon_updater.h"
23 #include "flash_define.h"
24 #include "flash_service.h"
25 #include "flashd/flashd.h"
26 #include "fs_manager/mount.h"
27 #include "serial_struct.h"
28 #include "transfer.h"
29 #include "unittest_comm.h"
30 
31 using namespace std;
32 using namespace flashd;
33 using namespace Hdc;
34 using namespace testing::ext;
35 
36 namespace {
37 static std::string TEST_PARTITION_NAME = "data";
38 static std::string TEST_UPDATER_PACKAGE_PATH = "/data/updater/updater/updater.zip";
39 static std::string TEST_FLASH_IMAGE_NAME = "/data/updater/updater/updater.zip";
40 
41 class FLashServiceUnitTest : public testing::Test {
42 public:
FLashServiceUnitTest()43     FLashServiceUnitTest() {}
~FLashServiceUnitTest()44     ~FLashServiceUnitTest() {}
45 
SetUpTestCase(void)46     static void SetUpTestCase(void) {}
TearDownTestCase(void)47     static void TearDownTestCase(void) {}
SetUp()48     void SetUp() {}
TearDown()49     void TearDown() {}
TestBody()50     void TestBody() {}
51 
52 public:
TestFindAllDevice()53     int TestFindAllDevice()
54     {
55         Updater::LoadFstab();
56         std::string errorMsg;
57         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
58         if (flash == nullptr) {
59             return 1;
60         }
61         flash->LoadSysDevice();
62         return 0;
63     }
64 
TestDoFlashPartition()65     int TestDoFlashPartition()
66     {
67         std::string errorMsg;
68         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
69         if (flash == nullptr) {
70             return 1;
71         }
72         flash->DoFlashPartition(TEST_FLASH_IMAGE_NAME, TEST_PARTITION_NAME);
73         return 0;
74     }
75 
TestDoUpdater()76     int TestDoUpdater()
77     {
78         std::string errorMsg;
79         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
80         if (flash == nullptr) {
81             return 1;
82         }
83         flash->DoUpdate(TEST_UPDATER_PACKAGE_PATH);
84         flash->PostProgress(UPDATEMOD_UPDATE, 1024 * 1024 * 4, nullptr); // 1024 * 1024 * 4 4M
85         return 0;
86     }
87 
TestDoErasePartition()88     int TestDoErasePartition()
89     {
90         std::string errorMsg;
91         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
92         if (flash == nullptr) {
93             return 1;
94         }
95         flash->DoErasePartition(TEST_PARTITION_NAME);
96         return 0;
97     }
98 
TestDoFormatPartition(const std::string & part,const std::string & type)99     int TestDoFormatPartition(const std::string &part, const std::string &type)
100     {
101         std::string errorMsg;
102         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
103         if (flash == nullptr) {
104             return 1;
105         }
106         flash->DoFormatPartition(part, type);
107         return 0;
108     }
109 
TestFlashServiceDoResizeParatiton()110     int TestFlashServiceDoResizeParatiton()
111     {
112         std::string errorMsg;
113         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
114         if (flash == nullptr) {
115             return 1;
116         }
117         flash->DoResizeParatiton("data", 4096); // 4096 partition size
118         flash->DoResizeParatiton("data", 1024); // 1024 partition size
119         return 0;
120     }
121 
TestFlashServiceDoPrepare(uint8_t type,const std::string & cmdParam)122     int TestFlashServiceDoPrepare(uint8_t type, const std::string &cmdParam)
123     {
124         std::string errorMsg;
125         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
126         if (flash == nullptr) {
127             return 1;
128         }
129         (void)flashd::DoUpdaterPrepare(flash.get(), type, cmdParam, TEST_UPDATER_PACKAGE_PATH);
130         return 0;
131     }
132 
TestFlashServiceDoFlash(uint8_t type,const std::string & cmdParam)133     int TestFlashServiceDoFlash(uint8_t type, const std::string &cmdParam)
134     {
135         std::string errorMsg;
136         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
137         if (flash == nullptr) {
138             return 1;
139         }
140         (void)flashd::DoUpdaterPrepare(flash.get(), type, cmdParam, TEST_UPDATER_PACKAGE_PATH);
141         return 0;
142     }
143 
TestFlashServiceDoFinish(uint8_t type)144     int TestFlashServiceDoFinish(uint8_t type)
145     {
146         std::string errorMsg;
147         std::shared_ptr<flashd::FlashService> flash = std::make_shared<flashd::FlashService>(errorMsg);
148         if (flash == nullptr) {
149             return 1;
150         }
151         (void)flashd::DoUpdaterFinish(flash.get(), type, "");
152         return 0;
153     }
154 
TestDaemonUpdater()155     int TestDaemonUpdater()
156     {
157         uv_loop_t loopMain;
158         uv_loop_init(&loopMain);
159 
160         HTaskInfo hTaskInfo = nullptr;
161         std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
162         if (task == nullptr) {
163             return -1;
164         }
165         hTaskInfo = task.get();
166         hTaskInfo->channelId = 1;
167         hTaskInfo->sessionId = 0;
168         hTaskInfo->runLoop = &loopMain;
169         hTaskInfo->serverOrDaemon = 1;
170         std::shared_ptr<DaemonUpdater> hdcDamon = std::make_shared<DaemonUpdater>(hTaskInfo);
171         if (hdcDamon == nullptr) {
172             return -1;
173         }
174 
175         // cmd: hdc updater packagename
176         // check
177         HdcTransferBase::TransferConfig transferConfig {};
178         transferConfig.functionName = "update";
179         transferConfig.options = "update";
180         std::string localPath = TEST_UPDATER_PACKAGE_PATH;
181         transferConfig.fileSize = 163884012; // 163884012 file size
182         WRITE_LOG(LOG_DEBUG, "CheckMaster %s", transferConfig.functionName.c_str());
183         transferConfig.optionalName = "updater.zip";
184         std::string bufString = SerialStruct::SerializeToString(transferConfig);
185         const uint64_t realSize = static_cast<uint64_t>(1024 * 1024 * 1024) * 5;
186         std::vector<uint8_t> buffer(sizeof(realSize) + bufString.size());
187         int ret = memcpy_s(buffer.data(), buffer.size(), &realSize, sizeof(realSize));
188         EXPECT_EQ(0, ret);
189         ret = memcpy_s(buffer.data() + sizeof(realSize), buffer.size(), bufString.c_str(), bufString.size());
190         EXPECT_EQ(0, ret);
191         hdcDamon->CommandDispatch(CMD_UPDATER_CHECK, buffer.data(), buffer.size());
192 
193         // begin
194         hdcDamon->CommandDispatch(CMD_UPDATER_BEGIN, NULL, 0);
195 
196         for (int i = 0; i < 10; i++) { // 10 send time
197             HdcTransferBase::TransferPayload payloadHead {};
198             payloadHead.compressType = HdcTransferBase::COMPRESS_NONE;
199             payloadHead.uncompressSize = transferConfig.fileSize / 10; // 10 time
200             payloadHead.compressSize = transferConfig.fileSize / 10; // 10 time
201             payloadHead.index = 0;
202             std::string bufData = SerialStruct::SerializeToString(payloadHead);
203             hdcDamon->CommandDispatch(CMD_UPDATER_DATA, reinterpret_cast<uint8_t *>(bufData.data()), bufData.size());
204         }
205         // end
206         hdcDamon->DoTransferFinish();
207         return 0;
208     }
209 
TestHdcDaemonInvalid()210     int TestHdcDaemonInvalid()
211     {
212         uv_loop_t loopMain;
213         uv_loop_init(&loopMain);
214 
215         HTaskInfo hTaskInfo = nullptr;
216         std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
217         if (task == nullptr) {
218             return -1;
219         }
220         hTaskInfo = task.get();
221         hTaskInfo->channelId = 2; // 2 channel id
222         hTaskInfo->sessionId = 0;
223         hTaskInfo->runLoop = &loopMain;
224         hTaskInfo->serverOrDaemon = 1;
225         std::shared_ptr<DaemonUpdater> hdcDamon = std::make_shared<DaemonUpdater>(hTaskInfo);
226         if (hdcDamon == nullptr) {
227             return -1;
228         }
229         // cmd: hdc flash partition packagename
230         // check
231         HdcTransferBase::TransferConfig transferConfig {};
232         transferConfig.functionName = "aaaa";
233         transferConfig.options = TEST_PARTITION_NAME;
234         transferConfig.options += "  ";
235         transferConfig.options += TEST_FLASH_IMAGE_NAME;
236         std::string localPath = TEST_FLASH_IMAGE_NAME;
237         transferConfig.fileSize = 1468006400; // 1468006400 file size
238         WRITE_LOG(LOG_DEBUG, "CheckMaster %s", transferConfig.functionName.c_str());
239         transferConfig.optionalName = "userdata.img";
240         std::string bufString = SerialStruct::SerializeToString(transferConfig);
241         const uint64_t realSize = static_cast<uint64_t>(1024 * 1024 * 1024) * 5;
242         std::vector<uint8_t> buffer(sizeof(realSize) + bufString.size());
243         int ret = memcpy_s(buffer.data(), buffer.size(), &realSize, sizeof(realSize));
244         EXPECT_EQ(0, ret);
245         ret = memcpy_s(buffer.data() + sizeof(realSize), buffer.size(), bufString.c_str(), bufString.size());
246         EXPECT_EQ(0, ret);
247         hdcDamon->CommandDispatch(CMD_UPDATER_CHECK, buffer.data(), buffer.size());
248         return 0;
249     }
250 
TestHdcDaemonFlash()251     int TestHdcDaemonFlash()
252     {
253         uv_loop_t loopMain;
254         uv_loop_init(&loopMain);
255 
256         HTaskInfo hTaskInfo = nullptr;
257         std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
258         if (task == nullptr) {
259             return -1;
260         }
261         hTaskInfo = task.get();
262         hTaskInfo->channelId = 2; // 2 channel id
263         hTaskInfo->sessionId = 0;
264         hTaskInfo->runLoop = &loopMain;
265         hTaskInfo->serverOrDaemon = 1;
266         std::shared_ptr<DaemonUpdater> hdcDamon = std::make_shared<DaemonUpdater>(hTaskInfo);
267         if (hdcDamon == nullptr) {
268             return -1;
269         }
270         // cmd: hdc flash partition packagename
271         // check
272         HdcTransferBase::TransferConfig transferConfig {};
273         transferConfig.functionName = "flash";
274         transferConfig.options = TEST_PARTITION_NAME;
275         transferConfig.options += "  ";
276         transferConfig.options += TEST_FLASH_IMAGE_NAME;
277         std::string localPath = TEST_FLASH_IMAGE_NAME;
278         transferConfig.fileSize = 1468006400; // 1468006400 file size
279         WRITE_LOG(LOG_DEBUG, "CheckMaster %s", transferConfig.functionName.c_str());
280         transferConfig.optionalName = "userdata.img";
281         std::string bufString = SerialStruct::SerializeToString(transferConfig);
282         const uint64_t realSize = transferConfig.fileSize;
283         std::vector<uint8_t> buffer(sizeof(realSize) + bufString.size());
284         int ret = memcpy_s(buffer.data(), buffer.size(), &realSize, sizeof(realSize));
285         EXPECT_EQ(0, ret);
286         ret = memcpy_s(buffer.data() + sizeof(realSize), buffer.size(), bufString.c_str(), bufString.size());
287         EXPECT_EQ(0, ret);
288         hdcDamon->CommandDispatch(CMD_UPDATER_CHECK, buffer.data(), buffer.size());
289 
290         // begin
291         hdcDamon->CommandDispatch(CMD_UPDATER_BEGIN, NULL, 0);
292 
293         HdcTransferBase::TransferPayload payloadHead {};
294         for (int i = 0; i < 10; i++) { // 10 send data
295             payloadHead.compressType = HdcTransferBase::COMPRESS_NONE;
296             payloadHead.uncompressSize = transferConfig.fileSize / 10; // 10 time
297             payloadHead.compressSize = transferConfig.fileSize / 10; // 10 time
298             payloadHead.index = 0;
299             std::string bufData = SerialStruct::SerializeToString(payloadHead);
300             hdcDamon->CommandDispatch(CMD_UPDATER_DATA, reinterpret_cast<uint8_t *>(bufData.data()), bufData.size());
301         }
302         std::string bufData = SerialStruct::SerializeToString(payloadHead);
303         hdcDamon->CommandDispatch(CMD_UPDATER_DATA, reinterpret_cast<uint8_t *>(bufData.data()), bufData.size());
304         // end
305         hdcDamon->DoTransferFinish();
306         return 0;
307     }
308 
TestHdcDaemonErase()309     int TestHdcDaemonErase()
310     {
311         uv_loop_t loopMain;
312         uv_loop_init(&loopMain);
313 
314         HTaskInfo hTaskInfo = nullptr;
315         std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
316         if (task == nullptr) {
317             return -1;
318         }
319         hTaskInfo = task.get();
320         hTaskInfo->channelId = 2; // 2 channel id
321         hTaskInfo->sessionId = 0;
322         hTaskInfo->runLoop = &loopMain;
323         hTaskInfo->serverOrDaemon = 1;
324         std::shared_ptr<DaemonUpdater> hdcDamon = std::make_shared<DaemonUpdater>(hTaskInfo);
325         if (hdcDamon == nullptr) {
326             return -1;
327         }
328         // cmd: hdc erase partition
329         // check
330         std::string bufString = "erase -f ";
331         bufString += TEST_PARTITION_NAME;
332         hdcDamon->CommandDispatch(CMD_UPDATER_ERASE, reinterpret_cast<uint8_t *>(bufString.data()), bufString.size());
333         return 0;
334     }
335 
TestHdcDaemonFormat()336     int TestHdcDaemonFormat()
337     {
338         uv_loop_t loopMain;
339         uv_loop_init(&loopMain);
340 
341         HTaskInfo hTaskInfo = nullptr;
342         std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
343         if (task == nullptr) {
344             return -1;
345         }
346         hTaskInfo = task.get();
347         hTaskInfo->channelId = 2; // 2 channel id
348         hTaskInfo->sessionId = 0;
349         hTaskInfo->runLoop = &loopMain;
350         hTaskInfo->serverOrDaemon = 1;
351         std::shared_ptr<DaemonUpdater> hdcDamon = std::make_shared<DaemonUpdater>(hTaskInfo);
352         if (hdcDamon == nullptr) {
353             return -1;
354         }
355         // cmd: hdc format partition
356         // check
357         std::string bufString = "format -f ";
358         bufString += TEST_PARTITION_NAME + "  -t ext4";
359         hdcDamon->CommandDispatch(CMD_UPDATER_FORMAT, reinterpret_cast<uint8_t *>(bufString.data()), bufString.size());
360 
361         return 0;
362     }
363 };
364 
365 HWTEST_F(FLashServiceUnitTest, FLashServiceUnitTest, TestSize.Level1)
366 {
367     FLashServiceUnitTest test;
368     EXPECT_EQ(0, test.TestFindAllDevice());
369 }
370 
371 HWTEST_F(FLashServiceUnitTest, TestDaemonUpdater, TestSize.Level1)
372 {
373     FLashServiceUnitTest test;
374     EXPECT_EQ(0, test.TestDaemonUpdater());
375 }
376 
377 HWTEST_F(FLashServiceUnitTest, TestDoUpdater, TestSize.Level1)
378 {
379     FLashServiceUnitTest test;
380     EXPECT_EQ(0, test.TestDoUpdater());
381 }
382 
383 HWTEST_F(FLashServiceUnitTest, TestHdcDaemonFlash, TestSize.Level1)
384 {
385     FLashServiceUnitTest test;
386     EXPECT_EQ(0, test.TestHdcDaemonFlash());
387 }
388 
389 HWTEST_F(FLashServiceUnitTest, TestHdcDaemonInvalid, TestSize.Level1)
390 {
391     FLashServiceUnitTest test;
392     EXPECT_EQ(0, test.TestHdcDaemonInvalid());
393 }
394 
395 HWTEST_F(FLashServiceUnitTest, TestHdcDaemonErase, TestSize.Level1)
396 {
397     FLashServiceUnitTest test;
398     EXPECT_EQ(0, test.TestHdcDaemonErase());
399 }
400 
401 HWTEST_F(FLashServiceUnitTest, TestHdcDaemonFormat, TestSize.Level1)
402 {
403     FLashServiceUnitTest test;
404     EXPECT_EQ(0, test.TestHdcDaemonFormat());
405 }
406 
407 HWTEST_F(FLashServiceUnitTest, TestFindAllDevice, TestSize.Level1)
408 {
409     FLashServiceUnitTest test;
410     EXPECT_EQ(0, test.TestFindAllDevice());
411 }
412 
413 HWTEST_F(FLashServiceUnitTest, TestDoFlashPartition, TestSize.Level1)
414 {
415     FLashServiceUnitTest test;
416     EXPECT_EQ(0, test.TestDoFlashPartition());
417 }
418 
419 HWTEST_F(FLashServiceUnitTest, TestDoFormatPartition, TestSize.Level1)
420 {
421     FLashServiceUnitTest test;
422     EXPECT_EQ(0, test.TestDoFormatPartition("data", "ext4"));
423     EXPECT_EQ(0, test.TestDoFormatPartition("data", "f2fs"));
424     EXPECT_EQ(0, test.TestDoFormatPartition("boot", "f2fs"));
425 }
426 
427 HWTEST_F(FLashServiceUnitTest, TestDoErasePartition, TestSize.Level1)
428 {
429     FLashServiceUnitTest test;
430     EXPECT_EQ(0, test.TestDoErasePartition());
431 }
432 
433 HWTEST_F(FLashServiceUnitTest, TestFlashServiceDoResizeParatiton, TestSize.Level1)
434 {
435     FLashServiceUnitTest test;
436     EXPECT_EQ(0, test.TestFlashServiceDoResizeParatiton());
437 }
438 
439 HWTEST_F(FLashServiceUnitTest, TestFlashServiceDoPrepare, TestSize.Level1)
440 {
441     FLashServiceUnitTest test;
442     std::string cmdParam = "update ";
443     EXPECT_EQ(0, test.TestFlashServiceDoPrepare(flashd::UPDATEMOD_UPDATE, cmdParam));
444 
445     cmdParam = "flash ";
446     cmdParam += TEST_PARTITION_NAME + "  ";
447     cmdParam += TEST_FLASH_IMAGE_NAME;
448     EXPECT_EQ(0, test.TestFlashServiceDoPrepare(flashd::UPDATEMOD_FLASH, cmdParam));
449 
450     cmdParam = "erase -f";
451     cmdParam += TEST_PARTITION_NAME + "  ";
452     EXPECT_EQ(0, test.TestFlashServiceDoPrepare(flashd::UPDATEMOD_ERASE, cmdParam));
453 
454     cmdParam = "format -f ";
455     cmdParam += TEST_PARTITION_NAME + "  -t ext4";
456     EXPECT_EQ(0, test.TestFlashServiceDoPrepare(flashd::UPDATEMOD_FORMAT, cmdParam));
457 
458     EXPECT_EQ(0, test.TestFlashServiceDoPrepare(flashd::UPDATEMOD_MAX, cmdParam));
459 }
460 
461 HWTEST_F(FLashServiceUnitTest, TestFlashServiceDoFlash, TestSize.Level1)
462 {
463     FLashServiceUnitTest test;
464     std::string cmdParam = "update ";
465     EXPECT_EQ(0, test.TestFlashServiceDoFlash(flashd::UPDATEMOD_UPDATE, cmdParam));
466 
467     cmdParam = "flash ";
468     cmdParam += TEST_PARTITION_NAME + "  ";
469     cmdParam += TEST_FLASH_IMAGE_NAME;
470     EXPECT_EQ(0, test.TestFlashServiceDoFlash(flashd::UPDATEMOD_FLASH, cmdParam));
471 
472     cmdParam = "erase -f ";
473     cmdParam += TEST_PARTITION_NAME + "  ";
474     EXPECT_EQ(0, test.TestFlashServiceDoFlash(flashd::UPDATEMOD_ERASE, cmdParam));
475 
476     cmdParam = "format -f -t ext4 ";
477     cmdParam += TEST_PARTITION_NAME + "  ";
478     EXPECT_EQ(0, test.TestFlashServiceDoFlash(flashd::UPDATEMOD_FORMAT, cmdParam));
479 
480     cmdParam = "format -f -t f2fs ";
481     cmdParam += TEST_PARTITION_NAME + "  ";
482     EXPECT_EQ(0, test.TestFlashServiceDoFlash(flashd::UPDATEMOD_FORMAT, cmdParam));
483 
484     EXPECT_EQ(0, test.TestFlashServiceDoFlash(flashd::UPDATEMOD_MAX, cmdParam));
485 }
486 
487 HWTEST_F(FLashServiceUnitTest, TestFlashServiceDoFinish, TestSize.Level1)
488 {
489     FLashServiceUnitTest test;
490     EXPECT_EQ(0, test.TestFlashServiceDoFinish(flashd::UPDATEMOD_UPDATE));
491     EXPECT_EQ(0, test.TestFlashServiceDoFinish(flashd::UPDATEMOD_FLASH));
492     EXPECT_EQ(0, test.TestFlashServiceDoFinish(flashd::UPDATEMOD_MAX));
493 }
494 
495 HWTEST_F(FLashServiceUnitTest, TestFlashdMain, TestSize.Level1)
496 {
497     const char* argv1[] = {"TestFlashdMain", "-t"};
498     flashd_main(sizeof(argv1) / sizeof(char*), const_cast<char**>(argv1));
499     const char *argv2[] = {"TestFlashdMain", "-u", " -l5"};
500     flashd_main(sizeof(argv2) / sizeof(char*), const_cast<char**>(argv2));
501 }
502 } // namespace