[br-tracker] enhance NetDataBrTracker to support filtering (#11964)

This change enhances `NetDataBrTracker` to support filtering of Border
Routers.

The `NetDataPeerBrTracker` is renamed to `NetDataBrTracker` to reflect
that it can now track all Border Routers, not just peers.

A new `Filter` enum is introduced with `kAllBorderRouters` and
`kExcludeThisDevice` options. This allows callers to specify whether
to include the current device in the list of Border Routers.

The `CountPeerBrs()` and `GetNext()` methods are updated to
`CountBrs()` and `GetNext()` respectively, and now accept a `Filter`
parameter.

This change provides more flexibility to the `NetDataBrTracker` and
makes the code more reusable. The unit tests are also updated to
clean up resources to avoid heap allocation leaks at the end of
tests.
This commit is contained in:
Abtin Keshavarzian
2025-09-29 18:05:55 -07:00
committed by GitHub
parent 14373d5543
commit 7bbdbc69ce
10 changed files with 168 additions and 64 deletions
+51 -1
View File
@@ -1472,6 +1472,8 @@ void TestSamePrefixesFromMultipleRouters(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestSamePrefixesFromMultipleRouters");
@@ -1622,6 +1624,8 @@ void TestOmrSelection(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestOmrSelection");
@@ -1890,6 +1894,8 @@ void TestOmrConfig(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestOmrConfig");
@@ -2055,6 +2061,17 @@ void TestDefaultRoute(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
// Remove the manually added on-mesh prefix with a default route.
// This ensures the device is no longer considered a BR, so its heap
// allocation in `NetDataBrTracker` is released. Otherwise, the
// `heapAllocations` check would fail.
SuccessOrQuit(otBorderRouterRemoveOnMeshPrefix(sInstance, &prefixConfig.mPrefix));
SuccessOrQuit(otBorderRouterRegister(sInstance));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestDefaultRoute");
@@ -2164,6 +2181,8 @@ void TestNonUlaPioWithOnlyOnLinkFlag(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestNonUlaPioWithOnlyOnLinkFlag");
@@ -2330,6 +2349,17 @@ void TestAdvNonUlaRoute(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
// Remove the manually added on-mesh prefix with a default route.
// This ensures the device is no longer considered a BR, so its
// heap allocation in `NetDataBrTracker` is released. Otherwise,
// the `heapAllocations` check would fail.
SuccessOrQuit(otBorderRouterRemoveOnMeshPrefix(sInstance, &prefixConfig.mPrefix));
SuccessOrQuit(otBorderRouterRegister(sInstance));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestAdvNonUlaRoute");
@@ -2449,6 +2479,8 @@ void TestFavoredOnLinkPrefix(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestFavoredOnLinkPrefix");
@@ -2587,6 +2619,8 @@ void TestLocalOnLinkPrefixDeprecation(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestLocalOnLinkPrefixDeprecation");
@@ -2743,6 +2777,8 @@ void TestDomainPrefixAsOmr(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestDomainPrefixAsOmr");
@@ -3252,6 +3288,8 @@ void TestExtPanIdChange(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestExtPanIdChange");
@@ -3390,6 +3428,8 @@ void TestPrefixStaleTime(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestPrefixStaleTime");
@@ -3543,6 +3583,8 @@ void TestRouterNsProbe(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestRouterNsProbe");
@@ -3714,6 +3756,7 @@ void TestLearningAndCopyingOfFlags(void)
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
VerifyDiscoveredRoutersIsEmpty();
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
@@ -3802,6 +3845,7 @@ void TestLearnRaHeader(void)
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
VerifyDiscoveredRoutersIsEmpty();
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
@@ -4022,6 +4066,8 @@ void TestConflictingPrefix(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
Log("End of TestConflictingPrefix");
@@ -4536,6 +4582,8 @@ void TestNat64PrefixSelection(void)
VerifyNat64PrefixInNetData(localNat64);
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(sHeapAllocatedPtrs.GetLength() == heapAllocations);
Log("End of TestNat64PrefixSelection");
@@ -4862,6 +4910,8 @@ void TestDhcp6Pd(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
AdvanceTime(3000);
VerifyOrQuit(sHeapAllocatedPtrs.GetLength() <= heapAllocations);
Log("End of TestDhcp6Pd");
@@ -5101,8 +5151,8 @@ void TestRdnss(void)
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SuccessOrQuit(sInstance->Get<BorderRouter::RoutingManager>().SetEnabled(false));
VerifyRdnssAddressTableIsEmpty();
AdvanceTime(3000);
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());