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

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