1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include <fcntl.h>
17fb299fa2Sopenharmony_ci#include <gtest/gtest.h>
18fb299fa2Sopenharmony_ci#include <memory>
19fb299fa2Sopenharmony_ci#include <sys/ioctl.h>
20fb299fa2Sopenharmony_ci
21fb299fa2Sopenharmony_ci#include "common.h"
22fb299fa2Sopenharmony_ci#include "flash_define.h"
23fb299fa2Sopenharmony_ci#include "host_updater.h"
24fb299fa2Sopenharmony_ci#include "serial_struct.h"
25fb299fa2Sopenharmony_ci#include "transfer.h"
26fb299fa2Sopenharmony_ci#include "unittest_comm.h"
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_ciusing namespace std;
29fb299fa2Sopenharmony_ciusing namespace Hdc;
30fb299fa2Sopenharmony_ciusing namespace testing::ext;
31fb299fa2Sopenharmony_cinamespace {
32fb299fa2Sopenharmony_cistatic std::string TEST_PARTITION_NAME = "data";
33fb299fa2Sopenharmony_cistatic std::string TEST_UPDATER_PACKAGE_PATH = "/data/updater/updater/updater.zip";
34fb299fa2Sopenharmony_cistatic std::string TEST_FLASH_IMAGE_NAME = "/data/updater/updater/test.img";
35fb299fa2Sopenharmony_ci
36fb299fa2Sopenharmony_ciclass FLashHostUnitTest : public testing::Test {
37fb299fa2Sopenharmony_cipublic:
38fb299fa2Sopenharmony_ci    FLashHostUnitTest() {}
39fb299fa2Sopenharmony_ci    ~FLashHostUnitTest() {}
40fb299fa2Sopenharmony_ci
41fb299fa2Sopenharmony_ci    static void SetUpTestCase(void) {}
42fb299fa2Sopenharmony_ci    static void TearDownTestCase(void) {}
43fb299fa2Sopenharmony_ci    void SetUp() {}
44fb299fa2Sopenharmony_ci    void TearDown() {}
45fb299fa2Sopenharmony_ci    void TestBody() {}
46fb299fa2Sopenharmony_ci
47fb299fa2Sopenharmony_cipublic:
48fb299fa2Sopenharmony_ci    int TestFlashHost(uint16_t command, const std::string &cmd)
49fb299fa2Sopenharmony_ci    {
50fb299fa2Sopenharmony_ci        HTaskInfo hTaskInfo = nullptr;
51fb299fa2Sopenharmony_ci        std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
52fb299fa2Sopenharmony_ci        if (task == nullptr) {
53fb299fa2Sopenharmony_ci            return -1;
54fb299fa2Sopenharmony_ci        }
55fb299fa2Sopenharmony_ci        hTaskInfo = task.get();
56fb299fa2Sopenharmony_ci        hTaskInfo->channelId = 1;
57fb299fa2Sopenharmony_ci        hTaskInfo->sessionId = 0;
58fb299fa2Sopenharmony_ci        hTaskInfo->runLoop = uv_default_loop();
59fb299fa2Sopenharmony_ci        hTaskInfo->serverOrDaemon = 0;
60fb299fa2Sopenharmony_ci        hTaskInfo->ownerSessionClass = nullptr;
61fb299fa2Sopenharmony_ci        std::shared_ptr<HostUpdater> flashHost = std::make_shared<HostUpdater>(hTaskInfo);
62fb299fa2Sopenharmony_ci        if (flashHost == nullptr) {
63fb299fa2Sopenharmony_ci            return -1;
64fb299fa2Sopenharmony_ci        }
65fb299fa2Sopenharmony_ci        flashHost->CommandDispatch(command,
66fb299fa2Sopenharmony_ci            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmd.data())), cmd.size());
67fb299fa2Sopenharmony_ci        return 0;
68fb299fa2Sopenharmony_ci    }
69fb299fa2Sopenharmony_ci
70fb299fa2Sopenharmony_ci    int TestFlashProgress(uint16_t command, const std::string &cmd, uint32_t progress)
71fb299fa2Sopenharmony_ci    {
72fb299fa2Sopenharmony_ci        HTaskInfo hTaskInfo = nullptr;
73fb299fa2Sopenharmony_ci        std::shared_ptr<TaskInformation> task = std::make_shared<TaskInformation>();
74fb299fa2Sopenharmony_ci        if (task == nullptr) {
75fb299fa2Sopenharmony_ci            return -1;
76fb299fa2Sopenharmony_ci        }
77fb299fa2Sopenharmony_ci        hTaskInfo = task.get();
78fb299fa2Sopenharmony_ci        hTaskInfo->channelId = 1;
79fb299fa2Sopenharmony_ci        hTaskInfo->sessionId = 0;
80fb299fa2Sopenharmony_ci        hTaskInfo->runLoop = uv_default_loop();
81fb299fa2Sopenharmony_ci        hTaskInfo->serverOrDaemon = 0;
82fb299fa2Sopenharmony_ci        hTaskInfo->ownerSessionClass = nullptr;
83fb299fa2Sopenharmony_ci        std::shared_ptr<HostUpdater> flashHost = std::make_shared<HostUpdater>(hTaskInfo);
84fb299fa2Sopenharmony_ci        if (flashHost == nullptr) {
85fb299fa2Sopenharmony_ci            return -1;
86fb299fa2Sopenharmony_ci        }
87fb299fa2Sopenharmony_ci        flashHost->CommandDispatch(command,
88fb299fa2Sopenharmony_ci            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmd.data())), cmd.size());
89fb299fa2Sopenharmony_ci        flashHost->OpenFile();
90fb299fa2Sopenharmony_ci
91fb299fa2Sopenharmony_ci        std::vector<uint8_t> data(MAX_SIZE_IOBUF * 2); // 2
92fb299fa2Sopenharmony_ci        flashHost->CommandDispatch(CMD_UPDATER_BEGIN, const_cast<uint8_t *>(data.data()), data.size());
93fb299fa2Sopenharmony_ci
94fb299fa2Sopenharmony_ci        std::string cmdInfo = "";
95fb299fa2Sopenharmony_ci        flashHost->CommandDispatch(CMD_UPDATER_CHECK,
96fb299fa2Sopenharmony_ci            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmdInfo.data())), cmdInfo.size());
97fb299fa2Sopenharmony_ci
98fb299fa2Sopenharmony_ci        flashHost->CommandDispatch(CMD_UPDATER_DATA, const_cast<uint8_t *>(data.data()), data.size());
99fb299fa2Sopenharmony_ci
100fb299fa2Sopenharmony_ci        vector<uint8_t> info = {0, 1, 's', 'u', 'c', 'c', 'e', 's', 's'};
101fb299fa2Sopenharmony_ci        flashHost->CommandDispatch(CMD_UPDATER_FINISH,
102fb299fa2Sopenharmony_ci            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(info.data())), info.size());
103fb299fa2Sopenharmony_ci
104fb299fa2Sopenharmony_ci        uint32_t percentage = 30; // 30 progress
105fb299fa2Sopenharmony_ci        cmdInfo.resize(sizeof(percentage));
106fb299fa2Sopenharmony_ci        (void)memcpy_s(cmdInfo.data(), cmdInfo.size(), &percentage, sizeof(percentage));
107fb299fa2Sopenharmony_ci        flashHost->CommandDispatch(CMD_UPDATER_PROGRESS,
108fb299fa2Sopenharmony_ci            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmdInfo.data())), cmdInfo.size());
109fb299fa2Sopenharmony_ci
110fb299fa2Sopenharmony_ci        percentage = static_cast<uint32_t>(progress);
111fb299fa2Sopenharmony_ci        cmdInfo.resize(sizeof(percentage));
112fb299fa2Sopenharmony_ci        (void)memcpy_s(cmdInfo.data(), cmdInfo.size(), &percentage, sizeof(percentage));
113fb299fa2Sopenharmony_ci        flashHost->CommandDispatch(CMD_UPDATER_PROGRESS,
114fb299fa2Sopenharmony_ci            const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(cmdInfo.data())), cmdInfo.size());
115fb299fa2Sopenharmony_ci        return 0;
116fb299fa2Sopenharmony_ci    }
117fb299fa2Sopenharmony_ci};
118fb299fa2Sopenharmony_ci
119fb299fa2Sopenharmony_ciHWTEST_F(FLashHostUnitTest, TestFlashHostErase, TestSize.Level1)
120fb299fa2Sopenharmony_ci{
121fb299fa2Sopenharmony_ci    FLashHostUnitTest test;
122fb299fa2Sopenharmony_ci    Base::SetLogLevel(LOG_LAST);  // debug log print
123fb299fa2Sopenharmony_ci
124fb299fa2Sopenharmony_ci    std::string cmdParam = "erase -f ";
125fb299fa2Sopenharmony_ci    cmdParam += TEST_PARTITION_NAME;
126fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestFlashHost(CMD_UPDATER_ERASE, cmdParam));
127fb299fa2Sopenharmony_ci}
128fb299fa2Sopenharmony_ci
129fb299fa2Sopenharmony_ciHWTEST_F(FLashHostUnitTest, TestFlashHostFormat, TestSize.Level1)
130fb299fa2Sopenharmony_ci{
131fb299fa2Sopenharmony_ci    FLashHostUnitTest test;
132fb299fa2Sopenharmony_ci    Base::SetLogLevel(LOG_LAST);  // debug log print
133fb299fa2Sopenharmony_ci
134fb299fa2Sopenharmony_ci    std::string cmdParam = "format -f ";
135fb299fa2Sopenharmony_ci    cmdParam += TEST_PARTITION_NAME;
136fb299fa2Sopenharmony_ci    cmdParam += "  -t ext4";
137fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestFlashHost(CMD_UPDATER_FORMAT, cmdParam));
138fb299fa2Sopenharmony_ci
139fb299fa2Sopenharmony_ci    cmdParam = "format ";
140fb299fa2Sopenharmony_ci    cmdParam += TEST_PARTITION_NAME;
141fb299fa2Sopenharmony_ci    cmdParam += "  -t ext4";
142fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestFlashHost(CMD_UPDATER_FORMAT, cmdParam));
143fb299fa2Sopenharmony_ci}
144fb299fa2Sopenharmony_ci
145fb299fa2Sopenharmony_ciHWTEST_F(FLashHostUnitTest, TestFlashHostUpdater, TestSize.Level1)
146fb299fa2Sopenharmony_ci{
147fb299fa2Sopenharmony_ci    FLashHostUnitTest test;
148fb299fa2Sopenharmony_ci    Base::SetLogLevel(LOG_LAST);  // debug log print
149fb299fa2Sopenharmony_ci
150fb299fa2Sopenharmony_ci    std::string cmdParam = TEST_UPDATER_PACKAGE_PATH;
151fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestFlashProgress(CMD_UPDATER_UPDATE_INIT, cmdParam, -1));
152fb299fa2Sopenharmony_ci
153fb299fa2Sopenharmony_ci    cmdParam = " -f ";
154fb299fa2Sopenharmony_ci    cmdParam += TEST_PARTITION_NAME + "  ";
155fb299fa2Sopenharmony_ci    cmdParam += TEST_FLASH_IMAGE_NAME;
156fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestFlashProgress(CMD_UPDATER_FLASH_INIT, cmdParam, -1));
157fb299fa2Sopenharmony_ci}
158fb299fa2Sopenharmony_ci
159fb299fa2Sopenharmony_ciHWTEST_F(FLashHostUnitTest, TestFlashHostFlash, TestSize.Level1)
160fb299fa2Sopenharmony_ci{
161fb299fa2Sopenharmony_ci    FLashHostUnitTest test;
162fb299fa2Sopenharmony_ci    Base::SetLogLevel(LOG_LAST);  // debug log print
163fb299fa2Sopenharmony_ci
164fb299fa2Sopenharmony_ci    std::string cmdParam = TEST_UPDATER_PACKAGE_PATH;
165fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestFlashProgress(CMD_UPDATER_UPDATE_INIT, cmdParam, 100));
166fb299fa2Sopenharmony_ci    cmdParam = " -f ";
167fb299fa2Sopenharmony_ci    cmdParam += TEST_PARTITION_NAME + "  ";
168fb299fa2Sopenharmony_ci    cmdParam += TEST_FLASH_IMAGE_NAME;
169fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestFlashProgress(CMD_UPDATER_FLASH_INIT, cmdParam, 100));
170fb299fa2Sopenharmony_ci}
171fb299fa2Sopenharmony_ci
172fb299fa2Sopenharmony_ciHWTEST_F(FLashHostUnitTest, TestFlashHostMatch, TestSize.Level1)
173fb299fa2Sopenharmony_ci{
174fb299fa2Sopenharmony_ci    std::string stringError;
175fb299fa2Sopenharmony_ci    uint16_t cmdFlag = 0;
176fb299fa2Sopenharmony_ci    bool bJumpDo = false;
177fb299fa2Sopenharmony_ci    bool ret = HostUpdater::CheckMatchUpdate("update updater.zip", stringError, cmdFlag, bJumpDo);
178fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
179fb299fa2Sopenharmony_ci    EXPECT_EQ(cmdFlag, CMD_UPDATER_UPDATE_INIT);
180fb299fa2Sopenharmony_ci    EXPECT_EQ(bJumpDo == false, 1);
181fb299fa2Sopenharmony_ci
182fb299fa2Sopenharmony_ci    ret = HostUpdater::CheckMatchUpdate("flash updater.zip", stringError, cmdFlag, bJumpDo);
183fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
184fb299fa2Sopenharmony_ci    EXPECT_EQ(cmdFlag, CMD_UPDATER_FLASH_INIT);
185fb299fa2Sopenharmony_ci    EXPECT_EQ(bJumpDo == false, 1);
186fb299fa2Sopenharmony_ci
187fb299fa2Sopenharmony_ci    ret = HostUpdater::CheckMatchUpdate("erase -f updater", stringError, cmdFlag, bJumpDo);
188fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
189fb299fa2Sopenharmony_ci    EXPECT_EQ(cmdFlag, CMD_UPDATER_ERASE);
190fb299fa2Sopenharmony_ci    EXPECT_EQ(bJumpDo == false, 1);
191fb299fa2Sopenharmony_ci
192fb299fa2Sopenharmony_ci    ret = HostUpdater::CheckMatchUpdate("format -f updater ", stringError, cmdFlag, bJumpDo);
193fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
194fb299fa2Sopenharmony_ci    EXPECT_EQ(cmdFlag, CMD_UPDATER_FORMAT);
195fb299fa2Sopenharmony_ci    EXPECT_EQ(bJumpDo == false, 1);
196fb299fa2Sopenharmony_ci
197fb299fa2Sopenharmony_ci    bJumpDo = false;
198fb299fa2Sopenharmony_ci    ret = HostUpdater::CheckMatchUpdate("install aaa.hap", stringError, cmdFlag, bJumpDo);
199fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == false, 1);
200fb299fa2Sopenharmony_ci    EXPECT_EQ(bJumpDo == false, 1);
201fb299fa2Sopenharmony_ci}
202fb299fa2Sopenharmony_ci
203fb299fa2Sopenharmony_ciHWTEST_F(FLashHostUnitTest, TestFlashHostConfirm, TestSize.Level1)
204fb299fa2Sopenharmony_ci{
205fb299fa2Sopenharmony_ci    bool closeInput = false;
206fb299fa2Sopenharmony_ci    bool ret = HostUpdater::ConfirmCommand("update updater.zip", closeInput);
207fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
208fb299fa2Sopenharmony_ci
209fb299fa2Sopenharmony_ci    HostUpdater::SetInput("yes");
210fb299fa2Sopenharmony_ci    closeInput = false;
211fb299fa2Sopenharmony_ci    ret = HostUpdater::ConfirmCommand("flash updater.zip", closeInput);
212fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
213fb299fa2Sopenharmony_ci    EXPECT_EQ(closeInput == true, 1);
214fb299fa2Sopenharmony_ci
215fb299fa2Sopenharmony_ci    closeInput = false;
216fb299fa2Sopenharmony_ci    ret = HostUpdater::ConfirmCommand("erase updater", closeInput);
217fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
218fb299fa2Sopenharmony_ci    EXPECT_EQ(closeInput == false, 1);
219fb299fa2Sopenharmony_ci
220fb299fa2Sopenharmony_ci    closeInput = false;
221fb299fa2Sopenharmony_ci    ret = HostUpdater::ConfirmCommand("format updater", closeInput);
222fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
223fb299fa2Sopenharmony_ci    EXPECT_EQ(closeInput == false, 1);
224fb299fa2Sopenharmony_ci
225fb299fa2Sopenharmony_ci    closeInput = false;
226fb299fa2Sopenharmony_ci    ret = HostUpdater::ConfirmCommand("format -f updater", closeInput);
227fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
228fb299fa2Sopenharmony_ci    EXPECT_EQ(closeInput == false, 1);
229fb299fa2Sopenharmony_ci
230fb299fa2Sopenharmony_ci    closeInput = false;
231fb299fa2Sopenharmony_ci    ret = HostUpdater::ConfirmCommand("erase -f updater", closeInput);
232fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == true, 1);
233fb299fa2Sopenharmony_ci    EXPECT_EQ(closeInput == false, 1);
234fb299fa2Sopenharmony_ci
235fb299fa2Sopenharmony_ci    HostUpdater::SetInput("no");
236fb299fa2Sopenharmony_ci    closeInput = false;
237fb299fa2Sopenharmony_ci    ret = HostUpdater::ConfirmCommand("format updater", closeInput);
238fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == false, 1);
239fb299fa2Sopenharmony_ci    EXPECT_EQ(closeInput == false, 1);
240fb299fa2Sopenharmony_ci
241fb299fa2Sopenharmony_ci    HostUpdater::SetInput("eeeeeeeee");
242fb299fa2Sopenharmony_ci    closeInput = false;
243fb299fa2Sopenharmony_ci    ret = HostUpdater::ConfirmCommand("format updater", closeInput);
244fb299fa2Sopenharmony_ci    EXPECT_EQ(ret == false, 1);
245fb299fa2Sopenharmony_ci    EXPECT_EQ(closeInput == false, 1);
246fb299fa2Sopenharmony_ci}
247fb299fa2Sopenharmony_ci} // namespace
248