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

Side by Side Diff: base/file_util_unittest.cc

Issue 157593005: Added new ReadFileToString API with a max_size argument (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added new ReadFileToString API with a max_size argument Created 6 years, 10 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
« base/file_util.h ('K') | « base/file_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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 1906 matching lines...) Expand 10 before | Expand all | Expand 10 after
1917 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length())); 1917 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length()));
1918 EXPECT_EQ(static_cast<int>(data.length()), 1918 EXPECT_EQ(static_cast<int>(data.length()),
1919 file_util::WriteFile(foobar, data.c_str(), data.length())); 1919 file_util::WriteFile(foobar, data.c_str(), data.length()));
1920 EXPECT_EQ(static_cast<int>(data.length()), 1920 EXPECT_EQ(static_cast<int>(data.length()),
1921 file_util::AppendToFile(foobar, data.c_str(), data.length())); 1921 file_util::AppendToFile(foobar, data.c_str(), data.length()));
1922 1922
1923 const std::wstring read_content = ReadTextFile(foobar); 1923 const std::wstring read_content = ReadTextFile(foobar);
1924 EXPECT_EQ(L"hellohello", read_content); 1924 EXPECT_EQ(L"hellohello", read_content);
1925 } 1925 }
1926 1926
1927 TEST_F(FileUtilTest, ReadFileToString) {
1928 const char kTestData[] = "0123";
1929 std::string data;
1930
1931 FilePath file_path =
1932 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
1933
1934 ASSERT_EQ(4, file_util::WriteFile(file_path, kTestData, 4));
1935
1936 EXPECT_TRUE(ReadFileToString(file_path, &data));
1937 EXPECT_EQ(kTestData, data);
1938
1939 data = "temp";
1940 EXPECT_FALSE(ReadFileToString(file_path, &data, 0));
1941 EXPECT_EQ(data.length(), 0u);
1942
1943 data = "temp";
1944 EXPECT_FALSE(ReadFileToString(file_path, &data, 2));
1945 EXPECT_EQ("01", data);
1946
1947 data.clear();
1948 EXPECT_FALSE(ReadFileToString(file_path, &data, 3));
1949 EXPECT_EQ("012", data);
1950
1951 data.clear();
1952 EXPECT_TRUE(ReadFileToString(file_path, &data, 4));
1953 EXPECT_EQ("0123", data);
1954
1955 data.clear();
1956 EXPECT_TRUE(ReadFileToString(file_path, &data, 6));
1957 EXPECT_EQ("0123", data);
1958
1959 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6));
1960
1961 EXPECT_TRUE(ReadFileToString(file_path, NULL));
1962
1963 EXPECT_TRUE(base::DeleteFile(file_path, false));
1964
1965 EXPECT_FALSE(ReadFileToString(file_path, &data));
bartfab (slow) 2014/02/18 14:56:51 Nit: You could add a data = "temp" statement befor
1966 EXPECT_EQ(data.length(), 0u);
1967
1968 EXPECT_FALSE(ReadFileToString(file_path, &data, 6));
bartfab (slow) 2014/02/18 14:56:51 Nit: You could add a data = "temp" statement befor
1969 EXPECT_EQ(data.length(), 0u);
1970 }
1971
1927 TEST_F(FileUtilTest, TouchFile) { 1972 TEST_F(FileUtilTest, TouchFile) {
1928 FilePath data_dir = 1973 FilePath data_dir =
1929 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); 1974 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1930 1975
1931 // Create a fresh, empty copy of this directory. 1976 // Create a fresh, empty copy of this directory.
1932 if (PathExists(data_dir)) { 1977 if (PathExists(data_dir)) {
1933 ASSERT_TRUE(DeleteFile(data_dir, true)); 1978 ASSERT_TRUE(DeleteFile(data_dir, true));
1934 } 1979 }
1935 ASSERT_TRUE(CreateDirectory(data_dir)); 1980 ASSERT_TRUE(CreateDirectory(data_dir));
1936 1981
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after
2386 int fd = OpenContentUriForRead(path); 2431 int fd = OpenContentUriForRead(path);
2387 EXPECT_EQ(-1, fd); 2432 EXPECT_EQ(-1, fd);
2388 } 2433 }
2389 #endif 2434 #endif
2390 2435
2391 #endif // defined(OS_POSIX) 2436 #endif // defined(OS_POSIX)
2392 2437
2393 } // namespace 2438 } // namespace
2394 2439
2395 } // namespace base 2440 } // namespace base
OLDNEW
« base/file_util.h ('K') | « base/file_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698