From e1b474244259bbbe6adbdf780928f987cef51f24 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Wed, 31 Jul 2013 23:23:21 +0900 Subject: [PATCH] Add accessors for all addresses and clarify compare* methods 1. Add a method to return all addresses and all LinkAddresses on all links (both base links and stacked links). We already had one for routes, but did not yet have any for addresses. 2. Rename compareRoutes to compareAllRoutes, because unlike the other compare* methods, it looks at all interfaces. Update what appears to be its only caller. 3. Update the documentation of the compare* methods to match reality (they don't return lists) and clarify whether they look at all links or only the base link. Change-Id: Ie22e6c7f163d5de8e407248b45171dc28382d2d3 --- core/java/android/net/LinkProperties.java | 64 +++++++++++++------ .../src/android/net/LinkPropertiesTest.java | 27 ++++++-- .../android/server/ConnectivityService.java | 2 +- 3 files changed, 69 insertions(+), 24 deletions(-) diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java index 75f8b5948b..6ab810c68d 100644 --- a/core/java/android/net/LinkProperties.java +++ b/core/java/android/net/LinkProperties.java @@ -128,6 +128,9 @@ public class LinkProperties implements Parcelable { return interfaceNames; } + /** + * Returns all the addresses on this link. + */ public Collection getAddresses() { Collection addresses = new ArrayList(); for (LinkAddress linkAddress : mLinkAddresses) { @@ -136,14 +139,43 @@ public class LinkProperties implements Parcelable { return Collections.unmodifiableCollection(addresses); } + /** + * Returns all the addresses on this link and all the links stacked above it. + */ + public Collection getAllAddresses() { + Collection addresses = new ArrayList(); + for (LinkAddress linkAddress : mLinkAddresses) { + addresses.add(linkAddress.getAddress()); + } + for (LinkProperties stacked: mStackedLinks.values()) { + addresses.addAll(stacked.getAllAddresses()); + } + return addresses; + } + public void addLinkAddress(LinkAddress address) { if (address != null) mLinkAddresses.add(address); } + /** + * Returns all the addresses on this link. + */ public Collection getLinkAddresses() { return Collections.unmodifiableCollection(mLinkAddresses); } + /** + * Returns all the addresses on this link and all the links stacked above it. + */ + public Collection getAllLinkAddresses() { + Collection addresses = new ArrayList(); + addresses.addAll(mLinkAddresses); + for (LinkProperties stacked: mStackedLinks.values()) { + addresses.addAll(stacked.getAllLinkAddresses()); + } + return addresses; + } + public void addDns(InetAddress dns) { if (dns != null) mDnses.add(dns); } @@ -426,13 +458,11 @@ public class LinkProperties implements Parcelable { } /** - * Return two lists, a list of addresses that would be removed from - * mLinkAddresses and a list of addresses that would be added to - * mLinkAddress which would then result in target and mLinkAddresses - * being the same list. + * Compares the addresses in this LinkProperties with another + * LinkProperties, examining only addresses on the base link. * - * @param target is a LinkProperties with the new list of addresses - * @return the removed and added lists. + * @param target a LinkProperties with the new list of addresses + * @return the differences between the addresses. */ public CompareResult compareAddresses(LinkProperties target) { /* @@ -456,13 +486,11 @@ public class LinkProperties implements Parcelable { } /** - * Return two lists, a list of dns addresses that would be removed from - * mDnses and a list of addresses that would be added to - * mDnses which would then result in target and mDnses - * being the same list. + * Compares the DNS addresses in this LinkProperties with another + * LinkProperties, examining only DNS addresses on the base link. * - * @param target is a LinkProperties with the new list of dns addresses - * @return the removed and added lists. + * @param target a LinkProperties with the new list of dns addresses + * @return the differences between the DNS addresses. */ public CompareResult compareDnses(LinkProperties target) { /* @@ -487,15 +515,13 @@ public class LinkProperties implements Parcelable { } /** - * Return two lists, a list of routes that would be removed from - * mRoutes and a list of routes that would be added to - * mRoutes which would then result in target and mRoutes - * being the same list. + * Compares all routes in this LinkProperties with another LinkProperties, + * examining both the the base link and all stacked links. * - * @param target is a LinkProperties with the new list of routes - * @return the removed and added lists. + * @param target a LinkProperties with the new list of routes + * @return the differences between the routes. */ - public CompareResult compareRoutes(LinkProperties target) { + public CompareResult compareAllRoutes(LinkProperties target) { /* * Duplicate the RouteInfos into removed, we will be removing * routes which are common between mRoutes and target diff --git a/core/tests/coretests/src/android/net/LinkPropertiesTest.java b/core/tests/coretests/src/android/net/LinkPropertiesTest.java index d6a7ee27a2..d8290f494b 100644 --- a/core/tests/coretests/src/android/net/LinkPropertiesTest.java +++ b/core/tests/coretests/src/android/net/LinkPropertiesTest.java @@ -273,28 +273,47 @@ public class LinkPropertiesTest extends TestCase { // Check comparisons work. LinkProperties lp2 = new LinkProperties(lp); assertAllRoutesHaveInterface("wlan0", lp); - assertEquals(0, lp.compareRoutes(lp2).added.size()); - assertEquals(0, lp.compareRoutes(lp2).removed.size()); + assertEquals(0, lp.compareAllRoutes(lp2).added.size()); + assertEquals(0, lp.compareAllRoutes(lp2).removed.size()); lp2.setInterfaceName("p2p0"); assertAllRoutesHaveInterface("p2p0", lp2); - assertEquals(3, lp.compareRoutes(lp2).added.size()); - assertEquals(3, lp.compareRoutes(lp2).removed.size()); + assertEquals(3, lp.compareAllRoutes(lp2).added.size()); + assertEquals(3, lp.compareAllRoutes(lp2).removed.size()); } @SmallTest public void testStackedInterfaces() { LinkProperties rmnet0 = new LinkProperties(); rmnet0.setInterfaceName("rmnet0"); + rmnet0.addLinkAddress(new LinkAddress( + NetworkUtils.numericToInetAddress(ADDRV6), 128)); LinkProperties clat4 = new LinkProperties(); clat4.setInterfaceName("clat4"); + clat4.addLinkAddress(new LinkAddress( + NetworkUtils.numericToInetAddress(ADDRV4), 32)); assertEquals(0, rmnet0.getStackedLinks().size()); + assertEquals(1, rmnet0.getAddresses().size()); + assertEquals(1, rmnet0.getLinkAddresses().size()); + assertEquals(1, rmnet0.getAllAddresses().size()); + assertEquals(1, rmnet0.getAllLinkAddresses().size()); + rmnet0.addStackedLink(clat4); assertEquals(1, rmnet0.getStackedLinks().size()); + assertEquals(1, rmnet0.getAddresses().size()); + assertEquals(1, rmnet0.getLinkAddresses().size()); + assertEquals(2, rmnet0.getAllAddresses().size()); + assertEquals(2, rmnet0.getAllLinkAddresses().size()); + rmnet0.addStackedLink(clat4); assertEquals(1, rmnet0.getStackedLinks().size()); + assertEquals(1, rmnet0.getAddresses().size()); + assertEquals(1, rmnet0.getLinkAddresses().size()); + assertEquals(2, rmnet0.getAllAddresses().size()); + assertEquals(2, rmnet0.getAllLinkAddresses().size()); + assertEquals(0, clat4.getStackedLinks().size()); // Modify an item in the returned collection to see what happens. diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java index 9615ff5fb8..af9fdffe88 100644 --- a/services/java/com/android/server/ConnectivityService.java +++ b/services/java/com/android/server/ConnectivityService.java @@ -2433,7 +2433,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { CompareResult routeDiff = new CompareResult(); if (curLp != null) { // check for the delta between the current set and the new - routeDiff = curLp.compareRoutes(newLp); + routeDiff = curLp.compareAllRoutes(newLp); dnsDiff = curLp.compareDnses(newLp); } else if (newLp != null) { routeDiff.added = newLp.getAllRoutes();