From f77aa7d227be155f52de1633b161b88307f301c6 Mon Sep 17 00:00:00 2001 From: jaychao Date: Tue, 12 Dec 2023 12:18:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ETDD-tar=5Ffile=5Ftest-3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jaychao --- tests/unittests/backup_ext/tar_file_test.cpp | 293 +++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 tests/unittests/backup_ext/tar_file_test.cpp diff --git a/tests/unittests/backup_ext/tar_file_test.cpp b/tests/unittests/backup_ext/tar_file_test.cpp new file mode 100644 index 000000000..b7cfa7e3f --- /dev/null +++ b/tests/unittests/backup_ext/tar_file_test.cpp @@ -0,0 +1,293 @@ +/* + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @tc.number: SUB_Tar_File_CompleteBlock_0200 + * @tc.name: SUB_Tar_File_CompleteBlock_0200 + * @tc.desc: 测试 CompleteBlock 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_CompleteBlock_0200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_CompleteBlock_0200"; + try { + CreateTarFile("SUB_Tar_File_CompleteBlock_0200"); + off_t size = 1; + bool ret = TarFile::GetInstance().CompleteBlock(size); + EXPECT_TRUE(ret); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_CompleteBlock_0200"; +} + +/** + * @tc.number: SUB_Tar_File_FillSplitTailBlocks_0100 + * @tc.name: SUB_Tar_File_FillSplitTailBlocks_0100 + * @tc.desc: 测试 FillSplitTailBlocks 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_FillSplitTailBlocks_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_FillSplitTailBlocks_0100"; + try { + bool ret = TarFile::GetInstance().FillSplitTailBlocks(); + EXPECT_FALSE(ret); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_FillSplitTailBlocks_0100"; +} + +/** + * @tc.number: SUB_Tar_File_FillSplitTailBlocks_0200 + * @tc.name: SUB_Tar_File_FillSplitTailBlocks_0200 + * @tc.desc: 测试 FillSplitTailBlocks 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_FillSplitTailBlocks_0200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_FillSplitTailBlocks_0200"; + try { + CreateTarFile("SUB_Tar_File_FillSplitTailBlocks_0200"); + bool ret = TarFile::GetInstance().FillSplitTailBlocks(); + EXPECT_FALSE(ret); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_FillSplitTailBlocks_0200"; +} + +/** + * @tc.number: SUB_Tar_File_SetCheckSum_0100 + * @tc.name: SUB_Tar_File_SetCheckSum_0100 + * @tc.desc: 测试 SetCheckSum 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_SetCheckSum_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_SetCheckSum_0100"; + try { + TarHeader hdr; + TarFile::GetInstance().SetCheckSum(hdr); + EXPECT_TRUE(strlen(hdr.chksum) > 0); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_SetCheckSum_0100"; +} + +/** + * @tc.number: SUB_Tar_File_FillOwnerName_0100 + * @tc.name: SUB_Tar_File_FillOwnerName_0100 + * @tc.desc: 测试 FillOwnerName 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_FillOwnerName_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_FillOwnerName_0100"; + try { + TarHeader hdr; + struct stat st; + TarFile::GetInstance().FillOwnerName(hdr, st); + EXPECT_TRUE(strcmp(hdr.uname, "") != 0); + EXPECT_TRUE(strcmp(hdr.gname, "") != 0); + GTEST_LOG_(INFO) << "hdr.uname:" << hdr.uname << ", hdr.gname:" << hdr.gname; + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_FillOwnerName_0100"; +} + +/** + * @tc.number: SUB_Tar_File_WriteLongName_0100 + * @tc.name: SUB_Tar_File_WriteLongName_0100 + * @tc.desc: 测试 WriteLongName 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_WriteLongName_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_WriteLongName_0100"; + try { + CreateTarFile("SUB_Tar_File_WriteLongName_0100"); + string name(""); + char type = GNUTYPE_LONGNAME; + bool ret = TarFile::GetInstance().WriteLongName(name, type); + EXPECT_TRUE(ret); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_WriteLongName_0100"; +} + +/** + * @tc.number: SUB_Tar_File_ReadAll_0100 + * @tc.name: SUB_Tar_File_ReadAll_0100 + * @tc.desc: 测试 ReadAll 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_ReadAll_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_ReadAll_0100"; + try { + int fd = 0; + std::vector ioBuffer {}; + off_t size = 0; + int ret = TarFile::GetInstance().ReadAll(fd, ioBuffer, size); + EXPECT_EQ(ret, 0); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_ReadAll_0100"; +} + +/** + * @tc.number: SUB_Tar_File_WriteAll_0100 + * @tc.name: SUB_Tar_File_WriteAll_0100 + * @tc.desc: 测试 WriteAll 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_WriteAll_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_WriteAll_0100"; + try { + std::vector buffer {}; + off_t len = 0; + int ret = TarFile::GetInstance().WriteAll(buffer, len); + EXPECT_EQ(ret, 0); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_WriteAll_0100"; +} + +/** + * @tc.number: SUB_Tar_File_WriteTarHeader_0100 + * @tc.name: SUB_Tar_File_WriteTarHeader_0100 + * @tc.desc: 测试 WriteTarHeader 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_WriteTarHeader_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_WriteTarHeader_0100"; + try { + CreateTarFile("SUB_Tar_File_WriteTarHeader_0100"); + TarHeader header; + int ret = TarFile::GetInstance().WriteTarHeader(header); + EXPECT_EQ(ret, BLOCK_SIZE); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_WriteTarHeader_0100"; +} + +/** + * @tc.number: SUB_Tar_File_I2Ocs_0100 + * @tc.name: SUB_Tar_File_I2Ocs_0100 + * @tc.desc: 测试 I2Ocs 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_I2Ocs_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_I2Ocs_0100"; + try { + int len = 0; + off_t val = 0; + string ret = TarFile::GetInstance().I2Ocs(len, val); + EXPECT_EQ(ret, "0"); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_I2Ocs_0100"; +} + +/** + * @tc.number: SUB_Tar_File_I2OcsConvert_0100 + * @tc.name: SUB_Tar_File_I2OcsConvert_0100 + * @tc.desc: 测试 I2OcsConvert 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(TarFileTest, SUB_Tar_File_I2OcsConvert_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TarFileTest-begin SUB_Tar_File_I2OcsConvert_0100"; + try { + struct stat st; + TarHeader hdr; + std::string fileName(""); + bool isSplit = false; + bool ret = TarFile::GetInstance().I2OcsConvert(st, hdr, fileName, isSplit); + EXPECT_TRUE(ret); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "TarFileTest-an exception occurred by TarFile."; + } + GTEST_LOG_(INFO) << "TarFileTest-end SUB_Tar_File_I2OcsConvert_0100"; +} + +} // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee