Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.net.cts;
     18 
     19 import android.content.Context;
     20 import android.net.ConnectivityManager;
     21 import android.net.Network;
     22 import android.net.NetworkCapabilities;
     23 import android.net.NetworkUtils;
     24 import android.system.ErrnoException;
     25 import android.system.OsConstants;
     26 import android.test.AndroidTestCase;
     27 
     28 import java.util.ArrayList;
     29 
     30 
     31 public class MultinetworkApiTest extends AndroidTestCase {
     32 
     33     static {
     34         System.loadLibrary("nativemultinetwork_jni");
     35     }
     36 
     37     private static final String TAG = "MultinetworkNativeApiTest";
     38 
     39     /**
     40      * @return 0 on success
     41      */
     42     private static native int runGetaddrinfoCheck(long networkHandle);
     43     private static native int runSetprocnetwork(long networkHandle);
     44     private static native int runSetsocknetwork(long networkHandle);
     45     private static native int runDatagramCheck(long networkHandle);
     46 
     47     private ConnectivityManager mCM;
     48 
     49     protected void setUp() throws Exception {
     50         super.setUp();
     51         mCM = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
     52     }
     53 
     54     private Network[] getTestableNetworks() {
     55         final ArrayList<Network> testableNetworks = new ArrayList<Network>();
     56         for (Network network : mCM.getAllNetworks()) {
     57             final NetworkCapabilities nc = mCM.getNetworkCapabilities(network);
     58             if (nc != null
     59                     && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
     60                     && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
     61                 testableNetworks.add(network);
     62             }
     63         }
     64 
     65         assertTrue(
     66                 "This test requires that at least one network be connected. " +
     67                 "Please ensure that the device is connected to a network.",
     68                 testableNetworks.size() >= 1);
     69         return testableNetworks.toArray(new Network[0]);
     70     }
     71 
     72     public void testGetaddrinfo() throws ErrnoException {
     73         for (Network network : getTestableNetworks()) {
     74             int errno = runGetaddrinfoCheck(network.getNetworkHandle());
     75             if (errno != 0) {
     76                 throw new ErrnoException(
     77                         "getaddrinfo on " + mCM.getNetworkInfo(network), -errno);
     78             }
     79         }
     80     }
     81 
     82     public void testSetprocnetwork() throws ErrnoException {
     83         // Hopefully no prior test in this process space has set a default network.
     84         assertNull(mCM.getProcessDefaultNetwork());
     85         assertEquals(0, NetworkUtils.getBoundNetworkForProcess());
     86 
     87         for (Network network : getTestableNetworks()) {
     88             mCM.setProcessDefaultNetwork(null);
     89             assertNull(mCM.getProcessDefaultNetwork());
     90 
     91             int errno = runSetprocnetwork(network.getNetworkHandle());
     92             if (errno != 0) {
     93                 throw new ErrnoException(
     94                         "setprocnetwork on " + mCM.getNetworkInfo(network), -errno);
     95             }
     96             Network processDefault = mCM.getProcessDefaultNetwork();
     97             assertNotNull(processDefault);
     98             assertEquals(network, processDefault);
     99             // TODO: open DatagramSockets, connect them to 192.0.2.1 and 2001:db8::,
    100             // and ensure that the source address is in fact on this network as
    101             // determined by mCM.getLinkProperties(network).
    102 
    103             mCM.setProcessDefaultNetwork(null);
    104         }
    105 
    106         for (Network network : getTestableNetworks()) {
    107             NetworkUtils.bindProcessToNetwork(0);
    108             assertNull(mCM.getBoundNetworkForProcess());
    109 
    110             int errno = runSetprocnetwork(network.getNetworkHandle());
    111             if (errno != 0) {
    112                 throw new ErrnoException(
    113                         "setprocnetwork on " + mCM.getNetworkInfo(network), -errno);
    114             }
    115             assertEquals(network, new Network(mCM.getBoundNetworkForProcess()));
    116             // TODO: open DatagramSockets, connect them to 192.0.2.1 and 2001:db8::,
    117             // and ensure that the source address is in fact on this network as
    118             // determined by mCM.getLinkProperties(network).
    119 
    120             NetworkUtils.bindProcessToNetwork(0);
    121         }
    122     }
    123 
    124     public void testSetsocknetwork() throws ErrnoException {
    125         for (Network network : getTestableNetworks()) {
    126             int errno = runSetsocknetwork(network.getNetworkHandle());
    127             if (errno != 0) {
    128                 throw new ErrnoException(
    129                         "setsocknetwork on " + mCM.getNetworkInfo(network), -errno);
    130             }
    131         }
    132     }
    133 
    134     public void testNativeDatagramTransmission() throws ErrnoException {
    135         for (Network network : getTestableNetworks()) {
    136             int errno = runDatagramCheck(network.getNetworkHandle());
    137             if (errno != 0) {
    138                 throw new ErrnoException(
    139                         "DatagramCheck on " + mCM.getNetworkInfo(network), -errno);
    140             }
    141         }
    142     }
    143 
    144     public void testNoSuchNetwork() {
    145         final Network eNoNet = new Network(54321);
    146         assertNull(mCM.getNetworkInfo(eNoNet));
    147 
    148         final long eNoNetHandle = eNoNet.getNetworkHandle();
    149         assertEquals(-OsConstants.ENONET, runSetsocknetwork(eNoNetHandle));
    150         assertEquals(-OsConstants.ENONET, runSetprocnetwork(eNoNetHandle));
    151         // TODO: correct test permissions so this call is not silently re-mapped
    152         // to query on the default network.
    153         // assertEquals(-OsConstants.ENONET, runGetaddrinfoCheck(eNoNetHandle));
    154     }
    155 
    156     public void testNetworkHandle() {
    157         // Test Network -> NetworkHandle -> Network results in the same Network.
    158         for (Network network : getTestableNetworks()) {
    159             long networkHandle = network.getNetworkHandle();
    160             Network newNetwork = Network.fromNetworkHandle(networkHandle);
    161             assertEquals(newNetwork, network);
    162         }
    163 
    164         // Test that only obfuscated handles are allowed.
    165         try {
    166             Network.fromNetworkHandle(100);
    167             fail();
    168         } catch (IllegalArgumentException e) {}
    169         try {
    170             Network.fromNetworkHandle(-1);
    171             fail();
    172         } catch (IllegalArgumentException e) {}
    173         try {
    174             Network.fromNetworkHandle(0);
    175             fail();
    176         } catch (IllegalArgumentException e) {}
    177     }
    178 }
    179