From 0f9f4ba1ea499a5df2f8ce11fc5f3e729e1a087a Mon Sep 17 00:00:00 2001 From: chensihan Date: Thu, 26 Dec 2024 10:14:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8C=BF=E5=90=8D?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=20Signed-off-by:=20chensihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unittests/backup_utils/BUILD.gn | 26 +++++++ .../backup_utils/b_anony/b_anony_test.cpp | 69 +++++++++++++++++++ utils/src/b_anony/b_anony.cpp | 28 +++++--- 3 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 tests/unittests/backup_utils/b_anony/b_anony_test.cpp diff --git a/tests/unittests/backup_utils/BUILD.gn b/tests/unittests/backup_utils/BUILD.gn index 2b543823f..4a16ef68c 100644 --- a/tests/unittests/backup_utils/BUILD.gn +++ b/tests/unittests/backup_utils/BUILD.gn @@ -14,6 +14,31 @@ import("//build/test.gni") import("//foundation/filemanagement/app_file_service/backup.gni") +ohos_unittest("b_anony_test") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + cfi = true + cfi_cross_dso = true + debug = false + } + + module_out_path = path_module_out_tests + + sources = [ "b_anony/b_anony_test.cpp" ] + + deps = [ + "${path_backup}/tests/utils:backup_test_utils", + "${path_backup}/utils/:backup_utils", + ] + + external_deps = [ "hilog:libhilog" ] + + defines = [ "private = public" ] + + use_exceptions = true +} + ohos_unittest("b_error_test") { branch_protector_ret = "pac_ret" sanitize = { @@ -374,6 +399,7 @@ group("backup_test") { testonly = true deps = [ + ":b_anony_test", ":b_error_test", ":b_file_test", ":b_json_clear_data_test", diff --git a/tests/unittests/backup_utils/b_anony/b_anony_test.cpp b/tests/unittests/backup_utils/b_anony/b_anony_test.cpp new file mode 100644 index 000000000..c12ef8605 --- /dev/null +++ b/tests/unittests/backup_utils/b_anony/b_anony_test.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024 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. + */ + +#include +#include + +#include "b_anony/b_anony.h" + +namespace OHOS::FileManagement::Backup { +class BAnonyTest : public testing::Test { +public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.number: SUB_backup_b_anony_GetAnonyPath_0100 + * @tc.name: b_anony_GetAnonyPath_0100 + * @tc.desc: Test function of GetAnonyPath interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 0 + * @tc.require: I6F3GV + */ +HWTEST_F(BAnonyTest, b_anony_GetAnonyPath_0100, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "BAnonyTest-begin b_anony_GetAnonyPath_0100"; + try { + std::string path = "/"; + std::string result = "/"; + EXPECT_EQ(GetAnonyPath(path), result); + path = "//"; + result = "//"; + EXPECT_EQ(GetAnonyPath(path), result); + path = "test.txt"; + result = "t******t.txt"; + EXPECT_EQ(GetAnonyPath(path), result); + path = "/test.txt"; + result = "/t******t.txt"; + EXPECT_EQ(GetAnonyPath(path), result); + path = "/*/*/shfkwam/xxf/x/xdf.db.xxx.xx"; + result = "/******/******/s******m/x******f/******/x******f.db.xxx.xx"; + EXPECT_EQ(GetAnonyPath(path), result); + path = "/euxnems/ioio...xxx/sk.ppt"; + result = "/e******s/i******x/******.ppt"; + EXPECT_EQ(GetAnonyPath(path), result); + path = "/....../......"; + result = "/.******./******......"; + EXPECT_EQ(GetAnonyPath(path), result); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BAnonyTest-an exception occurred by construction."; + } + GTEST_LOG_(INFO) << "BAnonyTest-end b_error_GetAnonyPath_0100"; +} \ No newline at end of file diff --git a/utils/src/b_anony/b_anony.cpp b/utils/src/b_anony/b_anony.cpp index ecbe94108..0e3b1c952 100644 --- a/utils/src/b_anony/b_anony.cpp +++ b/utils/src/b_anony/b_anony.cpp @@ -48,16 +48,26 @@ std::string GetAnonyString(const std::string &value) std::string GetAnonyPath(const std::string &value) { std::string res; - std::string sub; - size_t found = value.find_last_of('/'); - if (found == std::string::npos) { - sub = value; - } else { - sub = value.substr(found + 1); - res = value.substr(0, found + 1); + size_t pos = 0; + size_t found = value.find('/'); + while (found != std::string::npos) { + if (pos != found) { + res += GetAnonyString(value.substr(pos, found - pos)); + } + res += '/'; + pos = found + 1; + found = value.find('/', pos); + } + //处理文件名 + if (pos < value.length()) { + found = value.find('.', pos); + if (found != std::string::npos) { + res += GetAnonyString(value.substr(pos, found - pos)); + res += value.substr(found); + } else { + res += GetAnonyString(value.substr(pos)); + } } - res += GetAnonyString(sub); - return res; } } \ No newline at end of file -- Gitee From dd208c13e96e9f11b51502f11c920ca12a8cd3e5 Mon Sep 17 00:00:00 2001 From: chensihan Date: Thu, 26 Dec 2024 03:24:06 +0000 Subject: [PATCH 2/2] update tests/unittests/backup_utils/b_anony/b_anony_test.cpp. Signed-off-by: chensihan --- tests/unittests/backup_utils/b_anony/b_anony_test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unittests/backup_utils/b_anony/b_anony_test.cpp b/tests/unittests/backup_utils/b_anony/b_anony_test.cpp index c12ef8605..fbe16d220 100644 --- a/tests/unittests/backup_utils/b_anony/b_anony_test.cpp +++ b/tests/unittests/backup_utils/b_anony/b_anony_test.cpp @@ -61,9 +61,13 @@ HWTEST_F(BAnonyTest, b_anony_GetAnonyPath_0100, testing::ext::TestSize.Level0) path = "/....../......"; result = "/.******./******......"; EXPECT_EQ(GetAnonyPath(path), result); + path = "downloads/../&^%&*#/IMGS.tar.lz4"; + result = "d******s/******/&******#/I******S.tar.lz4"; + EXPECT_EQ(GetAnonyPath(path), result); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "BAnonyTest-an exception occurred by construction."; } GTEST_LOG_(INFO) << "BAnonyTest-end b_error_GetAnonyPath_0100"; +} } \ No newline at end of file -- Gitee