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

Side by Side Diff: base/file_util_unittest.cc

Issue 102873002: Move GetFileSize, NormalizeFilePath to base namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years 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 | Annotate | Revision Log
« no previous file with comments | « base/file_util_posix.cc ('k') | base/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 "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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 return u.QuadPart; 243 return u.QuadPart;
244 } 244 }
245 #endif 245 #endif
246 246
247 TEST_F(FileUtilTest, FileAndDirectorySize) { 247 TEST_F(FileUtilTest, FileAndDirectorySize) {
248 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize 248 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
249 // should return 53 bytes. 249 // should return 53 bytes.
250 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt")); 250 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
251 CreateTextFile(file_01, L"12345678901234567890"); 251 CreateTextFile(file_01, L"12345678901234567890");
252 int64 size_f1 = 0; 252 int64 size_f1 = 0;
253 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1)); 253 ASSERT_TRUE(GetFileSize(file_01, &size_f1));
254 EXPECT_EQ(20ll, size_f1); 254 EXPECT_EQ(20ll, size_f1);
255 255
256 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2")); 256 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
257 base::CreateDirectory(subdir_path); 257 base::CreateDirectory(subdir_path);
258 258
259 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt")); 259 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
260 CreateTextFile(file_02, L"123456789012345678901234567890"); 260 CreateTextFile(file_02, L"123456789012345678901234567890");
261 int64 size_f2 = 0; 261 int64 size_f2 = 0;
262 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2)); 262 ASSERT_TRUE(GetFileSize(file_02, &size_f2));
263 EXPECT_EQ(30ll, size_f2); 263 EXPECT_EQ(30ll, size_f2);
264 264
265 FilePath subsubdir_path = subdir_path.Append(FPL("Level3")); 265 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
266 base::CreateDirectory(subsubdir_path); 266 base::CreateDirectory(subsubdir_path);
267 267
268 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt")); 268 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
269 CreateTextFile(file_03, L"123"); 269 CreateTextFile(file_03, L"123");
270 270
271 int64 computed_size = ComputeDirectorySize(temp_dir_.path()); 271 int64 computed_size = ComputeDirectorySize(temp_dir_.path());
272 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size); 272 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
273 } 273 }
274 274
275 TEST_F(FileUtilTest, NormalizeFilePathBasic) { 275 TEST_F(FileUtilTest, NormalizeFilePathBasic) {
276 // Create a directory under the test dir. Because we create it, 276 // Create a directory under the test dir. Because we create it,
277 // we know it is not a link. 277 // we know it is not a link.
278 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a")); 278 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
279 FilePath dir_path = temp_dir_.path().Append(FPL("dir")); 279 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
280 FilePath file_b_path = dir_path.Append(FPL("file_b")); 280 FilePath file_b_path = dir_path.Append(FPL("file_b"));
281 base::CreateDirectory(dir_path); 281 base::CreateDirectory(dir_path);
282 282
283 FilePath normalized_file_a_path, normalized_file_b_path; 283 FilePath normalized_file_a_path, normalized_file_b_path;
284 ASSERT_FALSE(PathExists(file_a_path)); 284 ASSERT_FALSE(PathExists(file_a_path));
285 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path, 285 ASSERT_FALSE(NormalizeFilePath(file_a_path, &normalized_file_a_path))
286 &normalized_file_a_path))
287 << "NormalizeFilePath() should fail on nonexistent paths."; 286 << "NormalizeFilePath() should fail on nonexistent paths.";
288 287
289 CreateTextFile(file_a_path, bogus_content); 288 CreateTextFile(file_a_path, bogus_content);
290 ASSERT_TRUE(PathExists(file_a_path)); 289 ASSERT_TRUE(PathExists(file_a_path));
291 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path, 290 ASSERT_TRUE(NormalizeFilePath(file_a_path, &normalized_file_a_path));
292 &normalized_file_a_path));
293 291
294 CreateTextFile(file_b_path, bogus_content); 292 CreateTextFile(file_b_path, bogus_content);
295 ASSERT_TRUE(PathExists(file_b_path)); 293 ASSERT_TRUE(PathExists(file_b_path));
296 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path, 294 ASSERT_TRUE(NormalizeFilePath(file_b_path, &normalized_file_b_path));
297 &normalized_file_b_path));
298 295
299 // Beacuse this test created |dir_path|, we know it is not a link 296 // Beacuse this test created |dir_path|, we know it is not a link
300 // or junction. So, the real path of the directory holding file a 297 // or junction. So, the real path of the directory holding file a
301 // must be the parent of the path holding file b. 298 // must be the parent of the path holding file b.
302 ASSERT_TRUE(normalized_file_a_path.DirName() 299 ASSERT_TRUE(normalized_file_a_path.DirName()
303 .IsParent(normalized_file_b_path.DirName())); 300 .IsParent(normalized_file_b_path.DirName()));
304 } 301 }
305 302
306 #if defined(OS_WIN) 303 #if defined(OS_WIN)
307 304
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 ASSERT_TRUE(base::CreateDirectory(to_base_b)); 366 ASSERT_TRUE(base::CreateDirectory(to_base_b));
370 ReparsePoint reparse_to_base_b(to_base_b, base_b); 367 ReparsePoint reparse_to_base_b(to_base_b, base_b);
371 ASSERT_TRUE(reparse_to_base_b.IsValid()); 368 ASSERT_TRUE(reparse_to_base_b.IsValid());
372 369
373 FilePath to_sub_long = base_b.Append(FPL("to_sub_long")); 370 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
374 ASSERT_TRUE(base::CreateDirectory(to_sub_long)); 371 ASSERT_TRUE(base::CreateDirectory(to_sub_long));
375 ReparsePoint reparse_to_sub_long(to_sub_long, sub_long); 372 ReparsePoint reparse_to_sub_long(to_sub_long, sub_long);
376 ASSERT_TRUE(reparse_to_sub_long.IsValid()); 373 ASSERT_TRUE(reparse_to_sub_long.IsValid());
377 374
378 // Normalize a junction free path: base_a\sub_a\file.txt . 375 // Normalize a junction free path: base_a\sub_a\file.txt .
379 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path)); 376 ASSERT_TRUE(NormalizeFilePath(file_txt, &normalized_path));
380 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str()); 377 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
381 378
382 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude 379 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
383 // the junction to_sub_a. 380 // the junction to_sub_a.
384 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")), 381 ASSERT_TRUE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
385 &normalized_path)); 382 &normalized_path));
386 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str()); 383 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
387 384
388 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be 385 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
389 // normalized to exclude junctions to_base_b and to_sub_a . 386 // normalized to exclude junctions to_base_b and to_sub_a .
390 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b")) 387 ASSERT_TRUE(NormalizeFilePath(base_b.Append(FPL("to_base_b"))
391 .Append(FPL("to_base_b")) 388 .Append(FPL("to_base_b"))
392 .Append(FPL("to_sub_a")) 389 .Append(FPL("to_sub_a"))
393 .Append(FPL("file.txt")), 390 .Append(FPL("file.txt")),
394 &normalized_path)); 391 &normalized_path));
395 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str()); 392 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
396 393
397 // A long enough path will cause NormalizeFilePath() to fail. Make a long 394 // A long enough path will cause NormalizeFilePath() to fail. Make a long
398 // path using to_base_b many times, and check that paths long enough to fail 395 // path using to_base_b many times, and check that paths long enough to fail
399 // do not cause a crash. 396 // do not cause a crash.
400 FilePath long_path = base_b; 397 FilePath long_path = base_b;
401 const int kLengthLimit = MAX_PATH + 200; 398 const int kLengthLimit = MAX_PATH + 200;
402 while (long_path.value().length() <= kLengthLimit) { 399 while (long_path.value().length() <= kLengthLimit) {
403 long_path = long_path.Append(FPL("to_base_b")); 400 long_path = long_path.Append(FPL("to_base_b"));
404 } 401 }
405 long_path = long_path.Append(FPL("to_sub_a")) 402 long_path = long_path.Append(FPL("to_sub_a"))
406 .Append(FPL("file.txt")); 403 .Append(FPL("file.txt"));
407 404
408 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path)); 405 ASSERT_FALSE(NormalizeFilePath(long_path, &normalized_path));
409 406
410 // Normalizing the junction to deep.txt should fail, because the expanded 407 // Normalizing the junction to deep.txt should fail, because the expanded
411 // path to deep.txt is longer than MAX_PATH. 408 // path to deep.txt is longer than MAX_PATH.
412 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt), 409 ASSERT_FALSE(NormalizeFilePath(to_sub_long.Append(deep_txt),
413 &normalized_path)); 410 &normalized_path));
414 411
415 // Delete the reparse points, and see that NormalizeFilePath() fails 412 // Delete the reparse points, and see that NormalizeFilePath() fails
416 // to traverse them. 413 // to traverse them.
417 } 414 }
418 415
419 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")), 416 ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
420 &normalized_path)); 417 &normalized_path));
421 } 418 }
422 419
423 TEST_F(FileUtilTest, DevicePathToDriveLetter) { 420 TEST_F(FileUtilTest, DevicePathToDriveLetter) {
424 // Get a drive letter. 421 // Get a drive letter.
425 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2); 422 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
426 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) { 423 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
427 LOG(ERROR) << "Can't get a drive letter to test with."; 424 LOG(ERROR) << "Can't get a drive letter to test with.";
428 return; 425 return;
429 } 426 }
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 // Link one file to another. 600 // Link one file to another.
604 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); 601 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
605 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); 602 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
606 CreateTextFile(link_to, bogus_content); 603 CreateTextFile(link_to, bogus_content);
607 604
608 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from)) 605 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
609 << "Failed to create file symlink."; 606 << "Failed to create file symlink.";
610 607
611 // Check that NormalizeFilePath sees the link. 608 // Check that NormalizeFilePath sees the link.
612 FilePath normalized_path; 609 FilePath normalized_path;
613 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path)); 610 ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
614 EXPECT_NE(link_from, link_to); 611 EXPECT_NE(link_from, link_to);
615 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); 612 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
616 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); 613 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
617 614
618 // Link to a directory. 615 // Link to a directory.
619 link_from = temp_dir_.path().Append(FPL("from_dir")); 616 link_from = temp_dir_.path().Append(FPL("from_dir"));
620 link_to = temp_dir_.path().Append(FPL("to_dir")); 617 link_to = temp_dir_.path().Append(FPL("to_dir"));
621 ASSERT_TRUE(base::CreateDirectory(link_to)); 618 ASSERT_TRUE(base::CreateDirectory(link_to));
622 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from)) 619 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
623 << "Failed to create directory symlink."; 620 << "Failed to create directory symlink.";
624 621
625 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)) 622 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
626 << "Links to directories should return false."; 623 << "Links to directories should return false.";
627 624
628 // Test that a loop in the links causes NormalizeFilePath() to return false. 625 // Test that a loop in the links causes NormalizeFilePath() to return false.
629 link_from = temp_dir_.path().Append(FPL("link_a")); 626 link_from = temp_dir_.path().Append(FPL("link_a"));
630 link_to = temp_dir_.path().Append(FPL("link_b")); 627 link_to = temp_dir_.path().Append(FPL("link_b"));
631 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from)) 628 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
632 << "Failed to create loop symlink a."; 629 << "Failed to create loop symlink a.";
633 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to)) 630 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
634 << "Failed to create loop symlink b."; 631 << "Failed to create loop symlink b.";
635 632
636 // Infinite loop! 633 // Infinite loop!
637 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)); 634 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
638 } 635 }
639 #endif // defined(OS_POSIX) 636 #endif // defined(OS_POSIX)
640 637
641 TEST_F(FileUtilTest, DeleteNonExistent) { 638 TEST_F(FileUtilTest, DeleteNonExistent) {
642 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar"); 639 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
643 ASSERT_FALSE(PathExists(non_existent)); 640 ASSERT_FALSE(PathExists(non_existent));
644 641
645 EXPECT_TRUE(DeleteFile(non_existent, false)); 642 EXPECT_TRUE(DeleteFile(non_existent, false));
646 ASSERT_FALSE(PathExists(non_existent)); 643 ASSERT_FALSE(PathExists(non_existent));
647 EXPECT_TRUE(DeleteFile(non_existent, true)); 644 EXPECT_TRUE(DeleteFile(non_existent, true));
(...skipping 1663 matching lines...) Expand 10 before | Expand all | Expand 10 after
2311 2308
2312 #if defined(OS_ANDROID) 2309 #if defined(OS_ANDROID)
2313 TEST_F(FileUtilTest, ValidContentUriTest) { 2310 TEST_F(FileUtilTest, ValidContentUriTest) {
2314 // Get the test image path. 2311 // Get the test image path.
2315 FilePath data_dir; 2312 FilePath data_dir;
2316 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir)); 2313 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
2317 data_dir = data_dir.AppendASCII("file_util"); 2314 data_dir = data_dir.AppendASCII("file_util");
2318 ASSERT_TRUE(PathExists(data_dir)); 2315 ASSERT_TRUE(PathExists(data_dir));
2319 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png")); 2316 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2320 int64 image_size; 2317 int64 image_size;
2321 file_util::GetFileSize(image_file, &image_size); 2318 GetFileSize(image_file, &image_size);
2322 EXPECT_LT(0, image_size); 2319 EXPECT_LT(0, image_size);
2323 2320
2324 // Insert the image into MediaStore. MediaStore will do some conversions, and 2321 // Insert the image into MediaStore. MediaStore will do some conversions, and
2325 // return the content URI. 2322 // return the content URI.
2326 FilePath path = file_util::InsertImageIntoMediaStore(image_file); 2323 FilePath path = file_util::InsertImageIntoMediaStore(image_file);
2327 EXPECT_TRUE(path.IsContentUri()); 2324 EXPECT_TRUE(path.IsContentUri());
2328 EXPECT_TRUE(PathExists(path)); 2325 EXPECT_TRUE(PathExists(path));
2329 // The file size may not equal to the input image as MediaStore may convert 2326 // The file size may not equal to the input image as MediaStore may convert
2330 // the image. 2327 // the image.
2331 int64 content_uri_size; 2328 int64 content_uri_size;
2332 file_util::GetFileSize(path, &content_uri_size); 2329 GetFileSize(path, &content_uri_size);
2333 EXPECT_EQ(image_size, content_uri_size); 2330 EXPECT_EQ(image_size, content_uri_size);
2334 2331
2335 // We should be able to read the file. 2332 // We should be able to read the file.
2336 char* buffer = new char[image_size]; 2333 char* buffer = new char[image_size];
2337 int fd = OpenContentUriForRead(path); 2334 int fd = OpenContentUriForRead(path);
2338 EXPECT_LT(0, fd); 2335 EXPECT_LT(0, fd);
2339 EXPECT_TRUE(ReadFromFD(fd, buffer, image_size)); 2336 EXPECT_TRUE(ReadFromFD(fd, buffer, image_size));
2340 delete[] buffer; 2337 delete[] buffer;
2341 } 2338 }
2342 2339
2343 TEST_F(FileUtilTest, NonExistentContentUriTest) { 2340 TEST_F(FileUtilTest, NonExistentContentUriTest) {
2344 FilePath path("content://foo.bar"); 2341 FilePath path("content://foo.bar");
2345 EXPECT_TRUE(path.IsContentUri()); 2342 EXPECT_TRUE(path.IsContentUri());
2346 EXPECT_FALSE(PathExists(path)); 2343 EXPECT_FALSE(PathExists(path));
2347 // Size should be smaller than 0. 2344 // Size should be smaller than 0.
2348 int64 size; 2345 int64 size;
2349 EXPECT_FALSE(file_util::GetFileSize(path, &size)); 2346 EXPECT_FALSE(GetFileSize(path, &size));
2350 2347
2351 // We should not be able to read the file. 2348 // We should not be able to read the file.
2352 int fd = OpenContentUriForRead(path); 2349 int fd = OpenContentUriForRead(path);
2353 EXPECT_EQ(-1, fd); 2350 EXPECT_EQ(-1, fd);
2354 } 2351 }
2355 #endif 2352 #endif
2356 2353
2357 #endif // defined(OS_POSIX) 2354 #endif // defined(OS_POSIX)
2358 2355
2359 } // namespace 2356 } // namespace
2360 2357
2361 } // namespace base 2358 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698