diff --git a/test/processors/hybi00.cpp b/test/processors/hybi00.cpp index ed012799d9..b95ba87d5c 100644 --- a/test/processors/hybi00.cpp +++ b/test/processors/hybi00.cpp @@ -59,6 +59,8 @@ struct processor_setup { websocketpp::processor::hybi00 p; }; +typedef stub_config::message_type::ptr message_ptr; + BOOST_AUTO_TEST_CASE( exact_match ) { processor_setup env(true); @@ -167,3 +169,45 @@ BOOST_AUTO_TEST_CASE( extract_subprotocols ) { BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) ); BOOST_CHECK_EQUAL( subps.size(), 0 ); } + +BOOST_AUTO_TEST_CASE( prepare_data_frame_null ) { + processor_setup env(true); + + message_ptr in = env.msg_manager->get_message(); + message_ptr out = env.msg_manager->get_message(); + message_ptr invalid; + + + // empty pointers arguements should return sane error + BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,invalid), websocketpp::processor::error::invalid_arguments ); + + BOOST_CHECK_EQUAL( env.p.prepare_data_frame(in,invalid), websocketpp::processor::error::invalid_arguments ); + + BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,out), websocketpp::processor::error::invalid_arguments ); + + // test valid opcodes + // text (1) should be the only valid opcode + for (int i = 0; i < 0xF; i++) { + in->set_opcode(websocketpp::frame::opcode::value(i)); + + env.ec = env.p.prepare_data_frame(in,out); + + if (i != 1) { + BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode ); + } else { + BOOST_CHECK_NE( env.ec, websocketpp::processor::error::invalid_opcode ); + } + } + + /* + * TODO: tests for invalid UTF8 + char buf[2] = {0x00, 0x00}; + + in->set_opcode(websocketpp::frame::opcode::text); + in->set_payload("foo"); + + env.ec = env.p.prepare_data_frame(in,out); + BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_payload ); + */ +} + diff --git a/websocketpp/processors/hybi00.hpp b/websocketpp/processors/hybi00.hpp index ca9e3a35dd..319e42d1df 100644 --- a/websocketpp/processors/hybi00.hpp +++ b/websocketpp/processors/hybi00.hpp @@ -39,6 +39,7 @@ #include +#include #include namespace websocketpp { @@ -218,19 +219,39 @@ public: */ virtual lib::error_code prepare_data_frame(message_ptr in, message_ptr out) { - // assert msg + if (!in || !out) { + return make_error_code(error::invalid_arguments); + } - // check if the message is prepared already + // TODO: check if the message is prepared already // validate opcode + if (in->get_opcode() != frame::opcode::text) { + return make_error_code(error::invalid_opcode); + } + + std::string& i = in->get_raw_payload(); + //std::string& o = out->get_raw_payload(); + // validate payload utf8 - - // if we are a client generate a masking key - + if (!utf8_validator::validate(i)) { + return make_error_code(error::invalid_payload); + } + // generate header - // perform compression - // perform masking + char h = 0x00; + char f = 0xff; + out->set_header(std::string(&h,1)); + + // process payload + out->set_payload(i); + out->append_payload(std::string(&f,1)); + + // hybi00 doesn't support compression + // hybi00 doesn't have masking + out->set_prepared(true); + return lib::error_code(); }