2018-07-27 15:25:13 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright 2018 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
|
|
#import "Firestore/Example/FuzzTests/FuzzingTargets/FSTFuzzTestFieldPath.h"
|
|
|
|
|
|
2018-08-13 14:02:38 -04:00
|
|
|
#import "Firestore/Source/API/FIRFieldPath+Internal.h"
|
2018-07-27 15:25:13 -04:00
|
|
|
|
|
|
|
|
namespace firebase {
|
|
|
|
|
namespace firestore {
|
|
|
|
|
namespace fuzzing {
|
|
|
|
|
|
|
|
|
|
int FuzzTestFieldPath(const uint8_t *data, size_t size) {
|
|
|
|
|
@autoreleasepool {
|
|
|
|
|
// Convert the raw bytes to a string with UTF-8 format.
|
|
|
|
|
NSData *d = [NSData dataWithBytes:data length:size];
|
|
|
|
|
NSString *str = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
|
2018-10-30 00:15:22 -04:00
|
|
|
if (!str) {
|
|
|
|
|
// TODO(varconst): this happens when `NSData` doesn't happen to contain valid UTF-8, perhaps
|
|
|
|
|
// find a way to still convert it to a string.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-07-27 15:25:13 -04:00
|
|
|
|
2018-08-13 14:02:38 -04:00
|
|
|
// Create a FieldPath object from a string.
|
|
|
|
|
@try {
|
|
|
|
|
[FIRFieldPath pathWithDotSeparatedString:str];
|
|
|
|
|
} @catch (...) {
|
|
|
|
|
// Ignore caught exceptions.
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 15:25:13 -04:00
|
|
|
// Fuzz test creating a FieldPath from an array with a single string.
|
|
|
|
|
NSArray *str_arr1 = [NSArray arrayWithObjects:str, nil];
|
|
|
|
|
@try {
|
2018-10-30 00:15:22 -04:00
|
|
|
(void)[[FIRFieldPath alloc] initWithFields:str_arr1];
|
2018-07-27 15:25:13 -04:00
|
|
|
} @catch (...) {
|
|
|
|
|
// Caught exceptions are ignored because they are not what we are after in
|
|
|
|
|
// fuzz testing.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Split the string into an array using " .,/-" as separators.
|
|
|
|
|
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" .,/_"];
|
|
|
|
|
NSArray *str_arr2 = [str componentsSeparatedByCharactersInSet:set];
|
|
|
|
|
@try {
|
2018-10-30 00:15:22 -04:00
|
|
|
(void)[[FIRFieldPath alloc] initWithFields:str_arr2];
|
2018-07-27 15:25:13 -04:00
|
|
|
} @catch (...) {
|
|
|
|
|
// Ignore caught exceptions.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to parse the bytes as a string array and use it for initialization.
|
|
|
|
|
// NSJSONReadingMutableContainers specifies that arrays and dictionaries are
|
|
|
|
|
// created as mutable objects. Returns nil if there is a parsing error.
|
2019-01-02 09:27:31 -08:00
|
|
|
NSArray *str_arr3 = [NSJSONSerialization JSONObjectWithData:d
|
|
|
|
|
options:NSJSONReadingMutableContainers
|
|
|
|
|
error:nil];
|
2018-10-30 00:15:22 -04:00
|
|
|
NSMutableArray *mutable_array = [[NSMutableArray alloc] initWithArray:str_arr3];
|
|
|
|
|
if (str_arr3) {
|
|
|
|
|
for (int i = 0; i < str_arr3.count; ++i) {
|
|
|
|
|
NSObject *value = str_arr3[i];
|
|
|
|
|
// `FIRFieldPath initWithFields:` relies on all members having `length` attribute.
|
|
|
|
|
if (![value isKindOfClass:[NSString class]]) {
|
|
|
|
|
if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
|
mutable_array[i] = [[NSString alloc] initWithFormat:@"%@", (NSNumber *)value];
|
|
|
|
|
} else {
|
|
|
|
|
// TODO(varconst): convert to string recursively.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 15:25:13 -04:00
|
|
|
@try {
|
2018-10-30 00:15:22 -04:00
|
|
|
if (mutable_array) {
|
|
|
|
|
(void)[[FIRFieldPath alloc] initWithFields:mutable_array];
|
2018-07-27 15:25:13 -04:00
|
|
|
}
|
|
|
|
|
} @catch (...) {
|
|
|
|
|
// Ignore caught exceptions.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace fuzzing
|
|
|
|
|
} // namespace firestore
|
|
|
|
|
} // namespace firebase
|