• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <iostream>
17 #include <map>
18 #include <sstream>
19 
20 #include <gtest/gtest.h>
21 
22 #include "b_resources/b_constants.h"
23 #include "tools_op.h"
24 
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27 
28 namespace {
29 const string BUNDLE_NAME = "com.example.app2backup/";
30 const string MANAGE_JSON = "manage.json";
31 const string FILE_NAME = "1.tar";
32 } // namespace
33 
34 class ToolsOpRestoreAsyncTest : public testing::Test {
35 public:
SetUpTestCase(void)36     static void SetUpTestCase(void) {};
TearDownTestCase()37     static void TearDownTestCase() {};
SetUp()38     void SetUp() {};
TearDown()39     void TearDown() {};
40 };
41 
42 /**
43  * @tc.number: SUB_backup_tools_op_restore_async_0100
44  * @tc.name: SUB_backup_tools_op_restore_async_0100
45  * @tc.desc: 测试
46  * @tc.size: MEDIUM
47  * @tc.type: FUNC
48  * @tc.level Level 1
49  * @tc.require: I7L7A6
50  */
51 HWTEST_F(ToolsOpRestoreAsyncTest, SUB_backup_tools_op_restore_async_0100, testing::ext::TestSize.Level1)
52 {
53     GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-begin SUB_backup_tools_op_restore_async_0100";
54     try {
55         GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-info";
56         map<string, vector<string>> mapArgToVal;
57         string localCap = string(BConstants::SA_BUNDLE_BACKUP_TMP_DIR.data()) + "/tmp";
58         vector<string> path = {localCap.data()};
59         mapArgToVal.insert(make_pair("pathCapFile", path));
60         vector<string> bundles = {"com.example.app2backup"};
61         mapArgToVal.insert(make_pair("bundles", bundles));
62         vector<string> restoreType = {"true"};
63         mapArgToVal.insert(make_pair("restoreType", restoreType));
64         vector<string> userId = {"100"};
65         mapArgToVal.insert(make_pair("userId", userId));
66 
67         // 创建测试路径以及测试环境
68         string cmdMkdir = string("mkdir -p ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME;
69         system(cmdMkdir.c_str());
70         string cmdTool = string("mkdir -p ") + BConstants::SA_BUNDLE_BACKUP_TMP_DIR.data();
71         system(cmdTool.c_str());
72         string touchTar = string("touch ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME + FILE_NAME;
73         system(touchTar.c_str());
74         string touchManage = string("touch ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME + MANAGE_JSON;
75         system(touchManage.c_str());
76         string touchTmp = string("touch ") + localCap;
77         system(touchTmp.c_str());
78 
79         // 尝试匹配当前命令,成功后执行
80         GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-restoreAsync";
81         vector<string_view> curOp;
82         curOp.emplace_back("restoreAsync");
__anon3b4fe5d30202(const ToolsOp &op) 83         auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); };
84         auto &&opeartions = ToolsOp::GetAllOperations();
85         auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed);
86         if (matchedOp != opeartions.end()) {
87             auto ret = matchedOp->Execute(mapArgToVal);
88             EXPECT_EQ(ret, 0);
89         }
90 
91         mapArgToVal.clear();
92         mapArgToVal.insert(make_pair("pathCapFile", path));
93         mapArgToVal.insert(make_pair("bundles", bundles));
94         vector<string> restoreTypeF = {"false"};
95         mapArgToVal.insert(make_pair("restoreType", restoreTypeF));
96         mapArgToVal.insert(make_pair("userId", userId));
97         if (matchedOp != opeartions.end()) {
98             auto ret = matchedOp->Execute(mapArgToVal);
99             EXPECT_EQ(ret, 0);
100         }
101 
102     } catch (...) {
103         EXPECT_TRUE(false);
104         GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-an exception occurred by construction.";
105     }
106     GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-end SUB_backup_tools_op_restore_async_0100";
107 }
108 
109 /**
110  * @tc.number: SUB_backup_tools_op_restore_async_0200
111  * @tc.name: SUB_backup_tools_op_restore_async_0200
112  * @tc.desc: 测试
113  * @tc.size: MEDIUM
114  * @tc.type: FUNC
115  * @tc.level Level 1
116  * @tc.require: I7L7A6
117  */
118 HWTEST_F(ToolsOpRestoreAsyncTest, SUB_backup_tools_op_restore_async_0200, testing::ext::TestSize.Level1)
119 {
120     GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-begin SUB_backup_tools_op_restore_async_0200";
121     try {
122         GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-The pathCapFile field is not contained.";
123         map<string, vector<string>> mapArgToVal;
124         vector<string> bundles = {"com.example.app2backup"};
125         vector<string> path = {"/data/backup/tmp"};
126         mapArgToVal.insert(make_pair("bundles", bundles));
127         vector<string> restoreType = {"false"};
128         mapArgToVal.insert(make_pair("restoreType", restoreType));
129         vector<string> userId = {"100"};
130         mapArgToVal.insert(make_pair("userId", userId));
131 
132         vector<string_view> curOp;
133         curOp.emplace_back("restoreAsync");
__anon3b4fe5d30302(const ToolsOp &op) 134         auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); };
135         auto &&opeartions = ToolsOp::GetAllOperations();
136         auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed);
137         if (matchedOp == opeartions.end()) {
138             EXPECT_TRUE(false);
139             return;
140         }
141         int ret = 0;
142         ret = matchedOp->Execute(mapArgToVal);
143         EXPECT_NE(ret, 0);
144 
145         mapArgToVal.clear();
146         mapArgToVal.insert(make_pair("pathCapFile", path));
147         ret = matchedOp->Execute(mapArgToVal);
148         EXPECT_NE(ret, 0);
149 
150         mapArgToVal.clear();
151         mapArgToVal.insert(make_pair("pathCapFile", path));
152         mapArgToVal.insert(make_pair("bundles", bundles));
153         ret = matchedOp->Execute(mapArgToVal);
154         EXPECT_NE(ret, 0);
155 
156         mapArgToVal.clear();
157         mapArgToVal.insert(make_pair("pathCapFile", path));
158         mapArgToVal.insert(make_pair("bundles", bundles));
159         mapArgToVal.insert(make_pair("restoreType", restoreType));
160         ret = matchedOp->Execute(mapArgToVal);
161         EXPECT_NE(ret, 0);
162 
163         mapArgToVal.clear();
164         ret = matchedOp->Execute(mapArgToVal);
165         EXPECT_NE(ret, 0);
166     } catch (...) {
167         EXPECT_TRUE(false);
168         GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-an exception occurred by construction.";
169     }
170     GTEST_LOG_(INFO) << "ToolsOpRestoreAsyncTest-end SUB_backup_tools_op_restore_async_0200";
171 }
172 } // namespace OHOS::FileManagement::Backup