1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 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 <gmock/gmock.h>
18fb299fa2Sopenharmony_ci#include <gtest/gtest.h>
19fb299fa2Sopenharmony_ci#include <iostream>
20fb299fa2Sopenharmony_ci#include <string>
21fb299fa2Sopenharmony_ci#include "applypatch/command_process.h"
22fb299fa2Sopenharmony_ci#include "applypatch/transfer_manager.h"
23fb299fa2Sopenharmony_ci#include "applypatch/store.h"
24fb299fa2Sopenharmony_ci#include "log/log.h"
25fb299fa2Sopenharmony_ci#include "utils.h"
26fb299fa2Sopenharmony_ci
27fb299fa2Sopenharmony_ciusing namespace testing::ext;
28fb299fa2Sopenharmony_ciusing namespace Updater;
29fb299fa2Sopenharmony_ciusing namespace std;
30fb299fa2Sopenharmony_cinamespace UpdaterUt {
31fb299fa2Sopenharmony_ciclass CommandFunctionUnitTest : public testing::Test {
32fb299fa2Sopenharmony_cipublic:
33fb299fa2Sopenharmony_ci    static void SetUpTestCase(void);
34fb299fa2Sopenharmony_ci    static void TearDownTestCase(void) {};
35fb299fa2Sopenharmony_ci    void SetUp();
36fb299fa2Sopenharmony_ci    void TearDown();
37fb299fa2Sopenharmony_ci    CommandResult TestCommandFnExec(std::shared_ptr<Command> cmd, std::string cmdLine)
38fb299fa2Sopenharmony_ci    {
39fb299fa2Sopenharmony_ci        cmd->Init(cmdLine);
40fb299fa2Sopenharmony_ci        CommandFunction* cf = CommandFunctionFactory::GetInstance().GetCommandFunction(cmd->GetCommandHead());
41fb299fa2Sopenharmony_ci        CommandResult ret = cf->Execute(const_cast<Command &>(*cmd.get()));
42fb299fa2Sopenharmony_ci        return ret;
43fb299fa2Sopenharmony_ci    }
44fb299fa2Sopenharmony_ci};
45fb299fa2Sopenharmony_ci
46fb299fa2Sopenharmony_civoid CommandFunctionUnitTest::SetUpTestCase()
47fb299fa2Sopenharmony_ci{
48fb299fa2Sopenharmony_ci    cout << "Updater Unit CommandFunctionUnitTest Setup!" << endl;
49fb299fa2Sopenharmony_ci}
50fb299fa2Sopenharmony_ci
51fb299fa2Sopenharmony_civoid CommandFunctionUnitTest::SetUp()
52fb299fa2Sopenharmony_ci{
53fb299fa2Sopenharmony_ci    cout << "Updater Unit CommandFunctionUnitTest Begin!" << endl;
54fb299fa2Sopenharmony_ci}
55fb299fa2Sopenharmony_ci
56fb299fa2Sopenharmony_civoid CommandFunctionUnitTest::TearDown()
57fb299fa2Sopenharmony_ci{
58fb299fa2Sopenharmony_ci    cout << "Updater Unit CommandFunctionUnitTest End!" << endl;
59fb299fa2Sopenharmony_ci}
60fb299fa2Sopenharmony_ci
61fb299fa2Sopenharmony_ciHWTEST_F(CommandFunctionUnitTest, command_function_test_001, TestSize.Level1)
62fb299fa2Sopenharmony_ci{
63fb299fa2Sopenharmony_ci    std::string filePath = "/data/updater/updater/allCmdUnitTest.bin";
64fb299fa2Sopenharmony_ci    std::unique_ptr<TransferParams> transferParams = std::make_unique<TransferParams>();
65fb299fa2Sopenharmony_ci    transferParams->writerThreadInfo = std::make_unique<WriterThreadInfo>();
66fb299fa2Sopenharmony_ci    std::shared_ptr<Command> cmd = std::make_shared<Command>(transferParams.get());
67fb299fa2Sopenharmony_ci    mode_t dirMode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
68fb299fa2Sopenharmony_ci    transferParams->storeBase = "data/updater/updater/tmp/cmdtest";
69fb299fa2Sopenharmony_ci    Store::DoFreeSpace(transferParams->storeBase);
70fb299fa2Sopenharmony_ci    Utils::MkdirRecursive(transferParams->storeBase, dirMode);
71fb299fa2Sopenharmony_ci    int fd = open(filePath.c_str(), O_RDWR | O_CREAT, dirMode);
72fb299fa2Sopenharmony_ci    if (fd < 0) {
73fb299fa2Sopenharmony_ci        printf("Failed to open block %s, errno: %d\n", filePath.c_str(), errno);
74fb299fa2Sopenharmony_ci        return;
75fb299fa2Sopenharmony_ci    }
76fb299fa2Sopenharmony_ci    lseek64(fd, 0, SEEK_SET);
77fb299fa2Sopenharmony_ci    cmd->SetFileDescriptor(fd);
78fb299fa2Sopenharmony_ci    std::string cmdLine = std::string("erase 2,0,1");
79fb299fa2Sopenharmony_ci    CommandResult ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine);
80fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
81fb299fa2Sopenharmony_ci    cmdLine = "free 2,0,1";
82fb299fa2Sopenharmony_ci    ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine);
83fb299fa2Sopenharmony_ci    transferParams->storeCreated = 0;
84fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
85fb299fa2Sopenharmony_ci    cmdLine = "move ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb47c7a85dabd8b48892ca7 2,3,4 1 2,1,2";
86fb299fa2Sopenharmony_ci    lseek64(fd, 0, SEEK_SET);
87fb299fa2Sopenharmony_ci    ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine);
88fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
89fb299fa2Sopenharmony_ci    cmdLine = R"(bsdiff 0 132 ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb4
90fb299fa2Sopenharmony_ci    7c7a85dabd8b48892ca7 3431383721510cf1c211de027cf958c183e16db5fabb6b230
91fb299fa2Sopenharmony_ci    eb284c85e196aa9 2,0,1 1 - ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb4
92fb299fa2Sopenharmony_ci    7c7a85dabd8b48892ca7:2,0,1)";
93fb299fa2Sopenharmony_ci    lseek64(fd, 0, SEEK_SET);
94fb299fa2Sopenharmony_ci    ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine);
95fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, -1);
96fb299fa2Sopenharmony_ci    cmdLine = "abort";
97fb299fa2Sopenharmony_ci    ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine);
98fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
99fb299fa2Sopenharmony_ci    cmdLine = "new 2,0,1";
100fb299fa2Sopenharmony_ci    ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine);
101fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, -1);
102fb299fa2Sopenharmony_ci    cmdLine = "stash ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb47c7a85dabd8b48892ca7 2,2,3";
103fb299fa2Sopenharmony_ci    ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine);
104fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
105fb299fa2Sopenharmony_ci    cmdLine = "ppop";
106fb299fa2Sopenharmony_ci    cmd->Init(cmdLine);
107fb299fa2Sopenharmony_ci    CommandFunction* cf = CommandFunctionFactory::GetInstance().GetCommandFunction(cmd->GetCommandHead());
108fb299fa2Sopenharmony_ci    EXPECT_EQ(cf, nullptr);
109fb299fa2Sopenharmony_ci    unlink(filePath.c_str());
110fb299fa2Sopenharmony_ci}
111fb299fa2Sopenharmony_ci}
112