1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// Copyright (c) 2017-2020, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "include_base_utils.h"
#include "file_io_utils.h"
#include "net/net_utils_base.h"
#include "net/abstract_tcp_server2.h"
#include "storages/levin_abstract_invoke2.h"
#include "net/levin_protocol_handler_async.h"
#include "fuzzer.h"
namespace
{
class call_counter
{
public:
call_counter() : m_counter(0) { }
// memory_order_relaxed is enough for call counter
void inc() volatile { m_counter.fetch_add(1, std::memory_order_relaxed); }
size_t get() volatile const { return m_counter.load(std::memory_order_relaxed); }
void reset() volatile { m_counter.store(0, std::memory_order_relaxed); }
private:
std::atomic<size_t> m_counter;
};
struct test_levin_connection_context : public epee::net_utils::connection_context_base
{
static constexpr int handshake_command() noexcept { return 1001; }
static constexpr bool handshake_complete() noexcept { return true; }
};
typedef epee::levin::async_protocol_handler_config<test_levin_connection_context> test_levin_protocol_handler_config;
typedef epee::levin::async_protocol_handler<test_levin_connection_context> test_levin_protocol_handler;
struct test_levin_commands_handler : public epee::levin::levin_commands_handler<test_levin_connection_context>
{
test_levin_commands_handler()
: m_return_code(LEVIN_OK)
, m_last_command(-1)
{
}
virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, test_levin_connection_context& context)
{
m_invoke_counter.inc();
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_command = command;
m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
buff_out = m_invoke_out_buf;
return m_return_code;
}
virtual int notify(int command, const epee::span<const uint8_t> in_buff, test_levin_connection_context& context)
{
m_notify_counter.inc();
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_command = command;
m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
return m_return_code;
}
virtual void callback(test_levin_connection_context& context)
{
m_callback_counter.inc();
//std::cout << "test_levin_commands_handler::callback()" << std::endl;
}
virtual void on_connection_new(test_levin_connection_context& context)
{
m_new_connection_counter.inc();
//std::cout << "test_levin_commands_handler::on_connection_new()" << std::endl;
}
virtual void on_connection_close(test_levin_connection_context& context)
{
m_close_connection_counter.inc();
//std::cout << "test_levin_commands_handler::on_connection_close()" << std::endl;
}
size_t invoke_counter() const { return m_invoke_counter.get(); }
size_t notify_counter() const { return m_notify_counter.get(); }
size_t callback_counter() const { return m_callback_counter.get(); }
size_t new_connection_counter() const { return m_new_connection_counter.get(); }
size_t close_connection_counter() const { return m_close_connection_counter.get(); }
int return_code() const { return m_return_code; }
void return_code(int v) { m_return_code = v; }
const std::string& invoke_out_buf() const { return m_invoke_out_buf; }
void invoke_out_buf(const std::string& v) { m_invoke_out_buf = v; }
int last_command() const { return m_last_command; }
const std::string& last_in_buf() const { return m_last_in_buf; }
private:
call_counter m_invoke_counter;
call_counter m_notify_counter;
call_counter m_callback_counter;
call_counter m_new_connection_counter;
call_counter m_close_connection_counter;
boost::mutex m_mutex;
int m_return_code;
std::string m_invoke_out_buf;
int m_last_command;
std::string m_last_in_buf;
};
class test_connection : public epee::net_utils::i_service_endpoint
{
public:
test_connection(boost::asio::io_service& io_service, test_levin_protocol_handler_config& protocol_config)
: m_io_service(io_service)
, m_protocol_handler(this, protocol_config, m_context)
, m_send_return(true)
{
}
void start()
{
m_protocol_handler.after_init_connection();
}
// Implement epee::net_utils::i_service_endpoint interface
virtual bool do_send(epee::byte_slice message)
{
m_send_counter.inc();
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_send_data.append(reinterpret_cast<const char*>(message.data()), message.size());
return m_send_return;
}
virtual bool close() { return true; }
virtual bool send_done() { return true; }
virtual bool call_run_once_service_io() { return true; }
virtual bool request_callback() { return true; }
virtual boost::asio::io_service& get_io_service() { return m_io_service; }
virtual bool add_ref() { return true; }
virtual bool release() { return true; }
size_t send_counter() const { return m_send_counter.get(); }
const std::string& last_send_data() const { return m_last_send_data; }
void reset_last_send_data() { boost::unique_lock<boost::mutex> lock(m_mutex); m_last_send_data.clear(); }
bool send_return() const { return m_send_return; }
void send_return(bool v) { m_send_return = v; }
public:
test_levin_connection_context m_context;
test_levin_protocol_handler m_protocol_handler;
private:
boost::asio::io_service& m_io_service;
call_counter m_send_counter;
boost::mutex m_mutex;
std::string m_last_send_data;
bool m_send_return;
};
#if 0
class async_protocol_handler_test : public ::testing::Test
{
public:
const static uint64_t invoke_timeout = 5 * 1000;
const static size_t max_packet_size = 10 * 1024 * 1024;
typedef std::unique_ptr<test_connection> test_connection_ptr;
async_protocol_handler_test():
m_pcommands_handler(new test_levin_commands_handler()),
m_commands_handler(*m_pcommands_handler)
{
m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; });
m_handler_config.m_invoke_timeout = invoke_timeout;
m_handler_config.m_max_packet_size = max_packet_size;
}
virtual void SetUp()
{
}
protected:
test_connection_ptr create_connection(bool start = true)
{
test_connection_ptr conn(new test_connection(m_io_service, m_handler_config));
if (start)
{
conn->start();
}
return conn;
}
protected:
boost::asio::io_service m_io_service;
test_levin_protocol_handler_config m_handler_config;
test_levin_commands_handler *m_pcommands_handler, &m_commands_handler;
};
class positive_test_connection_to_levin_protocol_handler_calls : public async_protocol_handler_test
{
};
class test_levin_protocol_handler__hanle_recv_with_invalid_data : public async_protocol_handler_test
{
public:
static const int expected_command = 5615871;
static const int expected_return_code = 782546;
test_levin_protocol_handler__hanle_recv_with_invalid_data()
: m_expected_invoke_out_buf(512, 'y')
{
}
virtual void SetUp()
{
async_protocol_handler_test::SetUp();
m_conn = create_connection();
m_in_data.assign(256, 't');
m_req_head.m_signature = LEVIN_SIGNATURE;
m_req_head.m_cb = m_in_data.size();
m_req_head.m_have_to_return_data = true;
m_req_head.m_command = expected_command;
m_req_head.m_return_code = LEVIN_OK;
m_req_head.m_flags = LEVIN_PACKET_REQUEST;
m_req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
m_commands_handler.return_code(expected_return_code);
m_commands_handler.invoke_out_buf(m_expected_invoke_out_buf);
}
protected:
void prepare_buf()
{
m_buf.assign(reinterpret_cast<const char*>(&m_req_head), sizeof(m_req_head));
m_buf += m_in_data;
}
protected:
test_connection_ptr m_conn;
epee::levin::bucket_head2 m_req_head;
std::string m_in_data;
std::string m_buf;
std::string m_expected_invoke_out_buf;
};
#endif
}
BEGIN_INIT_SIMPLE_FUZZER()
END_INIT_SIMPLE_FUZZER()
BEGIN_SIMPLE_FUZZER()
#if 0
epee::levin::bucket_head2 req_head;
req_head.m_signature = LEVIN_SIGNATURE;
req_head.m_cb = 0;
req_head.m_have_to_return_data = true;
req_head.m_command = 2000;
req_head.m_flags = LEVIN_PACKET_REQUEST;
req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
req_head.m_return_code = 0;
FILE *f=fopen("/tmp/out.levin", "w");
fwrite(&req_head,sizeof(req_head),1, f);
fclose(f);
#endif
//std::unique_ptr<test_connection> conn = new test();
boost::asio::io_service io_service;
test_levin_protocol_handler_config m_handler_config;
test_levin_commands_handler *m_pcommands_handler = new test_levin_commands_handler();
m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; });
std::unique_ptr<test_connection> conn(new test_connection(io_service, m_handler_config));
conn->start();
//m_commands_handler.invoke_out_buf(expected_out_data);
//m_commands_handler.return_code(expected_return_code);
conn->m_protocol_handler.handle_recv(buf, len);
END_SIMPLE_FUZZER()
|