Merge "[AWARE] Add API to expose supported cipher suites"
diff --git a/tests/cts/net/src/android/net/cts/MacAddressTest.java b/tests/cts/net/src/android/net/cts/MacAddressTest.java
index af1e760..4d25e62 100644
--- a/tests/cts/net/src/android/net/cts/MacAddressTest.java
+++ b/tests/cts/net/src/android/net/cts/MacAddressTest.java
@@ -20,6 +20,11 @@
 import static android.net.MacAddress.TYPE_MULTICAST;
 import static android.net.MacAddress.TYPE_UNICAST;
 
+import static com.android.testutils.ParcelUtilsKt.assertParcelSane;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import android.net.MacAddress;
@@ -30,6 +35,7 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.net.Inet6Address;
 import java.util.Arrays;
 
 @SmallTest
@@ -161,4 +167,57 @@
         } catch (NullPointerException excepted) {
         }
     }
+
+    @Test
+    public void testMatches() {
+        // match 4 bytes prefix
+        assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
+                MacAddress.fromString("aa:bb:cc:dd:00:00"),
+                MacAddress.fromString("ff:ff:ff:ff:00:00")));
+
+        // match bytes 0,1,2 and 5
+        assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
+                MacAddress.fromString("aa:bb:cc:00:00:11"),
+                MacAddress.fromString("ff:ff:ff:00:00:ff")));
+
+        // match 34 bit prefix
+        assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
+                MacAddress.fromString("aa:bb:cc:dd:c0:00"),
+                MacAddress.fromString("ff:ff:ff:ff:c0:00")));
+
+        // fail to match 36 bit prefix
+        assertFalse(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
+                MacAddress.fromString("aa:bb:cc:dd:40:00"),
+                MacAddress.fromString("ff:ff:ff:ff:f0:00")));
+
+        // match all 6 bytes
+        assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
+                MacAddress.fromString("aa:bb:cc:dd:ee:11"),
+                MacAddress.fromString("ff:ff:ff:ff:ff:ff")));
+
+        // match none of 6 bytes
+        assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
+                MacAddress.fromString("00:00:00:00:00:00"),
+                MacAddress.fromString("00:00:00:00:00:00")));
+    }
+
+    /**
+     * Tests that link-local address generation from MAC is valid.
+     */
+    @Test
+    public void testLinkLocalFromMacGeneration() {
+        final MacAddress mac = MacAddress.fromString("52:74:f2:b1:a8:7f");
+        final byte[] inet6ll = {(byte) 0xfe, (byte) 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
+                0x74, (byte) 0xf2, (byte) 0xff, (byte) 0xfe, (byte) 0xb1, (byte) 0xa8, 0x7f};
+        final Inet6Address llv6 = mac.getLinkLocalIpv6FromEui48Mac();
+        assertTrue(llv6.isLinkLocalAddress());
+        assertArrayEquals(inet6ll, llv6.getAddress());
+    }
+
+    @Test
+    public void testParcelMacAddress() {
+        final MacAddress mac = MacAddress.fromString("52:74:f2:b1:a8:7f");
+
+        assertParcelSane(mac, 1);
+    }
 }
diff --git a/tests/cts/net/src/android/net/wifi/p2p/cts/WifiP2pConfigTest.java b/tests/cts/net/src/android/net/wifi/p2p/cts/WifiP2pConfigTest.java
index 639db8a..e93d573 100644
--- a/tests/cts/net/src/android/net/wifi/p2p/cts/WifiP2pConfigTest.java
+++ b/tests/cts/net/src/android/net/wifi/p2p/cts/WifiP2pConfigTest.java
@@ -22,41 +22,41 @@
 import android.test.AndroidTestCase;
 
 public class WifiP2pConfigTest extends AndroidTestCase {
-    static final String TEST_NETWORK_NAME = "DIRECT-xy-Hello";
-    static final String TEST_PASSPHRASE = "8etterW0r1d";
-    static final int TEST_OWNER_BAND = WifiP2pConfig.GROUP_OWNER_BAND_5GHZ;
-    static final int TEST_OWNER_FREQ = 2447;
-    static final String TEST_DEVICE_ADDRESS = "aa:bb:cc:dd:ee:ff";
+    private static final String TEST_NETWORK_NAME = "DIRECT-xy-Hello";
+    private static final String TEST_PASSPHRASE = "8etterW0r1d";
+    private static final int TEST_OWNER_BAND = WifiP2pConfig.GROUP_OWNER_BAND_5GHZ;
+    private static final int TEST_OWNER_FREQ = 2447;
+    private static final String TEST_DEVICE_ADDRESS = "aa:bb:cc:dd:ee:ff";
 
     public void testWifiP2pConfigBuilderForPersist() {
-        WifiP2pConfig.Builder builder = new WifiP2pConfig.Builder();
-        builder.setNetworkName(TEST_NETWORK_NAME)
+        WifiP2pConfig config = new WifiP2pConfig.Builder()
+                .setNetworkName(TEST_NETWORK_NAME)
                 .setPassphrase(TEST_PASSPHRASE)
                 .setGroupOperatingBand(TEST_OWNER_BAND)
                 .setDeviceAddress(MacAddress.fromString(TEST_DEVICE_ADDRESS))
-                .enablePersistentMode(true);
-        WifiP2pConfig config = builder.build();
+                .enablePersistentMode(true)
+                .build();
 
-        assertTrue(config.deviceAddress.equals(TEST_DEVICE_ADDRESS));
-        assertTrue(config.networkName.equals(TEST_NETWORK_NAME));
-        assertTrue(config.passphrase.equals(TEST_PASSPHRASE));
-        assertEquals(config.groupOwnerBand, TEST_OWNER_BAND);
-        assertEquals(config.netId, WifiP2pGroup.PERSISTENT_NET_ID);
+        assertEquals(config.deviceAddress, TEST_DEVICE_ADDRESS);
+        assertEquals(config.getNetworkName(), TEST_NETWORK_NAME);
+        assertEquals(config.getPassphrase(), TEST_PASSPHRASE);
+        assertEquals(config.getGroupOwnerBand(), TEST_OWNER_BAND);
+        assertEquals(config.getNetworkId(), WifiP2pGroup.PERSISTENT_NET_ID);
     }
 
     public void testWifiP2pConfigBuilderForNonPersist() {
-        WifiP2pConfig.Builder builder = new WifiP2pConfig.Builder();
-        builder.setNetworkName(TEST_NETWORK_NAME)
+        WifiP2pConfig config = new WifiP2pConfig.Builder()
+                .setNetworkName(TEST_NETWORK_NAME)
                 .setPassphrase(TEST_PASSPHRASE)
                 .setGroupOperatingFrequency(TEST_OWNER_FREQ)
                 .setDeviceAddress(MacAddress.fromString(TEST_DEVICE_ADDRESS))
-                .enablePersistentMode(false);
-        WifiP2pConfig config = builder.build();
+                .enablePersistentMode(false)
+                .build();
 
-        assertTrue(config.deviceAddress.equals(TEST_DEVICE_ADDRESS));
-        assertTrue(config.networkName.equals(TEST_NETWORK_NAME));
-        assertTrue(config.passphrase.equals(TEST_PASSPHRASE));
-        assertEquals(config.groupOwnerBand, TEST_OWNER_FREQ);
-        assertEquals(config.netId, WifiP2pGroup.TEMPORARY_NET_ID);
+        assertEquals(config.deviceAddress, TEST_DEVICE_ADDRESS);
+        assertEquals(config.getNetworkName(), TEST_NETWORK_NAME);
+        assertEquals(config.getPassphrase(), TEST_PASSPHRASE);
+        assertEquals(config.getGroupOwnerBand(), TEST_OWNER_FREQ);
+        assertEquals(config.getNetworkId(), WifiP2pGroup.TEMPORARY_NET_ID);
     }
 }
diff --git a/tests/cts/tethering/Android.bp b/tests/cts/tethering/Android.bp
new file mode 100644
index 0000000..0f98125
--- /dev/null
+++ b/tests/cts/tethering/Android.bp
@@ -0,0 +1,44 @@
+// 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.
+
+android_test {
+    name: "CtsTetheringTest",
+    defaults: ["cts_defaults"],
+
+    libs: [
+        "android.test.base.stubs",
+    ],
+
+    srcs: [
+        "src/**/*.java",
+    ],
+
+    static_libs: [
+        "compatibility-device-util-axt",
+        "ctstestrunner-axt",
+        "junit",
+        "junit-params",
+    ],
+
+    // Change to system current when TetheringManager move to bootclass path.
+    platform_apis: true,
+
+    // Tag this module as a cts test artifact
+    test_suites: [
+        "cts",
+        "general-tests",
+        "mts",
+    ],
+
+}
diff --git a/tests/cts/tethering/AndroidManifest.xml b/tests/cts/tethering/AndroidManifest.xml
new file mode 100644
index 0000000..665002e
--- /dev/null
+++ b/tests/cts/tethering/AndroidManifest.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.tethering.cts">
+
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+
+    <application android:debuggable="true">
+        <uses-library android:name="android.test.runner" />
+    </application>
+    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+                     android:targetPackage="android.tethering.cts"
+                     android:label="CTS tests of android.tethering">
+        <meta-data android:name="listener"
+            android:value="com.android.cts.runner.CtsTestRunListener" />
+    </instrumentation>
+
+</manifest>
diff --git a/tests/cts/tethering/AndroidTest.xml b/tests/cts/tethering/AndroidTest.xml
new file mode 100644
index 0000000..217d53a
--- /dev/null
+++ b/tests/cts/tethering/AndroidTest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<configuration description="Config for CTS Tethering test cases">
+    <option name="test-suite-tag" value="cts" />
+    <option name="config-descriptor:metadata" key="component" value="networking" />
+    <option name="config-descriptor:metadata" key="parameter" value="not_instant_app" />
+    <option name="config-descriptor:metadata" key="parameter" value="not_multi_abi" />
+    <option name="not-shardable" value="true" />
+    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+        <option name="cleanup-apks" value="true" />
+        <option name="test-file-name" value="CtsTetheringTest.apk" />
+    </target_preparer>
+    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+        <option name="package" value="android.tethering.cts" />
+    </test>
+</configuration>
diff --git a/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java b/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java
new file mode 100644
index 0000000..9efb8f3
--- /dev/null
+++ b/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java
@@ -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.tethering.test;
+
+import static org.junit.Assert.fail;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.os.ConditionVariable;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+@RunWith(AndroidJUnit4.class)
+public class TetheringManagerTest {
+
+    private Context mContext;
+
+    private ConnectivityManager mCM;
+
+    private TetherChangeReceiver mTetherChangeReceiver;
+
+    private String[] mTetheredList;
+
+    private static final int DEFAULT_TIMEOUT_MS = 60_000;
+
+    @Before
+    public void setUp() throws Exception {
+        InstrumentationRegistry.getInstrumentation()
+                .getUiAutomation()
+                .adoptShellPermissionIdentity();
+        mContext = InstrumentationRegistry.getContext();
+        mCM = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
+        mTetherChangeReceiver = new TetherChangeReceiver();
+        final IntentFilter filter = new IntentFilter(
+                ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
+        final Intent intent = mContext.registerReceiver(mTetherChangeReceiver, filter);
+        if (intent != null) mTetherChangeReceiver.onReceive(null, intent);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mContext.unregisterReceiver(mTetherChangeReceiver);
+        InstrumentationRegistry.getInstrumentation()
+                .getUiAutomation()
+                .dropShellPermissionIdentity();
+    }
+
+    private class TetherChangeReceiver extends BroadcastReceiver {
+        private class TetherState {
+            final ArrayList<String> mAvailable;
+            final ArrayList<String> mActive;
+            final ArrayList<String> mErrored;
+
+            TetherState(Intent intent) {
+                mAvailable = intent.getStringArrayListExtra(
+                        ConnectivityManager.EXTRA_AVAILABLE_TETHER);
+                mActive = intent.getStringArrayListExtra(
+                        ConnectivityManager.EXTRA_ACTIVE_TETHER);
+                mErrored = intent.getStringArrayListExtra(
+                        ConnectivityManager.EXTRA_ERRORED_TETHER);
+            }
+        }
+
+        @Override
+        public void onReceive(Context content, Intent intent) {
+            String action = intent.getAction();
+            if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
+                mResult.add(new TetherState(intent));
+            }
+        }
+
+        public final LinkedBlockingQueue<TetherState> mResult = new LinkedBlockingQueue<>();
+
+        // This method expects either an event where one of the interfaces is active, or an event
+        // where one of the interface is available followed by one where one of the interfaces is
+        // active.
+        public void expectActiveTethering(String[] ifaceRegexs) {
+            TetherState state = pollAndAssertNoError(DEFAULT_TIMEOUT_MS);
+            if (state == null) fail("Do not receive tethering state change broadcast");
+
+            if (isIfaceActive(ifaceRegexs, state)) return;
+
+            if (isIfaceAvailable(ifaceRegexs, state)) {
+                state = pollAndAssertNoError(DEFAULT_TIMEOUT_MS);
+                if (isIfaceActive(ifaceRegexs, state)) return;
+            }
+
+            fail("Tethering is not actived, available ifaces: " + state.mAvailable.toString()
+                    + ", active ifaces: " + state.mActive.toString());
+        }
+
+        private TetherState pollAndAssertNoError(final int timeout) {
+            final TetherState state = pollTetherState(timeout);
+            assertNoErroredIfaces(state);
+            return state;
+        }
+
+        private TetherState pollTetherState(final int timeout) {
+            try {
+                return mResult.poll(timeout, TimeUnit.MILLISECONDS);
+            } catch (InterruptedException e) {
+                fail("No result after " + timeout + " ms");
+                return null;
+            }
+        }
+
+        private boolean isIfaceActive(final String[] ifaceRegexs, final TetherState state) {
+            return isIfaceMatch(ifaceRegexs, state.mActive);
+        }
+
+        private boolean isIfaceAvailable(final String[] ifaceRegexs, final TetherState state) {
+            return isIfaceMatch(ifaceRegexs, state.mAvailable);
+        }
+
+        // This method requires a broadcast to have been recorded iff the timeout is non-zero.
+        public void expectNoActiveTethering(final int timeout) {
+            final TetherState state = pollAndAssertNoError(timeout);
+
+            if (state == null) {
+                if (timeout != 0) {
+                    fail("Do not receive tethering state change broadcast");
+                }
+                return;
+            }
+
+            assertNoActiveIfaces(state);
+
+            for (final TetherState ts : mResult) {
+                assertNoErroredIfaces(ts);
+
+                assertNoActiveIfaces(ts);
+            }
+        }
+
+        private void assertNoErroredIfaces(final TetherState state) {
+            if (state == null || state.mErrored == null) return;
+
+            if (state.mErrored.size() > 0) {
+                fail("Found failed tethering interfaces: " + state.mErrored.toArray());
+            }
+        }
+
+        private void assertNoActiveIfaces(final TetherState state) {
+            if (state.mActive != null && state.mActive.size() > 0) {
+                fail("Found active tethering interface: " + state.mActive.toArray());
+            }
+        }
+    }
+
+    private class OnStartTetheringCallback extends
+            ConnectivityManager.OnStartTetheringCallback {
+        @Override
+        public void onTetheringStarted() {
+            // Do nothing, TetherChangeReceiver will wait until it receives the broadcast.
+        }
+
+        @Override
+        public void onTetheringFailed() {
+            fail("startTethering fail");
+        }
+    }
+
+    private static boolean isIfaceMatch(final String[] ifaceRegexs,
+            final ArrayList<String> ifaces) {
+        if (ifaceRegexs == null) fail("ifaceRegexs should not be null");
+
+        if (ifaces == null) return false;
+
+        for (String s : ifaces) {
+            for (String regex : ifaceRegexs) {
+                if (s.matches(regex)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    @Test
+    public void testStartTetheringWithStateChangeBroadcast() throws Exception {
+        if (!mCM.isTetheringSupported()) return;
+
+        final String[] wifiRegexs = mCM.getTetherableWifiRegexs();
+        if (wifiRegexs.length == 0) return;
+
+        mTetherChangeReceiver.expectNoActiveTethering(0 /** timeout */);
+
+        final OnStartTetheringCallback startTetheringCallback = new OnStartTetheringCallback();
+        mCM.startTethering(ConnectivityManager.TETHERING_WIFI, true, startTetheringCallback);
+        mTetherChangeReceiver.expectActiveTethering(wifiRegexs);
+
+        mCM.stopTethering(ConnectivityManager.TETHERING_WIFI);
+        mTetherChangeReceiver.expectNoActiveTethering(DEFAULT_TIMEOUT_MS);
+    }
+}