| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "base/macros.h" |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/fuzzed_data_provider.h" |
| 10 #include "net/base/net_errors.h" |
| 11 #include "net/log/test_net_log.h" |
| 12 #include "net/server/http_server.h" |
| 13 #include "net/socket/fuzzed_server_socket.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 class WaitTillHttpCloseDelegate : public net::HttpServer::Delegate { |
| 18 public: |
| 19 WaitTillHttpCloseDelegate(base::FuzzedDataProvider* data_provider, |
| 20 const base::Closure& done_closure) |
| 21 : server_(nullptr), |
| 22 data_provider_(data_provider), |
| 23 done_closure_(done_closure), |
| 24 action_flags_(data_provider_->ConsumeUint8()) {} |
| 25 |
| 26 void set_server(net::HttpServer* server) { server_ = server; } |
| 27 |
| 28 void OnConnect(int connection_id) override { |
| 29 if (!(action_flags_ & ACCEPT_CONNECTION)) |
| 30 server_->Close(connection_id); |
| 31 } |
| 32 |
| 33 void OnHttpRequest(int connection_id, |
| 34 const net::HttpServerRequestInfo& info) override { |
| 35 if (!(action_flags_ & ACCEPT_MESSAGE)) { |
| 36 server_->Close(connection_id); |
| 37 return; |
| 38 } |
| 39 |
| 40 if (action_flags_ & REPLY_TO_MESSAGE) { |
| 41 server_->Send200(connection_id, |
| 42 data_provider_->ConsumeRandomLengthString(64), |
| 43 "text/html"); |
| 44 } |
| 45 } |
| 46 |
| 47 void OnWebSocketRequest(int connection_id, |
| 48 const net::HttpServerRequestInfo& info) override { |
| 49 if (action_flags_ & CLOSE_WEBSOCKET_RATHER_THAN_ACCEPT) { |
| 50 server_->Close(connection_id); |
| 51 return; |
| 52 } |
| 53 |
| 54 if (action_flags_ & ACCEPT_WEBSOCKET) |
| 55 server_->AcceptWebSocket(connection_id, info); |
| 56 } |
| 57 |
| 58 void OnWebSocketMessage(int connection_id, const std::string& data) override { |
| 59 if (!(action_flags_ & ACCEPT_MESSAGE)) { |
| 60 server_->Close(connection_id); |
| 61 return; |
| 62 } |
| 63 |
| 64 if (action_flags_ & REPLY_TO_MESSAGE) { |
| 65 server_->SendOverWebSocket(connection_id, |
| 66 data_provider_->ConsumeRandomLengthString(64)); |
| 67 } |
| 68 } |
| 69 |
| 70 void OnClose(int connection_id) override { done_closure_.Run(); } |
| 71 |
| 72 private: |
| 73 enum { |
| 74 ACCEPT_CONNECTION = 1, |
| 75 ACCEPT_MESSAGE = 2, |
| 76 REPLY_TO_MESSAGE = 4, |
| 77 ACCEPT_WEBSOCKET = 8, |
| 78 CLOSE_WEBSOCKET_RATHER_THAN_ACCEPT = 16 |
| 79 }; |
| 80 |
| 81 net::HttpServer* server_; |
| 82 base::FuzzedDataProvider* const data_provider_; |
| 83 base::Closure done_closure_; |
| 84 const uint8_t action_flags_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(WaitTillHttpCloseDelegate); |
| 87 }; |
| 88 |
| 89 } // namespace |
| 90 |
| 91 // Fuzzer for HttpServer |
| 92 // |
| 93 // |data| is used to create a FuzzedServerSocket. |
| 94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 95 net::TestNetLog test_net_log; |
| 96 base::FuzzedDataProvider data_provider(data, size); |
| 97 |
| 98 std::unique_ptr<net::ServerSocket> server_socket( |
| 99 base::MakeUnique<net::FuzzedServerSocket>(&data_provider, &test_net_log)); |
| 100 CHECK_EQ(net::OK, |
| 101 server_socket->ListenWithAddressAndPort("127.0.0.1", 80, 5)); |
| 102 |
| 103 base::RunLoop run_loop; |
| 104 WaitTillHttpCloseDelegate delegate(&data_provider, run_loop.QuitClosure()); |
| 105 net::HttpServer server(std::move(server_socket), &delegate); |
| 106 delegate.set_server(&server); |
| 107 run_loop.Run(); |
| 108 return 0; |
| 109 } |
| OLD | NEW |