diff options
Diffstat (limited to 'test/core/util')
-rw-r--r-- | test/core/util/grpc_profiler.c | 38 | ||||
-rw-r--r-- | test/core/util/grpc_profiler.h | 48 | ||||
-rw-r--r-- | test/core/util/parse_hexstring.c | 70 | ||||
-rw-r--r-- | test/core/util/parse_hexstring.h | 41 | ||||
-rw-r--r-- | test/core/util/port.c | 145 | ||||
-rw-r--r-- | test/core/util/port.h | 44 | ||||
-rw-r--r-- | test/core/util/slice_splitter.c | 138 | ||||
-rw-r--r-- | test/core/util/slice_splitter.h | 68 | ||||
-rw-r--r-- | test/core/util/test_config.c | 36 | ||||
-rw-r--r-- | test/core/util/test_config.h | 47 |
10 files changed, 675 insertions, 0 deletions
diff --git a/test/core/util/grpc_profiler.c b/test/core/util/grpc_profiler.c new file mode 100644 index 0000000000..e135743d57 --- /dev/null +++ b/test/core/util/grpc_profiler.c @@ -0,0 +1,38 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "test/core/util/grpc_profiler.h" + +void grpc_profiler_start(const char *filename) {} + +void grpc_profiler_stop() {} diff --git a/test/core/util/grpc_profiler.h b/test/core/util/grpc_profiler.h new file mode 100644 index 0000000000..7cc4733e65 --- /dev/null +++ b/test/core/util/grpc_profiler.h @@ -0,0 +1,48 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef __GRPC_TEST_UTIL_GRPC_PROFILER_H__ +#define __GRPC_TEST_UTIL_GRPC_PROFILER_H__ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +void grpc_profiler_start(const char *filename); +void grpc_profiler_stop(); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __GRPC_TEST_UTIL_GRPC_PROFILER_H__ */ diff --git a/test/core/util/parse_hexstring.c b/test/core/util/parse_hexstring.c new file mode 100644 index 0000000000..888d03bc68 --- /dev/null +++ b/test/core/util/parse_hexstring.c @@ -0,0 +1,70 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "test/core/util/parse_hexstring.h" +#include <grpc/support/log.h> + +gpr_slice parse_hexstring(const char *hexstring) { + int nibbles = 0; + const char *p = 0; + gpr_uint8 *out; + gpr_uint8 temp; + gpr_slice slice; + + for (p = hexstring; *p; p++) { + nibbles += (*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f'); + } + + GPR_ASSERT((nibbles & 1) == 0); + + slice = gpr_slice_malloc(nibbles / 2); + out = GPR_SLICE_START_PTR(slice); + + nibbles = 0; + temp = 0; + for (p = hexstring; *p; p++) { + if (*p >= '0' && *p <= '9') { + temp = (temp << 4) | (*p - '0'); + nibbles++; + } else if (*p >= 'a' && *p <= 'f') { + temp = (temp << 4) | (*p - 'a' + 10); + nibbles++; + } + if (nibbles == 2) { + *out++ = temp; + nibbles = 0; + } + } + + return slice; +} diff --git a/test/core/util/parse_hexstring.h b/test/core/util/parse_hexstring.h new file mode 100644 index 0000000000..7477986d60 --- /dev/null +++ b/test/core/util/parse_hexstring.h @@ -0,0 +1,41 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef __GRPC_TEST_UTIL_PARSE_HEXSTRING_H_ +#define __GRPC_TEST_UTIL_PARSE_HEXSTRING_H_ + +#include <grpc/support/slice.h> + +gpr_slice parse_hexstring(const char *hexstring); + +#endif /* __GRPC_TEST_UTIL_PARSE_HEXSTRING_H_ */ diff --git a/test/core/util/port.c b/test/core/util/port.c new file mode 100644 index 0000000000..133b53fd2b --- /dev/null +++ b/test/core/util/port.c @@ -0,0 +1,145 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "test/core/util/port.h" + +#include <netinet/in.h> +#include <sys/socket.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> + +#include <grpc/support/log.h> + +#define NUM_RANDOM_PORTS_TO_PICK 100 + +static int is_port_available(int *port, int is_tcp) { + const int proto = is_tcp ? IPPROTO_TCP : 0; + const int fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto); + int one = 1; + struct sockaddr_in addr; + socklen_t alen = sizeof(addr); + int actual_port; + + GPR_ASSERT(*port >= 0); + GPR_ASSERT(*port <= 65535); + if (fd < 0) { + gpr_log(GPR_ERROR, "socket() failed: %s", strerror(errno)); + return 0; + } + + /* Reuseaddr lets us start up a server immediately after it exits */ + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { + gpr_log(GPR_ERROR, "setsockopt() failed: %s", strerror(errno)); + close(fd); + return 0; + } + + /* Try binding to port */ + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(*port); + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + gpr_log(GPR_DEBUG, "bind(port=%d) failed: %s", *port, strerror(errno)); + close(fd); + return 0; + } + + /* Get the bound port number */ + if (getsockname(fd, (struct sockaddr *)&addr, &alen) < 0) { + gpr_log(GPR_ERROR, "getsockname() failed: %s", strerror(errno)); + close(fd); + return 0; + } + GPR_ASSERT(alen <= sizeof(addr)); + actual_port = ntohs(addr.sin_port); + GPR_ASSERT(actual_port > 0); + if (*port == 0) { + *port = actual_port; + } else { + GPR_ASSERT(*port == actual_port); + } + + close(fd); + return 1; +} + +int grpc_pick_unused_port() { + /* We repeatedly pick a port and then see whether or not it is + available for use both as a TCP socket and a UDP socket. First, we + pick a random large port number. For subsequent + iterations, we bind to an anonymous port and let the OS pick the + port number. The random port picking reduces the probability of + races with other processes on kernels that want to reuse the same + port numbers over and over. */ + + /* In alternating iterations we try UDP ports before TCP ports UDP + ports -- it could be the case that this machine has been using up + UDP ports and they are scarcer. */ + + /* Type of port to first pick in next iteration */ + int is_tcp = 1; + int try + = 0; + + for (;;) { + int port = try + < NUM_RANDOM_PORTS_TO_PICK ? rand() % (65536 - 30000) + 30000 : 0; + if (!is_port_available(&port, is_tcp)) { + continue; + } + GPR_ASSERT(port > 0); + /* Check that the port # is free for the other type of socket also */ + if (!is_port_available(&port, !is_tcp)) { + /* In the next iteration try to bind to the other type first + because perhaps it is more rare. */ + is_tcp = !is_tcp; + continue; + } + + /* TODO(ctiller): consider caching this port in some structure, to avoid + handing it out again */ + + return port; + } + + /* The port iterator reached the end without finding a suitable port. */ + return 0; +} + +int grpc_pick_unused_port_or_die() { + int port = grpc_pick_unused_port(); + GPR_ASSERT(port > 0); + return port; +} diff --git a/test/core/util/port.h b/test/core/util/port.h new file mode 100644 index 0000000000..d06f04731e --- /dev/null +++ b/test/core/util/port.h @@ -0,0 +1,44 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef __GRPC_TEST_UTIL_PORT_H__ +#define __GRPC_TEST_UTIL_PORT_H__ + +/* pick a port number that is currently unused by either tcp or udp. return + 0 on failure. */ +int grpc_pick_unused_port(); +/* pick a port number that is currently unused by either tcp or udp. abort + on failure. */ +int grpc_pick_unused_port_or_die(); + +#endif /* __GRPC_TEST_UTIL_PORT_H__ */ diff --git a/test/core/util/slice_splitter.c b/test/core/util/slice_splitter.c new file mode 100644 index 0000000000..1682ef4fcd --- /dev/null +++ b/test/core/util/slice_splitter.c @@ -0,0 +1,138 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "test/core/util/slice_splitter.h" + +#include <string.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/useful.h> + +const char *grpc_slice_split_mode_name(grpc_slice_split_mode mode) { + switch (mode) { + case GRPC_SLICE_SPLIT_IDENTITY: + return "identity"; + case GRPC_SLICE_SPLIT_MERGE_ALL: + return "merge_all"; + case GRPC_SLICE_SPLIT_ONE_BYTE: + return "one_byte"; + } + return "error"; +} + +void grpc_split_slices(grpc_slice_split_mode mode, gpr_slice *src_slices, + size_t src_slice_count, gpr_slice **dst_slices, + size_t *dst_slice_count) { + size_t i, j; + size_t length; + + switch (mode) { + case GRPC_SLICE_SPLIT_IDENTITY: + *dst_slice_count = src_slice_count; + *dst_slices = gpr_malloc(sizeof(gpr_slice) * src_slice_count); + for (i = 0; i < src_slice_count; i++) { + (*dst_slices)[i] = src_slices[i]; + gpr_slice_ref((*dst_slices)[i]); + } + break; + case GRPC_SLICE_SPLIT_MERGE_ALL: + *dst_slice_count = 1; + length = 0; + for (i = 0; i < src_slice_count; i++) { + length += GPR_SLICE_LENGTH(src_slices[i]); + } + *dst_slices = gpr_malloc(sizeof(gpr_slice)); + **dst_slices = gpr_slice_malloc(length); + length = 0; + for (i = 0; i < src_slice_count; i++) { + memcpy(GPR_SLICE_START_PTR(**dst_slices) + length, + GPR_SLICE_START_PTR(src_slices[i]), + GPR_SLICE_LENGTH(src_slices[i])); + length += GPR_SLICE_LENGTH(src_slices[i]); + } + break; + case GRPC_SLICE_SPLIT_ONE_BYTE: + length = 0; + for (i = 0; i < src_slice_count; i++) { + length += GPR_SLICE_LENGTH(src_slices[i]); + } + *dst_slice_count = length; + *dst_slices = gpr_malloc(sizeof(gpr_slice) * length); + length = 0; + for (i = 0; i < src_slice_count; i++) { + for (j = 0; j < GPR_SLICE_LENGTH(src_slices[i]); j++) { + (*dst_slices)[length] = gpr_slice_sub(src_slices[i], j, j + 1); + length++; + } + } + break; + } +} + +void grpc_split_slices_to_buffer(grpc_slice_split_mode mode, + gpr_slice *src_slices, size_t src_slice_count, + gpr_slice_buffer *dst) { + gpr_slice *slices; + size_t nslices; + size_t i; + grpc_split_slices(mode, src_slices, src_slice_count, &slices, &nslices); + for (i = 0; i < nslices; i++) { + /* add indexed to avoid re-merging split slices */ + gpr_slice_buffer_add_indexed(dst, slices[i]); + } + gpr_free(slices); +} + +void grpc_split_slice_buffer(grpc_slice_split_mode mode, gpr_slice_buffer *src, + gpr_slice_buffer *dst) { + grpc_split_slices_to_buffer(mode, src->slices, src->count, dst); +} + +gpr_slice grpc_slice_merge(gpr_slice *slices, size_t nslices) { + gpr_uint8 *out = NULL; + size_t length = 0; + size_t capacity = 0; + size_t i; + + for (i = 0; i < nslices; i++) { + if (GPR_SLICE_LENGTH(slices[i]) + length > capacity) { + capacity = GPR_MAX(capacity * 2, GPR_SLICE_LENGTH(slices[i]) + length); + out = gpr_realloc(out, capacity); + } + memcpy(out + length, GPR_SLICE_START_PTR(slices[i]), + GPR_SLICE_LENGTH(slices[i])); + length += GPR_SLICE_LENGTH(slices[i]); + } + + return gpr_slice_new(out, length, gpr_free); +} diff --git a/test/core/util/slice_splitter.h b/test/core/util/slice_splitter.h new file mode 100644 index 0000000000..bff4515c57 --- /dev/null +++ b/test/core/util/slice_splitter.h @@ -0,0 +1,68 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef __GRPC_TEST_UTIL_SLICE_SPLITTER_H__ +#define __GRPC_TEST_UTIL_SLICE_SPLITTER_H__ + +/* utility function to split/merge slices together to help create test + cases */ + +#include <grpc/support/slice.h> +#include <grpc/support/slice_buffer.h> + +typedef enum { + /* merge all input slices into a single slice */ + GRPC_SLICE_SPLIT_MERGE_ALL, + /* leave slices as is */ + GRPC_SLICE_SPLIT_IDENTITY, + /* split slices into one byte chunks */ + GRPC_SLICE_SPLIT_ONE_BYTE +} grpc_slice_split_mode; + +/* allocates *dst_slices; caller must unref all slices in dst_slices then free + it */ +void grpc_split_slices(grpc_slice_split_mode mode, gpr_slice *src_slices, + size_t src_slice_count, gpr_slice **dst_slices, + size_t *dst_slice_count); + +void grpc_split_slices_to_buffer(grpc_slice_split_mode mode, + gpr_slice *src_slices, size_t src_slice_count, + gpr_slice_buffer *dst); +void grpc_split_slice_buffer(grpc_slice_split_mode mode, gpr_slice_buffer *src, + gpr_slice_buffer *dst); + +gpr_slice grpc_slice_merge(gpr_slice *slices, size_t nslices); + +const char *grpc_slice_split_mode_name(grpc_slice_split_mode mode); + +#endif /* __GRPC_TEST_UTIL_SLICE_SPLITTER_H__ */ diff --git a/test/core/util/test_config.c b/test/core/util/test_config.c new file mode 100644 index 0000000000..993014aa14 --- /dev/null +++ b/test/core/util/test_config.c @@ -0,0 +1,36 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "test/core/util/test_config.h" + +void grpc_test_init(int argc, char **argv) {} diff --git a/test/core/util/test_config.h b/test/core/util/test_config.h new file mode 100644 index 0000000000..8ada77bfc7 --- /dev/null +++ b/test/core/util/test_config.h @@ -0,0 +1,47 @@ +/* + * + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef __GRPC_TEST_UTIL_TEST_CONFIG_H__ +#define __GRPC_TEST_UTIL_TEST_CONFIG_H__ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +void grpc_test_init(int argc, char **argv); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __GRPC_TEST_UTIL_TEST_CONFIG_H__ */ |