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

Unified Diff: net/server/http_server_unittest.cc

Issue 2648553002: HttpServer::ParseHeaders: don't DCHECK on bogus headers termination (Closed)
Patch Set: More refinements based on review feedback. Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/server/http_server.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/server/http_server_unittest.cc
diff --git a/net/server/http_server_unittest.cc b/net/server/http_server_unittest.cc
index 9b6dee1bea9d3f0c2b1b179044eae736cf97294f..0875a5a9be36ac5ad38fcf4e28e2d64c22dd2a9b 100644
--- a/net/server/http_server_unittest.cc
+++ b/net/server/http_server_unittest.cc
@@ -133,6 +133,17 @@ class TestHttpClient {
return true;
}
+ void ExpectUsedThenDisconnectedWithNoData() {
+ // Check that the socket was opened...
+ ASSERT_TRUE(socket_->WasEverUsed());
+
+ // ...then closed when the server disconnected. Verify that the socket was
+ // closed by checking that a Read() fails.
+ std::string response;
+ ASSERT_FALSE(Read(&response, 1u));
+ ASSERT_TRUE(response.empty());
+ }
+
TCPClientSocket& socket() { return *socket_; }
private:
@@ -191,7 +202,8 @@ class TestHttpClient {
class HttpServerTest : public testing::Test,
public HttpServer::Delegate {
public:
- HttpServerTest() : quit_after_request_count_(0) {}
+ HttpServerTest()
+ : quit_after_request_count_(0), quit_on_close_connection_(-1) {}
void SetUp() override {
std::unique_ptr<ServerSocket> server_socket(
@@ -201,6 +213,12 @@ class HttpServerTest : public testing::Test,
ASSERT_THAT(server_->GetLocalAddress(&server_address_), IsOk());
}
+ void TearDown() override {
+ // Run the event loop some to make sure that the memory handed over to
+ // DeleteSoon gets fully freed.
+ base::RunLoop().RunUntilIdle();
+ }
+
void OnConnect(int connection_id) override {
DCHECK(connection_map_.find(connection_id) == connection_map_.end());
connection_map_[connection_id] = true;
@@ -225,6 +243,8 @@ class HttpServerTest : public testing::Test,
void OnClose(int connection_id) override {
DCHECK(connection_map_.find(connection_id) != connection_map_.end());
connection_map_[connection_id] = false;
+ if (connection_id == quit_on_close_connection_)
+ run_loop_quit_func_.Run();
}
bool RunUntilRequestsReceived(size_t count) {
@@ -239,10 +259,27 @@ class HttpServerTest : public testing::Test,
return success;
}
+ bool RunUntilConnectionIdClosed(int connection_id) {
+ quit_on_close_connection_ = connection_id;
+ auto iter = connection_map_.find(connection_id);
+ if (iter != connection_map_.end() && !iter->second) {
+ // Already disconnected.
+ return true;
+ }
+
+ base::RunLoop run_loop;
+ run_loop_quit_func_ = run_loop.QuitClosure();
+ bool success = RunLoopWithTimeout(&run_loop);
+ run_loop_quit_func_.Reset();
+ return success;
+ }
+
HttpServerRequestInfo GetRequest(size_t request_index) {
return requests_[request_index].first;
}
+ size_t num_requests() const { return requests_.size(); }
+
int GetConnectionId(size_t request_index) {
return requests_[request_index].second;
}
@@ -264,6 +301,7 @@ class HttpServerTest : public testing::Test,
private:
size_t quit_after_request_count_;
+ int quit_on_close_connection_;
};
namespace {
@@ -296,6 +334,15 @@ TEST_F(HttpServerTest, Request) {
base::CompareCase::SENSITIVE));
}
+TEST_F(HttpServerTest, RequestBrokenTermination) {
+ TestHttpClient client;
+ ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk());
+ client.Send("GET /test HTTP/1.1\r\n\r)");
+ ASSERT_TRUE(RunUntilConnectionIdClosed(1));
+ EXPECT_EQ(0u, num_requests());
+ client.ExpectUsedThenDisconnectedWithNoData();
+}
+
TEST_F(HttpServerTest, RequestWithHeaders) {
TestHttpClient client;
ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk());
@@ -503,14 +550,7 @@ TEST_F(HttpServerTest, WrongProtocolRequest) {
ASSERT_EQ(1u, connection_map().size());
ASSERT_FALSE(connection_map().begin()->second);
- // Assert that the socket was opened...
- ASSERT_TRUE(client.socket().WasEverUsed());
-
- // ...then closed when the server disconnected. Verify that the socket was
- // closed by checking that a Read() fails.
- std::string response;
- ASSERT_FALSE(client.Read(&response, 1u));
- ASSERT_EQ(std::string(), response);
+ client.ExpectUsedThenDisconnectedWithNoData();
// Reset the state of the connection map.
connection_map().clear();
« no previous file with comments | « net/server/http_server.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698