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

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

Issue 1917893004: Merge M51: Add base::IsOnNetworkDrive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2704
Patch Set: Created 4 years, 8 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.h ('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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <fstream> 9 #include <fstream>
10 #include <set> 10 #include <set>
(...skipping 15 matching lines...) Expand all
26 #include "build/build_config.h" 26 #include "build/build_config.h"
27 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/platform_test.h" 28 #include "testing/platform_test.h"
29 29
30 #if defined(OS_WIN) 30 #if defined(OS_WIN)
31 #include <windows.h> 31 #include <windows.h>
32 #include <shellapi.h> 32 #include <shellapi.h>
33 #include <shlobj.h> 33 #include <shlobj.h>
34 #include <tchar.h> 34 #include <tchar.h>
35 #include <winioctl.h> 35 #include <winioctl.h>
36 #include "base/environment.h"
36 #include "base/win/scoped_handle.h" 37 #include "base/win/scoped_handle.h"
37 #include "base/win/windows_version.h" 38 #include "base/win/windows_version.h"
38 #endif 39 #endif
39 40
40 #if defined(OS_POSIX) 41 #if defined(OS_POSIX)
41 #include <errno.h> 42 #include <errno.h>
42 #include <fcntl.h> 43 #include <fcntl.h>
43 #include <unistd.h> 44 #include <unistd.h>
44 #endif 45 #endif
45 46
(...skipping 1611 matching lines...) Expand 10 before | Expand all | Expand 10 after
1657 } 1658 }
1658 1659
1659 // Restore the original $TMP. 1660 // Restore the original $TMP.
1660 if (original_tmp) { 1661 if (original_tmp) {
1661 ::_tputenv_s(kTmpKey, original_tmp); 1662 ::_tputenv_s(kTmpKey, original_tmp);
1662 free(original_tmp); 1663 free(original_tmp);
1663 } else { 1664 } else {
1664 ::_tputenv_s(kTmpKey, _T("")); 1665 ::_tputenv_s(kTmpKey, _T(""));
1665 } 1666 }
1666 } 1667 }
1668
1669 TEST_F(FileUtilTest, IsOnNetworkDrive) {
1670 struct LocalTestData {
1671 const FilePath::CharType* input;
1672 bool expected;
1673 };
1674
1675 const LocalTestData local_cases[] = {
1676 { FPL(""), false },
1677 { FPL("c:\\"), false },
1678 { FPL("c:"), false },
1679 { FPL("c:\\windows\\notepad.exe"), false }
1680 };
1681
1682 for (const auto& test_case : local_cases) {
1683 FilePath input(test_case.input);
1684 bool observed = IsOnNetworkDrive(input);
1685 EXPECT_EQ(test_case.expected, observed) << " input: " << input.value();
1686 }
1687
1688 Environment* env = Environment::Create();
1689 ASSERT_TRUE(!!env);
1690
1691 // To test IsOnNetworkDrive() for remote cases, set up a file server
1692 // and place a file called file.txt on the server e.g.
1693 // \\DC01\TESTSHARE\file.txt
1694 // then set the two environment variables:
1695 // set BASE_TEST_FILE_SERVER=DC01
1696 // set BASE_TEST_FILE_SHARE=TESTSHARE
1697 if (!env->HasVar("BASE_TEST_FILE_SERVER") ||
1698 !env->HasVar("BASE_TEST_FILE_SHARE")) {
1699 return;
1700 }
1701
1702 struct NetworkTestData {
1703 const wchar_t* input;
1704 bool expected;
1705 };
1706
1707 const NetworkTestData network_cases[] = {
1708 { L"\\\\%BASE_TEST_FILE_SERVER%", false },
1709 { L"\\\\%BASE_TEST_FILE_SERVER%\\", false },
1710 { L"\\\\%BASE_TEST_FILE_SERVER%\\file.txt", false },
1711 { L"\\\\%BASE_TEST_FILE_SERVER%\\%BASE_TEST_FILE_SHARE%", true },
1712 { L"\\\\%BASE_TEST_FILE_SERVER%\\%BASE_TEST_FILE_SHARE%\\", true },
1713 { L"\\\\%BASE_TEST_FILE_SERVER%\\%BASE_TEST_FILE_SHARE%\\file.txt", true },
1714 { L"\\\\%BASE_TEST_FILE_SERVER%\\%BASE_TEST_FILE_SHARE%\\no.txt", false }
1715 };
1716
1717 for (const auto& test_case : network_cases) {
1718 wchar_t path[MAX_PATH] = {0};
1719 ::ExpandEnvironmentStringsW(test_case.input, path, arraysize(path));
1720 FilePath input(path);
1721 EXPECT_EQ(test_case.expected, IsOnNetworkDrive(input)) << " input : "
1722 << input.value();
1723 }
1724 }
1667 #endif // OS_WIN 1725 #endif // OS_WIN
1668 1726
1669 TEST_F(FileUtilTest, CreateTemporaryFileTest) { 1727 TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1670 FilePath temp_files[3]; 1728 FilePath temp_files[3];
1671 for (int i = 0; i < 3; i++) { 1729 for (int i = 0; i < 3; i++) {
1672 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i]))); 1730 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
1673 EXPECT_TRUE(PathExists(temp_files[i])); 1731 EXPECT_TRUE(PathExists(temp_files[i]));
1674 EXPECT_FALSE(DirectoryExists(temp_files[i])); 1732 EXPECT_FALSE(DirectoryExists(temp_files[i]));
1675 } 1733 }
1676 for (int i = 0; i < 3; i++) 1734 for (int i = 0; i < 3; i++)
(...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after
2520 // Trying to close it should crash. This is important for security. 2578 // Trying to close it should crash. This is important for security.
2521 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2579 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2522 #endif 2580 #endif
2523 } 2581 }
2524 2582
2525 #endif // defined(OS_POSIX) 2583 #endif // defined(OS_POSIX)
2526 2584
2527 } // namespace 2585 } // namespace
2528 2586
2529 } // namespace base 2587 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util.h ('k') | base/files/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698