NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
permessage_deflate.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 //#define BOOST_TEST_DYN_LINK
28 #define BOOST_TEST_MODULE permessage_deflate
29 #include <boost/test/unit_test.hpp>
30 
31 #include <websocketpp/error.hpp>
32 
36 
37 #include <string>
38 
40 #include <iostream>
41 
42 class config {};
43 
46 
47 struct ext_vars {
50  websocketpp::lib::error_code ec;
53 };
56 
57 // Ensure the disabled extension behaves appropriately disabled
58 
59 BOOST_AUTO_TEST_CASE( disabled_is_disabled ) {
60  disabled_type exts;
61  BOOST_CHECK( !exts.is_implemented() );
62 }
63 
64 BOOST_AUTO_TEST_CASE( disabled_is_off ) {
65  disabled_type exts;
66  BOOST_CHECK( !exts.is_enabled() );
67 }
68 
69 // Ensure the enabled version actually works
70 
71 BOOST_AUTO_TEST_CASE( enabled_is_enabled ) {
72  ext_vars v;
73  BOOST_CHECK( v.exts.is_implemented() );
74  BOOST_CHECK( v.extc.is_implemented() );
75 }
76 
77 
78 BOOST_AUTO_TEST_CASE( enabled_starts_disabled ) {
79  ext_vars v;
80  BOOST_CHECK( !v.exts.is_enabled() );
81  BOOST_CHECK( !v.extc.is_enabled() );
82 }
83 
84 BOOST_AUTO_TEST_CASE( negotiation_empty_attr ) {
85  ext_vars v;
86 
87  v.esp = v.exts.negotiate(v.attr);
88  BOOST_CHECK( v.exts.is_enabled() );
89  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
90  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
91 }
92 
93 BOOST_AUTO_TEST_CASE( negotiation_invalid_attr ) {
94  ext_vars v;
95  v.attr["foo"] = "bar";
96 
97  v.esp = v.exts.negotiate(v.attr);
98  BOOST_CHECK( !v.exts.is_enabled() );
99  BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attributes) );
100  BOOST_CHECK_EQUAL( v.esp.second, "");
101 }
102 
103 // Negotiate server_no_context_takeover
104 BOOST_AUTO_TEST_CASE( negotiate_server_no_context_takeover_invalid ) {
105  ext_vars v;
106  v.attr["server_no_context_takeover"] = "foo";
107 
108  v.esp = v.exts.negotiate(v.attr);
109  BOOST_CHECK( !v.exts.is_enabled() );
110  BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
111  BOOST_CHECK_EQUAL( v.esp.second, "");
112 }
113 
114 BOOST_AUTO_TEST_CASE( negotiate_server_no_context_takeover ) {
115  ext_vars v;
116  v.attr["server_no_context_takeover"].clear();
117 
118  v.esp = v.exts.negotiate(v.attr);
119  BOOST_CHECK( v.exts.is_enabled() );
120  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
121  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; server_no_context_takeover");
122 }
123 
124 BOOST_AUTO_TEST_CASE( negotiate_server_no_context_takeover_server_initiated ) {
125  ext_vars v;
126 
128  v.esp = v.exts.negotiate(v.attr);
129  BOOST_CHECK( v.exts.is_enabled() );
130  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
131  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; server_no_context_takeover");
132 }
133 
134 // Negotiate client_no_context_takeover
135 BOOST_AUTO_TEST_CASE( negotiate_client_no_context_takeover_invalid ) {
136  ext_vars v;
137  v.attr["client_no_context_takeover"] = "foo";
138 
139  v.esp = v.exts.negotiate(v.attr);
140  BOOST_CHECK( !v.exts.is_enabled() );
141  BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
142  BOOST_CHECK_EQUAL( v.esp.second, "");
143 }
144 
145 BOOST_AUTO_TEST_CASE( negotiate_client_no_context_takeover ) {
146  ext_vars v;
147  v.attr["client_no_context_takeover"].clear();
148 
149  v.esp = v.exts.negotiate(v.attr);
150  BOOST_CHECK( v.exts.is_enabled() );
151  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
152  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; client_no_context_takeover");
153 }
154 
155 BOOST_AUTO_TEST_CASE( negotiate_client_no_context_takeover_server_initiated ) {
156  ext_vars v;
157 
159  v.esp = v.exts.negotiate(v.attr);
160  BOOST_CHECK( v.exts.is_enabled() );
161  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
162  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; client_no_context_takeover");
163 }
164 
165 
166 // Negotiate server_max_window_bits
167 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_invalid ) {
168  ext_vars v;
169 
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");
175 
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;
179 
180  v.esp = v.exts.negotiate(v.attr);
181  BOOST_CHECK( !v.exts.is_enabled() );
182  BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
183  BOOST_CHECK_EQUAL( v.esp.second, "");
184  }
185 }
186 
187 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_valid ) {
188  ext_vars v;
189  v.attr["server_max_window_bits"] = "8";
190 
191  v.esp = v.exts.negotiate(v.attr);
192  BOOST_CHECK( v.exts.is_enabled() );
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");
195 
196  v.attr["server_max_window_bits"] = "15";
197 
198  v.esp = v.exts.negotiate(v.attr);
199  BOOST_CHECK( v.exts.is_enabled() );
200  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
201  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
202 }
203 
204 BOOST_AUTO_TEST_CASE( invalid_set_server_max_window_bits ) {
205  ext_vars v;
206 
209 
212 }
213 
214 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_decline ) {
215  ext_vars v;
216  v.attr["server_max_window_bits"] = "8";
217 
219  v.esp = v.exts.negotiate(v.attr);
220  BOOST_CHECK( v.exts.is_enabled() );
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");
224 }
225 
226 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_accept ) {
227  ext_vars v;
228  v.attr["server_max_window_bits"] = "8";
229 
231  v.esp = v.exts.negotiate(v.attr);
232  BOOST_CHECK( v.exts.is_enabled() );
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");
236 }
237 
238 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_largest ) {
239  ext_vars v;
240  v.attr["server_max_window_bits"] = "8";
241 
243  v.esp = v.exts.negotiate(v.attr);
244  BOOST_CHECK( v.exts.is_enabled() );
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");
248 }
249 
250 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_smallest ) {
251  ext_vars v;
252  v.attr["server_max_window_bits"] = "8";
253 
255  v.esp = v.exts.negotiate(v.attr);
256  BOOST_CHECK( v.exts.is_enabled() );
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");
260 }
261 
262 // Negotiate server_max_window_bits
263 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_invalid ) {
264  ext_vars v;
265 
266  std::vector<std::string> values;
267  values.push_back("foo");
268  values.push_back("7");
269  values.push_back("16");
270 
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;
274 
275  v.esp = v.exts.negotiate(v.attr);
276  BOOST_CHECK( !v.exts.is_enabled() );
277  BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
278  BOOST_CHECK_EQUAL( v.esp.second, "");
279  }
280 }
281 
282 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_valid ) {
283  ext_vars v;
284 
285  v.attr["client_max_window_bits"].clear();
286  v.esp = v.exts.negotiate(v.attr);
287  BOOST_CHECK( v.exts.is_enabled() );
288  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
289  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
290 
291  v.attr["client_max_window_bits"] = "8";
292  v.esp = v.exts.negotiate(v.attr);
293  BOOST_CHECK( v.exts.is_enabled() );
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");
296 
297  v.attr["client_max_window_bits"] = "15";
298  v.esp = v.exts.negotiate(v.attr);
299  BOOST_CHECK( v.exts.is_enabled() );
300  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
301  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
302 }
303 
304 BOOST_AUTO_TEST_CASE( invalid_set_client_max_window_bits ) {
305  ext_vars v;
306 
309 
312 }
313 
314 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_decline ) {
315  ext_vars v;
316  v.attr["client_max_window_bits"] = "8";
317 
319  v.esp = v.exts.negotiate(v.attr);
320  BOOST_CHECK( v.exts.is_enabled() );
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");
324 }
325 
326 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_accept ) {
327  ext_vars v;
328  v.attr["client_max_window_bits"] = "8";
329 
331  v.esp = v.exts.negotiate(v.attr);
332  BOOST_CHECK( v.exts.is_enabled() );
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");
336 }
337 
338 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_largest ) {
339  ext_vars v;
340  v.attr["client_max_window_bits"] = "8";
341 
343  v.esp = v.exts.negotiate(v.attr);
344  BOOST_CHECK( v.exts.is_enabled() );
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");
348 }
349 
350 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_smallest ) {
351  ext_vars v;
352  v.attr["client_max_window_bits"] = "8";
353 
355  v.esp = v.exts.negotiate(v.attr);
356  BOOST_CHECK( v.exts.is_enabled() );
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");
360 }
361 
362 
363 // Combinations with 2
364 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated1 ) {
365  ext_vars v;
366 
367  v.attr["server_no_context_takeover"].clear();
368  v.attr["client_no_context_takeover"].clear();
369 
370  v.esp = v.exts.negotiate(v.attr);
371  BOOST_CHECK( v.exts.is_enabled() );
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");
374 }
375 
376 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated2 ) {
377  ext_vars v;
378 
379  v.attr["server_no_context_takeover"].clear();
380  v.attr["server_max_window_bits"] = "10";
381 
382  v.esp = v.exts.negotiate(v.attr);
383  BOOST_CHECK( v.exts.is_enabled() );
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");
386 }
387 
388 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated3 ) {
389  ext_vars v;
390 
391  v.attr["server_no_context_takeover"].clear();
392  v.attr["client_max_window_bits"] = "10";
393 
394  v.esp = v.exts.negotiate(v.attr);
395  BOOST_CHECK( v.exts.is_enabled() );
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");
398 }
399 
400 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated4 ) {
401  ext_vars v;
402 
403  v.attr["client_no_context_takeover"].clear();
404  v.attr["server_max_window_bits"] = "10";
405 
406  v.esp = v.exts.negotiate(v.attr);
407  BOOST_CHECK( v.exts.is_enabled() );
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");
410 }
411 
412 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated5 ) {
413  ext_vars v;
414 
415  v.attr["client_no_context_takeover"].clear();
416  v.attr["client_max_window_bits"] = "10";
417 
418  v.esp = v.exts.negotiate(v.attr);
419  BOOST_CHECK( v.exts.is_enabled() );
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");
422 }
423 
424 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated6 ) {
425  ext_vars v;
426 
427  v.attr["server_max_window_bits"] = "10";
428  v.attr["client_max_window_bits"] = "10";
429 
430  v.esp = v.exts.negotiate(v.attr);
431  BOOST_CHECK( v.exts.is_enabled() );
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");
434 }
435 
436 BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated1 ) {
437  ext_vars v;
438 
439  v.attr["server_no_context_takeover"].clear();
440  v.attr["client_no_context_takeover"].clear();
441  v.attr["server_max_window_bits"] = "10";
442 
443  v.esp = v.exts.negotiate(v.attr);
444  BOOST_CHECK( v.exts.is_enabled() );
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");
447 }
448 
449 BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated2 ) {
450  ext_vars v;
451 
452  v.attr["server_no_context_takeover"].clear();
453  v.attr["client_no_context_takeover"].clear();
454  v.attr["client_max_window_bits"] = "10";
455 
456  v.esp = v.exts.negotiate(v.attr);
457  BOOST_CHECK( v.exts.is_enabled() );
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");
460 }
461 
462 BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated3 ) {
463  ext_vars v;
464 
465  v.attr["server_no_context_takeover"].clear();
466  v.attr["server_max_window_bits"] = "10";
467  v.attr["client_max_window_bits"] = "10";
468 
469  v.esp = v.exts.negotiate(v.attr);
470  BOOST_CHECK( v.exts.is_enabled() );
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");
473 }
474 
475 BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated4 ) {
476  ext_vars v;
477 
478  v.attr["client_no_context_takeover"].clear();
479  v.attr["server_max_window_bits"] = "10";
480  v.attr["client_max_window_bits"] = "10";
481 
482  v.esp = v.exts.negotiate(v.attr);
483  BOOST_CHECK( v.exts.is_enabled() );
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");
486 }
487 
488 BOOST_AUTO_TEST_CASE( negotiate_four_client_initiated ) {
489  ext_vars v;
490 
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";
495 
496  v.esp = v.exts.negotiate(v.attr);
497  BOOST_CHECK( v.exts.is_enabled() );
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");
500 }
501 
502 // Compression
503 BOOST_AUTO_TEST_CASE( compress_data ) {
504  ext_vars v;
505 
506  std::string compress_in = "Hello";
507  std::string compress_out;
508  std::string decompress_out;
509 
510  v.exts.init(true);
511 
512  v.ec = v.exts.compress(compress_in,compress_out);
513  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
514 
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 );
518 }
519 
520 BOOST_AUTO_TEST_CASE( compress_data_multiple ) {
521  ext_vars v;
522 
523  v.exts.init(true);
524 
525  for (int i = 0; i < 2; i++) {
526  std::string compress_in = "Hello";
527  std::string compress_out;
528  std::string decompress_out;
529 
530  v.ec = v.exts.compress(compress_in,compress_out);
531  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
532 
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 );
536  }
537 }
538 
539 BOOST_AUTO_TEST_CASE( compress_data_large ) {
540  ext_vars v;
541 
542  std::string compress_in(600,'*');
543  std::string compress_out;
544  std::string decompress_out;
545 
547 
548  alist["server_max_window_bits"] = "8";
550 
551  v.exts.negotiate(alist);
552  v.exts.init(true);
553 
554  v.ec = v.exts.compress(compress_in,compress_out);
555  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
556 
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 );
560 }
561 
562 BOOST_AUTO_TEST_CASE( compress_data_no_context_takeover ) {
563  ext_vars v;
564 
565  std::string compress_in = "Hello";
566  std::string compress_out1;
567  std::string compress_out2;
568  std::string decompress_out;
569 
571 
572  alist["server_no_context_takeover"].clear();
574 
575  v.exts.negotiate(alist);
576  v.exts.init(true);
577 
578  v.ec = v.exts.compress(compress_in,compress_out1);
579  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
580 
581  v.ec = v.exts.decompress(
582  reinterpret_cast<const uint8_t *>(compress_out1.data()),
583  compress_out1.size(),
584  decompress_out
585  );
586  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
587  BOOST_CHECK_EQUAL( compress_in, decompress_out );
588 
589  decompress_out.clear();
590 
591  v.ec = v.exts.compress(compress_in,compress_out2);
592  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
593 
594  v.ec = v.exts.decompress(
595  reinterpret_cast<const uint8_t *>(compress_out2.data()),
596  compress_out2.size(),
597  decompress_out
598  );
599  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
600  BOOST_CHECK_EQUAL( compress_in, decompress_out );
601 
602  BOOST_CHECK_EQUAL( compress_out1, compress_out2 );
603 }
604 
605 BOOST_AUTO_TEST_CASE( compress_empty ) {
606  ext_vars v;
607 
608  std::string compress_in;
609  std::string compress_out;
610  std::string decompress_out;
611 
612  v.exts.init(true);
613 
614  v.ec = v.exts.compress(compress_in,compress_out);
615  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
616 
617  v.ec = v.exts.decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
618 
619  compress_out.clear();
620  decompress_out.clear();
621 
622  v.ec = v.exts.compress(compress_in,compress_out);
623  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
624 
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 );
628 }
629 
631 
635 // Decompression
636 BOOST_AUTO_TEST_CASE( decompress_data ) {
637  ext_vars v;
638 
639  uint8_t in[11] = {0xf2, 0x48, 0xcd, 0xc9, 0xc9, 0x07, 0x00, 0x00, 0x00, 0xff, 0xff};
640  std::string out;
641  std::string reference = "Hello";
642 
643  v.exts.init(true);
644 
645  v.ec = v.exts.decompress(in,11,out);
646 
647  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
648  BOOST_CHECK_EQUAL( out, reference );
649 }
lib::error_code set_server_max_window_bits(uint8_t bits, mode::value mode)
Limit server LZ77 sliding window size.
Definition: enabled.hpp:393
void enable_client_no_context_takeover()
Reset client&#39;s outgoing LZ77 sliding window for each new message.
Definition: enabled.hpp:367
lib::error_code make_error_code(error::value e)
Definition: error.hpp:235
BOOST_AUTO_TEST_CASE(disabled_is_disabled)
Decline any value the remote endpoint offers. Insist on defaults.
Definition: enabled.hpp:196
Accept any value the remote endpoint offers.
Definition: enabled.hpp:194
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Definition: error.hpp:41
bool is_enabled() const
Returns true if permessage_deflate functionality is active for this connection.
Definition: disabled.hpp:87
enabled_type exts
lib::error_code init(bool is_server)
Initialize zlib state.
Definition: enabled.hpp:260
Use the smallest value common to both offers.
Definition: enabled.hpp:200
lib::error_code set_client_max_window_bits(uint8_t bits, mode::value mode)
Limit client LZ77 sliding window size.
Definition: enabled.hpp:424
Stub class for use when disabling permessage_deflate extension.
Definition: disabled.hpp:52
enabled_type extc
lib::error_code decompress(uint8_t const *buf, size_t len, std::string &out)
Decompress bytes.
Definition: enabled.hpp:544
Use the largest value common to both offers.
Definition: enabled.hpp:198
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.
Definition: disabled.hpp:81
err_str_pair negotiate(http::attribute_list const &offer)
Negotiate extension.
Definition: enabled.hpp:467
bool is_implemented() const
Test if this object implements the permessage-deflate specification.
Definition: enabled.hpp:312
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
Definition: constants.hpp:45
websocketpp::extensions::permessage_deflate::disabled< config > disabled_type
bool is_enabled() const
Test if the extension was negotiated for this connection.
Definition: enabled.hpp:323
void enable_server_no_context_takeover()
Reset server&#39;s outgoing LZ77 sliding window for each new message.
Definition: enabled.hpp:348
Permessage deflate error values.
Definition: enabled.hpp:90
websocketpp::lib::error_code ec
lib::error_code compress(std::string const &in, std::string &out)
Compress bytes.
Definition: enabled.hpp:506
websocketpp::http::attribute_list attr