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 "flash_define.h"
23#include "host_updater.h"
24#include "serial_struct.h"
25#include "transfer.h"
26#include "unittest_comm.h"
27
28using namespace std;
29using namespace Hdc;
30using namespace testing::ext;
31namespace {
32static std::string TEST_PARTITION_NAME = "data";
33static std::string TEST_UPDATER_PACKAGE_PATH = "/data/updater/updater/updater.zip";
34static std::string TEST_FLASH_IMAGE_NAME = "/data/updater/updater/test.img";
35
36class FLashHostUnitTest : public testing::Test {
37public:
38    FLashHostUnitTest() {}
39    ~FLashHostUnitTest() {}
40
41    static void SetUpTestCase(void) {}
42    static void TearDownTestCase(void) {}
43    void SetUp() {}
44    void TearDown() {}
45    void TestBody() {}
46
47public:
48    int TestFlashHost(uint16_t command, const std::string &cmd)
49    {
50        HTaskInfo hTaskInfo = nullptr;
51        std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
52        if (task == nullptr) {
53            return -1;
54        }
55        hTaskInfo = task.get();
56        hTaskInfo->channelId = 1;
57        hTaskInfo->sessionId = 0;
58        hTaskInfo->runLoop = uv_default_loop();
59        hTaskInfo->serverOrDaemon = 0;
60        hTaskInfo->ownerSessionClass = nullptr;
61        std::shared_ptr<HostUpdater> flashHost = std::make_shared<HostUpdater>(hTaskInfo);
62        if (flashHost == nullptr) {
63            return -1;
64        }
65        flashHost->CommandDispatch(command,
66            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmd.data())), cmd.size());
67        return 0;
68    }
69
70    int TestFlashProgress(uint16_t command, const std::string &cmd, uint32_t progress)
71    {
72        HTaskInfo hTaskInfo = nullptr;
73        std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
74        if (task == nullptr) {
75            return -1;
76        }
77        hTaskInfo = task.get();
78        hTaskInfo->channelId = 1;
79        hTaskInfo->sessionId = 0;
80        hTaskInfo->runLoop = uv_default_loop();
81        hTaskInfo->serverOrDaemon = 0;
82        hTaskInfo->ownerSessionClass = nullptr;
83        std::shared_ptr<HostUpdater> flashHost = std::make_shared<HostUpdater>(hTaskInfo);
84        if (flashHost == nullptr) {
85            return -1;
86        }
87        flashHost->CommandDispatch(command,
88            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmd.data())), cmd.size());
89        flashHost->OpenFile();
90
91        std::vector<uint8_t> data(MAX_SIZE_IOBUF * 2); // 2
92        flashHost->CommandDispatch(CMD_UPDATER_BEGIN, const_cast<uint8_t *>(data.data()), data.size());
93
94        std::string cmdInfo = "";
95        flashHost->CommandDispatch(CMD_UPDATER_CHECK,
96            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmdInfo.data())), cmdInfo.size());
97
98        flashHost->CommandDispatch(CMD_UPDATER_DATA, const_cast<uint8_t *>(data.data()), data.size());
99
100        vector<uint8_t> info = {0, 1, 's', 'u', 'c', 'c', 'e', 's', 's'};
101        flashHost->CommandDispatch(CMD_UPDATER_FINISH,
102            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(info.data())), info.size());
103
104        uint32_t percentage = 30; // 30 progress
105        cmdInfo.resize(sizeof(percentage));
106        (void)memcpy_s(cmdInfo.data(), cmdInfo.size(), &percentage, sizeof(percentage));
107        flashHost->CommandDispatch(CMD_UPDATER_PROGRESS,
108            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmdInfo.data())), cmdInfo.size());
109
110        percentage = static_cast<uint32_t>(progress);
111        cmdInfo.resize(sizeof(percentage));
112        (void)memcpy_s(cmdInfo.data(), cmdInfo.size(), &percentage, sizeof(percentage));
113        flashHost->CommandDispatch(CMD_UPDATER_PROGRESS,
114            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmdInfo.data())), cmdInfo.size());
115        return 0;
116    }
117};
118
119HWTEST_F(FLashHostUnitTest, TestFlashHostErase, TestSize.Level1)
120{
121    FLashHostUnitTest test;
122    Base::SetLogLevel(LOG_LAST);  // debug log print
123
124    std::string cmdParam = "erase -f ";
125    cmdParam += TEST_PARTITION_NAME;
126    EXPECT_EQ(0, test.TestFlashHost(CMD_UPDATER_ERASE, cmdParam));
127}
128
129HWTEST_F(FLashHostUnitTest, TestFlashHostFormat, TestSize.Level1)
130{
131    FLashHostUnitTest test;
132    Base::SetLogLevel(LOG_LAST);  // debug log print
133
134    std::string cmdParam = "format -f ";
135    cmdParam += TEST_PARTITION_NAME;
136    cmdParam += "  -t ext4";
137    EXPECT_EQ(0, test.TestFlashHost(CMD_UPDATER_FORMAT, cmdParam));
138
139    cmdParam = "format ";
140    cmdParam += TEST_PARTITION_NAME;
141    cmdParam += "  -t ext4";
142    EXPECT_EQ(0, test.TestFlashHost(CMD_UPDATER_FORMAT, cmdParam));
143}
144
145HWTEST_F(FLashHostUnitTest, TestFlashHostUpdater, TestSize.Level1)
146{
147    FLashHostUnitTest test;
148    Base::SetLogLevel(LOG_LAST);  // debug log print
149
150    std::string cmdParam = TEST_UPDATER_PACKAGE_PATH;
151    EXPECT_EQ(0, test.TestFlashProgress(CMD_UPDATER_UPDATE_INIT, cmdParam, -1));
152
153    cmdParam = " -f ";
154    cmdParam += TEST_PARTITION_NAME + "  ";
155    cmdParam += TEST_FLASH_IMAGE_NAME;
156    EXPECT_EQ(0, test.TestFlashProgress(CMD_UPDATER_FLASH_INIT, cmdParam, -1));
157}
158
159HWTEST_F(FLashHostUnitTest, TestFlashHostFlash, TestSize.Level1)
160{
161    FLashHostUnitTest test;
162    Base::SetLogLevel(LOG_LAST);  // debug log print
163
164    std::string cmdParam = TEST_UPDATER_PACKAGE_PATH;
165    EXPECT_EQ(0, test.TestFlashProgress(CMD_UPDATER_UPDATE_INIT, cmdParam, 100));
166    cmdParam = " -f ";
167    cmdParam += TEST_PARTITION_NAME + "  ";
168    cmdParam += TEST_FLASH_IMAGE_NAME;
169    EXPECT_EQ(0, test.TestFlashProgress(CMD_UPDATER_FLASH_INIT, cmdParam, 100));
170}
171
172HWTEST_F(FLashHostUnitTest, TestFlashHostMatch, TestSize.Level1)
173{
174    std::string stringError;
175    uint16_t cmdFlag = 0;
176    bool bJumpDo = false;
177    bool ret = HostUpdater::CheckMatchUpdate("update updater.zip", stringError, cmdFlag, bJumpDo);
178    EXPECT_EQ(ret == true, 1);
179    EXPECT_EQ(cmdFlag, CMD_UPDATER_UPDATE_INIT);
180    EXPECT_EQ(bJumpDo == false, 1);
181
182    ret = HostUpdater::CheckMatchUpdate("flash updater.zip", stringError, cmdFlag, bJumpDo);
183    EXPECT_EQ(ret == true, 1);
184    EXPECT_EQ(cmdFlag, CMD_UPDATER_FLASH_INIT);
185    EXPECT_EQ(bJumpDo == false, 1);
186
187    ret = HostUpdater::CheckMatchUpdate("erase -f updater", stringError, cmdFlag, bJumpDo);
188    EXPECT_EQ(ret == true, 1);
189    EXPECT_EQ(cmdFlag, CMD_UPDATER_ERASE);
190    EXPECT_EQ(bJumpDo == false, 1);
191
192    ret = HostUpdater::CheckMatchUpdate("format -f updater ", stringError, cmdFlag, bJumpDo);
193    EXPECT_EQ(ret == true, 1);
194    EXPECT_EQ(cmdFlag, CMD_UPDATER_FORMAT);
195    EXPECT_EQ(bJumpDo == false, 1);
196
197    bJumpDo = false;
198    ret = HostUpdater::CheckMatchUpdate("install aaa.hap", stringError, cmdFlag, bJumpDo);
199    EXPECT_EQ(ret == false, 1);
200    EXPECT_EQ(bJumpDo == false, 1);
201}
202
203HWTEST_F(FLashHostUnitTest, TestFlashHostConfirm, TestSize.Level1)
204{
205    bool closeInput = false;
206    bool ret = HostUpdater::ConfirmCommand("update updater.zip", closeInput);
207    EXPECT_EQ(ret == true, 1);
208
209    HostUpdater::SetInput("yes");
210    closeInput = false;
211    ret = HostUpdater::ConfirmCommand("flash updater.zip", closeInput);
212    EXPECT_EQ(ret == true, 1);
213    EXPECT_EQ(closeInput == true, 1);
214
215    closeInput = false;
216    ret = HostUpdater::ConfirmCommand("erase updater", closeInput);
217    EXPECT_EQ(ret == true, 1);
218    EXPECT_EQ(closeInput == false, 1);
219
220    closeInput = false;
221    ret = HostUpdater::ConfirmCommand("format updater", closeInput);
222    EXPECT_EQ(ret == true, 1);
223    EXPECT_EQ(closeInput == false, 1);
224
225    closeInput = false;
226    ret = HostUpdater::ConfirmCommand("format -f updater", closeInput);
227    EXPECT_EQ(ret == true, 1);
228    EXPECT_EQ(closeInput == false, 1);
229
230    closeInput = false;
231    ret = HostUpdater::ConfirmCommand("erase -f updater", closeInput);
232    EXPECT_EQ(ret == true, 1);
233    EXPECT_EQ(closeInput == false, 1);
234
235    HostUpdater::SetInput("no");
236    closeInput = false;
237    ret = HostUpdater::ConfirmCommand("format updater", closeInput);
238    EXPECT_EQ(ret == false, 1);
239    EXPECT_EQ(closeInput == false, 1);
240
241    HostUpdater::SetInput("eeeeeeeee");
242    closeInput = false;
243    ret = HostUpdater::ConfirmCommand("format updater", closeInput);
244    EXPECT_EQ(ret == false, 1);
245    EXPECT_EQ(closeInput == false, 1);
246}
247} // namespace
248