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