restructures fuzzing client to be more generic

This commit is contained in:
Peter Thorson
2011-12-08 09:25:06 -06:00
parent f92a5f5e8b
commit 0e9b0b99cf

View File

@@ -34,99 +34,145 @@ typedef websocketpp::endpoint<websocketpp::role::client,websocketpp::socket::pla
typedef plain_endpoint_type::handler_ptr plain_handler_ptr;
typedef plain_endpoint_type::connection_ptr connection_ptr;
class echo_client_handler : public plain_endpoint_type::handler {
// base test case handler
class test_case_handler : public plain_endpoint_type::handler {
public:
typedef echo_client_handler type;
typedef test_case_handler type;
typedef plain_endpoint_type::connection_ptr connection_ptr;
void on_open(connection_ptr connection) {
agent = connection->get_response_header("Server");
std::cout << "Testing agent " << agent << std::endl;
// needs more C++11 intializer lists =(
test_9_1_X_sizes[0] = 65536;
test_9_1_X_sizes[1] = 262144;
test_9_1_X_sizes[2] = 262144;
test_9_1_X_sizes[3] = 4194304;
test_9_1_X_sizes[4] = 8388608;
test_9_1_X_sizes[5] = 16777216;
virtual void end(connection_ptr connection) {
// test is over, give control back to controlling handler
boost::posix_time::time_period len(m_start_time,m_end_time);
test = 1;
start_9_1_X(test);
connection->send(data,true);
}
void on_message(connection_ptr connection,websocketpp::message::data_ptr msg) {
end_time = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_period len(start_time,end_time);
if (msg->get_payload() == data) {
if (m_pass) {
std::cout << " passes in " << len.length() << std::endl;
} else {
std::cout << " fails in " << len.length() << std::endl;
}
connection->recycle(msg);
connection->close(websocketpp::close::status::NORMAL,"");
}
protected:
bool m_pass;
boost::posix_time::ptime m_start_time;
boost::posix_time::ptime m_end_time;
};
// test class for 9.1.* and 9.2.*
class test_9_1_X : public test_case_handler {
public:
test_9_1_X(int minor, int subtest) : m_minor(minor),m_subtest(subtest){
// needs more C++11 intializer lists =(
m_test_sizes[0] = 65536;
m_test_sizes[1] = 262144;
m_test_sizes[2] = 1048576;
m_test_sizes[3] = 4194304;
m_test_sizes[4] = 8388608;
m_test_sizes[5] = 16777216;
}
void on_open(connection_ptr connection) {
std::cout << "Test 9." << m_minor << "." << m_subtest;
if (test == 6) {
std::cout << "Done" << std::endl;
connection->close(websocketpp::close::status::NORMAL, "");
m_data.reserve(m_test_sizes[m_subtest-1]);
if (m_minor == 1) {
fill_utf8(connection,true);
m_start_time = boost::posix_time::microsec_clock::local_time();
connection->send(m_data,false);
} else if (m_minor == 2) {
fill_binary(connection,true);
m_start_time = boost::posix_time::microsec_clock::local_time();
connection->send(m_data,true);
} else {
test++;
start_9_1_X(test);
connection->send(data,true);
std::cout << " has unknown definition." << std::endl;
}
}
void http(connection_ptr connection) {
//connection->set_body("HTTP Response!!");
// Just does random ascii right now. True random UTF8 with multi-byte stuff
// would probably be better
void fill_utf8(connection_ptr connection,bool random = true) {
if (random) {
uint32_t data;
for (int i = 0; i < m_test_sizes[m_subtest-1]; i++) {
if (i%4 == 0) {
data = uint32_t(connection->rand());
}
m_data.push_back(char(((reinterpret_cast<uint8_t*>(&data)[i%4])%95)+32));
}
} else {
m_data.assign(m_test_sizes[m_subtest-1],'*');
}
}
void on_fail(connection_ptr connection) {
std::cout << "connection failed" << std::endl;
void fill_binary(connection_ptr connection, bool random = true) {
if (random) {
int32_t data;
for (int i = 0; i < m_test_sizes[m_subtest-1]; i++) {
if (i%4 == 0) {
data = connection->rand();
}
m_data.push_back((reinterpret_cast<char*>(&data))[i%4]);
}
} else {
m_data.assign(m_test_sizes[m_subtest-1],'*');
}
}
void start_9_1_X(int subtest) {
std::cout << "Test 9.2." << subtest << " ";
void on_message(connection_ptr connection,websocketpp::message::data_ptr msg) {
m_end_time = boost::posix_time::microsec_clock::local_time();
data.reserve(test_9_1_X_sizes[subtest-1]);
data.assign(test_9_1_X_sizes[subtest-1],'*');
start_time = boost::posix_time::microsec_clock::local_time();
// Check whether the echoed data matches exactly
m_pass = (msg->get_payload() == m_data);
connection->recycle(msg);
this->end(connection);
}
size_t test_9_1_X_sizes[6];
int test;
std::string agent;
std::string data;
boost::posix_time::ptime start_time;
boost::posix_time::ptime end_time;
private:
int m_minor;
int m_subtest;
size_t m_test_sizes[6];
std::string m_data;
};
int main(int argc, char* argv[]) {
std::string uri;
/*if (argc != 2) {
std::cout << "Usage: `echo_client test_url`" << std::endl;
if (argc != 2) {
uri = "ws://localhost:9002/";
} else {
uri = argv[1];
}*/
}
std::vector<plain_handler_ptr> tests;
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 6; j++) {
tests.push_back(plain_handler_ptr(new test_9_1_X(i,j)));
}
}
try {
plain_handler_ptr handler(new echo_client_handler());
connection_ptr connection;
plain_endpoint_type endpoint(handler);
plain_endpoint_type endpoint(tests[0]);
endpoint.alog().unset_level(websocketpp::log::alevel::ALL);
endpoint.elog().unset_level(websocketpp::log::elevel::ALL);
connection = endpoint.connect("ws://localhost:9000/");
endpoint.run();
for (int i = 0; i < tests.size(); i++) {
if (i > 0) {
endpoint.reset();
endpoint.set_handler(tests[i]);
}
endpoint.connect(uri);
endpoint.run();
}
std::cout << "done" << std::endl;