From d2959ca612c8661d245fafbfb937b47f11ee73ed Mon Sep 17 00:00:00 2001 From: James Buckland Date: Wed, 11 Jul 2018 16:54:41 -0400 Subject: [PATCH 1/2] Add test for consistency of RawStatData internal memory representation Signed-off-by: James Buckland --- test/common/stats/BUILD | 1 + test/common/stats/stats_impl_test.cc | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/test/common/stats/BUILD b/test/common/stats/BUILD index dfd1984f27290..b3cc4299407ea 100644 --- a/test/common/stats/BUILD +++ b/test/common/stats/BUILD @@ -12,6 +12,7 @@ envoy_cc_test( name = "stats_impl_test", srcs = ["stats_impl_test.cc"], deps = [ + "//source/common/common:hex_lib", "//source/common/stats:stats_lib", "//test/mocks/stats:stats_mocks", "//test/test_common:logging_lib", diff --git a/test/common/stats/stats_impl_test.cc b/test/common/stats/stats_impl_test.cc index 4d595c1b73ece..40403c1770a69 100644 --- a/test/common/stats/stats_impl_test.cc +++ b/test/common/stats/stats_impl_test.cc @@ -5,6 +5,7 @@ #include "envoy/config/metrics/v2/stats.pb.h" #include "envoy/stats/stats_macros.h" +#include "common/common/hex.h" #include "common/config/well_known_names.h" #include "common/stats/stats_impl.h" @@ -477,6 +478,32 @@ TEST(TagProducerTest, CheckConstructor) { "No regex specified for tag specifier and no default regex for name: 'test_extractor'"); } +// Check consistency of internal stat representation +TEST(RawStatDataTest, Consistency) { + HeapRawStatDataAllocator alloc; + // Generate a stat, encode it to hex, and take the hash of that hex string. We expect the hash to + // vary only when the internal representation of a stat has been intentionally changed, in which + // case SharedMemory::Version should be incremented as well. + uint64_t expected_hash = 1874506077228772558; + uint64_t max_name_length = RawStatData::maxNameLength(); + + const std::string name_1(max_name_length, 'A'); + RawStatData* stat_1 = alloc.alloc(name_1); + std::string stat_hex_dump_1 = + Hex::encode(reinterpret_cast(stat_1), sizeof(RawStatData) + max_name_length); + EXPECT_EQ(HashUtil::xxHash64(stat_hex_dump_1), expected_hash); + alloc.free(*stat_1); + + // If a stat name is truncated, we expect that its internal representation is the same as if it + // had been initialized with the already-truncated name. + const std::string name_2(max_name_length + 1, 'A'); + RawStatData* stat_2 = alloc.alloc(name_2); + std::string stat_hex_dump_2 = + Hex::encode(reinterpret_cast(stat_2), sizeof(RawStatData) + max_name_length); + EXPECT_EQ(HashUtil::xxHash64(stat_hex_dump_2), expected_hash); + alloc.free(*stat_2); +} + // Validate truncation behavior of RawStatData. TEST(RawStatDataTest, Truncate) { HeapRawStatDataAllocator alloc; From b9c30594e2ab9f9f9cc815ce99e6c20ab6c1ccad Mon Sep 17 00:00:00 2001 From: James Buckland Date: Wed, 11 Jul 2018 16:56:55 -0400 Subject: [PATCH 2/2] Fix capitalization of SharedMemory::VERSION Signed-off-by: James Buckland --- test/common/stats/stats_impl_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common/stats/stats_impl_test.cc b/test/common/stats/stats_impl_test.cc index 40403c1770a69..687f3031ac6d2 100644 --- a/test/common/stats/stats_impl_test.cc +++ b/test/common/stats/stats_impl_test.cc @@ -483,7 +483,7 @@ TEST(RawStatDataTest, Consistency) { HeapRawStatDataAllocator alloc; // Generate a stat, encode it to hex, and take the hash of that hex string. We expect the hash to // vary only when the internal representation of a stat has been intentionally changed, in which - // case SharedMemory::Version should be incremented as well. + // case SharedMemory::VERSION should be incremented as well. uint64_t expected_hash = 1874506077228772558; uint64_t max_name_length = RawStatData::maxNameLength();