FSTFuzzTestFieldPath.mm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/FSTFuzzTestFieldPath.h"
  20. #import "Firestore/Source/Public/FIRFieldPath.h"
  21. namespace firebase {
  22. namespace firestore {
  23. namespace fuzzing {
  24. int FuzzTestFieldPath(const uint8_t *data, size_t size) {
  25. @autoreleasepool {
  26. // Convert the raw bytes to a string with UTF-8 format.
  27. NSData *d = [NSData dataWithBytes:data length:size];
  28. NSString *str = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
  29. // Fuzz test creating a FieldPath from an array with a single string.
  30. NSArray *str_arr1 = [NSArray arrayWithObjects:str, nil];
  31. @try {
  32. [[FIRFieldPath alloc] initWithFields:str_arr1];
  33. } @catch (...) {
  34. // Caught exceptions are ignored because they are not what we are after in
  35. // fuzz testing.
  36. }
  37. // Split the string into an array using " .,/-" as separators.
  38. NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" .,/_"];
  39. NSArray *str_arr2 = [str componentsSeparatedByCharactersInSet:set];
  40. @try {
  41. [[FIRFieldPath alloc] initWithFields:str_arr2];
  42. } @catch (...) {
  43. // Ignore caught exceptions.
  44. }
  45. // Try to parse the bytes as a string array and use it for initialization.
  46. // NSJSONReadingMutableContainers specifies that arrays and dictionaries are
  47. // created as mutable objects. Returns nil if there is a parsing error.
  48. NSArray *str_arr3 =
  49. [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingMutableContainers error:nil];
  50. @try {
  51. if (str_arr3) {
  52. [[FIRFieldPath alloc] initWithFields:str_arr3];
  53. }
  54. } @catch (...) {
  55. // Ignore caught exceptions.
  56. }
  57. }
  58. return 0;
  59. }
  60. } // namespace fuzzing
  61. } // namespace firestore
  62. } // namespace firebase