blob: 287a2ccdadeaf7ab832f0b8aa88a2faf6c8e1439 [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>
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
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,
36 base::OnceCallback<void(const std::string& credentials)>
37 on_auth_acquired_callback) {}
Andreea Costinasc65f99b2020-04-27 12:54:14 +020038
39void OnConnectionSetupFinished(base::OnceClosure quit_task,
40 std::unique_ptr<patchpanel::SocketForwarder>,
41 system_proxy::ProxyConnectJob*) {
42 std::move(quit_task).Run();
43}
44} // namespace
45
46struct Environment {
47 Environment() {
Andreea Costinaseea01f92020-07-01 13:29:22 +020048 logging::SetMinLogLevel(logging::LOG_FATAL); // Disable logging.
Andreea Costinasc65f99b2020-04-27 12:54:14 +020049 }
50};
51
52extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
53 static Environment env;
54
55 // Mock main task runner
56 base::MessageLoopForIO message_loop;
57 brillo::BaseMessageLoop brillo_loop(&message_loop);
58 brillo_loop.SetAsCurrent();
59
60 base::RunLoop run_loop;
61
62 int socket_pair[2];
63 socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, socket_pair);
64 base::ScopedFD reader_fd(socket_pair[0]);
65 base::ScopedFD writer_fd(socket_pair[1]);
66 int fds[2];
67
68 socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
69 0 /* protocol */, fds);
70 patchpanel::Socket cros_client_socket((base::ScopedFD(fds[1])));
71
72 auto connect_job = std::make_unique<system_proxy::ProxyConnectJob>(
73 std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[0])), "",
Andreea Costinaseea01f92020-07-01 13:29:22 +020074 base::BindOnce(&ResolveProxyCallback, run_loop.QuitClosure()),
75 base::BindOnce(&NullAuthenticationRequiredCallback),
Andreea Costinasc65f99b2020-04-27 12:54:14 +020076 base::BindOnce(&OnConnectionSetupFinished, run_loop.QuitClosure()));
77 connect_job->Start();
78 cros_client_socket.SendTo(data, size);
79
80 run_loop.Run();
81 return 0;
82}