1 /*
2  * Copyright (c) 2021 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.h"
22 #include "log/log.h"
23 
24 using namespace testing::ext;
25 using namespace Updater;
26 using namespace std;
27 
28 namespace UpdaterUt {
29 class CommandsUnitTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
TearDownTestCase(void)32     static void TearDownTestCase(void) {};
33     void SetUp();
34     void TearDown();
35 };
36 
SetUpTestCase()37 void CommandsUnitTest::SetUpTestCase()
38 {
39     cout << "Updater Unit CommandsUnitTest Setup!" << endl;
40 }
41 
SetUp()42 void CommandsUnitTest::SetUp()
43 {
44     cout << "Updater Unit CommandsUnitTest Begin!" << endl;
45 }
46 
TearDown()47 void CommandsUnitTest::TearDown()
48 {
49     cout << "Updater Unit CommandsUnitTest End!" << endl;
50 }
51 
52 HWTEST_F(CommandsUnitTest, command_test_001, TestSize.Level0)
53 {
54     std::string hashValue = "5aa246ebe8e817740f12cc0f6e536c5ea22e5db177563a1caea5a86614275546";
55     std::string blockInfo = "2,20755,21031 276 2,20306,20582";
56     std::string cmdLine = std::string("move ") + hashValue + " " + blockInfo;
57     std::unique_ptr<TransferParams> transferParams = std::make_unique<TransferParams>();
58     transferParams->writerThreadInfo = std::make_unique<WriterThreadInfo>();
59     Command *cmd = new Command(transferParams.get());
60     cmd->Init(cmdLine);
61     auto type = cmd->GetCommandType();
62     EXPECT_EQ(type, CommandType::MOVE);
63     auto sha256 = cmd->GetArgumentByPos(10);
64     sha256 = cmd->GetArgumentByPos(1);
65     EXPECT_EQ(sha256, "5aa246ebe8e817740f12cc0f6e536c5ea22e5db177563a1caea5a86614275546");
66     EXPECT_EQ(cmd->GetCommandLine(), cmdLine);
67     delete cmd;
68 }
69 
70 HWTEST_F(CommandsUnitTest, command_test_002, TestSize.Level0)
71 {
72     std::string hashValue = "5aa246ebe8e817740f12cc0f6e536c5ea22e5db177563a1caea5a86614275546";
73     std::string blockInfo = "2,20755,21031 276 2,20306,20582";
74     std::string cmdLine = std::string("move ") + hashValue + " " + blockInfo;
75     std::unique_ptr<TransferParams> transferParams = std::make_unique<TransferParams>();
76     transferParams->writerThreadInfo = std::make_unique<WriterThreadInfo>();
77     Command *cmd = new Command(transferParams.get());
78     EXPECT_EQ(cmd->Init(cmdLine), true);
79     cmdLine = "abort";
80     EXPECT_EQ(cmd->Init(cmdLine), true);
81     cmdLine = "bsdiff 1,1";
82     EXPECT_EQ(cmd->Init(cmdLine), true);
83     cmdLine = "earse 1,1";
84     EXPECT_EQ(cmd->Init(cmdLine), true);
85     cmdLine = "free 1,1";
86     EXPECT_EQ(cmd->Init(cmdLine), true);
87     cmdLine = "pkgdiff 1,1";
88     EXPECT_EQ(cmd->Init(cmdLine), true);
89     cmdLine = "move 1,1";
90     EXPECT_EQ(cmd->Init(cmdLine), true);
91     cmdLine = "new 1,1";
92     EXPECT_EQ(cmd->Init(cmdLine), true);
93     cmdLine = "stash 1,1";
94     EXPECT_EQ(cmd->Init(cmdLine), true);
95     cmdLine = "zero 1,1";
96     EXPECT_EQ(cmd->Init(cmdLine), true);
97     cmdLine = "last 1,1";
98     EXPECT_EQ(cmd->Init(cmdLine), true);
99     delete cmd;
100 }
101 } // updater_ut
102