Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2014 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;
     18 
     19 import android.net.IpPrefix;
     20 import android.net.LinkAddress;
     21 import android.net.RouteInfo;
     22 import android.net.StaticIpConfiguration;
     23 import android.os.Parcel;
     24 
     25 import java.net.InetAddress;
     26 import java.util.HashSet;
     27 
     28 import junit.framework.TestCase;
     29 import android.test.suitebuilder.annotation.SmallTest;
     30 
     31 import static org.junit.Assert.*;
     32 
     33 
     34 public class StaticIpConfigurationTest extends TestCase {
     35 
     36     private static final String ADDRSTR = "192.0.2.2/25";
     37     private static final LinkAddress ADDR = new LinkAddress(ADDRSTR);
     38     private static final InetAddress GATEWAY = IpAddress("192.0.2.1");
     39     private static final InetAddress OFFLINKGATEWAY = IpAddress("192.0.2.129");
     40     private static final InetAddress DNS1 = IpAddress("8.8.8.8");
     41     private static final InetAddress DNS2 = IpAddress("8.8.4.4");
     42     private static final InetAddress DNS3 = IpAddress("4.2.2.2");
     43     private static final String IFACE = "eth0";
     44 
     45     private static InetAddress IpAddress(String addr) {
     46         return InetAddress.parseNumericAddress(addr);
     47     }
     48 
     49     private void checkEmpty(StaticIpConfiguration s) {
     50         assertNull(s.ipAddress);
     51         assertNull(s.gateway);
     52         assertNull(s.domains);
     53         assertEquals(0, s.dnsServers.size());
     54     }
     55 
     56     private boolean isEqual(StaticIpConfiguration s1, StaticIpConfiguration s2) {
     57         return s1.equals(s2);
     58     }
     59 
     60     private void assertEquals(StaticIpConfiguration s1, StaticIpConfiguration s2) {
     61         assertTrue(isEqual(s1, s2));
     62     }
     63 
     64     private void assertNotEquals(StaticIpConfiguration s1, StaticIpConfiguration s2) {
     65         assertFalse(isEqual(s1, s2));
     66     }
     67 
     68     private StaticIpConfiguration makeTestObject() {
     69         StaticIpConfiguration s = new StaticIpConfiguration();
     70         s.ipAddress = ADDR;
     71         s.gateway = GATEWAY;
     72         s.dnsServers.add(DNS1);
     73         s.dnsServers.add(DNS2);
     74         s.dnsServers.add(DNS3);
     75         s.domains = "google.com";
     76         return s;
     77     }
     78 
     79     @SmallTest
     80     public void testConstructor() {
     81         StaticIpConfiguration s = new StaticIpConfiguration();
     82         checkEmpty(s);
     83     }
     84 
     85     @SmallTest
     86     public void testCopyAndClear() {
     87         StaticIpConfiguration empty = new StaticIpConfiguration((StaticIpConfiguration) null);
     88         checkEmpty(empty);
     89 
     90         StaticIpConfiguration s1 = makeTestObject();
     91         StaticIpConfiguration s2 = new StaticIpConfiguration(s1);
     92         assertEquals(s1, s2);
     93         s2.clear();
     94         assertEquals(empty, s2);
     95     }
     96 
     97     @SmallTest
     98     public void testHashCodeAndEquals() {
     99         HashSet<Integer> hashCodes = new HashSet();
    100         hashCodes.add(0);
    101 
    102         StaticIpConfiguration s = new StaticIpConfiguration();
    103         // Check that this hash code is nonzero and different from all the ones seen so far.
    104         assertTrue(hashCodes.add(s.hashCode()));
    105 
    106         s.ipAddress = ADDR;
    107         assertTrue(hashCodes.add(s.hashCode()));
    108 
    109         s.gateway = GATEWAY;
    110         assertTrue(hashCodes.add(s.hashCode()));
    111 
    112         s.dnsServers.add(DNS1);
    113         assertTrue(hashCodes.add(s.hashCode()));
    114 
    115         s.dnsServers.add(DNS2);
    116         assertTrue(hashCodes.add(s.hashCode()));
    117 
    118         s.dnsServers.add(DNS3);
    119         assertTrue(hashCodes.add(s.hashCode()));
    120 
    121         s.domains = "example.com";
    122         assertTrue(hashCodes.add(s.hashCode()));
    123 
    124         assertFalse(s.equals(null));
    125         assertEquals(s, s);
    126 
    127         StaticIpConfiguration s2 = new StaticIpConfiguration(s);
    128         assertEquals(s, s2);
    129 
    130         s.ipAddress = new LinkAddress(DNS1, 32);
    131         assertNotEquals(s, s2);
    132 
    133         s2 = new StaticIpConfiguration(s);
    134         s.domains = "foo";
    135         assertNotEquals(s, s2);
    136 
    137         s2 = new StaticIpConfiguration(s);
    138         s.gateway = DNS2;
    139         assertNotEquals(s, s2);
    140 
    141         s2 = new StaticIpConfiguration(s);
    142         s.dnsServers.add(DNS3);
    143         assertNotEquals(s, s2);
    144     }
    145 
    146     @SmallTest
    147     public void testToLinkProperties() {
    148         LinkProperties expected = new LinkProperties();
    149         expected.setInterfaceName(IFACE);
    150 
    151         StaticIpConfiguration s = new StaticIpConfiguration();
    152         assertEquals(expected, s.toLinkProperties(IFACE));
    153 
    154         final RouteInfo connectedRoute = new RouteInfo(new IpPrefix(ADDRSTR), null, IFACE);
    155         s.ipAddress = ADDR;
    156         expected.addLinkAddress(ADDR);
    157         expected.addRoute(connectedRoute);
    158         assertEquals(expected, s.toLinkProperties(IFACE));
    159 
    160         s.gateway = GATEWAY;
    161         RouteInfo defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), GATEWAY, IFACE);
    162         expected.addRoute(defaultRoute);
    163         assertEquals(expected, s.toLinkProperties(IFACE));
    164 
    165         s.gateway = OFFLINKGATEWAY;
    166         expected.removeRoute(defaultRoute);
    167         defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), OFFLINKGATEWAY, IFACE);
    168         expected.addRoute(defaultRoute);
    169 
    170         RouteInfo gatewayRoute = new RouteInfo(new IpPrefix("192.0.2.129/32"), null, IFACE);
    171         expected.addRoute(gatewayRoute);
    172         assertEquals(expected, s.toLinkProperties(IFACE));
    173 
    174         s.dnsServers.add(DNS1);
    175         expected.addDnsServer(DNS1);
    176         assertEquals(expected, s.toLinkProperties(IFACE));
    177 
    178         s.dnsServers.add(DNS2);
    179         s.dnsServers.add(DNS3);
    180         expected.addDnsServer(DNS2);
    181         expected.addDnsServer(DNS3);
    182         assertEquals(expected, s.toLinkProperties(IFACE));
    183 
    184         s.domains = "google.com";
    185         expected.setDomains("google.com");
    186         assertEquals(expected, s.toLinkProperties(IFACE));
    187 
    188         s.gateway = null;
    189         expected.removeRoute(defaultRoute);
    190         expected.removeRoute(gatewayRoute);
    191         assertEquals(expected, s.toLinkProperties(IFACE));
    192 
    193         // Without knowing the IP address, we don't have a directly-connected route, so we can't
    194         // tell if the gateway is off-link or not and we don't add a host route. This isn't a real
    195         // configuration, but we should at least not crash.
    196         s.gateway = OFFLINKGATEWAY;
    197         s.ipAddress = null;
    198         expected.removeLinkAddress(ADDR);
    199         expected.removeRoute(connectedRoute);
    200         expected.addRoute(defaultRoute);
    201         assertEquals(expected, s.toLinkProperties(IFACE));
    202     }
    203 
    204     private StaticIpConfiguration passThroughParcel(StaticIpConfiguration s) {
    205         Parcel p = Parcel.obtain();
    206         StaticIpConfiguration s2 = null;
    207         try {
    208             s.writeToParcel(p, 0);
    209             p.setDataPosition(0);
    210             s2 = StaticIpConfiguration.CREATOR.createFromParcel(p);
    211         } finally {
    212             p.recycle();
    213         }
    214         assertNotNull(s2);
    215         return s2;
    216     }
    217 
    218     @SmallTest
    219     public void testParceling() {
    220         StaticIpConfiguration s = makeTestObject();
    221         StaticIpConfiguration s2 = passThroughParcel(s);
    222         assertEquals(s, s2);
    223     }
    224 }
    225 
    226