// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Fuzzer for dial_internal_message_util.cc.

#include "base/json/json_reader.h"
#include "base/values.h"
#include "chrome/browser/media/router/providers/dial/dial_internal_message_util.h"

namespace media_router {

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  // Limit input size to prevent out-of-memory failures like the one seen in
  // crbug.com/40628111.
  if (size > 16 * 1024) {
    return 0;
  }

  std::optional<base::Value> input = base::JSONReader::Read(
      std::string(reinterpret_cast<const char*>(data), size),
      base::JSON_PARSE_CHROMIUM_EXTENSIONS);
  if (!input || !input.value().is_dict()) {
    return 0;
  }

  std::string error_unused;
  auto dial_internal_message = DialInternalMessage::From(
      std::move(input.value().GetDict()), &error_unused);
  if (!dial_internal_message) {
    return 0;
  }

  DialInternalMessageUtil::IsStopSessionMessage(*dial_internal_message);

  auto custom_dial_launch_message_body_unused =
      CustomDialLaunchMessageBody::From(*dial_internal_message);
  return 0;
}

}  // namespace media_router
