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

Side by Side Diff: base/files/file_util_unittest.cc

Issue 2086103002: Add base::ExecutableExistsInPath (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rewrite unit test Created 4 years, 6 months 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <fstream> 9 #include <fstream>
10 #include <set> 10 #include <set>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/base_paths.h" 14 #include "base/base_paths.h"
15 #include "base/environment.h"
15 #include "base/files/file_enumerator.h" 16 #include "base/files/file_enumerator.h"
16 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
17 #include "base/files/file_util.h" 18 #include "base/files/file_util.h"
18 #include "base/files/scoped_file.h" 19 #include "base/files/scoped_file.h"
19 #include "base/files/scoped_temp_dir.h" 20 #include "base/files/scoped_temp_dir.h"
20 #include "base/macros.h" 21 #include "base/macros.h"
21 #include "base/path_service.h" 22 #include "base/path_service.h"
22 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
23 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
24 #include "base/test/test_file_util.h" 25 #include "base/test/test_file_util.h"
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES); 832 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
832 FindResultCollector c2(&f2); 833 FindResultCollector c2(&f2);
833 EXPECT_TRUE(c2.HasFile(file_name)); 834 EXPECT_TRUE(c2.HasFile(file_name));
834 EXPECT_EQ(1, c2.size()); 835 EXPECT_EQ(1, c2.size());
835 836
836 // Delete the file. 837 // Delete the file.
837 EXPECT_TRUE(DeleteFile(subdir_path, true)); 838 EXPECT_TRUE(DeleteFile(subdir_path, true));
838 EXPECT_FALSE(PathExists(subdir_path)); 839 EXPECT_FALSE(PathExists(subdir_path));
839 } 840 }
840 841
842 TEST_F(FileUtilTest, ExecutableExistsInPath) {
843 // Create two directories that we will put in our PATH
844 const char kPath[] = "PATH";
845 const FilePath::CharType kDir1[] = FPL("dir1");
846 const FilePath::CharType kDir2[] = FPL("dir2");
847
848 FilePath dir1 = temp_dir_.path().Append(kDir1);
849 FilePath dir2 = temp_dir_.path().Append(kDir2);
850 ASSERT_TRUE(CreateDirectory(dir1));
851 ASSERT_TRUE(CreateDirectory(dir2));
852
853 std::unique_ptr<Environment> env(base::Environment::Create());
854
855 ASSERT_TRUE(env->SetVar(kPath, dir1.value() + ":" + dir2.value()));
856
857 const FilePath::CharType kRegularFileName[] = FPL("regular_file");
858 const FilePath::CharType kExeFileName[] = FPL("exe");
859 const FilePath::CharType kDneFileName[] = FPL("does_not_exist");
860
861 const FilePath kExePath = dir1.Append(kExeFileName);
862 const FilePath kRegularFilePath = dir2.Append(kRegularFileName);
863
864 // Write file.
Lei Zhang 2016/06/23 19:17:06 Maybe just TouchFile() instead?
Tom (Use chromium acct) 2016/06/23 21:55:12 TouchFile checks if a file exists first https://cs
Lei Zhang 2016/06/23 22:21:46 I'm going to change my title to Offerer of Bad Adv
865 const std::string kData("hello");
866 ASSERT_EQ(static_cast<int>(kData.length()),
867 WriteFile(kExePath, kData.data(), kData.length()));
868 ASSERT_TRUE(PathExists(kExePath));
869 ASSERT_EQ(static_cast<int>(kData.length()),
870 WriteFile(kRegularFilePath, kData.data(), kData.length()));
871 ASSERT_TRUE(PathExists(kRegularFilePath));
872
873 ASSERT_TRUE(SetPosixFilePermissions(dir1.Append(kExeFileName),
874 FILE_PERMISSION_EXECUTE_BY_USER));
875
876 EXPECT_TRUE(ExecutableExistsInPath(env.get(), kExeFileName));
877 EXPECT_FALSE(ExecutableExistsInPath(env.get(), kRegularFileName));
878 EXPECT_FALSE(ExecutableExistsInPath(env.get(), kDneFileName));
879 }
880
841 #endif // defined(OS_POSIX) 881 #endif // defined(OS_POSIX)
842 882
843 #if defined(OS_WIN) 883 #if defined(OS_WIN)
844 // Tests that the Delete function works for wild cards, especially 884 // Tests that the Delete function works for wild cards, especially
845 // with the recursion flag. Also coincidentally tests PathExists. 885 // with the recursion flag. Also coincidentally tests PathExists.
846 // TODO(erikkay): see if anyone's actually using this feature of the API 886 // TODO(erikkay): see if anyone's actually using this feature of the API
847 TEST_F(FileUtilTest, DeleteWildCard) { 887 TEST_F(FileUtilTest, DeleteWildCard) {
848 // Create a file and a directory 888 // Create a file and a directory
849 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); 889 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
850 CreateTextFile(file_name, bogus_content); 890 CreateTextFile(file_name, bogus_content);
(...skipping 1727 matching lines...) Expand 10 before | Expand all | Expand 10 after
2578 // Trying to close it should crash. This is important for security. 2618 // Trying to close it should crash. This is important for security.
2579 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2619 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2580 #endif 2620 #endif
2581 } 2621 }
2582 2622
2583 #endif // defined(OS_POSIX) 2623 #endif // defined(OS_POSIX)
2584 2624
2585 } // namespace 2625 } // namespace
2586 2626
2587 } // namespace base 2627 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698