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

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

Issue 1892153003: Add base::IsOnNetworkDrive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: code review comments 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
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>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/base_paths.h" 14 #include "base/base_paths.h"
15 #include "base/files/file_enumerator.h" 15 #include "base/files/file_enumerator.h"
16 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
17 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
18 #include "base/files/scoped_file.h" 18 #include "base/files/scoped_file.h"
19 #include "base/files/scoped_temp_dir.h" 19 #include "base/files/scoped_temp_dir.h"
20 #include "base/macros.h" 20 #include "base/macros.h"
21 #include "base/path_service.h" 21 #include "base/path_service.h"
22 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
23 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
24 #include "base/test/test_file_util.h" 25 #include "base/test/test_file_util.h"
25 #include "base/threading/platform_thread.h" 26 #include "base/threading/platform_thread.h"
26 #include "build/build_config.h" 27 #include "build/build_config.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/platform_test.h" 29 #include "testing/platform_test.h"
29 30
30 #if defined(OS_WIN) 31 #if defined(OS_WIN)
31 #include <windows.h> 32 #include <windows.h>
32 #include <shellapi.h> 33 #include <shellapi.h>
33 #include <shlobj.h> 34 #include <shlobj.h>
34 #include <tchar.h> 35 #include <tchar.h>
35 #include <winioctl.h> 36 #include <winioctl.h>
37 #include "base/environment.h"
36 #include "base/win/scoped_handle.h" 38 #include "base/win/scoped_handle.h"
37 #include "base/win/windows_version.h" 39 #include "base/win/windows_version.h"
38 #endif 40 #endif
39 41
40 #if defined(OS_POSIX) 42 #if defined(OS_POSIX)
41 #include <errno.h> 43 #include <errno.h>
42 #include <fcntl.h> 44 #include <fcntl.h>
43 #include <unistd.h> 45 #include <unistd.h>
44 #endif 46 #endif
45 47
(...skipping 1611 matching lines...) Expand 10 before | Expand all | Expand 10 after
1657 } 1659 }
1658 1660
1659 // Restore the original $TMP. 1661 // Restore the original $TMP.
1660 if (original_tmp) { 1662 if (original_tmp) {
1661 ::_tputenv_s(kTmpKey, original_tmp); 1663 ::_tputenv_s(kTmpKey, original_tmp);
1662 free(original_tmp); 1664 free(original_tmp);
1663 } else { 1665 } else {
1664 ::_tputenv_s(kTmpKey, _T("")); 1666 ::_tputenv_s(kTmpKey, _T(""));
1665 } 1667 }
1666 } 1668 }
1669
1670 TEST_F(FileUtilTest, IsOnNetworkDrive) {
1671 struct LocalTestData {
1672 const FilePath::CharType* input;
1673 bool expected;
1674 };
1675
1676 const LocalTestData local_cases[] = {
1677 { FPL(""), false },
1678 { FPL("c:\\"), false },
1679 { FPL("c:"), false },
1680 { FPL("c:\\windows\\notepad.exe"), false }
1681 };
1682
1683 for (const auto& test_case : local_cases) {
1684 FilePath input(test_case.input);
1685 bool observed = IsOnNetworkDrive(input);
1686 EXPECT_EQ(test_case.expected, observed) << " input: " << input.value();
1687 }
1688
1689 Environment* env = Environment::Create();
1690 ASSERT_TRUE(!!env);
1691
1692 std::string network_server;
1693 std::string network_share;
1694
1695 // To test IsOnNetworkDrive() for remote cases, set up a file server
Will Harris 2016/04/15 23:42:19 added environment variables for testing network sh
1696 // and place a file called file.txt on the server e.g.
1697 // \\DC01\TESTSHARE\file.txt
1698 // then set the two environment variables:
1699 // set BASE_TEST_FILE_SERVER=DC01
1700 // set BASE_TEST_FILE_SHARE=TESTSHARE
1701 if (!env->GetVar("BASE_TEST_FILE_SERVER", &network_server) ||
1702 !env->GetVar("BASE_TEST_FILE_SHARE", &network_share)) {
1703 return;
1704 }
1705
1706 struct NetworkTestData {
1707 const char* input;
1708 bool format_string_both;
1709 bool expected;
1710 };
1711
1712 const NetworkTestData network_cases[] = {
1713 { "\\\\%hs", false, false },
1714 { "\\\\%hs\\", false, false },
1715 { "\\\\%hs\\file.txt", false, false },
1716 { "\\\\%hs\\%hs", true, true },
1717 { "\\\\%hs\\%hs\\", true, true },
1718 { "\\\\%hs\\%hs\\file.txt", true, true }
1719 };
1720
1721 for (const auto& test_case : network_cases) {
1722 std::string full_path;
1723 if (test_case.format_string_both) {
1724 full_path = base::StringPrintf(test_case.input, network_server.c_str(),
Will Harris 2016/04/15 23:51:08 in retrospect I should probably just use ExpandEnv
1725 network_share.c_str());
1726 } else {
1727 full_path = base::StringPrintf(test_case.input, network_server.c_str());
1728 }
1729 FilePath input(base::UTF8ToUTF16(full_path));
1730 EXPECT_EQ(test_case.expected, IsOnNetworkDrive(input)) << " input : "
1731 << input.value();
1732 }
1733 }
1667 #endif // OS_WIN 1734 #endif // OS_WIN
1668 1735
1669 TEST_F(FileUtilTest, CreateTemporaryFileTest) { 1736 TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1670 FilePath temp_files[3]; 1737 FilePath temp_files[3];
1671 for (int i = 0; i < 3; i++) { 1738 for (int i = 0; i < 3; i++) {
1672 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i]))); 1739 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
1673 EXPECT_TRUE(PathExists(temp_files[i])); 1740 EXPECT_TRUE(PathExists(temp_files[i]));
1674 EXPECT_FALSE(DirectoryExists(temp_files[i])); 1741 EXPECT_FALSE(DirectoryExists(temp_files[i]));
1675 } 1742 }
1676 for (int i = 0; i < 3; i++) 1743 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. 2587 // Trying to close it should crash. This is important for security.
2521 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2588 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2522 #endif 2589 #endif
2523 } 2590 }
2524 2591
2525 #endif // defined(OS_POSIX) 2592 #endif // defined(OS_POSIX)
2526 2593
2527 } // namespace 2594 } // namespace
2528 2595
2529 } // namespace base 2596 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698