OLD | NEW |
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 "net/test/spawned_test_server/spawner_communicator.h" | 5 #include "net/test/spawned_test_server/spawner_communicator.h" |
6 | 6 |
| 7 #include <limits> |
| 8 |
7 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
10 #include "base/supports_user_data.h" | 12 #include "base/supports_user_data.h" |
11 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
13 #include "base/values.h" | 15 #include "base/values.h" |
14 #include "build/build_config.h" | 16 #include "build/build_config.h" |
15 #include "net/base/elements_upload_data_stream.h" | 17 #include "net/base/elements_upload_data_stream.h" |
16 #include "net/base/port_util.h" | 18 #include "net/base/port_util.h" |
17 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
18 #include "net/base/upload_bytes_element_reader.h" | 20 #include "net/base/upload_bytes_element_reader.h" |
19 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
20 #include "net/url_request/url_request_test_util.h" | 22 #include "net/url_request/url_request_test_util.h" |
21 #include "url/gurl.h" | 23 #include "url/gurl.h" |
22 | 24 |
23 namespace net { | 25 namespace net { |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
27 GURL GenerateSpawnerCommandURL(const std::string& command, uint16 port) { | 29 GURL GenerateSpawnerCommandURL(const std::string& command, uint16_t port) { |
28 // Always performs HTTP request for sending command to the spawner server. | 30 // Always performs HTTP request for sending command to the spawner server. |
29 return GURL(base::StringPrintf("%s:%u/%s", "http://127.0.0.1", port, | 31 return GURL(base::StringPrintf("%s:%u/%s", "http://127.0.0.1", port, |
30 command.c_str())); | 32 command.c_str())); |
31 } | 33 } |
32 | 34 |
33 int kBufferSize = 2048; | 35 int kBufferSize = 2048; |
34 | 36 |
35 // A class to hold all data needed to send a command to spawner server. | 37 // A class to hold all data needed to send a command to spawner server. |
36 class SpawnerRequestData : public base::SupportsUserData::Data { | 38 class SpawnerRequestData : public base::SupportsUserData::Data { |
37 public: | 39 public: |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 93 |
92 // Used to track how many times the OnResponseStarted get called after | 94 // Used to track how many times the OnResponseStarted get called after |
93 // sending a command to spawner server. | 95 // sending a command to spawner server. |
94 int response_started_count_; | 96 int response_started_count_; |
95 | 97 |
96 DISALLOW_COPY_AND_ASSIGN(SpawnerRequestData); | 98 DISALLOW_COPY_AND_ASSIGN(SpawnerRequestData); |
97 }; | 99 }; |
98 | 100 |
99 } // namespace | 101 } // namespace |
100 | 102 |
101 SpawnerCommunicator::SpawnerCommunicator(uint16 port) | 103 SpawnerCommunicator::SpawnerCommunicator(uint16_t port) |
102 : io_thread_("spawner_communicator"), | 104 : io_thread_("spawner_communicator"), |
103 event_(false, false), | 105 event_(false, false), |
104 port_(port), | 106 port_(port), |
105 next_id_(0), | 107 next_id_(0), |
106 is_running_(false), | 108 is_running_(false), |
107 weak_factory_(this) {} | 109 weak_factory_(this) {} |
108 | 110 |
109 SpawnerCommunicator::~SpawnerCommunicator() { | 111 SpawnerCommunicator::~SpawnerCommunicator() { |
110 DCHECK(!is_running_); | 112 DCHECK(!is_running_); |
111 } | 113 } |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 | 319 |
318 if (data->ConsumeBytesRead(num_bytes)) { | 320 if (data->ConsumeBytesRead(num_bytes)) { |
319 // Keep reading. | 321 // Keep reading. |
320 ReadResult(request); | 322 ReadResult(request); |
321 } else { | 323 } else { |
322 OnSpawnerCommandCompleted(request); | 324 OnSpawnerCommandCompleted(request); |
323 } | 325 } |
324 } | 326 } |
325 | 327 |
326 bool SpawnerCommunicator::StartServer(const std::string& arguments, | 328 bool SpawnerCommunicator::StartServer(const std::string& arguments, |
327 uint16* port) { | 329 uint16_t* port) { |
328 *port = 0; | 330 *port = 0; |
329 // Send the start command to spawner server to start the Python test server | 331 // Send the start command to spawner server to start the Python test server |
330 // on remote machine. | 332 // on remote machine. |
331 std::string server_return_data; | 333 std::string server_return_data; |
332 int result_code; | 334 int result_code; |
333 SendCommandAndWaitForResult("start", arguments, &result_code, | 335 SendCommandAndWaitForResult("start", arguments, &result_code, |
334 &server_return_data); | 336 &server_return_data); |
335 if (OK != result_code || server_return_data.empty()) | 337 if (OK != result_code || server_return_data.empty()) |
336 return false; | 338 return false; |
337 | 339 |
338 // Check whether the data returned from spawner server is JSON-formatted. | 340 // Check whether the data returned from spawner server is JSON-formatted. |
339 scoped_ptr<base::Value> value = base::JSONReader::Read(server_return_data); | 341 scoped_ptr<base::Value> value = base::JSONReader::Read(server_return_data); |
340 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) { | 342 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
341 LOG(ERROR) << "Invalid server data: " << server_return_data.c_str(); | 343 LOG(ERROR) << "Invalid server data: " << server_return_data.c_str(); |
342 return false; | 344 return false; |
343 } | 345 } |
344 | 346 |
345 // Check whether spawner server returns valid data. | 347 // Check whether spawner server returns valid data. |
346 base::DictionaryValue* server_data = | 348 base::DictionaryValue* server_data = |
347 static_cast<base::DictionaryValue*>(value.get()); | 349 static_cast<base::DictionaryValue*>(value.get()); |
348 std::string message; | 350 std::string message; |
349 if (!server_data->GetString("message", &message) || message != "started") { | 351 if (!server_data->GetString("message", &message) || message != "started") { |
350 LOG(ERROR) << "Invalid message in server data: "; | 352 LOG(ERROR) << "Invalid message in server data: "; |
351 return false; | 353 return false; |
352 } | 354 } |
353 int int_port; | 355 int int_port; |
354 if (!server_data->GetInteger("port", &int_port) || int_port <= 0 || | 356 if (!server_data->GetInteger("port", &int_port) || int_port <= 0 || |
355 int_port > kuint16max) { | 357 int_port > std::numeric_limits<uint16_t>::max()) { |
356 LOG(ERROR) << "Invalid port value: " << int_port; | 358 LOG(ERROR) << "Invalid port value: " << int_port; |
357 return false; | 359 return false; |
358 } | 360 } |
359 *port = static_cast<uint16>(int_port); | 361 *port = static_cast<uint16_t>(int_port); |
360 return true; | 362 return true; |
361 } | 363 } |
362 | 364 |
363 bool SpawnerCommunicator::StopServer() { | 365 bool SpawnerCommunicator::StopServer() { |
364 // It's OK to stop the SpawnerCommunicator without starting it. Some tests | 366 // It's OK to stop the SpawnerCommunicator without starting it. Some tests |
365 // have test server on their test fixture but do not actually use it. | 367 // have test server on their test fixture but do not actually use it. |
366 if (!is_running_) | 368 if (!is_running_) |
367 return true; | 369 return true; |
368 | 370 |
369 // When the test is done, ask the test server spawner to kill the test server | 371 // When the test is done, ask the test server spawner to kill the test server |
370 // on the remote machine. | 372 // on the remote machine. |
371 std::string server_return_data; | 373 std::string server_return_data; |
372 int result_code; | 374 int result_code; |
373 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); | 375 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); |
374 Shutdown(); | 376 Shutdown(); |
375 if (OK != result_code || server_return_data != "killed") | 377 if (OK != result_code || server_return_data != "killed") |
376 return false; | 378 return false; |
377 return true; | 379 return true; |
378 } | 380 } |
379 | 381 |
380 } // namespace net | 382 } // namespace net |
OLD | NEW |