Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2078)

Unified Diff: base/file_util_unittest.cc

Issue 11300011: Don't expand path to use long names in CreateTemporaryFileInDir. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments and add a unit test Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/file_util_win.cc » ('j') | base/file_util_win.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/file_util_unittest.cc
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 04de367ec0f6433b5f80b655819a21013b992e59..8bee6a8066b1d126a6d57e7798dca9e9409e041b 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -21,6 +21,7 @@
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
+#include "base/test/test_file_util.h"
#include "base/threading/platform_thread.h"
#include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -626,6 +627,56 @@ TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
EXPECT_EQ(0, info.size);
}
+TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
+ // Test that CreateTemporaryFileInDir() creates a path and returns a long path
+ // if it is available. This test requires that:
+ // - the filesystem at |temp_dir_| supports long filenames.
+ // - the account has FILE_LIST_DIRECTORY permission for all ancestor
+ // directories of |temp_dir_|.
+ const FilePath::CharType kLongDirName[] = FPL("A long path");
+ const FilePath::CharType kTestSubDirName[] = FPL("test");
+ FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
+ ASSERT_TRUE(file_util::CreateDirectory(long_test_dir));
+
+ // kLongDirName is not a 8.3 component. So GetShortName() should give us a
+ // different short name.
+ WCHAR path_buffer[MAX_PATH];
+ DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
+ path_buffer, MAX_PATH);
+ ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
+ ASSERT_NE(DWORD(0), path_buffer_length);
+ FilePath short_test_dir(path_buffer);
+ ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
+
+ FilePath temp_file;
+ ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir, &temp_file));
+ EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
+ EXPECT_TRUE(file_util::PathExists(temp_file));
+
+ // Create a subdirectory of |long_test_dir| and make |long_test_dir|
+ // unreadable. We should still be able to create a temp file in the
+ // subdirectory, but we won't be able to determine the long path for it. This
+ // mimics the environment that some users run where their user profiles reside
+ // in a location where the don't have full access to the higher level
+ // directories. (Note that this assumption is true for NTFS, but not for some
+ // network file systems. E.g. AFS).
+ FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
+ ASSERT_TRUE(file_util::CreateDirectory(access_test_dir));
+ file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
+ ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
+
+ // Use the short form of the directory to create a temporary filename.
+ ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
+ short_test_dir.Append(kTestSubDirName), &temp_file));
+ EXPECT_TRUE(file_util::PathExists(temp_file));
+ EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
+
+ // Check that the long path can't be determined for |temp_file|.
+ path_buffer_length = GetLongPathName(temp_file.value().c_str(),
+ path_buffer, MAX_PATH);
+ EXPECT_EQ(DWORD(0), path_buffer_length);
+}
+
#endif // defined(OS_WIN)
#if defined(OS_POSIX)
« no previous file with comments | « no previous file | base/file_util_win.cc » ('j') | base/file_util_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698