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

Side by Side Diff: net/url_request/url_request_unittest.cc

Issue 2786583002: chromeos: Check both original and absolute paths for file: scheme (Closed)
Patch Set: add null checks for network_delegate() Created 3 years, 7 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 <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 822 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 base::test::ScopedTaskScheduler scoped_task_scheduler_; 833 base::test::ScopedTaskScheduler scoped_task_scheduler_;
834 834
835 protected: 835 protected:
836 TestNetLog net_log_; 836 TestNetLog net_log_;
837 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. 837 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
838 URLRequestJobFactoryImpl* job_factory_impl_; 838 URLRequestJobFactoryImpl* job_factory_impl_;
839 std::unique_ptr<URLRequestJobFactory> job_factory_; 839 std::unique_ptr<URLRequestJobFactory> job_factory_;
840 TestURLRequestContext default_context_; 840 TestURLRequestContext default_context_;
841 }; 841 };
842 842
843 // This NetworkDelegate is picky about what files are accessible. Only
844 // whitelisted files are allowed.
845 class PickyNetworkDelegate : public TestNetworkDelegate {
846 public:
847 // Adds |path| to the access white list.
848 void AddToWhitelist(const base::FilePath& path) {
849 whitelist_.insert(base::MakeAbsoluteFilePath(path));
850 }
851
852 bool OnCanAccessFile(const URLRequest& request,
853 const base::FilePath& original_path,
854 const base::FilePath& absolute_path) const override {
855 return whitelist_.count(absolute_path) > 0;
856 }
857
858 private:
859 std::set<base::FilePath> whitelist_;
860 };
861
843 TEST_F(URLRequestTest, AboutBlankTest) { 862 TEST_F(URLRequestTest, AboutBlankTest) {
844 TestDelegate d; 863 TestDelegate d;
845 { 864 {
846 std::unique_ptr<URLRequest> r( 865 std::unique_ptr<URLRequest> r(
847 default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, 866 default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY,
848 &d, TRAFFIC_ANNOTATION_FOR_TESTS)); 867 &d, TRAFFIC_ANNOTATION_FOR_TESTS));
849 868
850 r->Start(); 869 r->Start();
851 EXPECT_TRUE(r->is_pending()); 870 EXPECT_TRUE(r->is_pending());
852 871
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1078 r->Start(); 1097 r->Start();
1079 EXPECT_TRUE(r->is_pending()); 1098 EXPECT_TRUE(r->is_pending());
1080 1099
1081 base::RunLoop().Run(); 1100 base::RunLoop().Run();
1082 EXPECT_TRUE(d.request_failed()); 1101 EXPECT_TRUE(d.request_failed());
1083 } 1102 }
1084 1103
1085 EXPECT_TRUE(base::DeleteFile(temp_path, false)); 1104 EXPECT_TRUE(base::DeleteFile(temp_path, false));
1086 } 1105 }
1087 1106
1088 TEST_F(URLRequestTest, AllowFileURLs) { 1107 TEST_F(URLRequestTest, AllowFileURLs) {
mmenke 2017/05/10 18:03:31 Think we still need to test the original_path argu
satorux1 2017/05/12 13:24:11 Good point! My original plan was to always use abs
1089 base::ScopedTempDir temp_dir; 1108 base::ScopedTempDir temp_dir;
1090 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1109 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1091 base::FilePath test_file; 1110 base::FilePath test_file;
1092 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file)); 1111 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file));
1093 std::string test_data("monkey"); 1112 std::string test_data("monkey");
1094 base::WriteFile(test_file, test_data.data(), test_data.size()); 1113 base::WriteFile(test_file, test_data.data(), test_data.size());
1095 GURL test_file_url = FilePathToFileURL(test_file); 1114 GURL test_file_url = FilePathToFileURL(test_file);
1096
1097 { 1115 {
1098 TestDelegate d; 1116 TestDelegate d;
1099 TestNetworkDelegate network_delegate; 1117 PickyNetworkDelegate network_delegate;
1100 network_delegate.set_can_access_files(true); 1118 network_delegate.AddToWhitelist(test_file);
1101 default_context_.set_network_delegate(&network_delegate); 1119 default_context_.set_network_delegate(&network_delegate);
1102 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( 1120 std::unique_ptr<URLRequest> r(default_context_.CreateRequest(
1103 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); 1121 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS));
1104 r->Start(); 1122 r->Start();
1105 base::RunLoop().Run(); 1123 base::RunLoop().Run();
1124 // This should be allowed as the file path is white listed.
1106 EXPECT_FALSE(d.request_failed()); 1125 EXPECT_FALSE(d.request_failed());
1107 EXPECT_EQ(test_data, d.data_received()); 1126 EXPECT_EQ(test_data, d.data_received());
1108 } 1127 }
1109 1128
1110 { 1129 {
1111 TestDelegate d; 1130 TestDelegate d;
1112 TestNetworkDelegate network_delegate; 1131 PickyNetworkDelegate network_delegate;
1113 network_delegate.set_can_access_files(false);
1114 default_context_.set_network_delegate(&network_delegate); 1132 default_context_.set_network_delegate(&network_delegate);
1115 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( 1133 std::unique_ptr<URLRequest> r(default_context_.CreateRequest(
1116 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); 1134 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS));
1117 r->Start(); 1135 r->Start();
1118 base::RunLoop().Run(); 1136 base::RunLoop().Run();
1137 // This should be rejected as the file path is not white listed.
1119 EXPECT_TRUE(d.request_failed()); 1138 EXPECT_TRUE(d.request_failed());
1120 EXPECT_EQ("", d.data_received()); 1139 EXPECT_EQ("", d.data_received());
1121 } 1140 }
1122 } 1141 }
1123 1142
1143 #if defined(OS_POSIX) // Bacause of symbolic links.
1144
1145 TEST_F(URLRequestTest, SymlinksToFiles) {
1146 base::ScopedTempDir temp_dir;
1147 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1148 // The test file be added to the whitelist.
1149 base::FilePath test_file;
1150 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file));
1151 std::string test_data("monkey");
1152 base::WriteFile(test_file, test_data.data(), test_data.size());
1153
1154 // This_symlink will point to the test file.
1155 base::FilePath good_symlink = temp_dir.GetPath().AppendASCII("good_symlink");
1156 ASSERT_TRUE(base::CreateSymbolicLink(test_file, good_symlink));
1157 GURL good_file_url = FilePathToFileURL(good_symlink);
1158 // This_symlink will point to /dev/null.
1159 base::FilePath bad_symlink = temp_dir.GetPath().AppendASCII("bad_symlink");
1160 ASSERT_TRUE(base::CreateSymbolicLink(
1161 base::FilePath(FILE_PATH_LITERAL("/dev/null")), bad_symlink));
1162 GURL bad_file_url = FilePathToFileURL(bad_symlink);
1163
1164 {
1165 TestDelegate d;
1166 PickyNetworkDelegate network_delegate;
1167 network_delegate.AddToWhitelist(test_file);
1168 default_context_.set_network_delegate(&network_delegate);
1169 std::unique_ptr<URLRequest> r(
1170 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d));
1171 r->Start();
1172 base::RunLoop().Run();
1173 // good_file_url should be allowed.
1174 EXPECT_FALSE(d.request_failed());
1175 EXPECT_EQ(test_data, d.data_received());
1176 }
1177
1178 {
1179 TestDelegate d;
1180 PickyNetworkDelegate network_delegate;
1181 network_delegate.AddToWhitelist(test_file);
1182 default_context_.set_network_delegate(&network_delegate);
1183 std::unique_ptr<URLRequest> r(
1184 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d));
1185 r->Start();
1186 base::RunLoop().Run();
1187 // bad_file_url should be rejected.
1188 EXPECT_TRUE(d.request_failed());
1189 EXPECT_EQ("", d.data_received());
mmenke 2017/05/10 18:03:31 For the failure cases, can you check that d.reques
satorux1 2017/05/12 13:24:11 Good idea! Done.
1190 }
1191 }
1192
1193 TEST_F(URLRequestTest, SymlinksToDirs) {
1194 // The temporary dir will be added to the whitelist.
1195 base::ScopedTempDir temp_dir;
1196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1197
1198 // This_symlink will point to the temp dir.
1199 base::FilePath good_symlink = temp_dir.GetPath().AppendASCII("good_symlink");
1200 ASSERT_TRUE(base::CreateSymbolicLink(temp_dir.GetPath(), good_symlink));
1201 GURL good_file_url = FilePathToFileURL(good_symlink);
1202 // This_symlink will point to /dev.
1203 base::FilePath bad_symlink = temp_dir.GetPath().AppendASCII("bad_symlink");
1204 ASSERT_TRUE(base::CreateSymbolicLink(
1205 base::FilePath(FILE_PATH_LITERAL("/dev")), bad_symlink));
1206 GURL bad_file_url = FilePathToFileURL(bad_symlink);
1207
1208 {
1209 TestDelegate d;
1210 PickyNetworkDelegate network_delegate;
1211 network_delegate.AddToWhitelist(temp_dir.GetPath());
1212 default_context_.set_network_delegate(&network_delegate);
1213 std::unique_ptr<URLRequest> r(
1214 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d));
1215 r->Start();
1216 base::RunLoop().Run();
1217 // good_file_url should be allowed.
1218 EXPECT_FALSE(d.request_failed());
1219 ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos);
1220 }
1221
1222 {
1223 TestDelegate d;
1224 PickyNetworkDelegate network_delegate;
1225 network_delegate.AddToWhitelist(temp_dir.GetPath());
1226 default_context_.set_network_delegate(&network_delegate);
1227 std::unique_ptr<URLRequest> r(
1228 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d));
1229 r->Start();
1230 base::RunLoop().Run();
1231 // bad_file_url should be rejected.
1232 EXPECT_TRUE(d.request_failed());
1233 EXPECT_EQ("", d.data_received());
1234 }
1235 }
1236
1237 #endif // defined(OS_POSIX)
1124 1238
1125 TEST_F(URLRequestTest, FileDirCancelTest) { 1239 TEST_F(URLRequestTest, FileDirCancelTest) {
1126 // Put in mock resource provider. 1240 // Put in mock resource provider.
1127 NetModule::SetResourceProvider(TestNetResourceProvider); 1241 NetModule::SetResourceProvider(TestNetResourceProvider);
1128 1242
1129 TestDelegate d; 1243 TestDelegate d;
1130 { 1244 {
1131 base::FilePath file_path; 1245 base::FilePath file_path;
1132 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); 1246 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
1133 file_path = file_path.Append(FILE_PATH_LITERAL("net")); 1247 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1165 req->Start(); 1279 req->Start();
1166 base::RunLoop().Run(); 1280 base::RunLoop().Run();
1167 1281
1168 // Generate entry for the sentinel file. 1282 // Generate entry for the sentinel file.
1169 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); 1283 base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
1170 base::File::Info info; 1284 base::File::Info info;
1171 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); 1285 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
1172 EXPECT_GT(info.size, 0); 1286 EXPECT_GT(info.size, 0);
1173 std::string sentinel_output = GetDirectoryListingEntry( 1287 std::string sentinel_output = GetDirectoryListingEntry(
1174 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), 1288 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
1175 std::string(sentinel_name), 1289 std::string(sentinel_name), false /* is_dir */, info.size,
1176 false /* is_dir */, 1290
1177 info.size,
1178 info.last_modified); 1291 info.last_modified);
1179 1292
1180 ASSERT_LT(0, d.bytes_received()); 1293 ASSERT_LT(0, d.bytes_received());
1181 ASSERT_FALSE(d.request_failed()); 1294 ASSERT_FALSE(d.request_failed());
1182 EXPECT_EQ(OK, d.request_status()); 1295 EXPECT_EQ(OK, d.request_status());
1183 // Check for the entry generated for the "sentinel" file. 1296 // Check for the entry generated for the "sentinel" file.
1184 const std::string& data = d.data_received(); 1297 const std::string& data = d.data_received();
1185 ASSERT_NE(data.find(sentinel_output), std::string::npos); 1298 ASSERT_NE(data.find(sentinel_output), std::string::npos);
1186 } 1299 }
1187 1300
(...skipping 9887 matching lines...) Expand 10 before | Expand all | Expand 10 after
11075 AddTestInterceptor()->set_main_intercept_job(std::move(job)); 11188 AddTestInterceptor()->set_main_intercept_job(std::move(job));
11076 11189
11077 req->Start(); 11190 req->Start();
11078 req->Cancel(); 11191 req->Cancel();
11079 base::RunLoop().RunUntilIdle(); 11192 base::RunLoop().RunUntilIdle();
11080 EXPECT_EQ(ERR_ABORTED, d.request_status()); 11193 EXPECT_EQ(ERR_ABORTED, d.request_status());
11081 EXPECT_EQ(0, d.received_redirect_count()); 11194 EXPECT_EQ(0, d.received_redirect_count());
11082 } 11195 }
11083 11196
11084 } // namespace net 11197 } // namespace net
OLDNEW
« net/url_request/url_request_file_dir_job.cc ('K') | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698