FSTFuzzTestSerializer.mm 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #import <Foundation/Foundation.h>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #import "Firestore/Example/FuzzTests/FuzzingTargets/FSTFuzzTestSerializer.h"
  20. #include "Firestore/Protos/nanopb/google/firestore/v1/document.nanopb.h"
  21. #include "Firestore/core/src/model/database_id.h"
  22. #include "Firestore/core/src/nanopb/message.h"
  23. #include "Firestore/core/src/nanopb/reader.h"
  24. #include "Firestore/core/src/remote/serializer.h"
  25. namespace firebase {
  26. namespace firestore {
  27. namespace fuzzing {
  28. using firebase::firestore::model::DatabaseId;
  29. using firebase::firestore::nanopb::Message;
  30. using firebase::firestore::nanopb::StringReader;
  31. using firebase::firestore::remote::Serializer;
  32. int FuzzTestDeserialization(const uint8_t *data, size_t size) {
  33. Serializer serializer{DatabaseId{"project"}};
  34. @autoreleasepool {
  35. @try {
  36. StringReader reader{data, size};
  37. auto message = Message<google_firestore_v1_Value>::TryParse(&reader);
  38. serializer.DecodeFieldValue(&reader, *message);
  39. } @catch (...) {
  40. // Caught exceptions are ignored because the input might be malformed and
  41. // the deserialization might throw an error as intended. Fuzzing focuses on
  42. // runtime errors that are detected by the sanitizers.
  43. }
  44. }
  45. return 0;
  46. }
  47. } // namespace fuzzing
  48. } // namespace firestore
  49. } // namespace firebase