aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/data_encoding_fuzzer.cc
blob: 8d5d41ef4ee5fc3bd2e3c9e18f451e7f9902ad9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cstddef>
#include <cstdint>
#include <cstdio>

#include <brillo/data_encoding.h>

#include <base/logging.h>
#include <fuzzer/FuzzedDataProvider.h>

namespace {
constexpr int kMaxStringLength = 256;
constexpr int kMaxParamsSize = 8;

void FuzzUrlEncodeDecode(FuzzedDataProvider* provider) {
  brillo::data_encoding::UrlEncode(
      provider->ConsumeRandomLengthString(kMaxStringLength).c_str(),
      provider->ConsumeBool());

  brillo::data_encoding::UrlDecode(
      provider->ConsumeRandomLengthString(kMaxStringLength).c_str());
}

void FuzzWebParamsEncodeDecode(FuzzedDataProvider* provider) {
  brillo::data_encoding::WebParamList param_list;
  const auto num_params = provider->ConsumeIntegralInRange(0, kMaxParamsSize);
  for (auto i = 0; i < num_params; i++) {
    param_list.push_back(std::pair<std::string, std::string>(
        provider->ConsumeRandomLengthString(kMaxStringLength),
        provider->ConsumeRandomLengthString(kMaxStringLength)));
  }
  brillo::data_encoding::WebParamsEncode(param_list, provider->ConsumeBool());

  brillo::data_encoding::WebParamsDecode(
      provider->ConsumeRandomLengthString(kMaxStringLength));
}

void FuzzBase64EncodeDecode(FuzzedDataProvider* provider) {
  brillo::data_encoding::Base64Encode(
      provider->ConsumeRandomLengthString(kMaxStringLength));
  brillo::Blob output;
  brillo::data_encoding::Base64Decode(
      provider->ConsumeRandomLengthString(kMaxStringLength), &output);
}

bool IgnoreLogging(int, const char*, int, size_t, const std::string&) {
  return true;
}

}  // namespace

class Environment {
 public:
  Environment() {
    // Disable logging. Normally this would be done with logging::SetMinLogLevel
    // but that doesn't work for brillo::Error because it's not using the
    // LOG(ERROR) macro which is where the actual log level check occurs.
    logging::SetLogMessageHandler(&IgnoreLogging);
  }
};

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  static Environment env;
  FuzzedDataProvider data_provider(data, size);
  FuzzUrlEncodeDecode(&data_provider);
  FuzzWebParamsEncodeDecode(&data_provider);
  FuzzBase64EncodeDecode(&data_provider);
  return 0;
}