Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.dhcp; |
| 18 | |
| 19 | import android.net.NetworkUtils; |
Lorenzo Colitti | d973537 | 2015-06-02 13:15:50 +0900 | [diff] [blame] | 20 | import android.net.DhcpResults; |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 21 | import android.net.LinkAddress; |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 22 | import android.system.OsConstants; |
| 23 | import android.test.suitebuilder.annotation.SmallTest; |
| 24 | import junit.framework.TestCase; |
| 25 | |
| 26 | import java.net.Inet4Address; |
| 27 | import java.nio.ByteBuffer; |
Lorenzo Colitti | b0b3d0b | 2015-07-06 12:29:43 +0900 | [diff] [blame^] | 28 | import java.util.ArrayList; |
| 29 | |
| 30 | import libcore.util.HexEncoding; |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 31 | |
| 32 | import static android.net.dhcp.DhcpPacket.*; |
| 33 | |
| 34 | |
| 35 | public class DhcpPacketTest extends TestCase { |
| 36 | |
Lorenzo Colitti | b0b3d0b | 2015-07-06 12:29:43 +0900 | [diff] [blame^] | 37 | private static Inet4Address SERVER_ADDR = v4Address("192.0.2.1"); |
| 38 | private static Inet4Address CLIENT_ADDR = v4Address("192.0.2.234"); |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 39 | // Use our own empty address instead of Inet4Address.ANY or INADDR_ANY to ensure that the code |
| 40 | // doesn't use == instead of equals when comparing addresses. |
Lorenzo Colitti | b0b3d0b | 2015-07-06 12:29:43 +0900 | [diff] [blame^] | 41 | private static Inet4Address ANY = (Inet4Address) v4Address("0.0.0.0"); |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 42 | |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 43 | private static byte[] CLIENT_MAC = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; |
| 44 | |
Lorenzo Colitti | b0b3d0b | 2015-07-06 12:29:43 +0900 | [diff] [blame^] | 45 | private static final Inet4Address v4Address(String addrString) throws IllegalArgumentException { |
| 46 | return (Inet4Address) NetworkUtils.numericToInetAddress(addrString); |
| 47 | } |
| 48 | |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 49 | class TestDhcpPacket extends DhcpPacket { |
| 50 | private byte mType; |
| 51 | // TODO: Make this a map of option numbers to bytes instead. |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 52 | private byte[] mDomainBytes, mVendorInfoBytes, mLeaseTimeBytes, mNetmaskBytes; |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 53 | |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 54 | public TestDhcpPacket(byte type, Inet4Address clientIp, Inet4Address yourIp) { |
| 55 | super(0xdeadbeef, (short) 0, clientIp, yourIp, INADDR_ANY, INADDR_ANY, |
Lorenzo Colitti | 3e97932 | 2015-04-21 15:22:17 +0900 | [diff] [blame] | 56 | CLIENT_MAC, true); |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 57 | mType = type; |
Lorenzo Colitti | d973537 | 2015-06-02 13:15:50 +0900 | [diff] [blame] | 58 | } |
| 59 | |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 60 | public TestDhcpPacket(byte type) { |
| 61 | this(type, INADDR_ANY, CLIENT_ADDR); |
| 62 | } |
| 63 | |
Lorenzo Colitti | d973537 | 2015-06-02 13:15:50 +0900 | [diff] [blame] | 64 | public TestDhcpPacket setDomainBytes(byte[] domainBytes) { |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 65 | mDomainBytes = domainBytes; |
Lorenzo Colitti | d973537 | 2015-06-02 13:15:50 +0900 | [diff] [blame] | 66 | return this; |
| 67 | } |
| 68 | |
| 69 | public TestDhcpPacket setVendorInfoBytes(byte[] vendorInfoBytes) { |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 70 | mVendorInfoBytes = vendorInfoBytes; |
Lorenzo Colitti | d973537 | 2015-06-02 13:15:50 +0900 | [diff] [blame] | 71 | return this; |
| 72 | } |
| 73 | |
| 74 | public TestDhcpPacket setLeaseTimeBytes(byte[] leaseTimeBytes) { |
| 75 | mLeaseTimeBytes = leaseTimeBytes; |
| 76 | return this; |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 77 | } |
| 78 | |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 79 | public TestDhcpPacket setNetmaskBytes(byte[] netmaskBytes) { |
| 80 | mNetmaskBytes = netmaskBytes; |
| 81 | return this; |
| 82 | } |
| 83 | |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 84 | public ByteBuffer buildPacket(int encap, short unusedDestUdp, short unusedSrcUdp) { |
| 85 | ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH); |
| 86 | fillInPacket(encap, CLIENT_ADDR, SERVER_ADDR, |
| 87 | DHCP_CLIENT, DHCP_SERVER, result, DHCP_BOOTREPLY, false); |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | public void finishPacket(ByteBuffer buffer) { |
| 92 | addTlv(buffer, DHCP_MESSAGE_TYPE, mType); |
| 93 | if (mDomainBytes != null) { |
| 94 | addTlv(buffer, DHCP_DOMAIN_NAME, mDomainBytes); |
| 95 | } |
| 96 | if (mVendorInfoBytes != null) { |
Lorenzo Colitti | b0b3d0b | 2015-07-06 12:29:43 +0900 | [diff] [blame^] | 97 | addTlv(buffer, DHCP_VENDOR_INFO, mVendorInfoBytes); |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 98 | } |
Lorenzo Colitti | d973537 | 2015-06-02 13:15:50 +0900 | [diff] [blame] | 99 | if (mLeaseTimeBytes != null) { |
| 100 | addTlv(buffer, DHCP_LEASE_TIME, mLeaseTimeBytes); |
| 101 | } |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 102 | if (mNetmaskBytes != null) { |
| 103 | addTlv(buffer, DHCP_SUBNET_MASK, mNetmaskBytes); |
| 104 | } |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 105 | addTlvEnd(buffer); |
| 106 | } |
| 107 | |
| 108 | // Convenience method. |
| 109 | public ByteBuffer build() { |
| 110 | // ENCAP_BOOTP packets don't contain ports, so just pass in 0. |
| 111 | ByteBuffer pkt = buildPacket(ENCAP_BOOTP, (short) 0, (short) 0); |
| 112 | pkt.flip(); |
| 113 | return pkt; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private void assertDomainAndVendorInfoParses( |
| 118 | String expectedDomain, byte[] domainBytes, |
| 119 | String expectedVendorInfo, byte[] vendorInfoBytes) { |
Lorenzo Colitti | d973537 | 2015-06-02 13:15:50 +0900 | [diff] [blame] | 120 | ByteBuffer packet = new TestDhcpPacket(DHCP_MESSAGE_TYPE_OFFER) |
| 121 | .setDomainBytes(domainBytes) |
| 122 | .setVendorInfoBytes(vendorInfoBytes) |
| 123 | .build(); |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 124 | DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP); |
| 125 | assertEquals(expectedDomain, offerPacket.mDomainName); |
Lorenzo Colitti | b0b3d0b | 2015-07-06 12:29:43 +0900 | [diff] [blame^] | 126 | assertEquals(expectedVendorInfo, offerPacket.mVendorInfo); |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @SmallTest |
| 130 | public void testDomainName() throws Exception { |
| 131 | byte[] nullByte = new byte[] { 0x00 }; |
| 132 | byte[] twoNullBytes = new byte[] { 0x00, 0x00 }; |
| 133 | byte[] nonNullDomain = new byte[] { |
| 134 | (byte) 'g', (byte) 'o', (byte) 'o', (byte) '.', (byte) 'g', (byte) 'l' |
| 135 | }; |
| 136 | byte[] trailingNullDomain = new byte[] { |
| 137 | (byte) 'g', (byte) 'o', (byte) 'o', (byte) '.', (byte) 'g', (byte) 'l', 0x00 |
| 138 | }; |
| 139 | byte[] embeddedNullsDomain = new byte[] { |
| 140 | (byte) 'g', (byte) 'o', (byte) 'o', 0x00, 0x00, (byte) 'g', (byte) 'l' |
| 141 | }; |
| 142 | byte[] metered = "ANDROID_METERED".getBytes("US-ASCII"); |
| 143 | |
| 144 | byte[] meteredEmbeddedNull = metered.clone(); |
| 145 | meteredEmbeddedNull[7] = (char) 0; |
| 146 | |
| 147 | byte[] meteredTrailingNull = metered.clone(); |
| 148 | meteredTrailingNull[meteredTrailingNull.length - 1] = (char) 0; |
| 149 | |
| 150 | assertDomainAndVendorInfoParses("", nullByte, "\u0000", nullByte); |
| 151 | assertDomainAndVendorInfoParses("", twoNullBytes, "\u0000\u0000", twoNullBytes); |
| 152 | assertDomainAndVendorInfoParses("goo.gl", nonNullDomain, "ANDROID_METERED", metered); |
| 153 | assertDomainAndVendorInfoParses("goo", embeddedNullsDomain, |
| 154 | "ANDROID\u0000METERED", meteredEmbeddedNull); |
| 155 | assertDomainAndVendorInfoParses("goo.gl", trailingNullDomain, |
| 156 | "ANDROID_METERE\u0000", meteredTrailingNull); |
| 157 | } |
Lorenzo Colitti | d973537 | 2015-06-02 13:15:50 +0900 | [diff] [blame] | 158 | |
| 159 | private void assertLeaseTimeParses(boolean expectValid, Integer rawLeaseTime, |
| 160 | long leaseTimeMillis, byte[] leaseTimeBytes) { |
| 161 | TestDhcpPacket testPacket = new TestDhcpPacket(DHCP_MESSAGE_TYPE_OFFER); |
| 162 | if (leaseTimeBytes != null) { |
| 163 | testPacket.setLeaseTimeBytes(leaseTimeBytes); |
| 164 | } |
| 165 | ByteBuffer packet = testPacket.build(); |
| 166 | DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP); |
| 167 | if (!expectValid) { |
| 168 | assertNull(offerPacket); |
| 169 | return; |
| 170 | } |
| 171 | assertEquals(rawLeaseTime, offerPacket.mLeaseTime); |
| 172 | DhcpResults dhcpResults = offerPacket.toDhcpResults(); // Just check this doesn't crash. |
| 173 | assertEquals(leaseTimeMillis, offerPacket.getLeaseTimeMillis()); |
| 174 | } |
| 175 | |
| 176 | @SmallTest |
| 177 | public void testLeaseTime() throws Exception { |
| 178 | byte[] noLease = null; |
| 179 | byte[] tooShortLease = new byte[] { 0x00, 0x00 }; |
| 180 | byte[] tooLongLease = new byte[] { 0x00, 0x00, 0x00, 60, 0x01 }; |
| 181 | byte[] zeroLease = new byte[] { 0x00, 0x00, 0x00, 0x00 }; |
| 182 | byte[] tenSecondLease = new byte[] { 0x00, 0x00, 0x00, 10 }; |
| 183 | byte[] oneMinuteLease = new byte[] { 0x00, 0x00, 0x00, 60 }; |
| 184 | byte[] fiveMinuteLease = new byte[] { 0x00, 0x00, 0x01, 0x2c }; |
| 185 | byte[] oneDayLease = new byte[] { 0x00, 0x01, 0x51, (byte) 0x80 }; |
| 186 | byte[] maxIntPlusOneLease = new byte[] { (byte) 0x80, 0x00, 0x00, 0x01 }; |
| 187 | byte[] infiniteLease = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }; |
| 188 | |
| 189 | assertLeaseTimeParses(true, null, 0, noLease); |
| 190 | assertLeaseTimeParses(false, null, 0, tooShortLease); |
| 191 | assertLeaseTimeParses(false, null, 0, tooLongLease); |
| 192 | assertLeaseTimeParses(true, 0, 60 * 1000, zeroLease); |
| 193 | assertLeaseTimeParses(true, 10, 60 * 1000, tenSecondLease); |
| 194 | assertLeaseTimeParses(true, 60, 60 * 1000, oneMinuteLease); |
| 195 | assertLeaseTimeParses(true, 300, 300 * 1000, fiveMinuteLease); |
| 196 | assertLeaseTimeParses(true, 86400, 86400 * 1000, oneDayLease); |
| 197 | assertLeaseTimeParses(true, -2147483647, 2147483649L * 1000, maxIntPlusOneLease); |
| 198 | assertLeaseTimeParses(true, DhcpPacket.INFINITE_LEASE, 0, infiniteLease); |
| 199 | } |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 200 | |
| 201 | private void checkIpAddress(String expected, Inet4Address clientIp, Inet4Address yourIp, |
| 202 | byte[] netmaskBytes) { |
| 203 | checkIpAddress(expected, DHCP_MESSAGE_TYPE_OFFER, clientIp, yourIp, netmaskBytes); |
| 204 | checkIpAddress(expected, DHCP_MESSAGE_TYPE_ACK, clientIp, yourIp, netmaskBytes); |
| 205 | } |
| 206 | |
| 207 | private void checkIpAddress(String expected, byte type, |
| 208 | Inet4Address clientIp, Inet4Address yourIp, |
| 209 | byte[] netmaskBytes) { |
| 210 | ByteBuffer packet = new TestDhcpPacket(type, clientIp, yourIp) |
| 211 | .setNetmaskBytes(netmaskBytes) |
| 212 | .build(); |
| 213 | DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP); |
| 214 | DhcpResults results = offerPacket.toDhcpResults(); |
| 215 | |
| 216 | if (expected != null) { |
| 217 | LinkAddress expectedAddress = new LinkAddress(expected); |
| 218 | assertEquals(expectedAddress, results.ipAddress); |
| 219 | } else { |
| 220 | assertNull(results); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | @SmallTest |
| 225 | public void testIpAddress() throws Exception { |
| 226 | byte[] slash11Netmask = new byte[] { (byte) 0xff, (byte) 0xe0, 0x00, 0x00 }; |
| 227 | byte[] slash24Netmask = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00 }; |
| 228 | byte[] invalidNetmask = new byte[] { (byte) 0xff, (byte) 0xfb, (byte) 0xff, 0x00 }; |
Lorenzo Colitti | b0b3d0b | 2015-07-06 12:29:43 +0900 | [diff] [blame^] | 229 | Inet4Address example1 = v4Address("192.0.2.1"); |
| 230 | Inet4Address example2 = v4Address("192.0.2.43"); |
Lorenzo Colitti | f68edb1 | 2015-06-02 17:46:24 +0900 | [diff] [blame] | 231 | |
| 232 | // A packet without any addresses is not valid. |
| 233 | checkIpAddress(null, ANY, ANY, slash24Netmask); |
| 234 | |
| 235 | // ClientIP is used iff YourIP is not present. |
| 236 | checkIpAddress("192.0.2.1/24", example2, example1, slash24Netmask); |
| 237 | checkIpAddress("192.0.2.43/11", example2, ANY, slash11Netmask); |
| 238 | checkIpAddress("192.0.2.43/11", ANY, example2, slash11Netmask); |
| 239 | |
| 240 | // Invalid netmasks are ignored. |
| 241 | checkIpAddress(null, example2, ANY, invalidNetmask); |
| 242 | |
| 243 | // If there is no netmask, implicit netmasks are used. |
| 244 | checkIpAddress("192.0.2.43/24", ANY, example2, null); |
| 245 | } |
Lorenzo Colitti | b0b3d0b | 2015-07-06 12:29:43 +0900 | [diff] [blame^] | 246 | |
| 247 | private void assertDhcpResults(String ipAddress, String gateway, String dnsServersString, |
| 248 | String domains, String serverAddress, String vendorInfo, int leaseDuration, |
| 249 | boolean hasMeteredHint, DhcpResults dhcpResults) throws Exception { |
| 250 | assertEquals(new LinkAddress(ipAddress), dhcpResults.ipAddress); |
| 251 | assertEquals(v4Address(gateway), dhcpResults.gateway); |
| 252 | |
| 253 | String[] dnsServerStrings = dnsServersString.split(","); |
| 254 | ArrayList dnsServers = new ArrayList(); |
| 255 | for (String dnsServerString : dnsServerStrings) { |
| 256 | dnsServers.add(v4Address(dnsServerString)); |
| 257 | } |
| 258 | assertEquals(dnsServers, dhcpResults.dnsServers); |
| 259 | |
| 260 | assertEquals(domains, dhcpResults.domains); |
| 261 | assertEquals(v4Address(serverAddress), dhcpResults.serverAddress); |
| 262 | assertEquals(vendorInfo, dhcpResults.vendorInfo); |
| 263 | assertEquals(leaseDuration, dhcpResults.leaseDuration); |
| 264 | assertEquals(hasMeteredHint, dhcpResults.hasMeteredHint()); |
| 265 | } |
| 266 | |
| 267 | @SmallTest |
| 268 | public void testOffer1() throws Exception { |
| 269 | // TODO: Turn all of these into golden files. This will probably require modifying |
| 270 | // Android.mk appropriately, making this into an AndroidTestCase, and adding code to read |
| 271 | // the golden files from the test APK's assets via mContext.getAssets(). |
| 272 | final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode(( |
| 273 | // IP header. |
| 274 | "451001480000000080118849c0a89003c0a89ff7" + |
| 275 | // UDP header. |
| 276 | "004300440134dcfa" + |
| 277 | // BOOTP header. |
| 278 | "02010600c997a63b0000000000000000c0a89ff70000000000000000" + |
| 279 | // MAC address. |
| 280 | "30766ff2a90c00000000000000000000" + |
| 281 | // Server name. |
| 282 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 283 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 284 | // File. |
| 285 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 286 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 287 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 288 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 289 | // Options |
| 290 | "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" + |
| 291 | "3a0400000e103b040000189cff00000000000000000000" |
| 292 | ).toCharArray(), false)); |
| 293 | |
| 294 | DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3); |
| 295 | assertTrue(offerPacket instanceof DhcpOfferPacket); // Implicitly checks it's non-null. |
| 296 | DhcpResults dhcpResults = offerPacket.toDhcpResults(); |
| 297 | assertDhcpResults("192.168.159.247/20", "192.168.159.254", "8.8.8.8,8.8.4.4", |
| 298 | null, "192.168.144.3", null, 7200, false, dhcpResults); |
| 299 | } |
| 300 | |
| 301 | @SmallTest |
| 302 | public void testOffer2() throws Exception { |
| 303 | final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode(( |
| 304 | // IP header. |
| 305 | "450001518d0600004011144dc0a82b01c0a82bf7" + |
| 306 | // UDP header. |
| 307 | "00430044013d9ac7" + |
| 308 | // BOOTP header. |
| 309 | "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" + |
| 310 | // MAC address. |
| 311 | "30766ff2a90c00000000000000000000" + |
| 312 | // Server name. |
| 313 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 314 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 315 | // File. |
| 316 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 317 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 318 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 319 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 320 | // Options |
| 321 | "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" + |
| 322 | "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff" |
| 323 | ).toCharArray(), false)); |
| 324 | |
| 325 | assertEquals(337, packet.limit()); |
| 326 | DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3); |
| 327 | assertTrue(offerPacket instanceof DhcpOfferPacket); // Implicitly checks it's non-null. |
| 328 | DhcpResults dhcpResults = offerPacket.toDhcpResults(); |
| 329 | assertDhcpResults("192.168.43.247/24", "192.168.43.1", "192.168.43.1", |
| 330 | null, "192.168.43.1", "ANDROID_METERED", 3600, true, dhcpResults); |
| 331 | assertTrue(dhcpResults.hasMeteredHint()); |
| 332 | } |
| 333 | |
| 334 | @SmallTest |
| 335 | public void testPadAndOverloadedOptionsOffer() throws Exception { |
| 336 | // A packet observed in the real world that is interesting for two reasons: |
| 337 | // |
| 338 | // 1. It uses pad bytes, which we previously didn't support correctly. |
| 339 | // 2. It uses DHCP option overloading, which we don't currently support (but it doesn't |
| 340 | // store any information in the overloaded fields). |
| 341 | // |
| 342 | // For now, we just check that it parses correctly. |
| 343 | final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode(( |
| 344 | // Ethernet header. |
| 345 | "b4cef6000000e80462236e300800" + |
| 346 | // IP header. |
| 347 | "4500014c00000000ff11741701010101ac119876" + |
| 348 | // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation). |
| 349 | "004300440138ae5a" + |
| 350 | // BOOTP header. |
| 351 | "020106000fa0059f0000000000000000ac1198760000000000000000" + |
| 352 | // MAC address. |
| 353 | "b4cef600000000000000000000000000" + |
| 354 | // Server name. |
| 355 | "ff00000000000000000000000000000000000000000000000000000000000000" + |
| 356 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 357 | // File. |
| 358 | "ff00000000000000000000000000000000000000000000000000000000000000" + |
| 359 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 360 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 361 | "0000000000000000000000000000000000000000000000000000000000000000" + |
| 362 | // Options |
| 363 | "638253633501023604010101010104ffff000033040000a8c03401030304ac1101010604ac110101" + |
| 364 | "0000000000000000000000000000000000000000000000ff000000" |
| 365 | ).toCharArray(), false)); |
| 366 | |
| 367 | DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2); |
| 368 | assertTrue(offerPacket instanceof DhcpOfferPacket); |
| 369 | DhcpResults dhcpResults = offerPacket.toDhcpResults(); |
| 370 | assertDhcpResults("172.17.152.118/16", "172.17.1.1", "172.17.1.1", |
| 371 | null, "1.1.1.1", null, 43200, false, dhcpResults); |
| 372 | } |
Lorenzo Colitti | c2abb2b | 2015-03-31 16:26:57 +0900 | [diff] [blame] | 373 | } |