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 15 matching lines...) Expand all Loading... |
26 #include "base/test/test_file_util.h" | 26 #include "base/test/test_file_util.h" |
27 #include "base/threading/platform_thread.h" | 27 #include "base/threading/platform_thread.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "testing/platform_test.h" | 29 #include "testing/platform_test.h" |
30 | 30 |
31 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
32 #include "base/win/scoped_handle.h" | 32 #include "base/win/scoped_handle.h" |
33 #include "base/win/windows_version.h" | 33 #include "base/win/windows_version.h" |
34 #endif | 34 #endif |
35 | 35 |
| 36 #if defined(OS_ANDROID) |
| 37 #include "base/android/content_uri_utils.h" |
| 38 #endif |
| 39 |
36 // This macro helps avoid wrapped lines in the test structs. | 40 // This macro helps avoid wrapped lines in the test structs. |
37 #define FPL(x) FILE_PATH_LITERAL(x) | 41 #define FPL(x) FILE_PATH_LITERAL(x) |
38 | 42 |
39 using base::DirectoryExists; | 43 using base::DirectoryExists; |
40 using base::FileEnumerator; | 44 using base::FileEnumerator; |
41 using base::FilePath; | 45 using base::FilePath; |
42 using base::PathIsWritable; | 46 using base::PathIsWritable; |
43 using base::TextContentsEqual; | 47 using base::TextContentsEqual; |
44 | 48 |
45 namespace { | 49 namespace { |
(...skipping 2266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2312 file_util::VerifyPathControlledByUser( | 2316 file_util::VerifyPathControlledByUser( |
2313 base_dir_, sub_dir_, uid_, ok_gids_)); | 2317 base_dir_, sub_dir_, uid_, ok_gids_)); |
2314 EXPECT_TRUE( | 2318 EXPECT_TRUE( |
2315 file_util::VerifyPathControlledByUser( | 2319 file_util::VerifyPathControlledByUser( |
2316 base_dir_, text_file_, uid_, ok_gids_)); | 2320 base_dir_, text_file_, uid_, ok_gids_)); |
2317 EXPECT_TRUE( | 2321 EXPECT_TRUE( |
2318 file_util::VerifyPathControlledByUser( | 2322 file_util::VerifyPathControlledByUser( |
2319 sub_dir_, text_file_, uid_, ok_gids_)); | 2323 sub_dir_, text_file_, uid_, ok_gids_)); |
2320 } | 2324 } |
2321 | 2325 |
| 2326 #if defined(OS_ANDROID) |
| 2327 TEST_F(FileUtilTest, ValidContentUriTest) { |
| 2328 // Get the test image path. |
| 2329 FilePath data_dir; |
| 2330 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir)); |
| 2331 data_dir = data_dir.AppendASCII("file_util"); |
| 2332 ASSERT_TRUE(base::PathExists(data_dir)); |
| 2333 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png")); |
| 2334 int64 image_size; |
| 2335 file_util::GetFileSize(image_file, &image_size); |
| 2336 EXPECT_LT(0, image_size); |
| 2337 |
| 2338 // Insert the image into MediaStore. MediaStore will do some conversions, and |
| 2339 // return the content URI. |
| 2340 base::FilePath path = file_util::InsertImageIntoMediaStore(image_file); |
| 2341 EXPECT_TRUE(path.IsContentUri()); |
| 2342 EXPECT_TRUE(base::PathExists(path)); |
| 2343 // The file size may not equal to the input image as MediaStore may convert |
| 2344 // the image. |
| 2345 int64 content_uri_size; |
| 2346 file_util::GetFileSize(path, &content_uri_size); |
| 2347 EXPECT_EQ(image_size, content_uri_size); |
| 2348 |
| 2349 // We should be able to read the file. |
| 2350 char* buffer = new char[image_size]; |
| 2351 int fd = base::OpenContentUriForRead(path); |
| 2352 EXPECT_LT(0, fd); |
| 2353 EXPECT_TRUE(file_util::ReadFromFD(fd, buffer, image_size)); |
| 2354 delete[] buffer; |
| 2355 } |
| 2356 |
| 2357 TEST_F(FileUtilTest, NonExistentContentUriTest) { |
| 2358 base::FilePath path("content://foo.bar"); |
| 2359 EXPECT_TRUE(path.IsContentUri()); |
| 2360 EXPECT_FALSE(base::PathExists(path)); |
| 2361 // Size should be smaller than 0. |
| 2362 int64 size; |
| 2363 file_util::GetFileSize(path, &size); |
| 2364 EXPECT_GT(0, size); |
| 2365 |
| 2366 // We should not be able to read the file. |
| 2367 int fd = base::OpenContentUriForRead(path); |
| 2368 EXPECT_EQ(-1, fd); |
| 2369 } |
| 2370 #endif |
| 2371 |
2322 #endif // defined(OS_POSIX) | 2372 #endif // defined(OS_POSIX) |
2323 | 2373 |
2324 } // namespace | 2374 } // namespace |
OLD | NEW |