From 21695410a87e04a2e7110177776165d0507c3775 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Tue, 5 Apr 2022 12:31:24 -1000 Subject: [PATCH] Fail A record parsing if IPV4 is disabled in CHIP (#17074) * Fail A record parsing if IPV4 is disabled in CHIP * Enable ARecord parse test in IPV6 as well --- src/lib/dnssd/minimal_mdns/RecordData.cpp | 5 +++++ src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp | 13 +++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib/dnssd/minimal_mdns/RecordData.cpp b/src/lib/dnssd/minimal_mdns/RecordData.cpp index 860773f407491b..80bd1a7edef2bc 100644 --- a/src/lib/dnssd/minimal_mdns/RecordData.cpp +++ b/src/lib/dnssd/minimal_mdns/RecordData.cpp @@ -90,6 +90,7 @@ bool SrvRecord::Parse(const BytesRange & data, const BytesRange & packet) bool ParseARecord(const BytesRange & data, chip::Inet::IPAddress * addr) { +#if INET_CONFIG_ENABLE_IPV4 if (data.Size() != 4) { return false; @@ -101,6 +102,10 @@ bool ParseARecord(const BytesRange & data, chip::Inet::IPAddress * addr) addr->Addr[3] = htonl(chip::Encoding::BigEndian::Get32(data.Start())); return true; +#else + // IPV4 support is disabled: IPAddress should never get IPv4 values. + return false; +#endif } bool ParseAAAARecord(const BytesRange & data, chip::Inet::IPAddress * addr) diff --git a/src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp b/src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp index 436803f59dd4b8..8cc1d242b1f997 100644 --- a/src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp +++ b/src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp @@ -106,7 +106,6 @@ void SrvWithPtrRecord(nlTestSuite * inSuite, void * inContext) } } -#if INET_CONFIG_ENABLE_IPV4 void ARecordParsing(nlTestSuite * inSuite, void * inContext) { const uint8_t record[] = { @@ -117,13 +116,17 @@ void ARecordParsing(nlTestSuite * inSuite, void * inContext) }; Inet::IPAddress addr; + +#if INET_CONFIG_ENABLE_IPV4 Inet::IPAddress expected; NL_TEST_ASSERT(inSuite, ParseARecord(BytesRange(record, record + sizeof(record)), &addr)); NL_TEST_ASSERT(inSuite, Inet::IPAddress::FromString("10.11.12.13", expected)); NL_TEST_ASSERT(inSuite, addr == expected); -} +#else + NL_TEST_ASSERT(inSuite, !ParseARecord(BytesRange(record, record + sizeof(record)), &addr)); #endif // INET_CONFIG_ENABLE_IPV4 +} void AAAARecordParsing(nlTestSuite * inSuite, void * inContext) { @@ -240,11 +243,9 @@ void TxtRecord(nlTestSuite * inSuite, void * inContext) } const nlTest sTests[] = { - NL_TEST_DEF("SrvRecordSimpleParsing", SrvRecordSimpleParsing), // - NL_TEST_DEF("SrvWithPtrRecord", SrvWithPtrRecord), // -#if INET_CONFIG_ENABLE_IPV4 + NL_TEST_DEF("SrvRecordSimpleParsing", SrvRecordSimpleParsing), // + NL_TEST_DEF("SrvWithPtrRecord", SrvWithPtrRecord), // NL_TEST_DEF("ARecordParsing", ARecordParsing), // -#endif // INET_CONFIG_ENABLE_IPV4 NL_TEST_DEF("AAAARecordParsing", AAAARecordParsing), // NL_TEST_DEF("PtrRecordSimpleParsing", PtrRecordSimpleParsing), // NL_TEST_DEF("PtrRecordComplexParsing", PtrRecordComplexParsing), //