mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Deprecate kCheckIllegalCharacters flag.
PiperOrigin-RevId: 660433186
This commit is contained in:
committed by
Copybara-Service
parent
9d324f79d8
commit
b0f58ee5a0
@@ -29,9 +29,11 @@
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/types/span.h"
|
||||
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
|
||||
#include "internal/flags/nearby_flags.h"
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
@@ -40,14 +42,14 @@
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
|
||||
const wchar_t* kUpOneLevel = L"/..";
|
||||
const wchar_t* kUpOneLevel = L"..";
|
||||
constexpr wchar_t kDot = L'.';
|
||||
constexpr wchar_t kPathDelimiter = L'/';
|
||||
constexpr wchar_t kReplacementChar = L'_';
|
||||
constexpr wchar_t kForwardSlash = L'/';
|
||||
constexpr wchar_t kBackSlash = L'\\';
|
||||
|
||||
wchar_t const* kForbiddenPathNames[] = {
|
||||
constexpr std::wstring_view kForbiddenPathNames[] = {
|
||||
L"CON", L"PRN", L"AUX", L"NUL", L"COM1", L"COM2", L"COM3", L"COM4",
|
||||
L"COM5", L"COM6", L"COM7", L"COM8", L"COM9", L"LPT1", L"LPT2", L"LPT3",
|
||||
L"LPT4", L"LPT5", L"LPT6", L"LPT7", L"LPT8", L"LPT9"};
|
||||
@@ -55,22 +57,14 @@ wchar_t const* kForbiddenPathNames[] = {
|
||||
std::wstring FilePath::GetCustomSavePath(std::wstring parent_folder,
|
||||
std::wstring file_name) {
|
||||
std::wstring path;
|
||||
if (NearbyFlags::GetInstance().GetBoolFlag(
|
||||
nearby::connections::config_package_nearby::
|
||||
nearby_connections_feature::kCheckIllegalCharacters)) {
|
||||
SanitizeFileName(file_name);
|
||||
}
|
||||
SanitizeFileName(file_name);
|
||||
path += parent_folder + kPathDelimiter + file_name;
|
||||
return CreateOutputFileWithRename(path);
|
||||
}
|
||||
|
||||
std::wstring FilePath::GetDownloadPath(std::wstring parent_folder,
|
||||
std::wstring file_name) {
|
||||
if (NearbyFlags::GetInstance().GetBoolFlag(
|
||||
nearby::connections::config_package_nearby::
|
||||
nearby_connections_feature::kCheckIllegalCharacters)) {
|
||||
SanitizeFileName(file_name);
|
||||
}
|
||||
SanitizeFileName(file_name);
|
||||
return CreateOutputFileWithRename(
|
||||
GetDownloadPathInternal(parent_folder, file_name));
|
||||
}
|
||||
@@ -212,30 +206,24 @@ std::wstring FilePath::MutateForbiddenPathElements(std::wstring& str) {
|
||||
if (lastToken.length() > 0) path_elements.push_back(lastToken);
|
||||
|
||||
std::wstring processed_path;
|
||||
absl::Span<const std::wstring_view> forbidden(kForbiddenPathNames);
|
||||
|
||||
for (auto& path_element : path_elements) {
|
||||
auto tmp_path_element = path_element;
|
||||
|
||||
if (NearbyFlags::GetInstance().GetBoolFlag(
|
||||
nearby::connections::config_package_nearby::
|
||||
nearby_connections_feature::kCheckIllegalCharacters)) {
|
||||
if (tmp_path_element.size() == 1 && tmp_path_element[0] == kDot) {
|
||||
// Change the dot path name to an underscore.
|
||||
tmp_path_element[0] = kReplacementChar;
|
||||
NEARBY_LOGS(INFO) << "Renamed path element "
|
||||
if (tmp_path_element.size() == 1 && tmp_path_element[0] == kDot) {
|
||||
// Change the dot path name to an underscore.
|
||||
tmp_path_element[0] = kReplacementChar;
|
||||
NEARBY_LOGS(INFO) << "Renamed path element "
|
||||
<< wstring_to_string(path_element) << " to "
|
||||
<< wstring_to_string(tmp_path_element);
|
||||
path_element[0] = kReplacementChar;
|
||||
}
|
||||
path_element[0] = kReplacementChar;
|
||||
}
|
||||
|
||||
std::transform(tmp_path_element.begin(), tmp_path_element.end(),
|
||||
tmp_path_element.begin(),
|
||||
[](wchar_t c) { return std::toupper(c); });
|
||||
|
||||
std::vector<std::wstring> forbidden(std::begin(kForbiddenPathNames),
|
||||
std::end(kForbiddenPathNames));
|
||||
|
||||
while (std::find(forbidden.begin(), forbidden.end(), tmp_path_element) !=
|
||||
forbidden.end()) {
|
||||
tmp_path_element.insert(tmp_path_element.begin(), kReplacementChar);
|
||||
@@ -262,16 +250,6 @@ void FilePath::SanitizeFileName(std::wstring& file_name) {
|
||||
}
|
||||
|
||||
void FilePath::SanitizePath(std::wstring& path) {
|
||||
size_t pos = std::wstring::npos;
|
||||
if (!NearbyFlags::GetInstance().GetBoolFlag(
|
||||
nearby::connections::config_package_nearby::
|
||||
nearby_connections_feature::kCheckIllegalCharacters)) {
|
||||
// Search for the substring in string in a loop until nothing is found
|
||||
while ((pos = path.find(kUpOneLevel)) != std::string::npos) {
|
||||
// If found then erase it from the string
|
||||
path.erase(pos, wcslen(kUpOneLevel));
|
||||
}
|
||||
}
|
||||
path = MutateForbiddenPathElements(path);
|
||||
ReplaceInvalidCharacters(path);
|
||||
}
|
||||
@@ -290,15 +268,11 @@ void FilePath::ReplaceInvalidCharacters(std::wstring& path) {
|
||||
<< std::string(1, kReplacementChar);
|
||||
*it = kReplacementChar;
|
||||
}
|
||||
if (NearbyFlags::GetInstance().GetBoolFlag(
|
||||
nearby::connections::config_package_nearby::
|
||||
nearby_connections_feature::kCheckIllegalCharacters)) {
|
||||
if (*it == 0) { // character is null
|
||||
NEARBY_LOGS(INFO) << "In path " << wstring_to_string(path)
|
||||
<< " replaced \'NULL\' with \'"
|
||||
<< std::string(1, kReplacementChar) << "\'";
|
||||
*it = kReplacementChar;
|
||||
}
|
||||
if (*it == 0) { // character is null
|
||||
NEARBY_LOGS(INFO) << "In path " << wstring_to_string(path)
|
||||
<< " replaced \'NULL\' with \'"
|
||||
<< std::string(1, kReplacementChar) << "\'";
|
||||
*it = kReplacementChar;
|
||||
}
|
||||
for (auto illegal_character : kIllegalFileCharacters) {
|
||||
if (*it == illegal_character) {
|
||||
|
||||
@@ -22,11 +22,8 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
|
||||
#include "internal/flags/nearby_flags.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
@@ -129,71 +126,6 @@ FolderArgumentsShouldReturnBaseDownloadPath) {
|
||||
EXPECT_EQ(actual, default_download_path_);
|
||||
} // NOLINT false lint error here
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithAttemptToEscape\
|
||||
UsersDownloadFolderShouldReturnDownloadPathNotEscapingUsersDownloadFolder) {
|
||||
std::wstring parent_folder(kImmediateEscape);
|
||||
std::wstring file_name(L"");
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
EXPECT_EQ(actual, default_download_path_);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithMultiple\
|
||||
AttemptsToEscapeUsersDownloadFolderWithBackslashShouldReturnDownloadPath\
|
||||
NotEscapingUsersDownloadFolder) {
|
||||
std::wstring parent_folder(kLongEscapeBackSlash);
|
||||
std::wstring file_name(L"");
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithMultiple\
|
||||
AttemptsToEscapeUsersDownloadFolderShouldReturnDownloadPathNotEscapingUsers\
|
||||
DownloadFolder) {
|
||||
std::wstring parent_folder(kLongEscapeSlash);
|
||||
std::wstring file_name(L"");
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithMultiple\
|
||||
AttemptsToEscapeUsersDownloadFolderWithMixedSlashShouldReturnDownloadPath\
|
||||
NotEscapingUsersDownloadFolder) {
|
||||
std::wstring parent_folder(kLongEscapeMixedSlash);
|
||||
std::wstring file_name(L"");
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithMultiple\
|
||||
AttemptsToEscapeUsersDownloadFolderWithEndingEscapeShouldReturnDownload\
|
||||
PathNotEscapingUsersDownloadFolder) {
|
||||
std::wstring parent_folder(kLongEscapeEndingEscape);
|
||||
std::wstring file_name(L"");
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithMultiple\
|
||||
AttemptsToEscapeUsersDownloadFolderWithEndingSlashShouldReturnDownloadPathNot\
|
||||
EscapingUsersDownloadFolder) {
|
||||
std::wstring parent_folder(kLongEscapeEndingEscapeWithSlash);
|
||||
std::wstring file_name(L"");
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithSlashFileName\
|
||||
ArgumentsShouldReturnBaseDownloadPath) {
|
||||
std::wstring parent_folder(L"");
|
||||
@@ -858,13 +790,6 @@ AHoleBetweenRenamedFiles) {
|
||||
ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit);
|
||||
}
|
||||
|
||||
void SetCheckIllegalCharactersFlag(bool value) {
|
||||
NearbyFlags::GetInstance().OverrideBoolFlagValue(
|
||||
nearby::connections::config_package_nearby::nearby_connections_feature::
|
||||
kCheckIllegalCharacters,
|
||||
value);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithFileName\
|
||||
FileNameTwoDotsFrontShouldReturnBaseDownloadPathWithFileNameTwoDotsFront) {
|
||||
std::wstring parent_folder(L"");
|
||||
@@ -875,19 +800,12 @@ FileNameTwoDotsFrontShouldReturnBaseDownloadPathWithFileNameTwoDotsFront) {
|
||||
|
||||
std::wstring expected = path.str();
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
EXPECT_NE(actual, expected);
|
||||
|
||||
SetCheckIllegalCharactersFlag(true);
|
||||
actual = FilePath::GetDownloadPath(parent_folder, file_name);
|
||||
SetCheckIllegalCharactersFlag(false);
|
||||
auto actual = FilePath::GetDownloadPath(parent_folder, file_name);
|
||||
EXPECT_EQ(actual, expected);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithFileName\
|
||||
FileNameThreeDotsShouldReturnBaseDownloadPathWithUnderscore) {
|
||||
SetCheckIllegalCharactersFlag(true);
|
||||
std::wstring parent_folder(L"");
|
||||
std::wstring file_name(kFileNameWithThreeDots);
|
||||
|
||||
@@ -898,14 +816,11 @@ FileNameThreeDotsShouldReturnBaseDownloadPathWithUnderscore) {
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
SetCheckIllegalCharactersFlag(false);
|
||||
EXPECT_EQ(actual, expected);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPath_FileExistsReturns\
|
||||
FileWithIncrementedNameWithNull) {
|
||||
SetCheckIllegalCharactersFlag(true);
|
||||
|
||||
std::wstring file_name(kFileName);
|
||||
int size = file_name.size();
|
||||
file_name.append(L"1.txt");
|
||||
@@ -940,13 +855,11 @@ FileWithIncrementedNameWithNull) {
|
||||
|
||||
input_file.open(output_file_path, std::ifstream::binary | std::ifstream::in);
|
||||
|
||||
SetCheckIllegalCharactersFlag(false);
|
||||
ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit);
|
||||
}
|
||||
|
||||
TEST_F(FilePathTests, GetDownloadPathWithFileName\
|
||||
ParentFolderWithADotShouldBeReplaceedWithUnderscore) {
|
||||
SetCheckIllegalCharactersFlag(true);
|
||||
std::wstring parent_folder(L"test/./folder/");
|
||||
std::wstring file_name(kFileName);
|
||||
|
||||
@@ -957,7 +870,6 @@ ParentFolderWithADotShouldBeReplaceedWithUnderscore) {
|
||||
|
||||
auto actual(FilePath::GetDownloadPath(parent_folder, file_name));
|
||||
|
||||
SetCheckIllegalCharactersFlag(false);
|
||||
EXPECT_EQ(actual, expected);
|
||||
}
|
||||
} // namespace windows
|
||||
|
||||
Reference in New Issue
Block a user