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 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
828 } | 828 } |
829 | 829 |
830 protected: | 830 protected: |
831 TestNetLog net_log_; | 831 TestNetLog net_log_; |
832 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. | 832 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
833 URLRequestJobFactoryImpl* job_factory_impl_; | 833 URLRequestJobFactoryImpl* job_factory_impl_; |
834 std::unique_ptr<URLRequestJobFactory> job_factory_; | 834 std::unique_ptr<URLRequestJobFactory> job_factory_; |
835 TestURLRequestContext default_context_; | 835 TestURLRequestContext default_context_; |
836 }; | 836 }; |
837 | 837 |
838 // This NetworkDelegate is picky about what files are accessible. Only | |
839 // whitelisted files are allowed. | |
840 class CookieBlockingNetworkDelegate : public TestNetworkDelegate { | |
841 public: | |
842 CookieBlockingNetworkDelegate(){}; | |
843 | |
844 // Adds |directory| to the access white list. | |
845 void AddToWhitelist(const base::FilePath& directory) { | |
846 // TODO(satorux): Remove this. This is for debugging test failures on | |
847 // Windows bots. | |
848 LOG(ERROR) << "@@ AddToWhitelist: " << directory.value(); | |
satorux1
2017/05/19 07:41:56
will remove these failures on win bots are address
satorux1
2017/05/19 13:11:41
here's we got:
[ RUN ] URLRequestTest.Allow
| |
849 whitelist_.insert(directory); | |
850 } | |
851 | |
852 // Returns true if |path| matches the white list. | |
853 bool OnCanAccessFileInternal(const base::FilePath& path) const { | |
854 for (const auto& directory : whitelist_) { | |
855 if (directory == path || directory.IsParent(path)) | |
856 return true; | |
857 } | |
858 return false; | |
859 } | |
860 | |
861 // Returns true only if both |original_path| and |absolute_path| match the | |
862 // white list. | |
863 bool OnCanAccessFile(const URLRequest& request, | |
864 const base::FilePath& original_path, | |
865 const base::FilePath& absolute_path) const override { | |
866 // TODO(satorux): Remove this. This is for debugging test failures on | |
867 // Windows bots. | |
868 LOG(ERROR) << "@@ original_path: " << original_path.value(); | |
869 LOG(ERROR) << "@@ absolute_path: " << absolute_path.value(); | |
870 return (OnCanAccessFileInternal(original_path) && | |
871 OnCanAccessFileInternal(absolute_path)); | |
872 } | |
873 | |
874 private: | |
875 std::set<base::FilePath> whitelist_; | |
876 | |
877 DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); | |
878 }; | |
879 | |
838 TEST_F(URLRequestTest, AboutBlankTest) { | 880 TEST_F(URLRequestTest, AboutBlankTest) { |
839 TestDelegate d; | 881 TestDelegate d; |
840 { | 882 { |
841 std::unique_ptr<URLRequest> r( | 883 std::unique_ptr<URLRequest> r( |
842 default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, | 884 default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
843 &d, TRAFFIC_ANNOTATION_FOR_TESTS)); | 885 &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
844 | 886 |
845 r->Start(); | 887 r->Start(); |
846 EXPECT_TRUE(r->is_pending()); | 888 EXPECT_TRUE(r->is_pending()); |
847 | 889 |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1076 base::RunLoop().Run(); | 1118 base::RunLoop().Run(); |
1077 EXPECT_TRUE(d.request_failed()); | 1119 EXPECT_TRUE(d.request_failed()); |
1078 } | 1120 } |
1079 | 1121 |
1080 EXPECT_TRUE(base::DeleteFile(temp_path, false)); | 1122 EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
1081 } | 1123 } |
1082 | 1124 |
1083 TEST_F(URLRequestTest, AllowFileURLs) { | 1125 TEST_F(URLRequestTest, AllowFileURLs) { |
1084 base::ScopedTempDir temp_dir; | 1126 base::ScopedTempDir temp_dir; |
1085 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1127 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1128 // Get an absolute path since the path can contain a symbolic link. | |
1129 base::FilePath absolute_temp_dir = | |
1130 base::MakeAbsoluteFilePath(temp_dir.GetPath()); | |
1086 base::FilePath test_file; | 1131 base::FilePath test_file; |
1087 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file)); | 1132 ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, &test_file)); |
1088 std::string test_data("monkey"); | 1133 std::string test_data("monkey"); |
1089 base::WriteFile(test_file, test_data.data(), test_data.size()); | 1134 base::WriteFile(test_file, test_data.data(), test_data.size()); |
1090 GURL test_file_url = FilePathToFileURL(test_file); | 1135 GURL test_file_url = FilePathToFileURL(test_file); |
1091 | |
1092 { | 1136 { |
1093 TestDelegate d; | 1137 TestDelegate d; |
1094 TestNetworkDelegate network_delegate; | 1138 CookieBlockingNetworkDelegate network_delegate; |
1095 network_delegate.set_can_access_files(true); | 1139 network_delegate.AddToWhitelist(absolute_temp_dir); |
1096 default_context_.set_network_delegate(&network_delegate); | 1140 default_context_.set_network_delegate(&network_delegate); |
1097 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( | 1141 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
1098 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); | 1142 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
1099 r->Start(); | 1143 r->Start(); |
1100 base::RunLoop().Run(); | 1144 base::RunLoop().Run(); |
1145 // This should be allowed as the file path is white listed. | |
1101 EXPECT_FALSE(d.request_failed()); | 1146 EXPECT_FALSE(d.request_failed()); |
1102 EXPECT_EQ(test_data, d.data_received()); | 1147 EXPECT_EQ(test_data, d.data_received()); |
1103 } | 1148 } |
1104 | 1149 |
1105 { | 1150 { |
1106 TestDelegate d; | 1151 TestDelegate d; |
1107 TestNetworkDelegate network_delegate; | 1152 CookieBlockingNetworkDelegate network_delegate; |
1108 network_delegate.set_can_access_files(false); | |
1109 default_context_.set_network_delegate(&network_delegate); | 1153 default_context_.set_network_delegate(&network_delegate); |
1110 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( | 1154 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
1111 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); | 1155 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
1112 r->Start(); | 1156 r->Start(); |
1113 base::RunLoop().Run(); | 1157 base::RunLoop().Run(); |
1158 // This should be rejected as the file path is not white listed. | |
1114 EXPECT_TRUE(d.request_failed()); | 1159 EXPECT_TRUE(d.request_failed()); |
1115 EXPECT_EQ("", d.data_received()); | 1160 EXPECT_EQ("", d.data_received()); |
1161 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); | |
1116 } | 1162 } |
1117 } | 1163 } |
1118 | 1164 |
1165 #if defined(OS_POSIX) // Bacause of symbolic links. | |
1166 | |
1167 TEST_F(URLRequestTest, SymlinksToFiles) { | |
1168 base::ScopedTempDir temp_dir; | |
1169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
1170 // Get an absolute path since the path can contain a symbolic link. | |
1171 base::FilePath absolute_temp_dir = | |
1172 base::MakeAbsoluteFilePath(temp_dir.GetPath()); | |
1173 | |
1174 // Create a good directory (will be white listed) and a good file. | |
1175 base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); | |
1176 ASSERT_TRUE(base::CreateDirectory(good_dir)); | |
1177 base::FilePath good_file; | |
1178 ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); | |
1179 std::string good_data("good"); | |
1180 base::WriteFile(good_file, good_data.data(), good_data.size()); | |
1181 | |
1182 // Create a bad directory (will not be white listed) and a bad file. | |
1183 base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); | |
1184 ASSERT_TRUE(base::CreateDirectory(bad_dir)); | |
1185 base::FilePath bad_file; | |
1186 ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); | |
1187 std::string bad_data("bad"); | |
1188 base::WriteFile(bad_file, bad_data.data(), bad_data.size()); | |
1189 | |
1190 // This symlink will point to the good file. Access to the symlink will be | |
1191 // allowed as both the symlink and the destination file are in the same | |
1192 // good directory. | |
1193 base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); | |
1194 ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); | |
1195 GURL good_file_url = FilePathToFileURL(good_symlink); | |
1196 // This symlink will point to the bad file. Even though the symlink is in | |
1197 // the good directory, access to the symlink will be rejected since it | |
1198 // points to the bad file. | |
1199 base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); | |
1200 ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); | |
1201 GURL bad_file_url = FilePathToFileURL(bad_symlink); | |
1202 | |
1203 { | |
1204 TestDelegate d; | |
1205 CookieBlockingNetworkDelegate network_delegate; | |
1206 network_delegate.AddToWhitelist(good_dir); | |
1207 default_context_.set_network_delegate(&network_delegate); | |
1208 std::unique_ptr<URLRequest> r( | |
1209 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d)); | |
1210 r->Start(); | |
1211 base::RunLoop().Run(); | |
1212 // good_file_url should be allowed. | |
1213 EXPECT_FALSE(d.request_failed()); | |
1214 EXPECT_EQ(good_data, d.data_received()); | |
1215 } | |
1216 | |
1217 { | |
1218 TestDelegate d; | |
1219 CookieBlockingNetworkDelegate network_delegate; | |
1220 network_delegate.AddToWhitelist(good_dir); | |
1221 default_context_.set_network_delegate(&network_delegate); | |
1222 std::unique_ptr<URLRequest> r( | |
1223 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d)); | |
1224 r->Start(); | |
1225 base::RunLoop().Run(); | |
1226 // bad_file_url should be rejected. | |
1227 EXPECT_TRUE(d.request_failed()); | |
1228 EXPECT_EQ("", d.data_received()); | |
1229 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); | |
1230 } | |
1231 } | |
1232 | |
1233 TEST_F(URLRequestTest, SymlinksToDirs) { | |
1234 // The temporary dir will be added to the whitelist. | |
1235 base::ScopedTempDir temp_dir; | |
1236 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
1237 // Get an absolute path since the path can contain a symbolic link. | |
1238 base::FilePath absolute_temp_dir = | |
1239 base::MakeAbsoluteFilePath(temp_dir.GetPath()); | |
1240 | |
1241 // Create a good directory (will be white listed). | |
1242 base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); | |
1243 ASSERT_TRUE(base::CreateDirectory(good_dir)); | |
1244 | |
1245 // Create a bad directory (will not be white listed). | |
1246 base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); | |
1247 ASSERT_TRUE(base::CreateDirectory(bad_dir)); | |
1248 | |
1249 // This symlink will point to the good directory. Access to the symlink | |
1250 // will be allowed as the symlink is in the good dir that'll be white | |
1251 // listed. | |
1252 base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); | |
1253 ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); | |
1254 GURL good_file_url = FilePathToFileURL(good_symlink); | |
1255 // This symlink will point to the bad directory. Even though the symlink is | |
1256 // in the good directory, access to the symlink will be rejected since it | |
1257 // points to the bad directory. | |
1258 base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); | |
1259 ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); | |
1260 GURL bad_file_url = FilePathToFileURL(bad_symlink); | |
1261 | |
1262 { | |
1263 TestDelegate d; | |
1264 CookieBlockingNetworkDelegate network_delegate; | |
1265 network_delegate.AddToWhitelist(good_dir); | |
1266 default_context_.set_network_delegate(&network_delegate); | |
1267 std::unique_ptr<URLRequest> r( | |
1268 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d)); | |
1269 r->Start(); | |
1270 base::RunLoop().Run(); | |
1271 // good_file_url should be allowed. | |
1272 EXPECT_FALSE(d.request_failed()); | |
1273 ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); | |
1274 } | |
1275 | |
1276 { | |
1277 TestDelegate d; | |
1278 CookieBlockingNetworkDelegate network_delegate; | |
1279 network_delegate.AddToWhitelist(good_dir); | |
1280 default_context_.set_network_delegate(&network_delegate); | |
1281 std::unique_ptr<URLRequest> r( | |
1282 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d)); | |
1283 r->Start(); | |
1284 base::RunLoop().Run(); | |
1285 // bad_file_url should be rejected. | |
1286 EXPECT_TRUE(d.request_failed()); | |
1287 EXPECT_EQ("", d.data_received()); | |
1288 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); | |
1289 } | |
1290 } | |
1291 | |
1292 #endif // defined(OS_POSIX) | |
1119 | 1293 |
1120 TEST_F(URLRequestTest, FileDirCancelTest) { | 1294 TEST_F(URLRequestTest, FileDirCancelTest) { |
1121 // Put in mock resource provider. | 1295 // Put in mock resource provider. |
1122 NetModule::SetResourceProvider(TestNetResourceProvider); | 1296 NetModule::SetResourceProvider(TestNetResourceProvider); |
1123 | 1297 |
1124 TestDelegate d; | 1298 TestDelegate d; |
1125 { | 1299 { |
1126 base::FilePath file_path; | 1300 base::FilePath file_path; |
1127 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); | 1301 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
1128 file_path = file_path.Append(FILE_PATH_LITERAL("net")); | 1302 file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1160 req->Start(); | 1334 req->Start(); |
1161 base::RunLoop().Run(); | 1335 base::RunLoop().Run(); |
1162 | 1336 |
1163 // Generate entry for the sentinel file. | 1337 // Generate entry for the sentinel file. |
1164 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); | 1338 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
1165 base::File::Info info; | 1339 base::File::Info info; |
1166 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); | 1340 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
1167 EXPECT_GT(info.size, 0); | 1341 EXPECT_GT(info.size, 0); |
1168 std::string sentinel_output = GetDirectoryListingEntry( | 1342 std::string sentinel_output = GetDirectoryListingEntry( |
1169 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), | 1343 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
1170 std::string(sentinel_name), | 1344 std::string(sentinel_name), false /* is_dir */, info.size, |
1171 false /* is_dir */, | 1345 |
1172 info.size, | |
1173 info.last_modified); | 1346 info.last_modified); |
1174 | 1347 |
1175 ASSERT_LT(0, d.bytes_received()); | 1348 ASSERT_LT(0, d.bytes_received()); |
1176 ASSERT_FALSE(d.request_failed()); | 1349 ASSERT_FALSE(d.request_failed()); |
1177 EXPECT_EQ(OK, d.request_status()); | 1350 EXPECT_EQ(OK, d.request_status()); |
1178 // Check for the entry generated for the "sentinel" file. | 1351 // Check for the entry generated for the "sentinel" file. |
1179 const std::string& data = d.data_received(); | 1352 const std::string& data = d.data_received(); |
1180 ASSERT_NE(data.find(sentinel_output), std::string::npos); | 1353 ASSERT_NE(data.find(sentinel_output), std::string::npos); |
1181 } | 1354 } |
1182 | 1355 |
(...skipping 9876 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
11059 AddTestInterceptor()->set_main_intercept_job(std::move(job)); | 11232 AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
11060 | 11233 |
11061 req->Start(); | 11234 req->Start(); |
11062 req->Cancel(); | 11235 req->Cancel(); |
11063 base::RunLoop().RunUntilIdle(); | 11236 base::RunLoop().RunUntilIdle(); |
11064 EXPECT_EQ(ERR_ABORTED, d.request_status()); | 11237 EXPECT_EQ(ERR_ABORTED, d.request_status()); |
11065 EXPECT_EQ(0, d.received_redirect_count()); | 11238 EXPECT_EQ(0, d.received_redirect_count()); |
11066 } | 11239 } |
11067 | 11240 |
11068 } // namespace net | 11241 } // namespace net |
OLD | NEW |