[dns] add MatchesKey() to Dns::TxtEntry (#12579)

This commit adds `MatchesKey()` method to `Dns::TxtEntry` class to
check if the entry's key matches a given key string using a
case-insensitive string comparison.

The new method properly handles the case where the entry's key is
`nullptr` (which can happen when the key is longer than the
recommended max length). It simplifies the TXT entry parsing logic in
`BorderAgent` and `Trel::PeerDiscoverer`.

The unit tests for `Dns::TxtEntry` are also updated to verify the new
method.
This commit is contained in:
Abtin Keshavarzian
2026-02-27 18:55:35 -06:00
committed by GitHub
parent be4ce643ec
commit d0237f5e91
5 changed files with 44 additions and 37 deletions
+12 -4
View File
@@ -34,6 +34,7 @@
#include "test_util.hpp"
#include "common/array.hpp"
#include "common/string.hpp"
#include "instance/instance.hpp"
#include "net/dns_types.hpp"
@@ -1632,10 +1633,8 @@ void TestHeaderAndResourceRecords(void)
void TestDnsTxtEntry(void)
{
enum
{
kMaxTxtDataSize = 255,
};
static constexpr uint16_t kMaxTxtDataSize = 255;
static constexpr uint16_t kMaxKeyStringSize = Dns::TxtEntry::kMaxIterKeyLength;
struct EncodedTxtData
{
@@ -1718,6 +1717,7 @@ void TestDnsTxtEntry(void)
Dns::TxtEntry txtEntry;
Dns::TxtEntry::Iterator iterator;
MutableData<kWithUint16Length> data;
char keyString[kMaxKeyStringSize];
printf("================================================================\n");
printf("TestDnsTxtEntry()\n");
@@ -1772,6 +1772,14 @@ void TestDnsTxtEntry(void)
}
VerifyOrQuit(strcmp(txtEntry.mKey, expectedTxtEntry.mKey) == 0);
SuccessOrQuit(StringCopy(keyString, expectedTxtEntry.mKey));
VerifyOrQuit(txtEntry.MatchesKey(keyString));
StringConvertToLowercase(keyString);
VerifyOrQuit(txtEntry.MatchesKey(keyString));
StringConvertToUppercase(keyString);
VerifyOrQuit(txtEntry.MatchesKey(keyString));
VerifyOrQuit(txtEntry.mValueLength == expectedTxtEntry.mValueLength);
if (txtEntry.mValueLength != 0)