Chromium Code Reviews| 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"); | |
|
mmenke
2017/01/27 17:08:53
nit: Use braces when the body of an if takes up m
Maks Orlovich
2017/01/27 18:17:12
Done.
| |
| 44 } | |
| 45 | |
| 46 void OnWebSocketRequest(int connection_id, | |
| 47 const net::HttpServerRequestInfo& info) override { | |
| 48 // Randomize whether the delegate accepts web socket or not, to cover both | |
| 49 // options. | |
| 50 if (action_flags_ & ACCEPT_WEBSOCKET) | |
| 51 server_->AcceptWebSocket(connection_id, info); | |
|
mmenke
2017/01/27 17:08:53
Should we close the connection if we don't accept
morlovich
2017/01/27 17:30:29
Well, not doing either is basically the case this
mmenke
2017/01/27 17:34:59
Hrm...Good point. Seems like we're in an indeterm
mmenke
2017/01/27 17:40:13
Well, "indeterminant" might not be the word - we'd
| |
| 52 } | |
| 53 | |
| 54 void OnWebSocketMessage(int connection_id, const std::string& data) override { | |
| 55 if (!(action_flags_ & ACCEPT_MESSAGE)) { | |
| 56 server_->Close(connection_id); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 if (action_flags_ & REPLY_TO_MESSAGE) | |
| 61 server_->SendOverWebSocket(connection_id, | |
| 62 data_provider_->ConsumeRandomLengthString(64)); | |
|
mmenke
2017/01/27 17:08:53
nit: Braces
Maks Orlovich
2017/01/27 18:17:11
Done.
| |
| 63 } | |
| 64 | |
| 65 void OnClose(int connection_id) override { done_closure_.Run(); } | |
| 66 | |
| 67 private: | |
| 68 enum { | |
| 69 ACCEPT_CONNECTION = 1, | |
| 70 ACCEPT_MESSAGE = 2, | |
| 71 REPLY_TO_MESSAGE = 4, | |
| 72 ACCEPT_WEBSOCKET = 8 | |
| 73 }; | |
| 74 | |
| 75 net::HttpServer* server_; | |
| 76 base::FuzzedDataProvider* const data_provider_; | |
| 77 base::Closure done_closure_; | |
| 78 const uint8_t action_flags_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(WaitTillHttpCloseDelegate); | |
| 81 }; | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 // Fuzzer for HttpServer | |
| 86 // | |
| 87 // |data| is used to create a FuzzedServerSocket. | |
| 88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
| 89 net::TestNetLog test_net_log; | |
| 90 base::FuzzedDataProvider data_provider(data, size); | |
| 91 | |
| 92 std::unique_ptr<net::ServerSocket> server_socket( | |
| 93 base::MakeUnique<net::FuzzedServerSocket>(&data_provider, &test_net_log)); | |
| 94 CHECK_EQ(net::OK, | |
| 95 server_socket->ListenWithAddressAndPort("127.0.0.1", 80, 5)); | |
|
mmenke
2017/01/27 17:08:53
Should just use port 0. This both gets us on an e
morlovich
2017/01/27 17:30:29
This isn't actually listening, though (being a Fuz
mmenke
2017/01/27 17:34:59
Oh, right, it doesn't.
| |
| 96 | |
| 97 base::RunLoop run_loop; | |
| 98 WaitTillHttpCloseDelegate delegate(&data_provider, run_loop.QuitClosure()); | |
| 99 net::HttpServer server(std::move(server_socket), &delegate); | |
| 100 delegate.set_server(&server); | |
| 101 run_loop.Run(); | |
| 102 return 0; | |
| 103 } | |
| OLD | NEW |