LinkPropertiesTest.java revision 1994bc14ab9fd3a3c5dca8851cfc0042bcb25d46
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 android.net.LinkProperties; 20import android.net.RouteInfo; 21import android.test.suitebuilder.annotation.SmallTest; 22import junit.framework.TestCase; 23 24import java.net.InetAddress; 25 26public class LinkPropertiesTest extends TestCase { 27 private static String ADDRV4 = "75.208.6.1"; 28 private static String ADDRV6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; 29 private static String DNS1 = "75.208.7.1"; 30 private static String DNS2 = "69.78.7.1"; 31 private static String GATEWAY1 = "75.208.8.1"; 32 private static String GATEWAY2 = "69.78.8.1"; 33 private static String NAME = "qmi0"; 34 35 @SmallTest 36 public void testEqualsNull() { 37 LinkProperties source = new LinkProperties(); 38 LinkProperties target = new LinkProperties(); 39 40 assertFalse(source == target); 41 assertTrue(source.equals(target)); 42 assertTrue(source.hashCode() == target.hashCode()); 43 } 44 45 @SmallTest 46 public void testEqualsSameOrder() { 47 try { 48 LinkProperties source = new LinkProperties(); 49 source.setInterfaceName(NAME); 50 // set 2 link addresses 51 source.addLinkAddress(new LinkAddress( 52 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 53 source.addLinkAddress(new LinkAddress( 54 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 55 // set 2 dnses 56 source.addDns(NetworkUtils.numericToInetAddress(DNS1)); 57 source.addDns(NetworkUtils.numericToInetAddress(DNS2)); 58 // set 2 gateways 59 source.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); 60 source.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); 61 62 LinkProperties target = new LinkProperties(); 63 64 // All fields are same 65 target.setInterfaceName(NAME); 66 target.addLinkAddress(new LinkAddress( 67 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 68 target.addLinkAddress(new LinkAddress( 69 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 70 target.addDns(NetworkUtils.numericToInetAddress(DNS1)); 71 target.addDns(NetworkUtils.numericToInetAddress(DNS2)); 72 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); 73 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); 74 75 assertTrue(source.equals(target)); 76 assertTrue(source.hashCode() == target.hashCode()); 77 78 target.clear(); 79 // change Interface Name 80 target.setInterfaceName("qmi1"); 81 target.addLinkAddress(new LinkAddress( 82 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 83 target.addLinkAddress(new LinkAddress( 84 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 85 target.addDns(NetworkUtils.numericToInetAddress(DNS1)); 86 target.addDns(NetworkUtils.numericToInetAddress(DNS2)); 87 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); 88 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); 89 assertFalse(source.equals(target)); 90 91 target.clear(); 92 target.setInterfaceName(NAME); 93 // change link addresses 94 target.addLinkAddress(new LinkAddress( 95 NetworkUtils.numericToInetAddress("75.208.6.2"), 32)); 96 target.addLinkAddress(new LinkAddress( 97 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 98 target.addDns(NetworkUtils.numericToInetAddress(DNS1)); 99 target.addDns(NetworkUtils.numericToInetAddress(DNS2)); 100 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); 101 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); 102 assertFalse(source.equals(target)); 103 104 target.clear(); 105 target.setInterfaceName(NAME); 106 target.addLinkAddress(new LinkAddress( 107 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 108 target.addLinkAddress(new LinkAddress( 109 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 110 // change dnses 111 target.addDns(NetworkUtils.numericToInetAddress("75.208.7.2")); 112 target.addDns(NetworkUtils.numericToInetAddress(DNS2)); 113 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); 114 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); 115 assertFalse(source.equals(target)); 116 117 target.clear(); 118 target.setInterfaceName(NAME); 119 target.addLinkAddress(new LinkAddress( 120 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 121 target.addLinkAddress(new LinkAddress( 122 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 123 target.addDns(NetworkUtils.numericToInetAddress(DNS1)); 124 target.addDns(NetworkUtils.numericToInetAddress(DNS2)); 125 // change gateway 126 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress("75.208.8.2"))); 127 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); 128 assertFalse(source.equals(target)); 129 130 } catch (Exception e) { 131 throw new RuntimeException(e.toString()); 132 //fail(); 133 } 134 } 135 136 @SmallTest 137 public void testEqualsDifferentOrder() { 138 try { 139 LinkProperties source = new LinkProperties(); 140 source.setInterfaceName(NAME); 141 // set 2 link addresses 142 source.addLinkAddress(new LinkAddress( 143 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 144 source.addLinkAddress(new LinkAddress( 145 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 146 // set 2 dnses 147 source.addDns(NetworkUtils.numericToInetAddress(DNS1)); 148 source.addDns(NetworkUtils.numericToInetAddress(DNS2)); 149 // set 2 gateways 150 source.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); 151 source.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); 152 153 LinkProperties target = new LinkProperties(); 154 // Exchange order 155 target.setInterfaceName(NAME); 156 target.addLinkAddress(new LinkAddress( 157 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 158 target.addLinkAddress(new LinkAddress( 159 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 160 target.addDns(NetworkUtils.numericToInetAddress(DNS2)); 161 target.addDns(NetworkUtils.numericToInetAddress(DNS1)); 162 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2))); 163 target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1))); 164 165 assertTrue(source.equals(target)); 166 assertTrue(source.hashCode() == target.hashCode()); 167 } catch (Exception e) { 168 fail(); 169 } 170 } 171 172 @SmallTest 173 public void testEqualsDuplicated() { 174 try { 175 LinkProperties source = new LinkProperties(); 176 // set 3 link addresses, eg, [A, A, B] 177 source.addLinkAddress(new LinkAddress( 178 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 179 source.addLinkAddress(new LinkAddress( 180 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 181 source.addLinkAddress(new LinkAddress( 182 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 183 184 LinkProperties target = new LinkProperties(); 185 // set 3 link addresses, eg, [A, B, B] 186 target.addLinkAddress(new LinkAddress( 187 NetworkUtils.numericToInetAddress(ADDRV4), 32)); 188 target.addLinkAddress(new LinkAddress( 189 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 190 target.addLinkAddress(new LinkAddress( 191 NetworkUtils.numericToInetAddress(ADDRV6), 128)); 192 193 assertTrue(source.equals(target)); 194 assertTrue(source.hashCode() == target.hashCode()); 195 } catch (Exception e) { 196 fail(); 197 } 198 } 199 200 private void assertAllRoutesHaveInterface(String iface, LinkProperties lp) { 201 for (RouteInfo r : lp.getRoutes()) { 202 assertEquals(iface, r.getInterface()); 203 } 204 } 205 206 @SmallTest 207 public void testRouteInterfaces() { 208 LinkAddress prefix = new LinkAddress( 209 NetworkUtils.numericToInetAddress("2001:db8::"), 32); 210 InetAddress address = NetworkUtils.numericToInetAddress(ADDRV6); 211 212 // Add a route with no interface to a LinkProperties with no interface. No errors. 213 LinkProperties lp = new LinkProperties(); 214 RouteInfo r = new RouteInfo(prefix, address, null); 215 lp.addRoute(r); 216 assertEquals(1, lp.getRoutes().size()); 217 assertAllRoutesHaveInterface(null, lp); 218 219 // Add a route with an interface. Except an exception. 220 r = new RouteInfo(prefix, address, "wlan0"); 221 try { 222 lp.addRoute(r); 223 fail("Adding wlan0 route to LP with no interface, expect exception"); 224 } catch (IllegalArgumentException expected) {} 225 226 // Change the interface name. All the routes should change their interface name too. 227 lp.setInterfaceName("rmnet0"); 228 assertAllRoutesHaveInterface("rmnet0", lp); 229 230 // Now add a route with the wrong interface. This causes an exception too. 231 try { 232 lp.addRoute(r); 233 fail("Adding wlan0 route to rmnet0 LP, expect exception"); 234 } catch (IllegalArgumentException expected) {} 235 236 // If the interface name matches, the route is added. 237 lp.setInterfaceName("wlan0"); 238 lp.addRoute(r); 239 assertEquals(2, lp.getRoutes().size()); 240 assertAllRoutesHaveInterface("wlan0", lp); 241 242 // Routes with null interfaces are converted to wlan0. 243 r = RouteInfo.makeHostRoute(NetworkUtils.numericToInetAddress(ADDRV6), null); 244 lp.addRoute(r); 245 assertEquals(3, lp.getRoutes().size()); 246 assertAllRoutesHaveInterface("wlan0", lp); 247 248 // Check comparisons work. 249 LinkProperties lp2 = new LinkProperties(lp); 250 assertAllRoutesHaveInterface("wlan0", lp); 251 assertEquals(0, lp.compareRoutes(lp2).added.size()); 252 assertEquals(0, lp.compareRoutes(lp2).removed.size()); 253 254 lp2.setInterfaceName("p2p0"); 255 assertAllRoutesHaveInterface("p2p0", lp2); 256 assertEquals(3, lp.compareRoutes(lp2).added.size()); 257 assertEquals(3, lp.compareRoutes(lp2).removed.size()); 258 } 259} 260