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