2 * Copyright (c) 2011, 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 frame
29 #include <boost/test/unit_test.hpp>
34 #include <websocketpp/frame.hpp>
35 #include <websocketpp/utilities.hpp>
37 using namespace websocketpp;
39 BOOST_AUTO_TEST_CASE( basic_bits ) {
40 frame::basic_header h1(0x00,0x00); // all false
41 frame::basic_header h2(0xF0,0x80); // all true
44 BOOST_CHECK( frame::get_fin(h1) == false );
45 BOOST_CHECK( frame::get_rsv1(h1) == false );
46 BOOST_CHECK( frame::get_rsv2(h1) == false );
47 BOOST_CHECK( frame::get_rsv3(h1) == false );
48 BOOST_CHECK( frame::get_masked(h1) == false );
50 BOOST_CHECK( frame::get_fin(h2) == true );
51 BOOST_CHECK( frame::get_rsv1(h2) == true );
52 BOOST_CHECK( frame::get_rsv2(h2) == true );
53 BOOST_CHECK( frame::get_rsv3(h2) == true );
54 BOOST_CHECK( frame::get_masked(h2) == true );
57 frame::set_fin(h1,true);
58 BOOST_CHECK( h1.b0 == 0x80 );
60 frame::set_rsv1(h1,true);
61 BOOST_CHECK( h1.b0 == 0xC0 );
63 frame::set_rsv2(h1,true);
64 BOOST_CHECK( h1.b0 == 0xE0 );
66 frame::set_rsv3(h1,true);
67 BOOST_CHECK( h1.b0 == 0xF0 );
69 frame::set_masked(h1,true);
70 BOOST_CHECK( h1.b1 == 0x80 );
73 BOOST_AUTO_TEST_CASE( basic_constructors ) {
75 frame::basic_header h1(frame::opcode::TEXT,12,true,false);
76 BOOST_CHECK( frame::get_opcode(h1) == frame::opcode::TEXT );
77 BOOST_CHECK( frame::get_basic_size(h1) == 12 );
78 BOOST_CHECK( frame::get_fin(h1) == true );
79 BOOST_CHECK( frame::get_rsv1(h1) == false );
80 BOOST_CHECK( frame::get_rsv2(h1) == false );
81 BOOST_CHECK( frame::get_rsv3(h1) == false );
82 BOOST_CHECK( frame::get_masked(h1) == false );
84 frame::basic_header h2(frame::opcode::BINARY,0,false,false,false,true);
85 BOOST_CHECK( frame::get_opcode(h2) == frame::opcode::BINARY );
86 BOOST_CHECK( frame::get_basic_size(h2) == 0 );
87 BOOST_CHECK( frame::get_fin(h2) == false );
88 BOOST_CHECK( frame::get_rsv1(h2) == false );
89 BOOST_CHECK( frame::get_rsv2(h2) == true );
90 BOOST_CHECK( frame::get_rsv3(h2) == false );
91 BOOST_CHECK( frame::get_masked(h2) == false );
94 BOOST_AUTO_TEST_CASE( basic_size ) {
95 frame::basic_header h1(0x00,0x00); // length 0
96 frame::basic_header h2(0x00,0x01); // length 1
97 frame::basic_header h3(0x00,0x7D); // length 125
98 frame::basic_header h4(0x00,0x7E); // length 126
99 frame::basic_header h5(0x00,0x7F); // length 127
100 frame::basic_header h6(0x00,0x80); // length 0, mask bit set
102 BOOST_CHECK( frame::get_basic_size(h1) == 0 );
103 BOOST_CHECK( frame::get_basic_size(h2) == 1 );
104 BOOST_CHECK( frame::get_basic_size(h3) == 125 );
105 BOOST_CHECK( frame::get_basic_size(h4) == 126 );
106 BOOST_CHECK( frame::get_basic_size(h5) == 127 );
107 BOOST_CHECK( frame::get_basic_size(h6) == 0 );
109 /*frame::set_basic_size(h1,1);
110 BOOST_CHECK( h1.b1 == 0x01 );
112 frame::set_basic_size(h1,125);
113 BOOST_CHECK( h1.b1 == 0x7D );
115 frame::set_basic_size(h1,126);
116 BOOST_CHECK( h1.b1 == 0x7E );
118 frame::set_basic_size(h1,127);
119 BOOST_CHECK( h1.b1 == 0x7F );
121 frame::set_basic_size(h1,0);
122 BOOST_CHECK( h1.b1 == 0x00 );*/
125 BOOST_AUTO_TEST_CASE( basic_header_length ) {
126 frame::basic_header h1(0x82,0x00); // short binary frame, unmasked
127 frame::basic_header h2(0x82,0x80); // short binary frame, masked
128 frame::basic_header h3(0x82,0x7E); // medium binary frame, unmasked
129 frame::basic_header h4(0x82,0xFE); // medium binary frame, masked
130 frame::basic_header h5(0x82,0x7F); // jumbo binary frame, unmasked
131 frame::basic_header h6(0x82,0xFF); // jumbo binary frame, masked
133 BOOST_CHECK( frame::get_header_len(h1) == 2);
134 BOOST_CHECK( frame::get_header_len(h2) == 6);
135 BOOST_CHECK( frame::get_header_len(h3) == 4);
136 BOOST_CHECK( frame::get_header_len(h4) == 8);
137 BOOST_CHECK( frame::get_header_len(h5) == 10);
138 BOOST_CHECK( frame::get_header_len(h6) == 14);
141 BOOST_AUTO_TEST_CASE( basic_opcode ) {
142 frame::basic_header h1(0x00,0x00);
144 BOOST_CHECK( is_control(frame::opcode::CONTINUATION) == false);
145 BOOST_CHECK( is_control(frame::opcode::TEXT) == false);
146 BOOST_CHECK( is_control(frame::opcode::BINARY) == false);
147 BOOST_CHECK( is_control(frame::opcode::CLOSE) == true);
148 BOOST_CHECK( is_control(frame::opcode::PING) == true);
149 BOOST_CHECK( is_control(frame::opcode::PONG) == true);
151 BOOST_CHECK( frame::get_opcode(h1) == frame::opcode::CONTINUATION );
154 BOOST_AUTO_TEST_CASE( extended_header_basics ) {
155 frame::extended_header h1;
156 uint8_t h1_solution[12] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
159 frame::extended_header h2(uint16_t(255));
160 uint8_t h2_solution[12] = {0x00, 0xFF, 0x00, 0x00, 0x00, 0x00,
161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
163 frame::extended_header h3(uint16_t(256),htonl(0x8040201));
164 uint8_t h3_solution[12] = {0x01, 0x00, 0x08, 0x04, 0x02, 0x01,
165 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
167 frame::extended_header h4(uint64_t(0x0807060504030201LL));
168 uint8_t h4_solution[12] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
169 0x02, 0x01, 0x00, 0x00, 0x00, 0x00};
171 frame::extended_header h5(uint64_t(0x0807060504030201LL),htonl(0x8040201));
172 uint8_t h5_solution[12] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
173 0x02, 0x01, 0x08, 0x04, 0x02, 0x01};
175 BOOST_CHECK( std::equal(h1_solution,h1_solution+12,h1.bytes) );
176 BOOST_CHECK( std::equal(h2_solution,h2_solution+12,h2.bytes) );
177 BOOST_CHECK( std::equal(h3_solution,h3_solution+12,h3.bytes) );
178 BOOST_CHECK( std::equal(h4_solution,h4_solution+12,h4.bytes) );
179 BOOST_CHECK( std::equal(h5_solution,h5_solution+12,h5.bytes) );
182 BOOST_AUTO_TEST_CASE( extended_header_extractors ) {
183 frame::basic_header h1(0x00,0x7E);
184 frame::extended_header e1(uint16_t(255));
185 BOOST_CHECK( get_extended_size(e1) == 255 );
186 BOOST_CHECK( get_payload_size(h1,e1) == 255 );
187 BOOST_CHECK( get_masking_key_offset(h1) == 2 );
188 BOOST_CHECK( get_masking_key(h1,e1).i == 0 );
190 frame::basic_header h2(0x00,0x7F);
191 frame::extended_header e2(uint64_t(0x0807060504030201LL));
192 BOOST_CHECK( get_jumbo_size(e2) == 0x0807060504030201LL );
193 BOOST_CHECK( get_payload_size(h2,e2) == 0x0807060504030201LL );
194 BOOST_CHECK( get_masking_key_offset(h2) == 8 );
195 BOOST_CHECK( get_masking_key(h2,e2).i == 0 );
197 frame::basic_header h3(0x00,0xFE);
198 frame::extended_header e3(uint16_t(255),0x08040201);
199 BOOST_CHECK( get_extended_size(e3) == 255 );
200 BOOST_CHECK( get_payload_size(h3,e3) == 255 );
201 BOOST_CHECK( get_masking_key_offset(h3) == 2 );
202 BOOST_CHECK( get_masking_key(h3,e3).i == 0x08040201 );
204 frame::basic_header h4(0x00,0xFF);
205 frame::extended_header e4(uint64_t(0x0807060504030201LL),0x08040201);
206 BOOST_CHECK( get_jumbo_size(e4) == 0x0807060504030201LL );
207 BOOST_CHECK( get_payload_size(h4,e4) == 0x0807060504030201LL );
208 BOOST_CHECK( get_masking_key_offset(h4) == 8 );
209 BOOST_CHECK( get_masking_key(h4,e4).i == 0x08040201 );
211 frame::basic_header h5(0x00,0x7D);
212 frame::extended_header e5;
213 BOOST_CHECK( get_payload_size(h5,e5) == 125 );
216 BOOST_AUTO_TEST_CASE( header_preparation ) {
217 frame::basic_header h1(0x81,0xFF); //
218 frame::extended_header e1(uint64_t(0xFFFFFLL),htonl(0xD5FB70EE));
219 std::string p1 = prepare_header(h1, e1);
220 uint8_t s1[14] = {0x81, 0xFF,
221 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF,
222 0xD5, 0xFB, 0x70, 0xEE};
224 BOOST_CHECK( p1.size() == 14);
225 BOOST_CHECK( std::equal(p1.begin(),p1.end(),reinterpret_cast<char*>(s1)) );
227 frame::basic_header h2(0x81,0x7E); //
228 frame::extended_header e2(uint16_t(255));
229 std::string p2 = prepare_header(h2, e2);
230 uint8_t s2[4] = {0x81, 0x7E, 0x00, 0xFF};
232 BOOST_CHECK( p2.size() == 4);
233 BOOST_CHECK( std::equal(p2.begin(),p2.end(),reinterpret_cast<char*>(s2)) );
236 BOOST_AUTO_TEST_CASE( prepare_masking_key ) {
237 frame::masking_key_type key;
239 key.i = htonl(0x12345678);
241 if (sizeof(size_t) == 8) {
243 frame::prepare_masking_key(key) == lib::net::_htonll(0x1234567812345678LL)
246 BOOST_CHECK( frame::prepare_masking_key(key) == htonl(0x12345678) );
250 BOOST_AUTO_TEST_CASE( prepare_masking_key2 ) {
251 frame::masking_key_type key;
253 key.i = htonl(0xD5FB70EE);
256 if (sizeof(size_t) == 8) {
258 frame::prepare_masking_key(key) == lib::net::_htonll(0xD5FB70EED5FB70EELL)
261 BOOST_CHECK( frame::prepare_masking_key(key) == htonl(0xD5FB70EE) );
265 // TODO: figure out a way to run/test both 4 and 8 byte versions.
266 BOOST_AUTO_TEST_CASE( circshift ) {
267 /*if (sizeof(size_t) == 8) {
268 size_t test = 0x0123456789abcdef;
270 BOOST_CHECK( frame::circshift_prepared_key(test,0) == 0x0123456789abcdef);
271 BOOST_CHECK( frame::circshift_prepared_key(test,1) == 0xef0123456789abcd);
272 BOOST_CHECK( frame::circshift_prepared_key(test,2) == 0xcdef0123456789ab);
273 BOOST_CHECK( frame::circshift_prepared_key(test,3) == 0xabcdef0123456789);
275 size_t test = 0x01234567;
277 BOOST_CHECK( frame::circshift_prepared_key(test,0) == 0x01234567);
278 BOOST_CHECK( frame::circshift_prepared_key(test,1) == 0x67012345);
279 BOOST_CHECK( frame::circshift_prepared_key(test,2) == 0x45670123);
280 BOOST_CHECK( frame::circshift_prepared_key(test,3) == 0x23456701);
284 BOOST_AUTO_TEST_CASE( block_byte_mask ) {
285 uint8_t input[15] = {0x00, 0x00, 0x00, 0x00,
286 0x00, 0x00, 0x00, 0x00,
287 0x00, 0x00, 0x00, 0x00,
292 uint8_t masked[15] = {0x00, 0x01, 0x02, 0x03,
293 0x00, 0x01, 0x02, 0x03,
294 0x00, 0x01, 0x02, 0x03,
297 frame::masking_key_type key;
303 byte_mask(input,input+15,output,key);
305 BOOST_CHECK( std::equal(output,output+15,masked) );
308 BOOST_AUTO_TEST_CASE( block_byte_mask_inplace ) {
309 uint8_t buffer[15] = {0x00, 0x00, 0x00, 0x00,
310 0x00, 0x00, 0x00, 0x00,
311 0x00, 0x00, 0x00, 0x00,
314 uint8_t masked[15] = {0x00, 0x01, 0x02, 0x03,
315 0x00, 0x01, 0x02, 0x03,
316 0x00, 0x01, 0x02, 0x03,
319 frame::masking_key_type key;
325 byte_mask(buffer,buffer+15,key);
327 BOOST_CHECK( std::equal(buffer,buffer+15,masked) );
330 BOOST_AUTO_TEST_CASE( block_word_mask ) {
331 uint8_t input[15] = {0x00, 0x00, 0x00, 0x00,
332 0x00, 0x00, 0x00, 0x00,
333 0x00, 0x00, 0x00, 0x00,
338 uint8_t masked[15] = {0x00, 0x01, 0x02, 0x03,
339 0x00, 0x01, 0x02, 0x03,
340 0x00, 0x01, 0x02, 0x03,
343 frame::masking_key_type key;
349 word_mask_exact(input,output,15,key);
351 BOOST_CHECK( std::equal(output,output+15,masked) );
354 BOOST_AUTO_TEST_CASE( block_word_mask_inplace ) {
355 uint8_t buffer[15] = {0x00, 0x00, 0x00, 0x00,
356 0x00, 0x00, 0x00, 0x00,
357 0x00, 0x00, 0x00, 0x00,
360 uint8_t masked[15] = {0x00, 0x01, 0x02, 0x03,
361 0x00, 0x01, 0x02, 0x03,
362 0x00, 0x01, 0x02, 0x03,
365 frame::masking_key_type key;
371 word_mask_exact(buffer,15,key);
373 BOOST_CHECK( std::equal(buffer,buffer+15,masked) );
376 BOOST_AUTO_TEST_CASE( continuous_word_mask ) {
380 uint8_t masked[16] = {0x00, 0x01, 0x02, 0x03,
381 0x00, 0x01, 0x02, 0x03,
382 0x00, 0x01, 0x02, 0x03,
383 0x00, 0x01, 0x02, 0x00};
385 frame::masking_key_type key;
392 size_t pkey,pkey_temp;
393 pkey = frame::prepare_masking_key(key);
394 std::fill_n(input,16,0x00);
395 std::fill_n(output,16,0x00);
396 frame::word_mask_circ(input,output,15,pkey);
397 BOOST_CHECK( std::equal(output,output+16,masked) );
399 // calls not split on word boundaries
400 pkey = frame::prepare_masking_key(key);
401 std::fill_n(input,16,0x00);
402 std::fill_n(output,16,0x00);
404 pkey_temp = frame::word_mask_circ(input,output,7,pkey);
405 BOOST_CHECK( std::equal(output,output+7,masked) );
406 BOOST_CHECK( pkey_temp == frame::circshift_prepared_key(pkey,3) );
408 pkey_temp = frame::word_mask_circ(input+7,output+7,8,pkey_temp);
409 BOOST_CHECK( std::equal(output,output+16,masked) );
410 BOOST_CHECK_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
413 BOOST_AUTO_TEST_CASE( continuous_byte_mask ) {
417 uint8_t masked[16] = {0x00, 0x01, 0x02, 0x03,
418 0x00, 0x01, 0x02, 0x03,
419 0x00, 0x01, 0x02, 0x03,
420 0x00, 0x01, 0x02, 0x00};
422 frame::masking_key_type key;
429 size_t pkey,pkey_temp;
430 pkey = frame::prepare_masking_key(key);
431 std::fill_n(input,16,0x00);
432 std::fill_n(output,16,0x00);
433 frame::byte_mask_circ(input,output,15,pkey);
434 BOOST_CHECK( std::equal(output,output+16,masked) );
436 // calls not split on word boundaries
437 pkey = frame::prepare_masking_key(key);
438 std::fill_n(input,16,0x00);
439 std::fill_n(output,16,0x00);
441 pkey_temp = frame::byte_mask_circ(input,output,7,pkey);
442 BOOST_CHECK( std::equal(output,output+7,masked) );
443 BOOST_CHECK( pkey_temp == frame::circshift_prepared_key(pkey,3) );
445 pkey_temp = frame::byte_mask_circ(input+7,output+7,8,pkey_temp);
446 BOOST_CHECK( std::equal(output,output+16,masked) );
447 BOOST_CHECK_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
450 BOOST_AUTO_TEST_CASE( continuous_word_mask_inplace ) {
453 uint8_t masked[16] = {0x00, 0x01, 0x02, 0x03,
454 0x00, 0x01, 0x02, 0x03,
455 0x00, 0x01, 0x02, 0x03,
456 0x00, 0x01, 0x02, 0x00};
458 frame::masking_key_type key;
465 size_t pkey,pkey_temp;
466 pkey = frame::prepare_masking_key(key);
467 std::fill_n(buffer,16,0x00);
468 frame::word_mask_circ(buffer,15,pkey);
469 BOOST_CHECK( std::equal(buffer,buffer+16,masked) );
471 // calls not split on word boundaries
472 pkey = frame::prepare_masking_key(key);
473 std::fill_n(buffer,16,0x00);
475 pkey_temp = frame::word_mask_circ(buffer,7,pkey);
476 BOOST_CHECK( std::equal(buffer,buffer+7,masked) );
477 BOOST_CHECK_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
479 pkey_temp = frame::word_mask_circ(buffer+7,8,pkey_temp);
480 BOOST_CHECK( std::equal(buffer,buffer+16,masked) );
481 BOOST_CHECK_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
484 BOOST_AUTO_TEST_CASE( continuous_byte_mask_inplace ) {
487 uint8_t masked[16] = {0x00, 0x01, 0x02, 0x03,
488 0x00, 0x01, 0x02, 0x03,
489 0x00, 0x01, 0x02, 0x03,
490 0x00, 0x01, 0x02, 0x00};
492 frame::masking_key_type key;
499 size_t pkey,pkey_temp;
500 pkey = frame::prepare_masking_key(key);
501 std::fill_n(buffer,16,0x00);
502 frame::byte_mask_circ(buffer,15,pkey);
503 BOOST_CHECK( std::equal(buffer,buffer+16,masked) );
505 // calls not split on word boundaries
506 pkey = frame::prepare_masking_key(key);
507 std::fill_n(buffer,16,0x00);
509 pkey_temp = frame::byte_mask_circ(buffer,7,pkey);
510 BOOST_CHECK( std::equal(buffer,buffer+7,masked) );
511 BOOST_CHECK_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
513 pkey_temp = frame::byte_mask_circ(buffer+7,8,pkey_temp);
514 BOOST_CHECK( std::equal(buffer,buffer+16,masked) );
515 BOOST_CHECK_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
518 BOOST_AUTO_TEST_CASE( continuous_word_mask2 ) {
519 uint8_t buffer[12] = {0xA6, 0x15, 0x97, 0xB9,
520 0x81, 0x50, 0xAC, 0xBA,
521 0x9C, 0x1C, 0x9F, 0xF4};
523 uint8_t unmasked[12] = {0x48, 0x65, 0x6C, 0x6C,
524 0x6F, 0x20, 0x57, 0x6F,
525 0x72, 0x6C, 0x64, 0x21};
527 frame::masking_key_type key;
535 pkey = frame::prepare_masking_key(key);
536 frame::word_mask_circ(buffer,12,pkey);
537 BOOST_CHECK( std::equal(buffer,buffer+12,unmasked) );