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