NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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 
190  // confirm that a request for a value of 8 is interpreted as 9
191  v.attr["server_max_window_bits"] = "8";
192  v.esp = v.exts.negotiate(v.attr);
193  BOOST_CHECK( v.exts.is_enabled() );
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");
196 
197  v.attr["server_max_window_bits"] = "9";
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; server_max_window_bits=9");
202 
203 
204  v.attr["server_max_window_bits"] = "15";
205  v.esp = v.exts.negotiate(v.attr);
206  BOOST_CHECK( v.exts.is_enabled() );
207  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
208  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
209 }
210 
211 BOOST_AUTO_TEST_CASE( invalid_set_server_max_window_bits ) {
212  ext_vars v;
213 
216 
219 }
220 
221 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_decline ) {
222  ext_vars v;
223  v.attr["server_max_window_bits"] = "9";
224 
226  v.esp = v.exts.negotiate(v.attr);
227  BOOST_CHECK( v.exts.is_enabled() );
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");
231 }
232 
233 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_accept_8 ) {
234  ext_vars v;
235  v.attr["server_max_window_bits"] = "8";
236 
238  v.esp = v.exts.negotiate(v.attr);
239  BOOST_CHECK( v.exts.is_enabled() );
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");
243 }
244 
245 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_accept ) {
246  ext_vars v;
247  v.attr["server_max_window_bits"] = "9";
248 
250  v.esp = v.exts.negotiate(v.attr);
251  BOOST_CHECK( v.exts.is_enabled() );
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");
255 }
256 
257 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_largest_8 ) {
258  ext_vars v;
259  v.attr["server_max_window_bits"] = "8";
260 
262  v.esp = v.exts.negotiate(v.attr);
263  BOOST_CHECK( v.exts.is_enabled() );
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");
267 }
268 
269 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_largest ) {
270  ext_vars v;
271  v.attr["server_max_window_bits"] = "9";
272 
274  v.esp = v.exts.negotiate(v.attr);
275  BOOST_CHECK( v.exts.is_enabled() );
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");
279 }
280 
281 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_smallest_8 ) {
282  ext_vars v;
283  v.attr["server_max_window_bits"] = "8";
284 
286  v.esp = v.exts.negotiate(v.attr);
287  BOOST_CHECK( v.exts.is_enabled() );
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");
291 }
292 
293 BOOST_AUTO_TEST_CASE( negotiate_server_max_window_bits_smallest ) {
294  ext_vars v;
295  v.attr["server_max_window_bits"] = "9";
296 
298  v.esp = v.exts.negotiate(v.attr);
299  BOOST_CHECK( v.exts.is_enabled() );
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");
303 }
304 
305 // Negotiate server_max_window_bits
306 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_invalid ) {
307  ext_vars v;
308 
309  std::vector<std::string> values;
310  values.push_back("foo");
311  values.push_back("7");
312  values.push_back("16");
313 
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;
317 
318  v.esp = v.exts.negotiate(v.attr);
319  BOOST_CHECK( !v.exts.is_enabled() );
320  BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
321  BOOST_CHECK_EQUAL( v.esp.second, "");
322  }
323 }
324 
325 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_valid ) {
326  ext_vars v;
327 
328  v.attr["client_max_window_bits"].clear();
329  v.esp = v.exts.negotiate(v.attr);
330  BOOST_CHECK( v.exts.is_enabled() );
331  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
332  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
333 
334  v.attr["client_max_window_bits"] = "8";
335  v.esp = v.exts.negotiate(v.attr);
336  BOOST_CHECK( v.exts.is_enabled() );
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");
339 
340  v.attr["client_max_window_bits"] = "9";
341  v.esp = v.exts.negotiate(v.attr);
342  BOOST_CHECK( v.exts.is_enabled() );
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");
345 
346  v.attr["client_max_window_bits"] = "15";
347  v.esp = v.exts.negotiate(v.attr);
348  BOOST_CHECK( v.exts.is_enabled() );
349  BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
350  BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
351 }
352 
353 BOOST_AUTO_TEST_CASE( invalid_set_client_max_window_bits ) {
354  ext_vars v;
355 
358 
361 }
362 
363 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_decline_8 ) {
364  ext_vars v;
365  v.attr["client_max_window_bits"] = "8";
366 
368  v.esp = v.exts.negotiate(v.attr);
369  BOOST_CHECK( v.exts.is_enabled() );
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");
373 }
374 
375 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_decline ) {
376  ext_vars v;
377  v.attr["client_max_window_bits"] = "9";
378 
380  v.esp = v.exts.negotiate(v.attr);
381  BOOST_CHECK( v.exts.is_enabled() );
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");
385 }
386 
387 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_accept_8 ) {
388  ext_vars v;
389  v.attr["client_max_window_bits"] = "8";
390 
392  v.esp = v.exts.negotiate(v.attr);
393  BOOST_CHECK( v.exts.is_enabled() );
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");
397 }
398 
399 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_accept ) {
400  ext_vars v;
401  v.attr["client_max_window_bits"] = "9";
402 
404  v.esp = v.exts.negotiate(v.attr);
405  BOOST_CHECK( v.exts.is_enabled() );
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");
409 }
410 
411 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_largest_8 ) {
412  ext_vars v;
413  v.attr["client_max_window_bits"] = "8";
414 
416  v.esp = v.exts.negotiate(v.attr);
417  BOOST_CHECK( v.exts.is_enabled() );
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");
421 }
422 
423 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_largest ) {
424  ext_vars v;
425  v.attr["client_max_window_bits"] = "9";
426 
428  v.esp = v.exts.negotiate(v.attr);
429  BOOST_CHECK( v.exts.is_enabled() );
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");
433 }
434 
435 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_smallest_8 ) {
436  ext_vars v;
437  v.attr["client_max_window_bits"] = "8";
438 
440  v.esp = v.exts.negotiate(v.attr);
441  BOOST_CHECK( v.exts.is_enabled() );
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");
445 }
446 
447 BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_smallest ) {
448  ext_vars v;
449  v.attr["client_max_window_bits"] = "9";
450 
452  v.esp = v.exts.negotiate(v.attr);
453  BOOST_CHECK( v.exts.is_enabled() );
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");
457 }
458 
459 
460 // Combinations with 2
461 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated1 ) {
462  ext_vars v;
463 
464  v.attr["server_no_context_takeover"].clear();
465  v.attr["client_no_context_takeover"].clear();
466 
467  v.esp = v.exts.negotiate(v.attr);
468  BOOST_CHECK( v.exts.is_enabled() );
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");
471 }
472 
473 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated2 ) {
474  ext_vars v;
475 
476  v.attr["server_no_context_takeover"].clear();
477  v.attr["server_max_window_bits"] = "10";
478 
479  v.esp = v.exts.negotiate(v.attr);
480  BOOST_CHECK( v.exts.is_enabled() );
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");
483 }
484 
485 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated3 ) {
486  ext_vars v;
487 
488  v.attr["server_no_context_takeover"].clear();
489  v.attr["client_max_window_bits"] = "10";
490 
491  v.esp = v.exts.negotiate(v.attr);
492  BOOST_CHECK( v.exts.is_enabled() );
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");
495 }
496 
497 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated4 ) {
498  ext_vars v;
499 
500  v.attr["client_no_context_takeover"].clear();
501  v.attr["server_max_window_bits"] = "10";
502 
503  v.esp = v.exts.negotiate(v.attr);
504  BOOST_CHECK( v.exts.is_enabled() );
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");
507 }
508 
509 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated5 ) {
510  ext_vars v;
511 
512  v.attr["client_no_context_takeover"].clear();
513  v.attr["client_max_window_bits"] = "10";
514 
515  v.esp = v.exts.negotiate(v.attr);
516  BOOST_CHECK( v.exts.is_enabled() );
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");
519 }
520 
521 BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated6 ) {
522  ext_vars v;
523 
524  v.attr["server_max_window_bits"] = "10";
525  v.attr["client_max_window_bits"] = "10";
526 
527  v.esp = v.exts.negotiate(v.attr);
528  BOOST_CHECK( v.exts.is_enabled() );
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");
531 }
532 
533 BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated1 ) {
534  ext_vars v;
535 
536  v.attr["server_no_context_takeover"].clear();
537  v.attr["client_no_context_takeover"].clear();
538  v.attr["server_max_window_bits"] = "10";
539 
540  v.esp = v.exts.negotiate(v.attr);
541  BOOST_CHECK( v.exts.is_enabled() );
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");
544 }
545 
546 BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated2 ) {
547  ext_vars v;
548 
549  v.attr["server_no_context_takeover"].clear();
550  v.attr["client_no_context_takeover"].clear();
551  v.attr["client_max_window_bits"] = "10";
552 
553  v.esp = v.exts.negotiate(v.attr);
554  BOOST_CHECK( v.exts.is_enabled() );
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");
557 }
558 
559 BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated3 ) {
560  ext_vars v;
561 
562  v.attr["server_no_context_takeover"].clear();
563  v.attr["server_max_window_bits"] = "10";
564  v.attr["client_max_window_bits"] = "10";
565 
566  v.esp = v.exts.negotiate(v.attr);
567  BOOST_CHECK( v.exts.is_enabled() );
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");
570 }
571 
572 BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated4 ) {
573  ext_vars v;
574 
575  v.attr["client_no_context_takeover"].clear();
576  v.attr["server_max_window_bits"] = "10";
577  v.attr["client_max_window_bits"] = "10";
578 
579  v.esp = v.exts.negotiate(v.attr);
580  BOOST_CHECK( v.exts.is_enabled() );
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");
583 }
584 
585 BOOST_AUTO_TEST_CASE( negotiate_four_client_initiated ) {
586  ext_vars v;
587 
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";
592 
593  v.esp = v.exts.negotiate(v.attr);
594  BOOST_CHECK( v.exts.is_enabled() );
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");
597 }
598 
599 // Compression
600 BOOST_AUTO_TEST_CASE( compress_data ) {
601  ext_vars v;
602 
603  std::string compress_in = "Hello";
604  std::string compress_out;
605  std::string decompress_out;
606 
607  v.ec = v.exts.init(true);
608  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
609 
610  v.ec = v.exts.compress(compress_in,compress_out);
611  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
612 
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 );
616 }
617 
618 BOOST_AUTO_TEST_CASE( compress_data_multiple ) {
619  ext_vars v;
620 
621  v.ec = v.exts.init(true);
622  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
623 
624  for (int i = 0; i < 2; i++) {
625  std::string compress_in = "Hello";
626  std::string compress_out;
627  std::string decompress_out;
628 
629  v.ec = v.exts.compress(compress_in,compress_out);
630  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
631 
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 );
635  }
636 }
637 
638 BOOST_AUTO_TEST_CASE( compress_data_large ) {
639  ext_vars v;
640 
641  std::string compress_in(600,'*');
642  std::string compress_out;
643  std::string decompress_out;
644 
646 
647  alist["server_max_window_bits"] = "9";
649 
650  v.exts.negotiate(alist);
651  v.ec = v.exts.init(true);
652  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
653 
654  v.ec = v.exts.compress(compress_in,compress_out);
655  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
656 
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 );
660 }
661 
662 BOOST_AUTO_TEST_CASE( compress_data_no_context_takeover ) {
663  ext_vars v;
664 
665  std::string compress_in = "Hello";
666  std::string compress_out1;
667  std::string compress_out2;
668  std::string decompress_out;
669 
671 
672  alist["server_no_context_takeover"].clear();
674 
675  v.exts.negotiate(alist);
676  v.ec = v.exts.init(true);
677  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
678 
679  v.ec = v.exts.compress(compress_in,compress_out1);
680  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
681 
682  v.ec = v.exts.decompress(
683  reinterpret_cast<const uint8_t *>(compress_out1.data()),
684  compress_out1.size(),
685  decompress_out
686  );
687  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
688  BOOST_CHECK_EQUAL( compress_in, decompress_out );
689 
690  decompress_out.clear();
691 
692  v.ec = v.exts.compress(compress_in,compress_out2);
693  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
694 
695  v.ec = v.exts.decompress(
696  reinterpret_cast<const uint8_t *>(compress_out2.data()),
697  compress_out2.size(),
698  decompress_out
699  );
700  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
701  BOOST_CHECK_EQUAL( compress_in, decompress_out );
702 
703  BOOST_CHECK_EQUAL( compress_out1, compress_out2 );
704 }
705 
706 BOOST_AUTO_TEST_CASE( compress_empty ) {
707  ext_vars v;
708 
709  std::string compress_in;
710  std::string compress_out;
711  std::string decompress_out;
712 
713  v.ec = v.exts.init(true);
714  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
715 
716  v.ec = v.exts.compress(compress_in,compress_out);
717  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
718 
719  v.ec = v.exts.decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
720 
721  compress_out.clear();
722  decompress_out.clear();
723 
724  v.ec = v.exts.compress(compress_in,compress_out);
725  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
726 
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 );
730 }
731 
733 
737 // Decompression
738 BOOST_AUTO_TEST_CASE( decompress_data ) {
739  ext_vars v;
740 
741  uint8_t in[11] = {0xf2, 0x48, 0xcd, 0xc9, 0xc9, 0x07, 0x00, 0x00, 0x00, 0xff, 0xff};
742  std::string out;
743  std::string reference = "Hello";
744 
745  v.ec = v.exts.init(true);
746  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
747 
748  v.ec = v.exts.decompress(in,11,out);
749 
750  BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
751  BOOST_CHECK_EQUAL( out, reference );
752 }
lib::error_code set_server_max_window_bits(uint8_t bits, mode::value mode)
Limit server LZ77 sliding window size.
Definition: enabled.hpp:415
void enable_client_no_context_takeover()
Reset client&#39;s outgoing LZ77 sliding window for each new message.
Definition: enabled.hpp:381
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:209
Accept any value the remote endpoint offers.
Definition: enabled.hpp:207
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Definition: error.hpp:41
enabled_type exts
lib::error_code init(bool is_server)
Initialize zlib state.
Definition: enabled.hpp:273
Use the smallest value common to both offers.
Definition: enabled.hpp:213
lib::error_code set_client_max_window_bits(uint8_t bits, mode::value mode)
Limit client LZ77 sliding window size.
Definition: enabled.hpp:460
Stub class for use when disabling permessage_deflate extension.
Definition: disabled.hpp:53
enabled_type extc
lib::error_code decompress(uint8_t const *buf, size_t len, std::string &out)
Decompress bytes.
Definition: enabled.hpp:586
Use the largest value common to both offers.
Definition: enabled.hpp:211
bool is_implemented() const
Test if this object implements the permessage-deflate specification.
Definition: enabled.hpp:326
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:82
bool is_enabled() const
Test if the extension was negotiated for this connection.
Definition: enabled.hpp:337
err_str_pair negotiate(http::attribute_list const &offer)
Negotiate extension.
Definition: enabled.hpp:509
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
Definition: constants.hpp:45
bool is_enabled() const
Returns true if permessage_deflate functionality is active for this connection.
Definition: disabled.hpp:88
websocketpp::extensions::permessage_deflate::disabled< config > disabled_type
void enable_server_no_context_takeover()
Reset server&#39;s outgoing LZ77 sliding window for each new message.
Definition: enabled.hpp:362
Permessage deflate error values.
Definition: enabled.hpp:91
websocketpp::lib::error_code ec
lib::error_code compress(std::string const &in, std::string &out)
Compress bytes.
Definition: enabled.hpp:548
websocketpp::http::attribute_list attr