FSTFuzzTestSerializer.mm 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/core/src/firebase/firestore/model/database_id.h"
  21. #include "Firestore/core/src/firebase/firestore/nanopb/reader.h"
  22. #include "Firestore/core/src/firebase/firestore/remote/serializer.h"
  23. namespace firebase {
  24. namespace firestore {
  25. namespace fuzzing {
  26. using firebase::firestore::model::DatabaseId;
  27. using firebase::firestore::nanopb::Reader;
  28. using firebase::firestore::remote::Serializer;
  29. int FuzzTestDeserialization(const uint8_t *data, size_t size) {
  30. Serializer serializer{DatabaseId{"project", DatabaseId::kDefault}};
  31. @autoreleasepool {
  32. @try {
  33. Reader reader = Reader::Wrap(data, size);
  34. google_firestore_v1_Value nanopb_proto{};
  35. reader.ReadNanopbMessage(google_firestore_v1_Value_fields, &nanopb_proto);
  36. serializer.DecodeFieldValue(&reader, nanopb_proto);
  37. } @catch (...) {
  38. // Caught exceptions are ignored because the input might be malformed and
  39. // the deserialization might throw an error as intended. Fuzzing focuses on
  40. // runtime errors that are detected by the sanitizers.
  41. }
  42. }
  43. return 0;
  44. }
  45. } // namespace fuzzing
  46. } // namespace firestore
  47. } // namespace firebase