From eaf2e7b9f7bfecbbba8930b7e2aed169bef3264c Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 27 Nov 2019 12:16:31 -0800 Subject: [PATCH] [unit-test] add a common function to dump buffer content (#4367) This commit updates `test_utils.hpp/cpp` to include a common helper function `DumpBuffer` to print the content of a buffer (as hex and char string) to screen. It also removes unused helper functions and use of STL header files and types. --- tests/unit/Makefile.am | 44 ++++++++++---------- tests/unit/test_lowpan.cpp | 19 +++------ tests/unit/test_ncp_buffer.cpp | 43 +------------------- tests/unit/test_spinel_decoder.cpp | 45 +-------------------- tests/unit/test_spinel_encoder.cpp | 45 +-------------------- tests/unit/test_util.cpp | 65 +++++++++++++----------------- tests/unit/test_util.hpp | 22 +++++----- 7 files changed, 71 insertions(+), 212 deletions(-) diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 9f6b209da..c503e5d41 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -160,61 +160,63 @@ TESTS_ENVIRONMENT = \ top_srcdir='$(top_srcdir)' \ $(NULL) +COMMON_SOURCES = test_platform.cpp test_util.cpp + # Source, compiler, and linker options for test programs. test_aes_LDADD = $(COMMON_LDADD) -test_aes_SOURCES = test_platform.cpp test_aes.cpp +test_aes_SOURCES = $(COMMON_SOURCES) test_aes.cpp test_child_LDADD = $(COMMON_LDADD) -test_child_SOURCES = test_platform.cpp test_child.cpp +test_child_SOURCES = $(COMMON_SOURCES) test_child.cpp test_child_table_LDADD = $(COMMON_LDADD) -test_child_table_SOURCES = test_platform.cpp test_child_table.cpp +test_child_table_SOURCES = $(COMMON_SOURCES) test_child_table.cpp test_hdlc_LDADD = $(COMMON_LDADD) -test_hdlc_SOURCES = test_platform.cpp test_hdlc.cpp +test_hdlc_SOURCES = $(COMMON_SOURCES) test_hdlc.cpp test_heap_LDADD = $(COMMON_LDADD) -test_heap_SOURCES = test_platform.cpp test_heap.cpp +test_heap_SOURCES = $(COMMON_SOURCES) test_heap.cpp test_hmac_sha256_LDADD = $(COMMON_LDADD) -test_hmac_sha256_SOURCES = test_platform.cpp test_hmac_sha256.cpp +test_hmac_sha256_SOURCES = $(COMMON_SOURCES) test_hmac_sha256.cpp test_ip6_address_LDADD = $(COMMON_LDADD) -test_ip6_address_SOURCES = test_platform.cpp test_ip6_address.cpp +test_ip6_address_SOURCES = $(COMMON_SOURCES) test_ip6_address.cpp test_link_quality_LDADD = $(COMMON_LDADD) -test_link_quality_SOURCES = test_platform.cpp test_link_quality.cpp +test_link_quality_SOURCES = $(COMMON_SOURCES) test_link_quality.cpp test_linked_list_LDADD = $(COMMON_LDADD) -test_linked_list_SOURCES = test_platform.cpp test_linked_list.cpp +test_linked_list_SOURCES = $(COMMON_SOURCES) test_linked_list.cpp test_lowpan_LDADD = $(COMMON_LDADD) -test_lowpan_SOURCES = test_platform.cpp test_lowpan.cpp test_util.cpp +test_lowpan_SOURCES = $(COMMON_SOURCES) test_lowpan.cpp test_mac_frame_LDADD = $(COMMON_LDADD) -test_mac_frame_SOURCES = test_platform.cpp test_mac_frame.cpp +test_mac_frame_SOURCES = $(COMMON_SOURCES) test_mac_frame.cpp test_message_LDADD = $(COMMON_LDADD) -test_message_SOURCES = test_platform.cpp test_message.cpp +test_message_SOURCES = $(COMMON_SOURCES) test_message.cpp test_message_queue_LDADD = $(COMMON_LDADD) -test_message_queue_SOURCES = test_platform.cpp test_message_queue.cpp +test_message_queue_SOURCES = $(COMMON_SOURCES) test_message_queue.cpp test_ncp_buffer_LDADD = $(COMMON_LDADD) -test_ncp_buffer_SOURCES = test_platform.cpp test_ncp_buffer.cpp +test_ncp_buffer_SOURCES = $(COMMON_SOURCES) test_ncp_buffer.cpp test_network_data_LDADD = $(COMMON_LDADD) -test_network_data_SOURCES = test_platform.cpp test_network_data.cpp +test_network_data_SOURCES = $(COMMON_SOURCES) test_network_data.cpp test_priority_queue_LDADD = $(COMMON_LDADD) -test_priority_queue_SOURCES = test_platform.cpp test_priority_queue.cpp +test_priority_queue_SOURCES = $(COMMON_SOURCES) test_priority_queue.cpp test_pskc_LDADD = $(COMMON_LDADD) -test_pskc_SOURCES = test_platform.cpp test_pskc.cpp +test_pskc_SOURCES = $(COMMON_SOURCES) test_pskc.cpp test_string_LDADD = $(COMMON_LDADD) -test_string_SOURCES = test_platform.cpp test_string.cpp +test_string_SOURCES = $(COMMON_SOURCES) test_string.cpp test_strlcat_LDADD = $(COMMON_LDADD) test_strlcat_SOURCES = test_strlcat.c @@ -226,13 +228,13 @@ test_strnlen_LDADD = $(COMMON_LDADD) test_strnlen_SOURCES = test_strnlen.c test_spinel_decoder_LDADD = $(COMMON_LDADD) -test_spinel_decoder_SOURCES = test_platform.cpp test_spinel_decoder.cpp +test_spinel_decoder_SOURCES = $(COMMON_SOURCES) test_spinel_decoder.cpp test_spinel_encoder_LDADD = $(COMMON_LDADD) -test_spinel_encoder_SOURCES = test_platform.cpp test_spinel_encoder.cpp +test_spinel_encoder_SOURCES = $(COMMON_SOURCES) test_spinel_encoder.cpp test_timer_LDADD = $(COMMON_LDADD) -test_timer_SOURCES = test_platform.cpp test_timer.cpp +test_timer_SOURCES = $(COMMON_SOURCES) test_timer.cpp test_toolchain_LDADD = $(NULL) test_toolchain_SOURCES = test_toolchain.cpp test_toolchain_c.c diff --git a/tests/unit/test_lowpan.cpp b/tests/unit/test_lowpan.cpp index bab602d20..24a1c8e06 100644 --- a/tests/unit/test_lowpan.cpp +++ b/tests/unit/test_lowpan.cpp @@ -167,13 +167,8 @@ static void Test(TestIphcVector &aVector, bool aCompress, bool aDecompress) printf("LOWPAN_IPHC length ---------- %d\n", aVector.mIphcHeader.mLength); printf("IPv6 uncompressed offset ---- %d\n\n", aVector.mPayloadOffset); - printf("Expected IPv6 uncompressed packet: \n"); - otTestPrintHex(ip6, ip6Length); - printf("\n"); - - printf("Expected LOWPAN_IPHC compressed frame: \n"); - otTestPrintHex(iphc, iphcLength); - printf("\n"); + DumpBuffer("Expected IPv6 uncompressed packet", ip6, ip6Length); + DumpBuffer("Expected LOWPAN_IPHC compressed frame", iphc, iphcLength); if (aCompress) { @@ -194,9 +189,8 @@ static void Test(TestIphcVector &aVector, bool aCompress, bool aDecompress) // Append payload to the LOWPAN_IPHC. message->Read(message->GetOffset(), message->GetLength() - message->GetOffset(), result + compressBytes); - printf("Resulted LOWPAN_IPHC compressed frame: \n"); - otTestPrintHex(result, compressBytes + message->GetLength() - message->GetOffset()); - printf("\n"); + DumpBuffer("Resulted LOWPAN_IPHC compressed frame", result, + compressBytes + message->GetLength() - message->GetOffset()); VerifyOrQuit(compressBytes == aVector.mIphcHeader.mLength, "6lo: Lowpan::Compress failed"); VerifyOrQuit(message->GetOffset() == aVector.mPayloadOffset, "6lo: Lowpan::Compress failed"); @@ -223,9 +217,8 @@ static void Test(TestIphcVector &aVector, bool aCompress, bool aDecompress) memcpy(result + message->GetLength(), iphc + decompressedBytes, iphcLength - static_cast(decompressedBytes)); - printf("Resulted IPv6 uncompressed packet: \n"); - otTestPrintHex(result, message->GetLength() + iphcLength - decompressedBytes); - printf("\n"); + DumpBuffer("Resulted IPv6 uncompressed packet", result, + message->GetLength() + iphcLength - decompressedBytes); VerifyOrQuit(decompressedBytes == aVector.mIphcHeader.mLength, "6lo: Lowpan::Decompress failed"); VerifyOrQuit(message->GetOffset() == aVector.mPayloadOffset, "6lo: Lowpan::Decompress failed"); diff --git a/tests/unit/test_ncp_buffer.cpp b/tests/unit/test_ncp_buffer.cpp index 97368a800..354529011 100644 --- a/tests/unit/test_ncp_buffer.cpp +++ b/tests/unit/test_ncp_buffer.cpp @@ -35,7 +35,7 @@ #include "ncp/ncp_buffer.hpp" #include "test_platform.h" -#include "test_util.h" +#include "test_util.hpp" namespace ot { namespace Ncp { @@ -159,47 +159,6 @@ void FrameRemovedCallback(void * aContext, callbackContext->mFrameRemovedCount++; } -// Dump the buffer content to screen. -void DumpBuffer(const char *aTextMessage, uint8_t *aBuffer, uint16_t aBufferLength) -{ - enum - { - kBytesPerLine = 32, // Number of bytes per line. - }; - - char charBuff[kBytesPerLine + 1]; - uint16_t counter; - uint8_t byte; - - printf("\n%s - len = %u\n ", aTextMessage, aBufferLength); - - counter = 0; - - while (aBufferLength--) - { - byte = *aBuffer++; - printf("%02X ", byte); - charBuff[counter] = isprint(byte) ? static_cast(byte) : '.'; - counter++; - - if (counter == kBytesPerLine) - { - charBuff[counter] = 0; - printf(" %s\n ", charBuff); - counter = 0; - } - } - - charBuff[counter] = 0; - - while (counter++ < kBytesPerLine) - { - printf(" "); - } - - printf(" %s\n", charBuff); -} - // Reads bytes from the ncp buffer, and verifies that it matches with the given content buffer. void ReadAndVerifyContent(NcpFrameBuffer &aNcpBuffer, const uint8_t *aContentBuffer, uint16_t aBufferLength) { diff --git a/tests/unit/test_spinel_decoder.cpp b/tests/unit/test_spinel_decoder.cpp index 8be50de61..0a2d67498 100644 --- a/tests/unit/test_spinel_decoder.cpp +++ b/tests/unit/test_spinel_decoder.cpp @@ -26,13 +26,11 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include - #include "common/code_utils.hpp" #include "common/instance.hpp" #include "ncp/spinel_decoder.hpp" -#include "test_util.h" +#include "test_util.hpp" namespace ot { namespace Ncp { @@ -42,47 +40,6 @@ enum kTestBufferSize = 800, }; -// Dump the buffer content to screen. -void DumpBuffer(const char *aTextMessage, uint8_t *aBuffer, uint16_t aBufferLength) -{ - enum - { - kBytesPerLine = 32, // Number of bytes per line. - }; - - char charBuff[kBytesPerLine + 1]; - uint16_t counter; - uint8_t byte; - - printf("\n%s - len = %u\n ", aTextMessage, aBufferLength); - - counter = 0; - - while (aBufferLength--) - { - byte = *aBuffer++; - printf("%02X ", byte); - charBuff[counter] = isprint(byte) ? static_cast(byte) : '.'; - counter++; - - if (counter == kBytesPerLine) - { - charBuff[counter] = 0; - printf(" %s\n ", charBuff); - counter = 0; - } - } - - charBuff[counter] = 0; - - while (counter++ < kBytesPerLine) - { - printf(" "); - } - - printf(" %s\n", charBuff); -} - void TestSpinelDecoder(void) { uint8_t buffer[kTestBufferSize]; diff --git a/tests/unit/test_spinel_encoder.cpp b/tests/unit/test_spinel_encoder.cpp index cedb598c7..3030bc47e 100644 --- a/tests/unit/test_spinel_encoder.cpp +++ b/tests/unit/test_spinel_encoder.cpp @@ -26,13 +26,11 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include - #include "common/code_utils.hpp" #include "common/instance.hpp" #include "ncp/spinel_encoder.hpp" -#include "test_util.h" +#include "test_util.hpp" namespace ot { namespace Ncp { @@ -42,47 +40,6 @@ enum kTestBufferSize = 800, }; -// Dump the buffer content to screen. -void DumpBuffer(const char *aTextMessage, uint8_t *aBuffer, uint16_t aBufferLength) -{ - enum - { - kBytesPerLine = 32, // Number of bytes per line. - }; - - char charBuff[kBytesPerLine + 1]; - uint16_t counter; - uint8_t byte; - - printf("\n%s - len = %u\n ", aTextMessage, aBufferLength); - - counter = 0; - - while (aBufferLength--) - { - byte = *aBuffer++; - printf("%02X ", byte); - charBuff[counter] = isprint(byte) ? static_cast(byte) : '.'; - counter++; - - if (counter == kBytesPerLine) - { - charBuff[counter] = 0; - printf(" %s\n ", charBuff); - counter = 0; - } - } - - charBuff[counter] = 0; - - while (counter++ < kBytesPerLine) - { - printf(" "); - } - - printf(" %s\n", charBuff); -} - otError ReadFrame(NcpFrameBuffer &aNcpBuffer, uint8_t *aFrame, uint16_t &aFrameLen) { otError error = OT_ERROR_NONE; diff --git a/tests/unit/test_util.cpp b/tests/unit/test_util.cpp index 1c84bd354..0e0ee45dd 100644 --- a/tests/unit/test_util.cpp +++ b/tests/unit/test_util.cpp @@ -26,55 +26,46 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "test_util.h" +#include "test_util.hpp" -#include -#include -#include -#include +#include -#include - -void otTestHexToVector(std::string &aHex, std::vector &aOutBytes) +void DumpBuffer(const char *aTextMessage, uint8_t *aBuffer, uint16_t aBufferLength) { - std::istringstream ss(aHex); - std::string word; - - while (ss >> word) + enum { - uint8_t n = static_cast(strtol(word.data(), NULL, 16)); - aOutBytes.push_back(n); - } -} + kBytesPerLine = 16, // Number of bytes per line. + }; -void otTestPrintHex(uint8_t *aBuffer, int aLength) -{ - int i; + char charBuff[kBytesPerLine + 1]; + uint16_t counter; + uint8_t byte; - for (i = 0; i < aLength; i++) + printf("\n%s - len = %u\n ", aTextMessage ? aTextMessage : "Buffer", aBufferLength); + + counter = 0; + + while (aBufferLength--) { - printf("%02x ", aBuffer[i]); + byte = *aBuffer++; + printf("%02X ", byte); + charBuff[counter] = isprint(byte) ? static_cast(byte) : '.'; + counter++; - if (i % 16 == 7) + if (counter == kBytesPerLine) { - printf(" "); - } - - if (i % 16 == 15 && aLength != i + 1) - { - printf("\n"); + charBuff[counter] = 0; + printf(" %s\n ", charBuff); + counter = 0; } } - printf("\n"); -} + charBuff[counter] = 0; -void otTestPrintHex(std::string &aString) -{ - otTestPrintHex((uint8_t *)aString.data(), static_cast(aString.size())); -} + while (counter++ < kBytesPerLine) + { + printf(" "); + } -void otTestPrintHex(std::vector &aBytes) -{ - otTestPrintHex((uint8_t *)&aBytes[0], static_cast(aBytes.size())); + printf(" %s\n", charBuff); } diff --git a/tests/unit/test_util.hpp b/tests/unit/test_util.hpp index 8d25f3222..122c1ea4c 100644 --- a/tests/unit/test_util.hpp +++ b/tests/unit/test_util.hpp @@ -29,18 +29,18 @@ #ifndef TEST_UTIL_HPP #define TEST_UTIL_HPP +#include + #include "test_util.h" -// STL is okay in unit tests. -#include -#include - -void otTestHexToVector(std::string &aHex, std::vector &aOutBytes); - -void otTestPrintHex(uint8_t *aBuffer, int aLength); - -void otTestPrintHex(std::vector &aBytes); - -void otTestPrintHex(std::string &aString); +/** + * This function prints the content of a given buffer to screen as a hex dump along with ASCII text translation. + * + * @param[in] aTextMessag A text message to describe the buffer content (printed before the buffer content) + * @param[in] aBuffer A pointer to the buffer + * @param[in] aBufferLength Number of bytes in the buffer. + * + */ +void DumpBuffer(const char *aTextMessage, uint8_t *aBuffer, uint16_t aBufferLength); #endif