FSTFuzzTestFieldPath.mm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. if (!str) {
  30. // TODO(varconst): this happens when `NSData` doesn't happen to contain valid UTF-8, perhaps
  31. // find a way to still convert it to a string.
  32. return 0;
  33. }
  34. // Create a FieldPath object from a string.
  35. @try {
  36. [FIRFieldPath pathWithDotSeparatedString:str];
  37. } @catch (...) {
  38. // Ignore caught exceptions.
  39. }
  40. // Fuzz test creating a FieldPath from an array with a single string.
  41. NSArray *str_arr1 = [NSArray arrayWithObjects:str, nil];
  42. @try {
  43. (void)[[FIRFieldPath alloc] initWithFields:str_arr1];
  44. } @catch (...) {
  45. // Caught exceptions are ignored because they are not what we are after in
  46. // fuzz testing.
  47. }
  48. // Split the string into an array using " .,/-" as separators.
  49. NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" .,/_"];
  50. NSArray *str_arr2 = [str componentsSeparatedByCharactersInSet:set];
  51. @try {
  52. (void)[[FIRFieldPath alloc] initWithFields:str_arr2];
  53. } @catch (...) {
  54. // Ignore caught exceptions.
  55. }
  56. // Try to parse the bytes as a string array and use it for initialization.
  57. // NSJSONReadingMutableContainers specifies that arrays and dictionaries are
  58. // created as mutable objects. Returns nil if there is a parsing error.
  59. NSArray *str_arr3 = [NSJSONSerialization JSONObjectWithData:d
  60. options:NSJSONReadingMutableContainers
  61. error:nil];
  62. NSMutableArray *mutable_array = [[NSMutableArray alloc] initWithArray:str_arr3];
  63. if (str_arr3) {
  64. for (int i = 0; i < str_arr3.count; ++i) {
  65. NSObject *value = str_arr3[i];
  66. // `FIRFieldPath initWithFields:` relies on all members having `length` attribute.
  67. if (![value isKindOfClass:[NSString class]]) {
  68. if ([value isKindOfClass:[NSNumber class]]) {
  69. mutable_array[i] = [[NSString alloc] initWithFormat:@"%@", (NSNumber *)value];
  70. } else {
  71. // TODO(varconst): convert to string recursively.
  72. return 0;
  73. }
  74. }
  75. }
  76. }
  77. @try {
  78. if (mutable_array) {
  79. (void)[[FIRFieldPath alloc] initWithFields:mutable_array];
  80. }
  81. } @catch (...) {
  82. // Ignore caught exceptions.
  83. }
  84. }
  85. return 0;
  86. }
  87. } // namespace fuzzing
  88. } // namespace firestore
  89. } // namespace firebase