aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/endpoint
diff options
context:
space:
mode:
authorGravatar ctiller <ctiller@google.com>2014-12-17 09:44:44 -0800
committerGravatar Nicolas Noble <nnoble@google.com>2014-12-17 15:49:42 -0800
commit2bbb6c43119c25f173757ac8527074ed11a87ffa (patch)
tree306bdc4efa452cb096ee7311fd8671faf54531eb /test/core/endpoint
parentab4f91458bc2775a8bfe38451afd882c06ca7bfc (diff)
Remove endpoint/ directories.
Fold endpoint interface into iomgr, move secure_endpoint into security/. This will make it easier for endpoint to rely on some iomgr defined types (like pollset). Change on 2014/12/17 by ctiller <ctiller@google.com> ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=82338036
Diffstat (limited to 'test/core/endpoint')
-rw-r--r--test/core/endpoint/endpoint_tests.c438
-rw-r--r--test/core/endpoint/endpoint_tests.h57
-rw-r--r--test/core/endpoint/secure_endpoint_test.c207
-rw-r--r--test/core/endpoint/tcp_client_test.c177
4 files changed, 0 insertions, 879 deletions
diff --git a/test/core/endpoint/endpoint_tests.c b/test/core/endpoint/endpoint_tests.c
deleted file mode 100644
index f0abc30bdd..0000000000
--- a/test/core/endpoint/endpoint_tests.c
+++ /dev/null
@@ -1,438 +0,0 @@
-/*
- *
- * 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/endpoint/endpoint_tests.h"
-
-#include <sys/types.h>
-
-#include <grpc/support/alloc.h>
-#include <grpc/support/slice.h>
-#include <grpc/support/log.h>
-#include <grpc/support/time.h>
-
-/*
- General test notes:
-
- All tests which write data into an endpoint write i%256 into byte i, which
- is verified by readers.
-
- In general there are a few interesting things to vary which may lead to
- exercising different codepaths in an implementation:
- 1. Total amount of data written to the endpoint
- 2. Size of slice allocations
- 3. Amount of data we read from or write to the endpoint at once
-
- The tests here tend to parameterize these where applicable.
-
-*/
-
-size_t count_and_unref_slices(gpr_slice *slices, size_t nslices,
- int *current_data) {
- size_t num_bytes = 0;
- size_t i;
- size_t j;
- unsigned char *buf;
- for (i = 0; i < nslices; ++i) {
- buf = GPR_SLICE_START_PTR(slices[i]);
- for (j = 0; j < GPR_SLICE_LENGTH(slices[i]); ++j) {
- GPR_ASSERT(buf[j] == *current_data);
- *current_data = (*current_data + 1) % 256;
- }
- num_bytes += GPR_SLICE_LENGTH(slices[i]);
- gpr_slice_unref(slices[i]);
- }
- return num_bytes;
-}
-
-static grpc_endpoint_test_fixture begin_test(grpc_endpoint_test_config config,
- const char *test_name,
- size_t slice_size) {
- gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
- return config.create_fixture(slice_size);
-}
-
-static void end_test(grpc_endpoint_test_config config) { config.clean_up(); }
-
-static gpr_slice *allocate_blocks(size_t num_bytes, size_t slice_size,
- size_t *num_blocks, int *current_data) {
- size_t nslices = num_bytes / slice_size + (num_bytes % slice_size ? 1 : 0);
- gpr_slice *slices = malloc(sizeof(gpr_slice) * nslices);
- size_t num_bytes_left = num_bytes;
- size_t i;
- size_t j;
- unsigned char *buf;
- *num_blocks = nslices;
-
- for (i = 0; i < nslices; ++i) {
- slices[i] = gpr_slice_malloc(slice_size > num_bytes_left ? num_bytes_left
- : slice_size);
- num_bytes_left -= GPR_SLICE_LENGTH(slices[i]);
- buf = GPR_SLICE_START_PTR(slices[i]);
- for (j = 0; j < GPR_SLICE_LENGTH(slices[i]); ++j) {
- buf[j] = *current_data;
- *current_data = (*current_data + 1) % 256;
- }
- }
- GPR_ASSERT(num_bytes_left == 0);
- return slices;
-}
-
-struct read_and_write_test_state {
- grpc_endpoint *read_ep;
- grpc_endpoint *write_ep;
- gpr_mu mu;
- gpr_cv cv;
- size_t target_bytes;
- size_t bytes_read;
- size_t current_write_size;
- size_t bytes_written;
- int current_read_data;
- int current_write_data;
- int read_done;
- int write_done;
-};
-
-static void read_and_write_test_read_handler(void *data, gpr_slice *slices,
- size_t nslices,
- grpc_endpoint_cb_status error) {
- struct read_and_write_test_state *state = data;
- GPR_ASSERT(error != GRPC_ENDPOINT_CB_ERROR);
- if (error == GRPC_ENDPOINT_CB_SHUTDOWN) {
- gpr_log(GPR_INFO, "Read handler shutdown");
- gpr_mu_lock(&state->mu);
- state->read_done = 1;
- gpr_cv_signal(&state->cv);
- gpr_mu_unlock(&state->mu);
- return;
- }
-
- state->bytes_read +=
- count_and_unref_slices(slices, nslices, &state->current_read_data);
- if (state->bytes_read == state->target_bytes) {
- gpr_log(GPR_INFO, "Read handler done");
- gpr_mu_lock(&state->mu);
- state->read_done = 1;
- gpr_cv_signal(&state->cv);
- gpr_mu_unlock(&state->mu);
- } else {
- grpc_endpoint_notify_on_read(
- state->read_ep, read_and_write_test_read_handler, data, gpr_inf_future);
- }
-}
-
-static void read_and_write_test_write_handler(void *data,
- grpc_endpoint_cb_status error) {
- struct read_and_write_test_state *state = data;
- gpr_slice *slices = NULL;
- size_t nslices;
- grpc_endpoint_write_status write_status;
-
- GPR_ASSERT(error != GRPC_ENDPOINT_CB_ERROR);
-
- if (error == GRPC_ENDPOINT_CB_SHUTDOWN) {
- gpr_log(GPR_INFO, "Write handler shutdown");
- gpr_mu_lock(&state->mu);
- state->write_done = 1;
- gpr_cv_signal(&state->cv);
- gpr_mu_unlock(&state->mu);
- return;
- }
-
- for (;;) {
- /* Need to do inline writes until they don't succeed synchronously or we
- finish writing */
- state->bytes_written += state->current_write_size;
- if (state->target_bytes - state->bytes_written <
- state->current_write_size) {
- state->current_write_size = state->target_bytes - state->bytes_written;
- }
- if (state->current_write_size == 0) {
- break;
- }
-
- slices = allocate_blocks(state->current_write_size, 8192, &nslices,
- &state->current_write_data);
- write_status = grpc_endpoint_write(state->write_ep, slices, nslices,
- read_and_write_test_write_handler, state,
- gpr_inf_future);
- GPR_ASSERT(write_status != GRPC_ENDPOINT_WRITE_ERROR);
- free(slices);
- if (write_status == GRPC_ENDPOINT_WRITE_PENDING) {
- return;
- }
- }
- GPR_ASSERT(state->bytes_written == state->target_bytes);
-
- gpr_log(GPR_INFO, "Write handler done");
- gpr_mu_lock(&state->mu);
- state->write_done = 1;
- gpr_cv_signal(&state->cv);
- gpr_mu_unlock(&state->mu);
-}
-
-/* Do both reading and writing using the grpc_endpoint API.
-
- This also includes a test of the shutdown behavior.
- */
-static void read_and_write_test(grpc_endpoint_test_config config,
- size_t num_bytes, size_t write_size,
- size_t slice_size, int shutdown) {
- struct read_and_write_test_state state;
- gpr_timespec rel_deadline = {20, 0};
- gpr_timespec deadline = gpr_time_add(gpr_now(), rel_deadline);
- grpc_endpoint_test_fixture f = begin_test(config, __FUNCTION__, slice_size);
-
- if (shutdown) {
- gpr_log(GPR_INFO, "Start read and write shutdown test");
- } else {
- gpr_log(GPR_INFO, "Start read and write test with %d bytes, slice size %d",
- num_bytes, slice_size);
- }
-
- gpr_mu_init(&state.mu);
- gpr_cv_init(&state.cv);
-
- state.read_ep = f.client_ep;
- state.write_ep = f.server_ep;
- state.target_bytes = num_bytes;
- state.bytes_read = 0;
- state.current_write_size = write_size;
- state.bytes_written = 0;
- state.read_done = 0;
- state.write_done = 0;
- state.current_read_data = 0;
- state.current_write_data = 0;
-
- /* Get started by pretending an initial write completed */
- /* NOTE: Sets up initial conditions so we can have the same write handler
- for the first iteration as for later iterations. It does the right thing
- even when bytes_written is unsigned. */
- state.bytes_written -= state.current_write_size;
- read_and_write_test_write_handler(&state, GRPC_ENDPOINT_CB_OK);
-
- grpc_endpoint_notify_on_read(state.read_ep, read_and_write_test_read_handler,
- &state, gpr_inf_future);
-
- if (shutdown) {
- grpc_endpoint_shutdown(state.read_ep);
- grpc_endpoint_shutdown(state.write_ep);
- }
-
- gpr_mu_lock(&state.mu);
- while (!state.read_done || !state.write_done) {
- GPR_ASSERT(gpr_cv_wait(&state.cv, &state.mu, deadline) == 0);
- }
- gpr_mu_unlock(&state.mu);
-
- grpc_endpoint_destroy(state.read_ep);
- grpc_endpoint_destroy(state.write_ep);
- gpr_mu_destroy(&state.mu);
- gpr_cv_destroy(&state.cv);
- end_test(config);
-}
-
-struct timeout_test_state {
- gpr_event io_done;
-};
-
-static void read_timeout_test_read_handler(void *data, gpr_slice *slices,
- size_t nslices,
- grpc_endpoint_cb_status error) {
- struct timeout_test_state *state = data;
- GPR_ASSERT(error == GRPC_ENDPOINT_CB_TIMED_OUT);
- gpr_event_set(&state->io_done, (void *)1);
-}
-
-static void read_timeout_test(grpc_endpoint_test_config config,
- size_t slice_size) {
- gpr_timespec timeout = gpr_time_from_micros(10000);
- gpr_timespec read_deadline = gpr_time_add(gpr_now(), timeout);
- gpr_timespec test_deadline =
- gpr_time_add(gpr_now(), gpr_time_from_micros(2000000));
- struct timeout_test_state state;
- grpc_endpoint_test_fixture f = begin_test(config, __FUNCTION__, slice_size);
-
- gpr_event_init(&state.io_done);
-
- grpc_endpoint_notify_on_read(f.client_ep, read_timeout_test_read_handler,
- &state, read_deadline);
- GPR_ASSERT(gpr_event_wait(&state.io_done, test_deadline));
- grpc_endpoint_destroy(f.client_ep);
- grpc_endpoint_destroy(f.server_ep);
- end_test(config);
-}
-
-static void write_timeout_test_write_handler(void *data,
- grpc_endpoint_cb_status error) {
- struct timeout_test_state *state = data;
- GPR_ASSERT(error == GRPC_ENDPOINT_CB_TIMED_OUT);
- gpr_event_set(&state->io_done, (void *)1);
-}
-
-static void write_timeout_test(grpc_endpoint_test_config config,
- size_t slice_size) {
- gpr_timespec timeout = gpr_time_from_micros(10000);
- gpr_timespec write_deadline = gpr_time_add(gpr_now(), timeout);
- gpr_timespec test_deadline =
- gpr_time_add(gpr_now(), gpr_time_from_micros(2000000));
- struct timeout_test_state state;
- int current_data = 1;
- gpr_slice *slices;
- size_t nblocks;
- size_t size;
- grpc_endpoint_test_fixture f = begin_test(config, __FUNCTION__, slice_size);
-
- gpr_event_init(&state.io_done);
-
- /* TODO(klempner): Factor this out with the equivalent code in tcp_test.c */
- for (size = 1;; size *= 2) {
- slices = allocate_blocks(size, 1, &nblocks, &current_data);
- switch (grpc_endpoint_write(f.client_ep, slices, nblocks,
- write_timeout_test_write_handler, &state,
- write_deadline)) {
- case GRPC_ENDPOINT_WRITE_DONE:
- break;
- case GRPC_ENDPOINT_WRITE_ERROR:
- gpr_log(GPR_ERROR, "error writing");
- abort();
- case GRPC_ENDPOINT_WRITE_PENDING:
- GPR_ASSERT(gpr_event_wait(&state.io_done, test_deadline));
- gpr_free(slices);
- goto exit;
- }
- gpr_free(slices);
- }
-exit:
- grpc_endpoint_destroy(f.client_ep);
- grpc_endpoint_destroy(f.server_ep);
- end_test(config);
-}
-
-typedef struct {
- gpr_event ev;
- grpc_endpoint *ep;
-} shutdown_during_write_test_state;
-
-static void shutdown_during_write_test_read_handler(
- void *user_data, gpr_slice *slices, size_t nslices,
- grpc_endpoint_cb_status error) {
- size_t i;
- shutdown_during_write_test_state *st = user_data;
-
- for (i = 0; i < nslices; i++) {
- gpr_slice_unref(slices[i]);
- }
-
- if (error != GRPC_ENDPOINT_CB_OK) {
- grpc_endpoint_destroy(st->ep);
- gpr_event_set(&st->ev, (void *)(gpr_intptr)error);
- } else {
- grpc_endpoint_notify_on_read(st->ep,
- shutdown_during_write_test_read_handler,
- user_data, gpr_inf_future);
- }
-}
-
-static void shutdown_during_write_test_write_handler(
- void *user_data, grpc_endpoint_cb_status error) {
- shutdown_during_write_test_state *st = user_data;
- gpr_log(GPR_INFO, "shutdown_during_write_test_write_handler: error = %d",
- error);
- if (error == 0) {
- /* This happens about 0.5% of the time when run under TSAN, and is entirely
- legitimate, but means we aren't testing the path we think we are. */
- /* TODO(klempner): Change this test to retry the write in that case */
- gpr_log(GPR_ERROR,
- "shutdown_during_write_test_write_handler completed unexpectedly");
- }
- gpr_event_set(&st->ev, (void *)(gpr_intptr)1);
-}
-
-static void shutdown_during_write_test(grpc_endpoint_test_config config,
- size_t slice_size) {
- /* test that shutdown with a pending write creates no leaks */
- gpr_timespec deadline;
- size_t size;
- size_t nblocks;
- int current_data = 1;
- shutdown_during_write_test_state read_st;
- shutdown_during_write_test_state write_st;
- gpr_slice *slices;
- grpc_endpoint_test_fixture f = begin_test(config, __FUNCTION__, slice_size);
-
- gpr_log(GPR_INFO, "testing shutdown during a write");
-
- read_st.ep = f.client_ep;
- write_st.ep = f.server_ep;
- gpr_event_init(&read_st.ev);
- gpr_event_init(&write_st.ev);
-
- grpc_endpoint_notify_on_read(read_st.ep,
- shutdown_during_write_test_read_handler,
- &read_st, gpr_inf_future);
- for (size = 1;; size *= 2) {
- slices = allocate_blocks(size, 1, &nblocks, &current_data);
- switch (grpc_endpoint_write(write_st.ep, slices, nblocks,
- shutdown_during_write_test_write_handler,
- &write_st, gpr_inf_future)) {
- case GRPC_ENDPOINT_WRITE_DONE:
- break;
- case GRPC_ENDPOINT_WRITE_ERROR:
- gpr_log(GPR_ERROR, "error writing");
- abort();
- case GRPC_ENDPOINT_WRITE_PENDING:
- grpc_endpoint_shutdown(write_st.ep);
- deadline =
- gpr_time_add(gpr_now(), gpr_time_from_micros(10 * GPR_US_PER_SEC));
- GPR_ASSERT(gpr_event_wait(&write_st.ev, deadline));
- grpc_endpoint_destroy(write_st.ep);
- GPR_ASSERT(gpr_event_wait(&read_st.ev, deadline));
- gpr_free(slices);
- end_test(config);
- return;
- }
- gpr_free(slices);
- }
-
- gpr_log(GPR_ERROR, "should never reach here");
- abort();
-}
-
-void grpc_endpoint_tests(grpc_endpoint_test_config config) {
- read_and_write_test(config, 10000000, 100000, 8192, 0);
- read_and_write_test(config, 1000000, 100000, 1, 0);
- read_and_write_test(config, 100000000, 100000, 1, 1);
- read_timeout_test(config, 1000);
- write_timeout_test(config, 1000);
- shutdown_during_write_test(config, 1000);
-}
diff --git a/test/core/endpoint/endpoint_tests.h b/test/core/endpoint/endpoint_tests.h
deleted file mode 100644
index d5f34374da..0000000000
--- a/test/core/endpoint/endpoint_tests.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- *
- * 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_ENDPOINT_ENDPOINT_TESTS_H__
-#define __GRPC_TEST_ENDPOINT_ENDPOINT_TESTS_H__
-
-#include <sys/types.h>
-
-#include "src/core/endpoint/endpoint.h"
-
-typedef struct grpc_endpoint_test_config grpc_endpoint_test_config;
-typedef struct grpc_endpoint_test_fixture grpc_endpoint_test_fixture;
-
-struct grpc_endpoint_test_fixture {
- grpc_endpoint *client_ep;
- grpc_endpoint *server_ep;
-};
-
-struct grpc_endpoint_test_config {
- const char *name;
- grpc_endpoint_test_fixture (*create_fixture)(size_t slice_size);
- void (*clean_up)();
-};
-
-void grpc_endpoint_tests(grpc_endpoint_test_config config);
-
-#endif /* __GRPC_TEST_ENDPOINT_ENDPOINT_TESTS_H__ */
diff --git a/test/core/endpoint/secure_endpoint_test.c b/test/core/endpoint/secure_endpoint_test.c
deleted file mode 100644
index 18a33b5f52..0000000000
--- a/test/core/endpoint/secure_endpoint_test.c
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- *
- * 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 "endpoint_tests.h"
-
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <unistd.h>
-
-#include "src/core/endpoint/secure_endpoint.h"
-#include "src/core/iomgr/endpoint_pair.h"
-#include "src/core/iomgr/iomgr.h"
-#include <grpc/support/alloc.h>
-#include <grpc/support/log.h>
-#include "test/core/util/test_config.h"
-#include "src/core/tsi/fake_transport_security.h"
-
-static grpc_endpoint_test_fixture secure_endpoint_create_fixture_tcp_socketpair(
- size_t slice_size, gpr_slice *leftover_slices, size_t leftover_nslices) {
- tsi_frame_protector *fake_read_protector = tsi_create_fake_protector(NULL);
- tsi_frame_protector *fake_write_protector = tsi_create_fake_protector(NULL);
- grpc_endpoint_test_fixture f;
- grpc_endpoint_pair tcp;
-
- tcp = grpc_iomgr_create_endpoint_pair(slice_size);
-
- if (leftover_nslices == 0) {
- f.client_ep =
- grpc_secure_endpoint_create(fake_read_protector, tcp.client, NULL, 0);
- } else {
- int i;
- tsi_result result;
- gpr_uint32 still_pending_size;
- size_t total_buffer_size = 8192;
- size_t buffer_size = total_buffer_size;
- gpr_uint8 *encrypted_buffer = gpr_malloc(buffer_size);
- gpr_uint8 *cur = encrypted_buffer;
- gpr_slice encrypted_leftover;
- for (i = 0; i < leftover_nslices; i++) {
- gpr_slice plain = leftover_slices[i];
- gpr_uint8 *message_bytes = GPR_SLICE_START_PTR(plain);
- size_t message_size = GPR_SLICE_LENGTH(plain);
- while (message_size > 0) {
- gpr_uint32 protected_buffer_size_to_send = buffer_size;
- gpr_uint32 processed_message_size = message_size;
- result = tsi_frame_protector_protect(
- fake_write_protector, message_bytes, &processed_message_size, cur,
- &protected_buffer_size_to_send);
- GPR_ASSERT(result == TSI_OK);
- message_bytes += processed_message_size;
- message_size -= processed_message_size;
- cur += protected_buffer_size_to_send;
- buffer_size -= protected_buffer_size_to_send;
-
- GPR_ASSERT(buffer_size >= 0);
- }
- gpr_slice_unref(plain);
- }
- do {
- gpr_uint32 protected_buffer_size_to_send = buffer_size;
- result = tsi_frame_protector_protect_flush(fake_write_protector, cur,
- &protected_buffer_size_to_send,
- &still_pending_size);
- GPR_ASSERT(result == TSI_OK);
- cur += protected_buffer_size_to_send;
- buffer_size -= protected_buffer_size_to_send;
- GPR_ASSERT(buffer_size >= 0);
- } while (still_pending_size > 0);
- encrypted_leftover = gpr_slice_from_copied_buffer(
- (const char *)encrypted_buffer, total_buffer_size - buffer_size);
- f.client_ep = grpc_secure_endpoint_create(fake_read_protector, tcp.client,
- &encrypted_leftover, 1);
- gpr_slice_unref(encrypted_leftover);
- gpr_free(encrypted_buffer);
- }
-
- f.server_ep =
- grpc_secure_endpoint_create(fake_write_protector, tcp.server, NULL, 0);
- return f;
-}
-
-static grpc_endpoint_test_fixture
-secure_endpoint_create_fixture_tcp_socketpair_noleftover(size_t slice_size) {
- return secure_endpoint_create_fixture_tcp_socketpair(slice_size, NULL, 0);
-}
-
-static grpc_endpoint_test_fixture
-secure_endpoint_create_fixture_tcp_socketpair_leftover(size_t slice_size) {
- gpr_slice s =
- gpr_slice_from_copied_string("hello world 12345678900987654321");
- grpc_endpoint_test_fixture f;
-
- f = secure_endpoint_create_fixture_tcp_socketpair(slice_size, &s, 1);
- return f;
-}
-
-static void clean_up() {}
-
-static grpc_endpoint_test_config configs[] = {
- {"secure_ep/tcp_socketpair",
- secure_endpoint_create_fixture_tcp_socketpair_noleftover, clean_up},
- {"secure_ep/tcp_socketpair_leftover",
- secure_endpoint_create_fixture_tcp_socketpair_leftover, clean_up},
-};
-
-static void verify_leftover(void *user_data, gpr_slice *slices, size_t nslices,
- grpc_endpoint_cb_status error) {
- gpr_slice s =
- gpr_slice_from_copied_string("hello world 12345678900987654321");
-
- GPR_ASSERT(error == GRPC_ENDPOINT_CB_OK);
- GPR_ASSERT(nslices == 1);
-
- GPR_ASSERT(0 == gpr_slice_cmp(s, slices[0]));
- gpr_slice_unref(slices[0]);
- gpr_slice_unref(s);
- *(int *)user_data = 1;
-}
-
-static void test_leftover(grpc_endpoint_test_config config, size_t slice_size) {
- grpc_endpoint_test_fixture f = config.create_fixture(slice_size);
- int verified = 0;
- gpr_log(GPR_INFO, "Start test left over");
-
- grpc_endpoint_notify_on_read(f.client_ep, verify_leftover, &verified,
- gpr_inf_future);
- GPR_ASSERT(verified == 1);
-
- grpc_endpoint_shutdown(f.client_ep);
- grpc_endpoint_shutdown(f.server_ep);
- grpc_endpoint_destroy(f.client_ep);
- grpc_endpoint_destroy(f.server_ep);
- clean_up();
-}
-
-static void destroy_early(void *user_data, gpr_slice *slices, size_t nslices,
- grpc_endpoint_cb_status error) {
- grpc_endpoint_test_fixture *f = user_data;
- gpr_slice s =
- gpr_slice_from_copied_string("hello world 12345678900987654321");
-
- GPR_ASSERT(error == GRPC_ENDPOINT_CB_OK);
- GPR_ASSERT(nslices == 1);
-
- grpc_endpoint_shutdown(f->client_ep);
- grpc_endpoint_destroy(f->client_ep);
-
- GPR_ASSERT(0 == gpr_slice_cmp(s, slices[0]));
- gpr_slice_unref(slices[0]);
- gpr_slice_unref(s);
-}
-
-/* test which destroys the ep before finishing reading */
-static void test_destroy_ep_early(grpc_endpoint_test_config config,
- size_t slice_size) {
- grpc_endpoint_test_fixture f = config.create_fixture(slice_size);
- gpr_log(GPR_INFO, "Start test destroy early");
-
- grpc_endpoint_notify_on_read(f.client_ep, destroy_early, &f, gpr_inf_future);
-
- grpc_endpoint_shutdown(f.server_ep);
- grpc_endpoint_destroy(f.server_ep);
- clean_up();
-}
-
-int main(int argc, char **argv) {
- grpc_test_init(argc, argv);
-
- grpc_iomgr_init();
- grpc_endpoint_tests(configs[0]);
- test_leftover(configs[1], 1);
- test_destroy_ep_early(configs[1], 1);
- grpc_iomgr_shutdown();
-
- return 0;
-}
diff --git a/test/core/endpoint/tcp_client_test.c b/test/core/endpoint/tcp_client_test.c
deleted file mode 100644
index 10138e6af5..0000000000
--- a/test/core/endpoint/tcp_client_test.c
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- *
- * 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 "src/core/endpoint/tcp_client.h"
-
-#include <errno.h>
-#include <netinet/in.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <unistd.h>
-
-#include "src/core/eventmanager/em.h"
-#include <grpc/support/log.h>
-#include <grpc/support/time.h>
-
-static grpc_em em;
-
-static gpr_timespec test_deadline() {
- return gpr_time_add(gpr_now(), gpr_time_from_micros(1000000));
-}
-
-static void must_succeed(void *arg, grpc_endpoint *tcp) {
- GPR_ASSERT(tcp);
- grpc_endpoint_shutdown(tcp);
- grpc_endpoint_destroy(tcp);
- gpr_event_set(arg, (void *)1);
-}
-
-static void must_fail(void *arg, grpc_endpoint *tcp) {
- GPR_ASSERT(!tcp);
- gpr_event_set(arg, (void *)1);
-}
-
-void test_succeeds() {
- struct sockaddr_in addr;
- socklen_t addr_len = sizeof(addr);
- int svr_fd;
- int r;
- gpr_event ev;
-
- gpr_event_init(&ev);
-
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
-
- /* create a dummy server */
- svr_fd = socket(AF_INET, SOCK_STREAM, 0);
- GPR_ASSERT(svr_fd >= 0);
- GPR_ASSERT(0 == bind(svr_fd, (struct sockaddr *)&addr, addr_len));
- GPR_ASSERT(0 == listen(svr_fd, 1));
-
- /* connect to it */
- GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)&addr, &addr_len) == 0);
- grpc_tcp_client_connect(must_succeed, &ev, &em, (struct sockaddr *)&addr,
- addr_len, gpr_inf_future);
-
- /* await the connection */
- do {
- addr_len = sizeof(addr);
- r = accept(svr_fd, (struct sockaddr *)&addr, &addr_len);
- } while (r == -1 && errno == EINTR);
- GPR_ASSERT(r >= 0);
- close(r);
-
- /* wait for the connection callback to finish */
- GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
-}
-
-void test_fails() {
- struct sockaddr_in addr;
- socklen_t addr_len = sizeof(addr);
- gpr_event ev;
-
- gpr_event_init(&ev);
-
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
-
- /* connect to a broken address */
- grpc_tcp_client_connect(must_fail, &ev, &em, (struct sockaddr *)&addr,
- addr_len, gpr_inf_future);
-
- /* wait for the connection callback to finish */
- GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
-}
-
-void test_times_out() {
- struct sockaddr_in addr;
- socklen_t addr_len = sizeof(addr);
- int svr_fd;
-#define NUM_CLIENT_CONNECTS 10
- int client_fd[NUM_CLIENT_CONNECTS];
- int i;
- int r;
- gpr_event ev;
- gpr_timespec connect_deadline;
-
- gpr_event_init(&ev);
-
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
-
- /* create a dummy server */
- svr_fd = socket(AF_INET, SOCK_STREAM, 0);
- GPR_ASSERT(svr_fd >= 0);
- GPR_ASSERT(0 == bind(svr_fd, (struct sockaddr *)&addr, addr_len));
- GPR_ASSERT(0 == listen(svr_fd, 1));
- /* Get its address */
- GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)&addr, &addr_len) == 0);
-
- /* tie up the listen buffer, which is somewhat arbitrarily sized. */
- for (i = 0; i < NUM_CLIENT_CONNECTS; ++i) {
- client_fd[i] = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
- do {
- r = connect(client_fd[i], (struct sockaddr *)&addr, addr_len);
- } while (r == -1 && errno == EINTR);
- GPR_ASSERT(r < 0);
- GPR_ASSERT(errno == EWOULDBLOCK || errno == EINPROGRESS);
- }
-
- /* connect to dummy server address */
-
- connect_deadline = gpr_time_add(gpr_now(), gpr_time_from_micros(1000000));
-
- grpc_tcp_client_connect(must_fail, &ev, &em, (struct sockaddr *)&addr,
- addr_len, connect_deadline);
- /* Make sure the event doesn't trigger early */
- GPR_ASSERT(!gpr_event_wait(
- &ev, gpr_time_add(gpr_now(), gpr_time_from_micros(500000))));
- /* Now wait until it should have triggered */
- sleep(1);
-
- /* wait for the connection callback to finish */
- GPR_ASSERT(gpr_event_wait(&ev, test_deadline()));
- close(svr_fd);
- for (i = 0; i < NUM_CLIENT_CONNECTS; ++i) {
- close(client_fd[i]);
- }
-}
-
-int main(void) {
- grpc_em_init(&em);
- test_succeeds();
- test_fails();
- test_times_out();
- return 0;
-}