diff --git a/internal/platform/implementation/linux/BUILD b/internal/platform/implementation/linux/BUILD index 9119e7f6..e2bf1de9 100644 --- a/internal/platform/implementation/linux/BUILD +++ b/internal/platform/implementation/linux/BUILD @@ -41,8 +41,12 @@ cc_library( #"log_message.cc", "timer.cc", "linux_flags.cc", + "file_path.cc", + "http_loader.cc", ], hdrs = [ + "file_path.h", + "http_loader.h", "atomic_boolean.h", "atomic_reference.h", "atomic_uint32.h", @@ -307,17 +311,16 @@ cc_test( "ble_l2cap_socket_test.cc", "bluetooth_classic_socket_test.cc", # "bluetooth_adapter_test.cc", - # "crypto_test.cc", - # "device_info_test.cc", + "crypto_test.cc", # "executor_test.cc", - # "file_path_test.cc", - # "http_loader_test.cc", + "file_path_test.cc", + # "http_loader_test.cc", # "preferences_manager_test.cc", # "preferences_repository_test.cc", - # "scheduled_executor_test.cc", - # "submittable_executor_test.cc", - # "thread_pool_test.cc", - # "timer_test.cc", + # "scheduled_executor_test.cc", + # "submittable_executor_test.cc", + # "thread_pool_test.cc", + "timer_test.cc", ], tags = ["notap"], deps = [ diff --git a/internal/platform/implementation/linux/file_path.cc b/internal/platform/implementation/linux/file_path.cc index 1cc1d1cd..5fb469f4 100644 --- a/internal/platform/implementation/linux/file_path.cc +++ b/internal/platform/implementation/linux/file_path.cc @@ -53,20 +53,30 @@ std::wstring FilePath::GetDownloadPathInternal(std::wstring parent_folder, std::wstring file_name) { DeviceInfo info = DeviceInfo(linux::getSystemBusConnection()); - std::optional download_path = info.GetDownloadPath(); + auto nearby_path = info.GetDownloadPath(); + + std::optional download_path = + nearby_path ? std::optional( + std::filesystem::path(nearby_path->ToString())) + : std::nullopt; std::string base_path; - std::wstring wide_path(string_to_wstring(base_path)); - if (!download_path) { - // If grabbing the download path fails then we make a custom one - base_path = getenv("HOME"); - base_path.append("/Downloads"); + const char* home = std::getenv("HOME"); + + if (home == nullptr) { + base_path = "/tmp/Downloads"; // fallback for test environments + } else { + base_path = home; + base_path.append("/Downloads"); + } } else { - base_path = download_path.value(); + base_path = download_path->string(); } + std::wstring wide_path = string_to_wstring(base_path); + // If parent_folder starts with a \\ or /, then strip it while (!parent_folder.empty() && (*parent_folder.begin() == kBackSlash || *parent_folder.begin() == kForwardSlash)) { @@ -119,9 +129,18 @@ std::wstring FilePath::CreateOutputFileWithRename(std::wstring path) { // Remove any /..'s SanitizePath(sanitized_path); - auto last_delimiter = sanitized_path.find_last_of(kPathDelimiter); - std::wstring folder(sanitized_path.substr(0, last_delimiter)); - std::wstring file_name(sanitized_path.substr(last_delimiter)); +auto last_delimiter = sanitized_path.find_last_of(kPathDelimiter); + +std::wstring folder; +std::wstring file_name; + +if (last_delimiter == std::wstring::npos) { + folder = L""; + file_name = sanitized_path; +} else { + folder = sanitized_path.substr(0, last_delimiter); + file_name = sanitized_path.substr(last_delimiter + 1); +} // Locate the last dot auto first = file_name.find_last_of('.'); @@ -157,7 +176,7 @@ std::wstring FilePath::CreateOutputFileWithRename(std::wstring path) { if (count > 0) { LOG(INFO) << "Renamed " << wstring_to_string(path) << " to " - << wstring_to_string(target); + << wstring_to_string(target); } // The above leaves the file open, so close it. @@ -189,16 +208,16 @@ void FilePath::ReplaceInvalidCharacters(std::wstring& path) { for (auto& character : path) { // If 0 < character < 32, it's illegal, replace it if (character > 0 && character < 32) { - LOG(INFO) << "In path " << wstring_to_string(path) - << " replaced \'" << std::string(1, character) - << "\' with \'" << std::string(1, kReplacementChar); + LOG(INFO) << "In path " << wstring_to_string(path) << " replaced \'" + << std::string(1, character) << "\' with \'" + << std::string(1, kReplacementChar); character = kReplacementChar; } for (auto illegal_character : kIllegalFileCharacters) { if (character == illegal_character) { - LOG(INFO) << "In path " << wstring_to_string(path) - << " replaced \'" << std::string(1, character) - << "\' with \'" << std::string(1, kReplacementChar); + LOG(INFO) << "In path " << wstring_to_string(path) << " replaced \'" + << std::string(1, character) << "\' with \'" + << std::string(1, kReplacementChar); character = kReplacementChar; } } diff --git a/internal/platform/implementation/linux/file_path_test.cc b/internal/platform/implementation/linux/file_path_test.cc index e7fe44e0..7f050772 100644 --- a/internal/platform/implementation/linux/file_path_test.cc +++ b/internal/platform/implementation/linux/file_path_test.cc @@ -15,13 +15,14 @@ #include "internal/platform/implementation/linux/file_path.h" #include +#include #include #include #include #include -#include -#include +#include "internal/platform/implementation/linux/device_info.h" +#include "internal/platform/implementation/linux/utils.h" #include "gtest/gtest.h" namespace nearby { @@ -55,14 +56,13 @@ class FilePathTests : public testing::Test { // You can define per-test set-up logic as usual. FilePathTests() { default_download_path_ = - string_to_wstring(DeviceInfo().GetDownloadPath().value_or( - std::string(getenv("HOME")).append("/Downloads"))); + string_to_wstring(std::string(getenv("HOME")).append("/Downloads")); } std::wstring default_download_path_; }; -TEST_F(FilePathTests, GetDownloadPathWithEmptyStringArguments\ -ShouldReturnBaseDownloadPath) { +TEST_F(FilePathTests, + GetDownloadPathWithEmptyStringArgumentsShouldReturnBaseDownloadPath) { std::wstring parent_folder(L""); std::wstring file_name(L""); @@ -71,8 +71,9 @@ ShouldReturnBaseDownloadPath) { EXPECT_EQ(actual, default_download_path_); } // NOLINT false lint error here -TEST_F(FilePathTests, GetDownloadPathWithSlashParent\ -FolderArgumentsShouldReturnBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithSlashParentFolderArgumentsShouldReturnBaseDownloadPath) { std::wstring parent_folder(L"/"); std::wstring file_name(L""); @@ -81,8 +82,9 @@ FolderArgumentsShouldReturnBaseDownloadPath) { EXPECT_EQ(actual, default_download_path_); } // NOLINT false lint error here -TEST_F(FilePathTests, GetDownloadPathWithBackslashParent\ -FolderArgumentsShouldReturnBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithBackslashParentFolderArgumentsShouldReturnBaseDownloadPath) { std::wstring parent_folder(L"\\"); std::wstring file_name(L""); @@ -91,8 +93,9 @@ FolderArgumentsShouldReturnBaseDownloadPath) { EXPECT_EQ(actual, default_download_path_); } // NOLINT false lint error here -TEST_F(FilePathTests, GetDownloadPathWithAttemptToEscape\ -UsersDownloadFolderShouldReturnDownloadPathNotEscapingUsersDownloadFolder) { +TEST_F( + FilePathTests, + GetDownloadPathWithAttemptToEscapeUsersDownloadFolderShouldReturnDownloadPathNotEscapingUsersDownloadFolder) { std::wstring parent_folder(kImmediateEscape); std::wstring file_name(L""); @@ -101,9 +104,9 @@ UsersDownloadFolderShouldReturnDownloadPathNotEscapingUsersDownloadFolder) { EXPECT_EQ(actual, default_download_path_); } -TEST_F(FilePathTests, GetDownloadPathWithMultiple\ -AttemptsToEscapeUsersDownloadFolderWithBackslashShouldReturnDownloadPath\ -NotEscapingUsersDownloadFolder) { +TEST_F( + FilePathTests, + GetDownloadPathWithMultipleAttemptsToEscapeUsersDownloadFolderWithBackslashShouldReturnDownloadPathNotEscapingUsersDownloadFolder) { std::wstring parent_folder(kLongEscapeBackSlash); std::wstring file_name(L""); @@ -112,9 +115,9 @@ NotEscapingUsersDownloadFolder) { EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder); } -TEST_F(FilePathTests, GetDownloadPathWithMultiple\ -AttemptsToEscapeUsersDownloadFolderShouldReturnDownloadPathNotEscapingUsers\ -DownloadFolder) { +TEST_F( + FilePathTests, + GetDownloadPathWithMultipleAttemptsToEscapeUsersDownloadFolderShouldReturnDownloadPathNotEscapingUsersDownloadFolder) { std::wstring parent_folder(kLongEscapeSlash); std::wstring file_name(L""); @@ -123,9 +126,9 @@ DownloadFolder) { EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder); } -TEST_F(FilePathTests, GetDownloadPathWithMultiple\ -AttemptsToEscapeUsersDownloadFolderWithMixedSlashShouldReturnDownloadPath\ -NotEscapingUsersDownloadFolder) { +TEST_F( + FilePathTests, + GetDownloadPathWithMultipleAttemptsToEscapeUsersDownloadFolderWithMixedSlashShouldReturnDownloadPathNotEscapingUsersDownloadFolder) { std::wstring parent_folder(kLongEscapeMixedSlash); std::wstring file_name(L""); @@ -134,9 +137,9 @@ NotEscapingUsersDownloadFolder) { EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder); } -TEST_F(FilePathTests, GetDownloadPathWithMultiple\ -AttemptsToEscapeUsersDownloadFolderWithEndingEscapeShouldReturnDownload\ -PathNotEscapingUsersDownloadFolder) { +TEST_F( + FilePathTests, + GetDownloadPathWithMultipleAttemptsToEscapeUsersDownloadFolderWithEndingEscapeShouldReturnDownloadPathNotEscapingUsersDownloadFolder) { std::wstring parent_folder(kLongEscapeEndingEscape); std::wstring file_name(L""); @@ -145,9 +148,9 @@ PathNotEscapingUsersDownloadFolder) { EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder); } -TEST_F(FilePathTests, GetDownloadPathWithMultiple\ -AttemptsToEscapeUsersDownloadFolderWithEndingSlashShouldReturnDownloadPathNot\ -EscapingUsersDownloadFolder) { +TEST_F( + FilePathTests, + GetDownloadPathWithMultipleAttemptsToEscapeUsersDownloadFolderWithEndingSlashShouldReturnDownloadPathNotEscapingUsersDownloadFolder) { std::wstring parent_folder(kLongEscapeEndingEscapeWithSlash); std::wstring file_name(L""); @@ -156,8 +159,8 @@ EscapingUsersDownloadFolder) { EXPECT_EQ(actual, default_download_path_ + kTwoLevelFolder); } -TEST_F(FilePathTests, GetDownloadPathWithSlashFileName\ -ArgumentsShouldReturnBaseDownloadPath) { +TEST_F(FilePathTests, + GetDownloadPathWithSlashFileNameArgumentsShouldReturnBaseDownloadPath) { std::wstring parent_folder(L""); std::wstring file_name(L"/"); @@ -166,8 +169,9 @@ ArgumentsShouldReturnBaseDownloadPath) { EXPECT_EQ(actual, default_download_path_); } -TEST_F(FilePathTests, GetDownloadPathWithBackslashFile\ -NameArgumentsShouldReturnBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithBackslashFileNameArgumentsShouldReturnBaseDownloadPath) { std::wstring parent_folder(L""); std::wstring file_name(L"\\"); @@ -179,8 +183,9 @@ NameArgumentsShouldReturnBaseDownloadPath) { EXPECT_EQ(actual, default_download_path_); } -TEST_F(FilePathTests, GetDownloadPathWithParentFolder\ -ShouldReturnParentFolderAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithParentFolderShouldReturnParentFolderAppendedToBaseDownloadPath) { std::wstring parent_folder(L"test_parent_folder"); std::wstring file_name(L""); @@ -194,8 +199,9 @@ ShouldReturnParentFolderAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithParentFolder\ -StartingWithSlashArgumentsShouldReturnParentFolderAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithParentFolderStartingWithSlashArgumentsShouldReturnParentFolderAppendedToBaseDownloadPath) { std::wstring parent_folder(L"/test_parent_folder"); std::wstring file_name(L""); @@ -209,9 +215,9 @@ StartingWithSlashArgumentsShouldReturnParentFolderAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithParentFolder\ -StartingWithBackslashArgumentsShouldReturnParentFolderAppendedToBase\ -DownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithParentFolderStartingWithBackslashArgumentsShouldReturnParentFolderAppendedToBaseDownloadPath) { std::wstring parent_folder(L"\\test_parent_folder"); std::wstring file_name(L""); @@ -225,8 +231,9 @@ DownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithParentFolder\ -EndingWithSlashArgumentsShouldReturnParentFolderAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithParentFolderEndingWithSlashArgumentsShouldReturnParentFolderAppendedToBaseDownloadPath) { std::wstring parent_folder(L"test_parent_folder/"); std::wstring file_name(L""); @@ -240,9 +247,9 @@ EndingWithSlashArgumentsShouldReturnParentFolderAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithParentFolder\ -EndingWithBackslashArguments\ -ShouldReturnParentFolderAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithParentFolderEndingWithBackslashArgumentsShouldReturnParentFolderAppendedToBaseDownloadPath) { std::wstring parent_folder(L"test_parent_folder\\"); std::wstring file_name(L""); @@ -256,8 +263,9 @@ ShouldReturnParentFolderAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithFileName\ -BeginningWithSlashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithFileNameBeginningWithSlashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { std::wstring parent_folder(L""); std::wstring file_name(L"/test_file_name.name"); @@ -271,8 +279,9 @@ BeginningWithSlashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithFileName\ -BeginningWithBackslashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithFileNameBeginningWithBackslashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { std::wstring parent_folder(L""); std::wstring file_name(L"\\test_file_name.name"); @@ -284,8 +293,9 @@ BeginningWithBackslashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { EXPECT_EQ(actual, path.str().c_str()); } -TEST_F(FilePathTests, GetDownloadPathWithFileNameEnding\ -WithSlashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithFileNameEndingWithSlashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { std::wstring parent_folder(L""); std::wstring file_name(L"test_file_name.name/"); @@ -299,8 +309,9 @@ WithSlashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithFileNameEnding\ -WithBackslashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithFileNameEndingWithBackslashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { std::wstring parent_folder(L""); std::wstring file_name(L"test_file_name.name\\"); @@ -314,9 +325,9 @@ WithBackslashArgumentsShouldReturnFileNameAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithParentFolderAnd\ -FileNameArgumentsShould\ -ReturnParentFolderAndFileNameAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithParentFolderAndFileNameArgumentsShouldReturnParentFolderAndFileNameAppendedToBaseDownloadPath) { std::wstring parent_folder(L"test_parent_folder"); std::wstring file_name(L"test_file_name.name"); @@ -332,9 +343,9 @@ ReturnParentFolderAndFileNameAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithParentFolder\ -EndingWithBackslashAndFileNameArgumentsShouldReturnParentFolderAndFileName\ -AppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithParentFolderEndingWithBackslashAndFileNameArgumentsShouldReturnParentFolderAndFileNameAppendedToBaseDownloadPath) { std::wstring parent_folder(L"test_parent_folder\\"); std::wstring file_name(L"test_file_name.name"); @@ -350,9 +361,9 @@ AppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPathWithFileName\ -StartingWithBackslashAndParentFolderArgumentsShouldReturnParentFolderAnd\ -FileNameAppendedToBaseDownloadPath) { +TEST_F( + FilePathTests, + GetDownloadPathWithFileNameStartingWithBackslashAndParentFolderArgumentsShouldReturnParentFolderAndFileNameAppendedToBaseDownloadPath) { std::wstring parent_folder(L"test_parent_folder"); std::wstring file_name(L"\\test_file_name.name"); @@ -368,8 +379,9 @@ FileNameAppendedToBaseDownloadPath) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_IllegalFileNameCharacters\ -ReturnsFileNameWithUnderbarSubstituted) { +TEST_F( + FilePathTests, + GetDownloadPath_IllegalFileNameCharactersReturnsFileNameWithUnderbarSubstituted) { // char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x05, // 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 }; auto illegal_character_sequence(L"Test\x5Test"); @@ -384,8 +396,9 @@ ReturnsFileNameWithUnderbarSubstituted) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_LowestIllegalFileNameCharacter\ -ReturnsFileNameWithUnderbarSubstituted) { +TEST_F( + FilePathTests, + GetDownloadPath_LowestIllegalFileNameCharacterReturnsFileNameWithUnderbarSubstituted) { // char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x01, // 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 }; auto illegal_character_sequence(L"Test\x1Test"); @@ -401,8 +414,9 @@ ReturnsFileNameWithUnderbarSubstituted) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_HighestIllegalFileNameCharacter\ -ReturnsFileNameWithUnderbarSubstituted) { +TEST_F( + FilePathTests, + GetDownloadPath_HighestIllegalFileNameCharacterReturnsFileNameWithUnderbarSubstituted) { // char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x1f, // 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 }; auto illegal_character_sequence(L"Test\x1fTest"); @@ -418,8 +432,9 @@ ReturnsFileNameWithUnderbarSubstituted) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_IllegalFileNameCharacterQuestionMark\ -ReturnsFileNameWithUnderbarSubstituted) { +TEST_F( + FilePathTests, + GetDownloadPath_IllegalFileNameCharacterQuestionMarkReturnsFileNameWithUnderbarSubstituted) { // char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2f, // 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 }; auto illegal_character_sequence(L"Test?Test"); @@ -435,8 +450,9 @@ ReturnsFileNameWithUnderbarSubstituted) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_IllegalFileNameCharacterAsterisk\ -ReturnsFileNameWithUnderbarSubstituted) { +TEST_F( + FilePathTests, + GetDownloadPath_IllegalFileNameCharacterAsteriskReturnsFileNameWithUnderbarSubstituted) { // char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2f, // 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 }; auto illegal_character_sequence(L"Test*Test"); @@ -452,8 +468,9 @@ ReturnsFileNameWithUnderbarSubstituted) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_IllegalFileNameCharacterLessThan\ -ReturnsFileNameWithUnderbarSubstituted) { +TEST_F( + FilePathTests, + GetDownloadPath_IllegalFileNameCharacterLessThanReturnsFileNameWithUnderbarSubstituted) { // char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2f, // 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 }; auto illegal_character_sequence(L"TestTest"); @@ -486,8 +504,9 @@ ReturnsFileNameWithUnderbarSubstituted) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_IllegalFileNameCharacterVerticalBar\ -ReturnsFileNameWithUnderbarSubstituted) { +TEST_F( + FilePathTests, + GetDownloadPath_IllegalFileNameCharacterVerticalBarReturnsFileNameWithUnderbarSubstituted) { // char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2f, // 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 }; auto illegal_character_sequence(L"Test|Test"); @@ -503,8 +522,9 @@ ReturnsFileNameWithUnderbarSubstituted) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_IllegalFileNameCharacterColon\ -ReturnsFileNameWithUnderbarSubstituted) { +TEST_F( + FilePathTests, + GetDownloadPath_IllegalFileNameCharacterColonReturnsFileNameWithUnderbarSubstituted) { // char illegal_character_sequence[]{ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2f, // 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0 }; auto illegal_character_sequence(L"Test:Test"); @@ -520,8 +540,8 @@ ReturnsFileNameWithUnderbarSubstituted) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_FileDoesntExist\ -ReturnsFileWithPassedName) { +TEST_F(FilePathTests, + GetDownloadPath_FileDoesntExistReturnsFileWithPassedName) { std::wstring file_name(kFileName); std::wstring parent_folder(L""); @@ -534,8 +554,8 @@ ReturnsFileWithPassedName) { EXPECT_EQ(actual, expected); } -TEST_F(FilePathTests, GetDownloadPath_FileExistsReturns\ -FileWithIncrementedName) { +TEST_F(FilePathTests, + GetDownloadPath_FileExistsReturnsFileWithIncrementedName) { std::wstring file_name(kFileName); std::wstring renamed_file_name(kFirstIterationFileName); std::wstring parent_folder(L""); @@ -571,8 +591,8 @@ FileWithIncrementedName) { ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit); } -TEST_F(FilePathTests, GetDownloadPath_MultipleFilesExist\ -ReturnsNextIncrementedFileName) { +TEST_F(FilePathTests, + GetDownloadPath_MultipleFilesExistReturnsNextIncrementedFileName) { std::ofstream output_file; std::ifstream input_file; @@ -586,7 +606,8 @@ ReturnsNextIncrementedFileName) { expected.append(second_renamed_file_name.c_str()); std::wstring output_file1_path(default_download_path_); - output_file1_path.append(L"/" + file_name); + output_file1_path.append(L"/"); + output_file1_path.append(file_name); std::wstring output_file2_path(default_download_path_); output_file2_path.append(first_renamed_file_name); @@ -610,7 +631,8 @@ ReturnsNextIncrementedFileName) { // Remove the test files and check that it is removed // File 1 std::filesystem::remove(wstring_to_string(output_file1_path).c_str()); - input_file.open(output_file1_path, std::ifstream::binary | std::ifstream::in); + input_file.open(wstring_to_string(output_file1_path), + std::ifstream::binary | std::ifstream::in); ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit); @@ -624,8 +646,9 @@ ReturnsNextIncrementedFileName) { ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit); } -TEST_F(FilePathTests, GetDownloadPath_FileNameContains\ -MultipleDotsReturnsIncrementBeforeFirstDot) { +TEST_F( + FilePathTests, + GetDownloadPath_FileNameContainsMultipleDotsReturnsIncrementBeforeFirstDot) { std::ifstream input_file; std::ofstream output_file; @@ -635,7 +658,8 @@ MultipleDotsReturnsIncrementBeforeFirstDot) { std::wstring parent_folder(L""); std::wstring output_file1_path(default_download_path_); - output_file1_path.append(L"/" + file_name); + output_file1_path.append(L"/"); + output_file1_path.append(file_name); std::wstring output_file2_path(default_download_path_); output_file2_path.append(renamed_file_name); @@ -659,8 +683,8 @@ MultipleDotsReturnsIncrementBeforeFirstDot) { ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit); } -TEST_F(FilePathTests, GetDownloadPath_FileNameContainsNo\ -DotsReturnsWithIncrementAtEnd) { +TEST_F(FilePathTests, + GetDownloadPath_FileNameContainsNoDotsReturnsWithIncrementAtEnd) { std::ifstream input_file; std::ofstream output_file; @@ -670,10 +694,12 @@ DotsReturnsWithIncrementAtEnd) { std::wstring parent_folder(L""); std::wstring output_file1_path(default_download_path_); - output_file1_path.append(L"/" + file_name); + output_file1_path.append(L"/"); + output_file1_path.append(file_name); std::wstring output_file2_path(default_download_path_); - output_file2_path.append(L"/" + renamed_file_name); + output_file2_path.append(L"/"); + output_file2_path.append(renamed_file_name); std::wstring expected(default_download_path_); expected.append(renamed_file_name); @@ -694,8 +720,8 @@ DotsReturnsWithIncrementAtEnd) { ASSERT_FALSE(input_file.rdstate() == std::ifstream::goodbit); } -TEST_F(FilePathTests, GetDownloadPath_FileNameExistsWith\ -AHoleBetweenRenamedFiles) { +TEST_F(FilePathTests, + GetDownloadPath_FileNameExistsWithAHoleBetweenRenamedFiles) { std::ifstream input_file; std::ofstream output_file; @@ -708,8 +734,8 @@ AHoleBetweenRenamedFiles) { // Create the path for the original file name std::wstring output_file_path(default_download_path_); + output_file_path.append(L"/"); output_file_path.append( - L"/" + file_name); // Original file name example: "increment_file_test.txt" // Create the path for the first iteration of the original file name diff --git a/internal/platform/implementation/linux/utils.cc b/internal/platform/implementation/linux/utils.cc index f526a5da..2dd8338e 100644 --- a/internal/platform/implementation/linux/utils.cc +++ b/internal/platform/implementation/linux/utils.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include +#include #include #include @@ -98,5 +100,21 @@ std::string RandWPAPassphrase() { return RandString(allowed_chars, 63); } + +std::string wstring_to_string(const std::wstring& str) { + if (str.empty()) { + return {}; + } + std::wstring_convert> converter; + return converter.to_bytes(str); +} + +std::wstring string_to_wstring(const std::string& str) { + if (str.empty()) { + return {}; + } + std::wstring_convert> converter; + return converter.from_bytes(str); +} } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/utils.h b/internal/platform/implementation/linux/utils.h index 3f251f4a..69989732 100644 --- a/internal/platform/implementation/linux/utils.h +++ b/internal/platform/implementation/linux/utils.h @@ -32,6 +32,9 @@ std::optional NewUuidStr(); std::string RandSSID(); std::string RandWPAPassphrase(); +std::string wstring_to_string(const std::wstring& str); +std::wstring string_to_wstring(const std::string& str); + } // namespace linux } // namespace nearby