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,
"");
189 v.
attr[
"server_max_window_bits"] =
"8";
193 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
194 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=8");
196 v.
attr[
"server_max_window_bits"] =
"15";
200 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
201 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
216 v.
attr[
"server_max_window_bits"] =
"8";
221 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
222 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
223 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
228 v.
attr[
"server_max_window_bits"] =
"8";
233 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
234 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
235 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=8");
240 v.
attr[
"server_max_window_bits"] =
"8";
245 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
246 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
247 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=8");
252 v.
attr[
"server_max_window_bits"] =
"8";
257 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
258 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
259 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=8");
266 std::vector<std::string> values;
267 values.push_back(
"foo");
268 values.push_back(
"7");
269 values.push_back(
"16");
271 std::vector<std::string>::const_iterator it;
272 for (it = values.begin(); it != values.end(); ++it) {
273 v.
attr[
"client_max_window_bits"] = *it;
278 BOOST_CHECK_EQUAL( v.
esp.second,
"");
285 v.
attr[
"client_max_window_bits"].clear();
288 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
289 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
291 v.
attr[
"client_max_window_bits"] =
"8";
294 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
295 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=8");
297 v.
attr[
"client_max_window_bits"] =
"15";
300 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
301 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
316 v.
attr[
"client_max_window_bits"] =
"8";
321 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
322 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
323 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate");
328 v.
attr[
"client_max_window_bits"] =
"8";
333 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
334 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
335 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=8");
340 v.
attr[
"client_max_window_bits"] =
"8";
345 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
346 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
347 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=8");
352 v.
attr[
"client_max_window_bits"] =
"8";
357 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
358 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
359 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_max_window_bits=8");
367 v.
attr[
"server_no_context_takeover"].clear();
368 v.
attr[
"client_no_context_takeover"].clear();
372 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
373 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_no_context_takeover");
379 v.
attr[
"server_no_context_takeover"].clear();
380 v.
attr[
"server_max_window_bits"] =
"10";
384 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
385 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; server_max_window_bits=10");
391 v.
attr[
"server_no_context_takeover"].clear();
392 v.
attr[
"client_max_window_bits"] =
"10";
396 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
397 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_max_window_bits=10");
403 v.
attr[
"client_no_context_takeover"].clear();
404 v.
attr[
"server_max_window_bits"] =
"10";
408 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
409 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_no_context_takeover; server_max_window_bits=10");
415 v.
attr[
"client_no_context_takeover"].clear();
416 v.
attr[
"client_max_window_bits"] =
"10";
420 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
421 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_no_context_takeover; client_max_window_bits=10");
427 v.
attr[
"server_max_window_bits"] =
"10";
428 v.
attr[
"client_max_window_bits"] =
"10";
432 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
433 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_max_window_bits=10; client_max_window_bits=10");
439 v.
attr[
"server_no_context_takeover"].clear();
440 v.
attr[
"client_no_context_takeover"].clear();
441 v.
attr[
"server_max_window_bits"] =
"10";
445 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
446 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_no_context_takeover; server_max_window_bits=10");
452 v.
attr[
"server_no_context_takeover"].clear();
453 v.
attr[
"client_no_context_takeover"].clear();
454 v.
attr[
"client_max_window_bits"] =
"10";
458 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
459 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; client_no_context_takeover; client_max_window_bits=10");
465 v.
attr[
"server_no_context_takeover"].clear();
466 v.
attr[
"server_max_window_bits"] =
"10";
467 v.
attr[
"client_max_window_bits"] =
"10";
471 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
472 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; server_no_context_takeover; server_max_window_bits=10; client_max_window_bits=10");
478 v.
attr[
"client_no_context_takeover"].clear();
479 v.
attr[
"server_max_window_bits"] =
"10";
480 v.
attr[
"client_max_window_bits"] =
"10";
484 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
485 BOOST_CHECK_EQUAL( v.
esp.second,
"permessage-deflate; client_no_context_takeover; server_max_window_bits=10; client_max_window_bits=10");
491 v.
attr[
"server_no_context_takeover"].clear();
492 v.
attr[
"client_no_context_takeover"].clear();
493 v.
attr[
"server_max_window_bits"] =
"10";
494 v.
attr[
"client_max_window_bits"] =
"10";
498 BOOST_CHECK_EQUAL( v.
esp.first, websocketpp::lib::error_code() );
499 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");
506 std::string compress_in =
"Hello";
507 std::string compress_out;
508 std::string decompress_out;
513 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
515 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
516 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
517 BOOST_CHECK_EQUAL( compress_in, decompress_out );
525 for (
int i = 0; i < 2; i++) {
526 std::string compress_in =
"Hello";
527 std::string compress_out;
528 std::string decompress_out;
531 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
533 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
534 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
535 BOOST_CHECK_EQUAL( compress_in, decompress_out );
542 std::string compress_in(600,
'*');
543 std::string compress_out;
544 std::string decompress_out;
548 alist[
"server_max_window_bits"] =
"8";
555 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
557 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
558 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
559 BOOST_CHECK_EQUAL( compress_in, decompress_out );
565 std::string compress_in =
"Hello";
566 std::string compress_out1;
567 std::string compress_out2;
568 std::string decompress_out;
572 alist[
"server_no_context_takeover"].clear();
579 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
582 reinterpret_cast<const uint8_t *>(compress_out1.data()),
583 compress_out1.size(),
586 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
587 BOOST_CHECK_EQUAL( compress_in, decompress_out );
589 decompress_out.clear();
592 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
595 reinterpret_cast<const uint8_t *>(compress_out2.data()),
596 compress_out2.size(),
599 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
600 BOOST_CHECK_EQUAL( compress_in, decompress_out );
602 BOOST_CHECK_EQUAL( compress_out1, compress_out2 );
608 std::string compress_in;
609 std::string compress_out;
610 std::string decompress_out;
615 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
617 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
619 compress_out.clear();
620 decompress_out.clear();
623 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
625 v.
ec = v.
exts.
decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
626 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
627 BOOST_CHECK_EQUAL( compress_in, decompress_out );
639 uint8_t in[11] = {0xf2, 0x48, 0xcd, 0xc9, 0xc9, 0x07, 0x00, 0x00, 0x00, 0xff, 0xff};
641 std::string reference =
"Hello";
647 BOOST_CHECK_EQUAL( v.
ec, websocketpp::lib::error_code() );
648 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.
bool is_enabled() const
Returns true if permessage_deflate functionality is active for this connection.
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.
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.
err_str_pair negotiate(http::attribute_list const &offer)
Negotiate extension.
bool is_implemented() const
Test if this object implements the permessage-deflate specification.
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
websocketpp::extensions::permessage_deflate::disabled< config > disabled_type
bool is_enabled() const
Test if the extension was negotiated for this connection.
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