Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2010 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 java.lang.reflect.Method;
     20 import java.net.InetAddress;
     21 
     22 import android.net.LinkAddress;
     23 import android.net.RouteInfo;
     24 import android.os.Parcel;
     25 
     26 import junit.framework.TestCase;
     27 import android.test.suitebuilder.annotation.SmallTest;
     28 
     29 public class RouteInfoTest extends TestCase {
     30 
     31     private InetAddress Address(String addr) {
     32         return InetAddress.parseNumericAddress(addr);
     33     }
     34 
     35     private LinkAddress Prefix(String prefix) {
     36         String[] parts = prefix.split("/");
     37         return new LinkAddress(Address(parts[0]), Integer.parseInt(parts[1]));
     38     }
     39 
     40     @SmallTest
     41     public void testConstructor() {
     42         RouteInfo r;
     43 
     44         // Invalid input.
     45         try {
     46             r = new RouteInfo(null, null, "rmnet0");
     47             fail("Expected RuntimeException:  destination and gateway null");
     48         } catch(RuntimeException e) {}
     49 
     50         // Null destination is default route.
     51         r = new RouteInfo(null, Address("2001:db8::1"), null);
     52         assertEquals(Prefix("::/0"), r.getDestination());
     53         assertEquals(Address("2001:db8::1"), r.getGateway());
     54         assertNull(r.getInterface());
     55 
     56         r = new RouteInfo(null, Address("192.0.2.1"), "wlan0");
     57         assertEquals(Prefix("0.0.0.0/0"), r.getDestination());
     58         assertEquals(Address("192.0.2.1"), r.getGateway());
     59         assertEquals("wlan0", r.getInterface());
     60 
     61         // Null gateway sets gateway to unspecified address (why?).
     62         r = new RouteInfo(Prefix("2001:db8:beef:cafe::/48"), null, "lo");
     63         assertEquals(Prefix("2001:db8:beef::/48"), r.getDestination());
     64         assertEquals(Address("::"), r.getGateway());
     65         assertEquals("lo", r.getInterface());
     66 
     67         r = new RouteInfo(Prefix("192.0.2.5/24"), null);
     68         assertEquals(Prefix("192.0.2.0/24"), r.getDestination());
     69         assertEquals(Address("0.0.0.0"), r.getGateway());
     70         assertNull(r.getInterface());
     71     }
     72 
     73     public void testMatches() {
     74         class PatchedRouteInfo extends RouteInfo {
     75             public PatchedRouteInfo(LinkAddress destination, InetAddress gateway, String iface) {
     76                 super(destination, gateway, iface);
     77             }
     78 
     79             public boolean matches(InetAddress destination) {
     80                 return super.matches(destination);
     81             }
     82         }
     83 
     84         RouteInfo r;
     85 
     86         r = new PatchedRouteInfo(Prefix("2001:db8:f00::ace:d00d/127"), null, "rmnet0");
     87         assertTrue(r.matches(Address("2001:db8:f00::ace:d00c")));
     88         assertTrue(r.matches(Address("2001:db8:f00::ace:d00d")));
     89         assertFalse(r.matches(Address("2001:db8:f00::ace:d00e")));
     90         assertFalse(r.matches(Address("2001:db8:f00::bad:d00d")));
     91         assertFalse(r.matches(Address("2001:4868:4860::8888")));
     92 
     93         r = new PatchedRouteInfo(Prefix("192.0.2.0/23"), null, "wlan0");
     94         assertTrue(r.matches(Address("192.0.2.43")));
     95         assertTrue(r.matches(Address("192.0.3.21")));
     96         assertFalse(r.matches(Address("192.0.0.21")));
     97         assertFalse(r.matches(Address("8.8.8.8")));
     98 
     99         RouteInfo ipv6Default = new PatchedRouteInfo(Prefix("::/0"), null, "rmnet0");
    100         assertTrue(ipv6Default.matches(Address("2001:db8::f00")));
    101         assertFalse(ipv6Default.matches(Address("192.0.2.1")));
    102 
    103         RouteInfo ipv4Default = new PatchedRouteInfo(Prefix("0.0.0.0/0"), null, "rmnet0");
    104         assertTrue(ipv4Default.matches(Address("255.255.255.255")));
    105         assertTrue(ipv4Default.matches(Address("192.0.2.1")));
    106         assertFalse(ipv4Default.matches(Address("2001:db8::f00")));
    107     }
    108 
    109     private void assertAreEqual(Object o1, Object o2) {
    110         assertTrue(o1.equals(o2));
    111         assertTrue(o2.equals(o1));
    112     }
    113 
    114     private void assertAreNotEqual(Object o1, Object o2) {
    115         assertFalse(o1.equals(o2));
    116         assertFalse(o2.equals(o1));
    117     }
    118 
    119     public void testEquals() {
    120         // IPv4
    121         RouteInfo r1 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "wlan0");
    122         RouteInfo r2 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "wlan0");
    123         assertAreEqual(r1, r2);
    124 
    125         RouteInfo r3 = new RouteInfo(Prefix("2001:db8:ace::/49"), Address("2001:db8::1"), "wlan0");
    126         RouteInfo r4 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::2"), "wlan0");
    127         RouteInfo r5 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "rmnet0");
    128         assertAreNotEqual(r1, r3);
    129         assertAreNotEqual(r1, r4);
    130         assertAreNotEqual(r1, r5);
    131 
    132         // IPv6
    133         r1 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "wlan0");
    134         r2 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "wlan0");
    135         assertAreEqual(r1, r2);
    136 
    137         r3 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0");
    138         r4 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.2"), "wlan0");
    139         r5 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "rmnet0");
    140         assertAreNotEqual(r1, r3);
    141         assertAreNotEqual(r1, r4);
    142         assertAreNotEqual(r1, r5);
    143 
    144         // Interfaces (but not destinations or gateways) can be null.
    145         r1 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), null);
    146         r2 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), null);
    147         r3 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0");
    148         assertAreEqual(r1, r2);
    149         assertAreNotEqual(r1, r3);
    150     }
    151 
    152     public void testHostRoute() {
    153       RouteInfo r;
    154 
    155       r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0");
    156       assertFalse(r.isHostRoute());
    157 
    158       r = new RouteInfo(Prefix("::/0"), Address("::"), "wlan0");
    159       assertFalse(r.isHostRoute());
    160 
    161       r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0");
    162       assertFalse(r.isHostRoute());
    163 
    164       r = new RouteInfo(Prefix("2001:db8::/48"), null, "wlan0");
    165       assertFalse(r.isHostRoute());
    166 
    167       r = new RouteInfo(Prefix("192.0.2.0/32"), Address("0.0.0.0"), "wlan0");
    168       assertTrue(r.isHostRoute());
    169 
    170       r = new RouteInfo(Prefix("2001:db8::/128"), Address("::"), "wlan0");
    171       assertTrue(r.isHostRoute());
    172 
    173       r = new RouteInfo(Prefix("192.0.2.0/32"), null, "wlan0");
    174       assertTrue(r.isHostRoute());
    175 
    176       r = new RouteInfo(Prefix("2001:db8::/128"), null, "wlan0");
    177       assertTrue(r.isHostRoute());
    178 
    179       r = new RouteInfo(Prefix("::/128"), Address("fe80::"), "wlan0");
    180       assertTrue(r.isHostRoute());
    181 
    182       r = new RouteInfo(Prefix("0.0.0.0/32"), Address("192.0.2.1"), "wlan0");
    183       assertTrue(r.isHostRoute());
    184     }
    185 
    186     public RouteInfo passThroughParcel(RouteInfo r) {
    187         Parcel p = Parcel.obtain();
    188         RouteInfo r2 = null;
    189         try {
    190             r.writeToParcel(p, 0);
    191             p.setDataPosition(0);
    192             r2 = RouteInfo.CREATOR.createFromParcel(p);
    193         } finally {
    194             p.recycle();
    195         }
    196         assertNotNull(r2);
    197         return r2;
    198     }
    199 
    200     public void assertParcelingIsLossless(RouteInfo r) {
    201       RouteInfo r2 = passThroughParcel(r);
    202       assertEquals(r, r2);
    203     }
    204 
    205     public void testParceling() {
    206         RouteInfo r;
    207 
    208         r = new RouteInfo(Prefix("::/0"), Address("2001:db8::"), null);
    209         assertParcelingIsLossless(r);
    210 
    211         r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0");
    212         assertParcelingIsLossless(r);
    213     }
    214 }
    215