serializer_fuzzer.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <cstddef>
  17. #include <cstdint>
  18. #include "Firestore/Protos/nanopb/google/firestore/v1/document.nanopb.h"
  19. #include "Firestore/core/src/model/database_id.h"
  20. #include "Firestore/core/src/nanopb/message.h"
  21. #include "Firestore/core/src/nanopb/reader.h"
  22. #include "Firestore/core/src/remote/serializer.h"
  23. #include "Firestore/core/src/util/read_context.h"
  24. using firebase::firestore::google_firestore_v1_Value;
  25. using firebase::firestore::model::DatabaseId;
  26. using firebase::firestore::nanopb::Message;
  27. using firebase::firestore::nanopb::StringReader;
  28. using firebase::firestore::remote::Serializer;
  29. using firebase::firestore::util::ReadContext;
  30. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  31. Serializer serializer{DatabaseId{"project", DatabaseId::kDefault}};
  32. try {
  33. // Try to decode the received data using the serializer.
  34. StringReader reader{data, size};
  35. auto message = Message<google_firestore_v1_Value>::TryParse(&reader);
  36. ReadContext context;
  37. serializer.DecodeFieldValue(&context, *message);
  38. } catch (...) {
  39. // Ignore caught errors and assertions because fuzz testing is looking for
  40. // crashes and memory errors.
  41. }
  42. return 0;
  43. }