OLD | NEW |
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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shellapi.h> | 9 #include <shellapi.h> |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
(...skipping 1994 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2005 &exact_buffer[0], | 2005 &exact_buffer[0], |
2006 static_cast<int>(exact_buffer.size()))); | 2006 static_cast<int>(exact_buffer.size()))); |
2007 } | 2007 } |
2008 | 2008 |
2009 TEST_F(FileUtilTest, ReadFileToString) { | 2009 TEST_F(FileUtilTest, ReadFileToString) { |
2010 const char kTestData[] = "0123"; | 2010 const char kTestData[] = "0123"; |
2011 std::string data; | 2011 std::string data; |
2012 | 2012 |
2013 FilePath file_path = | 2013 FilePath file_path = |
2014 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest")); | 2014 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest")); |
| 2015 FilePath file_path_dangerous = |
| 2016 temp_dir_.path().Append(FILE_PATH_LITERAL("..")). |
| 2017 Append(temp_dir_.path().BaseName()). |
| 2018 Append(FILE_PATH_LITERAL("ReadFileToStringTest")); |
2015 | 2019 |
| 2020 // Create test file. |
2016 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4)); | 2021 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4)); |
2017 | 2022 |
2018 EXPECT_TRUE(ReadFileToString(file_path, &data)); | 2023 EXPECT_TRUE(ReadFileToString(file_path, &data)); |
2019 EXPECT_EQ(kTestData, data); | 2024 EXPECT_EQ(kTestData, data); |
2020 | 2025 |
2021 data = "temp"; | 2026 data = "temp"; |
2022 EXPECT_FALSE(ReadFileToString(file_path, &data, 0)); | 2027 EXPECT_FALSE(ReadFileToString(file_path, &data, 0)); |
2023 EXPECT_EQ(data.length(), 0u); | 2028 EXPECT_EQ(0u, data.length()); |
2024 | 2029 |
2025 data = "temp"; | 2030 data = "temp"; |
2026 EXPECT_FALSE(ReadFileToString(file_path, &data, 2)); | 2031 EXPECT_FALSE(ReadFileToString(file_path, &data, 2)); |
2027 EXPECT_EQ("01", data); | 2032 EXPECT_EQ("01", data); |
2028 | 2033 |
2029 data.clear(); | 2034 data.clear(); |
2030 EXPECT_FALSE(ReadFileToString(file_path, &data, 3)); | 2035 EXPECT_FALSE(ReadFileToString(file_path, &data, 3)); |
2031 EXPECT_EQ("012", data); | 2036 EXPECT_EQ("012", data); |
2032 | 2037 |
2033 data.clear(); | 2038 data.clear(); |
2034 EXPECT_TRUE(ReadFileToString(file_path, &data, 4)); | 2039 EXPECT_TRUE(ReadFileToString(file_path, &data, 4)); |
2035 EXPECT_EQ("0123", data); | 2040 EXPECT_EQ("0123", data); |
2036 | 2041 |
2037 data.clear(); | 2042 data.clear(); |
2038 EXPECT_TRUE(ReadFileToString(file_path, &data, 6)); | 2043 EXPECT_TRUE(ReadFileToString(file_path, &data, 6)); |
2039 EXPECT_EQ("0123", data); | 2044 EXPECT_EQ("0123", data); |
2040 | 2045 |
2041 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6)); | 2046 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6)); |
2042 | 2047 |
2043 EXPECT_TRUE(ReadFileToString(file_path, NULL)); | 2048 EXPECT_TRUE(ReadFileToString(file_path, NULL)); |
2044 | 2049 |
| 2050 data = "temp"; |
| 2051 EXPECT_FALSE(ReadFileToString(file_path_dangerous, &data)); |
| 2052 EXPECT_EQ(0u, data.length()); |
| 2053 |
| 2054 // Delete test file. |
2045 EXPECT_TRUE(base::DeleteFile(file_path, false)); | 2055 EXPECT_TRUE(base::DeleteFile(file_path, false)); |
2046 | 2056 |
2047 data = "temp"; | 2057 data = "temp"; |
2048 EXPECT_FALSE(ReadFileToString(file_path, &data)); | 2058 EXPECT_FALSE(ReadFileToString(file_path, &data)); |
2049 EXPECT_EQ(data.length(), 0u); | 2059 EXPECT_EQ(0u, data.length()); |
2050 | 2060 |
2051 data = "temp"; | 2061 data = "temp"; |
2052 EXPECT_FALSE(ReadFileToString(file_path, &data, 6)); | 2062 EXPECT_FALSE(ReadFileToString(file_path, &data, 6)); |
2053 EXPECT_EQ(data.length(), 0u); | 2063 EXPECT_EQ(0u, data.length()); |
2054 } | 2064 } |
2055 | 2065 |
2056 TEST_F(FileUtilTest, TouchFile) { | 2066 TEST_F(FileUtilTest, TouchFile) { |
2057 FilePath data_dir = | 2067 FilePath data_dir = |
2058 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); | 2068 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); |
2059 | 2069 |
2060 // Create a fresh, empty copy of this directory. | 2070 // Create a fresh, empty copy of this directory. |
2061 if (PathExists(data_dir)) { | 2071 if (PathExists(data_dir)) { |
2062 ASSERT_TRUE(DeleteFile(data_dir, true)); | 2072 ASSERT_TRUE(DeleteFile(data_dir, true)); |
2063 } | 2073 } |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2552 // Trying to close it should crash. This is important for security. | 2562 // Trying to close it should crash. This is important for security. |
2553 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); | 2563 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); |
2554 #endif | 2564 #endif |
2555 } | 2565 } |
2556 | 2566 |
2557 #endif // defined(OS_POSIX) | 2567 #endif // defined(OS_POSIX) |
2558 | 2568 |
2559 } // namespace | 2569 } // namespace |
2560 | 2570 |
2561 } // namespace base | 2571 } // namespace base |
OLD | NEW |