From a549ac4e9d98de85a4a3796ea6b0aef2aacf07a0 Mon Sep 17 00:00:00 2001 From: Adam Mitz Date: Wed, 19 Jun 2024 19:54:34 +0000 Subject: [PATCH] Updated to_dds_string int-to-string conversions for consistent formatting Hex formats use leading zeroes. Hex for the C++ long type is not supported since it has different sizes on different platforms. --- dds/DCPS/GuidConverter.cpp | 4 +- dds/DCPS/GuidConverter.h | 2 +- dds/DCPS/SafetyProfileStreams.cpp | 14 ++-- dds/DCPS/SafetyProfileStreams.h | 6 +- dds/DCPS/security/CommonUtilities.cpp | 4 +- tests/unit-tests/dds/DCPS/GuidConverter.cpp | 4 +- .../dds/DCPS/SafetyProfileStreams.cpp | 73 +++++++++++++++++++ 7 files changed, 89 insertions(+), 18 deletions(-) diff --git a/dds/DCPS/GuidConverter.cpp b/dds/DCPS/GuidConverter.cpp index 1636579c0b0..cafdf55876c 100644 --- a/dds/DCPS/GuidConverter.cpp +++ b/dds/DCPS/GuidConverter.cpp @@ -28,7 +28,7 @@ GuidConverter::GuidConverter(const GUID_t& guid) GuidConverter::~GuidConverter() {} -long +unsigned int GuidConverter::checksum() const { return ACE::crc32(reinterpret_cast(&guid_), sizeof(guid_)); @@ -156,7 +156,7 @@ GuidConverter::operator OPENDDS_STRING() const { OPENDDS_STRING ret(to_string(guid_)); ret += "("; - ret += to_dds_string((unsigned long) checksum(), true); + ret += to_dds_string(checksum(), true); ret += ")"; return ret; } diff --git a/dds/DCPS/GuidConverter.h b/dds/DCPS/GuidConverter.h index 2e24d98e6b6..0c193007bda 100644 --- a/dds/DCPS/GuidConverter.h +++ b/dds/DCPS/GuidConverter.h @@ -68,7 +68,7 @@ class OpenDDS_Dcps_Export GuidConverter { virtual ~GuidConverter(); /// Calculate the CRC32 checksum. - long checksum() const; + unsigned int checksum() const; /// Extract the VendorId value. long vendorId() const; diff --git a/dds/DCPS/SafetyProfileStreams.cpp b/dds/DCPS/SafetyProfileStreams.cpp index d4481a55d1c..56ee096efa5 100644 --- a/dds/DCPS/SafetyProfileStreams.cpp +++ b/dds/DCPS/SafetyProfileStreams.cpp @@ -18,15 +18,15 @@ String to_dds_string(ACE_CDR::Octet to_convert, bool as_hex) { static const int buff_size = 3 + 1; // note +1 for null terminator char buf[buff_size]; - ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%02x" : "%d", to_convert); + ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%02x" : "%u", to_convert); return String(buf); } -String to_dds_string(unsigned short to_convert) +String to_dds_string(unsigned short to_convert, bool as_hex) { static const int buff_size = 5 + 1; // note +1 for null terminator char buf[buff_size]; - ACE_OS::snprintf(&buf[0], buff_size, "%hu", to_convert); + ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%04hx" : "%hu", to_convert); return String(buf); } @@ -42,7 +42,7 @@ String to_dds_string(unsigned int to_convert, bool as_hex) { static const int buff_size = 20 + 1; // note +1 for null terminator char buf[buff_size]; - ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%x" : "%u", to_convert); + ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%08x" : "%u", to_convert); return String(buf); } @@ -66,15 +66,15 @@ String to_dds_string(unsigned long long to_convert, bool as_hex) { const int buff_size = 20 + 1; // note +1 for null terminator char buf[buff_size]; - ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%0llx" : "%llu", to_convert); + ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%016llx" : "%llu", to_convert); return String(buf); } -String to_dds_string(unsigned long to_convert, bool as_hex) +String to_dds_string(unsigned long to_convert) { const int buff_size = 20 + 1; // note +1 for null terminator char buf[buff_size]; - ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%08lx" : "%lu", to_convert); + ACE_OS::snprintf(&buf[0], buff_size, "%lu", to_convert); return String(buf); } diff --git a/dds/DCPS/SafetyProfileStreams.h b/dds/DCPS/SafetyProfileStreams.h index 9a7f6c31b90..94a3ce08e7a 100644 --- a/dds/DCPS/SafetyProfileStreams.h +++ b/dds/DCPS/SafetyProfileStreams.h @@ -5,8 +5,6 @@ #include "PoolAllocator.h" #include -#include -#include #ifdef OPENDDS_SAFETY_PROFILE # include // For strto* @@ -23,13 +21,13 @@ namespace OpenDDS { namespace DCPS { OpenDDS_Dcps_Export String to_dds_string(ACE_CDR::Octet to_convert, bool as_hex = false); -OpenDDS_Dcps_Export String to_dds_string(unsigned short to_convert); +OpenDDS_Dcps_Export String to_dds_string(unsigned short to_convert, bool as_hex = false); OpenDDS_Dcps_Export String to_dds_string(int to_convert); OpenDDS_Dcps_Export String to_dds_string(unsigned int to_convert, bool as_hex = false); OpenDDS_Dcps_Export String to_dds_string(long to_convert); +OpenDDS_Dcps_Export String to_dds_string(unsigned long to_convert); OpenDDS_Dcps_Export String to_dds_string(long long to_convert); OpenDDS_Dcps_Export String to_dds_string(unsigned long long to_convert, bool as_hex = false); -OpenDDS_Dcps_Export String to_dds_string(unsigned long to_convert, bool as_hex = false); OpenDDS_Dcps_Export String to_dds_string(const unsigned char* array, size_t length); OpenDDS_Dcps_Export String to_dds_string(double value); OpenDDS_Dcps_Export String to_dds_string(const void* to_convert); diff --git a/dds/DCPS/security/CommonUtilities.cpp b/dds/DCPS/security/CommonUtilities.cpp index 13778fdff68..8e99fa01d6f 100644 --- a/dds/DCPS/security/CommonUtilities.cpp +++ b/dds/DCPS/security/CommonUtilities.cpp @@ -80,7 +80,7 @@ bool set_security_error(DDS::Security::SecurityException& ex, ex.minor_code = minor_code; // OpenSSL errors are rendered in hex, matching "openssl errstr" static const bool as_hex = true; - ex.message = (message_prefix + String(" OpenSSL error ") + to_dds_string(openssl_error, as_hex)).c_str(); + ex.message = (message_prefix + String(" OpenSSL error ") + to_dds_string(static_cast(openssl_error), as_hex)).c_str(); return false; } @@ -93,7 +93,7 @@ bool set_security_error(DDS::Security::SecurityException& ex, { std::string full(message); const size_t i = full.size(); - full.resize(i + 25); + full.resize(i + 26); std::sprintf(&full[i], " %.2x %.2x %.2x %.2x, %.2x %.2x %.2x %.2x", a1[0], a1[1], a1[2], a1[3], a2[0], a2[1], a2[2], a2[3]); return set_security_error(ex, code, minor_code, full.c_str()); diff --git a/tests/unit-tests/dds/DCPS/GuidConverter.cpp b/tests/unit-tests/dds/DCPS/GuidConverter.cpp index 20e3758812c..e8293de7a56 100644 --- a/tests/unit-tests/dds/DCPS/GuidConverter.cpp +++ b/tests/unit-tests/dds/DCPS/GuidConverter.cpp @@ -55,8 +55,8 @@ TEST(dds_DCPS_GuidConverter, validate_Checksum) GUID_t guid0 = InitGUID(0); GUID_t guid1 = InitGUID(1); - long crc0 = GuidConverter(guid0).checksum(); - long crc1 = GuidConverter(guid1).checksum(); + unsigned int crc0 = GuidConverter(guid0).checksum(); + unsigned int crc1 = GuidConverter(guid1).checksum(); EXPECT_EQ(crc0, 0xf09df109); EXPECT_EQ(crc1, 0xf1b7254f); diff --git a/tests/unit-tests/dds/DCPS/SafetyProfileStreams.cpp b/tests/unit-tests/dds/DCPS/SafetyProfileStreams.cpp index c87f417a0f7..e3a383d1f3b 100644 --- a/tests/unit-tests/dds/DCPS/SafetyProfileStreams.cpp +++ b/tests/unit-tests/dds/DCPS/SafetyProfileStreams.cpp @@ -4,6 +4,79 @@ using namespace OpenDDS::DCPS; +TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_octet) +{ + const ACE_CDR::Octet nine = 9, ninetyNine = 99; + EXPECT_EQ(to_dds_string(nine), "9"); + EXPECT_EQ(to_dds_string(nine, true), "09"); + EXPECT_EQ(to_dds_string(ninetyNine), "99"); + EXPECT_EQ(to_dds_string(ninetyNine, true), "63"); +} + +TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_ushort) +{ + const unsigned short ninetyNine = 99, nineThousand = 9000, fiftyNineThousand = 59000; + EXPECT_EQ(to_dds_string(ninetyNine), "99"); + EXPECT_EQ(to_dds_string(ninetyNine, true), "0063"); + EXPECT_EQ(to_dds_string(nineThousand), "9000"); + EXPECT_EQ(to_dds_string(nineThousand, true), "2328"); + EXPECT_EQ(to_dds_string(fiftyNineThousand), "59000"); + EXPECT_EQ(to_dds_string(fiftyNineThousand, true), "e678"); +} + +TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_int) +{ + const int ninetyNine = 99, nineThousand = 9000, negativeNineThousand = -9000; + EXPECT_EQ(to_dds_string(ninetyNine), "99"); + EXPECT_EQ(to_dds_string(nineThousand), "9000"); + EXPECT_EQ(to_dds_string(negativeNineThousand), "-9000"); +} + +TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_uint) +{ + const unsigned int ninetyNine = 99, nineThousand = 9000, fiftyNineThousand = 59000; + EXPECT_EQ(to_dds_string(ninetyNine), "99"); + EXPECT_EQ(to_dds_string(ninetyNine, true), "00000063"); + EXPECT_EQ(to_dds_string(nineThousand), "9000"); + EXPECT_EQ(to_dds_string(nineThousand, true), "00002328"); + EXPECT_EQ(to_dds_string(fiftyNineThousand), "59000"); + EXPECT_EQ(to_dds_string(fiftyNineThousand, true), "0000e678"); +} + +TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_long) +{ + const long ninetyNine = 99, nineThousand = 9000, negativeNineThousand = -9000; + EXPECT_EQ(to_dds_string(ninetyNine), "99"); + EXPECT_EQ(to_dds_string(nineThousand), "9000"); + EXPECT_EQ(to_dds_string(negativeNineThousand), "-9000"); +} + +TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_ulong) +{ + const unsigned long ninetyNine = 99, nineThousand = 9000; + EXPECT_EQ(to_dds_string(ninetyNine), "99"); + EXPECT_EQ(to_dds_string(nineThousand), "9000"); +} + +TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_longlong) +{ + const long long ninetyNine = 99, nineThousand = 9000, negativeNineThousand = -9000; + EXPECT_EQ(to_dds_string(ninetyNine), "99"); + EXPECT_EQ(to_dds_string(nineThousand), "9000"); + EXPECT_EQ(to_dds_string(negativeNineThousand), "-9000"); +} + +TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_ulonglong) +{ + const unsigned long long ninetyNine = 99, nineThousand = 9000, fiftyNineThousand = 59000; + EXPECT_EQ(to_dds_string(ninetyNine), "99"); + EXPECT_EQ(to_dds_string(ninetyNine, true), "0000000000000063"); + EXPECT_EQ(to_dds_string(nineThousand), "9000"); + EXPECT_EQ(to_dds_string(nineThousand, true), "0000000000002328"); + EXPECT_EQ(to_dds_string(fiftyNineThousand), "59000"); + EXPECT_EQ(to_dds_string(fiftyNineThousand, true), "000000000000e678"); +} + TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_double) { EXPECT_EQ(to_dds_string(1.5), "1.5");