]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // read_until.cpp | |
3 | // ~~~~~~~~~~~~~~ | |
4 | // | |
b32b8144 | 5 | // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | // Disable autolinking for unit tests. | |
12 | #if !defined(BOOST_ALL_NO_LIB) | |
13 | #define BOOST_ALL_NO_LIB 1 | |
14 | #endif // !defined(BOOST_ALL_NO_LIB) | |
15 | ||
16 | // Test that header file is self-contained. | |
17 | #include <boost/asio/read_until.hpp> | |
18 | ||
19 | #include <cstring> | |
20 | #include "archetypes/async_result.hpp" | |
b32b8144 FG |
21 | #include <boost/asio/io_context.hpp> |
22 | #include <boost/asio/post.hpp> | |
7c673cae FG |
23 | #include <boost/asio/streambuf.hpp> |
24 | #include "unit_test.hpp" | |
25 | ||
26 | #if defined(BOOST_ASIO_HAS_BOOST_BIND) | |
27 | # include <boost/bind.hpp> | |
28 | #else // defined(BOOST_ASIO_HAS_BOOST_BIND) | |
29 | # include <functional> | |
30 | #endif // defined(BOOST_ASIO_HAS_BOOST_BIND) | |
31 | ||
32 | class test_stream | |
33 | { | |
34 | public: | |
b32b8144 | 35 | typedef boost::asio::io_context::executor_type executor_type; |
7c673cae | 36 | |
b32b8144 FG |
37 | test_stream(boost::asio::io_context& io_context) |
38 | : io_context_(io_context), | |
7c673cae FG |
39 | length_(0), |
40 | position_(0), | |
41 | next_read_length_(0) | |
42 | { | |
43 | } | |
44 | ||
b32b8144 | 45 | executor_type get_executor() BOOST_ASIO_NOEXCEPT |
7c673cae | 46 | { |
b32b8144 | 47 | return io_context_.get_executor(); |
7c673cae FG |
48 | } |
49 | ||
50 | void reset(const void* data, size_t length) | |
51 | { | |
52 | using namespace std; // For memcpy. | |
53 | ||
54 | BOOST_ASIO_CHECK(length <= max_length); | |
55 | ||
56 | memcpy(data_, data, length); | |
57 | length_ = length; | |
58 | position_ = 0; | |
59 | next_read_length_ = length; | |
60 | } | |
61 | ||
62 | void next_read_length(size_t length) | |
63 | { | |
64 | next_read_length_ = length; | |
65 | } | |
66 | ||
67 | template <typename Mutable_Buffers> | |
68 | size_t read_some(const Mutable_Buffers& buffers) | |
69 | { | |
70 | size_t n = boost::asio::buffer_copy(buffers, | |
71 | boost::asio::buffer(data_, length_) + position_, | |
72 | next_read_length_); | |
73 | position_ += n; | |
74 | return n; | |
75 | } | |
76 | ||
77 | template <typename Mutable_Buffers> | |
78 | size_t read_some(const Mutable_Buffers& buffers, | |
79 | boost::system::error_code& ec) | |
80 | { | |
81 | ec = boost::system::error_code(); | |
82 | return read_some(buffers); | |
83 | } | |
84 | ||
85 | template <typename Mutable_Buffers, typename Handler> | |
86 | void async_read_some(const Mutable_Buffers& buffers, Handler handler) | |
87 | { | |
88 | size_t bytes_transferred = read_some(buffers); | |
b32b8144 FG |
89 | boost::asio::post(get_executor(), |
90 | boost::asio::detail::bind_handler( | |
91 | BOOST_ASIO_MOVE_CAST(Handler)(handler), | |
92 | boost::system::error_code(), bytes_transferred)); | |
7c673cae FG |
93 | } |
94 | ||
95 | private: | |
b32b8144 | 96 | boost::asio::io_context& io_context_; |
7c673cae FG |
97 | enum { max_length = 8192 }; |
98 | char data_[max_length]; | |
99 | size_t length_; | |
100 | size_t position_; | |
101 | size_t next_read_length_; | |
102 | }; | |
103 | ||
104 | static const char read_data[] | |
105 | = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
106 | ||
107 | void test_char_read_until() | |
108 | { | |
b32b8144 FG |
109 | boost::asio::io_context ioc; |
110 | test_stream s(ioc); | |
7c673cae FG |
111 | boost::asio::streambuf sb1; |
112 | boost::asio::streambuf sb2(25); | |
113 | boost::system::error_code ec; | |
114 | ||
115 | s.reset(read_data, sizeof(read_data)); | |
116 | sb1.consume(sb1.size()); | |
117 | std::size_t length = boost::asio::read_until(s, sb1, 'Z'); | |
118 | BOOST_ASIO_CHECK(length == 26); | |
119 | ||
120 | s.reset(read_data, sizeof(read_data)); | |
121 | s.next_read_length(1); | |
122 | sb1.consume(sb1.size()); | |
123 | length = boost::asio::read_until(s, sb1, 'Z'); | |
124 | BOOST_ASIO_CHECK(length == 26); | |
125 | ||
126 | s.reset(read_data, sizeof(read_data)); | |
127 | s.next_read_length(10); | |
128 | sb1.consume(sb1.size()); | |
129 | length = boost::asio::read_until(s, sb1, 'Z'); | |
130 | BOOST_ASIO_CHECK(length == 26); | |
131 | ||
132 | s.reset(read_data, sizeof(read_data)); | |
133 | sb1.consume(sb1.size()); | |
134 | length = boost::asio::read_until(s, sb1, 'Z', ec); | |
135 | BOOST_ASIO_CHECK(!ec); | |
136 | BOOST_ASIO_CHECK(length == 26); | |
137 | ||
138 | s.reset(read_data, sizeof(read_data)); | |
139 | s.next_read_length(1); | |
140 | sb1.consume(sb1.size()); | |
141 | length = boost::asio::read_until(s, sb1, 'Z', ec); | |
142 | BOOST_ASIO_CHECK(!ec); | |
143 | BOOST_ASIO_CHECK(length == 26); | |
144 | ||
145 | s.reset(read_data, sizeof(read_data)); | |
146 | s.next_read_length(10); | |
147 | sb1.consume(sb1.size()); | |
148 | length = boost::asio::read_until(s, sb1, 'Z', ec); | |
149 | BOOST_ASIO_CHECK(!ec); | |
150 | BOOST_ASIO_CHECK(length == 26); | |
151 | ||
152 | s.reset(read_data, sizeof(read_data)); | |
153 | sb2.consume(sb2.size()); | |
154 | length = boost::asio::read_until(s, sb2, 'Z', ec); | |
155 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
156 | BOOST_ASIO_CHECK(length == 0); | |
157 | ||
158 | s.reset(read_data, sizeof(read_data)); | |
159 | s.next_read_length(1); | |
160 | sb2.consume(sb2.size()); | |
161 | length = boost::asio::read_until(s, sb2, 'Z', ec); | |
162 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
163 | BOOST_ASIO_CHECK(length == 0); | |
164 | ||
165 | s.reset(read_data, sizeof(read_data)); | |
166 | s.next_read_length(10); | |
167 | sb2.consume(sb2.size()); | |
168 | length = boost::asio::read_until(s, sb2, 'Z', ec); | |
169 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
170 | BOOST_ASIO_CHECK(length == 0); | |
171 | ||
172 | s.reset(read_data, sizeof(read_data)); | |
173 | sb2.consume(sb2.size()); | |
174 | length = boost::asio::read_until(s, sb2, 'Y', ec); | |
175 | BOOST_ASIO_CHECK(!ec); | |
176 | BOOST_ASIO_CHECK(length == 25); | |
177 | ||
178 | s.reset(read_data, sizeof(read_data)); | |
179 | s.next_read_length(1); | |
180 | sb2.consume(sb2.size()); | |
181 | length = boost::asio::read_until(s, sb2, 'Y', ec); | |
182 | BOOST_ASIO_CHECK(!ec); | |
183 | BOOST_ASIO_CHECK(length == 25); | |
184 | ||
185 | s.reset(read_data, sizeof(read_data)); | |
186 | s.next_read_length(10); | |
187 | sb2.consume(sb2.size()); | |
188 | length = boost::asio::read_until(s, sb2, 'Y', ec); | |
189 | BOOST_ASIO_CHECK(!ec); | |
190 | BOOST_ASIO_CHECK(length == 25); | |
191 | } | |
192 | ||
193 | void test_string_read_until() | |
194 | { | |
b32b8144 FG |
195 | boost::asio::io_context ioc; |
196 | test_stream s(ioc); | |
7c673cae FG |
197 | boost::asio::streambuf sb1; |
198 | boost::asio::streambuf sb2(25); | |
199 | boost::system::error_code ec; | |
200 | ||
201 | s.reset(read_data, sizeof(read_data)); | |
202 | sb1.consume(sb1.size()); | |
203 | std::size_t length = boost::asio::read_until(s, sb1, "XYZ"); | |
204 | BOOST_ASIO_CHECK(length == 26); | |
205 | ||
206 | s.reset(read_data, sizeof(read_data)); | |
207 | s.next_read_length(1); | |
208 | sb1.consume(sb1.size()); | |
209 | length = boost::asio::read_until(s, sb1, "XYZ"); | |
210 | BOOST_ASIO_CHECK(length == 26); | |
211 | ||
212 | s.reset(read_data, sizeof(read_data)); | |
213 | s.next_read_length(10); | |
214 | sb1.consume(sb1.size()); | |
215 | length = boost::asio::read_until(s, sb1, "XYZ"); | |
216 | BOOST_ASIO_CHECK(length == 26); | |
217 | ||
218 | s.reset(read_data, sizeof(read_data)); | |
219 | sb1.consume(sb1.size()); | |
220 | length = boost::asio::read_until(s, sb1, "XYZ", ec); | |
221 | BOOST_ASIO_CHECK(!ec); | |
222 | BOOST_ASIO_CHECK(length == 26); | |
223 | ||
224 | s.reset(read_data, sizeof(read_data)); | |
225 | s.next_read_length(1); | |
226 | sb1.consume(sb1.size()); | |
227 | length = boost::asio::read_until(s, sb1, "XYZ", ec); | |
228 | BOOST_ASIO_CHECK(!ec); | |
229 | BOOST_ASIO_CHECK(length == 26); | |
230 | ||
231 | s.reset(read_data, sizeof(read_data)); | |
232 | s.next_read_length(10); | |
233 | sb1.consume(sb1.size()); | |
234 | length = boost::asio::read_until(s, sb1, "XYZ", ec); | |
235 | BOOST_ASIO_CHECK(!ec); | |
236 | BOOST_ASIO_CHECK(length == 26); | |
237 | ||
238 | s.reset(read_data, sizeof(read_data)); | |
239 | sb2.consume(sb2.size()); | |
240 | length = boost::asio::read_until(s, sb2, "XYZ", ec); | |
241 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
242 | BOOST_ASIO_CHECK(length == 0); | |
243 | ||
244 | s.reset(read_data, sizeof(read_data)); | |
245 | s.next_read_length(1); | |
246 | sb2.consume(sb2.size()); | |
247 | length = boost::asio::read_until(s, sb2, "XYZ", ec); | |
248 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
249 | BOOST_ASIO_CHECK(length == 0); | |
250 | ||
251 | s.reset(read_data, sizeof(read_data)); | |
252 | s.next_read_length(10); | |
253 | sb2.consume(sb2.size()); | |
254 | length = boost::asio::read_until(s, sb2, "XYZ", ec); | |
255 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
256 | BOOST_ASIO_CHECK(length == 0); | |
257 | ||
258 | s.reset(read_data, sizeof(read_data)); | |
259 | sb2.consume(sb2.size()); | |
260 | length = boost::asio::read_until(s, sb2, "WXY", ec); | |
261 | BOOST_ASIO_CHECK(!ec); | |
262 | BOOST_ASIO_CHECK(length == 25); | |
263 | ||
264 | s.reset(read_data, sizeof(read_data)); | |
265 | s.next_read_length(1); | |
266 | sb2.consume(sb2.size()); | |
267 | length = boost::asio::read_until(s, sb2, "WXY", ec); | |
268 | BOOST_ASIO_CHECK(!ec); | |
269 | BOOST_ASIO_CHECK(length == 25); | |
270 | ||
271 | s.reset(read_data, sizeof(read_data)); | |
272 | s.next_read_length(10); | |
273 | sb2.consume(sb2.size()); | |
274 | length = boost::asio::read_until(s, sb2, "WXY", ec); | |
275 | BOOST_ASIO_CHECK(!ec); | |
276 | BOOST_ASIO_CHECK(length == 25); | |
277 | } | |
278 | ||
279 | class match_char | |
280 | { | |
281 | public: | |
282 | explicit match_char(char c) : c_(c) {} | |
283 | ||
284 | template <typename Iterator> | |
285 | std::pair<Iterator, bool> operator()( | |
286 | Iterator begin, Iterator end) const | |
287 | { | |
288 | Iterator i = begin; | |
289 | while (i != end) | |
290 | if (c_ == *i++) | |
291 | return std::make_pair(i, true); | |
292 | return std::make_pair(i, false); | |
293 | } | |
294 | ||
295 | private: | |
296 | char c_; | |
297 | }; | |
298 | ||
299 | namespace boost { | |
300 | namespace asio { | |
301 | template <> struct is_match_condition<match_char> | |
302 | { | |
303 | enum { value = true }; | |
304 | }; | |
305 | } // namespace asio | |
306 | } // namespace boost | |
307 | ||
308 | void test_match_condition_read_until() | |
309 | { | |
b32b8144 FG |
310 | boost::asio::io_context ioc; |
311 | test_stream s(ioc); | |
7c673cae FG |
312 | boost::asio::streambuf sb1; |
313 | boost::asio::streambuf sb2(25); | |
314 | boost::system::error_code ec; | |
315 | ||
316 | s.reset(read_data, sizeof(read_data)); | |
317 | sb1.consume(sb1.size()); | |
318 | std::size_t length = boost::asio::read_until(s, sb1, match_char('Z')); | |
319 | BOOST_ASIO_CHECK(length == 26); | |
320 | ||
321 | s.reset(read_data, sizeof(read_data)); | |
322 | s.next_read_length(1); | |
323 | sb1.consume(sb1.size()); | |
324 | length = boost::asio::read_until(s, sb1, match_char('Z')); | |
325 | BOOST_ASIO_CHECK(length == 26); | |
326 | ||
327 | s.reset(read_data, sizeof(read_data)); | |
328 | s.next_read_length(10); | |
329 | sb1.consume(sb1.size()); | |
330 | length = boost::asio::read_until(s, sb1, match_char('Z')); | |
331 | BOOST_ASIO_CHECK(length == 26); | |
332 | ||
333 | s.reset(read_data, sizeof(read_data)); | |
334 | sb1.consume(sb1.size()); | |
335 | length = boost::asio::read_until(s, sb1, match_char('Z'), ec); | |
336 | BOOST_ASIO_CHECK(!ec); | |
337 | BOOST_ASIO_CHECK(length == 26); | |
338 | ||
339 | s.reset(read_data, sizeof(read_data)); | |
340 | s.next_read_length(1); | |
341 | sb1.consume(sb1.size()); | |
342 | length = boost::asio::read_until(s, sb1, match_char('Z'), ec); | |
343 | BOOST_ASIO_CHECK(!ec); | |
344 | BOOST_ASIO_CHECK(length == 26); | |
345 | ||
346 | s.reset(read_data, sizeof(read_data)); | |
347 | s.next_read_length(10); | |
348 | sb1.consume(sb1.size()); | |
349 | length = boost::asio::read_until(s, sb1, match_char('Z'), ec); | |
350 | BOOST_ASIO_CHECK(!ec); | |
351 | BOOST_ASIO_CHECK(length == 26); | |
352 | ||
353 | s.reset(read_data, sizeof(read_data)); | |
354 | sb2.consume(sb2.size()); | |
355 | length = boost::asio::read_until(s, sb2, match_char('Z'), ec); | |
356 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
357 | BOOST_ASIO_CHECK(length == 0); | |
358 | ||
359 | s.reset(read_data, sizeof(read_data)); | |
360 | s.next_read_length(1); | |
361 | sb2.consume(sb2.size()); | |
362 | length = boost::asio::read_until(s, sb2, match_char('Z'), ec); | |
363 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
364 | BOOST_ASIO_CHECK(length == 0); | |
365 | ||
366 | s.reset(read_data, sizeof(read_data)); | |
367 | s.next_read_length(10); | |
368 | sb2.consume(sb2.size()); | |
369 | length = boost::asio::read_until(s, sb2, match_char('Z'), ec); | |
370 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
371 | BOOST_ASIO_CHECK(length == 0); | |
372 | ||
373 | s.reset(read_data, sizeof(read_data)); | |
374 | sb2.consume(sb2.size()); | |
375 | length = boost::asio::read_until(s, sb2, match_char('Y'), ec); | |
376 | BOOST_ASIO_CHECK(!ec); | |
377 | BOOST_ASIO_CHECK(length == 25); | |
378 | ||
379 | s.reset(read_data, sizeof(read_data)); | |
380 | s.next_read_length(1); | |
381 | sb2.consume(sb2.size()); | |
382 | length = boost::asio::read_until(s, sb2, match_char('Y'), ec); | |
383 | BOOST_ASIO_CHECK(!ec); | |
384 | BOOST_ASIO_CHECK(length == 25); | |
385 | ||
386 | s.reset(read_data, sizeof(read_data)); | |
387 | s.next_read_length(10); | |
388 | sb2.consume(sb2.size()); | |
389 | length = boost::asio::read_until(s, sb2, match_char('Y'), ec); | |
390 | BOOST_ASIO_CHECK(!ec); | |
391 | BOOST_ASIO_CHECK(length == 25); | |
392 | } | |
393 | ||
394 | void async_read_handler( | |
395 | const boost::system::error_code& err, boost::system::error_code* err_out, | |
396 | std::size_t bytes_transferred, std::size_t* bytes_out, bool* called) | |
397 | { | |
398 | *err_out = err; | |
399 | *bytes_out = bytes_transferred; | |
400 | *called = true; | |
401 | } | |
402 | ||
403 | void test_char_async_read_until() | |
404 | { | |
405 | #if defined(BOOST_ASIO_HAS_BOOST_BIND) | |
406 | namespace bindns = boost; | |
407 | #else // defined(BOOST_ASIO_HAS_BOOST_BIND) | |
408 | namespace bindns = std; | |
409 | using std::placeholders::_1; | |
410 | using std::placeholders::_2; | |
411 | #endif // defined(BOOST_ASIO_HAS_BOOST_BIND) | |
412 | ||
b32b8144 FG |
413 | boost::asio::io_context ioc; |
414 | test_stream s(ioc); | |
7c673cae FG |
415 | boost::asio::streambuf sb1; |
416 | boost::asio::streambuf sb2(25); | |
417 | boost::system::error_code ec; | |
418 | std::size_t length; | |
419 | bool called; | |
420 | ||
421 | s.reset(read_data, sizeof(read_data)); | |
422 | ec = boost::system::error_code(); | |
423 | length = 0; | |
424 | called = false; | |
425 | sb1.consume(sb1.size()); | |
426 | boost::asio::async_read_until(s, sb1, 'Z', | |
427 | bindns::bind(async_read_handler, _1, &ec, | |
428 | _2, &length, &called)); | |
b32b8144 FG |
429 | ioc.restart(); |
430 | ioc.run(); | |
7c673cae FG |
431 | BOOST_ASIO_CHECK(called); |
432 | BOOST_ASIO_CHECK(!ec); | |
433 | BOOST_ASIO_CHECK(length == 26); | |
434 | ||
435 | s.reset(read_data, sizeof(read_data)); | |
436 | s.next_read_length(1); | |
437 | ec = boost::system::error_code(); | |
438 | length = 0; | |
439 | called = false; | |
440 | sb1.consume(sb1.size()); | |
441 | boost::asio::async_read_until(s, sb1, 'Z', | |
442 | bindns::bind(async_read_handler, _1, &ec, | |
443 | _2, &length, &called)); | |
b32b8144 FG |
444 | ioc.restart(); |
445 | ioc.run(); | |
7c673cae FG |
446 | BOOST_ASIO_CHECK(called); |
447 | BOOST_ASIO_CHECK(!ec); | |
448 | BOOST_ASIO_CHECK(length == 26); | |
449 | ||
450 | s.reset(read_data, sizeof(read_data)); | |
451 | s.next_read_length(10); | |
452 | ec = boost::system::error_code(); | |
453 | length = 0; | |
454 | called = false; | |
455 | sb1.consume(sb1.size()); | |
456 | boost::asio::async_read_until(s, sb1, 'Z', | |
457 | bindns::bind(async_read_handler, _1, &ec, | |
458 | _2, &length, &called)); | |
b32b8144 FG |
459 | ioc.restart(); |
460 | ioc.run(); | |
7c673cae FG |
461 | BOOST_ASIO_CHECK(called); |
462 | BOOST_ASIO_CHECK(!ec); | |
463 | BOOST_ASIO_CHECK(length == 26); | |
464 | ||
465 | s.reset(read_data, sizeof(read_data)); | |
466 | ec = boost::system::error_code(); | |
467 | length = 0; | |
468 | called = false; | |
469 | sb2.consume(sb2.size()); | |
470 | boost::asio::async_read_until(s, sb2, 'Z', | |
471 | bindns::bind(async_read_handler, _1, &ec, | |
472 | _2, &length, &called)); | |
b32b8144 FG |
473 | ioc.restart(); |
474 | ioc.run(); | |
7c673cae FG |
475 | BOOST_ASIO_CHECK(called); |
476 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
477 | BOOST_ASIO_CHECK(length == 0); | |
478 | ||
479 | s.reset(read_data, sizeof(read_data)); | |
480 | s.next_read_length(1); | |
481 | ec = boost::system::error_code(); | |
482 | length = 0; | |
483 | called = false; | |
484 | sb2.consume(sb2.size()); | |
485 | boost::asio::async_read_until(s, sb2, 'Z', | |
486 | bindns::bind(async_read_handler, _1, &ec, | |
487 | _2, &length, &called)); | |
b32b8144 FG |
488 | ioc.restart(); |
489 | ioc.run(); | |
7c673cae FG |
490 | BOOST_ASIO_CHECK(called); |
491 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
492 | BOOST_ASIO_CHECK(length == 0); | |
493 | ||
494 | s.reset(read_data, sizeof(read_data)); | |
495 | s.next_read_length(10); | |
496 | ec = boost::system::error_code(); | |
497 | length = 0; | |
498 | called = false; | |
499 | sb2.consume(sb2.size()); | |
500 | boost::asio::async_read_until(s, sb2, 'Z', | |
501 | bindns::bind(async_read_handler, _1, &ec, | |
502 | _2, &length, &called)); | |
b32b8144 FG |
503 | ioc.restart(); |
504 | ioc.run(); | |
7c673cae FG |
505 | BOOST_ASIO_CHECK(called); |
506 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
507 | BOOST_ASIO_CHECK(length == 0); | |
508 | ||
509 | s.reset(read_data, sizeof(read_data)); | |
510 | ec = boost::system::error_code(); | |
511 | length = 0; | |
512 | called = false; | |
513 | sb2.consume(sb2.size()); | |
514 | boost::asio::async_read_until(s, sb2, 'Y', | |
515 | bindns::bind(async_read_handler, _1, &ec, | |
516 | _2, &length, &called)); | |
b32b8144 FG |
517 | ioc.restart(); |
518 | ioc.run(); | |
7c673cae FG |
519 | BOOST_ASIO_CHECK(called); |
520 | BOOST_ASIO_CHECK(!ec); | |
521 | BOOST_ASIO_CHECK(length == 25); | |
522 | ||
523 | s.reset(read_data, sizeof(read_data)); | |
524 | s.next_read_length(1); | |
525 | ec = boost::system::error_code(); | |
526 | length = 0; | |
527 | called = false; | |
528 | sb2.consume(sb2.size()); | |
529 | boost::asio::async_read_until(s, sb2, 'Y', | |
530 | bindns::bind(async_read_handler, _1, &ec, | |
531 | _2, &length, &called)); | |
b32b8144 FG |
532 | ioc.restart(); |
533 | ioc.run(); | |
7c673cae FG |
534 | BOOST_ASIO_CHECK(called); |
535 | BOOST_ASIO_CHECK(!ec); | |
536 | BOOST_ASIO_CHECK(length == 25); | |
537 | ||
538 | s.reset(read_data, sizeof(read_data)); | |
539 | s.next_read_length(10); | |
540 | ec = boost::system::error_code(); | |
541 | length = 0; | |
542 | called = false; | |
543 | sb2.consume(sb2.size()); | |
544 | boost::asio::async_read_until(s, sb2, 'Y', | |
545 | bindns::bind(async_read_handler, _1, &ec, | |
546 | _2, &length, &called)); | |
b32b8144 FG |
547 | ioc.restart(); |
548 | ioc.run(); | |
7c673cae FG |
549 | BOOST_ASIO_CHECK(called); |
550 | BOOST_ASIO_CHECK(!ec); | |
551 | BOOST_ASIO_CHECK(length == 25); | |
552 | ||
553 | s.reset(read_data, sizeof(read_data)); | |
554 | sb2.consume(sb2.size()); | |
555 | int i = boost::asio::async_read_until(s, sb2, 'Y', | |
556 | archetypes::lazy_handler()); | |
557 | BOOST_ASIO_CHECK(i == 42); | |
b32b8144 FG |
558 | ioc.restart(); |
559 | ioc.run(); | |
7c673cae FG |
560 | } |
561 | ||
562 | void test_string_async_read_until() | |
563 | { | |
564 | #if defined(BOOST_ASIO_HAS_BOOST_BIND) | |
565 | namespace bindns = boost; | |
566 | #else // defined(BOOST_ASIO_HAS_BOOST_BIND) | |
567 | namespace bindns = std; | |
568 | using std::placeholders::_1; | |
569 | using std::placeholders::_2; | |
570 | #endif // defined(BOOST_ASIO_HAS_BOOST_BIND) | |
571 | ||
b32b8144 FG |
572 | boost::asio::io_context ioc; |
573 | test_stream s(ioc); | |
7c673cae FG |
574 | boost::asio::streambuf sb1; |
575 | boost::asio::streambuf sb2(25); | |
576 | boost::system::error_code ec; | |
577 | std::size_t length; | |
578 | bool called; | |
579 | ||
580 | s.reset(read_data, sizeof(read_data)); | |
581 | ec = boost::system::error_code(); | |
582 | length = 0; | |
583 | called = false; | |
584 | sb1.consume(sb1.size()); | |
585 | boost::asio::async_read_until(s, sb1, "XYZ", | |
586 | bindns::bind(async_read_handler, _1, &ec, | |
587 | _2, &length, &called)); | |
b32b8144 FG |
588 | ioc.restart(); |
589 | ioc.run(); | |
7c673cae FG |
590 | BOOST_ASIO_CHECK(called); |
591 | BOOST_ASIO_CHECK(!ec); | |
592 | BOOST_ASIO_CHECK(length == 26); | |
593 | ||
594 | s.reset(read_data, sizeof(read_data)); | |
595 | s.next_read_length(1); | |
596 | ec = boost::system::error_code(); | |
597 | length = 0; | |
598 | called = false; | |
599 | sb1.consume(sb1.size()); | |
600 | boost::asio::async_read_until(s, sb1, "XYZ", | |
601 | bindns::bind(async_read_handler, _1, &ec, | |
602 | _2, &length, &called)); | |
b32b8144 FG |
603 | ioc.restart(); |
604 | ioc.run(); | |
7c673cae FG |
605 | BOOST_ASIO_CHECK(called); |
606 | BOOST_ASIO_CHECK(!ec); | |
607 | BOOST_ASIO_CHECK(length == 26); | |
608 | ||
609 | s.reset(read_data, sizeof(read_data)); | |
610 | s.next_read_length(10); | |
611 | ec = boost::system::error_code(); | |
612 | length = 0; | |
613 | called = false; | |
614 | sb1.consume(sb1.size()); | |
615 | boost::asio::async_read_until(s, sb1, "XYZ", | |
616 | bindns::bind(async_read_handler, _1, &ec, | |
617 | _2, &length, &called)); | |
b32b8144 FG |
618 | ioc.restart(); |
619 | ioc.run(); | |
7c673cae FG |
620 | BOOST_ASIO_CHECK(called); |
621 | BOOST_ASIO_CHECK(!ec); | |
622 | BOOST_ASIO_CHECK(length == 26); | |
623 | ||
624 | s.reset(read_data, sizeof(read_data)); | |
625 | ec = boost::system::error_code(); | |
626 | length = 0; | |
627 | called = false; | |
628 | sb2.consume(sb2.size()); | |
629 | boost::asio::async_read_until(s, sb2, "XYZ", | |
630 | bindns::bind(async_read_handler, _1, &ec, | |
631 | _2, &length, &called)); | |
b32b8144 FG |
632 | ioc.restart(); |
633 | ioc.run(); | |
7c673cae FG |
634 | BOOST_ASIO_CHECK(called); |
635 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
636 | BOOST_ASIO_CHECK(length == 0); | |
637 | ||
638 | s.reset(read_data, sizeof(read_data)); | |
639 | s.next_read_length(1); | |
640 | ec = boost::system::error_code(); | |
641 | length = 0; | |
642 | called = false; | |
643 | sb2.consume(sb2.size()); | |
644 | boost::asio::async_read_until(s, sb2, "XYZ", | |
645 | bindns::bind(async_read_handler, _1, &ec, | |
646 | _2, &length, &called)); | |
b32b8144 FG |
647 | ioc.restart(); |
648 | ioc.run(); | |
7c673cae FG |
649 | BOOST_ASIO_CHECK(called); |
650 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
651 | BOOST_ASIO_CHECK(length == 0); | |
652 | ||
653 | s.reset(read_data, sizeof(read_data)); | |
654 | s.next_read_length(10); | |
655 | ec = boost::system::error_code(); | |
656 | length = 0; | |
657 | called = false; | |
658 | sb2.consume(sb2.size()); | |
659 | boost::asio::async_read_until(s, sb2, "XYZ", | |
660 | bindns::bind(async_read_handler, _1, &ec, | |
661 | _2, &length, &called)); | |
b32b8144 FG |
662 | ioc.restart(); |
663 | ioc.run(); | |
7c673cae FG |
664 | BOOST_ASIO_CHECK(called); |
665 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
666 | BOOST_ASIO_CHECK(length == 0); | |
667 | ||
668 | s.reset(read_data, sizeof(read_data)); | |
669 | ec = boost::system::error_code(); | |
670 | length = 0; | |
671 | called = false; | |
672 | sb2.consume(sb2.size()); | |
673 | boost::asio::async_read_until(s, sb2, "WXY", | |
674 | bindns::bind(async_read_handler, _1, &ec, | |
675 | _2, &length, &called)); | |
b32b8144 FG |
676 | ioc.restart(); |
677 | ioc.run(); | |
7c673cae FG |
678 | BOOST_ASIO_CHECK(called); |
679 | BOOST_ASIO_CHECK(!ec); | |
680 | BOOST_ASIO_CHECK(length == 25); | |
681 | ||
682 | s.reset(read_data, sizeof(read_data)); | |
683 | s.next_read_length(1); | |
684 | ec = boost::system::error_code(); | |
685 | length = 0; | |
686 | called = false; | |
687 | sb2.consume(sb2.size()); | |
688 | boost::asio::async_read_until(s, sb2, "WXY", | |
689 | bindns::bind(async_read_handler, _1, &ec, | |
690 | _2, &length, &called)); | |
b32b8144 FG |
691 | ioc.restart(); |
692 | ioc.run(); | |
7c673cae FG |
693 | BOOST_ASIO_CHECK(called); |
694 | BOOST_ASIO_CHECK(!ec); | |
695 | BOOST_ASIO_CHECK(length == 25); | |
696 | ||
697 | s.reset(read_data, sizeof(read_data)); | |
698 | s.next_read_length(10); | |
699 | ec = boost::system::error_code(); | |
700 | length = 0; | |
701 | called = false; | |
702 | sb2.consume(sb2.size()); | |
703 | boost::asio::async_read_until(s, sb2, "WXY", | |
704 | bindns::bind(async_read_handler, _1, &ec, | |
705 | _2, &length, &called)); | |
b32b8144 FG |
706 | ioc.restart(); |
707 | ioc.run(); | |
7c673cae FG |
708 | BOOST_ASIO_CHECK(called); |
709 | BOOST_ASIO_CHECK(!ec); | |
710 | BOOST_ASIO_CHECK(length == 25); | |
711 | ||
712 | s.reset(read_data, sizeof(read_data)); | |
713 | sb2.consume(sb2.size()); | |
714 | int i = boost::asio::async_read_until(s, sb2, "WXY", | |
715 | archetypes::lazy_handler()); | |
716 | BOOST_ASIO_CHECK(i == 42); | |
b32b8144 FG |
717 | ioc.restart(); |
718 | ioc.run(); | |
7c673cae FG |
719 | } |
720 | ||
721 | void test_match_condition_async_read_until() | |
722 | { | |
723 | #if defined(BOOST_ASIO_HAS_BOOST_BIND) | |
724 | namespace bindns = boost; | |
725 | #else // defined(BOOST_ASIO_HAS_BOOST_BIND) | |
726 | namespace bindns = std; | |
727 | using std::placeholders::_1; | |
728 | using std::placeholders::_2; | |
729 | #endif // defined(BOOST_ASIO_HAS_BOOST_BIND) | |
730 | ||
b32b8144 FG |
731 | boost::asio::io_context ioc; |
732 | test_stream s(ioc); | |
7c673cae FG |
733 | boost::asio::streambuf sb1; |
734 | boost::asio::streambuf sb2(25); | |
735 | boost::system::error_code ec; | |
736 | std::size_t length; | |
737 | bool called; | |
738 | ||
739 | s.reset(read_data, sizeof(read_data)); | |
740 | ec = boost::system::error_code(); | |
741 | length = 0; | |
742 | called = false; | |
743 | sb1.consume(sb1.size()); | |
744 | boost::asio::async_read_until(s, sb1, match_char('Z'), | |
745 | bindns::bind(async_read_handler, _1, &ec, | |
746 | _2, &length, &called)); | |
b32b8144 FG |
747 | ioc.restart(); |
748 | ioc.run(); | |
7c673cae FG |
749 | BOOST_ASIO_CHECK(called); |
750 | BOOST_ASIO_CHECK(!ec); | |
751 | BOOST_ASIO_CHECK(length == 26); | |
752 | ||
753 | s.reset(read_data, sizeof(read_data)); | |
754 | s.next_read_length(1); | |
755 | ec = boost::system::error_code(); | |
756 | length = 0; | |
757 | called = false; | |
758 | sb1.consume(sb1.size()); | |
759 | boost::asio::async_read_until(s, sb1, match_char('Z'), | |
760 | bindns::bind(async_read_handler, _1, &ec, | |
761 | _2, &length, &called)); | |
b32b8144 FG |
762 | ioc.restart(); |
763 | ioc.run(); | |
7c673cae FG |
764 | BOOST_ASIO_CHECK(called); |
765 | BOOST_ASIO_CHECK(!ec); | |
766 | BOOST_ASIO_CHECK(length == 26); | |
767 | ||
768 | s.reset(read_data, sizeof(read_data)); | |
769 | s.next_read_length(10); | |
770 | ec = boost::system::error_code(); | |
771 | length = 0; | |
772 | called = false; | |
773 | sb1.consume(sb1.size()); | |
774 | boost::asio::async_read_until(s, sb1, match_char('Z'), | |
775 | bindns::bind(async_read_handler, _1, &ec, | |
776 | _2, &length, &called)); | |
b32b8144 FG |
777 | ioc.restart(); |
778 | ioc.run(); | |
7c673cae FG |
779 | BOOST_ASIO_CHECK(called); |
780 | BOOST_ASIO_CHECK(!ec); | |
781 | BOOST_ASIO_CHECK(length == 26); | |
782 | ||
783 | s.reset(read_data, sizeof(read_data)); | |
784 | ec = boost::system::error_code(); | |
785 | length = 0; | |
786 | called = false; | |
787 | sb2.consume(sb2.size()); | |
788 | boost::asio::async_read_until(s, sb2, match_char('Z'), | |
789 | bindns::bind(async_read_handler, _1, &ec, | |
790 | _2, &length, &called)); | |
b32b8144 FG |
791 | ioc.restart(); |
792 | ioc.run(); | |
7c673cae FG |
793 | BOOST_ASIO_CHECK(called); |
794 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
795 | BOOST_ASIO_CHECK(length == 0); | |
796 | ||
797 | s.reset(read_data, sizeof(read_data)); | |
798 | s.next_read_length(1); | |
799 | ec = boost::system::error_code(); | |
800 | length = 0; | |
801 | called = false; | |
802 | sb2.consume(sb2.size()); | |
803 | boost::asio::async_read_until(s, sb2, match_char('Z'), | |
804 | bindns::bind(async_read_handler, _1, &ec, | |
805 | _2, &length, &called)); | |
b32b8144 FG |
806 | ioc.restart(); |
807 | ioc.run(); | |
7c673cae FG |
808 | BOOST_ASIO_CHECK(called); |
809 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
810 | BOOST_ASIO_CHECK(length == 0); | |
811 | ||
812 | s.reset(read_data, sizeof(read_data)); | |
813 | s.next_read_length(10); | |
814 | ec = boost::system::error_code(); | |
815 | length = 0; | |
816 | called = false; | |
817 | sb2.consume(sb2.size()); | |
818 | boost::asio::async_read_until(s, sb2, match_char('Z'), | |
819 | bindns::bind(async_read_handler, _1, &ec, | |
820 | _2, &length, &called)); | |
b32b8144 FG |
821 | ioc.restart(); |
822 | ioc.run(); | |
7c673cae FG |
823 | BOOST_ASIO_CHECK(called); |
824 | BOOST_ASIO_CHECK(ec == boost::asio::error::not_found); | |
825 | BOOST_ASIO_CHECK(length == 0); | |
826 | ||
827 | s.reset(read_data, sizeof(read_data)); | |
828 | ec = boost::system::error_code(); | |
829 | length = 0; | |
830 | called = false; | |
831 | sb2.consume(sb2.size()); | |
832 | boost::asio::async_read_until(s, sb2, match_char('Y'), | |
833 | bindns::bind(async_read_handler, _1, &ec, | |
834 | _2, &length, &called)); | |
b32b8144 FG |
835 | ioc.restart(); |
836 | ioc.run(); | |
7c673cae FG |
837 | BOOST_ASIO_CHECK(called); |
838 | BOOST_ASIO_CHECK(!ec); | |
839 | BOOST_ASIO_CHECK(length == 25); | |
840 | ||
841 | s.reset(read_data, sizeof(read_data)); | |
842 | s.next_read_length(1); | |
843 | ec = boost::system::error_code(); | |
844 | length = 0; | |
845 | called = false; | |
846 | sb2.consume(sb2.size()); | |
847 | boost::asio::async_read_until(s, sb2, match_char('Y'), | |
848 | bindns::bind(async_read_handler, _1, &ec, | |
849 | _2, &length, &called)); | |
b32b8144 FG |
850 | ioc.restart(); |
851 | ioc.run(); | |
7c673cae FG |
852 | BOOST_ASIO_CHECK(called); |
853 | BOOST_ASIO_CHECK(!ec); | |
854 | BOOST_ASIO_CHECK(length == 25); | |
855 | ||
856 | s.reset(read_data, sizeof(read_data)); | |
857 | s.next_read_length(10); | |
858 | ec = boost::system::error_code(); | |
859 | length = 0; | |
860 | called = false; | |
861 | sb2.consume(sb2.size()); | |
862 | boost::asio::async_read_until(s, sb2, match_char('Y'), | |
863 | bindns::bind(async_read_handler, _1, &ec, | |
864 | _2, &length, &called)); | |
b32b8144 FG |
865 | ioc.restart(); |
866 | ioc.run(); | |
7c673cae FG |
867 | BOOST_ASIO_CHECK(called); |
868 | BOOST_ASIO_CHECK(!ec); | |
869 | BOOST_ASIO_CHECK(length == 25); | |
870 | ||
871 | s.reset(read_data, sizeof(read_data)); | |
872 | sb2.consume(sb2.size()); | |
873 | int i = boost::asio::async_read_until(s, sb2, match_char('Y'), | |
874 | archetypes::lazy_handler()); | |
875 | BOOST_ASIO_CHECK(i == 42); | |
b32b8144 FG |
876 | ioc.restart(); |
877 | ioc.run(); | |
7c673cae FG |
878 | } |
879 | ||
880 | BOOST_ASIO_TEST_SUITE | |
881 | ( | |
882 | "read_until", | |
883 | BOOST_ASIO_TEST_CASE(test_char_read_until) | |
884 | BOOST_ASIO_TEST_CASE(test_string_read_until) | |
885 | BOOST_ASIO_TEST_CASE(test_match_condition_read_until) | |
886 | BOOST_ASIO_TEST_CASE(test_char_async_read_until) | |
887 | BOOST_ASIO_TEST_CASE(test_string_async_read_until) | |
888 | BOOST_ASIO_TEST_CASE(test_match_condition_async_read_until) | |
889 | ) |