28 #define BOOST_TEST_MODULE permessage_deflate 29 #include <boost/test/unit_test.hpp> 50 websocketpp::lib::error_code
ec;
89 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
90 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
95 v.
attr[
"foo"] =
"bar";
100 BOOST_CHECK_EQUAL( v.
esp.second,
"");
106 v.
attr[
"server_no_context_takeover"] =
"foo";
111 BOOST_CHECK_EQUAL( v.
esp.second,
"");
116 v.
attr[
"server_no_context_takeover"].clear();
120 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
121 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover");
130 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
131 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover");
137 v.
attr[
"client_no_context_takeover"] =
"foo";
142 BOOST_CHECK_EQUAL( v.
esp.second,
"");
147 v.
attr[
"client_no_context_takeover"].clear();
151 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
152 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_no_context_takeover");
161 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
162 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_no_context_takeover");
170 std::vector<std::string> values;
171 values.push_back(
"");
172 values.push_back(
"foo");
173 values.push_back(
"7");
174 values.push_back(
"16");
176 std::vector<std::string>::const_iterator it;
177 for (it = values.begin(); it != values.end(); ++it) {
178 v.
attr[
"server_max_window_bits"] = *it;
183 BOOST_CHECK_EQUAL( v.
esp.second,
"");
191 v.
attr[
"server_max_window_bits"] =
"8";
194 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
195 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=9");
197 v.
attr[
"server_max_window_bits"] =
"9";
200 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
201 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=9");
204 v.
attr[
"server_max_window_bits"] =
"15";
207 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
208 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
223 v.
attr[
"server_max_window_bits"] =
"9";
228 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
229 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
230 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
235 v.
attr[
"server_max_window_bits"] =
"8";
240 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
241 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
242 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=9");
247 v.
attr[
"server_max_window_bits"] =
"9";
252 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
253 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
254 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=9");
259 v.
attr[
"server_max_window_bits"] =
"8";
264 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
265 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
266 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=9");
271 v.
attr[
"server_max_window_bits"] =
"9";
276 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
277 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
278 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=9");
283 v.
attr[
"server_max_window_bits"] =
"8";
288 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
289 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
290 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=9");
295 v.
attr[
"server_max_window_bits"] =
"9";
300 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
301 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
302 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=9");
309 std::vector<std::string> values;
310 values.push_back(
"foo");
311 values.push_back(
"7");
312 values.push_back(
"16");
314 std::vector<std::string>::const_iterator it;
315 for (it = values.begin(); it != values.end(); ++it) {
316 v.
attr[
"client_max_window_bits"] = *it;
321 BOOST_CHECK_EQUAL( v.
esp.second,
"");
328 v.
attr[
"client_max_window_bits"].clear();
331 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
332 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
334 v.
attr[
"client_max_window_bits"] =
"8";
337 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
338 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=9");
340 v.
attr[
"client_max_window_bits"] =
"9";
343 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
344 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=9");
346 v.
attr[
"client_max_window_bits"] =
"15";
349 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
350 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
365 v.
attr[
"client_max_window_bits"] =
"8";
370 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
371 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
372 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
377 v.
attr[
"client_max_window_bits"] =
"9";
382 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
383 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
384 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
389 v.
attr[
"client_max_window_bits"] =
"8";
394 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
395 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
396 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=9");
401 v.
attr[
"client_max_window_bits"] =
"9";
406 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
407 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
408 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=9");
413 v.
attr[
"client_max_window_bits"] =
"8";
418 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
419 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
420 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=9");
425 v.
attr[
"client_max_window_bits"] =
"9";
430 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
431 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
432 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=9");
437 v.
attr[
"client_max_window_bits"] =
"8";
442 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
443 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
444 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=9");
449 v.
attr[
"client_max_window_bits"] =
"9";
454 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
455 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
456 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=9");
464 v.
attr[
"server_no_context_takeover"].clear();
465 v.
attr[
"client_no_context_takeover"].clear();
469 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
470 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_no_context_takeover");
476 v.
attr[
"server_no_context_takeover"].clear();
477 v.
attr[
"server_max_window_bits"] =
"10";
481 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
482 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; server_max_window_bits=10");
488 v.
attr[
"server_no_context_takeover"].clear();
489 v.
attr[
"client_max_window_bits"] =
"10";
493 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
494 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_max_window_bits=10");
500 v.
attr[
"client_no_context_takeover"].clear();
501 v.
attr[
"server_max_window_bits"] =
"10";
505 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
506 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_no_context_takeover; server_max_window_bits=10");
512 v.
attr[
"client_no_context_takeover"].clear();
513 v.
attr[
"client_max_window_bits"] =
"10";
517 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
518 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_no_context_takeover; client_max_window_bits=10");
524 v.
attr[
"server_max_window_bits"] =
"10";
525 v.
attr[
"client_max_window_bits"] =
"10";
529 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
530 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=10; client_max_window_bits=10");
536 v.
attr[
"server_no_context_takeover"].clear();
537 v.
attr[
"client_no_context_takeover"].clear();
538 v.
attr[
"server_max_window_bits"] =
"10";
542 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
543 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_no_context_takeover; server_max_window_bits=10");
549 v.
attr[
"server_no_context_takeover"].clear();
550 v.
attr[
"client_no_context_takeover"].clear();
551 v.
attr[
"client_max_window_bits"] =
"10";
555 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
556 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_no_context_takeover; client_max_window_bits=10");
562 v.
attr[
"server_no_context_takeover"].clear();
563 v.
attr[
"server_max_window_bits"] =
"10";
564 v.
attr[
"client_max_window_bits"] =
"10";
568 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
569 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; server_max_window_bits=10; client_max_window_bits=10");
575 v.
attr[
"client_no_context_takeover"].clear();
576 v.
attr[
"server_max_window_bits"] =
"10";
577 v.
attr[
"client_max_window_bits"] =
"10";
581 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
582 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_no_context_takeover; server_max_window_bits=10; client_max_window_bits=10");
588 v.
attr[
"server_no_context_takeover"].clear();
589 v.
attr[
"client_no_context_takeover"].clear();
590 v.
attr[
"server_max_window_bits"] =
"10";
591 v.
attr[
"client_max_window_bits"] =
"10";
595 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
596 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_no_context_takeover; server_max_window_bits=10; client_max_window_bits=10");
603 std::string compress_in =
"Hello";
604 std::string compress_out;
605 std::string decompress_out;
608 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
611 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
613 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
614 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
615 BOOST_CHECK_EQUAL( compress_in, decompress_out );
622 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
624 for (
int i = 0; i < 2; i++) {
625 std::string compress_in =
"Hello";
626 std::string compress_out;
627 std::string decompress_out;
630 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
632 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
633 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
634 BOOST_CHECK_EQUAL( compress_in, decompress_out );
641 std::string compress_in(600,
'*');
642 std::string compress_out;
643 std::string decompress_out;
647 alist[
"server_max_window_bits"] =
"9";
652 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
655 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
657 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
658 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
659 BOOST_CHECK_EQUAL( compress_in, decompress_out );
665 std::string compress_in =
"Hello";
666 std::string compress_out1;
667 std::string compress_out2;
668 std::string decompress_out;
672 alist[
"server_no_context_takeover"].clear();
677 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
680 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
683 reinterpret_cast<const uint8_t *>(compress_out1.data()),
684 compress_out1.size(),
687 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
688 BOOST_CHECK_EQUAL( compress_in, decompress_out );
690 decompress_out.clear();
693 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
696 reinterpret_cast<const uint8_t *>(compress_out2.data()),
697 compress_out2.size(),
700 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
701 BOOST_CHECK_EQUAL( compress_in, decompress_out );
703 BOOST_CHECK_EQUAL( compress_out1, compress_out2 );
709 std::string compress_in;
710 std::string compress_out;
711 std::string decompress_out;
714 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
717 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
719 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
721 compress_out.clear();
722 decompress_out.clear();
725 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
727 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
728 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
729 BOOST_CHECK_EQUAL( compress_in, decompress_out );
741 uint8_t in[11] = {0xf2, 0x48, 0xcd, 0xc9, 0xc9, 0x07, 0x00, 0x00, 0x00, 0xff, 0xff};
743 std::string reference =
"Hello";
746 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
750 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
751 BOOST_CHECK_EQUAL( out, reference );
lib::error_code set_server_max_window_bits(uint8_t bits, mode::value mode)
Limit server LZ77 sliding window size.
void enable_client_no_context_takeover()
Reset client's outgoing LZ77 sliding window for each new message.
lib::error_code make_error_code(error::value e)
BOOST_AUTO_TEST_CASE(disabled_is_disabled)
Decline any value the remote endpoint offers. Insist on defaults.
Accept any value the remote endpoint offers.
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Invalid value for max_window_bits.
Invalid extension attribute value.
lib::error_code init(bool is_server)
Initialize zlib state.
Use the smallest value common to both offers.
lib::error_code set_client_max_window_bits(uint8_t bits, mode::value mode)
Limit client LZ77 sliding window size.
Stub class for use when disabling permessage_deflate extension.
lib::error_code decompress(uint8_t const *buf, size_t len, std::string &out)
Decompress bytes.
Use the largest value common to both offers.
bool is_implemented() const
Test if this object implements the permessage-deflate specification.
websocketpp::err_str_pair esp
websocketpp::extensions::permessage_deflate::enabled< config > enabled_type
bool is_implemented() const
Returns true if the extension is capable of providing permessage_deflate functionality.
bool is_enabled() const
Test if the extension was negotiated for this connection.
err_str_pair negotiate(http::attribute_list const &offer)
Negotiate extension.
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
bool is_enabled() const
Returns true if permessage_deflate functionality is active for this connection.
websocketpp::extensions::permessage_deflate::disabled< config > disabled_type
void enable_server_no_context_takeover()
Reset server's outgoing LZ77 sliding window for each new message.
Permessage deflate error values.
websocketpp::lib::error_code ec
lib::error_code compress(std::string const &in, std::string &out)
Compress bytes.
Invalid extension attributes.
websocketpp::http::attribute_list attr