• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         std::unique_ptr<CommandFunction> cf = CommandFunctionFactory::GetCommandFunction(cmd->GetCommandType());
41         CommandResult ret = cf->Execute(const_cast<Command &>(*cmd.get()));
42         CommandFunctionFactory::ReleaseCommandFunction(cf);
43         return ret;
44     }
45 };
46 
SetUpTestCase()47 void CommandFunctionUnitTest::SetUpTestCase()
48 {
49     cout << "Updater Unit CommandFunctionUnitTest Setup!" << endl;
50 }
51 
SetUp()52 void CommandFunctionUnitTest::SetUp()
53 {
54     cout << "Updater Unit CommandFunctionUnitTest Begin!" << endl;
55 }
56 
TearDown()57 void CommandFunctionUnitTest::TearDown()
58 {
59     cout << "Updater Unit CommandFunctionUnitTest End!" << endl;
60 }
61 
62 HWTEST_F(CommandFunctionUnitTest, command_function_test_001, TestSize.Level1)
63 {
64     std::string filePath = "/data/updater/updater/allCmdUnitTest.bin";
65     std::shared_ptr<Command> cmd;
66     cmd = std::make_shared<Command>();
67     TransferManagerPtr tm = TransferManager::GetTransferManagerInstance();
68     mode_t dirMode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
69     tm->GetGlobalParams()->storeBase = "data/updater/updater/tmp/cmdtest";
70     Store::DoFreeSpace(TransferManager::GetTransferManagerInstance()->GetGlobalParams()->storeBase);
71     Utils::MkdirRecursive(TransferManager::GetTransferManagerInstance()->GetGlobalParams()->storeBase, dirMode);
72     int fd = open(filePath.c_str(), O_RDWR | O_CREAT, dirMode);
73     if (fd < 0) {
74         printf("Failed to open block %s, errno: %d\n", filePath.c_str(), errno);
75         return;
76     }
77     lseek64(fd, 0, SEEK_SET);
78     cmd->SetFileDescriptor(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     tm->GetGlobalParams()->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     std::unique_ptr<CommandFunction> cf = CommandFunctionFactory::GetCommandFunction(cmd->GetCommandType());
109     EXPECT_EQ(cf, nullptr);
110     unlink(filePath.c_str());
111 }
112 }
113