1 /*
2  * Copyright (c) 2022-2023 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 <gtest/gtest.h>
17 #include <iostream>
18 #include <map>
19 #include <sstream>
20 
21 #include "tools_op.h"
22 #include "tools_op_check_sa.h"
23 #include "utils_mock_global_variable.h"
24 
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27 class ToolsOpCheckSaTest : public testing::Test {
28 public:
SetUpTestCase(void)29     static void SetUpTestCase(void) {};
TearDownTestCase()30     static void TearDownTestCase() {};
SetUp()31     void SetUp() {};
TearDown()32     void TearDown() {};
33 };
34 
35 /**
36  * @tc.number: SUB_backup_tools_op_check_sa_0100
37  * @tc.name: SUB_backup_tools_op_check_sa_0100
38  * @tc.desc: 测试
39  * @tc.size: MEDIUM
40  * @tc.type: FUNC
41  * @tc.level Level 1
42  * @tc.require: I6F3GV
43  */
44 HWTEST_F(ToolsOpCheckSaTest, SUB_backup_tools_op_check_sa_0100, testing::ext::TestSize.Level1)
45 {
46     GTEST_LOG_(INFO) << "ToolsOpCheckSaTest-begin SUB_backup_tools_op_check_sa_0100";
47     try {
48         GTEST_LOG_(INFO) << "ToolsOpCheckSaTest-info";
49         map<string, vector<string>> mapArgToVal;
50         vector<string_view> curOp;
51         curOp.emplace_back("check");
52         curOp.emplace_back("sa");
__anone1c4afbc0102(const ToolsOp &op) 53         auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); };
54         auto &&opeartions = ToolsOp::GetAllOperations();
55         auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed);
56         int ret = 0;
57         if (matchedOp != opeartions.end()) {
58             ret = matchedOp->Execute(mapArgToVal);
59             EXPECT_EQ(ret, 0);
60         }
61         GTEST_LOG_(INFO) << "Setting GetInstance to return false";
62         SetMockGetInstance(false);
63         if (matchedOp != opeartions.end()) {
64             ret = matchedOp->Execute(mapArgToVal);
65             EXPECT_NE(ret, 0);
66         }
67     } catch (...) {
68         EXPECT_TRUE(false);
69         GTEST_LOG_(INFO) << "ToolsOpCheckSaTest-an exception occurred by construction.";
70     }
71     GTEST_LOG_(INFO) << "ToolsOpCheckSaTest-end SUB_backup_tools_op_check_sa_0100";
72 }
73 
74 /**
75  * @tc.number: SUB_backup_tools_op_check_sa_0200
76  * @tc.name: SUB_backup_tools_op_check_sa_0200
77  * @tc.desc: 测试Exec
78  * @tc.size: MEDIUM
79  * @tc.type: FUNC
80  * @tc.level Level 1
81  * @tc.require: I6F3GV
82  */
83 HWTEST_F(ToolsOpCheckSaTest, SUB_backup_tools_op_check_sa_0200, testing::ext::TestSize.Level1)
84 {
85     GTEST_LOG_(INFO) << "ToolsOpCheckSaTest-begin SUB_backup_tools_op_check_sa_0200";
86     try {
87         GTEST_LOG_(INFO) << "ToolsOpCheckSaTest-info";
88         map<string, vector<string>> mapArgToVal;
89         SetMockGetInstance(true);
90         vector<string_view> curOp;
91         curOp.emplace_back("check");
92         curOp.emplace_back("sa");
93         CheckSaRegister();
__anone1c4afbc0202(const ToolsOp &op) 94         auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); };
95         auto &&opeartions = ToolsOp::GetAllOperations();
96         auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed);
97         int ret = 0;
98         if (matchedOp != opeartions.end()) {
99             ret = matchedOp->Execute(mapArgToVal);
100             EXPECT_EQ(ret, 0);
101         }
102         GTEST_LOG_(INFO) << "GetInstance is false";
103         SetMockGetInstance(false);
104         if (matchedOp != opeartions.end()) {
105             ret = matchedOp->Execute(mapArgToVal);
106             EXPECT_NE(ret, 0);
107         }
108     } catch (...) {
109         EXPECT_TRUE(false);
110         GTEST_LOG_(INFO) << "ToolsOpCheckSaTest-an exception occurred by construction.";
111     }
112     GTEST_LOG_(INFO) << "ToolsOpCheckSaTest-end SUB_backup_tools_op_check_sa_0200";
113 }
114 } // namespace OHOS::FileManagement::Backup