blob: d15f9a49046e8f0b70e1a6167cf8740eec3e4138 [file] [log] [blame]
Garrick Evanse1f11c32020-05-21 16:54:06 +09001// 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 <base/logging.h>
6#include <dbus/message.h>
7#include <fuzzer/FuzzedDataProvider.h>
8
9#include "patchpanel/client.h"
10
11namespace patchpanel {
12
13class Environment {
14 public:
15 Environment() {
16 logging::SetMinLogLevel(logging::LOG_FATAL); // <- DISABLE LOGGING.
17 }
18};
19
20class FakeObjectProxy : public dbus::ObjectProxy {
21 public:
22 explicit FakeObjectProxy(dbus::Bus* bus)
23 : dbus::ObjectProxy(bus, "svc", dbus::ObjectPath("/obj/path"), 0) {}
24
25 std::unique_ptr<dbus::Response> CallMethodAndBlockWithErrorDetails(
26 dbus::MethodCall* method_call,
27 int timeout_ms,
28 dbus::ScopedDBusError* error) override {
29 return nullptr;
30 }
31
32 std::unique_ptr<dbus::Response> CallMethodAndBlock(
33 dbus::MethodCall* method_call, int timeout_ms) override {
34 return nullptr;
35 }
36
37 void CallMethod(dbus::MethodCall* method_call,
38 int timeout_ms,
39 ResponseCallback callback) override {}
40
41 void CallMethodWithErrorResponse(dbus::MethodCall* method_call,
42 int timeout_ms,
43 ResponseOrErrorCallback callback) override {}
44
45 void CallMethodWithErrorCallback(dbus::MethodCall* method_call,
46 int timeout_ms,
47 ResponseCallback callback,
48 ErrorCallback error_callback) override {}
49
50 void ConnectToSignal(const std::string& interface_name,
51 const std::string& signal_name,
52 SignalCallback signal_callback,
53 OnConnectedCallback on_connected_callback) override {}
54
55 void WaitForServiceToBeAvailable(
56 WaitForServiceToBeAvailableCallback callback) override {}
57
58 void Detach() override {}
59};
60
61extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
62 // static Environment env;
63 dbus::Bus::Options options;
64 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
65 scoped_refptr<dbus::ObjectProxy> proxy(new FakeObjectProxy(bus.get()));
66 Client client(bus, proxy.get());
67 FuzzedDataProvider provider(data, size);
68
69 while (provider.remaining_bytes() > 0) {
70 client.NotifyArcStartup(provider.ConsumeIntegral<pid_t>());
71 client.NotifyArcVmStartup(provider.ConsumeIntegral<uint32_t>());
72 client.NotifyArcVmShutdown(provider.ConsumeIntegral<uint32_t>());
73 NetworkDevice device;
74 device.set_ifname(provider.ConsumeRandomLengthString(100));
75 device.set_ipv4_addr(provider.ConsumeIntegral<uint32_t>());
76 device.mutable_ipv4_subnet()->set_base_addr(
77 provider.ConsumeIntegral<uint32_t>());
78 device.mutable_ipv4_subnet()->set_prefix_len(
79 provider.ConsumeIntegral<uint32_t>());
80 IPv4Subnet subnet;
81 subnet.set_base_addr(provider.ConsumeIntegral<uint32_t>());
82 subnet.set_prefix_len(provider.ConsumeIntegral<uint32_t>());
83 client.NotifyTerminaVmStartup(provider.ConsumeIntegral<uint32_t>(), &device,
84 &subnet);
85 client.NotifyTerminaVmShutdown(provider.ConsumeIntegral<uint32_t>());
86 client.NotifyPluginVmStartup(provider.ConsumeIntegral<uint64_t>(),
87 provider.ConsumeIntegral<int>(), &device);
88 client.NotifyPluginVmShutdown(provider.ConsumeIntegral<uint64_t>());
89 // TODO(garrick): Enable the following once the memory leaks in Chrome OS
90 // DBus are resolved.
91 // client.DefaultVpnRouting(provider.ConsumeIntegral<int>());
92 // client.RouteOnVpn(provider.ConsumeIntegral<int>());
93 // client.BypassVpn(provider.ConsumeIntegral<int>());
94 client.ConnectNamespace(provider.ConsumeIntegral<pid_t>(),
95 provider.ConsumeRandomLengthString(100),
96 provider.ConsumeBool());
97 }
98 bus->ShutdownAndBlock();
99 return 0;
100}
101
102} // namespace patchpanel