1*1a96fba6SXin Li // Copyright 2019 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li
5*1a96fba6SXin Li #include <cstddef>
6*1a96fba6SXin Li #include <cstdint>
7*1a96fba6SXin Li #include <cstdio>
8*1a96fba6SXin Li
9*1a96fba6SXin Li #include <brillo/data_encoding.h>
10*1a96fba6SXin Li
11*1a96fba6SXin Li #include <base/logging.h>
12*1a96fba6SXin Li #include <fuzzer/FuzzedDataProvider.h>
13*1a96fba6SXin Li
14*1a96fba6SXin Li namespace {
15*1a96fba6SXin Li constexpr int kMaxStringLength = 256;
16*1a96fba6SXin Li constexpr int kMaxParamsSize = 8;
17*1a96fba6SXin Li
FuzzUrlEncodeDecode(FuzzedDataProvider * provider)18*1a96fba6SXin Li void FuzzUrlEncodeDecode(FuzzedDataProvider* provider) {
19*1a96fba6SXin Li brillo::data_encoding::UrlEncode(
20*1a96fba6SXin Li provider->ConsumeRandomLengthString(kMaxStringLength).c_str(),
21*1a96fba6SXin Li provider->ConsumeBool());
22*1a96fba6SXin Li
23*1a96fba6SXin Li brillo::data_encoding::UrlDecode(
24*1a96fba6SXin Li provider->ConsumeRandomLengthString(kMaxStringLength).c_str());
25*1a96fba6SXin Li }
26*1a96fba6SXin Li
FuzzWebParamsEncodeDecode(FuzzedDataProvider * provider)27*1a96fba6SXin Li void FuzzWebParamsEncodeDecode(FuzzedDataProvider* provider) {
28*1a96fba6SXin Li brillo::data_encoding::WebParamList param_list;
29*1a96fba6SXin Li const auto num_params = provider->ConsumeIntegralInRange(0, kMaxParamsSize);
30*1a96fba6SXin Li for (auto i = 0; i < num_params; i++) {
31*1a96fba6SXin Li param_list.push_back(std::pair<std::string, std::string>(
32*1a96fba6SXin Li provider->ConsumeRandomLengthString(kMaxStringLength),
33*1a96fba6SXin Li provider->ConsumeRandomLengthString(kMaxStringLength)));
34*1a96fba6SXin Li }
35*1a96fba6SXin Li brillo::data_encoding::WebParamsEncode(param_list, provider->ConsumeBool());
36*1a96fba6SXin Li
37*1a96fba6SXin Li brillo::data_encoding::WebParamsDecode(
38*1a96fba6SXin Li provider->ConsumeRandomLengthString(kMaxStringLength));
39*1a96fba6SXin Li }
40*1a96fba6SXin Li
FuzzBase64EncodeDecode(FuzzedDataProvider * provider)41*1a96fba6SXin Li void FuzzBase64EncodeDecode(FuzzedDataProvider* provider) {
42*1a96fba6SXin Li brillo::data_encoding::Base64Encode(
43*1a96fba6SXin Li provider->ConsumeRandomLengthString(kMaxStringLength));
44*1a96fba6SXin Li brillo::Blob output;
45*1a96fba6SXin Li brillo::data_encoding::Base64Decode(
46*1a96fba6SXin Li provider->ConsumeRandomLengthString(kMaxStringLength), &output);
47*1a96fba6SXin Li }
48*1a96fba6SXin Li
IgnoreLogging(int,const char *,int,size_t,const std::string &)49*1a96fba6SXin Li bool IgnoreLogging(int, const char*, int, size_t, const std::string&) {
50*1a96fba6SXin Li return true;
51*1a96fba6SXin Li }
52*1a96fba6SXin Li
53*1a96fba6SXin Li } // namespace
54*1a96fba6SXin Li
55*1a96fba6SXin Li class Environment {
56*1a96fba6SXin Li public:
Environment()57*1a96fba6SXin Li Environment() {
58*1a96fba6SXin Li // Disable logging. Normally this would be done with logging::SetMinLogLevel
59*1a96fba6SXin Li // but that doesn't work for brillo::Error because it's not using the
60*1a96fba6SXin Li // LOG(ERROR) macro which is where the actual log level check occurs.
61*1a96fba6SXin Li logging::SetLogMessageHandler(&IgnoreLogging);
62*1a96fba6SXin Li }
63*1a96fba6SXin Li };
64*1a96fba6SXin Li
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65*1a96fba6SXin Li extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
66*1a96fba6SXin Li static Environment env;
67*1a96fba6SXin Li FuzzedDataProvider data_provider(data, size);
68*1a96fba6SXin Li FuzzUrlEncodeDecode(&data_provider);
69*1a96fba6SXin Li FuzzWebParamsEncodeDecode(&data_provider);
70*1a96fba6SXin Li FuzzBase64EncodeDecode(&data_provider);
71*1a96fba6SXin Li return 0;
72*1a96fba6SXin Li }
73