42 call_counter() : m_counter(0) { }
45 void inc()
volatile { m_counter.fetch_add(1, std::memory_order_relaxed); }
46 size_t get()
volatile const {
return m_counter.load(std::memory_order_relaxed); }
47 void reset()
volatile { m_counter.store(0, std::memory_order_relaxed); }
50 std::atomic<size_t> m_counter;
62 test_levin_commands_handler()
70 m_invoke_counter.inc();
71 boost::unique_lock<boost::mutex> lock(m_mutex);
72 m_last_command = command;
74 buff_out = m_invoke_out_buf;
80 m_notify_counter.inc();
81 boost::unique_lock<boost::mutex> lock(m_mutex);
82 m_last_command = command;
89 m_callback_counter.inc();
95 m_new_connection_counter.inc();
101 m_close_connection_counter.inc();
105 size_t invoke_counter()
const {
return m_invoke_counter.get(); }
106 size_t notify_counter()
const {
return m_notify_counter.get(); }
107 size_t callback_counter()
const {
return m_callback_counter.get(); }
108 size_t new_connection_counter()
const {
return m_new_connection_counter.get(); }
109 size_t close_connection_counter()
const {
return m_close_connection_counter.get(); }
111 int return_code()
const {
return m_return_code; }
112 void return_code(
int v) { m_return_code = v; }
114 const std::string& invoke_out_buf()
const {
return m_invoke_out_buf; }
115 void invoke_out_buf(
const std::string& v) { m_invoke_out_buf = v; }
117 int last_command()
const {
return m_last_command; }
118 const std::string& last_in_buf()
const {
return m_last_in_buf; }
121 call_counter m_invoke_counter;
122 call_counter m_notify_counter;
123 call_counter m_callback_counter;
124 call_counter m_new_connection_counter;
125 call_counter m_close_connection_counter;
127 boost::mutex m_mutex;
140 : m_io_service(io_service)
141 , m_protocol_handler(this, protocol_config, m_context)
142 , m_send_return(
true)
148 m_protocol_handler.after_init_connection();
152 virtual bool do_send(
const void* ptr,
size_t cb)
154 m_send_counter.inc();
155 boost::unique_lock<boost::mutex> lock(m_mutex);
156 m_last_send_data.append(reinterpret_cast<const char*>(ptr), cb);
157 return m_send_return;
160 virtual bool close() {
return true; }
161 virtual bool send_done() {
return true; }
164 virtual boost::asio::io_service&
get_io_service() {
return m_io_service; }
165 virtual bool add_ref() {
return true; }
166 virtual bool release() {
return true; }
168 size_t send_counter()
const {
return m_send_counter.get(); }
170 const std::string& last_send_data()
const {
return m_last_send_data; }
171 void reset_last_send_data() { boost::unique_lock<boost::mutex> lock(m_mutex); m_last_send_data.clear(); }
173 bool send_return()
const {
return m_send_return; }
174 void send_return(
bool v) { m_send_return = v; }
177 test_levin_connection_context m_context;
181 boost::asio::io_service& m_io_service;
183 call_counter m_send_counter;
184 boost::mutex m_mutex;
195 const static uint64_t invoke_timeout = 5 * 1000;
196 const static size_t max_packet_size = 10 * 1024 * 1024;
198 typedef std::unique_ptr<test_connection> test_connection_ptr;
200 async_protocol_handler_test():
201 m_pcommands_handler(new test_levin_commands_handler()),
202 m_commands_handler(*m_pcommands_handler)
205 m_handler_config.m_invoke_timeout = invoke_timeout;
206 m_handler_config.m_max_packet_size = max_packet_size;
214 test_connection_ptr create_connection(
bool start =
true)
216 test_connection_ptr conn(
new test_connection(m_io_service, m_handler_config));
225 boost::asio::io_service m_io_service;
227 test_levin_commands_handler *m_pcommands_handler, &m_commands_handler;
230 class positive_test_connection_to_levin_protocol_handler_calls :
public async_protocol_handler_test
234 class test_levin_protocol_handler__hanle_recv_with_invalid_data :
public async_protocol_handler_test
237 static const int expected_command = 5615871;
238 static const int expected_return_code = 782546;
240 test_levin_protocol_handler__hanle_recv_with_invalid_data()
241 : m_expected_invoke_out_buf(512,
'y')
247 async_protocol_handler_test::SetUp();
249 m_conn = create_connection();
251 m_in_data.assign(256,
't');
254 m_req_head.m_cb = m_in_data.size();
255 m_req_head.m_have_to_return_data =
true;
256 m_req_head.m_command = expected_command;
257 m_req_head.m_return_code =
LEVIN_OK;
261 m_commands_handler.return_code(expected_return_code);
262 m_commands_handler.invoke_out_buf(m_expected_invoke_out_buf);
268 m_buf.assign(reinterpret_cast<const char*>(&m_req_head),
sizeof(m_req_head));
273 test_connection_ptr m_conn;
312 FILE *f=fopen(
"/tmp/out.levin",
"w");
313 fwrite(&req_head,
sizeof(req_head),1, f);
318 std::cout <<
"Error: failed to load file " << filename << std::endl;
324 boost::asio::io_service io_service;
326 test_levin_commands_handler *m_pcommands_handler =
new test_levin_commands_handler();
328 std::unique_ptr<test_connection> conn(
new test_connection(io_service, m_handler_config));
332 conn->m_protocol_handler.handle_recv(s.data(), s.size());
334 catch (
const std::exception &e)
336 std::cerr <<
"Failed to test http client: " << e.what() << std::endl;
342 int main(
int argc,
const char **argv)
epee::net_utils::connection< test_levin_protocol_handler > test_connection
uint32_t m_protocol_version
int main(int argc, const char **argv)
#define LEVIN_PACKET_REQUEST
bool load_file_to_string(const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
Non-owning sequence of data. Does not deep copy.
virtual int invoke(int command, const epee::span< const uint8_t > in_buff, std::string &buff_out, t_connection_context &context)=0
constexpr std::size_t size() const noexcept
virtual int notify(int command, const epee::span< const uint8_t > in_buff, t_connection_context &context)=0
virtual void callback(t_connection_context &context)
the connection templated-class for one peer connection
epee::levin::async_protocol_handler_config< test_connection_context > test_levin_protocol_handler_config
unsigned __int64 uint64_t
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
virtual boost::asio::io_service & get_io_service()=0
bool m_have_to_return_data
epee::levin::async_protocol_handler< test_connection_context > test_levin_protocol_handler
int run_fuzzer(int argc, const char **argv, Fuzzer &fuzzer)
virtual bool call_run_once_service_io()=0
#define LEVIN_PROTOCOL_VER_1
virtual void on_connection_new(t_connection_context &context)
virtual bool send_done()=0
virtual bool request_callback()=0
virtual bool do_send(const void *ptr, size_t cb)=0
#define CATCH_ENTRY_L0(lacation, return_val)
virtual void on_connection_close(t_connection_context &context)
constexpr pointer data() const noexcept
virtual int run(const std::string &filename)