diff --git a/tests/unittests/backup_utils/BUILD.gn b/tests/unittests/backup_utils/BUILD.gn index 2b543823f4c4697ecbe3422a50e172127068a2ac..4a16ef68cc82112f2fd75758e5f3cca2ca2b088a 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 0000000000000000000000000000000000000000..fbe16d220b931208d1a4c23ef9705df5efb49e3b --- /dev/null +++ b/tests/unittests/backup_utils/b_anony/b_anony_test.cpp @@ -0,0 +1,73 @@ +/* + * 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); + 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 diff --git a/utils/src/b_anony/b_anony.cpp b/utils/src/b_anony/b_anony.cpp index ecbe941081d06fd837a32c96875411e2372cf32e..0e3b1c952e12d3982978eed5ca530aac97a1c0f2 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