2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
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.
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.
27 //#define BOOST_TEST_DYN_LINK
28 #define BOOST_TEST_MODULE extension_permessage_deflate
29 #include <boost/test/unit_test.hpp>
33 #include <websocketpp/common/memory.hpp>
35 #include <websocketpp/http/request.hpp>
36 #include <websocketpp/extensions/permessage_deflate/enabled.hpp>
39 typedef websocketpp::http::parser::request request_type;
41 typedef websocketpp::extensions::permessage_deflate::enabled<config>
44 using namespace websocketpp;
46 BOOST_AUTO_TEST_CASE( deflate_init ) {
47 /*compressor_type compressor;
48 websocketpp::http::parser::attribute_list attributes;
49 std::pair<lib::error_code,std::string> neg_ret;
51 neg_ret = compressor.negotiate(attributes);
53 BOOST_CHECK_EQUAL( neg_ret.first,
54 extensions::permessage_deflate::error::invalid_parameters );*/
57 * Window size is primarily controlled by the writer. A stream can only be
58 * read by a window size equal to or greater than the one use to compress
59 * it initially. The default windows size is also the maximum window size.
62 * Outbound window size can be limited unilaterally under the assumption
63 * that the opposite end will be using the default (maximum size which can
66 * Inbound window size must be limited by asking the remote endpoint to
67 * do so and it agreeing.
69 * Context takeover is also primarily controlled by the writer. If the
70 * compressor does not clear its context between messages then the reader
73 * Outbound messages may clear context between messages unilaterally.
74 * Inbound messages must retain state unless the remote endpoint signals
77 * Negotiation options:
78 * Client must choose from the following options:
79 * - whether or not to request an inbound window limit
80 * - whether or not to signal that it will honor an outbound window limit
81 * - whether or not to request that the server disallow context takeover
83 * Server must answer in the following ways
84 * - If client requested a window size limit, is the window size limit
86 * - If client allows window limit requests, should we send one?
87 * - If client requested no context takeover, should we accept?
92 * Req: permessage-compress; method=deflate
93 * Ans: permessage-compress; method=deflate
95 * # Client wants to limit the size of inbound windows from server
96 * permessage-compress; method="deflate; s2c_max_window_bits=8, deflate"
97 * Ans: permessage-compress; method="deflate; s2c_max_window_bits=8"
99 * Ans: permessage-compress; method=deflate
101 * # Server wants to limit the size of inbound windows from client
103 * permessage-compress; method="deflate; c2s_max_window_bits, deflate"
106 * permessage-compress; method="deflate; c2s_max_window_bits=8"
120 /* processor::extensions::deflate_method d(true);
121 http::parser::attribute_list attributes;
124 attributes.push_back(http::parser::attribute("foo","bar"));
125 ec = d.init(attributes);
126 BOOST_CHECK(ec == processor::extensions::error::unknown_method_parameter);
129 attributes.push_back(http::parser::attribute("s2c_max_window_bits","bar"));
130 ec = d.init(attributes);
131 BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
134 attributes.push_back(http::parser::attribute("s2c_max_window_bits","7"));
135 ec = d.init(attributes);
136 BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
139 attributes.push_back(http::parser::attribute("s2c_max_window_bits","16"));
140 ec = d.init(attributes);
141 BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
144 attributes.push_back(http::parser::attribute("s2c_max_window_bits","9"));
145 ec = d.init(attributes);
149 ec = d.init(attributes);
152 processor::extensions::deflate_engine de;
154 unsigned char test_in[] = "HelloHelloHelloHello";
155 unsigned char test_out[30];
157 uLongf test_out_size = 30;
161 ret = compress(test_out, &test_out_size, test_in, 20);
163 std::cout << ret << std::endl
164 << websocketpp::utility::to_hex(test_in,20) << std::endl
165 << websocketpp::utility::to_hex(test_out,test_out_size) << std::endl;
167 std::string input = "Hello";
169 ec = de.compress(input,output);
171 BOOST_CHECK( ec == processor::extensions::error::uninitialized );
173 //std::cout << ec.message() << websocketpp::utility::to_hex(output) << std::endl;
175 ec = de.init(15,15,Z_DEFAULT_COMPRESSION,8,Z_FIXED);
179 ec = de.compress(input,output);
180 std::cout << ec.message() << std::endl
181 << websocketpp::utility::to_hex(input) << std::endl
182 << websocketpp::utility::to_hex(output) << std::endl;
186 ec = de.compress(input,output);
187 std::cout << ec.message() << std::endl
188 << websocketpp::utility::to_hex(input) << std::endl
189 << websocketpp::utility::to_hex(output) << std::endl;
193 ec = de.decompress(input,output);
194 std::cout << ec.message() << std::endl
195 << websocketpp::utility::to_hex(input) << std::endl
196 << websocketpp::utility::to_hex(output) << std::endl;