Commit a501df83 authored by rohitrao's avatar rohitrao Committed by Commit bot

Retry until EmbeddedTestServer is listening on a valid port.

The net stack will not send HTTP requests to a specific set of
blacklisted ports, but the OS may assign one of	those ports to
the EmbeddedTestServer.  This was causing spurious net_unittests
failures on iOS when it happened.  This CL modifies the
EmbeddedTestServer to retry until it is assigned a
non-blacklisted port.

BUG=607630

Review-Url: https://codereview.chromium.org/2562893002
Cr-Commit-Position: refs/heads/master@{#437602}
parent 4e23c36f
......@@ -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,6 +93,18 @@ bool EmbeddedTestServer::Start() {
bool EmbeddedTestServer::InitializeAndListen() {
DCHECK(!Started());
const int max_tries = 5;
int num_tries = 0;
bool is_valid_port = 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;
}
listen_socket_.reset(new TCPServerSocket(nullptr, NetLogSource()));
int result = listen_socket_->ListenWithAddressAndPort("127.0.0.1", 0, 10);
......@@ -108,6 +121,11 @@ bool EmbeddedTestServer::InitializeAndListen() {
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());
if (cert_ == CERT_MISMATCHED_NAME || cert_ == CERT_COMMON_NAME_IS_DOMAIN) {
......@@ -117,7 +135,6 @@ bool EmbeddedTestServer::InitializeAndListen() {
} else {
base_url_ = GURL("http://" + local_endpoint_.ToString());
}
port_ = local_endpoint_.port();
listen_socket_->DetachFromThread();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment