| 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 <memory> | 5 #include <memory> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 830 base::test::ScopedTaskScheduler scoped_task_scheduler_; | 830 base::test::ScopedTaskScheduler scoped_task_scheduler_; |
| 831 | 831 |
| 832 protected: | 832 protected: |
| 833 TestNetLog net_log_; | 833 TestNetLog net_log_; |
| 834 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. | 834 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 835 URLRequestJobFactoryImpl* job_factory_impl_; | 835 URLRequestJobFactoryImpl* job_factory_impl_; |
| 836 std::unique_ptr<URLRequestJobFactory> job_factory_; | 836 std::unique_ptr<URLRequestJobFactory> job_factory_; |
| 837 TestURLRequestContext default_context_; | 837 TestURLRequestContext default_context_; |
| 838 }; | 838 }; |
| 839 | 839 |
| 840 // This NetworkDelegate is picky about what files are accessible. Only |
| 841 // whitelisted files are allowed. |
| 842 class PickyNetworkDelegate : public TestNetworkDelegate { |
| 843 public: |
| 844 // Adds |path| to the access white list. |
| 845 void AddToWhitelist(const base::FilePath& path) { |
| 846 whitelist_.insert(base::MakeAbsoluteFilePath(path)); |
| 847 } |
| 848 |
| 849 bool OnCanAccessFile(const URLRequest& request, |
| 850 const base::FilePath& original_path, |
| 851 const base::FilePath& absolute_path) const override { |
| 852 return whitelist_.count(absolute_path) > 0; |
| 853 } |
| 854 |
| 855 private: |
| 856 std::set<base::FilePath> whitelist_; |
| 857 }; |
| 858 |
| 840 TEST_F(URLRequestTest, AboutBlankTest) { | 859 TEST_F(URLRequestTest, AboutBlankTest) { |
| 841 TestDelegate d; | 860 TestDelegate d; |
| 842 { | 861 { |
| 843 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( | 862 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 844 GURL("about:blank"), DEFAULT_PRIORITY, &d)); | 863 GURL("about:blank"), DEFAULT_PRIORITY, &d)); |
| 845 | 864 |
| 846 r->Start(); | 865 r->Start(); |
| 847 EXPECT_TRUE(r->is_pending()); | 866 EXPECT_TRUE(r->is_pending()); |
| 848 | 867 |
| 849 base::RunLoop().Run(); | 868 base::RunLoop().Run(); |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 } | 1100 } |
| 1082 | 1101 |
| 1083 TEST_F(URLRequestTest, AllowFileURLs) { | 1102 TEST_F(URLRequestTest, AllowFileURLs) { |
| 1084 base::ScopedTempDir temp_dir; | 1103 base::ScopedTempDir temp_dir; |
| 1085 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1104 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1086 base::FilePath test_file; | 1105 base::FilePath test_file; |
| 1087 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file)); | 1106 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file)); |
| 1088 std::string test_data("monkey"); | 1107 std::string test_data("monkey"); |
| 1089 base::WriteFile(test_file, test_data.data(), test_data.size()); | 1108 base::WriteFile(test_file, test_data.data(), test_data.size()); |
| 1090 GURL test_file_url = FilePathToFileURL(test_file); | 1109 GURL test_file_url = FilePathToFileURL(test_file); |
| 1091 | 1110 LOG(ERROR) << "@@ test_file: " << test_file.value(); |
| 1111 LOG(ERROR) << "@@ test_file_url: " << test_file_url.spec(); |
| 1092 { | 1112 { |
| 1093 TestDelegate d; | 1113 TestDelegate d; |
| 1094 TestNetworkDelegate network_delegate; | 1114 PickyNetworkDelegate network_delegate; |
| 1095 network_delegate.set_can_access_files(true); | 1115 network_delegate.AddToWhitelist(test_file); |
| 1096 default_context_.set_network_delegate(&network_delegate); | 1116 default_context_.set_network_delegate(&network_delegate); |
| 1097 std::unique_ptr<URLRequest> r( | 1117 std::unique_ptr<URLRequest> r( |
| 1098 default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); | 1118 default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
| 1099 r->Start(); | 1119 r->Start(); |
| 1100 base::RunLoop().Run(); | 1120 base::RunLoop().Run(); |
| 1121 // This should be allowed as the file path is white listed. |
| 1101 EXPECT_FALSE(d.request_failed()); | 1122 EXPECT_FALSE(d.request_failed()); |
| 1102 EXPECT_EQ(test_data, d.data_received()); | 1123 EXPECT_EQ(test_data, d.data_received()); |
| 1103 } | 1124 } |
| 1104 | 1125 |
| 1105 { | 1126 { |
| 1106 TestDelegate d; | 1127 TestDelegate d; |
| 1107 TestNetworkDelegate network_delegate; | 1128 PickyNetworkDelegate network_delegate; |
| 1108 network_delegate.set_can_access_files(false); | |
| 1109 default_context_.set_network_delegate(&network_delegate); | 1129 default_context_.set_network_delegate(&network_delegate); |
| 1110 std::unique_ptr<URLRequest> r( | 1130 std::unique_ptr<URLRequest> r( |
| 1111 default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); | 1131 default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
| 1112 r->Start(); | 1132 r->Start(); |
| 1113 base::RunLoop().Run(); | 1133 base::RunLoop().Run(); |
| 1134 // This should be rejected as the file path is not white listed. |
| 1114 EXPECT_TRUE(d.request_failed()); | 1135 EXPECT_TRUE(d.request_failed()); |
| 1115 EXPECT_EQ("", d.data_received()); | 1136 EXPECT_EQ("", d.data_received()); |
| 1116 } | 1137 } |
| 1117 } | 1138 } |
| 1118 | 1139 |
| 1140 #if defined(OS_POSIX) // Bacause of symbolic links. |
| 1141 |
| 1142 TEST_F(URLRequestTest, SymlinksToFiles) { |
| 1143 base::ScopedTempDir temp_dir; |
| 1144 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1145 // The test file be added to the whitelist. |
| 1146 base::FilePath test_file; |
| 1147 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file)); |
| 1148 std::string test_data("monkey"); |
| 1149 base::WriteFile(test_file, test_data.data(), test_data.size()); |
| 1150 |
| 1151 // This_symlink will point to the test file. |
| 1152 base::FilePath good_symlink = temp_dir.GetPath().AppendASCII("good_symlink"); |
| 1153 ASSERT_TRUE(base::CreateSymbolicLink(test_file, good_symlink)); |
| 1154 GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1155 // This_symlink will point to /dev/null. |
| 1156 base::FilePath bad_symlink = temp_dir.GetPath().AppendASCII("bad_symlink"); |
| 1157 ASSERT_TRUE(base::CreateSymbolicLink( |
| 1158 base::FilePath(FILE_PATH_LITERAL("/dev/null")), bad_symlink)); |
| 1159 GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1160 |
| 1161 { |
| 1162 TestDelegate d; |
| 1163 PickyNetworkDelegate network_delegate; |
| 1164 network_delegate.AddToWhitelist(test_file); |
| 1165 default_context_.set_network_delegate(&network_delegate); |
| 1166 std::unique_ptr<URLRequest> r( |
| 1167 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d)); |
| 1168 r->Start(); |
| 1169 base::RunLoop().Run(); |
| 1170 // good_file_url should be allowed. |
| 1171 EXPECT_FALSE(d.request_failed()); |
| 1172 EXPECT_EQ(test_data, d.data_received()); |
| 1173 } |
| 1174 |
| 1175 { |
| 1176 TestDelegate d; |
| 1177 PickyNetworkDelegate network_delegate; |
| 1178 network_delegate.AddToWhitelist(test_file); |
| 1179 default_context_.set_network_delegate(&network_delegate); |
| 1180 std::unique_ptr<URLRequest> r( |
| 1181 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d)); |
| 1182 r->Start(); |
| 1183 base::RunLoop().Run(); |
| 1184 // bad_file_url should be rejected. |
| 1185 EXPECT_TRUE(d.request_failed()); |
| 1186 EXPECT_EQ("", d.data_received()); |
| 1187 } |
| 1188 } |
| 1189 |
| 1190 TEST_F(URLRequestTest, SymlinksToDirs) { |
| 1191 // The temporary dir will be added to the whitelist. |
| 1192 base::ScopedTempDir temp_dir; |
| 1193 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1194 |
| 1195 // This_symlink will point to the temp dir. |
| 1196 base::FilePath good_symlink = temp_dir.GetPath().AppendASCII("good_symlink"); |
| 1197 ASSERT_TRUE(base::CreateSymbolicLink(temp_dir.GetPath(), good_symlink)); |
| 1198 GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1199 // This_symlink will point to /dev. |
| 1200 base::FilePath bad_symlink = temp_dir.GetPath().AppendASCII("bad_symlink"); |
| 1201 ASSERT_TRUE(base::CreateSymbolicLink( |
| 1202 base::FilePath(FILE_PATH_LITERAL("/dev")), bad_symlink)); |
| 1203 GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1204 |
| 1205 { |
| 1206 TestDelegate d; |
| 1207 PickyNetworkDelegate network_delegate; |
| 1208 network_delegate.AddToWhitelist(temp_dir.GetPath()); |
| 1209 default_context_.set_network_delegate(&network_delegate); |
| 1210 std::unique_ptr<URLRequest> r( |
| 1211 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d)); |
| 1212 r->Start(); |
| 1213 base::RunLoop().Run(); |
| 1214 // good_file_url should be allowed. |
| 1215 EXPECT_FALSE(d.request_failed()); |
| 1216 ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1217 } |
| 1218 |
| 1219 { |
| 1220 TestDelegate d; |
| 1221 PickyNetworkDelegate network_delegate; |
| 1222 network_delegate.AddToWhitelist(temp_dir.GetPath()); |
| 1223 default_context_.set_network_delegate(&network_delegate); |
| 1224 std::unique_ptr<URLRequest> r( |
| 1225 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d)); |
| 1226 r->Start(); |
| 1227 base::RunLoop().Run(); |
| 1228 // bad_file_url should be rejected. |
| 1229 EXPECT_TRUE(d.request_failed()); |
| 1230 EXPECT_EQ("", d.data_received()); |
| 1231 } |
| 1232 } |
| 1233 |
| 1234 #endif // defined(OS_POSIX) |
| 1119 | 1235 |
| 1120 TEST_F(URLRequestTest, FileDirCancelTest) { | 1236 TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1121 // Put in mock resource provider. | 1237 // Put in mock resource provider. |
| 1122 NetModule::SetResourceProvider(TestNetResourceProvider); | 1238 NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1123 | 1239 |
| 1124 TestDelegate d; | 1240 TestDelegate d; |
| 1125 { | 1241 { |
| 1126 base::FilePath file_path; | 1242 base::FilePath file_path; |
| 1127 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); | 1243 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1128 file_path = file_path.Append(FILE_PATH_LITERAL("net")); | 1244 file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1158 req->Start(); | 1274 req->Start(); |
| 1159 base::RunLoop().Run(); | 1275 base::RunLoop().Run(); |
| 1160 | 1276 |
| 1161 // Generate entry for the sentinel file. | 1277 // Generate entry for the sentinel file. |
| 1162 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); | 1278 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
| 1163 base::File::Info info; | 1279 base::File::Info info; |
| 1164 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); | 1280 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
| 1165 EXPECT_GT(info.size, 0); | 1281 EXPECT_GT(info.size, 0); |
| 1166 std::string sentinel_output = GetDirectoryListingEntry( | 1282 std::string sentinel_output = GetDirectoryListingEntry( |
| 1167 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), | 1283 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 1168 std::string(sentinel_name), | 1284 std::string(sentinel_name), false /* is_dir */, info.size, |
| 1169 false /* is_dir */, | 1285 |
| 1170 info.size, | |
| 1171 info.last_modified); | 1286 info.last_modified); |
| 1172 | 1287 |
| 1173 ASSERT_LT(0, d.bytes_received()); | 1288 ASSERT_LT(0, d.bytes_received()); |
| 1174 ASSERT_FALSE(d.request_failed()); | 1289 ASSERT_FALSE(d.request_failed()); |
| 1175 EXPECT_EQ(OK, d.request_status()); | 1290 EXPECT_EQ(OK, d.request_status()); |
| 1176 // Check for the entry generated for the "sentinel" file. | 1291 // Check for the entry generated for the "sentinel" file. |
| 1177 const std::string& data = d.data_received(); | 1292 const std::string& data = d.data_received(); |
| 1178 ASSERT_NE(data.find(sentinel_output), std::string::npos); | 1293 ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1179 } | 1294 } |
| 1180 | 1295 |
| (...skipping 9690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10871 AddTestInterceptor()->set_main_intercept_job(std::move(job)); | 10986 AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 10872 | 10987 |
| 10873 req->Start(); | 10988 req->Start(); |
| 10874 req->Cancel(); | 10989 req->Cancel(); |
| 10875 base::RunLoop().RunUntilIdle(); | 10990 base::RunLoop().RunUntilIdle(); |
| 10876 EXPECT_EQ(ERR_ABORTED, d.request_status()); | 10991 EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 10877 EXPECT_EQ(0, d.received_redirect_count()); | 10992 EXPECT_EQ(0, d.received_redirect_count()); |
| 10878 } | 10993 } |
| 10879 | 10994 |
| 10880 } // namespace net | 10995 } // namespace net |
| OLD | NEW |