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 <string> 17 #include <string_view> 18 #include <vector> 19 20 #include <fcntl.h> 21 #include <sys/stat.h> 22 #include <sys/types.h> 23 24 #include <file_ex.h> 25 #include <gtest/gtest.h> 26 #include <unique_fd.h> 27 28 #include "b_error/b_error.h" 29 #include "b_tarball/b_tarball_cmdline.h" 30 #include "test_manager.h" 31 32 namespace OHOS::FileManagement::Backup { 33 using namespace std; 34 35 class BTarballCmdlineTest : public testing::Test { 36 public: SetUpTestCase()37 static void SetUpTestCase() {}; TearDownTestCase()38 static void TearDownTestCase() {}; SetUp()39 void SetUp() {}; TearDown()40 void TearDown() {}; 41 }; 42 43 /** 44 * @tc.number: SUB_b_tarball_cmdline_0100 45 * @tc.name: b_tarball_cmdline_0100 46 * @tc.desc: 测试BTarballCmdline类构造函数是否成功 47 * @tc.size: MEDIUM 48 * @tc.type: FUNC 49 * @tc.level Level 1 50 * @tc.require: I6F3GV 51 */ 52 HWTEST_F(BTarballCmdlineTest, b_tarball_cmdline_0100, testing::ext::TestSize.Level1) 53 { 54 GTEST_LOG_(INFO) << "BTarballCmdlineTest-begin b_tarball_cmdline_0100"; 55 try { 56 TestManager tm("b_tarball_cmdline_0100"); 57 string root = tm.GetRootDirCurTest(); 58 string_view tarballDir = root; 59 string_view tarballName = "test.tar"; 60 61 BTarballCmdline tarballCmdline(tarballDir, tarballName); 62 } catch (...) { 63 EXPECT_TRUE(false); 64 GTEST_LOG_(INFO) << "BTarballCmdlineTest-an exception occurred by BTarballCmdline."; 65 } 66 GTEST_LOG_(INFO) << "BTarballCmdlineTest-end b_tarball_cmdline_0100"; 67 } 68 69 /** 70 * @tc.number: SUB_b_tarball_cmdline_0200 71 * @tc.name: b_tarball_cmdline_0200 72 * @tc.desc: 测试BTarballCmdline类Tar函数是否成功 73 * @tc.size: MEDIUM 74 * @tc.type: FUNC 75 * @tc.level Level 1 76 * @tc.require: I6F3GV 77 */ 78 HWTEST_F(BTarballCmdlineTest, b_tarball_cmdline_0200, testing::ext::TestSize.Level1) 79 { 80 GTEST_LOG_(INFO) << "BTarballCmdlineTest-begin b_tarball_cmdline_0200"; 81 try { 82 // 预置文件和目录 83 TestManager tm("b_tarball_cmdline_0200"); 84 string root = tm.GetRootDirCurTest(); 85 string_view tarballDir = root; 86 string_view tarballName = "test.tar"; 87 string testDir = root + "/testdir"; 88 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) { 89 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno; 90 throw BError(errno); 91 } 92 string strFile = root + tarballName.data(); 93 UniqueFd fd(open(strFile.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)); 94 if (fd < 0) { 95 GTEST_LOG_(INFO) << " invoked open failure, errno :" << errno; 96 throw BError(errno); 97 } 98 99 string aFile = testDir + "/a.txt"; 100 string bFile = testDir + "/b.txt"; 101 SaveStringToFile(aFile, "hello"); 102 SaveStringToFile(bFile, "world"); 103 vector<string_view> includes {testDir}; 104 vector<string_view> excludes {bFile}; 105 106 // 调用tar打包 107 BTarballCmdline tarballCmdline(tarballDir, tarballName); 108 tarballCmdline.Tar(root, includes, excludes); 109 } catch (...) { 110 EXPECT_TRUE(false); 111 GTEST_LOG_(INFO) << "BTarballCmdlineTest-an exception occurred by BTarballCmdline."; 112 } 113 GTEST_LOG_(INFO) << "BTarballCmdlineTest-end b_tarball_cmdline_0200"; 114 } 115 116 /** 117 * @tc.number: SUB_b_tarball_cmdline_0300 118 * @tc.name: b_tarball_cmdline_0300 119 * @tc.desc: 测试BTarballCmdline类Untar函数是否成功 120 * @tc.size: MEDIUM 121 * @tc.type: FUNC 122 * @tc.level Level 1 123 * @tc.require: I6F3GV 124 */ 125 HWTEST_F(BTarballCmdlineTest, b_tarball_cmdline_0300, testing::ext::TestSize.Level1) 126 { 127 GTEST_LOG_(INFO) << "BTarballCmdlineTest-begin b_tarball_cmdline_0300"; 128 try { 129 // 预置文件和目录 130 TestManager tm("b_tarball_cmdline_0300"); 131 string root = tm.GetRootDirCurTest(); 132 string_view tarballDir = root; 133 string_view tarballName = "test.tar"; 134 string testUntarDir = root + "/untardir"; 135 if (mkdir(testUntarDir.data(), S_IRWXU) && errno != EEXIST) { 136 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno; 137 throw BError(errno); 138 } 139 // 调用tar打包 140 BTarballCmdline tarballCmdline(tarballDir, tarballName); 141 tarballCmdline.Untar(testUntarDir); 142 143 } catch (...) { 144 EXPECT_TRUE(false); 145 GTEST_LOG_(INFO) << "BTarballCmdlineTest-an exception occurred by BTarballCmdline."; 146 } 147 GTEST_LOG_(INFO) << "BTarballCmdlineTest-end b_tarball_cmdline_0300"; 148 } 149 } // namespace OHOS::FileManagement::Backup