Skip to content
Snippets Groups Projects
Commit 983448e3 authored by Handa Wang's avatar Handa Wang
Browse files

[mdns] avoid checking conflicts against the registration itself

The current implementation contains a bug that an updated
registration may be checked for conflict with itself. In this way
it will get renamed although its not a real conflict.

Bug: 283053491
Change-Id: I9b717c1a389bd92b8cffa9fe3f58e921609bd79b
parent cfe53ecb
No related branches found
No related tags found
No related merge requests found
......@@ -253,12 +253,13 @@ public class MdnsAdvertiser {
private boolean hasAnyServiceConflict(
@NonNull BiPredicate<Network, InterfaceAdvertiserRequest> applicableAdvertiserFilter,
@NonNull NsdServiceInfo newInfo) {
@NonNull NsdServiceInfo newInfo,
@NonNull Registration originalRegistration) {
return any(
mAdvertiserRequests,
(network, adv) ->
applicableAdvertiserFilter.test(network, adv)
&& adv.hasServiceConflict(newInfo));
&& adv.hasServiceConflict(newInfo, originalRegistration));
}
private boolean hasAnyHostConflict(
......@@ -283,7 +284,7 @@ public class MdnsAdvertiser {
NsdServiceInfo newInfo = registration.getServiceInfo();
int renameServiceCount = 0;
while (hasAnyServiceConflict(applicableAdvertiserFilter, newInfo)) {
while (hasAnyServiceConflict(applicableAdvertiserFilter, newInfo, registration)) {
renameServiceCount++;
newInfo = registration.makeNewServiceInfoForServiceConflict(renameServiceCount);
}
......@@ -378,8 +379,9 @@ public class MdnsAdvertiser {
* Return whether using the proposed new {@link NsdServiceInfo} to add a registration would
* cause a conflict of the service in this {@link InterfaceAdvertiserRequest}.
*/
boolean hasServiceConflict(@NonNull NsdServiceInfo newInfo) {
return getConflictingRegistrationDueToService(newInfo) >= 0;
boolean hasServiceConflict(
@NonNull NsdServiceInfo newInfo, @NonNull Registration originalRegistration) {
return getConflictingRegistrationDueToService(newInfo, originalRegistration) >= 0;
}
/**
......@@ -393,11 +395,16 @@ public class MdnsAdvertiser {
}
/** Get the ID of a conflicting registration due to service, or -1 if none. */
int getConflictingRegistrationDueToService(@NonNull NsdServiceInfo info) {
int getConflictingRegistrationDueToService(
@NonNull NsdServiceInfo info, @NonNull Registration originalRegistration) {
if (TextUtils.isEmpty(info.getServiceName())) {
return -1;
}
for (int i = 0; i < mPendingRegistrations.size(); i++) {
// Never conflict with itself
if (mPendingRegistrations.valueAt(i) == originalRegistration) {
continue;
}
final NsdServiceInfo other = mPendingRegistrations.valueAt(i).getServiceInfo();
if (MdnsUtils.equalsIgnoreDnsCase(info.getServiceName(), other.getServiceName())
&& MdnsUtils.equalsIgnoreDnsCase(info.getServiceType(),
......
......@@ -1220,8 +1220,7 @@ class NsdManagerTest {
// Registration must use an updated hostname to avoid the conflict
val cb = registrationRecord.expectCallback<ServiceRegistered>(REGISTRATION_TIMEOUT_MS)
// Service name is not renamed because there's no conflict on the service name.
// TODO: b/283053491 - enable this check
// assertEquals(serviceName, cb.serviceInfo.serviceName)
assertEquals(serviceName, cb.serviceInfo.serviceName)
val hostname = cb.serviceInfo.hostname ?: fail("Missing hostname")
hostname.let {
assertTrue("Unexpected registered hostname: $it",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment