From 4aaba75a822c6dde718d7fe646f7671b4f64d8a0 Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Thu, 2 Jun 2016 17:11:46 -0700 Subject: initial implementation of resource handling --- test/core/census/README | 7 ++ test/core/census/data/resource_empty_name.pb | 1 + test/core/census/data/resource_empty_name.txt | 5 + test/core/census/data/resource_full.pb | 2 + test/core/census/data/resource_full.txt | 9 ++ test/core/census/data/resource_minimal_good.pb | 2 + test/core/census/data/resource_minimal_good.txt | 5 + test/core/census/data/resource_no_name.pb | 1 + test/core/census/data/resource_no_name.txt | 4 + test/core/census/data/resource_no_numerator.pb | 2 + test/core/census/data/resource_no_numerator.txt | 6 + test/core/census/data/resource_no_unit.pb | 2 + test/core/census/data/resource_no_unit.txt | 2 + test/core/census/resource_test.c | 157 ++++++++++++++++++++++++ 14 files changed, 205 insertions(+) create mode 100644 test/core/census/README create mode 100644 test/core/census/data/resource_empty_name.pb create mode 100644 test/core/census/data/resource_empty_name.txt create mode 100644 test/core/census/data/resource_full.pb create mode 100644 test/core/census/data/resource_full.txt create mode 100644 test/core/census/data/resource_minimal_good.pb create mode 100644 test/core/census/data/resource_minimal_good.txt create mode 100644 test/core/census/data/resource_no_name.pb create mode 100644 test/core/census/data/resource_no_name.txt create mode 100644 test/core/census/data/resource_no_numerator.pb create mode 100644 test/core/census/data/resource_no_numerator.txt create mode 100644 test/core/census/data/resource_no_unit.pb create mode 100644 test/core/census/data/resource_no_unit.txt create mode 100644 test/core/census/resource_test.c (limited to 'test/core/census') diff --git a/test/core/census/README b/test/core/census/README new file mode 100644 index 0000000000..d5363b7233 --- /dev/null +++ b/test/core/census/README @@ -0,0 +1,7 @@ +Test source and data files for Census. + +binary proto files (*.pb) in data directory are generated from the *.txt file, +via: + +BASE="filename" +cat $BASE.txt | protoc --encode=google.census.Resource census.proto > $BASE.pb diff --git a/test/core/census/data/resource_empty_name.pb b/test/core/census/data/resource_empty_name.pb new file mode 100644 index 0000000000..4d547445fa --- /dev/null +++ b/test/core/census/data/resource_empty_name.pb @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/core/census/data/resource_empty_name.txt b/test/core/census/data/resource_empty_name.txt new file mode 100644 index 0000000000..271fd3274c --- /dev/null +++ b/test/core/census/data/resource_empty_name.txt @@ -0,0 +1,5 @@ +# Name is present, but empty. +name : '' +unit { + numerator : SECS +} diff --git a/test/core/census/data/resource_full.pb b/test/core/census/data/resource_full.pb new file mode 100644 index 0000000000..e4c6a2aef5 --- /dev/null +++ b/test/core/census/data/resource_full.pb @@ -0,0 +1,2 @@ + + full_resource"A resource with everything defined \ No newline at end of file diff --git a/test/core/census/data/resource_full.txt b/test/core/census/data/resource_full.txt new file mode 100644 index 0000000000..1aa2fafe3a --- /dev/null +++ b/test/core/census/data/resource_full.txt @@ -0,0 +1,9 @@ +# A full resource definition - all fields filled out. +name : 'full_resource' +description : 'A resource with everything defined' +unit { + # Megabits per second. + prefix : 6 + numerator : BITS + denominator : SECS +} diff --git a/test/core/census/data/resource_minimal_good.pb b/test/core/census/data/resource_minimal_good.pb new file mode 100644 index 0000000000..7100c462bf --- /dev/null +++ b/test/core/census/data/resource_minimal_good.pb @@ -0,0 +1,2 @@ + + minimal_good \ No newline at end of file diff --git a/test/core/census/data/resource_minimal_good.txt b/test/core/census/data/resource_minimal_good.txt new file mode 100644 index 0000000000..a7a7e71dd6 --- /dev/null +++ b/test/core/census/data/resource_minimal_good.txt @@ -0,0 +1,5 @@ +# A minimal "good" Resource definition: has a name and numerator/unit. +name : 'minimal_good' +unit { + numerator : SECS +} diff --git a/test/core/census/data/resource_no_name.pb b/test/core/census/data/resource_no_name.pb new file mode 100644 index 0000000000..4d547445fa --- /dev/null +++ b/test/core/census/data/resource_no_name.pb @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/core/census/data/resource_no_name.txt b/test/core/census/data/resource_no_name.txt new file mode 100644 index 0000000000..8f12a91d35 --- /dev/null +++ b/test/core/census/data/resource_no_name.txt @@ -0,0 +1,4 @@ +# The minimal good Resource without a name. +unit { + numerator : SECS +} diff --git a/test/core/census/data/resource_no_numerator.pb b/test/core/census/data/resource_no_numerator.pb new file mode 100644 index 0000000000..2a5cceee70 --- /dev/null +++ b/test/core/census/data/resource_no_numerator.pb @@ -0,0 +1,2 @@ + +resource_no_numeratorýÿÿÿÿÿÿÿÿ \ No newline at end of file diff --git a/test/core/census/data/resource_no_numerator.txt b/test/core/census/data/resource_no_numerator.txt new file mode 100644 index 0000000000..fc1fec74a2 --- /dev/null +++ b/test/core/census/data/resource_no_numerator.txt @@ -0,0 +1,6 @@ +# Resource without a numerator +name : 'resource_no_numerator' +unit { + prefix : -3 + denominator : SECS +} diff --git a/test/core/census/data/resource_no_unit.pb b/test/core/census/data/resource_no_unit.pb new file mode 100644 index 0000000000..9dca2620e0 --- /dev/null +++ b/test/core/census/data/resource_no_unit.pb @@ -0,0 +1,2 @@ + +resource_no_unit \ No newline at end of file diff --git a/test/core/census/data/resource_no_unit.txt b/test/core/census/data/resource_no_unit.txt new file mode 100644 index 0000000000..c5d5115ceb --- /dev/null +++ b/test/core/census/data/resource_no_unit.txt @@ -0,0 +1,2 @@ +# The minimal good resource without a unit +name : 'resource_no_unit' diff --git a/test/core/census/resource_test.c b/test/core/census/resource_test.c new file mode 100644 index 0000000000..bfb76bcfb5 --- /dev/null +++ b/test/core/census/resource_test.c @@ -0,0 +1,157 @@ +/* + * + * Copyright 2016, 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/ext/census/resource.h" +#include +#include +#include +#include +#include +#include +#include +#include "src/core/ext/census/base_resources.h" +#include "test/core/util/test_config.h" + +// Test all the functionality for dealing with Resources. + +// Just startup and shutdown resources subsystem. +static void test_enable_disable() { + initialize_resources(); + shutdown_resources(); +} + +// A blank/empty initialization should not work. +static void test_empty_definition() { + initialize_resources(); + int32_t rid = census_define_resource(NULL, 0); + GPR_ASSERT(rid == -1); + uint8_t buffer[50] = {0}; + rid = census_define_resource(buffer, 50); + GPR_ASSERT(rid == -1); + shutdown_resources(); +} + +// Given a file name, read raw proto and define the resource included within. +// Returns resource id from census_define_resource(). +static int32_t define_resource_from_file(const char *file) { + const size_t buf_size = 512; + uint8_t buffer[buf_size]; + FILE *input = fopen(file, "r"); + GPR_ASSERT(input != NULL); + size_t nbytes = fread(buffer, 1, buf_size, input); + GPR_ASSERT(nbytes != 0 && nbytes < buf_size); + int32_t rid = census_define_resource(buffer, nbytes); + GPR_ASSERT(fclose(input) == 0); + return rid; +} + +// Test definition of a single resource, using a proto read from a file. The +// `succeed` parameter indicates whether we expect the definition to succeed or +// fail. `name` is used to check that the returned resource can be looked up by +// name. +static void test_define_single_resource(const char *file, const char *name, + bool succeed) { + gpr_log(GPR_INFO, "Test defining resource \"%s\"\n", name); + initialize_resources(); + int32_t rid = define_resource_from_file(file); + if (succeed) { + GPR_ASSERT(rid >= 0); + int32_t rid2 = census_resource_id(name); + GPR_ASSERT(rid == rid2); + } else { + GPR_ASSERT(rid < 0); + } + shutdown_resources(); +} + +// Try deleting various resources (both those that exist and those that don't). +static void test_delete_resource() { + initialize_resources(); + // Try deleting resource before any are defined. + census_delete_resource(0); + // Create and check a couple of resources. + int32_t rid1 = define_resource_from_file( + "test/core/census/data/resource_minimal_good.pb"); + int32_t rid2 = + define_resource_from_file("test/core/census/data/resource_full.pb"); + GPR_ASSERT(rid1 >= 0 && rid2 >= 0 && rid1 != rid2); + int32_t rid3 = census_resource_id("minimal_good"); + int32_t rid4 = census_resource_id("full_resource"); + GPR_ASSERT(rid1 == rid3 && rid2 == rid4); + // Try deleting non-existant resources. + census_delete_resource(-1); + census_delete_resource(rid1 + rid2 + 1); + census_delete_resource(10000000); + // Delete one of the previously defined resources and check for deletion. + census_delete_resource(rid1); + rid3 = census_resource_id("minimal_good"); + GPR_ASSERT(rid3 < 0); + // Check that re-adding works. + rid1 = define_resource_from_file( + "test/core/census/data/resource_minimal_good.pb"); + GPR_ASSERT(rid1 >= 0); + rid3 = census_resource_id("minimal_good"); + GPR_ASSERT(rid1 == rid3); + shutdown_resources(); +} + +// Test define base resources. +static void test_base_resources() { + initialize_resources(); + define_base_resources(); + int32_t rid1 = census_resource_id("client_rpc_latency"); + int32_t rid2 = census_resource_id("server_rpc_latency"); + GPR_ASSERT(rid1 >= 0 && rid2 >= 0 && rid1 != rid2); + shutdown_resources(); +} + +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_enable_disable(); + test_empty_definition(); + test_define_single_resource("test/core/census/data/resource_minimal_good.pb", + "minimal_good", true); + test_define_single_resource("test/core/census/data/resource_full.pb", + "full_resource", true); + test_define_single_resource("test/core/census/data/resource_no_name.pb", + "resource_no_name", false); + test_define_single_resource("test/core/census/data/resource_no_numerator.pb", + "resource_no_numerator", false); + test_define_single_resource("test/core/census/data/resource_no_unit.pb", + "resource_no_unit", false); + test_define_single_resource("test/core/census/data/resource_empty_name.pb", + "resource_empty_name", false); + test_delete_resource(); + test_base_resources(); + return 0; +} -- cgit v1.2.3 From 7a97fe2c10e5b55c5f28f7031c2c67fc47581e09 Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Fri, 3 Jun 2016 12:10:45 -0700 Subject: use macro rather than constant in array size --- test/core/census/resource_test.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test/core/census') diff --git a/test/core/census/resource_test.c b/test/core/census/resource_test.c index bfb76bcfb5..1375d56a6b 100644 --- a/test/core/census/resource_test.c +++ b/test/core/census/resource_test.c @@ -64,12 +64,12 @@ static void test_empty_definition() { // Given a file name, read raw proto and define the resource included within. // Returns resource id from census_define_resource(). static int32_t define_resource_from_file(const char *file) { - const size_t buf_size = 512; - uint8_t buffer[buf_size]; +#define BUF_SIZE 512 + uint8_t buffer[BUF_SIZE]; FILE *input = fopen(file, "r"); GPR_ASSERT(input != NULL); - size_t nbytes = fread(buffer, 1, buf_size, input); - GPR_ASSERT(nbytes != 0 && nbytes < buf_size); + size_t nbytes = fread(buffer, 1, BUF_SIZE, input); + GPR_ASSERT(nbytes != 0 && nbytes < BUF_SIZE); int32_t rid = census_define_resource(buffer, nbytes); GPR_ASSERT(fclose(input) == 0); return rid; -- cgit v1.2.3 From 8c7618e88d6a272e17fc67072f6700e0dc4080ab Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Tue, 26 Jul 2016 10:30:36 -0700 Subject: Fix memory overwrite in proto decding; fix test for windows --- src/core/ext/census/resource.c | 6 ++++-- test/core/census/resource_test.c | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'test/core/census') diff --git a/src/core/ext/census/resource.c b/src/core/ext/census/resource.c index 905de2d63d..63bfb75b47 100644 --- a/src/core/ext/census/resource.c +++ b/src/core/ext/census/resource.c @@ -156,9 +156,11 @@ static bool validate_units_helper(pb_istream_t *stream, int *count, gpr_free(*bup); } *bup = new_bup; - if (!pb_decode_varint(stream, (uint64_t *)(*bup + *count - 1))) { + uint64_t value; + if (!pb_decode_varint(stream, &value)) { return false; } + *(*bup + *count - 1) = (google_census_Resource_BasicUnit)value; } return true; } @@ -290,7 +292,7 @@ int32_t define_resource(const resource *base) { resources[id]->name = gpr_malloc(len); memcpy(resources[id]->name, base->name, len); if (base->description) { - len = strlen(base->description); + len = strlen(base->description) + 1; resources[id]->description = gpr_malloc(len); memcpy(resources[id]->description, base->description, len); } diff --git a/test/core/census/resource_test.c b/test/core/census/resource_test.c index 1375d56a6b..e1556d7d7b 100644 --- a/test/core/census/resource_test.c +++ b/test/core/census/resource_test.c @@ -66,10 +66,10 @@ static void test_empty_definition() { static int32_t define_resource_from_file(const char *file) { #define BUF_SIZE 512 uint8_t buffer[BUF_SIZE]; - FILE *input = fopen(file, "r"); + FILE *input = fopen(file, "rb"); GPR_ASSERT(input != NULL); size_t nbytes = fread(buffer, 1, BUF_SIZE, input); - GPR_ASSERT(nbytes != 0 && nbytes < BUF_SIZE); + GPR_ASSERT(nbytes != 0 && nbytes < BUF_SIZE && feof(input) && !ferror(input)); int32_t rid = census_define_resource(buffer, nbytes); GPR_ASSERT(fclose(input) == 0); return rid; -- cgit v1.2.3 From 3ed00635f91dad88788b99b2e06d9d7e647fb9d5 Mon Sep 17 00:00:00 2001 From: Ken Payson Date: Thu, 11 Aug 2016 08:46:39 -0700 Subject: Add optional resource parameters to census resource test --- test/core/census/resource_test.c | 47 ++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 19 deletions(-) (limited to 'test/core/census') diff --git a/test/core/census/resource_test.c b/test/core/census/resource_test.c index e1556d7d7b..cfe4f037fd 100644 --- a/test/core/census/resource_test.c +++ b/test/core/census/resource_test.c @@ -95,15 +95,13 @@ static void test_define_single_resource(const char *file, const char *name, } // Try deleting various resources (both those that exist and those that don't). -static void test_delete_resource() { +static void test_delete_resource(char* minimal_good, char* full) { initialize_resources(); // Try deleting resource before any are defined. census_delete_resource(0); // Create and check a couple of resources. - int32_t rid1 = define_resource_from_file( - "test/core/census/data/resource_minimal_good.pb"); - int32_t rid2 = - define_resource_from_file("test/core/census/data/resource_full.pb"); + int32_t rid1 = define_resource_from_file(minimal_good); + int32_t rid2 = define_resource_from_file(full); GPR_ASSERT(rid1 >= 0 && rid2 >= 0 && rid1 != rid2); int32_t rid3 = census_resource_id("minimal_good"); int32_t rid4 = census_resource_id("full_resource"); @@ -117,8 +115,7 @@ static void test_delete_resource() { rid3 = census_resource_id("minimal_good"); GPR_ASSERT(rid3 < 0); // Check that re-adding works. - rid1 = define_resource_from_file( - "test/core/census/data/resource_minimal_good.pb"); + rid1 = define_resource_from_file(minimal_good); GPR_ASSERT(rid1 >= 0); rid3 = census_resource_id("minimal_good"); GPR_ASSERT(rid1 == rid3); @@ -136,22 +133,34 @@ static void test_base_resources() { } int main(int argc, char **argv) { + char *resource_empty_name_pb, *resource_full_pb, *resource_minimal_good_pb, + *resource_no_name_pb, *resource_no_numerator_pb, *resource_no_unit_pb; + if (argc >= 7) { + resource_empty_name_pb = argv[1]; + resource_full_pb = argv[2]; + resource_minimal_good_pb = argv[3]; + resource_no_name_pb = argv[4]; + resource_no_numerator_pb = argv[5]; + resource_no_unit_pb = argv[6]; + } else { + resource_empty_name_pb = "test/core/census/data/resource_empty_name.pb"; + resource_full_pb = "test/core/census/data/resource_full.pb"; + resource_minimal_good_pb = "test/core/census/data/resource_minimal_good.pb"; + resource_no_name_pb = "test/core/census/data/resource_no_name.pb"; + resource_no_numerator_pb = "test/core/census/data/resource_no_numerator.pb"; + resource_no_unit_pb = "test/core/census/data/resource_no_unit.pb"; + } grpc_test_init(argc, argv); test_enable_disable(); test_empty_definition(); - test_define_single_resource("test/core/census/data/resource_minimal_good.pb", - "minimal_good", true); - test_define_single_resource("test/core/census/data/resource_full.pb", - "full_resource", true); - test_define_single_resource("test/core/census/data/resource_no_name.pb", - "resource_no_name", false); - test_define_single_resource("test/core/census/data/resource_no_numerator.pb", + test_define_single_resource(resource_minimal_good_pb, "minimal_good", true); + test_define_single_resource(resource_full_pb, "full_resource", true); + test_define_single_resource(resource_no_name_pb, "resource_no_name", false); + test_define_single_resource(resource_no_numerator_pb, "resource_no_numerator", false); - test_define_single_resource("test/core/census/data/resource_no_unit.pb", - "resource_no_unit", false); - test_define_single_resource("test/core/census/data/resource_empty_name.pb", - "resource_empty_name", false); - test_delete_resource(); + test_define_single_resource(resource_no_unit_pb, "resource_no_unit", false); + test_define_single_resource(resource_empty_name_pb, "resource_empty_name", false); + test_delete_resource(resource_minimal_good_pb, resource_full_pb); test_base_resources(); return 0; } -- cgit v1.2.3 From a6402a9d43410057100d90b0498371790c6052e4 Mon Sep 17 00:00:00 2001 From: Ken Payson Date: Thu, 11 Aug 2016 10:09:37 -0700 Subject: Made strings constant --- test/core/census/resource_test.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'test/core/census') diff --git a/test/core/census/resource_test.c b/test/core/census/resource_test.c index cfe4f037fd..8f5a4387d1 100644 --- a/test/core/census/resource_test.c +++ b/test/core/census/resource_test.c @@ -95,7 +95,7 @@ static void test_define_single_resource(const char *file, const char *name, } // Try deleting various resources (both those that exist and those that don't). -static void test_delete_resource(char* minimal_good, char* full) { +static void test_delete_resource(const char *minimal_good, const char *full) { initialize_resources(); // Try deleting resource before any are defined. census_delete_resource(0); @@ -133,9 +133,9 @@ static void test_base_resources() { } int main(int argc, char **argv) { - char *resource_empty_name_pb, *resource_full_pb, *resource_minimal_good_pb, + const char *resource_empty_name_pb, *resource_full_pb, *resource_minimal_good_pb, *resource_no_name_pb, *resource_no_numerator_pb, *resource_no_unit_pb; - if (argc >= 7) { + if (argc == 7) { resource_empty_name_pb = argv[1]; resource_full_pb = argv[2]; resource_minimal_good_pb = argv[3]; @@ -143,6 +143,7 @@ int main(int argc, char **argv) { resource_no_numerator_pb = argv[5]; resource_no_unit_pb = argv[6]; } else { + GPR_ASSERT(argc == 1); resource_empty_name_pb = "test/core/census/data/resource_empty_name.pb"; resource_full_pb = "test/core/census/data/resource_full.pb"; resource_minimal_good_pb = "test/core/census/data/resource_minimal_good.pb"; -- cgit v1.2.3 From 530284269a31568fab04c200992857c7c52c6b56 Mon Sep 17 00:00:00 2001 From: Ken Payson Date: Thu, 11 Aug 2016 11:21:30 -0700 Subject: Clang format --- test/core/census/resource_test.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'test/core/census') diff --git a/test/core/census/resource_test.c b/test/core/census/resource_test.c index 8f5a4387d1..f0e7039615 100644 --- a/test/core/census/resource_test.c +++ b/test/core/census/resource_test.c @@ -133,8 +133,9 @@ static void test_base_resources() { } int main(int argc, char **argv) { - const char *resource_empty_name_pb, *resource_full_pb, *resource_minimal_good_pb, - *resource_no_name_pb, *resource_no_numerator_pb, *resource_no_unit_pb; + const char *resource_empty_name_pb, *resource_full_pb, + *resource_minimal_good_pb, *resource_no_name_pb, + *resource_no_numerator_pb, *resource_no_unit_pb; if (argc == 7) { resource_empty_name_pb = argv[1]; resource_full_pb = argv[2]; @@ -157,10 +158,11 @@ int main(int argc, char **argv) { test_define_single_resource(resource_minimal_good_pb, "minimal_good", true); test_define_single_resource(resource_full_pb, "full_resource", true); test_define_single_resource(resource_no_name_pb, "resource_no_name", false); - test_define_single_resource(resource_no_numerator_pb, - "resource_no_numerator", false); + test_define_single_resource(resource_no_numerator_pb, "resource_no_numerator", + false); test_define_single_resource(resource_no_unit_pb, "resource_no_unit", false); - test_define_single_resource(resource_empty_name_pb, "resource_empty_name", false); + test_define_single_resource(resource_empty_name_pb, "resource_empty_name", + false); test_delete_resource(resource_minimal_good_pb, resource_full_pb); test_base_resources(); return 0; -- cgit v1.2.3