onc_validator_fuzzer.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2021 The Chromium 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. #include <stddef.h>
  5. #include <stdint.h>
  6. #include "base/json/json_reader.h"
  7. #include "base/logging.h"
  8. #include "base/strings/string_piece.h"
  9. #include "base/values.h"
  10. #include "chromeos/components/onc/onc_signature.h"
  11. #include "chromeos/components/onc/onc_validator.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace chromeos {
  14. namespace onc {
  15. namespace {
  16. // Holds the state and performs initialization that's shared across fuzzer runs.
  17. struct Environment {
  18. Environment() {
  19. // Prevent spamming stdout with ONC validation errors.
  20. logging::SetMinLogLevel(logging::LOG_FATAL);
  21. }
  22. };
  23. } // namespace
  24. // Fuzzer for methods of the `Validator` class.
  25. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  26. static Environment env;
  27. absl::optional<base::Value> parsed_json = base::JSONReader::Read(
  28. base::StringPiece(reinterpret_cast<const char*>(data), size));
  29. if (!parsed_json || !parsed_json->is_dict())
  30. return 0;
  31. for (bool error_on_unknown_field : {false, true}) {
  32. for (bool error_on_wrong_recommended : {false, true}) {
  33. for (bool error_on_missing_field : {false, true}) {
  34. for (bool managed_onc : {false, true}) {
  35. Validator validator(
  36. error_on_unknown_field, error_on_wrong_recommended,
  37. error_on_missing_field, managed_onc, /*log_warnings=*/false);
  38. Validator::Result validation_result;
  39. validator.ValidateAndRepairObject(&kNetworkConfigurationSignature,
  40. *parsed_json, &validation_result);
  41. }
  42. }
  43. }
  44. }
  45. return 0;
  46. }
  47. } // namespace onc
  48. } // namespace chromeos