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 27using namespace testing::ext; 28using namespace Updater; 29using namespace std; 30namespace UpdaterUt { 31class CommandFunctionUnitTest : public testing::Test { 32public: 33 static void SetUpTestCase(void); 34 static void TearDownTestCase(void) {}; 35 void SetUp(); 36 void TearDown(); 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 46void CommandFunctionUnitTest::SetUpTestCase() 47{ 48 cout << "Updater Unit CommandFunctionUnitTest Setup!" << endl; 49} 50 51void CommandFunctionUnitTest::SetUp() 52{ 53 cout << "Updater Unit CommandFunctionUnitTest Begin!" << endl; 54} 55 56void CommandFunctionUnitTest::TearDown() 57{ 58 cout << "Updater Unit CommandFunctionUnitTest End!" << endl; 59} 60 61HWTEST_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->SetFileDescriptor(fd); 78 std::string cmdLine = std::string("erase 2,0,1"); 79 CommandResult ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine); 80 EXPECT_EQ(ret, 0); 81 cmdLine = "free 2,0,1"; 82 ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine); 83 transferParams->storeCreated = 0; 84 EXPECT_EQ(ret, 0); 85 cmdLine = "move ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb47c7a85dabd8b48892ca7 2,3,4 1 2,1,2"; 86 lseek64(fd, 0, SEEK_SET); 87 ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine); 88 EXPECT_EQ(ret, 0); 89 cmdLine = R"(bsdiff 0 132 ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb4 90 7c7a85dabd8b48892ca7 3431383721510cf1c211de027cf958c183e16db5fabb6b230 91 eb284c85e196aa9 2,0,1 1 - ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb4 92 7c7a85dabd8b48892ca7:2,0,1)"; 93 lseek64(fd, 0, SEEK_SET); 94 ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine); 95 EXPECT_EQ(ret, -1); 96 cmdLine = "abort"; 97 ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine); 98 EXPECT_EQ(ret, 0); 99 cmdLine = "new 2,0,1"; 100 ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine); 101 EXPECT_EQ(ret, -1); 102 cmdLine = "stash ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb47c7a85dabd8b48892ca7 2,2,3"; 103 ret = CommandFunctionUnitTest::TestCommandFnExec(cmd, cmdLine); 104 EXPECT_EQ(ret, 0); 105 cmdLine = "ppop"; 106 cmd->Init(cmdLine); 107 CommandFunction* cf = CommandFunctionFactory::GetInstance().GetCommandFunction(cmd->GetCommandHead()); 108 EXPECT_EQ(cf, nullptr); 109 unlink(filePath.c_str()); 110} 111} 112