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 <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 ToolsOpRestoreTest : 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_0100 44 * @tc.name: SUB_backup_tools_op_restore_0100 45 * @tc.desc: 测试 46 * @tc.size: MEDIUM 47 * @tc.type: FUNC 48 * @tc.level Level 1 49 * @tc.require: I6F3GV 50 */ 51 HWTEST_F(ToolsOpRestoreTest, SUB_backup_tools_op_restore_0100, testing::ext::TestSize.Level1) 52 { 53 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-begin SUB_backup_tools_op_restore_0100"; 54 try { 55 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-info"; 56 map<string, vector<string>> mapArgToVal; 57 string localCap = string(BConstants::GetSaBundleBackupToolDir(BConstants::DEFAULT_USER_ID).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 63 // 创建测试路径以及测试环境 64 string cmdMkdir = string("mkdir -p ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME; 65 system(cmdMkdir.c_str()); 66 string cmdTool = string("mkdir -p ") + BConstants::GetSaBundleBackupToolDir(BConstants::DEFAULT_USER_ID).data(); 67 system(cmdTool.c_str()); 68 string touchTar = string("touch ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME + FILE_NAME; 69 system(touchTar.c_str()); 70 string touchManage = string("touch ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME + MANAGE_JSON; 71 system(touchManage.c_str()); 72 string touchTmp = string("touch ") + localCap; 73 system(touchTmp.c_str()); 74 75 // 尝试匹配当前命令,成功后执行 76 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-restore"; 77 vector<string_view> curOp; 78 curOp.emplace_back("restore"); __anoncf315f360202(const ToolsOp &op) 79 auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); }; 80 auto &&opeartions = ToolsOp::GetAllOperations(); 81 auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed); 82 if (matchedOp != opeartions.end()) { 83 auto ret = matchedOp->Execute(mapArgToVal); 84 EXPECT_EQ(ret, 0); 85 } 86 } catch (...) { 87 EXPECT_TRUE(false); 88 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-an exception occurred by construction."; 89 } 90 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-end SUB_backup_tools_op_restore_0100"; 91 } 92 93 /** 94 * @tc.number: SUB_backup_tools_op_restore_0200 95 * @tc.name: SUB_backup_tools_op_restore_0200 96 * @tc.desc: 测试 97 * @tc.size: MEDIUM 98 * @tc.type: FUNC 99 * @tc.level Level 1 100 * @tc.require: I6F3GV 101 */ 102 HWTEST_F(ToolsOpRestoreTest, SUB_backup_tools_op_restore_0200, testing::ext::TestSize.Level1) 103 { 104 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-begin SUB_backup_tools_op_restore_0200"; 105 try { 106 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-The pathCapFile field is not contained."; 107 map<string, vector<string>> mapArgToVal; 108 vector<string> bundles = {"com.example.app2backup"}; 109 vector<string> path = {"/data/backup/tmp"}; 110 mapArgToVal.insert(make_pair("bundles", bundles)); 111 112 vector<string_view> curOp; 113 curOp.emplace_back("restore"); __anoncf315f360302(const ToolsOp &op) 114 auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); }; 115 auto &&opeartions = ToolsOp::GetAllOperations(); 116 auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed); 117 int ret = 0; 118 if (matchedOp != opeartions.end()) { 119 ret = matchedOp->Execute(mapArgToVal); 120 EXPECT_NE(ret, 0); 121 } 122 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-The bundles field is not contained."; 123 mapArgToVal.clear(); 124 mapArgToVal.insert(make_pair("pathCapFile", path)); 125 if (matchedOp != opeartions.end()) { 126 ret = matchedOp->Execute(mapArgToVal); 127 EXPECT_NE(ret, 0); 128 } 129 130 mapArgToVal.clear(); 131 if (matchedOp != opeartions.end()) { 132 ret = matchedOp->Execute(mapArgToVal); 133 EXPECT_NE(ret, 0); 134 } 135 } catch (...) { 136 EXPECT_TRUE(false); 137 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-an exception occurred by construction."; 138 } 139 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-end SUB_backup_tools_op_restore_0200"; 140 } 141 } // namespace OHOS::FileManagement::Backup