| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #import <Cronet/Cronet.h> | |
| 6 | |
| 7 #include <stdint.h> | 5 #include <stdint.h> |
| 8 #include <list> | 6 #include <list> |
| 9 #include <map> | 7 #include <map> |
| 10 #include <string> | 8 #include <string> |
| 11 #include <Cronet/cronet_c_for_grpc.h> | |
| 12 | 9 |
| 13 #include "base/at_exit.h" | |
| 14 #include "base/logging.h" | 10 #include "base/logging.h" |
| 15 #include "base/mac/scoped_nsobject.h" | |
| 16 #include "base/macros.h" | 11 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 18 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 19 #include "base/strings/sys_string_conversions.h" | |
| 20 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 21 #include "components/cronet/ios/test/quic_test_server.h" | 15 #include "components/grpc_support/include/bidirectional_stream_c.h" |
| 22 #include "net/base/mac/url_conversions.h" | 16 #include "components/grpc_support/test/get_stream_engine.h" |
| 17 #include "components/grpc_support/test/quic_test_server.h" |
| 23 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 24 #include "net/test/test_data_directory.h" | 19 #include "net/test/test_data_directory.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "testing/gtest_mac.h" | |
| 27 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 28 | 22 |
| 29 namespace { | 23 namespace { |
| 30 | 24 |
| 31 cronet_bidirectional_stream_header kTestHeaders[] = { | 25 bidirectional_stream_header kTestHeaders[] = { |
| 32 {"header1", "foo"}, | 26 {"header1", "foo"}, |
| 33 {"header2", "bar"}, | 27 {"header2", "bar"}, |
| 34 }; | 28 }; |
| 35 const cronet_bidirectional_stream_header_array kTestHeadersArray = { | 29 const bidirectional_stream_header_array kTestHeadersArray = {2, 2, |
| 36 2, 2, kTestHeaders}; | 30 kTestHeaders}; |
| 37 } // namespace | 31 } // namespace |
| 38 | 32 |
| 39 namespace cronet { | 33 namespace grpc_support { |
| 40 | 34 |
| 41 base::AtExitManager* g_at_exit_ = nullptr; | 35 class BidirectionalStreamTest : public ::testing::TestWithParam<bool> { |
| 42 | |
| 43 void StartCronetIfNecessary() { | |
| 44 static bool initialized = false; | |
| 45 if (!initialized) { | |
| 46 initialized = true; | |
| 47 [Cronet setUserAgent:@"CronetTest/1.0.0.0" partial:NO]; | |
| 48 [Cronet setHttp2Enabled:true]; | |
| 49 [Cronet setQuicEnabled:true]; | |
| 50 [Cronet setSslKeyLogFileName:@"SSLKEYLOGFILE"]; | |
| 51 | |
| 52 [Cronet addQuicHint:@"test.example.com" | |
| 53 port:cronet::kTestServerPort | |
| 54 altPort:cronet::kTestServerPort]; | |
| 55 [Cronet enableTestCertVerifierForTesting]; | |
| 56 [Cronet | |
| 57 setHostResolverRulesForTesting:@"MAP test.example.com 127.0.0.1," | |
| 58 "MAP notfound.example.com ~NOTFOUND"]; | |
| 59 [Cronet start]; | |
| 60 | |
| 61 // This method must be called once from the main thread. | |
| 62 if (!g_at_exit_) | |
| 63 g_at_exit_ = new base::AtExitManager; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 class CronetBidirectionalStreamTest : public ::testing::TestWithParam<bool> { | |
| 68 protected: | 36 protected: |
| 69 CronetBidirectionalStreamTest() {} | 37 BidirectionalStreamTest() {} |
| 70 ~CronetBidirectionalStreamTest() override {} | 38 ~BidirectionalStreamTest() override {} |
| 71 | 39 |
| 72 void SetUp() override { | 40 void SetUp() override { |
| 73 StartCronetIfNecessary(); | |
| 74 StartQuicTestServer(); | 41 StartQuicTestServer(); |
| 75 [Cronet startNetLogToFile:@"cronet_netlog.json" logBytes:YES]; | |
| 76 } | 42 } |
| 77 | 43 |
| 78 void TearDown() override { | 44 void TearDown() override { |
| 79 ShutdownQuicTestServer(); | 45 ShutdownQuicTestServer(); |
| 80 [Cronet stopNetLog]; | |
| 81 } | 46 } |
| 82 | 47 |
| 83 cronet_engine* engine() { return [Cronet getGlobalEngine]; } | 48 stream_engine* engine() { return GetTestStreamEngine(); } |
| 49 |
| 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(BidirectionalStreamTest); |
| 84 }; | 52 }; |
| 85 | 53 |
| 86 class TestBidirectionalStreamCallback { | 54 class TestBidirectionalStreamCallback { |
| 87 public: | 55 public: |
| 88 enum ResponseStep { | 56 enum ResponseStep { |
| 89 NOTHING, | 57 NOTHING, |
| 90 ON_STREAM_READY, | 58 ON_STREAM_READY, |
| 91 ON_RESPONSE_STARTED, | 59 ON_RESPONSE_STARTED, |
| 92 ON_READ_COMPLETED, | 60 ON_READ_COMPLETED, |
| 93 ON_WRITE_COMPLETED, | 61 ON_WRITE_COMPLETED, |
| 94 ON_TRAILERS, | 62 ON_TRAILERS, |
| 95 ON_CANCELED, | 63 ON_CANCELED, |
| 96 ON_FAILED, | 64 ON_FAILED, |
| 97 ON_SUCCEEDED | 65 ON_SUCCEEDED |
| 98 }; | 66 }; |
| 99 | 67 |
| 100 struct WriteData { | 68 struct WriteData { |
| 101 std::string buffer; | 69 std::string buffer; |
| 102 // If |flush| is true, then cronet_bidirectional_stream_flush() will be | 70 // If |flush| is true, then bidirectional_stream_flush() will be |
| 103 // called after writing of the |buffer|. | 71 // called after writing of the |buffer|. |
| 104 bool flush; | 72 bool flush; |
| 105 | 73 |
| 106 WriteData(const std::string& buffer, bool flush); | 74 WriteData(const std::string& buffer, bool flush); |
| 107 ~WriteData(); | 75 ~WriteData(); |
| 108 | 76 |
| 109 DISALLOW_COPY_AND_ASSIGN(WriteData); | 77 DISALLOW_COPY_AND_ASSIGN(WriteData); |
| 110 }; | 78 }; |
| 111 | 79 |
| 112 cronet_bidirectional_stream* stream; | 80 bidirectional_stream* stream; |
| 113 base::WaitableEvent stream_done_event; | 81 base::WaitableEvent stream_done_event; |
| 114 | 82 |
| 115 // Test parameters. | 83 // Test parameters. |
| 116 std::map<std::string, std::string> request_headers; | 84 std::map<std::string, std::string> request_headers; |
| 117 std::list<std::unique_ptr<WriteData>> write_data; | 85 std::list<std::unique_ptr<WriteData>> write_data; |
| 118 std::string expected_negotiated_protocol; | 86 std::string expected_negotiated_protocol; |
| 119 ResponseStep cancel_from_step; | 87 ResponseStep cancel_from_step; |
| 120 size_t read_buffer_size; | 88 size_t read_buffer_size; |
| 121 | 89 |
| 122 // Test results. | 90 // Test results. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 133 base::WaitableEvent::InitialState::NOT_SIGNALED), | 101 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 134 expected_negotiated_protocol("quic/1+spdy/3"), | 102 expected_negotiated_protocol("quic/1+spdy/3"), |
| 135 cancel_from_step(NOTHING), | 103 cancel_from_step(NOTHING), |
| 136 read_buffer_size(32768), | 104 read_buffer_size(32768), |
| 137 response_step(NOTHING), | 105 response_step(NOTHING), |
| 138 read_buffer(nullptr), | 106 read_buffer(nullptr), |
| 139 net_error(0) {} | 107 net_error(0) {} |
| 140 | 108 |
| 141 ~TestBidirectionalStreamCallback() { | 109 ~TestBidirectionalStreamCallback() { |
| 142 if (read_buffer) | 110 if (read_buffer) |
| 143 delete read_buffer; | 111 delete[] read_buffer; |
| 144 } | 112 } |
| 145 | 113 |
| 146 static TestBidirectionalStreamCallback* FromStream( | 114 static TestBidirectionalStreamCallback* FromStream( |
| 147 cronet_bidirectional_stream* stream) { | 115 bidirectional_stream* stream) { |
| 148 DCHECK(stream); | 116 DCHECK(stream); |
| 149 return (TestBidirectionalStreamCallback*)stream->annotation; | 117 return (TestBidirectionalStreamCallback*)stream->annotation; |
| 150 } | 118 } |
| 151 | 119 |
| 152 virtual bool MaybeCancel(cronet_bidirectional_stream* stream, | 120 virtual bool MaybeCancel(bidirectional_stream* stream, ResponseStep step) { |
| 153 ResponseStep step) { | |
| 154 DCHECK_EQ(stream, this->stream); | 121 DCHECK_EQ(stream, this->stream); |
| 155 response_step = step; | 122 response_step = step; |
| 156 DLOG(WARNING) << "Step: " << step; | 123 DLOG(WARNING) << "Step: " << step; |
| 157 | 124 |
| 158 if (step != cancel_from_step) | 125 if (step != cancel_from_step) |
| 159 return false; | 126 return false; |
| 160 | 127 |
| 161 cronet_bidirectional_stream_cancel(stream); | 128 bidirectional_stream_cancel(stream); |
| 162 cronet_bidirectional_stream_write(stream, "abc", 3, false); | 129 bidirectional_stream_write(stream, "abc", 3, false); |
| 163 | 130 |
| 164 return true; | 131 return true; |
| 165 } | 132 } |
| 166 | 133 |
| 167 void SignalDone() { stream_done_event.Signal(); } | 134 void SignalDone() { stream_done_event.Signal(); } |
| 168 | 135 |
| 169 void BlockForDone() { stream_done_event.Wait(); } | 136 void BlockForDone() { stream_done_event.Wait(); } |
| 170 | 137 |
| 171 void AddWriteData(const std::string& data) { AddWriteData(data, true); } | 138 void AddWriteData(const std::string& data) { AddWriteData(data, true); } |
| 172 void AddWriteData(const std::string& data, bool flush) { | 139 void AddWriteData(const std::string& data, bool flush) { |
| 173 write_data.push_back(base::MakeUnique<WriteData>(data, flush)); | 140 write_data.push_back(base::MakeUnique<WriteData>(data, flush)); |
| 174 } | 141 } |
| 175 | 142 |
| 176 virtual void MaybeWriteNextData(cronet_bidirectional_stream* stream) { | 143 virtual void MaybeWriteNextData(bidirectional_stream* stream) { |
| 177 DCHECK_EQ(stream, this->stream); | 144 DCHECK_EQ(stream, this->stream); |
| 178 if (write_data.empty()) | 145 if (write_data.empty()) |
| 179 return; | 146 return; |
| 180 for (const auto& data : write_data) { | 147 for (const auto& data : write_data) { |
| 181 cronet_bidirectional_stream_write(stream, data->buffer.c_str(), | 148 bidirectional_stream_write(stream, data->buffer.c_str(), |
| 182 data->buffer.size(), | 149 data->buffer.size(), |
| 183 data == write_data.back()); | 150 data == write_data.back()); |
| 184 if (data->flush) { | 151 if (data->flush) { |
| 185 cronet_bidirectional_stream_flush(stream); | 152 bidirectional_stream_flush(stream); |
| 186 break; | 153 break; |
| 187 } | 154 } |
| 188 } | 155 } |
| 189 } | 156 } |
| 190 | 157 |
| 191 cronet_bidirectional_stream_callback* callback() const { return &s_callback; } | 158 bidirectional_stream_callback* callback() const { return &s_callback; } |
| 192 | 159 |
| 193 private: | 160 private: |
| 194 // C callbacks. | 161 // C callbacks. |
| 195 static void on_stream_ready_callback(cronet_bidirectional_stream* stream) { | 162 static void on_stream_ready_callback(bidirectional_stream* stream) { |
| 196 TestBidirectionalStreamCallback* test = FromStream(stream); | 163 TestBidirectionalStreamCallback* test = FromStream(stream); |
| 197 if (test->MaybeCancel(stream, ON_STREAM_READY)) | 164 if (test->MaybeCancel(stream, ON_STREAM_READY)) |
| 198 return; | 165 return; |
| 199 test->MaybeWriteNextData(stream); | 166 test->MaybeWriteNextData(stream); |
| 200 } | 167 } |
| 201 | 168 |
| 202 static void on_response_headers_received_callback( | 169 static void on_response_headers_received_callback( |
| 203 cronet_bidirectional_stream* stream, | 170 bidirectional_stream* stream, |
| 204 const cronet_bidirectional_stream_header_array* headers, | 171 const bidirectional_stream_header_array* headers, |
| 205 const char* negotiated_protocol) { | 172 const char* negotiated_protocol) { |
| 206 TestBidirectionalStreamCallback* test = FromStream(stream); | 173 TestBidirectionalStreamCallback* test = FromStream(stream); |
| 207 ASSERT_EQ(test->expected_negotiated_protocol, | 174 ASSERT_EQ(test->expected_negotiated_protocol, |
| 208 std::string(negotiated_protocol)); | 175 std::string(negotiated_protocol)); |
| 209 for (size_t i = 0; i < headers->count; ++i) { | 176 for (size_t i = 0; i < headers->count; ++i) { |
| 210 test->response_headers[headers->headers[i].key] = | 177 test->response_headers[headers->headers[i].key] = |
| 211 headers->headers[i].value; | 178 headers->headers[i].value; |
| 212 } | 179 } |
| 213 if (test->MaybeCancel(stream, ON_RESPONSE_STARTED)) | 180 if (test->MaybeCancel(stream, ON_RESPONSE_STARTED)) |
| 214 return; | 181 return; |
| 215 test->read_buffer = new char[test->read_buffer_size]; | 182 test->read_buffer = new char[test->read_buffer_size]; |
| 216 cronet_bidirectional_stream_read(stream, test->read_buffer, | 183 bidirectional_stream_read(stream, test->read_buffer, |
| 217 test->read_buffer_size); | 184 test->read_buffer_size); |
| 218 } | 185 } |
| 219 | 186 |
| 220 static void on_read_completed_callback(cronet_bidirectional_stream* stream, | 187 static void on_read_completed_callback(bidirectional_stream* stream, |
| 221 char* data, | 188 char* data, |
| 222 int count) { | 189 int count) { |
| 223 TestBidirectionalStreamCallback* test = FromStream(stream); | 190 TestBidirectionalStreamCallback* test = FromStream(stream); |
| 224 test->read_data.push_back(std::string(data, count)); | 191 test->read_data.push_back(std::string(data, count)); |
| 225 if (test->MaybeCancel(stream, ON_READ_COMPLETED)) | 192 if (test->MaybeCancel(stream, ON_READ_COMPLETED)) |
| 226 return; | 193 return; |
| 227 if (count == 0) | 194 if (count == 0) |
| 228 return; | 195 return; |
| 229 cronet_bidirectional_stream_read(stream, test->read_buffer, | 196 bidirectional_stream_read(stream, test->read_buffer, |
| 230 test->read_buffer_size); | 197 test->read_buffer_size); |
| 231 } | 198 } |
| 232 | 199 |
| 233 static void on_write_completed_callback(cronet_bidirectional_stream* stream, | 200 static void on_write_completed_callback(bidirectional_stream* stream, |
| 234 const char* data) { | 201 const char* data) { |
| 235 TestBidirectionalStreamCallback* test = FromStream(stream); | 202 TestBidirectionalStreamCallback* test = FromStream(stream); |
| 236 ASSERT_EQ(test->write_data.front()->buffer.c_str(), data); | 203 ASSERT_EQ(test->write_data.front()->buffer.c_str(), data); |
| 237 if (test->MaybeCancel(stream, ON_WRITE_COMPLETED)) | 204 if (test->MaybeCancel(stream, ON_WRITE_COMPLETED)) |
| 238 return; | 205 return; |
| 239 bool continue_writing = test->write_data.front()->flush; | 206 bool continue_writing = test->write_data.front()->flush; |
| 240 test->write_data.pop_front(); | 207 test->write_data.pop_front(); |
| 241 if (continue_writing) | 208 if (continue_writing) |
| 242 test->MaybeWriteNextData(stream); | 209 test->MaybeWriteNextData(stream); |
| 243 } | 210 } |
| 244 | 211 |
| 245 static void on_response_trailers_received_callback( | 212 static void on_response_trailers_received_callback( |
| 246 cronet_bidirectional_stream* stream, | 213 bidirectional_stream* stream, |
| 247 const cronet_bidirectional_stream_header_array* trailers) { | 214 const bidirectional_stream_header_array* trailers) { |
| 248 TestBidirectionalStreamCallback* test = FromStream(stream); | 215 TestBidirectionalStreamCallback* test = FromStream(stream); |
| 249 for (size_t i = 0; i < trailers->count; ++i) { | 216 for (size_t i = 0; i < trailers->count; ++i) { |
| 250 test->response_trailers[trailers->headers[i].key] = | 217 test->response_trailers[trailers->headers[i].key] = |
| 251 trailers->headers[i].value; | 218 trailers->headers[i].value; |
| 252 } | 219 } |
| 253 | 220 |
| 254 if (test->MaybeCancel(stream, ON_TRAILERS)) | 221 if (test->MaybeCancel(stream, ON_TRAILERS)) |
| 255 return; | 222 return; |
| 256 } | 223 } |
| 257 | 224 |
| 258 static void on_succeded_callback(cronet_bidirectional_stream* stream) { | 225 static void on_succeded_callback(bidirectional_stream* stream) { |
| 259 TestBidirectionalStreamCallback* test = FromStream(stream); | 226 TestBidirectionalStreamCallback* test = FromStream(stream); |
| 260 ASSERT_TRUE(test->write_data.empty()); | 227 ASSERT_TRUE(test->write_data.empty()); |
| 261 test->MaybeCancel(stream, ON_SUCCEEDED); | 228 test->MaybeCancel(stream, ON_SUCCEEDED); |
| 262 test->SignalDone(); | 229 test->SignalDone(); |
| 263 } | 230 } |
| 264 | 231 |
| 265 static void on_failed_callback(cronet_bidirectional_stream* stream, | 232 static void on_failed_callback(bidirectional_stream* stream, int net_error) { |
| 266 int net_error) { | |
| 267 TestBidirectionalStreamCallback* test = FromStream(stream); | 233 TestBidirectionalStreamCallback* test = FromStream(stream); |
| 268 test->net_error = net_error; | 234 test->net_error = net_error; |
| 269 test->MaybeCancel(stream, ON_FAILED); | 235 test->MaybeCancel(stream, ON_FAILED); |
| 270 test->SignalDone(); | 236 test->SignalDone(); |
| 271 } | 237 } |
| 272 | 238 |
| 273 static void on_canceled_callback(cronet_bidirectional_stream* stream) { | 239 static void on_canceled_callback(bidirectional_stream* stream) { |
| 274 TestBidirectionalStreamCallback* test = FromStream(stream); | 240 TestBidirectionalStreamCallback* test = FromStream(stream); |
| 275 test->MaybeCancel(stream, ON_CANCELED); | 241 test->MaybeCancel(stream, ON_CANCELED); |
| 276 test->SignalDone(); | 242 test->SignalDone(); |
| 277 } | 243 } |
| 278 | 244 |
| 279 static cronet_bidirectional_stream_callback s_callback; | 245 static bidirectional_stream_callback s_callback; |
| 280 }; | 246 }; |
| 281 | 247 |
| 282 cronet_bidirectional_stream_callback | 248 bidirectional_stream_callback TestBidirectionalStreamCallback::s_callback = { |
| 283 TestBidirectionalStreamCallback::s_callback = { | 249 on_stream_ready_callback, |
| 284 on_stream_ready_callback, | 250 on_response_headers_received_callback, |
| 285 on_response_headers_received_callback, | 251 on_read_completed_callback, |
| 286 on_read_completed_callback, | 252 on_write_completed_callback, |
| 287 on_write_completed_callback, | 253 on_response_trailers_received_callback, |
| 288 on_response_trailers_received_callback, | 254 on_succeded_callback, |
| 289 on_succeded_callback, | 255 on_failed_callback, |
| 290 on_failed_callback, | 256 on_canceled_callback}; |
| 291 on_canceled_callback}; | |
| 292 | 257 |
| 293 TestBidirectionalStreamCallback::WriteData::WriteData(const std::string& data, | 258 TestBidirectionalStreamCallback::WriteData::WriteData(const std::string& data, |
| 294 bool flush_after) | 259 bool flush_after) |
| 295 : buffer(data), flush(flush_after) {} | 260 : buffer(data), flush(flush_after) {} |
| 296 | 261 |
| 297 TestBidirectionalStreamCallback::WriteData::~WriteData() {} | 262 TestBidirectionalStreamCallback::WriteData::~WriteData() {} |
| 298 | 263 |
| 299 TEST_P(CronetBidirectionalStreamTest, StartExampleBidiStream) { | 264 TEST_P(BidirectionalStreamTest, StartExampleBidiStream) { |
| 300 TestBidirectionalStreamCallback test; | 265 TestBidirectionalStreamCallback test; |
| 301 test.AddWriteData("Hello, "); | 266 test.AddWriteData("Hello, "); |
| 302 test.AddWriteData("world!"); | 267 test.AddWriteData("world!"); |
| 303 // Use small read buffer size to test that response is split properly. | 268 // Use small read buffer size to test that response is split properly. |
| 304 test.read_buffer_size = 2; | 269 test.read_buffer_size = 2; |
| 305 test.stream = | 270 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 306 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 307 DCHECK(test.stream); | 271 DCHECK(test.stream); |
| 308 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 272 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 309 GetParam()); | 273 GetParam()); |
| 310 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 274 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 311 &kTestHeadersArray, false); | 275 &kTestHeadersArray, false); |
| 312 test.BlockForDone(); | 276 test.BlockForDone(); |
| 313 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 277 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 314 ASSERT_EQ(std::string(kHelloHeaderValue), | 278 ASSERT_EQ(std::string(kHelloHeaderValue), |
| 315 test.response_headers[kHelloHeaderName]); | 279 test.response_headers[kHelloHeaderName]); |
| 316 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); | 280 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); |
| 317 ASSERT_EQ(std::string(kHelloBodyValue, 2), test.read_data.front()); | 281 ASSERT_EQ(std::string(kHelloBodyValue, 2), test.read_data.front()); |
| 318 // Verify that individual read data joined using empty separator match | 282 // Verify that individual read data joined using empty separator match |
| 319 // expected body. | 283 // expected body. |
| 320 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); | 284 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); |
| 321 ASSERT_EQ(std::string(kHelloTrailerValue), | 285 ASSERT_EQ(std::string(kHelloTrailerValue), |
| 322 test.response_trailers[kHelloTrailerName]); | 286 test.response_trailers[kHelloTrailerName]); |
| 323 cronet_bidirectional_stream_destroy(test.stream); | 287 bidirectional_stream_destroy(test.stream); |
| 324 } | 288 } |
| 325 | 289 |
| 326 TEST_P(CronetBidirectionalStreamTest, SimplePutWithEmptyWriteDataAtTheEnd) { | 290 TEST_P(BidirectionalStreamTest, SimplePutWithEmptyWriteDataAtTheEnd) { |
| 327 TestBidirectionalStreamCallback test; | 291 TestBidirectionalStreamCallback test; |
| 328 test.AddWriteData("Hello, "); | 292 test.AddWriteData("Hello, "); |
| 329 test.AddWriteData("world!"); | 293 test.AddWriteData("world!"); |
| 330 test.AddWriteData(""); | 294 test.AddWriteData(""); |
| 331 test.stream = | 295 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 332 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 333 DCHECK(test.stream); | 296 DCHECK(test.stream); |
| 334 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 297 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 335 GetParam()); | 298 GetParam()); |
| 336 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "PUT", | 299 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "PUT", |
| 337 &kTestHeadersArray, false); | 300 &kTestHeadersArray, false); |
| 338 test.BlockForDone(); | 301 test.BlockForDone(); |
| 339 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 302 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 340 ASSERT_EQ(std::string(kHelloHeaderValue), | 303 ASSERT_EQ(std::string(kHelloHeaderValue), |
| 341 test.response_headers[kHelloHeaderName]); | 304 test.response_headers[kHelloHeaderName]); |
| 342 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); | 305 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); |
| 343 ASSERT_EQ(std::string(kHelloBodyValue), test.read_data.front()); | 306 ASSERT_EQ(std::string(kHelloBodyValue), test.read_data.front()); |
| 344 ASSERT_EQ(std::string(kHelloTrailerValue), | 307 ASSERT_EQ(std::string(kHelloTrailerValue), |
| 345 test.response_trailers[kHelloTrailerName]); | 308 test.response_trailers[kHelloTrailerName]); |
| 346 cronet_bidirectional_stream_destroy(test.stream); | 309 bidirectional_stream_destroy(test.stream); |
| 347 } | 310 } |
| 348 | 311 |
| 349 TEST_P(CronetBidirectionalStreamTest, SimpleGetWithFlush) { | 312 TEST_P(BidirectionalStreamTest, SimpleGetWithFlush) { |
| 350 TestBidirectionalStreamCallback test; | 313 TestBidirectionalStreamCallback test; |
| 351 test.stream = | 314 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 352 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 353 DCHECK(test.stream); | 315 DCHECK(test.stream); |
| 354 cronet_bidirectional_stream_disable_auto_flush(test.stream, true); | 316 bidirectional_stream_disable_auto_flush(test.stream, true); |
| 355 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 317 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 356 GetParam()); | 318 GetParam()); |
| 357 // Flush before start is ignored. | 319 // Flush before start is ignored. |
| 358 cronet_bidirectional_stream_flush(test.stream); | 320 bidirectional_stream_flush(test.stream); |
| 359 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "GET", | 321 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "GET", |
| 360 &kTestHeadersArray, true); | 322 &kTestHeadersArray, true); |
| 361 test.BlockForDone(); | 323 test.BlockForDone(); |
| 362 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 324 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 363 ASSERT_EQ(std::string(kHelloHeaderValue), | 325 ASSERT_EQ(std::string(kHelloHeaderValue), |
| 364 test.response_headers[kHelloHeaderName]); | 326 test.response_headers[kHelloHeaderName]); |
| 365 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); | 327 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); |
| 366 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); | 328 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); |
| 367 ASSERT_EQ(std::string(kHelloTrailerValue), | 329 ASSERT_EQ(std::string(kHelloTrailerValue), |
| 368 test.response_trailers[kHelloTrailerName]); | 330 test.response_trailers[kHelloTrailerName]); |
| 369 // Flush after done is ignored. | 331 // Flush after done is ignored. |
| 370 cronet_bidirectional_stream_flush(test.stream); | 332 bidirectional_stream_flush(test.stream); |
| 371 cronet_bidirectional_stream_destroy(test.stream); | 333 bidirectional_stream_destroy(test.stream); |
| 372 } | 334 } |
| 373 | 335 |
| 374 TEST_P(CronetBidirectionalStreamTest, SimplePostWithFlush) { | 336 TEST_P(BidirectionalStreamTest, SimplePostWithFlush) { |
| 375 TestBidirectionalStreamCallback test; | 337 TestBidirectionalStreamCallback test; |
| 376 test.AddWriteData("Test String", false); | 338 test.AddWriteData("Test String", false); |
| 377 test.AddWriteData("1234567890", false); | 339 test.AddWriteData("1234567890", false); |
| 378 test.AddWriteData("woot!", true); | 340 test.AddWriteData("woot!", true); |
| 379 test.stream = | 341 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 380 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 381 DCHECK(test.stream); | 342 DCHECK(test.stream); |
| 382 cronet_bidirectional_stream_disable_auto_flush(test.stream, true); | 343 bidirectional_stream_disable_auto_flush(test.stream, true); |
| 383 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 344 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 384 GetParam()); | 345 GetParam()); |
| 385 // Flush before start is ignored. | 346 // Flush before start is ignored. |
| 386 cronet_bidirectional_stream_flush(test.stream); | 347 bidirectional_stream_flush(test.stream); |
| 387 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 348 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 388 &kTestHeadersArray, false); | 349 &kTestHeadersArray, false); |
| 389 test.BlockForDone(); | 350 test.BlockForDone(); |
| 390 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 351 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 391 ASSERT_EQ(std::string(kHelloHeaderValue), | 352 ASSERT_EQ(std::string(kHelloHeaderValue), |
| 392 test.response_headers[kHelloHeaderName]); | 353 test.response_headers[kHelloHeaderName]); |
| 393 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); | 354 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); |
| 394 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); | 355 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); |
| 395 ASSERT_EQ(std::string(kHelloTrailerValue), | 356 ASSERT_EQ(std::string(kHelloTrailerValue), |
| 396 test.response_trailers[kHelloTrailerName]); | 357 test.response_trailers[kHelloTrailerName]); |
| 397 // Flush after done is ignored. | 358 // Flush after done is ignored. |
| 398 cronet_bidirectional_stream_flush(test.stream); | 359 bidirectional_stream_flush(test.stream); |
| 399 cronet_bidirectional_stream_destroy(test.stream); | 360 bidirectional_stream_destroy(test.stream); |
| 400 } | 361 } |
| 401 | 362 |
| 402 TEST_P(CronetBidirectionalStreamTest, SimplePostWithFlushTwice) { | 363 TEST_P(BidirectionalStreamTest, SimplePostWithFlushTwice) { |
| 403 TestBidirectionalStreamCallback test; | 364 TestBidirectionalStreamCallback test; |
| 404 test.AddWriteData("Test String", false); | 365 test.AddWriteData("Test String", false); |
| 405 test.AddWriteData("1234567890", false); | 366 test.AddWriteData("1234567890", false); |
| 406 test.AddWriteData("woot!", true); | 367 test.AddWriteData("woot!", true); |
| 407 test.AddWriteData("Test String", false); | 368 test.AddWriteData("Test String", false); |
| 408 test.AddWriteData("1234567890", false); | 369 test.AddWriteData("1234567890", false); |
| 409 test.AddWriteData("woot!", true); | 370 test.AddWriteData("woot!", true); |
| 410 test.stream = | 371 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 411 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 412 DCHECK(test.stream); | 372 DCHECK(test.stream); |
| 413 cronet_bidirectional_stream_disable_auto_flush(test.stream, true); | 373 bidirectional_stream_disable_auto_flush(test.stream, true); |
| 414 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 374 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 415 GetParam()); | 375 GetParam()); |
| 416 // Flush before start is ignored. | 376 // Flush before start is ignored. |
| 417 cronet_bidirectional_stream_flush(test.stream); | 377 bidirectional_stream_flush(test.stream); |
| 418 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 378 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 419 &kTestHeadersArray, false); | 379 &kTestHeadersArray, false); |
| 420 test.BlockForDone(); | 380 test.BlockForDone(); |
| 421 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 381 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 422 ASSERT_EQ(std::string(kHelloHeaderValue), | 382 ASSERT_EQ(std::string(kHelloHeaderValue), |
| 423 test.response_headers[kHelloHeaderName]); | 383 test.response_headers[kHelloHeaderName]); |
| 424 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); | 384 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); |
| 425 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); | 385 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); |
| 426 ASSERT_EQ(std::string(kHelloTrailerValue), | 386 ASSERT_EQ(std::string(kHelloTrailerValue), |
| 427 test.response_trailers[kHelloTrailerName]); | 387 test.response_trailers[kHelloTrailerName]); |
| 428 // Flush after done is ignored. | 388 // Flush after done is ignored. |
| 429 cronet_bidirectional_stream_flush(test.stream); | 389 bidirectional_stream_flush(test.stream); |
| 430 cronet_bidirectional_stream_destroy(test.stream); | 390 bidirectional_stream_destroy(test.stream); |
| 431 } | 391 } |
| 432 | 392 |
| 433 TEST_P(CronetBidirectionalStreamTest, SimplePostWithFlushAfterOneWrite) { | 393 TEST_P(BidirectionalStreamTest, SimplePostWithFlushAfterOneWrite) { |
| 434 TestBidirectionalStreamCallback test; | 394 TestBidirectionalStreamCallback test; |
| 435 test.AddWriteData("Test String", false); | 395 test.AddWriteData("Test String", false); |
| 436 test.AddWriteData("1234567890", false); | 396 test.AddWriteData("1234567890", false); |
| 437 test.AddWriteData("woot!", true); | 397 test.AddWriteData("woot!", true); |
| 438 test.stream = | 398 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 439 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 440 DCHECK(test.stream); | 399 DCHECK(test.stream); |
| 441 cronet_bidirectional_stream_disable_auto_flush(test.stream, true); | 400 bidirectional_stream_disable_auto_flush(test.stream, true); |
| 442 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 401 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 443 GetParam()); | 402 GetParam()); |
| 444 // Flush before start is ignored. | 403 // Flush before start is ignored. |
| 445 cronet_bidirectional_stream_flush(test.stream); | 404 bidirectional_stream_flush(test.stream); |
| 446 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 405 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 447 &kTestHeadersArray, false); | 406 &kTestHeadersArray, false); |
| 448 test.BlockForDone(); | 407 test.BlockForDone(); |
| 449 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 408 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 450 ASSERT_EQ(std::string(kHelloHeaderValue), | 409 ASSERT_EQ(std::string(kHelloHeaderValue), |
| 451 test.response_headers[kHelloHeaderName]); | 410 test.response_headers[kHelloHeaderName]); |
| 452 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); | 411 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); |
| 453 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); | 412 ASSERT_EQ(std::string(kHelloBodyValue), base::JoinString(test.read_data, "")); |
| 454 ASSERT_EQ(std::string(kHelloTrailerValue), | 413 ASSERT_EQ(std::string(kHelloTrailerValue), |
| 455 test.response_trailers[kHelloTrailerName]); | 414 test.response_trailers[kHelloTrailerName]); |
| 456 // Flush after done is ignored. | 415 // Flush after done is ignored. |
| 457 cronet_bidirectional_stream_flush(test.stream); | 416 bidirectional_stream_flush(test.stream); |
| 458 cronet_bidirectional_stream_destroy(test.stream); | 417 bidirectional_stream_destroy(test.stream); |
| 459 } | 418 } |
| 460 | 419 |
| 461 TEST_P(CronetBidirectionalStreamTest, TestDelayedFlush) { | 420 TEST_P(BidirectionalStreamTest, TestDelayedFlush) { |
| 462 class CustomTestBidirectionalStreamCallback | 421 class CustomTestBidirectionalStreamCallback |
| 463 : public TestBidirectionalStreamCallback { | 422 : public TestBidirectionalStreamCallback { |
| 464 void MaybeWriteNextData(cronet_bidirectional_stream* stream) override { | 423 void MaybeWriteNextData(bidirectional_stream* stream) override { |
| 465 DCHECK_EQ(stream, this->stream); | 424 DCHECK_EQ(stream, this->stream); |
| 466 if (write_data.empty()) | 425 if (write_data.empty()) |
| 467 return; | 426 return; |
| 468 // Write all buffers when stream is ready. | 427 // Write all buffers when stream is ready. |
| 469 // Flush after "3" and "5". | 428 // Flush after "3" and "5". |
| 470 // EndOfStream is set with "6" but not flushed, so it is not sent. | 429 // EndOfStream is set with "6" but not flushed, so it is not sent. |
| 471 if (write_data.front()->buffer == "1") { | 430 if (write_data.front()->buffer == "1") { |
| 472 for (const auto& data : write_data) { | 431 for (const auto& data : write_data) { |
| 473 cronet_bidirectional_stream_write(stream, data->buffer.c_str(), | 432 bidirectional_stream_write(stream, data->buffer.c_str(), |
| 474 data->buffer.size(), | 433 data->buffer.size(), |
| 475 data == write_data.back()); | 434 data == write_data.back()); |
| 476 if (data->flush) { | 435 if (data->flush) { |
| 477 cronet_bidirectional_stream_flush(stream); | 436 bidirectional_stream_flush(stream); |
| 478 } | 437 } |
| 479 } | 438 } |
| 480 } | 439 } |
| 481 // Flush the final buffer with endOfStream flag. | 440 // Flush the final buffer with endOfStream flag. |
| 482 if (write_data.front()->buffer == "6") | 441 if (write_data.front()->buffer == "6") |
| 483 cronet_bidirectional_stream_flush(stream); | 442 bidirectional_stream_flush(stream); |
| 484 } | 443 } |
| 485 }; | 444 }; |
| 486 | 445 |
| 487 CustomTestBidirectionalStreamCallback test; | 446 CustomTestBidirectionalStreamCallback test; |
| 488 test.AddWriteData("1", false); | 447 test.AddWriteData("1", false); |
| 489 test.AddWriteData("2", false); | 448 test.AddWriteData("2", false); |
| 490 test.AddWriteData("3", true); | 449 test.AddWriteData("3", true); |
| 491 test.AddWriteData("4", false); | 450 test.AddWriteData("4", false); |
| 492 test.AddWriteData("5", true); | 451 test.AddWriteData("5", true); |
| 493 test.AddWriteData("6", false); | 452 test.AddWriteData("6", false); |
| 494 test.stream = | 453 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 495 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 496 DCHECK(test.stream); | 454 DCHECK(test.stream); |
| 497 cronet_bidirectional_stream_disable_auto_flush(test.stream, true); | 455 bidirectional_stream_disable_auto_flush(test.stream, true); |
| 498 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 456 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 499 GetParam()); | 457 GetParam()); |
| 500 // Flush before start is ignored. | 458 // Flush before start is ignored. |
| 501 cronet_bidirectional_stream_flush(test.stream); | 459 bidirectional_stream_flush(test.stream); |
| 502 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 460 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 503 &kTestHeadersArray, false); | 461 &kTestHeadersArray, false); |
| 504 test.BlockForDone(); | 462 test.BlockForDone(); |
| 505 // Flush after done is ignored. | 463 // Flush after done is ignored. |
| 506 cronet_bidirectional_stream_flush(test.stream); | 464 bidirectional_stream_flush(test.stream); |
| 507 cronet_bidirectional_stream_destroy(test.stream); | 465 bidirectional_stream_destroy(test.stream); |
| 508 } | 466 } |
| 509 | 467 |
| 510 TEST_P(CronetBidirectionalStreamTest, CancelOnRead) { | 468 TEST_P(BidirectionalStreamTest, CancelOnRead) { |
| 511 TestBidirectionalStreamCallback test; | 469 TestBidirectionalStreamCallback test; |
| 512 test.stream = | 470 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 513 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 514 DCHECK(test.stream); | 471 DCHECK(test.stream); |
| 515 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 472 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 516 GetParam()); | 473 GetParam()); |
| 517 test.cancel_from_step = TestBidirectionalStreamCallback::ON_READ_COMPLETED; | 474 test.cancel_from_step = TestBidirectionalStreamCallback::ON_READ_COMPLETED; |
| 518 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 475 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 519 &kTestHeadersArray, true); | 476 &kTestHeadersArray, true); |
| 520 test.BlockForDone(); | 477 test.BlockForDone(); |
| 521 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 478 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 522 ASSERT_EQ(std::string(kHelloBodyValue), test.read_data.front()); | 479 ASSERT_EQ(std::string(kHelloBodyValue), test.read_data.front()); |
| 523 ASSERT_EQ(TestBidirectionalStreamCallback::ON_CANCELED, test.response_step); | 480 ASSERT_EQ(TestBidirectionalStreamCallback::ON_CANCELED, test.response_step); |
| 524 cronet_bidirectional_stream_destroy(test.stream); | 481 bidirectional_stream_destroy(test.stream); |
| 525 } | 482 } |
| 526 | 483 |
| 527 TEST_P(CronetBidirectionalStreamTest, CancelOnResponse) { | 484 TEST_P(BidirectionalStreamTest, CancelOnResponse) { |
| 528 TestBidirectionalStreamCallback test; | 485 TestBidirectionalStreamCallback test; |
| 529 test.stream = | 486 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 530 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 531 DCHECK(test.stream); | 487 DCHECK(test.stream); |
| 532 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 488 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 533 GetParam()); | 489 GetParam()); |
| 534 test.cancel_from_step = TestBidirectionalStreamCallback::ON_RESPONSE_STARTED; | 490 test.cancel_from_step = TestBidirectionalStreamCallback::ON_RESPONSE_STARTED; |
| 535 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 491 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 536 &kTestHeadersArray, true); | 492 &kTestHeadersArray, true); |
| 537 test.BlockForDone(); | 493 test.BlockForDone(); |
| 538 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 494 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 539 ASSERT_TRUE(test.read_data.empty()); | 495 ASSERT_TRUE(test.read_data.empty()); |
| 540 ASSERT_EQ(TestBidirectionalStreamCallback::ON_CANCELED, test.response_step); | 496 ASSERT_EQ(TestBidirectionalStreamCallback::ON_CANCELED, test.response_step); |
| 541 cronet_bidirectional_stream_destroy(test.stream); | 497 bidirectional_stream_destroy(test.stream); |
| 542 } | 498 } |
| 543 | 499 |
| 544 TEST_P(CronetBidirectionalStreamTest, CancelOnSucceeded) { | 500 TEST_P(BidirectionalStreamTest, CancelOnSucceeded) { |
| 545 TestBidirectionalStreamCallback test; | 501 TestBidirectionalStreamCallback test; |
| 546 test.stream = | 502 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 547 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 548 DCHECK(test.stream); | 503 DCHECK(test.stream); |
| 549 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 504 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 550 GetParam()); | 505 GetParam()); |
| 551 test.cancel_from_step = TestBidirectionalStreamCallback::ON_SUCCEEDED; | 506 test.cancel_from_step = TestBidirectionalStreamCallback::ON_SUCCEEDED; |
| 552 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 507 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 553 &kTestHeadersArray, true); | 508 &kTestHeadersArray, true); |
| 554 test.BlockForDone(); | 509 test.BlockForDone(); |
| 555 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); | 510 ASSERT_EQ(std::string(kHelloStatus), test.response_headers[kStatusHeader]); |
| 556 ASSERT_EQ(std::string(kHelloBodyValue), test.read_data.front()); | 511 ASSERT_EQ(std::string(kHelloBodyValue), test.read_data.front()); |
| 557 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); | 512 ASSERT_EQ(TestBidirectionalStreamCallback::ON_SUCCEEDED, test.response_step); |
| 558 cronet_bidirectional_stream_destroy(test.stream); | 513 bidirectional_stream_destroy(test.stream); |
| 559 } | 514 } |
| 560 | 515 |
| 561 TEST_P(CronetBidirectionalStreamTest, ReadFailsBeforeRequestStarted) { | 516 TEST_P(BidirectionalStreamTest, ReadFailsBeforeRequestStarted) { |
| 562 TestBidirectionalStreamCallback test; | 517 TestBidirectionalStreamCallback test; |
| 563 test.stream = | 518 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 564 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 565 DCHECK(test.stream); | 519 DCHECK(test.stream); |
| 566 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 520 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 567 GetParam()); | 521 GetParam()); |
| 568 char read_buffer[1]; | 522 char read_buffer[1]; |
| 569 cronet_bidirectional_stream_read(test.stream, read_buffer, | 523 bidirectional_stream_read(test.stream, read_buffer, sizeof(read_buffer)); |
| 570 sizeof(read_buffer)); | |
| 571 test.BlockForDone(); | 524 test.BlockForDone(); |
| 572 ASSERT_TRUE(test.read_data.empty()); | 525 ASSERT_TRUE(test.read_data.empty()); |
| 573 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); | 526 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); |
| 574 ASSERT_EQ(net::ERR_UNEXPECTED, test.net_error); | 527 ASSERT_EQ(net::ERR_UNEXPECTED, test.net_error); |
| 575 cronet_bidirectional_stream_destroy(test.stream); | 528 bidirectional_stream_destroy(test.stream); |
| 576 } | 529 } |
| 577 | 530 |
| 578 TEST_P(CronetBidirectionalStreamTest, | 531 TEST_P(BidirectionalStreamTest, |
| 579 StreamFailBeforeReadIsExecutedOnNetworkThread) { | 532 StreamFailBeforeReadIsExecutedOnNetworkThread) { |
| 580 class CustomTestBidirectionalStreamCallback | 533 class CustomTestBidirectionalStreamCallback |
| 581 : public TestBidirectionalStreamCallback { | 534 : public TestBidirectionalStreamCallback { |
| 582 bool MaybeCancel(cronet_bidirectional_stream* stream, | 535 bool MaybeCancel(bidirectional_stream* stream, ResponseStep step) override { |
| 583 ResponseStep step) override { | |
| 584 if (step == ResponseStep::ON_READ_COMPLETED) { | 536 if (step == ResponseStep::ON_READ_COMPLETED) { |
| 585 // Shut down the server, and the stream should error out. | 537 // Shut down the server, and the stream should error out. |
| 586 // The second call to ShutdownQuicTestServer is no-op. | 538 // The second call to ShutdownQuicTestServer is no-op. |
| 587 ShutdownQuicTestServer(); | 539 ShutdownQuicTestServer(); |
| 588 } | 540 } |
| 589 return TestBidirectionalStreamCallback::MaybeCancel(stream, step); | 541 return TestBidirectionalStreamCallback::MaybeCancel(stream, step); |
| 590 } | 542 } |
| 591 }; | 543 }; |
| 592 | 544 |
| 593 CustomTestBidirectionalStreamCallback test; | 545 CustomTestBidirectionalStreamCallback test; |
| 594 test.AddWriteData("Hello, "); | 546 test.AddWriteData("Hello, "); |
| 595 test.AddWriteData("world!"); | 547 test.AddWriteData("world!"); |
| 596 test.read_buffer_size = 2; | 548 test.read_buffer_size = 2; |
| 597 test.stream = | 549 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 598 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 599 DCHECK(test.stream); | 550 DCHECK(test.stream); |
| 600 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 551 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 601 GetParam()); | 552 GetParam()); |
| 602 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 553 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 603 &kTestHeadersArray, false); | 554 &kTestHeadersArray, false); |
| 604 test.BlockForDone(); | 555 test.BlockForDone(); |
| 605 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); | 556 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); |
| 606 ASSERT_EQ(net::ERR_QUIC_PROTOCOL_ERROR, test.net_error); | 557 ASSERT_EQ(net::ERR_QUIC_PROTOCOL_ERROR, test.net_error); |
| 607 cronet_bidirectional_stream_destroy(test.stream); | 558 bidirectional_stream_destroy(test.stream); |
| 608 } | 559 } |
| 609 | 560 |
| 610 TEST_P(CronetBidirectionalStreamTest, WriteFailsBeforeRequestStarted) { | 561 TEST_P(BidirectionalStreamTest, WriteFailsBeforeRequestStarted) { |
| 611 TestBidirectionalStreamCallback test; | 562 TestBidirectionalStreamCallback test; |
| 612 test.stream = | 563 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 613 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 614 DCHECK(test.stream); | 564 DCHECK(test.stream); |
| 615 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 565 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 616 GetParam()); | 566 GetParam()); |
| 617 cronet_bidirectional_stream_write(test.stream, "1", 1, false); | 567 bidirectional_stream_write(test.stream, "1", 1, false); |
| 618 test.BlockForDone(); | 568 test.BlockForDone(); |
| 619 ASSERT_TRUE(test.read_data.empty()); | 569 ASSERT_TRUE(test.read_data.empty()); |
| 620 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); | 570 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); |
| 621 ASSERT_EQ(net::ERR_UNEXPECTED, test.net_error); | 571 ASSERT_EQ(net::ERR_UNEXPECTED, test.net_error); |
| 622 cronet_bidirectional_stream_destroy(test.stream); | 572 bidirectional_stream_destroy(test.stream); |
| 623 } | 573 } |
| 624 | 574 |
| 625 TEST_P(CronetBidirectionalStreamTest, StreamFailAfterStreamReadyCallback) { | 575 TEST_P(BidirectionalStreamTest, StreamFailAfterStreamReadyCallback) { |
| 626 class CustomTestBidirectionalStreamCallback | 576 class CustomTestBidirectionalStreamCallback |
| 627 : public TestBidirectionalStreamCallback { | 577 : public TestBidirectionalStreamCallback { |
| 628 bool MaybeCancel(cronet_bidirectional_stream* stream, | 578 bool MaybeCancel(bidirectional_stream* stream, ResponseStep step) override { |
| 629 ResponseStep step) override { | |
| 630 if (step == ResponseStep::ON_STREAM_READY) { | 579 if (step == ResponseStep::ON_STREAM_READY) { |
| 631 // Shut down the server, and the stream should error out. | 580 // Shut down the server, and the stream should error out. |
| 632 // The second call to ShutdownQuicTestServer is no-op. | 581 // The second call to ShutdownQuicTestServer is no-op. |
| 633 ShutdownQuicTestServer(); | 582 ShutdownQuicTestServer(); |
| 634 } | 583 } |
| 635 return TestBidirectionalStreamCallback::MaybeCancel(stream, step); | 584 return TestBidirectionalStreamCallback::MaybeCancel(stream, step); |
| 636 } | 585 } |
| 637 }; | 586 }; |
| 638 | 587 |
| 639 CustomTestBidirectionalStreamCallback test; | 588 CustomTestBidirectionalStreamCallback test; |
| 640 test.AddWriteData("Test String"); | 589 test.AddWriteData("Test String"); |
| 641 test.stream = | 590 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 642 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 643 DCHECK(test.stream); | 591 DCHECK(test.stream); |
| 644 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 592 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 645 GetParam()); | 593 GetParam()); |
| 646 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 594 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 647 &kTestHeadersArray, false); | 595 &kTestHeadersArray, false); |
| 648 test.BlockForDone(); | 596 test.BlockForDone(); |
| 649 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); | 597 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); |
| 650 ASSERT_TRUE(test.net_error == net::ERR_QUIC_PROTOCOL_ERROR || | 598 ASSERT_TRUE(test.net_error == net::ERR_QUIC_PROTOCOL_ERROR || |
| 651 test.net_error == net::ERR_QUIC_HANDSHAKE_FAILED); | 599 test.net_error == net::ERR_QUIC_HANDSHAKE_FAILED || |
| 652 cronet_bidirectional_stream_destroy(test.stream); | 600 test.net_error == net::ERR_CONNECTION_REFUSED); |
| 601 bidirectional_stream_destroy(test.stream); |
| 653 } | 602 } |
| 654 | 603 |
| 655 TEST_P(CronetBidirectionalStreamTest, | 604 TEST_P(BidirectionalStreamTest, |
| 656 StreamFailBeforeWriteIsExecutedOnNetworkThread) { | 605 StreamFailBeforeWriteIsExecutedOnNetworkThread) { |
| 657 class CustomTestBidirectionalStreamCallback | 606 class CustomTestBidirectionalStreamCallback |
| 658 : public TestBidirectionalStreamCallback { | 607 : public TestBidirectionalStreamCallback { |
| 659 bool MaybeCancel(cronet_bidirectional_stream* stream, | 608 bool MaybeCancel(bidirectional_stream* stream, ResponseStep step) override { |
| 660 ResponseStep step) override { | |
| 661 if (step == ResponseStep::ON_WRITE_COMPLETED) { | 609 if (step == ResponseStep::ON_WRITE_COMPLETED) { |
| 662 // Shut down the server, and the stream should error out. | 610 // Shut down the server, and the stream should error out. |
| 663 // The second call to ShutdownQuicTestServer is no-op. | 611 // The second call to ShutdownQuicTestServer is no-op. |
| 664 ShutdownQuicTestServer(); | 612 ShutdownQuicTestServer(); |
| 665 } | 613 } |
| 666 return TestBidirectionalStreamCallback::MaybeCancel(stream, step); | 614 return TestBidirectionalStreamCallback::MaybeCancel(stream, step); |
| 667 } | 615 } |
| 668 }; | 616 }; |
| 669 | 617 |
| 670 CustomTestBidirectionalStreamCallback test; | 618 CustomTestBidirectionalStreamCallback test; |
| 671 test.AddWriteData("Test String"); | 619 test.AddWriteData("Test String"); |
| 672 test.AddWriteData("1234567890"); | 620 test.AddWriteData("1234567890"); |
| 673 test.AddWriteData("woot!"); | 621 test.AddWriteData("woot!"); |
| 674 test.stream = | 622 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 675 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 676 DCHECK(test.stream); | 623 DCHECK(test.stream); |
| 677 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 624 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 678 GetParam()); | 625 GetParam()); |
| 679 cronet_bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", | 626 bidirectional_stream_start(test.stream, kTestServerUrl, 0, "POST", |
| 680 &kTestHeadersArray, false); | 627 &kTestHeadersArray, false); |
| 681 test.BlockForDone(); | 628 test.BlockForDone(); |
| 682 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); | 629 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); |
| 683 ASSERT_TRUE(test.net_error == net::ERR_QUIC_PROTOCOL_ERROR || | 630 ASSERT_TRUE(test.net_error == net::ERR_QUIC_PROTOCOL_ERROR || |
| 684 test.net_error == net::ERR_QUIC_HANDSHAKE_FAILED); | 631 test.net_error == net::ERR_QUIC_HANDSHAKE_FAILED); |
| 685 cronet_bidirectional_stream_destroy(test.stream); | 632 bidirectional_stream_destroy(test.stream); |
| 686 } | 633 } |
| 687 | 634 |
| 688 TEST_P(CronetBidirectionalStreamTest, FailedResolution) { | 635 TEST_P(BidirectionalStreamTest, FailedResolution) { |
| 689 TestBidirectionalStreamCallback test; | 636 TestBidirectionalStreamCallback test; |
| 690 test.stream = | 637 test.stream = bidirectional_stream_create(engine(), &test, test.callback()); |
| 691 cronet_bidirectional_stream_create(engine(), &test, test.callback()); | |
| 692 DCHECK(test.stream); | 638 DCHECK(test.stream); |
| 693 cronet_bidirectional_stream_delay_request_headers_until_flush(test.stream, | 639 bidirectional_stream_delay_request_headers_until_flush(test.stream, |
| 694 GetParam()); | 640 GetParam()); |
| 695 test.cancel_from_step = TestBidirectionalStreamCallback::ON_FAILED; | 641 test.cancel_from_step = TestBidirectionalStreamCallback::ON_FAILED; |
| 696 cronet_bidirectional_stream_start(test.stream, "https://notfound.example.com", | 642 bidirectional_stream_start(test.stream, "https://notfound.example.com", 0, |
| 697 0, "GET", &kTestHeadersArray, true); | 643 "GET", &kTestHeadersArray, true); |
| 698 test.BlockForDone(); | 644 test.BlockForDone(); |
| 699 ASSERT_TRUE(test.read_data.empty()); | 645 ASSERT_TRUE(test.read_data.empty()); |
| 700 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); | 646 ASSERT_EQ(TestBidirectionalStreamCallback::ON_FAILED, test.response_step); |
| 701 ASSERT_EQ(net::ERR_NAME_NOT_RESOLVED, test.net_error); | 647 ASSERT_EQ(net::ERR_NAME_NOT_RESOLVED, test.net_error); |
| 702 cronet_bidirectional_stream_destroy(test.stream); | 648 bidirectional_stream_destroy(test.stream); |
| 703 } | 649 } |
| 704 | 650 |
| 705 INSTANTIATE_TEST_CASE_P(CronetBidirectionalStreamDelayRequestHeadersUntilFlush, | 651 INSTANTIATE_TEST_CASE_P(BidirectionalStreamDelayRequestHeadersUntilFlush, |
| 706 CronetBidirectionalStreamTest, | 652 BidirectionalStreamTest, |
| 707 ::testing::Values(true, false)); | 653 ::testing::Values(true, false)); |
| 708 | 654 |
| 709 } // namespace cronet | 655 } // namespace grpc_support |
| OLD | NEW |