[dns] add Name::Matches() to compare DNS names (#9734)

This commit adds `Name::Matches()` method which compares a `Name`
instance (which can be from a C string or encoded in a `Message`)
with a given set of labels and domain name strings. This method
allows the caller to specify name components separately, enabling
scenarios like comparing "service instance name" with separate
instance label, service type, and domain strings. Unit test
`test_dns` is also updated to validate the behavior of the newly
added method.
This commit is contained in:
Abtin Keshavarzian
2023-12-20 14:20:58 -08:00
committed by GitHub
parent 16596a38c3
commit 51a682ec0e
3 changed files with 129 additions and 0 deletions
+47
View File
@@ -56,6 +56,15 @@ void TestDnsName(void)
const char *mExpectedReadName;
};
struct TestMatches
{
const char *mFullName;
const char *mFirstLabels;
const char *mSecondLabels;
const char *mDomain;
bool mShouldMatch;
};
Instance *instance;
MessagePool *messagePool;
Message *message;
@@ -139,6 +148,25 @@ void TestDnsName(void)
static const char kBadLabel[] = "badlabel";
static const char kBadName[] = "bad.name";
static const TestMatches kTestMatches[] = {
{"foo.bar.local.", "foo", "bar", "local.", true},
{"foo.bar.local.", "foo.bar", nullptr, "local.", true},
{"foo.bar.local.", nullptr, "foo.bar", "local.", true},
{"foo.bar.local.", nullptr, nullptr, "foo.bar.local.", true},
{"foo.bar.local.", "foo", "ba", "local.", false},
{"foo.bar.local.", "fooooo", "bar", "local.", false},
{"foo.bar.local.", "foo", "bar", "locall.", false},
{"foo.bar.local.", "f", "bar", "local.", false},
{"foo.bar.local.", "foo", "barr", "local.", false},
{"foo.bar.local.", "foo", "bar", ".local.", false},
{"My Lovely Instance._mt._udp.local.", "mY lovely instancE", "_mt._udp", "local.", true},
{"My Lovely Instance._mt._udp.local.", "mY lovely instancE._mt", "_udp", "local.", true},
{"_s1._sub._srv._udp.default.service.arpa.", "_s1._sub", "_srv._udp", "default.service.arpa.", true},
{"_s1._sub._srv._udp.default.service.arpa.", "_s1._sub", "_srv._udp", "default.service.arpa", false},
{"_s1._sub._srv._udp.default.service.arpa.", "_s1._sub", "_srv._udp.", "default.service.arpa.", false},
{"_s1._sub._srv._udp.default.service.arpa.", "_s1._sub.", "_srv._udp", "default.service.arpa.", false},
};
printf("================================================================\n");
printf("TestDnsName()\n");
@@ -496,6 +524,25 @@ void TestDnsName(void)
VerifyOrQuit(memcmp(buffer, test.mEncodedData, len) == 0, "Encoded name data does not match expected data");
}
printf("----------------------------------------------------------------\n");
printf("Name::Matches() variations\n");
for (const TestMatches &test : kTestMatches)
{
Dns::Name name;
printf(" \"%s\"\n", test.mFullName);
name.Set(test.mFullName);
VerifyOrQuit(name.Matches(test.mFirstLabels, test.mSecondLabels, test.mDomain) == test.mShouldMatch);
IgnoreError(message->SetLength(0));
SuccessOrQuit(name.AppendTo(*message));
name.SetFromMessage(*message, 0);
VerifyOrQuit(name.Matches(test.mFirstLabels, test.mSecondLabels, test.mDomain) == test.mShouldMatch);
}
message->Free();
testFreeInstance(instance);
}