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> |
11 #include <tchar.h> | 11 #include <tchar.h> |
12 #include <winioctl.h> | 12 #include <winioctl.h> |
13 #endif | 13 #endif |
14 | 14 |
15 #if defined(OS_POSIX) | 15 #if defined(OS_POSIX) |
16 #include <errno.h> | 16 #include <errno.h> |
17 #include <fcntl.h> | 17 #include <fcntl.h> |
18 #include <unistd.h> | 18 #include <unistd.h> |
19 #endif | 19 #endif |
20 | 20 |
21 #include <algorithm> | 21 #include <algorithm> |
22 #include <fstream> | 22 #include <fstream> |
23 #include <set> | 23 #include <set> |
| 24 #include <vector> |
24 | 25 |
25 #include "base/base_paths.h" | 26 #include "base/base_paths.h" |
26 #include "base/file_util.h" | 27 #include "base/file_util.h" |
27 #include "base/files/file_enumerator.h" | 28 #include "base/files/file_enumerator.h" |
28 #include "base/files/file_path.h" | 29 #include "base/files/file_path.h" |
29 #include "base/files/scoped_file.h" | 30 #include "base/files/scoped_file.h" |
30 #include "base/files/scoped_temp_dir.h" | 31 #include "base/files/scoped_temp_dir.h" |
31 #include "base/path_service.h" | 32 #include "base/path_service.h" |
32 #include "base/strings/utf_string_conversions.h" | 33 #include "base/strings/utf_string_conversions.h" |
33 #include "base/test/test_file_util.h" | 34 #include "base/test/test_file_util.h" |
(...skipping 1919 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1953 EXPECT_EQ(-1, AppendToFile(foobar, data.c_str(), data.length())); | 1954 EXPECT_EQ(-1, AppendToFile(foobar, data.c_str(), data.length())); |
1954 EXPECT_EQ(static_cast<int>(data.length()), | 1955 EXPECT_EQ(static_cast<int>(data.length()), |
1955 WriteFile(foobar, data.c_str(), data.length())); | 1956 WriteFile(foobar, data.c_str(), data.length())); |
1956 EXPECT_EQ(static_cast<int>(data.length()), | 1957 EXPECT_EQ(static_cast<int>(data.length()), |
1957 AppendToFile(foobar, data.c_str(), data.length())); | 1958 AppendToFile(foobar, data.c_str(), data.length())); |
1958 | 1959 |
1959 const std::wstring read_content = ReadTextFile(foobar); | 1960 const std::wstring read_content = ReadTextFile(foobar); |
1960 EXPECT_EQ(L"hellohello", read_content); | 1961 EXPECT_EQ(L"hellohello", read_content); |
1961 } | 1962 } |
1962 | 1963 |
| 1964 TEST_F(FileUtilTest, ReadFile) { |
| 1965 // Create a test file to be read. |
| 1966 const std::string kTestData("The quick brown fox jumps over the lazy dog."); |
| 1967 FilePath file_path = |
| 1968 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileTest")); |
| 1969 |
| 1970 ASSERT_EQ(static_cast<int>(kTestData.size()), |
| 1971 WriteFile(file_path, kTestData.data(), kTestData.size())); |
| 1972 |
| 1973 // Make buffers with various size. |
| 1974 std::vector<char> small_buffer(kTestData.size() / 2); |
| 1975 std::vector<char> exact_buffer(kTestData.size()); |
| 1976 std::vector<char> large_buffer(kTestData.size() * 2); |
| 1977 |
| 1978 // Read the file with smaller buffer. |
| 1979 int bytes_read_small = ReadFile( |
| 1980 file_path, &small_buffer[0], static_cast<int>(small_buffer.size())); |
| 1981 EXPECT_EQ(bytes_read_small, static_cast<int>(small_buffer.size())); |
| 1982 EXPECT_EQ( |
| 1983 std::string(small_buffer.begin(), small_buffer.end()), |
| 1984 std::string(kTestData.begin(), kTestData.begin() + small_buffer.size())); |
| 1985 |
| 1986 // Read the file with buffer which have exactly same size. |
| 1987 int bytes_read_exact = ReadFile( |
| 1988 file_path, &exact_buffer[0], static_cast<int>(exact_buffer.size())); |
| 1989 EXPECT_EQ(bytes_read_exact, static_cast<int>(kTestData.size())); |
| 1990 EXPECT_EQ(std::string(exact_buffer.begin(), exact_buffer.end()), kTestData); |
| 1991 |
| 1992 // Read the file with larger buffer. |
| 1993 int bytes_read_large = ReadFile( |
| 1994 file_path, &large_buffer[0], static_cast<int>(large_buffer.size())); |
| 1995 EXPECT_EQ(bytes_read_large, static_cast<int>(kTestData.size())); |
| 1996 EXPECT_EQ(std::string(large_buffer.begin(), |
| 1997 large_buffer.begin() + kTestData.size()), |
| 1998 kTestData); |
| 1999 |
| 2000 // Make sure the return value is -1 if the file doesn't exist. |
| 2001 FilePath file_path_not_exist = |
| 2002 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileNotExistTest")); |
| 2003 EXPECT_EQ(-1, |
| 2004 ReadFile(file_path_not_exist, |
| 2005 &exact_buffer[0], |
| 2006 static_cast<int>(exact_buffer.size()))); |
| 2007 } |
| 2008 |
1963 TEST_F(FileUtilTest, ReadFileToString) { | 2009 TEST_F(FileUtilTest, ReadFileToString) { |
1964 const char kTestData[] = "0123"; | 2010 const char kTestData[] = "0123"; |
1965 std::string data; | 2011 std::string data; |
1966 | 2012 |
1967 FilePath file_path = | 2013 FilePath file_path = |
1968 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest")); | 2014 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest")); |
1969 | 2015 |
1970 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4)); | 2016 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4)); |
1971 | 2017 |
1972 EXPECT_TRUE(ReadFileToString(file_path, &data)); | 2018 EXPECT_TRUE(ReadFileToString(file_path, &data)); |
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2506 // Trying to close it should crash. This is important for security. | 2552 // Trying to close it should crash. This is important for security. |
2507 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); | 2553 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); |
2508 #endif | 2554 #endif |
2509 } | 2555 } |
2510 | 2556 |
2511 #endif // defined(OS_POSIX) | 2557 #endif // defined(OS_POSIX) |
2512 | 2558 |
2513 } // namespace | 2559 } // namespace |
2514 | 2560 |
2515 } // namespace base | 2561 } // namespace base |
OLD | NEW |