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

Unified Diff: net/test/embedded_test_server/embedded_test_server.cc

Issue 2562893002: Adds ScopedPortExceptions to EmbeddedTestServer. (Closed)
Patch Set: Another approach Created 4 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/test/embedded_test_server/embedded_test_server.cc
diff --git a/net/test/embedded_test_server/embedded_test_server.cc b/net/test/embedded_test_server/embedded_test_server.cc
index 3d57df5ed6a28d8638d7761bde540c5f2c8789a1..0f89bd9c9e4ad8aec00628a3a8ea2bdc849abddc 100644
--- a/net/test/embedded_test_server/embedded_test_server.cc
+++ b/net/test/embedded_test_server/embedded_test_server.cc
@@ -23,6 +23,7 @@
#include "crypto/rsa_private_key.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
+#include "net/base/port_util.h"
#include "net/cert/pem_tokenizer.h"
#include "net/cert/test_root_certs.h"
#include "net/log/net_log_source.h"
@@ -92,21 +93,38 @@ bool EmbeddedTestServer::Start() {
bool EmbeddedTestServer::InitializeAndListen() {
DCHECK(!Started());
- listen_socket_.reset(new TCPServerSocket(nullptr, NetLogSource()));
+ const int max_tries = 5;
+ int num_tries = 0;
+ bool is_valid_port = false;
- int result = listen_socket_->ListenWithAddressAndPort("127.0.0.1", 0, 10);
- if (result) {
- LOG(ERROR) << "Listen failed: " << ErrorToString(result);
- listen_socket_.reset();
- return false;
- }
+ do {
+ if (++num_tries > max_tries) {
+ LOG(ERROR) << "Failed to listen on a valid port after " << max_tries
+ << " attempts.";
+ listen_socket_.reset();
+ return false;
+ }
- result = listen_socket_->GetLocalAddress(&local_endpoint_);
- if (result != OK) {
- LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result);
- listen_socket_.reset();
- return false;
- }
+ listen_socket_.reset(new TCPServerSocket(nullptr, NetLogSource()));
+
+ int result = listen_socket_->ListenWithAddressAndPort("127.0.0.1", 0, 10);
+ if (result) {
+ LOG(ERROR) << "Listen failed: " << ErrorToString(result);
+ listen_socket_.reset();
+ return false;
+ }
+
+ result = listen_socket_->GetLocalAddress(&local_endpoint_);
+ if (result != OK) {
+ LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result);
+ listen_socket_.reset();
+ return false;
+ }
+
+ port_ = local_endpoint_.port();
+ is_valid_port |= net::IsPortAllowedForScheme(
+ port_, is_using_ssl_ ? url::kHttpsScheme : url::kHttpScheme);
+ } while (!is_valid_port);
if (is_using_ssl_) {
base_url_ = GURL("https://" + local_endpoint_.ToString());
@@ -117,7 +135,6 @@ bool EmbeddedTestServer::InitializeAndListen() {
} else {
base_url_ = GURL("http://" + local_endpoint_.ToString());
}
- port_ = local_endpoint_.port();
listen_socket_->DetachFromThread();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698