serializer_fuzzer.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. using firebase::firestore::google_firestore_v1_Value;
  24. using firebase::firestore::model::DatabaseId;
  25. using firebase::firestore::nanopb::Message;
  26. using firebase::firestore::nanopb::StringReader;
  27. using firebase::firestore::remote::Serializer;
  28. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  29. Serializer serializer{DatabaseId{"project", DatabaseId::kDefault}};
  30. try {
  31. // Try to decode the received data using the serializer.
  32. StringReader reader{data, size};
  33. auto message = Message<google_firestore_v1_Value>::TryParse(&reader);
  34. serializer.DecodeFieldValue(&reader, *message);
  35. } catch (...) {
  36. // Ignore caught errors and assertions because fuzz testing is looking for
  37. // crashes and memory errors.
  38. }
  39. return 0;
  40. }