1 /*
2  * Copyright (c) 2022-2024 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 <iostream>
17 #include <map>
18 #include <sstream>
19 
20 #include <gtest/gtest.h>
21 
22 #include "b_resources/b_constants.h"
23 #include "directory_ex.h"
24 #include "tools_op.h"
25 
26 namespace OHOS::FileManagement::Backup {
27 using namespace std;
28 class ToolsOpTest : public testing::Test {
29 public:
SetUpTestCase(void)30     static void SetUpTestCase(void) {};
TearDownTestCase()31     static void TearDownTestCase() {};
SetUp()32     void SetUp() {};
TearDown()33     void TearDown() {};
34 };
35 
36 /**
37  * @tc.number: SUB_backup_tools_op_0100
38  * @tc.name: SUB_backup_tools_op_0100
39  * @tc.desc: 测试Execute分支
40  * @tc.size: MEDIUM
41  * @tc.type: FUNC
42  * @tc.level Level 1
43  * @tc.require: I6F3GV
44  */
45 HWTEST_F(ToolsOpTest, SUB_backup_tools_op_0100, testing::ext::TestSize.Level1)
46 {
47     GTEST_LOG_(INFO) << "ToolsOpTest-begin SUB_backup_tools_op_0100";
48     try {
49         GTEST_LOG_(INFO) << "ToolsOpTest-info";
50         bool autoTest = ToolsOp::Register(ToolsOp {ToolsOp::Descriptor {
51             .opName = {"test"},
52             .funcGenHelpMsg = nullptr,
53             .funcExec = nullptr,
54         }});
55         EXPECT_TRUE(autoTest);
56         map<string, vector<string>> mapArgToVal;
57         vector<string_view> curOp;
58         curOp.emplace_back("test");
__anon52eb102c0102(const ToolsOp &op) 59         auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); };
60         auto &&opeartions = ToolsOp::GetAllOperations();
61         auto matchedOpIterator = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed);
62         if (matchedOpIterator != opeartions.end()) {
63             matchedOpIterator->Execute(mapArgToVal);
64         }
65     } catch (...) {
66         EXPECT_TRUE(false);
67         GTEST_LOG_(INFO) << "ToolsOpTest-an exception occurred by construction.";
68     }
69     GTEST_LOG_(INFO) << "ToolsOpTest-end SUB_backup_tools_op_0100";
70 }
71 
72 /**
73  * @tc.number: SUB_backup_tools_op_0200
74  * @tc.name: SUB_backup_tools_op_0200
75  * @tc.desc: 测试Register分支
76  * @tc.size: MEDIUM
77  * @tc.type: FUNC
78  * @tc.level Level 1
79  * @tc.require: I6F3GV
80  */
81 HWTEST_F(ToolsOpTest, SUB_backup_tools_op_0200, testing::ext::TestSize.Level1)
82 {
83     GTEST_LOG_(INFO) << "ToolsOpTest-begin SUB_backup_tools_op_0200";
84     try {
85         GTEST_LOG_(INFO) << "ToolsOpTest-info";
86         bool autoTest = ToolsOp::Register(ToolsOp {ToolsOp::Descriptor {
87             .opName = {"???"},
88             .funcGenHelpMsg = nullptr,
89             .funcExec = nullptr,
90         }});
91         EXPECT_FALSE(autoTest);
92     } catch (...) {
93         EXPECT_TRUE(false);
94         GTEST_LOG_(INFO) << "ToolsOpTest-an exception occurred by construction.";
95     }
96     GTEST_LOG_(INFO) << "ToolsOpTest-end SUB_backup_tools_op_0200";
97 }
98 
99 /**
100  * @tc.number: SUB_backup_tools_op_0300
101  * @tc.name: SUB_backup_tools_op_0300
102  * @tc.desc: 测试GetName分支
103  * @tc.size: MEDIUM
104  * @tc.type: FUNC
105  * @tc.level Level 1
106  * @tc.require: I6F3GV
107  */
108 HWTEST_F(ToolsOpTest, SUB_backup_tools_op_0300, testing::ext::TestSize.Level1)
109 {
110     GTEST_LOG_(INFO) << "ToolsOpTest-begin SUB_backup_tools_op_0300";
111     try {
112         GTEST_LOG_(INFO) << "ToolsOpTest-info";
113         ToolsOp obj({ToolsOp::Descriptor {
114             .opName = {"abcd", "hijk"},
115             .funcGenHelpMsg = nullptr,
116             .funcExec = nullptr,
117         }});
118         obj.GetName();
119     } catch (...) {
120         EXPECT_TRUE(false);
121         GTEST_LOG_(INFO) << "ToolsOpTest-an exception occurred by construction.";
122     }
123     GTEST_LOG_(INFO) << "ToolsOpTest-end SUB_backup_tools_op_0300";
124 }
125 
126 /**
127  * @tc.number: SUB_backup_tools_op_0400
128  * @tc.name: SUB_backup_tools_op_0400
129  * @tc.desc: 测试Execute分支
130  * @tc.size: MEDIUM
131  * @tc.type: FUNC
132  * @tc.level Level 1
133  * @tc.require: I6F3GV
134  */
135 HWTEST_F(ToolsOpTest, SUB_backup_tools_op_0400, testing::ext::TestSize.Level1)
136 {
137     GTEST_LOG_(INFO) << "ToolsOpTest-begin SUB_backup_tools_op_0400";
138     try {
139         map<string, vector<string>> mapArgToVal;
140         vector<string_view> curOp;
141         curOp.emplace_back("test");
__anon52eb102c0202(const ToolsOp &op) 142         auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); };
143         auto &&opeartions = ToolsOp::GetAllOperations();
144         auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed);
145         if (matchedOp != opeartions.end()) {
146             matchedOp->Execute(mapArgToVal);
147         }
148     } catch (...) {
149         EXPECT_TRUE(false);
150         GTEST_LOG_(INFO) << "ToolsOpTest-an exception occurred by construction.";
151     }
152     GTEST_LOG_(INFO) << "ToolsOpTest-end SUB_backup_tools_op_0400";
153 }
154 
155 /**
156  * @tc.number: SUB_backup_tools_op_0500
157  * @tc.name: SUB_backup_tools_op_0500
158  * @tc.desc: 测试Execute分支
159  * @tc.size: MEDIUM
160  * @tc.type: FUNC
161  * @tc.level Level 1
162  * @tc.require: I6F3GV
163  */
164 HWTEST_F(ToolsOpTest, SUB_backup_tools_op_0500, testing::ext::TestSize.Level1)
165 {
166     GTEST_LOG_(INFO) << "ToolsOpTest-begin SUB_backup_tools_op_0500";
167     try {
168         std::string bundleName = "test";
169         std::string wholePath = string(BConstants::BACKUP_TOOL_RECEIVE_DIR) + bundleName;
170         std::string incrementalpath = string(BConstants::BACKUP_TOOL_INCREMENTAL_RECEIVE_DIR) + bundleName;
171         if (access(incrementalpath.c_str(), F_OK) == 0) {
172             ForceRemoveDirectory(incrementalpath.data());
173         }
174         int result = ToolsOp::GetFIleNums(bundleName, false);
175         EXPECT_TRUE(result == DEFAULT_ERR_NUMBER);
176         if (access(wholePath.c_str(), F_OK) == 0) {
177             ForceRemoveDirectory(wholePath.data());
178         }
179         result = ToolsOp::GetFIleNums(bundleName, true);
180         EXPECT_TRUE(result == DEFAULT_ERR_NUMBER);
181 
182 
183         if (access(incrementalpath.c_str(), F_OK) != 0) {
184             ForceCreateDirectory(incrementalpath.data());
185         }
186         result = ToolsOp::GetFIleNums(bundleName, false);
187         EXPECT_TRUE(result == 0);
188         if (access(wholePath.c_str(), F_OK) != 0) {
189             ForceCreateDirectory(wholePath.data());
190         }
191         result = ToolsOp::GetFIleNums(bundleName, true);
192         EXPECT_TRUE(result == 0);
193         if (access(incrementalpath.c_str(), F_OK) == 0) {
194             ForceRemoveDirectory(incrementalpath.data());
195         }
196         if (access(wholePath.c_str(), F_OK) != 0) {
197             ForceCreateDirectory(wholePath.data());
198         }
199     } catch (...) {
200         EXPECT_TRUE(false);
201         GTEST_LOG_(INFO) << "ToolsOpTest-an exception occurred by construction.";
202     }
203     GTEST_LOG_(INFO) << "ToolsOpTest-end SUB_backup_tools_op_0500";
204 }
205 } // namespace OHOS::FileManagement::Backup