From 2449e193bb42ffbcfc163cae43aacd0f4a777ab7 Mon Sep 17 00:00:00 2001 From: Robbie Shade Date: Fri, 15 Jul 2016 15:16:25 -0400 Subject: [PATCH 1/5] Extends the payload test to use large random bodies. --- test/core/end2end/cq_verifier.c | 2 +- test/core/end2end/cq_verifier.h | 1 + test/core/end2end/tests/payload.c | 27 +++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/test/core/end2end/cq_verifier.c b/test/core/end2end/cq_verifier.c index 890309c44a8..03a14755a86 100644 --- a/test/core/end2end/cq_verifier.c +++ b/test/core/end2end/cq_verifier.c @@ -128,7 +128,7 @@ static gpr_slice merge_slices(gpr_slice *slices, size_t nslices) { return out; } -static int byte_buffer_eq_slice(grpc_byte_buffer *bb, gpr_slice b) { +int byte_buffer_eq_slice(grpc_byte_buffer *bb, gpr_slice b) { gpr_slice a; int ok; diff --git a/test/core/end2end/cq_verifier.h b/test/core/end2end/cq_verifier.h index 8c9a85c2187..3bf2141438a 100644 --- a/test/core/end2end/cq_verifier.h +++ b/test/core/end2end/cq_verifier.h @@ -61,6 +61,7 @@ void cq_verify_empty(cq_verifier *v); the event. */ void cq_expect_completion(cq_verifier *v, void *tag, bool success); +int byte_buffer_eq_slice(grpc_byte_buffer *bb, gpr_slice b); int byte_buffer_eq_string(grpc_byte_buffer *byte_buffer, const char *string); int contains_metadata(grpc_metadata_array *array, const char *key, const char *value); diff --git a/test/core/end2end/tests/payload.c b/test/core/end2end/tests/payload.c index 443d85eecc4..3cb3f2e53a9 100644 --- a/test/core/end2end/tests/payload.c +++ b/test/core/end2end/tests/payload.c @@ -97,9 +97,27 @@ static void end_test(grpc_end2end_test_fixture *f) { grpc_completion_queue_destroy(f->cq); } +/* Creates and returns a gpr_slice of specified length, containing random + * alphanumeric characters. */ +static gpr_slice generate_random_slice(int length_bytes) { + int i; + gpr_slice slice = gpr_slice_malloc(length_bytes); + static const char alphanum[] = "abcdefghijklmnopqrstuvwxyz01234567890"; + for (i = 0; i < length_bytes; ++i) { + *(GPR_SLICE_START_PTR(slice) + i) = + alphanum[rand() % (sizeof(alphanum) - 1)]; + } + return slice; +} + static void request_response_with_payload(grpc_end2end_test_fixture f) { - gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); - gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); + /* Create large request and response bodies. These are big enough to require + * multiple round trips to deliver to the peer, and their exact contents of + * will be verified on completion. */ + int payload_size_bytes = 1024 * 1024; /* 1 MB */ + gpr_slice request_payload_slice = generate_random_slice(payload_size_bytes); + gpr_slice response_payload_slice = generate_random_slice(payload_size_bytes); + grpc_call *c; grpc_call *s; grpc_byte_buffer *request_payload = @@ -224,8 +242,9 @@ static void request_response_with_payload(grpc_end2end_test_fixture f) { GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); GPR_ASSERT(was_cancelled == 0); - GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); - GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you")); + GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice)); + GPR_ASSERT( + byte_buffer_eq_slice(response_payload_recv, response_payload_slice)); gpr_free(details); grpc_metadata_array_destroy(&initial_metadata_recv); From bd10faaf0f4284576b628ef2b4da5edeac9f6454 Mon Sep 17 00:00:00 2001 From: Robbie Shade Date: Fri, 15 Jul 2016 15:27:01 -0400 Subject: [PATCH 2/5] Fixing int conversion warnings --- test/core/end2end/tests/payload.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/core/end2end/tests/payload.c b/test/core/end2end/tests/payload.c index 3cb3f2e53a9..0a68a01730a 100644 --- a/test/core/end2end/tests/payload.c +++ b/test/core/end2end/tests/payload.c @@ -99,13 +99,13 @@ static void end_test(grpc_end2end_test_fixture *f) { /* Creates and returns a gpr_slice of specified length, containing random * alphanumeric characters. */ -static gpr_slice generate_random_slice(int length_bytes) { - int i; +static gpr_slice generate_random_slice(size_t length_bytes) { + size_t i; gpr_slice slice = gpr_slice_malloc(length_bytes); - static const char alphanum[] = "abcdefghijklmnopqrstuvwxyz01234567890"; + static const uint8_t alphanum[] = "abcdefghijklmnopqrstuvwxyz01234567890"; for (i = 0; i < length_bytes; ++i) { *(GPR_SLICE_START_PTR(slice) + i) = - alphanum[rand() % (sizeof(alphanum) - 1)]; + alphanum[rand() % (int)(sizeof(alphanum) - 1)]; } return slice; } @@ -114,7 +114,7 @@ static void request_response_with_payload(grpc_end2end_test_fixture f) { /* Create large request and response bodies. These are big enough to require * multiple round trips to deliver to the peer, and their exact contents of * will be verified on completion. */ - int payload_size_bytes = 1024 * 1024; /* 1 MB */ + size_t payload_size_bytes = 1024 * 1024; /* 1 MB */ gpr_slice request_payload_slice = generate_random_slice(payload_size_bytes); gpr_slice response_payload_slice = generate_random_slice(payload_size_bytes); From 61aed92b8262b3a7fa86394cc6b89987ada85228 Mon Sep 17 00:00:00 2001 From: Robbie Shade Date: Fri, 15 Jul 2016 16:38:44 -0400 Subject: [PATCH 3/5] Just create 1 MB strings, no argument. --- test/core/end2end/tests/payload.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/core/end2end/tests/payload.c b/test/core/end2end/tests/payload.c index 0a68a01730a..d530443fb81 100644 --- a/test/core/end2end/tests/payload.c +++ b/test/core/end2end/tests/payload.c @@ -97,26 +97,23 @@ static void end_test(grpc_end2end_test_fixture *f) { grpc_completion_queue_destroy(f->cq); } -/* Creates and returns a gpr_slice of specified length, containing random - * alphanumeric characters. */ -static gpr_slice generate_random_slice(size_t length_bytes) { +/* Creates and returns a gpr_slice containing random alphanumeric characters. */ +static gpr_slice generate_random_slice() { size_t i; - gpr_slice slice = gpr_slice_malloc(length_bytes); - static const uint8_t alphanum[] = "abcdefghijklmnopqrstuvwxyz01234567890"; - for (i = 0; i < length_bytes; ++i) { - *(GPR_SLICE_START_PTR(slice) + i) = - alphanum[rand() % (int)(sizeof(alphanum) - 1)]; + static const char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890"; + char output[1024 * 1024]; /* 1 MB */ + for (i = 0; i < 1024 * 1024; ++i) { + output[i] = chars[rand() % (int)(sizeof(chars) - 1)]; } - return slice; + return gpr_slice_from_copied_string(output); } static void request_response_with_payload(grpc_end2end_test_fixture f) { /* Create large request and response bodies. These are big enough to require * multiple round trips to deliver to the peer, and their exact contents of * will be verified on completion. */ - size_t payload_size_bytes = 1024 * 1024; /* 1 MB */ - gpr_slice request_payload_slice = generate_random_slice(payload_size_bytes); - gpr_slice response_payload_slice = generate_random_slice(payload_size_bytes); + gpr_slice request_payload_slice = generate_random_slice(); + gpr_slice response_payload_slice = generate_random_slice(); grpc_call *c; grpc_call *s; From c6f776752d29bc471818258b172602d5e08b6402 Mon Sep 17 00:00:00 2001 From: Robbie Shade Date: Fri, 15 Jul 2016 17:08:08 -0400 Subject: [PATCH 4/5] Explicitly add terminating null character. --- test/core/end2end/tests/payload.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/core/end2end/tests/payload.c b/test/core/end2end/tests/payload.c index d530443fb81..fa6a6ea2588 100644 --- a/test/core/end2end/tests/payload.c +++ b/test/core/end2end/tests/payload.c @@ -102,9 +102,10 @@ static gpr_slice generate_random_slice() { size_t i; static const char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890"; char output[1024 * 1024]; /* 1 MB */ - for (i = 0; i < 1024 * 1024; ++i) { + for (i = 0; i < 1024 * 1024 - 1; ++i) { output[i] = chars[rand() % (int)(sizeof(chars) - 1)]; } + output[1024 * 1024 - 1] = '\0'; return gpr_slice_from_copied_string(output); } From d5d851baf3dbdf92b1c1946c6c46334e6d095b3d Mon Sep 17 00:00:00 2001 From: Robbie Shade Date: Fri, 15 Jul 2016 17:16:00 -0400 Subject: [PATCH 5/5] Add byte_buffer_eq_slice method --- test/core/end2end/cq_verifier.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/test/core/end2end/cq_verifier.c b/test/core/end2end/cq_verifier.c index 03a14755a86..74da9bc406a 100644 --- a/test/core/end2end/cq_verifier.c +++ b/test/core/end2end/cq_verifier.c @@ -128,14 +128,14 @@ static gpr_slice merge_slices(gpr_slice *slices, size_t nslices) { return out; } -int byte_buffer_eq_slice(grpc_byte_buffer *bb, gpr_slice b) { +int raw_byte_buffer_eq_slice(grpc_byte_buffer *rbb, gpr_slice b) { gpr_slice a; int ok; - if (!bb) return 0; + if (!rbb) return 0; - a = merge_slices(bb->data.raw.slice_buffer.slices, - bb->data.raw.slice_buffer.count); + a = merge_slices(rbb->data.raw.slice_buffer.slices, + rbb->data.raw.slice_buffer.count); ok = GPR_SLICE_LENGTH(a) == GPR_SLICE_LENGTH(b) && 0 == memcmp(GPR_SLICE_START_PTR(a), GPR_SLICE_START_PTR(b), GPR_SLICE_LENGTH(a)); @@ -144,6 +144,21 @@ int byte_buffer_eq_slice(grpc_byte_buffer *bb, gpr_slice b) { return ok; } +int byte_buffer_eq_slice(grpc_byte_buffer *bb, gpr_slice b) { + grpc_byte_buffer_reader reader; + grpc_byte_buffer *rbb; + int res; + + GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) && + "Couldn't init byte buffer reader"); + rbb = grpc_raw_byte_buffer_from_reader(&reader); + res = raw_byte_buffer_eq_slice(rbb, b); + grpc_byte_buffer_reader_destroy(&reader); + grpc_byte_buffer_destroy(rbb); + + return res; +} + int byte_buffer_eq_string(grpc_byte_buffer *bb, const char *str) { grpc_byte_buffer_reader reader; grpc_byte_buffer *rbb; @@ -152,7 +167,7 @@ int byte_buffer_eq_string(grpc_byte_buffer *bb, const char *str) { GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) && "Couldn't init byte buffer reader"); rbb = grpc_raw_byte_buffer_from_reader(&reader); - res = byte_buffer_eq_slice(rbb, gpr_slice_from_copied_string(str)); + res = raw_byte_buffer_eq_slice(rbb, gpr_slice_from_copied_string(str)); grpc_byte_buffer_reader_destroy(&reader); grpc_byte_buffer_destroy(rbb);