You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

536 lines
15 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/gunit.h"
  28. #include "talk/base/httpbase.h"
  29. #include "talk/base/testutils.h"
  30. namespace talk_base {
  31. const char* const kHttpResponse =
  32. "HTTP/1.1 200\r\n"
  33. "Connection: Keep-Alive\r\n"
  34. "Content-Type: text/plain\r\n"
  35. "Proxy-Authorization: 42\r\n"
  36. "Transfer-Encoding: chunked\r\n"
  37. "\r\n"
  38. "00000008\r\n"
  39. "Goodbye!\r\n"
  40. "0\r\n\r\n";
  41. const char* const kHttpEmptyResponse =
  42. "HTTP/1.1 200\r\n"
  43. "Connection: Keep-Alive\r\n"
  44. "Content-Length: 0\r\n"
  45. "Proxy-Authorization: 42\r\n"
  46. "\r\n";
  47. const char* const kHttpResponsePrefix =
  48. "HTTP/1.1 200\r\n"
  49. "Connection: Keep-Alive\r\n"
  50. "Content-Type: text/plain\r\n"
  51. "Proxy-Authorization: 42\r\n"
  52. "Transfer-Encoding: chunked\r\n"
  53. "\r\n"
  54. "8\r\n"
  55. "Goodbye!\r\n";
  56. class HttpBaseTest : public testing::Test, public IHttpNotify {
  57. public:
  58. enum EventType { E_HEADER_COMPLETE, E_COMPLETE, E_CLOSED };
  59. struct Event {
  60. EventType event;
  61. bool chunked;
  62. size_t data_size;
  63. HttpMode mode;
  64. HttpError err;
  65. };
  66. HttpBaseTest() : mem(NULL), obtain_stream(false), http_stream(NULL) { }
  67. virtual void SetUp() { }
  68. virtual void TearDown() {
  69. // Avoid an ASSERT, in case a test doesn't clean up properly
  70. base.abort(HE_NONE);
  71. }
  72. virtual HttpError onHttpHeaderComplete(bool chunked, size_t& data_size) {
  73. LOG_F(LS_VERBOSE) << "chunked: " << chunked << " size: " << data_size;
  74. Event e = { E_HEADER_COMPLETE, chunked, data_size, HM_NONE, HE_NONE};
  75. events.push_back(e);
  76. if (obtain_stream) {
  77. ObtainDocumentStream();
  78. }
  79. return HE_NONE;
  80. }
  81. virtual void onHttpComplete(HttpMode mode, HttpError err) {
  82. LOG_F(LS_VERBOSE) << "mode: " << mode << " err: " << err;
  83. Event e = { E_COMPLETE, false, 0, mode, err };
  84. events.push_back(e);
  85. }
  86. virtual void onHttpClosed(HttpError err) {
  87. LOG_F(LS_VERBOSE) << "err: " << err;
  88. Event e = { E_CLOSED, false, 0, HM_NONE, err };
  89. events.push_back(e);
  90. }
  91. void SetupSource(const char* response);
  92. void VerifyHeaderComplete(size_t event_count, bool empty_doc);
  93. void VerifyDocumentContents(const char* expected_data,
  94. size_t expected_length = SIZE_UNKNOWN);
  95. void ObtainDocumentStream();
  96. void VerifyDocumentStreamIsOpening();
  97. void VerifyDocumentStreamOpenEvent();
  98. void ReadDocumentStreamData(const char* expected_data);
  99. void VerifyDocumentStreamIsEOS();
  100. void SetupDocument(const char* response);
  101. void VerifySourceContents(const char* expected_data,
  102. size_t expected_length = SIZE_UNKNOWN);
  103. void VerifyTransferComplete(HttpMode mode, HttpError error);
  104. HttpBase base;
  105. MemoryStream* mem;
  106. HttpResponseData data;
  107. // The source of http data, and source events
  108. testing::StreamSource src;
  109. std::vector<Event> events;
  110. // Document stream, and stream events
  111. bool obtain_stream;
  112. StreamInterface* http_stream;
  113. testing::StreamSink sink;
  114. };
  115. void HttpBaseTest::SetupSource(const char* http_data) {
  116. LOG_F(LS_VERBOSE) << "Enter";
  117. src.SetState(SS_OPENING);
  118. src.QueueString(http_data);
  119. base.notify(this);
  120. base.attach(&src);
  121. EXPECT_TRUE(events.empty());
  122. src.SetState(SS_OPEN);
  123. ASSERT_EQ(1U, events.size());
  124. EXPECT_EQ(E_COMPLETE, events[0].event);
  125. EXPECT_EQ(HM_CONNECT, events[0].mode);
  126. EXPECT_EQ(HE_NONE, events[0].err);
  127. events.clear();
  128. mem = new MemoryStream;
  129. data.document.reset(mem);
  130. LOG_F(LS_VERBOSE) << "Exit";
  131. }
  132. void HttpBaseTest::VerifyHeaderComplete(size_t event_count, bool empty_doc) {
  133. LOG_F(LS_VERBOSE) << "Enter";
  134. ASSERT_EQ(event_count, events.size());
  135. EXPECT_EQ(E_HEADER_COMPLETE, events[0].event);
  136. std::string header;
  137. EXPECT_EQ(HVER_1_1, data.version);
  138. EXPECT_EQ(static_cast<uint32>(HC_OK), data.scode);
  139. EXPECT_TRUE(data.hasHeader(HH_PROXY_AUTHORIZATION, &header));
  140. EXPECT_EQ("42", header);
  141. EXPECT_TRUE(data.hasHeader(HH_CONNECTION, &header));
  142. EXPECT_EQ("Keep-Alive", header);
  143. if (empty_doc) {
  144. EXPECT_FALSE(events[0].chunked);
  145. EXPECT_EQ(0U, events[0].data_size);
  146. EXPECT_TRUE(data.hasHeader(HH_CONTENT_LENGTH, &header));
  147. EXPECT_EQ("0", header);
  148. } else {
  149. EXPECT_TRUE(events[0].chunked);
  150. EXPECT_EQ(SIZE_UNKNOWN, events[0].data_size);
  151. EXPECT_TRUE(data.hasHeader(HH_CONTENT_TYPE, &header));
  152. EXPECT_EQ("text/plain", header);
  153. EXPECT_TRUE(data.hasHeader(HH_TRANSFER_ENCODING, &header));
  154. EXPECT_EQ("chunked", header);
  155. }
  156. LOG_F(LS_VERBOSE) << "Exit";
  157. }
  158. void HttpBaseTest::VerifyDocumentContents(const char* expected_data,
  159. size_t expected_length) {
  160. LOG_F(LS_VERBOSE) << "Enter";
  161. if (SIZE_UNKNOWN == expected_length) {
  162. expected_length = strlen(expected_data);
  163. }
  164. EXPECT_EQ(mem, data.document.get());
  165. size_t length;
  166. mem->GetSize(&length);
  167. EXPECT_EQ(expected_length, length);
  168. EXPECT_TRUE(0 == memcmp(expected_data, mem->GetBuffer(), length));
  169. LOG_F(LS_VERBOSE) << "Exit";
  170. }
  171. void HttpBaseTest::ObtainDocumentStream() {
  172. LOG_F(LS_VERBOSE) << "Enter";
  173. EXPECT_FALSE(http_stream);
  174. http_stream = base.GetDocumentStream();
  175. ASSERT_TRUE(NULL != http_stream);
  176. sink.Monitor(http_stream);
  177. LOG_F(LS_VERBOSE) << "Exit";
  178. }
  179. void HttpBaseTest::VerifyDocumentStreamIsOpening() {
  180. LOG_F(LS_VERBOSE) << "Enter";
  181. ASSERT_TRUE(NULL != http_stream);
  182. EXPECT_EQ(0, sink.Events(http_stream));
  183. EXPECT_EQ(SS_OPENING, http_stream->GetState());
  184. size_t read = 0;
  185. char buffer[5] = { 0 };
  186. EXPECT_EQ(SR_BLOCK, http_stream->Read(buffer, sizeof(buffer), &read, NULL));
  187. LOG_F(LS_VERBOSE) << "Exit";
  188. }
  189. void HttpBaseTest::VerifyDocumentStreamOpenEvent() {
  190. LOG_F(LS_VERBOSE) << "Enter";
  191. ASSERT_TRUE(NULL != http_stream);
  192. EXPECT_EQ(SE_OPEN | SE_READ, sink.Events(http_stream));
  193. EXPECT_EQ(SS_OPEN, http_stream->GetState());
  194. // HTTP headers haven't arrived yet
  195. EXPECT_EQ(0U, events.size());
  196. EXPECT_EQ(static_cast<uint32>(HC_INTERNAL_SERVER_ERROR), data.scode);
  197. LOG_F(LS_VERBOSE) << "Exit";
  198. }
  199. void HttpBaseTest::ReadDocumentStreamData(const char* expected_data) {
  200. LOG_F(LS_VERBOSE) << "Enter";
  201. ASSERT_TRUE(NULL != http_stream);
  202. EXPECT_EQ(SS_OPEN, http_stream->GetState());
  203. // Pump the HTTP I/O using Read, and verify the results.
  204. size_t verified_length = 0;
  205. const size_t expected_length = strlen(expected_data);
  206. while (verified_length < expected_length) {
  207. size_t read = 0;
  208. char buffer[5] = { 0 };
  209. size_t amt_to_read = _min(expected_length - verified_length, sizeof(buffer));
  210. EXPECT_EQ(SR_SUCCESS, http_stream->Read(buffer, amt_to_read, &read, NULL));
  211. EXPECT_EQ(amt_to_read, read);
  212. EXPECT_TRUE(0 == memcmp(expected_data + verified_length, buffer, read));
  213. verified_length += read;
  214. }
  215. LOG_F(LS_VERBOSE) << "Exit";
  216. }
  217. void HttpBaseTest::VerifyDocumentStreamIsEOS() {
  218. LOG_F(LS_VERBOSE) << "Enter";
  219. ASSERT_TRUE(NULL != http_stream);
  220. size_t read = 0;
  221. char buffer[5] = { 0 };
  222. EXPECT_EQ(SR_EOS, http_stream->Read(buffer, sizeof(buffer), &read, NULL));
  223. EXPECT_EQ(SS_CLOSED, http_stream->GetState());
  224. // When EOS is caused by Read, we don't expect SE_CLOSE
  225. EXPECT_EQ(0, sink.Events(http_stream));
  226. LOG_F(LS_VERBOSE) << "Exit";
  227. }
  228. void HttpBaseTest::SetupDocument(const char* document_data) {
  229. LOG_F(LS_VERBOSE) << "Enter";
  230. src.SetState(SS_OPEN);
  231. base.notify(this);
  232. base.attach(&src);
  233. EXPECT_TRUE(events.empty());
  234. if (document_data) {
  235. // Note: we could just call data.set_success("text/plain", mem), but that
  236. // won't allow us to use the chunked transfer encoding.
  237. mem = new MemoryStream(document_data);
  238. data.document.reset(mem);
  239. data.setHeader(HH_CONTENT_TYPE, "text/plain");
  240. data.setHeader(HH_TRANSFER_ENCODING, "chunked");
  241. } else {
  242. data.setHeader(HH_CONTENT_LENGTH, "0");
  243. }
  244. data.scode = HC_OK;
  245. data.setHeader(HH_PROXY_AUTHORIZATION, "42");
  246. data.setHeader(HH_CONNECTION, "Keep-Alive");
  247. LOG_F(LS_VERBOSE) << "Exit";
  248. }
  249. void HttpBaseTest::VerifySourceContents(const char* expected_data,
  250. size_t expected_length) {
  251. LOG_F(LS_VERBOSE) << "Enter";
  252. if (SIZE_UNKNOWN == expected_length) {
  253. expected_length = strlen(expected_data);
  254. }
  255. std::string contents = src.ReadData();
  256. EXPECT_EQ(expected_length, contents.length());
  257. EXPECT_TRUE(0 == memcmp(expected_data, contents.data(), expected_length));
  258. LOG_F(LS_VERBOSE) << "Exit";
  259. }
  260. void HttpBaseTest::VerifyTransferComplete(HttpMode mode, HttpError error) {
  261. LOG_F(LS_VERBOSE) << "Enter";
  262. // Verify that http operation has completed
  263. ASSERT_TRUE(events.size() > 0);
  264. size_t last_event = events.size() - 1;
  265. EXPECT_EQ(E_COMPLETE, events[last_event].event);
  266. EXPECT_EQ(mode, events[last_event].mode);
  267. EXPECT_EQ(error, events[last_event].err);
  268. LOG_F(LS_VERBOSE) << "Exit";
  269. }
  270. //
  271. // Tests
  272. //
  273. TEST_F(HttpBaseTest, SupportsSend) {
  274. // Queue response document
  275. SetupDocument("Goodbye!");
  276. // Begin send
  277. base.send(&data);
  278. // Send completed successfully
  279. VerifyTransferComplete(HM_SEND, HE_NONE);
  280. VerifySourceContents(kHttpResponse);
  281. }
  282. TEST_F(HttpBaseTest, SupportsSendNoDocument) {
  283. // Queue response document
  284. SetupDocument(NULL);
  285. // Begin send
  286. base.send(&data);
  287. // Send completed successfully
  288. VerifyTransferComplete(HM_SEND, HE_NONE);
  289. VerifySourceContents(kHttpEmptyResponse);
  290. }
  291. TEST_F(HttpBaseTest, SignalsCompleteOnInterruptedSend) {
  292. // This test is attempting to expose a bug that occurs when a particular
  293. // base objects is used for receiving, and then used for sending. In
  294. // particular, the HttpParser state is different after receiving. Simulate
  295. // that here.
  296. SetupSource(kHttpResponse);
  297. base.recv(&data);
  298. VerifyTransferComplete(HM_RECV, HE_NONE);
  299. src.Clear();
  300. data.clear(true);
  301. events.clear();
  302. base.detach();
  303. // Queue response document
  304. SetupDocument("Goodbye!");
  305. // Prevent entire response from being sent
  306. const size_t kInterruptedLength = strlen(kHttpResponse) - 1;
  307. src.SetWriteBlock(kInterruptedLength);
  308. // Begin send
  309. base.send(&data);
  310. // Document is mostly complete, but no completion signal yet.
  311. EXPECT_TRUE(events.empty());
  312. VerifySourceContents(kHttpResponse, kInterruptedLength);
  313. src.SetState(SS_CLOSED);
  314. // Send completed with disconnect error, and no additional data.
  315. VerifyTransferComplete(HM_SEND, HE_DISCONNECTED);
  316. EXPECT_TRUE(src.ReadData().empty());
  317. }
  318. TEST_F(HttpBaseTest, SupportsReceiveViaDocumentPush) {
  319. // Queue response document
  320. SetupSource(kHttpResponse);
  321. // Begin receive
  322. base.recv(&data);
  323. // Document completed successfully
  324. VerifyHeaderComplete(2, false);
  325. VerifyTransferComplete(HM_RECV, HE_NONE);
  326. VerifyDocumentContents("Goodbye!");
  327. }
  328. TEST_F(HttpBaseTest, SupportsReceiveViaStreamPull) {
  329. // Switch to pull mode
  330. ObtainDocumentStream();
  331. VerifyDocumentStreamIsOpening();
  332. // Queue response document
  333. SetupSource(kHttpResponse);
  334. VerifyDocumentStreamIsOpening();
  335. // Begin receive
  336. base.recv(&data);
  337. // Pull document data
  338. VerifyDocumentStreamOpenEvent();
  339. ReadDocumentStreamData("Goodbye!");
  340. VerifyDocumentStreamIsEOS();
  341. // Document completed successfully
  342. VerifyHeaderComplete(2, false);
  343. VerifyTransferComplete(HM_RECV, HE_NONE);
  344. VerifyDocumentContents("");
  345. }
  346. TEST_F(HttpBaseTest, DISABLED_AllowsCloseStreamBeforeDocumentIsComplete) {
  347. // TODO: Remove extra logging once test failure is understood
  348. int old_sev = talk_base::LogMessage::GetLogToDebug();
  349. talk_base::LogMessage::LogToDebug(LS_VERBOSE);
  350. // Switch to pull mode
  351. ObtainDocumentStream();
  352. VerifyDocumentStreamIsOpening();
  353. // Queue response document
  354. SetupSource(kHttpResponse);
  355. VerifyDocumentStreamIsOpening();
  356. // Begin receive
  357. base.recv(&data);
  358. // Pull some of the data
  359. VerifyDocumentStreamOpenEvent();
  360. ReadDocumentStreamData("Goodb");
  361. // We've seen the header by now
  362. VerifyHeaderComplete(1, false);
  363. // Close the pull stream, this will transition back to push I/O.
  364. http_stream->Close();
  365. Thread::Current()->ProcessMessages(0);
  366. // Remainder of document completed successfully
  367. VerifyTransferComplete(HM_RECV, HE_NONE);
  368. VerifyDocumentContents("ye!");
  369. talk_base::LogMessage::LogToDebug(old_sev);
  370. }
  371. TEST_F(HttpBaseTest, AllowsGetDocumentStreamInResponseToHttpHeader) {
  372. // Queue response document
  373. SetupSource(kHttpResponse);
  374. // Switch to pull mode in response to header arrival
  375. obtain_stream = true;
  376. // Begin receive
  377. base.recv(&data);
  378. // We've already seen the header, but not data has arrived
  379. VerifyHeaderComplete(1, false);
  380. VerifyDocumentContents("");
  381. // Pull the document data
  382. ReadDocumentStreamData("Goodbye!");
  383. VerifyDocumentStreamIsEOS();
  384. // Document completed successfully
  385. VerifyTransferComplete(HM_RECV, HE_NONE);
  386. VerifyDocumentContents("");
  387. }
  388. TEST_F(HttpBaseTest, AllowsGetDocumentStreamWithEmptyDocumentBody) {
  389. // Queue empty response document
  390. SetupSource(kHttpEmptyResponse);
  391. // Switch to pull mode in response to header arrival
  392. obtain_stream = true;
  393. // Begin receive
  394. base.recv(&data);
  395. // We've already seen the header, but not data has arrived
  396. VerifyHeaderComplete(1, true);
  397. VerifyDocumentContents("");
  398. // The document is still open, until we attempt to read
  399. ASSERT_TRUE(NULL != http_stream);
  400. EXPECT_EQ(SS_OPEN, http_stream->GetState());
  401. // Attempt to read data, and discover EOS
  402. VerifyDocumentStreamIsEOS();
  403. // Document completed successfully
  404. VerifyTransferComplete(HM_RECV, HE_NONE);
  405. VerifyDocumentContents("");
  406. }
  407. TEST_F(HttpBaseTest, SignalsDocumentStreamCloseOnUnexpectedClose) {
  408. // Switch to pull mode
  409. ObtainDocumentStream();
  410. VerifyDocumentStreamIsOpening();
  411. // Queue response document
  412. SetupSource(kHttpResponsePrefix);
  413. VerifyDocumentStreamIsOpening();
  414. // Begin receive
  415. base.recv(&data);
  416. // Pull document data
  417. VerifyDocumentStreamOpenEvent();
  418. ReadDocumentStreamData("Goodbye!");
  419. // Simulate unexpected close
  420. src.SetState(SS_CLOSED);
  421. // Observe error event on document stream
  422. EXPECT_EQ(testing::SSE_ERROR, sink.Events(http_stream));
  423. // Future reads give an error
  424. int error = 0;
  425. char buffer[5] = { 0 };
  426. EXPECT_EQ(SR_ERROR, http_stream->Read(buffer, sizeof(buffer), NULL, &error));
  427. EXPECT_EQ(HE_DISCONNECTED, error);
  428. // Document completed with error
  429. VerifyHeaderComplete(2, false);
  430. VerifyTransferComplete(HM_RECV, HE_DISCONNECTED);
  431. VerifyDocumentContents("");
  432. }
  433. } // namespace talk_base