-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Make paths absolute before weakly_canonical and add regression tests #41290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ggarzia-MSFT
wants to merge
6
commits into
master
Choose a base branch
from
user/ggarzia/iidfile-cleanup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f01b89
Make paths absolute before weakly_canonical and add regression tests
ggarzia-MSFT 5c965aa
Add shared GetCanonicalPath helpers and use them for path canonicaliz…
ggarzia-MSFT 957be6c
Merge remote-tracking branch 'origin/master' into user/ggarzia/iidfil…
ggarzia-MSFT d1a9e26
Include filesystem.hpp directly where GetCanonicalPath is used
ggarzia-MSFT 83fa7ab
Simplify the filesystem unit tests so they no longer create files or …
ggarzia-MSFT 7621953
Use a path Win32 always rejects instead of an empty one in the filesy…
ggarzia-MSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /*++ | ||
|
|
||
| Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| Module Name: | ||
|
|
||
| FilesystemUnitTests.cpp | ||
|
|
||
| Abstract: | ||
|
|
||
| This file contains unit tests for the helpers in src/windows/common/filesystem.cpp. | ||
| These tests only read from the local filesystem so they do not require an installed distribution. | ||
|
|
||
| --*/ | ||
|
|
||
| #include "precomp.h" | ||
| #include "Common.h" | ||
|
|
||
| using wsl::windows::common::filesystem::GetCanonicalPath; | ||
|
|
||
| namespace { | ||
|
|
||
| // Returns a file name that does not exist in the given directory. | ||
| std::wstring UniqueMissingName(const std::filesystem::path& Directory) | ||
| { | ||
| static int counter = 0; | ||
| const auto name = std::format(L"wsl_ut_canonical_{}_{}.txt", GetCurrentProcessId(), ++counter); | ||
| VERIFY_IS_FALSE(std::filesystem::exists(Directory / name)); | ||
|
|
||
| return name; | ||
| } | ||
|
|
||
| // The canonical form of the current directory, which is what a relative path is expected to resolve | ||
| // against. std::filesystem::canonical is used rather than weakly_canonical so the expected value is | ||
| // computed independently of the API under test. | ||
| std::filesystem::path CanonicalCurrentDirectory() | ||
| { | ||
| return std::filesystem::canonical(std::filesystem::current_path()); | ||
| } | ||
|
|
||
| // A path that std::filesystem::absolute is guaranteed to reject, because it exceeds the longest path | ||
| // Win32 can express. An empty path is not used: whether absolute rejects one is implementation | ||
| // defined, and some standard library versions accept it. | ||
| std::filesystem::path UnresolvablePath() | ||
| { | ||
| return {L"C:\\" + std::wstring(40000, L'a')}; | ||
| } | ||
|
|
||
| // A file that is known to exist, used to cover paths that resolve to a real filesystem entry. The | ||
| // test module itself is used so that no file has to be created. | ||
| std::filesystem::path ExistingFile() | ||
| { | ||
| return {wil::GetModuleFileNameW<std::wstring>(wil::GetModuleInstanceHandle())}; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace FilesystemUnitTests { | ||
| class FilesystemUnitTests | ||
| { | ||
| WSL_TEST_CLASS(FilesystemUnitTests) | ||
|
|
||
| // A relative path naming a file that does not exist must still resolve to an absolute path. | ||
| // std::filesystem::weakly_canonical cannot do this on its own: it builds its result from the | ||
| // longest leading sequence of elements that exist, so a bare missing file name has nothing to | ||
| // canonicalize and is returned unchanged. | ||
| TEST_METHOD(GetCanonicalPath_RelativeMissingPathIsMadeAbsolute) | ||
| { | ||
| const auto name = UniqueMissingName(std::filesystem::current_path()); | ||
| VERIFY_IS_FALSE(std::filesystem::weakly_canonical(name).is_absolute()); | ||
|
|
||
| const auto result = GetCanonicalPath(name); | ||
|
|
||
| VERIFY_IS_TRUE(result.is_absolute()); | ||
| VERIFY_ARE_EQUAL((CanonicalCurrentDirectory() / name).wstring(), result.wstring()); | ||
| } | ||
|
|
||
| // The same resolution must happen for a relative path whose target already exists. | ||
| TEST_METHOD(GetCanonicalPath_RelativeExistingPathIsMadeAbsolute) | ||
| { | ||
| const auto existing = ExistingFile(); | ||
| const auto relativePath = std::filesystem::relative(existing, std::filesystem::current_path()); | ||
| VERIFY_IS_FALSE(relativePath.empty()); | ||
| VERIFY_IS_FALSE(relativePath.is_absolute()); | ||
|
|
||
| const auto result = GetCanonicalPath(relativePath); | ||
|
|
||
| VERIFY_IS_TRUE(result.is_absolute()); | ||
| VERIFY_ARE_EQUAL(std::filesystem::canonical(existing).wstring(), result.wstring()); | ||
| } | ||
|
|
||
| // '.' and '..' components must be collapsed even when the intermediate directory does not exist. | ||
| TEST_METHOD(GetCanonicalPath_CollapsesDotSegments) | ||
| { | ||
| const auto name = UniqueMissingName(std::filesystem::current_path()); | ||
|
|
||
| const auto result = GetCanonicalPath(L".\\nonexistent\\..\\" + name); | ||
|
|
||
| VERIFY_ARE_EQUAL((CanonicalCurrentDirectory() / name).wstring(), result.wstring()); | ||
| } | ||
|
|
||
| // An already absolute path must be returned unchanged. | ||
| TEST_METHOD(GetCanonicalPath_AbsolutePathIsUnchanged) | ||
| { | ||
| const auto expected = CanonicalCurrentDirectory() / UniqueMissingName(std::filesystem::current_path()); | ||
|
|
||
| VERIFY_ARE_EQUAL(expected.wstring(), GetCanonicalPath(expected).wstring()); | ||
| } | ||
|
|
||
| // A failure from std::filesystem::absolute must be reported. absolute returns an empty path when | ||
| // it fails, and weakly_canonical succeeds on an empty path and clears the error_code, so calling | ||
| // the two in sequence without checking in between silently turns the failure into success. | ||
| TEST_METHOD(GetCanonicalPath_ErrorOverloadReportsFailure) | ||
| { | ||
| std::error_code error; | ||
| const auto result = GetCanonicalPath(UnresolvablePath(), error); | ||
|
|
||
| VERIFY_ARE_NOT_EQUAL(std::error_code{}, error); | ||
| VERIFY_IS_TRUE(result.empty()); | ||
| } | ||
|
|
||
| // Error must be cleared when the call succeeds so callers can reuse the same variable. | ||
| TEST_METHOD(GetCanonicalPath_ErrorOverloadClearsErrorOnSuccess) | ||
| { | ||
| const auto expected = CanonicalCurrentDirectory() / UniqueMissingName(std::filesystem::current_path()); | ||
|
|
||
| auto error = std::make_error_code(std::errc::permission_denied); | ||
| const auto result = GetCanonicalPath(expected, error); | ||
|
|
||
| VERIFY_ARE_EQUAL(std::error_code{}, error); | ||
| VERIFY_ARE_EQUAL(expected.wstring(), result.wstring()); | ||
| } | ||
|
|
||
| // The throwing overload must surface the same failure the non-throwing overload reports. | ||
| TEST_METHOD(GetCanonicalPath_ThrowingOverloadSurfacesFailure) | ||
| { | ||
| std::error_code error; | ||
| (void)GetCanonicalPath(UnresolvablePath(), error); | ||
| VERIFY_ARE_NOT_EQUAL(std::error_code{}, error); | ||
|
|
||
| const auto expectedResult = HRESULT_FROM_WIN32(error.value()); | ||
| VERIFY_THROWS_SPECIFIC(GetCanonicalPath(UnresolvablePath()), wil::ResultException, [&](const wil::ResultException& e) { | ||
| return e.GetErrorCode() == expectedResult; | ||
| }); | ||
| } | ||
| }; | ||
| } // namespace FilesystemUnitTests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.