blob: 838710cdca11f10156bb8f25131132e8fc40820c [file] [log] [blame]
Andreea Costinasc65f99b2020-04-27 12:54:14 +02001// 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>
Andreea Costinasc65f99b2020-04-27 12:54:14 +020014#include <base/run_loop.h>
Qijiang Fan34014672020-07-20 16:05:38 +090015#include <base/task/single_thread_task_executor.h>
Andreea Costinasc65f99b2020-04-27 12:54:14 +020016#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
22namespace {
Andreea Costinaseea01f92020-07-01 13:29:22 +020023void ResolveProxyCallback(
24 base::OnceClosure quit_task,
Andreea Costinasc65f99b2020-04-27 12:54:14 +020025 const std::string&,
Andreea Costinaseea01f92020-07-01 13:29:22 +020026 base::OnceCallback<void(const std::list<std::string>&)>
27 on_proxy_resolution_callback) {
28 // Exit the fuzzer if the input fed to the test is a valid HTTP CONNECT
29 // request.
30 std::move(quit_task).Run();
31}
32void NullAuthenticationRequiredCallback(
33 const std::string& proxy_url,
34 const std::string& scheme,
35 const std::string& realm,
Andreea Costinasb49162b2020-08-26 15:30:41 +020036 const std::string& bad_cached_credentials,
Andreea Costinased9e6122020-08-12 12:06:19 +020037 base::RepeatingCallback<void(const std::string& credentials)>
Andreea Costinaseea01f92020-07-01 13:29:22 +020038 on_auth_acquired_callback) {}
Andreea Costinasc65f99b2020-04-27 12:54:14 +020039
40void OnConnectionSetupFinished(base::OnceClosure quit_task,
41 std::unique_ptr<patchpanel::SocketForwarder>,
42 system_proxy::ProxyConnectJob*) {
43 std::move(quit_task).Run();
44}
45} // namespace
46
47struct Environment {
48 Environment() {
Andreea Costinaseea01f92020-07-01 13:29:22 +020049 logging::SetMinLogLevel(logging::LOG_FATAL); // Disable logging.
Andreea Costinasc65f99b2020-04-27 12:54:14 +020050 }
51};
52
53extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
54 static Environment env;
55
56 // Mock main task runner
Qijiang Fan34014672020-07-20 16:05:38 +090057 base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO);
58 brillo::BaseMessageLoop brillo_loop(task_executor.task_runner());
Andreea Costinasc65f99b2020-04-27 12:54:14 +020059 brillo_loop.SetAsCurrent();
60
61 base::RunLoop run_loop;
62
63 int socket_pair[2];
64 socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, socket_pair);
65 base::ScopedFD reader_fd(socket_pair[0]);
66 base::ScopedFD writer_fd(socket_pair[1]);
67 int fds[2];
68
69 socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
70 0 /* protocol */, fds);
71 patchpanel::Socket cros_client_socket((base::ScopedFD(fds[1])));
72
73 auto connect_job = std::make_unique<system_proxy::ProxyConnectJob>(
74 std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[0])), "",
Andreea Costinaseea01f92020-07-01 13:29:22 +020075 base::BindOnce(&ResolveProxyCallback, run_loop.QuitClosure()),
Andreea Costinased9e6122020-08-12 12:06:19 +020076 base::BindRepeating(&NullAuthenticationRequiredCallback),
Andreea Costinasc65f99b2020-04-27 12:54:14 +020077 base::BindOnce(&OnConnectionSetupFinished, run_loop.QuitClosure()));
78 connect_job->Start();
79 cros_client_socket.SendTo(data, size);
80
81 run_loop.Run();
82 return 0;
83}