diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2018-06-03 07:21:05 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2018-06-03 07:21:05 +0000 |
commit | 35a4b41ad8e39ee27d3be87cca34712e2eb450e4 (patch) | |
tree | 3a5c9c6dc299819de04cee113981e12a81b7a65c | |
parent | aa6d557b954e89db26cfbb26ebbaa75e1fdfda69 (diff) | |
parent | 1e53f6b418d977186f945cd9d646f18f770db346 (diff) | |
download | ethernet-pie-r2-s1-release.tar.gz |
Snap for 4818534 from 1e53f6b418d977186f945cd9d646f18f770db346 to pi-releaseandroid-wear-9.0.0_r9android-wear-9.0.0_r8android-wear-9.0.0_r7android-wear-9.0.0_r6android-wear-9.0.0_r5android-wear-9.0.0_r4android-wear-9.0.0_r34android-wear-9.0.0_r33android-wear-9.0.0_r32android-wear-9.0.0_r31android-wear-9.0.0_r30android-wear-9.0.0_r3android-wear-9.0.0_r29android-wear-9.0.0_r28android-wear-9.0.0_r27android-wear-9.0.0_r26android-wear-9.0.0_r25android-wear-9.0.0_r24android-wear-9.0.0_r23android-wear-9.0.0_r22android-wear-9.0.0_r21android-wear-9.0.0_r20android-wear-9.0.0_r2android-wear-9.0.0_r19android-wear-9.0.0_r18android-wear-9.0.0_r17android-wear-9.0.0_r16android-wear-9.0.0_r15android-wear-9.0.0_r14android-wear-9.0.0_r13android-wear-9.0.0_r12android-wear-9.0.0_r11android-wear-9.0.0_r10android-wear-9.0.0_r1android-vts-9.0_r9android-vts-9.0_r8android-vts-9.0_r7android-vts-9.0_r6android-vts-9.0_r5android-vts-9.0_r4android-vts-9.0_r19android-vts-9.0_r18android-vts-9.0_r17android-vts-9.0_r16android-vts-9.0_r15android-vts-9.0_r14android-vts-9.0_r13android-vts-9.0_r12android-vts-9.0_r11android-vts-9.0_r10android-security-9.0.0_r76android-security-9.0.0_r75android-security-9.0.0_r74android-security-9.0.0_r73android-security-9.0.0_r72android-security-9.0.0_r71android-security-9.0.0_r70android-security-9.0.0_r69android-security-9.0.0_r68android-security-9.0.0_r67android-security-9.0.0_r66android-security-9.0.0_r65android-security-9.0.0_r64android-security-9.0.0_r63android-security-9.0.0_r62android-cts-9.0_r9android-cts-9.0_r8android-cts-9.0_r7android-cts-9.0_r6android-cts-9.0_r5android-cts-9.0_r4android-cts-9.0_r3android-cts-9.0_r20android-cts-9.0_r2android-cts-9.0_r19android-cts-9.0_r18android-cts-9.0_r17android-cts-9.0_r16android-cts-9.0_r15android-cts-9.0_r14android-cts-9.0_r13android-cts-9.0_r12android-cts-9.0_r11android-cts-9.0_r10android-cts-9.0_r1android-9.0.0_r9android-9.0.0_r8android-9.0.0_r7android-9.0.0_r61android-9.0.0_r60android-9.0.0_r6android-9.0.0_r59android-9.0.0_r58android-9.0.0_r57android-9.0.0_r56android-9.0.0_r55android-9.0.0_r54android-9.0.0_r53android-9.0.0_r52android-9.0.0_r51android-9.0.0_r50android-9.0.0_r5android-9.0.0_r49android-9.0.0_r48android-9.0.0_r3android-9.0.0_r2android-9.0.0_r18android-9.0.0_r17android-9.0.0_r10android-9.0.0_r1security-pi-releasepie-vts-releasepie-security-releasepie-s2-releasepie-release-2pie-releasepie-r2-s2-releasepie-r2-s1-releasepie-r2-releasepie-cts-release
Change-Id: I8a39d0b29bd6f3055d316be12f3a6033372f30a8
-rw-r--r-- | Android.mk | 2 | ||||
-rw-r--r-- | java/com/android/server/ethernet/EthernetTracker.java | 58 | ||||
-rw-r--r-- | tests/Android.mk | 36 | ||||
-rw-r--r-- | tests/AndroidManifest.xml | 28 | ||||
-rw-r--r-- | tests/java/com/android/server/ethernet/EthernetTrackerTest.java | 126 |
5 files changed, 244 insertions, 6 deletions
@@ -28,3 +28,5 @@ LOCAL_JAVA_LIBRARIES := services LOCAL_MODULE := ethernet-service include $(BUILD_JAVA_LIBRARY) + +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/java/com/android/server/ethernet/EthernetTracker.java b/java/com/android/server/ethernet/EthernetTracker.java index 688d84a..00eedd5 100644 --- a/java/com/android/server/ethernet/EthernetTracker.java +++ b/java/com/android/server/ethernet/EthernetTracker.java @@ -36,10 +36,13 @@ import android.text.TextUtils; import android.util.ArrayMap; import android.util.Log; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.IndentingPrintWriter; import com.android.server.net.BaseNetworkObserver; import java.io.FileDescriptor; +import java.net.InetAddress; +import java.util.ArrayList; import java.util.concurrent.ConcurrentHashMap; /** @@ -300,7 +303,7 @@ final class EthernetTracker { mNetworkCapabilities.put(name, nc); if (tokens.length > 2 && !TextUtils.isEmpty(tokens[2])) { - IpConfiguration ipConfig = createStaticIpConfiguration(tokens[2]); + IpConfiguration ipConfig = parseStaticIpConfiguration(tokens[2]); mIpConfigurations.put(name, ipConfig); } } @@ -342,11 +345,54 @@ final class EthernetTracker { return nc; } - private static IpConfiguration createStaticIpConfiguration(String strIpAddress) { - StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration(); - staticIpConfiguration.ipAddress = new LinkAddress(strIpAddress); - return new IpConfiguration( - IpAssignment.STATIC, ProxySettings.NONE, staticIpConfiguration, null); + /** + * Parses static IP configuration. + * + * @param staticIpConfig represents static IP configuration in the following format: {@code + * ip=<ip-address/mask> gateway=<ip-address> dns=<comma-sep-ip-addresses> + * domains=<comma-sep-domains>} + */ + @VisibleForTesting + static IpConfiguration parseStaticIpConfiguration(String staticIpConfig) { + StaticIpConfiguration ipConfig = new StaticIpConfiguration(); + + for (String keyValueAsString : staticIpConfig.trim().split(" ")) { + if (TextUtils.isEmpty(keyValueAsString)) continue; + + String[] pair = keyValueAsString.split("="); + if (pair.length != 2) { + throw new IllegalArgumentException("Unexpected token: " + keyValueAsString + + " in " + staticIpConfig); + } + + String key = pair[0]; + String value = pair[1]; + + switch (key) { + case "ip": + ipConfig.ipAddress = new LinkAddress(value); + break; + case "domains": + ipConfig.domains = value; + break; + case "gateway": + ipConfig.gateway = InetAddress.parseNumericAddress(value); + break; + case "dns": { + ArrayList<InetAddress> dnsAddresses = new ArrayList<>(); + for (String address: value.split(",")) { + dnsAddresses.add(InetAddress.parseNumericAddress(address)); + } + ipConfig.dnsServers.addAll(dnsAddresses); + break; + } + default : { + throw new IllegalArgumentException("Unexpected key: " + key + + " in " + staticIpConfig); + } + } + } + return new IpConfiguration(IpAssignment.STATIC, ProxySettings.NONE, ipConfig, null); } private static IpConfiguration createDefaultIpConfiguration() { diff --git a/tests/Android.mk b/tests/Android.mk new file mode 100644 index 0000000..6b2c103 --- /dev/null +++ b/tests/Android.mk @@ -0,0 +1,36 @@ +# Copyright (C) 2018 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. +# + +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := $(call all-java-files-under, java) + +LOCAL_PACKAGE_NAME := EthernetServiceTests + +LOCAL_CERTIFICATE := platform +LOCAL_PRIVATE_PLATFORM_APIS := true + +LOCAL_MODULE_TAGS := tests + +LOCAL_JAVA_LIBRARIES := \ + android.test.runner \ + android.test.base + +LOCAL_STATIC_JAVA_LIBRARIES := \ + android-support-test \ + ethernet-service + +include $(BUILD_PACKAGE) diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml new file mode 100644 index 0000000..1bc0775 --- /dev/null +++ b/tests/AndroidManifest.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright (C) 2018 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 + --> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.server.ethernet.tests"> + + <application android:label="EthernetServiceTests"> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner" + android:targetPackage="com.android.server.ethernet.tests" + android:label="Ethernet Service Tests" /> +</manifest> diff --git a/tests/java/com/android/server/ethernet/EthernetTrackerTest.java b/tests/java/com/android/server/ethernet/EthernetTrackerTest.java new file mode 100644 index 0000000..70d316d --- /dev/null +++ b/tests/java/com/android/server/ethernet/EthernetTrackerTest.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2018 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 com.android.server.ethernet; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import android.net.IpConfiguration; +import android.net.IpConfiguration.IpAssignment; +import android.net.IpConfiguration.ProxySettings; +import android.net.LinkAddress; +import android.net.StaticIpConfiguration; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.net.InetAddress; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class EthernetTrackerTest { + + @Test + public void createStaticIpConfiguration() { + assertStaticConfiguration("", new StaticIpConfiguration()); + + assertStaticConfiguration( + "ip=192.0.2.10/24", + new StaticIpConfigBuilder().setIp("192.0.2.10/24").build()); + + assertStaticConfiguration( + "ip=192.0.2.10/24 dns=4.4.4.4,8.8.8.8 gateway=192.0.2.1 domains=android", + new StaticIpConfigBuilder() + .setIp("192.0.2.10/24") + .setDns(new String[] {"4.4.4.4", "8.8.8.8"}) + .setGateway("192.0.2.1") + .setDomains("android") + .build()); + + // Verify order doesn't matter + assertStaticConfiguration( + "domains=android ip=192.0.2.10/24 gateway=192.0.2.1 dns=4.4.4.4,8.8.8.8 ", + new StaticIpConfigBuilder() + .setIp("192.0.2.10/24") + .setDns(new String[] {"4.4.4.4", "8.8.8.8"}) + .setGateway("192.0.2.1") + .setDomains("android") + .build()); + } + + @Test + public void createStaticIpConfiguration_Bad() { + assertFails("ip=192.0.2.1/24 gateway= blah=20.20.20.20"); // Unknown key + assertFails("ip=192.0.2.1"); // mask is missing + assertFails("ip=a.b.c"); // not a valid ip address + assertFails("dns=4.4.4.4,1.2.3.A"); // not valid ip address in dns + assertFails("="); // Key and value is empty + assertFails("ip="); // Value is empty + assertFails("ip=192.0.2.1/24 gateway="); // Gateway is empty + } + + private void assertFails(String config) { + try { + EthernetTracker.parseStaticIpConfiguration(config); + fail("Expected to fail: " + config); + } catch (IllegalArgumentException e) { + // expected + } + } + + private void assertStaticConfiguration(String configAsString, + StaticIpConfiguration expectedStaticIpConfig) { + IpConfiguration expectedIpConfiguration = new IpConfiguration(IpAssignment.STATIC, + ProxySettings.NONE, expectedStaticIpConfig, null); + + assertEquals(expectedIpConfiguration, + EthernetTracker.parseStaticIpConfiguration(configAsString)); + } + + private static class StaticIpConfigBuilder { + private final StaticIpConfiguration config = new StaticIpConfiguration(); + + StaticIpConfigBuilder setIp(String address) { + config.ipAddress = new LinkAddress(address); + return this; + } + + StaticIpConfigBuilder setDns(String[] dnsArray) { + for (String dns : dnsArray) { + config.dnsServers.add(InetAddress.parseNumericAddress(dns)); + } + return this; + } + + StaticIpConfigBuilder setGateway(String gateway) { + config.gateway = InetAddress.parseNumericAddress(gateway); + return this; + } + + StaticIpConfigBuilder setDomains(String domains) { + config.domains = domains; + return this; + } + + + StaticIpConfiguration build() { + return new StaticIpConfiguration(config); + } + } +} |