Electroneum
Loading...
Searching...
No Matches
levin.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-2019, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "include_base_utils.h"
30#include "file_io_utils.h"
31#include "net/net_utils_base.h"
35#include "fuzzer.h"
36
37namespace
38{
39 class call_counter
40 {
41 public:
42 call_counter() : m_counter(0) { }
43
44 // memory_order_relaxed is enough for call counter
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); }
48
49 private:
50 std::atomic<size_t> m_counter;
51 };
52
53 struct test_levin_connection_context : public epee::net_utils::connection_context_base
54 {
55 };
56
59
60 struct test_levin_commands_handler : public epee::levin::levin_commands_handler<test_levin_connection_context>
61 {
62 test_levin_commands_handler()
63 : m_return_code(LEVIN_OK)
64 , m_last_command(-1)
65 {
66 }
67
68 virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, test_levin_connection_context& context)
69 {
70 m_invoke_counter.inc();
71 boost::unique_lock<boost::mutex> lock(m_mutex);
72 m_last_command = command;
73 m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
74 buff_out = m_invoke_out_buf;
75 return m_return_code;
76 }
77
78 virtual int notify(int command, const epee::span<const uint8_t> in_buff, test_levin_connection_context& context)
79 {
80 m_notify_counter.inc();
81 boost::unique_lock<boost::mutex> lock(m_mutex);
82 m_last_command = command;
83 m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
84 return m_return_code;
85 }
86
87 virtual void callback(test_levin_connection_context& context)
88 {
89 m_callback_counter.inc();
90 //std::cout << "test_levin_commands_handler::callback()" << std::endl;
91 }
92
93 virtual void on_connection_new(test_levin_connection_context& context)
94 {
95 m_new_connection_counter.inc();
96 //std::cout << "test_levin_commands_handler::on_connection_new()" << std::endl;
97 }
98
99 virtual void on_connection_close(test_levin_connection_context& context)
100 {
101 m_close_connection_counter.inc();
102 //std::cout << "test_levin_commands_handler::on_connection_close()" << std::endl;
103 }
104
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(); }
110
111 int return_code() const { return m_return_code; }
112 void return_code(int v) { m_return_code = v; }
113
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; }
116
117 int last_command() const { return m_last_command; }
118 const std::string& last_in_buf() const { return m_last_in_buf; }
119
120 private:
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;
126
127 boost::mutex m_mutex;
128
129 int m_return_code;
130 std::string m_invoke_out_buf;
131
132 int m_last_command;
133 std::string m_last_in_buf;
134 };
135
136 class test_connection : public epee::net_utils::i_service_endpoint
137 {
138 public:
139 test_connection(boost::asio::io_service& io_service, test_levin_protocol_handler_config& protocol_config)
140 : m_io_service(io_service)
141 , m_protocol_handler(this, protocol_config, m_context)
142 , m_send_return(true)
143 {
144 }
145
146 void start()
147 {
148 m_protocol_handler.after_init_connection();
149 }
150
151 // Implement epee::net_utils::i_service_endpoint interface
152 virtual bool do_send(const void* ptr, size_t cb)
153 {
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;
158 }
159
160 virtual bool close() { return true; }
161 virtual bool send_done() { return true; }
162 virtual bool call_run_once_service_io() { return true; }
163 virtual bool request_callback() { 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; }
167
168 size_t send_counter() const { return m_send_counter.get(); }
169
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(); }
172
173 bool send_return() const { return m_send_return; }
174 void send_return(bool v) { m_send_return = v; }
175
176 public:
177 test_levin_connection_context m_context;
178 test_levin_protocol_handler m_protocol_handler;
179
180 private:
181 boost::asio::io_service& m_io_service;
182
183 call_counter m_send_counter;
184 boost::mutex m_mutex;
185
186 std::string m_last_send_data;
187
188 bool m_send_return;
189 };
190
191#if 0
192 class async_protocol_handler_test : public ::testing::Test
193 {
194 public:
195 const static uint64_t invoke_timeout = 5 * 1000;
196 const static size_t max_packet_size = 10 * 1024 * 1024;
197
198 typedef std::unique_ptr<test_connection> test_connection_ptr;
199
200 async_protocol_handler_test():
201 m_pcommands_handler(new test_levin_commands_handler()),
202 m_commands_handler(*m_pcommands_handler)
203 {
204 m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; });
205 m_handler_config.m_invoke_timeout = invoke_timeout;
206 m_handler_config.m_max_packet_size = max_packet_size;
207 }
208
209 virtual void SetUp()
210 {
211 }
212
213 protected:
214 test_connection_ptr create_connection(bool start = true)
215 {
216 test_connection_ptr conn(new test_connection(m_io_service, m_handler_config));
217 if (start)
218 {
219 conn->start();
220 }
221 return conn;
222 }
223
224 protected:
225 boost::asio::io_service m_io_service;
226 test_levin_protocol_handler_config m_handler_config;
227 test_levin_commands_handler *m_pcommands_handler, &m_commands_handler;
228 };
229
230 class positive_test_connection_to_levin_protocol_handler_calls : public async_protocol_handler_test
231 {
232 };
233
234 class test_levin_protocol_handler__hanle_recv_with_invalid_data : public async_protocol_handler_test
235 {
236 public:
237 static const int expected_command = 5615871;
238 static const int expected_return_code = 782546;
239
240 test_levin_protocol_handler__hanle_recv_with_invalid_data()
241 : m_expected_invoke_out_buf(512, 'y')
242 {
243 }
244
245 virtual void SetUp()
246 {
247 async_protocol_handler_test::SetUp();
248
249 m_conn = create_connection();
250
251 m_in_data.assign(256, 't');
252
253 m_req_head.m_signature = LEVIN_SIGNATURE;
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;
258 m_req_head.m_flags = LEVIN_PACKET_REQUEST;
259 m_req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
260
261 m_commands_handler.return_code(expected_return_code);
262 m_commands_handler.invoke_out_buf(m_expected_invoke_out_buf);
263 }
264
265 protected:
266 void prepare_buf()
267 {
268 m_buf.assign(reinterpret_cast<const char*>(&m_req_head), sizeof(m_req_head));
269 m_buf += m_in_data;
270 }
271
272 protected:
273 test_connection_ptr m_conn;
274 epee::levin::bucket_head2 m_req_head;
275 std::string m_in_data;
276 std::string m_buf;
277 std::string m_expected_invoke_out_buf;
278 };
279#endif
280}
281
282class LevinFuzzer: public Fuzzer
283{
284public:
285 LevinFuzzer() {} //: handler(endpoint, config, context) {}
286 virtual int init();
287 virtual int run(const std::string &filename);
288
289private:
290 //epee::net_utils::connection_context_base context;
291 //epee::levin::async_protocol_handler<> handler;
292};
293
295{
296 return 0;
297}
298
299int LevinFuzzer::run(const std::string &filename)
300{
301 std::string s;
302
303#if 0
305 req_head.m_signature = LEVIN_SIGNATURE;
306 req_head.m_cb = 0;
307 req_head.m_have_to_return_data = true;
308 req_head.m_command = 2000;
309 req_head.m_flags = LEVIN_PACKET_REQUEST;
311 req_head.m_return_code = 0;
312 FILE *f=fopen("/tmp/out.levin", "w");
313 fwrite(&req_head,sizeof(req_head),1, f);
314 fclose(f);
315#endif
317 {
318 std::cout << "Error: failed to load file " << filename << std::endl;
319 return 1;
320 }
321 try
322 {
323 //std::unique_ptr<test_connection> conn = new test();
324 boost::asio::io_service io_service;
325 test_levin_protocol_handler_config m_handler_config;
326 test_levin_commands_handler *m_pcommands_handler = new test_levin_commands_handler();
327 m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; });
328 std::unique_ptr<test_connection> conn(new test_connection(io_service, m_handler_config));
329 conn->start();
330 //m_commands_handler.invoke_out_buf(expected_out_data);
331 //m_commands_handler.return_code(expected_return_code);
332 conn->m_protocol_handler.handle_recv(s.data(), s.size());
333 }
334 catch (const std::exception &e)
335 {
336 std::cerr << "Failed to test http client: " << e.what() << std::endl;
337 return 1;
338 }
339 return 0;
340}
341
342int main(int argc, const char **argv)
343{
344 TRY_ENTRY();
345 LevinFuzzer fuzzer;
346 return run_fuzzer(argc, argv, fuzzer);
347 CATCH_ENTRY_L0("main", 1);
348}
349
the connection templated-class for one peer connection
int main()
virtual int run(const std::string &filename)
Definition levin.cpp:299
virtual int init()
Definition levin.cpp:294
constexpr std::size_t size() const noexcept
Definition span.h:111
constexpr pointer data() const noexcept
Definition span.h:110
int run_fuzzer(int argc, const char **argv, Fuzzer &fuzzer)
Definition fuzzer.cpp:48
void get(std::istream &input, bool &res)
Definition io.h:62
#define LEVIN_PROTOCOL_VER_1
Definition levin_base.h:78
#define LEVIN_PACKET_REQUEST
Definition levin_base.h:73
#define LEVIN_OK
Definition levin_base.h:93
#define LEVIN_SIGNATURE
Definition levin_base.h:34
#define CATCH_ENTRY_L0(lacation, return_val)
#define TRY_ENTRY()
bool load_file_to_string(const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
epee::levin::async_protocol_handler_config< test_connection_context > test_levin_protocol_handler_config
epee::net_utils::connection< test_levin_protocol_handler > test_connection
epee::levin::async_protocol_handler< test_connection_context > test_levin_protocol_handler
#define true
unsigned __int64 uint64_t
Definition stdint.h:136