diff options
author | mukesh agrawal <quiche@google.com> | 2016-09-29 14:59:27 -0700 |
---|---|---|
committer | mukesh agrawal <quiche@google.com> | 2016-10-04 18:28:05 -0700 |
commit | 0da05477c58df736e69ec421d14a1c3216011d81 (patch) | |
tree | 86b955201974c06427260e0ead9e1ac0a643b7c1 /tests | |
parent | 3d8db0877bb66bfea17e37e12a586b4b29440963 (diff) | |
download | wifilogd-0da05477c58df736e69ec421d14a1c3216011d81.tar.gz |
local_utils: add CopyFromBufferOrDie
This new template function helps copying data out of a chunk
of memory, into a struct.
Bug: 31862263
Test: ./runtests.sh (on bullhead)
Change-Id: I522c76c245829d5b6397dcd5633421188d44d325
Diffstat (limited to 'tests')
-rw-r--r-- | tests/local_utils_unittest.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/local_utils_unittest.cpp b/tests/local_utils_unittest.cpp index 49c4453..8111799 100644 --- a/tests/local_utils_unittest.cpp +++ b/tests/local_utils_unittest.cpp @@ -24,8 +24,21 @@ namespace android { namespace wifilogd { +using local_utils::CopyFromBufferOrDie; using local_utils::GetMaxVal; +TEST(LocalUtilsTest, CopyFromBufferOrDieCopiesData) { + struct Message { + int a; + char b; + }; + const Message original{5, 'c'}; + const auto& duplicate = + CopyFromBufferOrDie<Message>(&original, sizeof(original)); + EXPECT_EQ(original.a, duplicate.a); + EXPECT_EQ(original.b, duplicate.b); +} + TEST(LocalUtilsTest, GetMaxValFromTypeIsCorrectForUnsignedTypes) { EXPECT_EQ(std::numeric_limits<uint8_t>::max(), GetMaxVal<uint8_t>()); EXPECT_EQ(std::numeric_limits<uint16_t>::max(), GetMaxVal<uint16_t>()); @@ -80,5 +93,19 @@ TEST(LocalUtilsTest, SafelyClampWorksForUnsignedToSigned) { EXPECT_EQ(int8_t{127}, (SAFELY_CLAMP(uint8_t{128}, int8_t, 0, 127))); } +// Per +// github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#death-tests), +// death tests should be specially named. + +TEST(LocalUtilsDeathTest, CopyFromBufferOrDieWithShortBufferCausesDeath) { + struct Message { + int a; + char b; + }; + const Message original{5, 'c'}; + EXPECT_DEATH((CopyFromBufferOrDie<Message>(&original, sizeof(original) - 1)), + "Check failed"); +} + } // namespace wifilogd } // namespace android |