blob: e8bf56e15df93e9c3af7bfe1cb640a50c460f112 [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
Andreea Costinascc4d54e2020-10-19 15:46:25 +020011#include <curl/curl.h>
12
Andreea Costinasc65f99b2020-04-27 12:54:14 +020013#include <base/bind.h>
14#include <base/files/file_util.h>
15#include <base/logging.h>
Andreea Costinasc65f99b2020-04-27 12:54:14 +020016#include <base/run_loop.h>
Qijiang Fan34014672020-07-20 16:05:38 +090017#include <base/task/single_thread_task_executor.h>
Andreea Costinasc65f99b2020-04-27 12:54:14 +020018#include <brillo/message_loops/base_message_loop.h>
19#include <chromeos/patchpanel/socket.h>
20#include <chromeos/patchpanel/socket_forwarder.h>
21
22#include "system-proxy/proxy_connect_job.h"
23
24namespace {
Andreea Costinaseea01f92020-07-01 13:29:22 +020025void ResolveProxyCallback(
26 base::OnceClosure quit_task,
Andreea Costinasc65f99b2020-04-27 12:54:14 +020027 const std::string&,
Andreea Costinaseea01f92020-07-01 13:29:22 +020028 base::OnceCallback<void(const std::list<std::string>&)>
29 on_proxy_resolution_callback) {
30 // Exit the fuzzer if the input fed to the test is a valid HTTP CONNECT
31 // request.
32 std::move(quit_task).Run();
33}
34void NullAuthenticationRequiredCallback(
35 const std::string& proxy_url,
36 const std::string& scheme,
37 const std::string& realm,
Andreea Costinasb49162b2020-08-26 15:30:41 +020038 const std::string& bad_cached_credentials,
Andreea Costinased9e6122020-08-12 12:06:19 +020039 base::RepeatingCallback<void(const std::string& credentials)>
Andreea Costinaseea01f92020-07-01 13:29:22 +020040 on_auth_acquired_callback) {}
Andreea Costinasc65f99b2020-04-27 12:54:14 +020041
42void OnConnectionSetupFinished(base::OnceClosure quit_task,
43 std::unique_ptr<patchpanel::SocketForwarder>,
44 system_proxy::ProxyConnectJob*) {
45 std::move(quit_task).Run();
46}
47} // namespace
48
49struct Environment {
50 Environment() {
Andreea Costinaseea01f92020-07-01 13:29:22 +020051 logging::SetMinLogLevel(logging::LOG_FATAL); // Disable logging.
Andreea Costinasc65f99b2020-04-27 12:54:14 +020052 }
53};
54
55extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
56 static Environment env;
57
58 // Mock main task runner
Qijiang Fan34014672020-07-20 16:05:38 +090059 base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO);
60 brillo::BaseMessageLoop brillo_loop(task_executor.task_runner());
Andreea Costinasc65f99b2020-04-27 12:54:14 +020061 brillo_loop.SetAsCurrent();
62
63 base::RunLoop run_loop;
64
65 int socket_pair[2];
66 socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, socket_pair);
67 base::ScopedFD reader_fd(socket_pair[0]);
68 base::ScopedFD writer_fd(socket_pair[1]);
69 int fds[2];
70
71 socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
72 0 /* protocol */, fds);
73 patchpanel::Socket cros_client_socket((base::ScopedFD(fds[1])));
74
75 auto connect_job = std::make_unique<system_proxy::ProxyConnectJob>(
76 std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[0])), "",
Andreea Costinascc4d54e2020-10-19 15:46:25 +020077 CURLAUTH_ANY,
Andreea Costinaseea01f92020-07-01 13:29:22 +020078 base::BindOnce(&ResolveProxyCallback, run_loop.QuitClosure()),
Andreea Costinased9e6122020-08-12 12:06:19 +020079 base::BindRepeating(&NullAuthenticationRequiredCallback),
Andreea Costinasc65f99b2020-04-27 12:54:14 +020080 base::BindOnce(&OnConnectionSetupFinished, run_loop.QuitClosure()));
81 connect_job->Start();
82 cros_client_socket.SendTo(data, size);
83
84 run_loop.Run();
85 return 0;
86}