fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / test / processors / extension_permessage_compress.cpp
1 /*
2  * Copyright (c) 2014, 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 extension_permessage_deflate
29 #include <boost/test/unit_test.hpp>
30
31 #include <iostream>
32 #include <string>
33 #include <websocketpp/common/memory.hpp>
34
35 #include <websocketpp/http/request.hpp>
36 #include <websocketpp/extensions/permessage_deflate/enabled.hpp>
37
38 struct config {
39     typedef websocketpp::http::parser::request request_type;
40 };
41 typedef websocketpp::extensions::permessage_deflate::enabled<config>
42     compressor_type;
43
44 using namespace websocketpp;
45
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;
50
51     neg_ret = compressor.negotiate(attributes);
52
53     BOOST_CHECK_EQUAL( neg_ret.first,
54         extensions::permessage_deflate::error::invalid_parameters );*/
55
56     /**
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.
60      * Thus:
61      *
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
64      * read anything)
65      *
66      * Inbound window size must be limited by asking the remote endpoint to
67      * do so and it agreeing.
68      *
69      * Context takeover is also primarily controlled by the writer. If the
70      * compressor does not clear its context between messages then the reader
71      * can't either.
72      *
73      * Outbound messages may clear context between messages unilaterally.
74      * Inbound messages must retain state unless the remote endpoint signals
75      * otherwise.
76      *
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
82      *
83      * Server must answer in the following ways
84      * - If client requested a window size limit, is the window size limit
85      *   acceptable?
86      * - If client allows window limit requests, should we send one?
87      * - If client requested no context takeover, should we accept?
88      *
89      *
90      *
91      * All Defaults
92      * Req: permessage-compress; method=deflate
93      * Ans: permessage-compress; method=deflate
94      *
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"
98      * OR
99      * Ans: permessage-compress; method=deflate
100      *
101      * # Server wants to limit the size of inbound windows from client
102      * Client:
103      * permessage-compress; method="deflate; c2s_max_window_bits, deflate"
104      *
105      * Server:
106      * permessage-compress; method="deflate; c2s_max_window_bits=8"
107      *
108      * # Client wants to
109      *
110      *
111      *
112      *
113      *
114      *
115      */
116
117
118
119
120    /* processor::extensions::deflate_method d(true);
121     http::parser::attribute_list attributes;
122     lib::error_code ec;
123
124     attributes.push_back(http::parser::attribute("foo","bar"));
125     ec = d.init(attributes);
126     BOOST_CHECK(ec == processor::extensions::error::unknown_method_parameter);
127
128     attributes.clear();
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);
132
133     attributes.clear();
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);
137
138     attributes.clear();
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);
142
143     attributes.clear();
144     attributes.push_back(http::parser::attribute("s2c_max_window_bits","9"));
145     ec = d.init(attributes);
146     BOOST_CHECK( !ec);
147
148     attributes.clear();
149     ec = d.init(attributes);
150     BOOST_CHECK( !ec);
151
152     processor::extensions::deflate_engine de;
153
154     unsigned char test_in[] = "HelloHelloHelloHello";
155     unsigned char test_out[30];
156
157     uLongf test_out_size = 30;
158
159     int ret;
160
161     ret = compress(test_out, &test_out_size, test_in, 20);
162
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;
166
167     std::string input = "Hello";
168     std::string output;
169     ec = de.compress(input,output);
170
171     BOOST_CHECK( ec == processor::extensions::error::uninitialized );
172
173     //std::cout << ec.message() << websocketpp::utility::to_hex(output) << std::endl;
174
175     ec = de.init(15,15,Z_DEFAULT_COMPRESSION,8,Z_FIXED);
176     //ec = de.init();
177     BOOST_CHECK( !ec );
178
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;
183
184     output.clear();
185
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;
190
191     input = output;
192     output.clear();
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;
197     */
198 }