Add stable AIDL parcelables for LinkProperties

Test: atest android.net.shared.LinkPropertiesParcelableUtilTest
Bug: b/112869080
Change-Id: I2b69980e25bae9f51fb5b8cd3cafb403e6bd53da
This commit is contained in:
Remi NGUYEN VAN
2019-01-08 10:46:21 +09:00
parent be8881da3b
commit 4855a2e2c6
9 changed files with 618 additions and 0 deletions

View File

@@ -830,7 +830,12 @@ aidl_interface {
"core/java/android/net/IIpMemoryStore.aidl",
"core/java/android/net/INetworkStackConnector.aidl",
"core/java/android/net/INetworkStackStatusCallback.aidl",
"core/java/android/net/IpPrefixParcelable.aidl",
"core/java/android/net/LinkAddressParcelable.aidl",
"core/java/android/net/LinkPropertiesParcelable.aidl",
"core/java/android/net/PrivateDnsConfigParcel.aidl",
"core/java/android/net/ProxyInfoParcelable.aidl",
"core/java/android/net/RouteInfoParcelable.aidl",
"core/java/android/net/dhcp/DhcpServingParamsParcel.aidl",
"core/java/android/net/dhcp/IDhcpServer.aidl",
"core/java/android/net/dhcp/IDhcpServerCallbacks.aidl",

View File

@@ -0,0 +1,22 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
parcelable IpPrefixParcelable {
String address;
int prefixLength;
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
parcelable LinkAddressParcelable {
String address;
int prefixLength;
int flags;
int scope;
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
import android.net.IpPrefixParcelable;
import android.net.LinkAddressParcelable;
import android.net.ProxyInfoParcelable;
import android.net.RouteInfoParcelable;
parcelable LinkPropertiesParcelable {
String ifaceName;
LinkAddressParcelable[] linkAddresses;
String[] dnses;
String[] pcscfs;
String[] validatedPrivateDnses;
boolean usePrivateDns;
String privateDnsServerName;
String domains;
RouteInfoParcelable[] routes;
ProxyInfoParcelable httpProxy;
int mtu;
String tcpBufferSizes;
IpPrefixParcelable nat64Prefix;
LinkPropertiesParcelable[] stackedLinks;
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
parcelable ProxyInfoParcelable {
String host;
int port;
String[] exclusionList;
String pacFileUrl;
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
import android.net.IpPrefixParcelable;
parcelable RouteInfoParcelable {
IpPrefixParcelable destination;
String gatewayAddr;
String ifaceName;
int type;
}

View File

@@ -0,0 +1,222 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.shared;
import static android.net.shared.ParcelableUtil.fromParcelableArray;
import static android.net.shared.ParcelableUtil.toParcelableArray;
import android.annotation.Nullable;
import android.net.InetAddresses;
import android.net.IpPrefix;
import android.net.IpPrefixParcelable;
import android.net.LinkAddress;
import android.net.LinkAddressParcelable;
import android.net.LinkProperties;
import android.net.LinkPropertiesParcelable;
import android.net.ProxyInfo;
import android.net.ProxyInfoParcelable;
import android.net.RouteInfo;
import android.net.RouteInfoParcelable;
import android.net.Uri;
import java.net.InetAddress;
import java.util.Arrays;
/**
* Collection of utility methods to convert to and from stable AIDL parcelables for LinkProperties
* and its attributes.
* @hide
*/
public final class LinkPropertiesParcelableUtil {
/**
* Convert a ProxyInfo to a ProxyInfoParcelable
*/
public static ProxyInfoParcelable toStableParcelable(@Nullable ProxyInfo proxyInfo) {
if (proxyInfo == null) {
return null;
}
final ProxyInfoParcelable parcel = new ProxyInfoParcelable();
parcel.host = proxyInfo.getHost();
parcel.port = proxyInfo.getPort();
parcel.exclusionList = proxyInfo.getExclusionList();
parcel.pacFileUrl = proxyInfo.getPacFileUrl().toString();
return parcel;
}
/**
* Convert a ProxyInfoParcelable to a ProxyInfo
*/
public static ProxyInfo fromStableParcelable(@Nullable ProxyInfoParcelable parcel) {
if (parcel == null) {
return null;
}
if (Uri.EMPTY.toString().equals(parcel.pacFileUrl)) {
return ProxyInfo.buildDirectProxy(
parcel.host, parcel.port, Arrays.asList(parcel.exclusionList));
} else {
return ProxyInfo.buildPacProxy(Uri.parse(parcel.pacFileUrl));
}
}
/**
* Convert an IpPrefixParcelable to an IpPrefix
*/
public static IpPrefixParcelable toStableParcelable(@Nullable IpPrefix ipPrefix) {
if (ipPrefix == null) {
return null;
}
final IpPrefixParcelable parcel = new IpPrefixParcelable();
parcel.address = ipPrefix.getAddress().getHostAddress();
parcel.prefixLength = ipPrefix.getPrefixLength();
return parcel;
}
/**
* Convert an IpPrefix to an IpPrefixParcelable
*/
public static IpPrefix fromStableParcelable(@Nullable IpPrefixParcelable parcel) {
if (parcel == null) {
return null;
}
return new IpPrefix(InetAddresses.parseNumericAddress(parcel.address), parcel.prefixLength);
}
/**
* Convert a RouteInfoParcelable to a RouteInfo
*/
public static RouteInfoParcelable toStableParcelable(@Nullable RouteInfo routeInfo) {
if (routeInfo == null) {
return null;
}
final RouteInfoParcelable parcel = new RouteInfoParcelable();
parcel.destination = toStableParcelable(routeInfo.getDestination());
parcel.gatewayAddr = routeInfo.getGateway().getHostAddress();
parcel.ifaceName = routeInfo.getInterface();
parcel.type = routeInfo.getType();
return parcel;
}
/**
* Convert a RouteInfo to a RouteInfoParcelable
*/
public static RouteInfo fromStableParcelable(@Nullable RouteInfoParcelable parcel) {
if (parcel == null) {
return null;
}
final IpPrefix destination = fromStableParcelable(parcel.destination);
return new RouteInfo(
destination, InetAddresses.parseNumericAddress(parcel.gatewayAddr),
parcel.ifaceName, parcel.type);
}
/**
* Convert a LinkAddressParcelable to a LinkAddress
*/
public static LinkAddressParcelable toStableParcelable(@Nullable LinkAddress la) {
if (la == null) {
return null;
}
final LinkAddressParcelable parcel = new LinkAddressParcelable();
parcel.address = la.getAddress().getHostAddress();
parcel.prefixLength = la.getPrefixLength();
parcel.flags = la.getFlags();
parcel.scope = la.getScope();
return parcel;
}
/**
* Convert a LinkAddress to a LinkAddressParcelable
*/
public static LinkAddress fromStableParcelable(@Nullable LinkAddressParcelable parcel) {
if (parcel == null) {
return null;
}
return new LinkAddress(
InetAddresses.parseNumericAddress(parcel.address),
parcel.prefixLength,
parcel.flags,
parcel.scope);
}
/**
* Convert a LinkProperties to a LinkPropertiesParcelable
*/
public static LinkPropertiesParcelable toStableParcelable(@Nullable LinkProperties lp) {
if (lp == null) {
return null;
}
final LinkPropertiesParcelable parcel = new LinkPropertiesParcelable();
parcel.ifaceName = lp.getInterfaceName();
parcel.linkAddresses = toParcelableArray(
lp.getLinkAddresses(),
LinkPropertiesParcelableUtil::toStableParcelable,
LinkAddressParcelable.class);
parcel.dnses = toParcelableArray(
lp.getDnsServers(), InetAddress::getHostAddress, String.class);
parcel.pcscfs = toParcelableArray(
lp.getPcscfServers(), InetAddress::getHostAddress, String.class);
parcel.validatedPrivateDnses = toParcelableArray(
lp.getValidatedPrivateDnsServers(), InetAddress::getHostAddress, String.class);
parcel.usePrivateDns = lp.isPrivateDnsActive();
parcel.privateDnsServerName = lp.getPrivateDnsServerName();
parcel.domains = lp.getDomains();
parcel.routes = toParcelableArray(
lp.getRoutes(), LinkPropertiesParcelableUtil::toStableParcelable,
RouteInfoParcelable.class);
parcel.httpProxy = toStableParcelable(lp.getHttpProxy());
parcel.mtu = lp.getMtu();
parcel.tcpBufferSizes = lp.getTcpBufferSizes();
parcel.nat64Prefix = toStableParcelable(lp.getNat64Prefix());
parcel.stackedLinks = toParcelableArray(
lp.getStackedLinks(), LinkPropertiesParcelableUtil::toStableParcelable,
LinkPropertiesParcelable.class);
return parcel;
}
/**
* Convert a LinkPropertiesParcelable to a LinkProperties
*/
public static LinkProperties fromStableParcelable(@Nullable LinkPropertiesParcelable parcel) {
if (parcel == null) {
return null;
}
final LinkProperties lp = new LinkProperties();
lp.setInterfaceName(parcel.ifaceName);
lp.setLinkAddresses(fromParcelableArray(parcel.linkAddresses,
LinkPropertiesParcelableUtil::fromStableParcelable));
lp.setDnsServers(fromParcelableArray(parcel.dnses, InetAddresses::parseNumericAddress));
lp.setPcscfServers(fromParcelableArray(parcel.pcscfs, InetAddresses::parseNumericAddress));
lp.setValidatedPrivateDnsServers(
fromParcelableArray(parcel.validatedPrivateDnses,
InetAddresses::parseNumericAddress));
lp.setUsePrivateDns(parcel.usePrivateDns);
lp.setPrivateDnsServerName(parcel.privateDnsServerName);
lp.setDomains(parcel.domains);
for (RouteInfoParcelable route : parcel.routes) {
lp.addRoute(fromStableParcelable(route));
}
lp.setHttpProxy(fromStableParcelable(parcel.httpProxy));
lp.setMtu(parcel.mtu);
lp.setTcpBufferSizes(parcel.tcpBufferSizes);
lp.setNat64Prefix(fromStableParcelable(parcel.nat64Prefix));
for (LinkPropertiesParcelable stackedLink : parcel.stackedLinks) {
lp.addStackedLink(fromStableParcelable(stackedLink));
}
return lp;
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.shared;
import android.annotation.NonNull;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
/**
* Utility methods to help convert to/from stable parcelables.
* @hide
*/
public final class ParcelableUtil {
// Below methods could be implemented easily with streams, but streams are frowned upon in
// frameworks code.
/**
* Convert a list of BaseType items to an array of ParcelableType items using the specified
* converter function.
*/
public static <ParcelableType, BaseType> ParcelableType[] toParcelableArray(
@NonNull List<BaseType> base,
@NonNull Function<BaseType, ParcelableType> conv,
@NonNull Class<ParcelableType> parcelClass) {
final ParcelableType[] out = (ParcelableType[]) Array.newInstance(parcelClass, base.size());
int i = 0;
for (BaseType b : base) {
out[i] = conv.apply(b);
i++;
}
return out;
}
/**
* Convert an array of ParcelableType items to a list of BaseType items using the specified
* converter function.
*/
public static <ParcelableType, BaseType> ArrayList<BaseType> fromParcelableArray(
@NonNull ParcelableType[] parceled, @NonNull Function<ParcelableType, BaseType> conv) {
final ArrayList<BaseType> out = new ArrayList<>(parceled.length);
for (ParcelableType t : parceled) {
out.add(conv.apply(t));
}
return out;
}
}

View File

@@ -0,0 +1,193 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.shared;
import static android.net.shared.LinkPropertiesParcelableUtil.fromStableParcelable;
import static android.net.shared.LinkPropertiesParcelableUtil.toStableParcelable;
import static org.junit.Assert.assertEquals;
import android.net.InetAddresses;
import android.net.IpPrefix;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.ProxyInfo;
import android.net.RouteInfo;
import android.net.Uri;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
/**
* Tests for {@link LinkPropertiesParcelableUtil}
*/
@RunWith(AndroidJUnit4.class)
@SmallTest
public class LinkPropertiesParcelableUtilTest {
private LinkProperties mLinkProperties;
private static final String TEST_LINKPROPS_IFACE = "TEST_IFACE";
private static final String TEST_STACKED_LINK_1_IFACE = "TEST_STACKED_IFACE_1";
private static final String TEST_STACKED_LINK_2_IFACE = "TEST_STACKED_IFACE_2";
@Before
public void setUp() {
mLinkProperties = makeLinkProperties(TEST_LINKPROPS_IFACE);
mLinkProperties.addStackedLink(makeLinkProperties(TEST_STACKED_LINK_1_IFACE));
mLinkProperties.addStackedLink(makeLinkProperties(TEST_STACKED_LINK_2_IFACE));
}
private static LinkProperties makeLinkProperties(String iface) {
final LinkProperties lp = new LinkProperties();
lp.setInterfaceName(iface);
lp.setLinkAddresses(Arrays.asList(
new LinkAddress(InetAddresses.parseNumericAddress("192.168.0.42"), 16),
new LinkAddress(InetAddresses.parseNumericAddress("2001:db8::7"), 42)));
lp.setDnsServers(Arrays.asList(
InetAddresses.parseNumericAddress("2001:db8::42"),
InetAddresses.parseNumericAddress("192.168.1.1")
));
lp.setValidatedPrivateDnsServers(Arrays.asList(
InetAddresses.parseNumericAddress("2001:db8::43"),
InetAddresses.parseNumericAddress("192.168.42.43")
));
lp.setPcscfServers(Arrays.asList(
InetAddresses.parseNumericAddress("2001:db8::47"),
InetAddresses.parseNumericAddress("192.168.42.47")
));
lp.setUsePrivateDns(true);
lp.setPrivateDnsServerName("test.example.com");
lp.setDomains("test1.example.com,test2.example.com");
lp.addRoute(new RouteInfo(
new IpPrefix(InetAddresses.parseNumericAddress("2001:db8::44"), 45),
InetAddresses.parseNumericAddress("2001:db8::45"),
iface,
RouteInfo.RTN_UNICAST
));
lp.addRoute(new RouteInfo(
new IpPrefix(InetAddresses.parseNumericAddress("192.168.44.45"), 16),
InetAddresses.parseNumericAddress("192.168.45.1"),
iface,
RouteInfo.RTN_THROW
));
lp.setHttpProxy(new ProxyInfo("test3.example.com", 8000,
"excl1.example.com,excl2.example.com"));
lp.setMtu(5000);
lp.setTcpBufferSizes("1,2,3,4,5,6");
lp.setNat64Prefix(new IpPrefix(InetAddresses.parseNumericAddress("2001:db8::48"), 96));
// Verify that this test does not miss any new field added later.
// If any added field is not included in LinkProperties#equals, assertLinkPropertiesEquals
// must also be updated.
assertEquals(14, Arrays.stream(LinkProperties.class.getDeclaredFields())
.filter(f -> !Modifier.isStatic(f.getModifiers())).count());
return lp;
}
@Test
public void testParcelUnparcel() {
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_NullInterface() {
mLinkProperties.setInterfaceName(null);
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_NullPrivateDnsServer() {
mLinkProperties.setPrivateDnsServerName(null);
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_NullDomains() {
mLinkProperties.setDomains(null);
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_NullProxy() {
mLinkProperties.setHttpProxy(null);
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_NullTcpBufferSizes() {
mLinkProperties.setTcpBufferSizes(null);
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_EmptyLinkAddresses() {
mLinkProperties.setLinkAddresses(Collections.emptyList());
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_EmptyDnses() {
mLinkProperties.setDnsServers(Collections.emptyList());
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_EmptyValidatedPrivateDnses() {
mLinkProperties.setValidatedPrivateDnsServers(Collections.emptyList());
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_EmptyRoutes() {
for (RouteInfo r : mLinkProperties.getAllRoutes()) {
mLinkProperties.removeRoute(r);
}
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_PacFileProxyInfo() {
mLinkProperties.setHttpProxy(new ProxyInfo(Uri.parse("http://pacfile.example.com")));
doParcelUnparcelTest();
}
@Test
public void testParcelUnparcel_NullNat64Prefix() {
mLinkProperties.setNat64Prefix(null);
doParcelUnparcelTest();
}
private void doParcelUnparcelTest() {
final LinkProperties unparceled = fromStableParcelable(toStableParcelable(mLinkProperties));
assertLinkPropertiesEquals(mLinkProperties, unparceled);
}
private static void assertLinkPropertiesEquals(LinkProperties expected, LinkProperties actual) {
assertEquals(expected, actual);
// LinkProperties equals() does not include stacked links
assertEquals(expected.getStackedLinks(), actual.getStackedLinks());
}
}