diff options
Diffstat (limited to 'test/core/util')
-rw-r--r-- | test/core/util/BUILD | 2 | ||||
-rw-r--r-- | test/core/util/fuzzer_util.cc | 82 | ||||
-rw-r--r-- | test/core/util/fuzzer_util.h | 49 | ||||
-rw-r--r-- | test/core/util/port_isolated_runtime_environment.cc | 17 |
4 files changed, 146 insertions, 4 deletions
diff --git a/test/core/util/BUILD b/test/core/util/BUILD index 886cfddf86..f52570cde5 100644 --- a/test/core/util/BUILD +++ b/test/core/util/BUILD @@ -52,6 +52,7 @@ grpc_cc_library( name = "grpc_test_util_base", srcs = [ "cmdline.cc", + "fuzzer_util.cc", "grpc_profiler.cc", "histogram.cc", "mock_endpoint.cc", @@ -70,6 +71,7 @@ grpc_cc_library( ], hdrs = [ "cmdline.h", + "fuzzer_util.h", "grpc_profiler.h", "histogram.h", "mock_endpoint.h", diff --git a/test/core/util/fuzzer_util.cc b/test/core/util/fuzzer_util.cc new file mode 100644 index 0000000000..29c9b8875f --- /dev/null +++ b/test/core/util/fuzzer_util.cc @@ -0,0 +1,82 @@ +/* + * + * Copyright 2018 gRPC authors. + * + * 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. + * + */ + +#include "test/core/util/fuzzer_util.h" + +#include <grpc/support/alloc.h> + +#include "src/core/lib/gpr/useful.h" + +namespace grpc_core { +namespace testing { + +uint8_t grpc_fuzzer_get_next_byte(input_stream* inp) { + if (inp->cur == inp->end) { + return 0; + } + return *inp->cur++; +} + +char* grpc_fuzzer_get_next_string(input_stream* inp, bool* special) { + char* str = nullptr; + size_t cap = 0; + size_t sz = 0; + char c; + do { + if (cap == sz) { + cap = GPR_MAX(3 * cap / 2, cap + 8); + str = static_cast<char*>(gpr_realloc(str, cap)); + } + c = static_cast<char>(grpc_fuzzer_get_next_byte(inp)); + str[sz++] = c; + } while (c != 0 && c != 1); + if (special != nullptr) { + *special = (c == 1); + } + if (c == 1) { + str[sz - 1] = 0; + } + return str; +} + +uint32_t grpc_fuzzer_get_next_uint32(input_stream* inp) { + uint8_t b = grpc_fuzzer_get_next_byte(inp); + uint32_t x = b & 0x7f; + if (b & 0x80) { + x <<= 7; + b = grpc_fuzzer_get_next_byte(inp); + x |= b & 0x7f; + if (b & 0x80) { + x <<= 7; + b = grpc_fuzzer_get_next_byte(inp); + x |= b & 0x7f; + if (b & 0x80) { + x <<= 7; + b = grpc_fuzzer_get_next_byte(inp); + x |= b & 0x7f; + if (b & 0x80) { + x = (x << 4) | (grpc_fuzzer_get_next_byte(inp) & 0x0f); + } + } + } + } + return x; +} + +} // namespace testing +} // namespace grpc_core diff --git a/test/core/util/fuzzer_util.h b/test/core/util/fuzzer_util.h new file mode 100644 index 0000000000..0e938399a1 --- /dev/null +++ b/test/core/util/fuzzer_util.h @@ -0,0 +1,49 @@ +/* + * + * Copyright 2018 gRPC authors. + * + * 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. + * + */ + +#ifndef GRPC_TEST_CORE_UTIL_FUZZER_UTIL_H +#define GRPC_TEST_CORE_UTIL_FUZZER_UTIL_H + +#include <stdint.h> + +namespace grpc_core { + +namespace testing { + +// Main struct for input_stream. It allows easy access to input +// bytes, and allows reading a little past the end(avoiding +// needing to check everywhere). +typedef struct { + const uint8_t* cur; + const uint8_t* end; +} input_stream; + +// get a byte from an input stream. +uint8_t grpc_fuzzer_get_next_byte(input_stream* inp); + +// get a string and boolean values (if special is not null) from an input +// stream. +char* grpc_fuzzer_get_next_string(input_stream* inp, bool* special); + +// get a uint32 value from an input stream. +uint32_t grpc_fuzzer_get_next_uint32(input_stream* inp); + +} // namespace testing +} // namespace grpc_core + +#endif /* GRPC_TEST_CORE_UTIL_FUZZER_UTIL_H */ diff --git a/test/core/util/port_isolated_runtime_environment.cc b/test/core/util/port_isolated_runtime_environment.cc index 5f0585e9fb..ff8342ff4a 100644 --- a/test/core/util/port_isolated_runtime_environment.cc +++ b/test/core/util/port_isolated_runtime_environment.cc @@ -19,19 +19,28 @@ /* When running tests on remote machines, the framework takes a round-robin pick * of a port within certain range. There is no need to recycle ports. */ +#include <grpc/support/time.h> +#include <stdlib.h> #include "src/core/lib/iomgr/port.h" #include "test/core/util/test_config.h" #if defined(GRPC_PORT_ISOLATED_RUNTIME) #include "test/core/util/port.h" -#define LOWER_PORT 49152 -static int s_allocated_port = LOWER_PORT; +#define MIN_PORT 49152 +#define MAX_PORT 65536 + +int get_random_starting_port() { + srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); + return rand() % (MAX_PORT - MIN_PORT + 1) + MIN_PORT; +} + +static int s_allocated_port = get_random_starting_port(); int grpc_pick_unused_port_or_die(void) { int allocated_port = s_allocated_port++; - if (s_allocated_port == 65536) { - s_allocated_port = LOWER_PORT; + if (s_allocated_port == MAX_PORT) { + s_allocated_port = MIN_PORT; } return allocated_port; |