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

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

Issue 1549853002: Switch to standard integer types in base/files/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 4 years, 12 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
« no previous file with comments | « base/files/file_util_proxy.cc ('k') | base/files/file_util_win.cc » ('j') | 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 <stddef.h>
6 6 #include <stdint.h>
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #include <shellapi.h>
10 #include <shlobj.h>
11 #include <tchar.h>
12 #include <winioctl.h>
13 #endif
14
15 #if defined(OS_POSIX)
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #endif
20 7
21 #include <algorithm> 8 #include <algorithm>
22 #include <fstream> 9 #include <fstream>
23 #include <set> 10 #include <set>
24 #include <utility> 11 #include <utility>
25 #include <vector> 12 #include <vector>
26 13
27 #include "base/base_paths.h" 14 #include "base/base_paths.h"
28 #include "base/files/file_enumerator.h" 15 #include "base/files/file_enumerator.h"
29 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
30 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
31 #include "base/files/scoped_file.h" 18 #include "base/files/scoped_file.h"
32 #include "base/files/scoped_temp_dir.h" 19 #include "base/files/scoped_temp_dir.h"
20 #include "base/macros.h"
33 #include "base/path_service.h" 21 #include "base/path_service.h"
34 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
35 #include "base/strings/utf_string_conversions.h" 23 #include "base/strings/utf_string_conversions.h"
36 #include "base/test/test_file_util.h" 24 #include "base/test/test_file_util.h"
37 #include "base/threading/platform_thread.h" 25 #include "base/threading/platform_thread.h"
26 #include "build/build_config.h"
38 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
39 #include "testing/platform_test.h" 28 #include "testing/platform_test.h"
40 29
41 #if defined(OS_WIN) 30 #if defined(OS_WIN)
31 #include <windows.h>
32 #include <shellapi.h>
33 #include <shlobj.h>
34 #include <tchar.h>
35 #include <winioctl.h>
42 #include "base/win/scoped_handle.h" 36 #include "base/win/scoped_handle.h"
43 #include "base/win/windows_version.h" 37 #include "base/win/windows_version.h"
44 #endif 38 #endif
45 39
40 #if defined(OS_POSIX)
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #endif
45
46 #if defined(OS_ANDROID) 46 #if defined(OS_ANDROID)
47 #include "base/android/content_uri_utils.h" 47 #include "base/android/content_uri_utils.h"
48 #endif 48 #endif
49 49
50 // This macro helps avoid wrapped lines in the test structs. 50 // This macro helps avoid wrapped lines in the test structs.
51 #define FPL(x) FILE_PATH_LITERAL(x) 51 #define FPL(x) FILE_PATH_LITERAL(x)
52 52
53 namespace base { 53 namespace base {
54 54
55 namespace { 55 namespace {
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 file.getline(contents, arraysize(contents)); 243 file.getline(contents, arraysize(contents));
244 file.close(); 244 file.close();
245 return std::wstring(contents); 245 return std::wstring(contents);
246 } 246 }
247 247
248 TEST_F(FileUtilTest, FileAndDirectorySize) { 248 TEST_F(FileUtilTest, FileAndDirectorySize) {
249 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize 249 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
250 // should return 53 bytes. 250 // should return 53 bytes.
251 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt")); 251 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
252 CreateTextFile(file_01, L"12345678901234567890"); 252 CreateTextFile(file_01, L"12345678901234567890");
253 int64 size_f1 = 0; 253 int64_t size_f1 = 0;
254 ASSERT_TRUE(GetFileSize(file_01, &size_f1)); 254 ASSERT_TRUE(GetFileSize(file_01, &size_f1));
255 EXPECT_EQ(20ll, size_f1); 255 EXPECT_EQ(20ll, size_f1);
256 256
257 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2")); 257 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
258 CreateDirectory(subdir_path); 258 CreateDirectory(subdir_path);
259 259
260 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt")); 260 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
261 CreateTextFile(file_02, L"123456789012345678901234567890"); 261 CreateTextFile(file_02, L"123456789012345678901234567890");
262 int64 size_f2 = 0; 262 int64_t size_f2 = 0;
263 ASSERT_TRUE(GetFileSize(file_02, &size_f2)); 263 ASSERT_TRUE(GetFileSize(file_02, &size_f2));
264 EXPECT_EQ(30ll, size_f2); 264 EXPECT_EQ(30ll, size_f2);
265 265
266 FilePath subsubdir_path = subdir_path.Append(FPL("Level3")); 266 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
267 CreateDirectory(subsubdir_path); 267 CreateDirectory(subsubdir_path);
268 268
269 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt")); 269 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
270 CreateTextFile(file_03, L"123"); 270 CreateTextFile(file_03, L"123");
271 271
272 int64 computed_size = ComputeDirectorySize(temp_dir_.path()); 272 int64_t computed_size = ComputeDirectorySize(temp_dir_.path());
273 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size); 273 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
274 } 274 }
275 275
276 TEST_F(FileUtilTest, NormalizeFilePathBasic) { 276 TEST_F(FileUtilTest, NormalizeFilePathBasic) {
277 // Create a directory under the test dir. Because we create it, 277 // Create a directory under the test dir. Because we create it,
278 // we know it is not a link. 278 // we know it is not a link.
279 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a")); 279 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
280 FilePath dir_path = temp_dir_.path().Append(FPL("dir")); 280 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
281 FilePath file_b_path = dir_path.Append(FPL("file_b")); 281 FilePath file_b_path = dir_path.Append(FPL("file_b"));
282 CreateDirectory(dir_path); 282 CreateDirectory(dir_path);
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 715
716 int buffer_size = kData.length(); 716 int buffer_size = kData.length();
717 char* buffer = new char[buffer_size]; 717 char* buffer = new char[buffer_size];
718 718
719 // Write file. 719 // Write file.
720 EXPECT_EQ(static_cast<int>(kData.length()), 720 EXPECT_EQ(static_cast<int>(kData.length()),
721 WriteFile(file_name, kData.data(), kData.length())); 721 WriteFile(file_name, kData.data(), kData.length()));
722 EXPECT_TRUE(PathExists(file_name)); 722 EXPECT_TRUE(PathExists(file_name));
723 723
724 // Make sure the file is readable. 724 // Make sure the file is readable.
725 int32 mode = 0; 725 int32_t mode = 0;
726 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); 726 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
727 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER); 727 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
728 728
729 // Get rid of the read permission. 729 // Get rid of the read permission.
730 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u)); 730 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
731 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode)); 731 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
732 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER); 732 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
733 // Make sure the file can't be read. 733 // Make sure the file can't be read.
734 EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size)); 734 EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size));
735 735
(...skipping 1705 matching lines...) Expand 10 before | Expand all | Expand 10 after
2441 } 2441 }
2442 2442
2443 #if defined(OS_ANDROID) 2443 #if defined(OS_ANDROID)
2444 TEST_F(FileUtilTest, ValidContentUriTest) { 2444 TEST_F(FileUtilTest, ValidContentUriTest) {
2445 // Get the test image path. 2445 // Get the test image path.
2446 FilePath data_dir; 2446 FilePath data_dir;
2447 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir)); 2447 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
2448 data_dir = data_dir.AppendASCII("file_util"); 2448 data_dir = data_dir.AppendASCII("file_util");
2449 ASSERT_TRUE(PathExists(data_dir)); 2449 ASSERT_TRUE(PathExists(data_dir));
2450 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png")); 2450 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2451 int64 image_size; 2451 int64_t image_size;
2452 GetFileSize(image_file, &image_size); 2452 GetFileSize(image_file, &image_size);
2453 EXPECT_LT(0, image_size); 2453 EXPECT_LT(0, image_size);
2454 2454
2455 // Insert the image into MediaStore. MediaStore will do some conversions, and 2455 // Insert the image into MediaStore. MediaStore will do some conversions, and
2456 // return the content URI. 2456 // return the content URI.
2457 FilePath path = InsertImageIntoMediaStore(image_file); 2457 FilePath path = InsertImageIntoMediaStore(image_file);
2458 EXPECT_TRUE(path.IsContentUri()); 2458 EXPECT_TRUE(path.IsContentUri());
2459 EXPECT_TRUE(PathExists(path)); 2459 EXPECT_TRUE(PathExists(path));
2460 // The file size may not equal to the input image as MediaStore may convert 2460 // The file size may not equal to the input image as MediaStore may convert
2461 // the image. 2461 // the image.
2462 int64 content_uri_size; 2462 int64_t content_uri_size;
2463 GetFileSize(path, &content_uri_size); 2463 GetFileSize(path, &content_uri_size);
2464 EXPECT_EQ(image_size, content_uri_size); 2464 EXPECT_EQ(image_size, content_uri_size);
2465 2465
2466 // We should be able to read the file. 2466 // We should be able to read the file.
2467 char* buffer = new char[image_size]; 2467 char* buffer = new char[image_size];
2468 File file = OpenContentUriForRead(path); 2468 File file = OpenContentUriForRead(path);
2469 EXPECT_TRUE(file.IsValid()); 2469 EXPECT_TRUE(file.IsValid());
2470 EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size)); 2470 EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size));
2471 delete[] buffer; 2471 delete[] buffer;
2472 } 2472 }
2473 2473
2474 TEST_F(FileUtilTest, NonExistentContentUriTest) { 2474 TEST_F(FileUtilTest, NonExistentContentUriTest) {
2475 FilePath path("content://foo.bar"); 2475 FilePath path("content://foo.bar");
2476 EXPECT_TRUE(path.IsContentUri()); 2476 EXPECT_TRUE(path.IsContentUri());
2477 EXPECT_FALSE(PathExists(path)); 2477 EXPECT_FALSE(PathExists(path));
2478 // Size should be smaller than 0. 2478 // Size should be smaller than 0.
2479 int64 size; 2479 int64_t size;
2480 EXPECT_FALSE(GetFileSize(path, &size)); 2480 EXPECT_FALSE(GetFileSize(path, &size));
2481 2481
2482 // We should not be able to read the file. 2482 // We should not be able to read the file.
2483 File file = OpenContentUriForRead(path); 2483 File file = OpenContentUriForRead(path);
2484 EXPECT_FALSE(file.IsValid()); 2484 EXPECT_FALSE(file.IsValid());
2485 } 2485 }
2486 #endif 2486 #endif
2487 2487
2488 TEST(ScopedFD, ScopedFDDoesClose) { 2488 TEST(ScopedFD, ScopedFDDoesClose) {
2489 int fds[2]; 2489 int fds[2];
(...skipping 30 matching lines...) Expand all
2520 // Trying to close it should crash. This is important for security. 2520 // Trying to close it should crash. This is important for security.
2521 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2521 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2522 #endif 2522 #endif
2523 } 2523 }
2524 2524
2525 #endif // defined(OS_POSIX) 2525 #endif // defined(OS_POSIX)
2526 2526
2527 } // namespace 2527 } // namespace
2528 2528
2529 } // namespace base 2529 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util_proxy.cc ('k') | base/files/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698