Skip to content

Commit

Permalink
Fail A record parsing if IPV4 is disabled in CHIP (#17074)
Browse files Browse the repository at this point in the history
* Fail A record parsing if IPV4 is disabled in CHIP

* Enable ARecord parse test in IPV6 as well
  • Loading branch information
andy31415 authored and pull[bot] committed Jan 20, 2024
1 parent 5be91f8 commit 021adef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/lib/dnssd/minimal_mdns/RecordData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand All @@ -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)
{
Expand Down Expand Up @@ -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), //
Expand Down

0 comments on commit 021adef

Please sign in to comment.