FSTFuzzTestFieldPath.mm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/API/FIRFieldPath+Internal.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. // Create a FieldPath object from a string.
  30. @try {
  31. [FIRFieldPath pathWithDotSeparatedString:str];
  32. } @catch (...) {
  33. // Ignore caught exceptions.
  34. }
  35. // Fuzz test creating a FieldPath from an array with a single string.
  36. NSArray *str_arr1 = [NSArray arrayWithObjects:str, nil];
  37. @try {
  38. [[FIRFieldPath alloc] initWithFields:str_arr1];
  39. } @catch (...) {
  40. // Caught exceptions are ignored because they are not what we are after in
  41. // fuzz testing.
  42. }
  43. // Split the string into an array using " .,/-" as separators.
  44. NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" .,/_"];
  45. NSArray *str_arr2 = [str componentsSeparatedByCharactersInSet:set];
  46. @try {
  47. [[FIRFieldPath alloc] initWithFields:str_arr2];
  48. } @catch (...) {
  49. // Ignore caught exceptions.
  50. }
  51. // Try to parse the bytes as a string array and use it for initialization.
  52. // NSJSONReadingMutableContainers specifies that arrays and dictionaries are
  53. // created as mutable objects. Returns nil if there is a parsing error.
  54. NSArray *str_arr3 =
  55. [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingMutableContainers error:nil];
  56. @try {
  57. if (str_arr3) {
  58. [[FIRFieldPath alloc] initWithFields:str_arr3];
  59. }
  60. } @catch (...) {
  61. // Ignore caught exceptions.
  62. }
  63. }
  64. return 0;
  65. }
  66. } // namespace fuzzing
  67. } // namespace firestore
  68. } // namespace firebase