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 17package android.net; 18 19import java.lang.reflect.Method; 20import java.net.InetAddress; 21 22import android.net.IpPrefix; 23import android.net.RouteInfo; 24import android.os.Parcel; 25 26import junit.framework.TestCase; 27import android.test.suitebuilder.annotation.SmallTest; 28 29public class RouteInfoTest extends TestCase { 30 31 private InetAddress Address(String addr) { 32 return InetAddress.parseNumericAddress(addr); 33 } 34 35 private IpPrefix Prefix(String prefix) { 36 return new IpPrefix(prefix); 37 } 38 39 @SmallTest 40 public void testConstructor() { 41 RouteInfo r; 42 43 // Invalid input. 44 try { 45 r = new RouteInfo((IpPrefix) null, null, "rmnet0"); 46 fail("Expected RuntimeException: destination and gateway null"); 47 } catch(RuntimeException e) {} 48 49 // Null destination is default route. 50 r = new RouteInfo((IpPrefix) null, Address("2001:db8::1"), null); 51 assertEquals(Prefix("::/0"), r.getDestination()); 52 assertEquals(Address("2001:db8::1"), r.getGateway()); 53 assertNull(r.getInterface()); 54 55 r = new RouteInfo((IpPrefix) null, Address("192.0.2.1"), "wlan0"); 56 assertEquals(Prefix("0.0.0.0/0"), r.getDestination()); 57 assertEquals(Address("192.0.2.1"), r.getGateway()); 58 assertEquals("wlan0", r.getInterface()); 59 60 // Null gateway sets gateway to unspecified address (why?). 61 r = new RouteInfo(Prefix("2001:db8:beef:cafe::/48"), null, "lo"); 62 assertEquals(Prefix("2001:db8:beef::/48"), r.getDestination()); 63 assertEquals(Address("::"), r.getGateway()); 64 assertEquals("lo", r.getInterface()); 65 66 r = new RouteInfo(Prefix("192.0.2.5/24"), null); 67 assertEquals(Prefix("192.0.2.0/24"), r.getDestination()); 68 assertEquals(Address("0.0.0.0"), r.getGateway()); 69 assertNull(r.getInterface()); 70 } 71 72 public void testMatches() { 73 class PatchedRouteInfo { 74 private final RouteInfo mRouteInfo; 75 76 public PatchedRouteInfo(IpPrefix destination, InetAddress gateway, String iface) { 77 mRouteInfo = new RouteInfo(destination, gateway, iface); 78 } 79 80 public boolean matches(InetAddress destination) { 81 return mRouteInfo.matches(destination); 82 } 83 } 84 85 PatchedRouteInfo r; 86 87 r = new PatchedRouteInfo(Prefix("2001:db8:f00::ace:d00d/127"), null, "rmnet0"); 88 assertTrue(r.matches(Address("2001:db8:f00::ace:d00c"))); 89 assertTrue(r.matches(Address("2001:db8:f00::ace:d00d"))); 90 assertFalse(r.matches(Address("2001:db8:f00::ace:d00e"))); 91 assertFalse(r.matches(Address("2001:db8:f00::bad:d00d"))); 92 assertFalse(r.matches(Address("2001:4868:4860::8888"))); 93 assertFalse(r.matches(Address("8.8.8.8"))); 94 95 r = new PatchedRouteInfo(Prefix("192.0.2.0/23"), null, "wlan0"); 96 assertTrue(r.matches(Address("192.0.2.43"))); 97 assertTrue(r.matches(Address("192.0.3.21"))); 98 assertFalse(r.matches(Address("192.0.0.21"))); 99 assertFalse(r.matches(Address("8.8.8.8"))); 100 101 PatchedRouteInfo ipv6Default = new PatchedRouteInfo(Prefix("::/0"), null, "rmnet0"); 102 assertTrue(ipv6Default.matches(Address("2001:db8::f00"))); 103 assertFalse(ipv6Default.matches(Address("192.0.2.1"))); 104 105 PatchedRouteInfo ipv4Default = new PatchedRouteInfo(Prefix("0.0.0.0/0"), null, "rmnet0"); 106 assertTrue(ipv4Default.matches(Address("255.255.255.255"))); 107 assertTrue(ipv4Default.matches(Address("192.0.2.1"))); 108 assertFalse(ipv4Default.matches(Address("2001:db8::f00"))); 109 } 110 111 private void assertAreEqual(Object o1, Object o2) { 112 assertTrue(o1.equals(o2)); 113 assertTrue(o2.equals(o1)); 114 } 115 116 private void assertAreNotEqual(Object o1, Object o2) { 117 assertFalse(o1.equals(o2)); 118 assertFalse(o2.equals(o1)); 119 } 120 121 public void testEquals() { 122 // IPv4 123 RouteInfo r1 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "wlan0"); 124 RouteInfo r2 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "wlan0"); 125 assertAreEqual(r1, r2); 126 127 RouteInfo r3 = new RouteInfo(Prefix("2001:db8:ace::/49"), Address("2001:db8::1"), "wlan0"); 128 RouteInfo r4 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::2"), "wlan0"); 129 RouteInfo r5 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "rmnet0"); 130 assertAreNotEqual(r1, r3); 131 assertAreNotEqual(r1, r4); 132 assertAreNotEqual(r1, r5); 133 134 // IPv6 135 r1 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "wlan0"); 136 r2 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "wlan0"); 137 assertAreEqual(r1, r2); 138 139 r3 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0"); 140 r4 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.2"), "wlan0"); 141 r5 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "rmnet0"); 142 assertAreNotEqual(r1, r3); 143 assertAreNotEqual(r1, r4); 144 assertAreNotEqual(r1, r5); 145 146 // Interfaces (but not destinations or gateways) can be null. 147 r1 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), null); 148 r2 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), null); 149 r3 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0"); 150 assertAreEqual(r1, r2); 151 assertAreNotEqual(r1, r3); 152 } 153 154 public void testHostAndDefaultRoutes() { 155 RouteInfo r; 156 157 r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0"); 158 assertFalse(r.isHostRoute()); 159 assertTrue(r.isDefaultRoute()); 160 assertTrue(r.isIPv4Default()); 161 assertFalse(r.isIPv6Default()); 162 163 r = new RouteInfo(Prefix("::/0"), Address("::"), "wlan0"); 164 assertFalse(r.isHostRoute()); 165 assertTrue(r.isDefaultRoute()); 166 assertFalse(r.isIPv4Default()); 167 assertTrue(r.isIPv6Default()); 168 169 r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0"); 170 assertFalse(r.isHostRoute()); 171 assertFalse(r.isDefaultRoute()); 172 assertFalse(r.isIPv4Default()); 173 assertFalse(r.isIPv6Default()); 174 175 r = new RouteInfo(Prefix("2001:db8::/48"), null, "wlan0"); 176 assertFalse(r.isHostRoute()); 177 assertFalse(r.isDefaultRoute()); 178 assertFalse(r.isIPv4Default()); 179 assertFalse(r.isIPv6Default()); 180 181 r = new RouteInfo(Prefix("192.0.2.0/32"), Address("0.0.0.0"), "wlan0"); 182 assertTrue(r.isHostRoute()); 183 assertFalse(r.isDefaultRoute()); 184 assertFalse(r.isIPv4Default()); 185 assertFalse(r.isIPv6Default()); 186 187 r = new RouteInfo(Prefix("2001:db8::/128"), Address("::"), "wlan0"); 188 assertTrue(r.isHostRoute()); 189 assertFalse(r.isDefaultRoute()); 190 assertFalse(r.isIPv4Default()); 191 assertFalse(r.isIPv6Default()); 192 193 r = new RouteInfo(Prefix("192.0.2.0/32"), null, "wlan0"); 194 assertTrue(r.isHostRoute()); 195 assertFalse(r.isDefaultRoute()); 196 assertFalse(r.isIPv4Default()); 197 assertFalse(r.isIPv6Default()); 198 199 r = new RouteInfo(Prefix("2001:db8::/128"), null, "wlan0"); 200 assertTrue(r.isHostRoute()); 201 assertFalse(r.isDefaultRoute()); 202 assertFalse(r.isIPv4Default()); 203 assertFalse(r.isIPv6Default()); 204 205 r = new RouteInfo(Prefix("::/128"), Address("fe80::"), "wlan0"); 206 assertTrue(r.isHostRoute()); 207 assertFalse(r.isDefaultRoute()); 208 assertFalse(r.isIPv4Default()); 209 assertFalse(r.isIPv6Default()); 210 211 r = new RouteInfo(Prefix("0.0.0.0/32"), Address("192.0.2.1"), "wlan0"); 212 assertTrue(r.isHostRoute()); 213 assertFalse(r.isDefaultRoute()); 214 assertFalse(r.isIPv4Default()); 215 assertFalse(r.isIPv6Default()); 216 } 217 218 public void testTruncation() { 219 LinkAddress l; 220 RouteInfo r; 221 222 l = new LinkAddress("192.0.2.5/30"); 223 r = new RouteInfo(l, Address("192.0.2.1"), "wlan0"); 224 assertEquals("192.0.2.4", r.getDestination().getAddress().getHostAddress()); 225 226 l = new LinkAddress("2001:db8:1:f::5/63"); 227 r = new RouteInfo(l, Address("2001:db8:5::1"), "wlan0"); 228 assertEquals("2001:db8:1:e::", r.getDestination().getAddress().getHostAddress()); 229 } 230 231 // Make sure that creating routes to multicast addresses doesn't throw an exception. Even though 232 // there's nothing we can do with them, we don't want to crash if, e.g., someone calls 233 // requestRouteToHostAddress("230.0.0.0", MOBILE_HIPRI); 234 public void testMulticastRoute() { 235 RouteInfo r; 236 r = new RouteInfo(Prefix("230.0.0.0/32"), Address("192.0.2.1"), "wlan0"); 237 r = new RouteInfo(Prefix("ff02::1/128"), Address("2001:db8::1"), "wlan0"); 238 // No exceptions? Good. 239 } 240 241 public RouteInfo passThroughParcel(RouteInfo r) { 242 Parcel p = Parcel.obtain(); 243 RouteInfo r2 = null; 244 try { 245 r.writeToParcel(p, 0); 246 p.setDataPosition(0); 247 r2 = RouteInfo.CREATOR.createFromParcel(p); 248 } finally { 249 p.recycle(); 250 } 251 assertNotNull(r2); 252 return r2; 253 } 254 255 public void assertParcelingIsLossless(RouteInfo r) { 256 RouteInfo r2 = passThroughParcel(r); 257 assertEquals(r, r2); 258 } 259 260 public void testParceling() { 261 RouteInfo r; 262 263 r = new RouteInfo(Prefix("::/0"), Address("2001:db8::"), null); 264 assertParcelingIsLossless(r); 265 266 r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0"); 267 assertParcelingIsLossless(r); 268 } 269} 270