Andreea Costinas | c65f99b | 2020-04-27 12:54:14 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS 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 <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include <sys/socket.h> |
| 9 | #include <sys/types.h> |
| 10 | |
| 11 | #include <base/bind.h> |
| 12 | #include <base/files/file_util.h> |
| 13 | #include <base/logging.h> |
| 14 | #include <base/message_loop/message_loop.h> |
| 15 | #include <base/run_loop.h> |
| 16 | #include <brillo/message_loops/base_message_loop.h> |
| 17 | #include <chromeos/patchpanel/socket.h> |
| 18 | #include <chromeos/patchpanel/socket_forwarder.h> |
| 19 | |
| 20 | #include "system-proxy/proxy_connect_job.h" |
| 21 | |
| 22 | namespace { |
| 23 | void NullProxyResolver( |
| 24 | const std::string&, |
| 25 | base::OnceCallback<void(const std::list<std::string>&)>) {} |
| 26 | |
| 27 | void OnConnectionSetupFinished(base::OnceClosure quit_task, |
| 28 | std::unique_ptr<patchpanel::SocketForwarder>, |
| 29 | system_proxy::ProxyConnectJob*) { |
| 30 | std::move(quit_task).Run(); |
| 31 | } |
| 32 | } // namespace |
| 33 | |
| 34 | struct Environment { |
| 35 | Environment() { |
| 36 | logging::SetMinLogLevel(logging::LOG_INFO); // Disable logging. |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 41 | static Environment env; |
| 42 | |
| 43 | // Mock main task runner |
| 44 | base::MessageLoopForIO message_loop; |
| 45 | brillo::BaseMessageLoop brillo_loop(&message_loop); |
| 46 | brillo_loop.SetAsCurrent(); |
| 47 | |
| 48 | base::RunLoop run_loop; |
| 49 | |
| 50 | int socket_pair[2]; |
| 51 | socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, socket_pair); |
| 52 | base::ScopedFD reader_fd(socket_pair[0]); |
| 53 | base::ScopedFD writer_fd(socket_pair[1]); |
| 54 | int fds[2]; |
| 55 | |
| 56 | socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, |
| 57 | 0 /* protocol */, fds); |
| 58 | patchpanel::Socket cros_client_socket((base::ScopedFD(fds[1]))); |
| 59 | |
| 60 | auto connect_job = std::make_unique<system_proxy::ProxyConnectJob>( |
| 61 | std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[0])), "", |
| 62 | base::BindOnce(&NullProxyResolver), |
| 63 | base::BindOnce(&OnConnectionSetupFinished, run_loop.QuitClosure())); |
| 64 | connect_job->Start(); |
| 65 | cros_client_socket.SendTo(data, size); |
| 66 | |
| 67 | run_loop.Run(); |
| 68 | return 0; |
| 69 | } |