[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.
This commit is contained in:
Abtin Keshavarzian
2019-12-11 09:16:48 -08:00
committed by Jonathan Hui
parent 91e8903699
commit eaf2e7b9f7
7 changed files with 71 additions and 212 deletions
+23 -21
View File
@@ -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
+6 -13
View File
@@ -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<uint16_t>(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");
+1 -42
View File
@@ -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<char>(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)
{
+1 -44
View File
@@ -26,13 +26,11 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#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<char>(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];
+1 -44
View File
@@ -26,13 +26,11 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#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<char>(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;
+28 -37
View File
@@ -26,55 +26,46 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "test_util.h"
#include "test_util.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <ctype.h>
#include <stdint.h>
void otTestHexToVector(std::string &aHex, std::vector<uint8_t> &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<uint8_t>(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<char>(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<int>(aString.size()));
}
while (counter++ < kBytesPerLine)
{
printf(" ");
}
void otTestPrintHex(std::vector<uint8_t> &aBytes)
{
otTestPrintHex((uint8_t *)&aBytes[0], static_cast<int>(aBytes.size()));
printf(" %s\n", charBuff);
}
+11 -11
View File
@@ -29,18 +29,18 @@
#ifndef TEST_UTIL_HPP
#define TEST_UTIL_HPP
#include <stdint.h>
#include "test_util.h"
// STL is okay in unit tests.
#include <string>
#include <vector>
void otTestHexToVector(std::string &aHex, std::vector<uint8_t> &aOutBytes);
void otTestPrintHex(uint8_t *aBuffer, int aLength);
void otTestPrintHex(std::vector<uint8_t> &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