| 1 | // Copyright (c) 2017-2022, 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" |
| 32 | #include "net/abstract_tcp_server2.h" |
| 33 | #include "storages/levin_abstract_invoke2.h" |
| 34 | #include "net/levin_protocol_handler_async.h" |
| 35 | #include "fuzzer.h" |
| 36 | |
| 37 | namespace |
| 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 | static constexpr int handshake_command() noexcept { return 1001; } |
| 56 | static constexpr bool handshake_complete() noexcept { return true; } |
| 57 | size_t get_max_bytes(int command) const { return LEVIN_DEFAULT_MAX_PACKET_SIZE; } |
| 58 | }; |
| 59 | |
| 60 | typedef epee::levin::async_protocol_handler_config<test_levin_connection_context> test_levin_protocol_handler_config; |
| 61 | typedef epee::levin::async_protocol_handler<test_levin_connection_context> test_levin_protocol_handler; |
| 62 | |
| 63 | struct test_levin_commands_handler : public epee::levin::levin_commands_handler<test_levin_connection_context> |
| 64 | { |
| 65 | test_levin_commands_handler() |
| 66 | : m_return_code(LEVIN_OK) |
| 67 | , m_last_command(-1) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | virtual int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_stream& buff_out, test_levin_connection_context& context) |
| 72 | { |
| 73 | m_invoke_counter.inc(); |
| 74 | boost::unique_lock<boost::mutex> lock(m_mutex); |
| 75 | m_last_command = command; |
| 76 | m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size()); |
| 77 | buff_out.write(epee::to_span(m_invoke_out_buf)); |
| 78 | return m_return_code; |
| 79 | } |
| 80 | |
| 81 | virtual int notify(int command, const epee::span<const uint8_t> in_buff, test_levin_connection_context& context) |
| 82 | { |
| 83 | m_notify_counter.inc(); |
| 84 | boost::unique_lock<boost::mutex> lock(m_mutex); |
| 85 | m_last_command = command; |
| 86 | m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size()); |
| 87 | return m_return_code; |
| 88 | } |
| 89 | |
| 90 | virtual void callback(test_levin_connection_context& context) |
| 91 | { |
| 92 | m_callback_counter.inc(); |
| 93 | //std::cout << "test_levin_commands_handler::callback()" << std::endl; |
| 94 | } |
| 95 | |
| 96 | virtual void on_connection_new(test_levin_connection_context& context) |
| 97 | { |
| 98 | m_new_connection_counter.inc(); |
| 99 | //std::cout << "test_levin_commands_handler::on_connection_new()" << std::endl; |
| 100 | } |
| 101 | |
| 102 | virtual void on_connection_close(test_levin_connection_context& context) |
| 103 | { |
| 104 | m_close_connection_counter.inc(); |
| 105 | //std::cout << "test_levin_commands_handler::on_connection_close()" << std::endl; |
| 106 | } |
| 107 | |
| 108 | size_t invoke_counter() const { return m_invoke_counter.get(); } |
| 109 | size_t notify_counter() const { return m_notify_counter.get(); } |
| 110 | size_t callback_counter() const { return m_callback_counter.get(); } |
| 111 | size_t new_connection_counter() const { return m_new_connection_counter.get(); } |
| 112 | size_t close_connection_counter() const { return m_close_connection_counter.get(); } |
| 113 | |
| 114 | int return_code() const { return m_return_code; } |
| 115 | void return_code(int v) { m_return_code = v; } |
| 116 | |
| 117 | void invoke_out_buf(std::string v) { m_invoke_out_buf = epee::byte_slice{std::move(v)}; } |
| 118 | |
| 119 | int last_command() const { return m_last_command; } |
| 120 | const std::string& last_in_buf() const { return m_last_in_buf; } |
| 121 | |
| 122 | private: |
| 123 | call_counter m_invoke_counter; |
| 124 | call_counter m_notify_counter; |
| 125 | call_counter m_callback_counter; |
| 126 | call_counter m_new_connection_counter; |
| 127 | call_counter m_close_connection_counter; |
| 128 | |
| 129 | boost::mutex m_mutex; |
| 130 | |
| 131 | int m_return_code; |
| 132 | epee::byte_slice m_invoke_out_buf; |
| 133 | |
| 134 | int m_last_command; |
| 135 | std::string m_last_in_buf; |
| 136 | }; |
| 137 | |
| 138 | class test_connection : public epee::net_utils::i_service_endpoint |
| 139 | { |
| 140 | public: |
| 141 | test_connection(boost::asio::io_context& io_service, test_levin_protocol_handler_config& protocol_config) |
| 142 | : m_io_service(io_service) |
| 143 | , m_protocol_handler(this, protocol_config, m_context) |
| 144 | , m_send_return(true) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | void start() |
| 149 | { |
| 150 | m_protocol_handler.after_init_connection(); |
| 151 | } |
| 152 | |
| 153 | // Implement epee::net_utils::i_service_endpoint interface |
| 154 | virtual bool do_send(epee::byte_slice message) |
| 155 | { |
| 156 | m_send_counter.inc(); |
| 157 | boost::unique_lock<boost::mutex> lock(m_mutex); |
| 158 | m_last_send_data.append(reinterpret_cast<const char*>(message.data()), message.size()); |
| 159 | return m_send_return; |
| 160 | } |
| 161 | |
| 162 | virtual bool close(const bool wait_for_shutdown) { return true; } |
| 163 | virtual bool send_done() { return true; } |
| 164 | virtual bool call_run_once_service_io() { return true; } |
| 165 | virtual bool request_callback() { return true; } |
| 166 | virtual boost::asio::io_context& get_io_context() { return m_io_service; } |
| 167 | virtual bool add_ref() { return true; } |
| 168 | virtual bool release() { return true; } |
| 169 | |
| 170 | size_t send_counter() const { return m_send_counter.get(); } |
| 171 | |
| 172 | const std::string& last_send_data() const { return m_last_send_data; } |
| 173 | void reset_last_send_data() { boost::unique_lock<boost::mutex> lock(m_mutex); m_last_send_data.clear(); } |
| 174 | |
| 175 | bool send_return() const { return m_send_return; } |
| 176 | void send_return(bool v) { m_send_return = v; } |
| 177 | |
| 178 | public: |
| 179 | test_levin_connection_context m_context; |
| 180 | test_levin_protocol_handler m_protocol_handler; |
| 181 | |
| 182 | private: |
| 183 | boost::asio::io_context& m_io_service; |
| 184 | |
| 185 | call_counter m_send_counter; |
| 186 | boost::mutex m_mutex; |
| 187 | |
| 188 | std::string m_last_send_data; |
| 189 | |
| 190 | bool m_send_return; |
| 191 | }; |
| 192 | |
| 193 | #if 0 |
| 194 | class async_protocol_handler_test : public ::testing::Test |
| 195 | { |
| 196 | public: |
| 197 | const static uint64_t invoke_timeout = 5 * 1000; |
| 198 | const static size_t max_packet_size = 10 * 1024 * 1024; |
| 199 | |
| 200 | typedef std::unique_ptr<test_connection> test_connection_ptr; |
| 201 | |
| 202 | async_protocol_handler_test(): |
| 203 | m_pcommands_handler(new test_levin_commands_handler()), |
| 204 | m_commands_handler(*m_pcommands_handler) |
| 205 | { |
| 206 | m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; }); |
| 207 | m_handler_config.m_invoke_timeout = invoke_timeout; |
| 208 | m_handler_config.m_max_packet_size = max_packet_size; |
| 209 | } |
| 210 | |
| 211 | virtual void SetUp() |
| 212 | { |
| 213 | } |
| 214 | |
| 215 | protected: |
| 216 | test_connection_ptr create_connection(bool start = true) |
| 217 | { |
| 218 | test_connection_ptr conn(new test_connection(m_io_service, m_handler_config)); |
| 219 | if (start) |
| 220 | { |
| 221 | conn->start(); |
| 222 | } |
| 223 | return conn; |
| 224 | } |
| 225 | |
| 226 | protected: |
| 227 | boost::asio::io_context m_io_service; |
| 228 | test_levin_protocol_handler_config m_handler_config; |
| 229 | test_levin_commands_handler *m_pcommands_handler, &m_commands_handler; |
| 230 | }; |
| 231 | |
| 232 | class positive_test_connection_to_levin_protocol_handler_calls : public async_protocol_handler_test |
| 233 | { |
| 234 | }; |
| 235 | |
| 236 | class test_levin_protocol_handler__hanle_recv_with_invalid_data : public async_protocol_handler_test |
| 237 | { |
| 238 | public: |
| 239 | static const int expected_command = 5615871; |
| 240 | static const int expected_return_code = 782546; |
| 241 | |
| 242 | test_levin_protocol_handler__hanle_recv_with_invalid_data() |
| 243 | : m_expected_invoke_out_buf(512, 'y') |
| 244 | { |
| 245 | } |
| 246 | |
| 247 | virtual void SetUp() |
| 248 | { |
| 249 | async_protocol_handler_test::SetUp(); |
| 250 | |
| 251 | m_conn = create_connection(); |
| 252 | |
| 253 | m_in_data.assign(256, 't'); |
| 254 | |
| 255 | m_req_head.m_signature = LEVIN_SIGNATURE; |
| 256 | m_req_head.m_cb = m_in_data.size(); |
| 257 | m_req_head.m_have_to_return_data = true; |
| 258 | m_req_head.m_command = expected_command; |
| 259 | m_req_head.m_return_code = LEVIN_OK; |
| 260 | m_req_head.m_flags = LEVIN_PACKET_REQUEST; |
| 261 | m_req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1; |
| 262 | |
| 263 | m_commands_handler.return_code(expected_return_code); |
| 264 | m_commands_handler.invoke_out_buf(m_expected_invoke_out_buf); |
| 265 | } |
| 266 | |
| 267 | protected: |
| 268 | void prepare_buf() |
| 269 | { |
| 270 | m_buf.assign(reinterpret_cast<const char*>(&m_req_head), sizeof(m_req_head)); |
| 271 | m_buf += m_in_data; |
| 272 | } |
| 273 | |
| 274 | protected: |
| 275 | test_connection_ptr m_conn; |
| 276 | epee::levin::bucket_head2 m_req_head; |
| 277 | std::string m_in_data; |
| 278 | std::string m_buf; |
| 279 | std::string m_expected_invoke_out_buf; |
| 280 | }; |
| 281 | #endif |
| 282 | } |
| 283 | |
| 284 | BEGIN_INIT_SIMPLE_FUZZER() |
| 285 | END_INIT_SIMPLE_FUZZER() |
| 286 | |
| 287 | BEGIN_SIMPLE_FUZZER() |
| 288 | |
| 289 | #if 0 |
| 290 | epee::levin::bucket_head2 req_head; |
| 291 | req_head.m_signature = LEVIN_SIGNATURE; |
| 292 | req_head.m_cb = 0; |
| 293 | req_head.m_have_to_return_data = true; |
| 294 | req_head.m_command = 2000; |
| 295 | req_head.m_flags = LEVIN_PACKET_REQUEST; |
| 296 | req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1; |
| 297 | req_head.m_return_code = 0; |
| 298 | FILE *f=fopen("/tmp/out.levin", "w"); |
| 299 | fwrite(&req_head,sizeof(req_head),1, f); |
| 300 | fclose(f); |
| 301 | #endif |
| 302 | //std::unique_ptr<test_connection> conn = new test(); |
| 303 | boost::asio::io_context io_service; |
| 304 | test_levin_protocol_handler_config m_handler_config; |
| 305 | test_levin_commands_handler *m_pcommands_handler = new test_levin_commands_handler(); |
| 306 | m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler<test_levin_connection_context> *handler) { delete handler; }); |
| 307 | std::unique_ptr<test_connection> conn(new test_connection(io_service, m_handler_config)); |
| 308 | conn->start(); |
| 309 | //m_commands_handler.invoke_out_buf(expected_out_data); |
| 310 | //m_commands_handler.return_code(expected_return_code); |
| 311 | conn->m_protocol_handler.handle_recv(buf, len); |
| 312 | END_SIMPLE_FUZZER() |