blob: 4f80c6056ac8681a576993c9f089280c9d0666ea [file] [log] [blame]
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +09001/*
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
17package android.net.dhcp;
18
19import android.net.NetworkUtils;
Lorenzo Colittid9735372015-06-02 13:15:50 +090020import android.net.DhcpResults;
Lorenzo Colittif68edb12015-06-02 17:46:24 +090021import android.net.LinkAddress;
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090022import android.system.OsConstants;
23import android.test.suitebuilder.annotation.SmallTest;
Lorenzo Colittif28e62b2015-07-27 16:35:22 +090024import com.android.internal.util.HexDump;
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090025
26import java.net.Inet4Address;
27import java.nio.ByteBuffer;
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +090028import java.util.ArrayList;
29
Lorenzo Colittif28e62b2015-07-27 16:35:22 +090030import junit.framework.TestCase;
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +090031import libcore.util.HexEncoding;
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090032
33import static android.net.dhcp.DhcpPacket.*;
34
35
36public class DhcpPacketTest extends TestCase {
37
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +090038 private static Inet4Address SERVER_ADDR = v4Address("192.0.2.1");
39 private static Inet4Address CLIENT_ADDR = v4Address("192.0.2.234");
Lorenzo Colittif68edb12015-06-02 17:46:24 +090040 // Use our own empty address instead of Inet4Address.ANY or INADDR_ANY to ensure that the code
41 // doesn't use == instead of equals when comparing addresses.
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +090042 private static Inet4Address ANY = (Inet4Address) v4Address("0.0.0.0");
Lorenzo Colittif68edb12015-06-02 17:46:24 +090043
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090044 private static byte[] CLIENT_MAC = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
45
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +090046 private static final Inet4Address v4Address(String addrString) throws IllegalArgumentException {
47 return (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
48 }
49
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090050 class TestDhcpPacket extends DhcpPacket {
51 private byte mType;
52 // TODO: Make this a map of option numbers to bytes instead.
Lorenzo Colittif68edb12015-06-02 17:46:24 +090053 private byte[] mDomainBytes, mVendorInfoBytes, mLeaseTimeBytes, mNetmaskBytes;
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090054
Lorenzo Colittif68edb12015-06-02 17:46:24 +090055 public TestDhcpPacket(byte type, Inet4Address clientIp, Inet4Address yourIp) {
56 super(0xdeadbeef, (short) 0, clientIp, yourIp, INADDR_ANY, INADDR_ANY,
Lorenzo Colitti3e979322015-04-21 15:22:17 +090057 CLIENT_MAC, true);
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090058 mType = type;
Lorenzo Colittid9735372015-06-02 13:15:50 +090059 }
60
Lorenzo Colittif68edb12015-06-02 17:46:24 +090061 public TestDhcpPacket(byte type) {
62 this(type, INADDR_ANY, CLIENT_ADDR);
63 }
64
Lorenzo Colittid9735372015-06-02 13:15:50 +090065 public TestDhcpPacket setDomainBytes(byte[] domainBytes) {
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090066 mDomainBytes = domainBytes;
Lorenzo Colittid9735372015-06-02 13:15:50 +090067 return this;
68 }
69
70 public TestDhcpPacket setVendorInfoBytes(byte[] vendorInfoBytes) {
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090071 mVendorInfoBytes = vendorInfoBytes;
Lorenzo Colittid9735372015-06-02 13:15:50 +090072 return this;
73 }
74
75 public TestDhcpPacket setLeaseTimeBytes(byte[] leaseTimeBytes) {
76 mLeaseTimeBytes = leaseTimeBytes;
77 return this;
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090078 }
79
Lorenzo Colittif68edb12015-06-02 17:46:24 +090080 public TestDhcpPacket setNetmaskBytes(byte[] netmaskBytes) {
81 mNetmaskBytes = netmaskBytes;
82 return this;
83 }
84
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090085 public ByteBuffer buildPacket(int encap, short unusedDestUdp, short unusedSrcUdp) {
86 ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH);
87 fillInPacket(encap, CLIENT_ADDR, SERVER_ADDR,
88 DHCP_CLIENT, DHCP_SERVER, result, DHCP_BOOTREPLY, false);
89 return result;
90 }
91
92 public void finishPacket(ByteBuffer buffer) {
93 addTlv(buffer, DHCP_MESSAGE_TYPE, mType);
94 if (mDomainBytes != null) {
95 addTlv(buffer, DHCP_DOMAIN_NAME, mDomainBytes);
96 }
97 if (mVendorInfoBytes != null) {
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +090098 addTlv(buffer, DHCP_VENDOR_INFO, mVendorInfoBytes);
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +090099 }
Lorenzo Colittid9735372015-06-02 13:15:50 +0900100 if (mLeaseTimeBytes != null) {
101 addTlv(buffer, DHCP_LEASE_TIME, mLeaseTimeBytes);
102 }
Lorenzo Colittif68edb12015-06-02 17:46:24 +0900103 if (mNetmaskBytes != null) {
104 addTlv(buffer, DHCP_SUBNET_MASK, mNetmaskBytes);
105 }
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +0900106 addTlvEnd(buffer);
107 }
108
109 // Convenience method.
110 public ByteBuffer build() {
111 // ENCAP_BOOTP packets don't contain ports, so just pass in 0.
112 ByteBuffer pkt = buildPacket(ENCAP_BOOTP, (short) 0, (short) 0);
113 pkt.flip();
114 return pkt;
115 }
116 }
117
118 private void assertDomainAndVendorInfoParses(
119 String expectedDomain, byte[] domainBytes,
Erik Kline496906e2015-09-16 15:44:54 +0900120 String expectedVendorInfo, byte[] vendorInfoBytes) throws Exception {
Lorenzo Colittid9735372015-06-02 13:15:50 +0900121 ByteBuffer packet = new TestDhcpPacket(DHCP_MESSAGE_TYPE_OFFER)
122 .setDomainBytes(domainBytes)
123 .setVendorInfoBytes(vendorInfoBytes)
124 .build();
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +0900125 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
126 assertEquals(expectedDomain, offerPacket.mDomainName);
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +0900127 assertEquals(expectedVendorInfo, offerPacket.mVendorInfo);
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +0900128 }
129
130 @SmallTest
131 public void testDomainName() throws Exception {
132 byte[] nullByte = new byte[] { 0x00 };
133 byte[] twoNullBytes = new byte[] { 0x00, 0x00 };
134 byte[] nonNullDomain = new byte[] {
135 (byte) 'g', (byte) 'o', (byte) 'o', (byte) '.', (byte) 'g', (byte) 'l'
136 };
137 byte[] trailingNullDomain = new byte[] {
138 (byte) 'g', (byte) 'o', (byte) 'o', (byte) '.', (byte) 'g', (byte) 'l', 0x00
139 };
140 byte[] embeddedNullsDomain = new byte[] {
141 (byte) 'g', (byte) 'o', (byte) 'o', 0x00, 0x00, (byte) 'g', (byte) 'l'
142 };
143 byte[] metered = "ANDROID_METERED".getBytes("US-ASCII");
144
145 byte[] meteredEmbeddedNull = metered.clone();
146 meteredEmbeddedNull[7] = (char) 0;
147
148 byte[] meteredTrailingNull = metered.clone();
149 meteredTrailingNull[meteredTrailingNull.length - 1] = (char) 0;
150
151 assertDomainAndVendorInfoParses("", nullByte, "\u0000", nullByte);
152 assertDomainAndVendorInfoParses("", twoNullBytes, "\u0000\u0000", twoNullBytes);
153 assertDomainAndVendorInfoParses("goo.gl", nonNullDomain, "ANDROID_METERED", metered);
154 assertDomainAndVendorInfoParses("goo", embeddedNullsDomain,
155 "ANDROID\u0000METERED", meteredEmbeddedNull);
156 assertDomainAndVendorInfoParses("goo.gl", trailingNullDomain,
157 "ANDROID_METERE\u0000", meteredTrailingNull);
158 }
Lorenzo Colittid9735372015-06-02 13:15:50 +0900159
160 private void assertLeaseTimeParses(boolean expectValid, Integer rawLeaseTime,
Erik Kline496906e2015-09-16 15:44:54 +0900161 long leaseTimeMillis, byte[] leaseTimeBytes) throws Exception {
Lorenzo Colittid9735372015-06-02 13:15:50 +0900162 TestDhcpPacket testPacket = new TestDhcpPacket(DHCP_MESSAGE_TYPE_OFFER);
163 if (leaseTimeBytes != null) {
164 testPacket.setLeaseTimeBytes(leaseTimeBytes);
165 }
166 ByteBuffer packet = testPacket.build();
Erik Kline496906e2015-09-16 15:44:54 +0900167 DhcpPacket offerPacket = null;
168
Lorenzo Colittid9735372015-06-02 13:15:50 +0900169 if (!expectValid) {
Erik Kline496906e2015-09-16 15:44:54 +0900170 try {
171 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
172 fail("Invalid packet parsed successfully: " + offerPacket);
173 } catch (ParseException expected) {
174 }
Lorenzo Colittid9735372015-06-02 13:15:50 +0900175 return;
176 }
Erik Kline496906e2015-09-16 15:44:54 +0900177
178 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
179 assertNotNull(offerPacket);
Lorenzo Colittid9735372015-06-02 13:15:50 +0900180 assertEquals(rawLeaseTime, offerPacket.mLeaseTime);
181 DhcpResults dhcpResults = offerPacket.toDhcpResults(); // Just check this doesn't crash.
182 assertEquals(leaseTimeMillis, offerPacket.getLeaseTimeMillis());
183 }
184
185 @SmallTest
186 public void testLeaseTime() throws Exception {
187 byte[] noLease = null;
188 byte[] tooShortLease = new byte[] { 0x00, 0x00 };
189 byte[] tooLongLease = new byte[] { 0x00, 0x00, 0x00, 60, 0x01 };
190 byte[] zeroLease = new byte[] { 0x00, 0x00, 0x00, 0x00 };
191 byte[] tenSecondLease = new byte[] { 0x00, 0x00, 0x00, 10 };
192 byte[] oneMinuteLease = new byte[] { 0x00, 0x00, 0x00, 60 };
193 byte[] fiveMinuteLease = new byte[] { 0x00, 0x00, 0x01, 0x2c };
194 byte[] oneDayLease = new byte[] { 0x00, 0x01, 0x51, (byte) 0x80 };
195 byte[] maxIntPlusOneLease = new byte[] { (byte) 0x80, 0x00, 0x00, 0x01 };
196 byte[] infiniteLease = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
197
198 assertLeaseTimeParses(true, null, 0, noLease);
199 assertLeaseTimeParses(false, null, 0, tooShortLease);
200 assertLeaseTimeParses(false, null, 0, tooLongLease);
201 assertLeaseTimeParses(true, 0, 60 * 1000, zeroLease);
202 assertLeaseTimeParses(true, 10, 60 * 1000, tenSecondLease);
203 assertLeaseTimeParses(true, 60, 60 * 1000, oneMinuteLease);
204 assertLeaseTimeParses(true, 300, 300 * 1000, fiveMinuteLease);
205 assertLeaseTimeParses(true, 86400, 86400 * 1000, oneDayLease);
206 assertLeaseTimeParses(true, -2147483647, 2147483649L * 1000, maxIntPlusOneLease);
207 assertLeaseTimeParses(true, DhcpPacket.INFINITE_LEASE, 0, infiniteLease);
208 }
Lorenzo Colittif68edb12015-06-02 17:46:24 +0900209
210 private void checkIpAddress(String expected, Inet4Address clientIp, Inet4Address yourIp,
Erik Kline496906e2015-09-16 15:44:54 +0900211 byte[] netmaskBytes) throws Exception {
Lorenzo Colittif68edb12015-06-02 17:46:24 +0900212 checkIpAddress(expected, DHCP_MESSAGE_TYPE_OFFER, clientIp, yourIp, netmaskBytes);
213 checkIpAddress(expected, DHCP_MESSAGE_TYPE_ACK, clientIp, yourIp, netmaskBytes);
214 }
215
216 private void checkIpAddress(String expected, byte type,
217 Inet4Address clientIp, Inet4Address yourIp,
Erik Kline496906e2015-09-16 15:44:54 +0900218 byte[] netmaskBytes) throws Exception {
Lorenzo Colittif68edb12015-06-02 17:46:24 +0900219 ByteBuffer packet = new TestDhcpPacket(type, clientIp, yourIp)
220 .setNetmaskBytes(netmaskBytes)
221 .build();
222 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
223 DhcpResults results = offerPacket.toDhcpResults();
224
225 if (expected != null) {
226 LinkAddress expectedAddress = new LinkAddress(expected);
227 assertEquals(expectedAddress, results.ipAddress);
228 } else {
229 assertNull(results);
230 }
231 }
232
233 @SmallTest
234 public void testIpAddress() throws Exception {
235 byte[] slash11Netmask = new byte[] { (byte) 0xff, (byte) 0xe0, 0x00, 0x00 };
236 byte[] slash24Netmask = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00 };
237 byte[] invalidNetmask = new byte[] { (byte) 0xff, (byte) 0xfb, (byte) 0xff, 0x00 };
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +0900238 Inet4Address example1 = v4Address("192.0.2.1");
239 Inet4Address example2 = v4Address("192.0.2.43");
Lorenzo Colittif68edb12015-06-02 17:46:24 +0900240
241 // A packet without any addresses is not valid.
242 checkIpAddress(null, ANY, ANY, slash24Netmask);
243
244 // ClientIP is used iff YourIP is not present.
245 checkIpAddress("192.0.2.1/24", example2, example1, slash24Netmask);
246 checkIpAddress("192.0.2.43/11", example2, ANY, slash11Netmask);
247 checkIpAddress("192.0.2.43/11", ANY, example2, slash11Netmask);
248
249 // Invalid netmasks are ignored.
250 checkIpAddress(null, example2, ANY, invalidNetmask);
251
252 // If there is no netmask, implicit netmasks are used.
253 checkIpAddress("192.0.2.43/24", ANY, example2, null);
254 }
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +0900255
256 private void assertDhcpResults(String ipAddress, String gateway, String dnsServersString,
257 String domains, String serverAddress, String vendorInfo, int leaseDuration,
258 boolean hasMeteredHint, DhcpResults dhcpResults) throws Exception {
259 assertEquals(new LinkAddress(ipAddress), dhcpResults.ipAddress);
260 assertEquals(v4Address(gateway), dhcpResults.gateway);
261
262 String[] dnsServerStrings = dnsServersString.split(",");
263 ArrayList dnsServers = new ArrayList();
264 for (String dnsServerString : dnsServerStrings) {
265 dnsServers.add(v4Address(dnsServerString));
266 }
267 assertEquals(dnsServers, dhcpResults.dnsServers);
268
269 assertEquals(domains, dhcpResults.domains);
270 assertEquals(v4Address(serverAddress), dhcpResults.serverAddress);
271 assertEquals(vendorInfo, dhcpResults.vendorInfo);
272 assertEquals(leaseDuration, dhcpResults.leaseDuration);
273 assertEquals(hasMeteredHint, dhcpResults.hasMeteredHint());
274 }
275
276 @SmallTest
277 public void testOffer1() throws Exception {
278 // TODO: Turn all of these into golden files. This will probably require modifying
279 // Android.mk appropriately, making this into an AndroidTestCase, and adding code to read
280 // the golden files from the test APK's assets via mContext.getAssets().
281 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
282 // IP header.
283 "451001480000000080118849c0a89003c0a89ff7" +
284 // UDP header.
285 "004300440134dcfa" +
286 // BOOTP header.
287 "02010600c997a63b0000000000000000c0a89ff70000000000000000" +
288 // MAC address.
289 "30766ff2a90c00000000000000000000" +
290 // Server name.
291 "0000000000000000000000000000000000000000000000000000000000000000" +
292 "0000000000000000000000000000000000000000000000000000000000000000" +
293 // File.
294 "0000000000000000000000000000000000000000000000000000000000000000" +
295 "0000000000000000000000000000000000000000000000000000000000000000" +
296 "0000000000000000000000000000000000000000000000000000000000000000" +
297 "0000000000000000000000000000000000000000000000000000000000000000" +
298 // Options
299 "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" +
300 "3a0400000e103b040000189cff00000000000000000000"
301 ).toCharArray(), false));
302
303 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
304 assertTrue(offerPacket instanceof DhcpOfferPacket); // Implicitly checks it's non-null.
305 DhcpResults dhcpResults = offerPacket.toDhcpResults();
306 assertDhcpResults("192.168.159.247/20", "192.168.159.254", "8.8.8.8,8.8.4.4",
307 null, "192.168.144.3", null, 7200, false, dhcpResults);
308 }
309
310 @SmallTest
311 public void testOffer2() throws Exception {
312 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
313 // IP header.
314 "450001518d0600004011144dc0a82b01c0a82bf7" +
315 // UDP header.
316 "00430044013d9ac7" +
317 // BOOTP header.
318 "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
319 // MAC address.
320 "30766ff2a90c00000000000000000000" +
321 // Server name.
322 "0000000000000000000000000000000000000000000000000000000000000000" +
323 "0000000000000000000000000000000000000000000000000000000000000000" +
324 // File.
325 "0000000000000000000000000000000000000000000000000000000000000000" +
326 "0000000000000000000000000000000000000000000000000000000000000000" +
327 "0000000000000000000000000000000000000000000000000000000000000000" +
328 "0000000000000000000000000000000000000000000000000000000000000000" +
329 // Options
330 "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
331 "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"
332 ).toCharArray(), false));
333
334 assertEquals(337, packet.limit());
335 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
336 assertTrue(offerPacket instanceof DhcpOfferPacket); // Implicitly checks it's non-null.
337 DhcpResults dhcpResults = offerPacket.toDhcpResults();
338 assertDhcpResults("192.168.43.247/24", "192.168.43.1", "192.168.43.1",
339 null, "192.168.43.1", "ANDROID_METERED", 3600, true, dhcpResults);
340 assertTrue(dhcpResults.hasMeteredHint());
341 }
342
343 @SmallTest
Lorenzo Colittid64144a2015-09-03 17:36:20 +0900344 public void testBadHwaddrLength() throws Exception {
345 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
346 // IP header.
347 "450001518d0600004011144dc0a82b01c0a82bf7" +
348 // UDP header.
349 "00430044013d9ac7" +
350 // BOOTP header.
351 "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
352 // MAC address.
353 "30766ff2a90c00000000000000000000" +
354 // Server name.
355 "0000000000000000000000000000000000000000000000000000000000000000" +
356 "0000000000000000000000000000000000000000000000000000000000000000" +
357 // File.
358 "0000000000000000000000000000000000000000000000000000000000000000" +
359 "0000000000000000000000000000000000000000000000000000000000000000" +
360 "0000000000000000000000000000000000000000000000000000000000000000" +
361 "0000000000000000000000000000000000000000000000000000000000000000" +
362 // Options
363 "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
364 "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"
365 ).toCharArray(), false));
366 String expectedClientMac = "30766FF2A90C";
367
368 final int hwAddrLenOffset = 20 + 8 + 2;
369 assertEquals(6, packet.get(hwAddrLenOffset));
370
371 // Expect the expected.
372 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
373 assertNotNull(offerPacket);
374 assertEquals(6, offerPacket.getClientMac().length);
375 assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
376
377 // Reduce the hardware address length and verify that it shortens the client MAC.
378 packet.flip();
379 packet.put(hwAddrLenOffset, (byte) 5);
380 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
381 assertNotNull(offerPacket);
382 assertEquals(5, offerPacket.getClientMac().length);
383 assertEquals(expectedClientMac.substring(0, 10),
384 HexDump.toHexString(offerPacket.getClientMac()));
385
386 packet.flip();
387 packet.put(hwAddrLenOffset, (byte) 3);
388 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
389 assertNotNull(offerPacket);
390 assertEquals(3, offerPacket.getClientMac().length);
391 assertEquals(expectedClientMac.substring(0, 6),
392 HexDump.toHexString(offerPacket.getClientMac()));
393
394 // Set the the hardware address length to 0xff and verify that we a) don't treat it as -1
395 // and crash, and b) hardcode it to 6.
396 packet.flip();
397 packet.put(hwAddrLenOffset, (byte) -1);
398 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
399 assertNotNull(offerPacket);
400 assertEquals(6, offerPacket.getClientMac().length);
401 assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
402
403 // Set the the hardware address length to a positive invalid value (> 16) and verify that we
404 // hardcode it to 6.
405 packet.flip();
406 packet.put(hwAddrLenOffset, (byte) 17);
407 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
408 assertNotNull(offerPacket);
409 assertEquals(6, offerPacket.getClientMac().length);
410 assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
411 }
412
413 @SmallTest
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +0900414 public void testPadAndOverloadedOptionsOffer() throws Exception {
415 // A packet observed in the real world that is interesting for two reasons:
416 //
417 // 1. It uses pad bytes, which we previously didn't support correctly.
418 // 2. It uses DHCP option overloading, which we don't currently support (but it doesn't
419 // store any information in the overloaded fields).
420 //
421 // For now, we just check that it parses correctly.
422 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
423 // Ethernet header.
424 "b4cef6000000e80462236e300800" +
425 // IP header.
426 "4500014c00000000ff11741701010101ac119876" +
427 // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
428 "004300440138ae5a" +
429 // BOOTP header.
430 "020106000fa0059f0000000000000000ac1198760000000000000000" +
431 // MAC address.
432 "b4cef600000000000000000000000000" +
433 // Server name.
434 "ff00000000000000000000000000000000000000000000000000000000000000" +
435 "0000000000000000000000000000000000000000000000000000000000000000" +
436 // File.
437 "ff00000000000000000000000000000000000000000000000000000000000000" +
438 "0000000000000000000000000000000000000000000000000000000000000000" +
439 "0000000000000000000000000000000000000000000000000000000000000000" +
440 "0000000000000000000000000000000000000000000000000000000000000000" +
441 // Options
442 "638253633501023604010101010104ffff000033040000a8c03401030304ac1101010604ac110101" +
443 "0000000000000000000000000000000000000000000000ff000000"
444 ).toCharArray(), false));
445
446 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
447 assertTrue(offerPacket instanceof DhcpOfferPacket);
448 DhcpResults dhcpResults = offerPacket.toDhcpResults();
449 assertDhcpResults("172.17.152.118/16", "172.17.1.1", "172.17.1.1",
450 null, "1.1.1.1", null, 43200, false, dhcpResults);
451 }
Lorenzo Colittif28e62b2015-07-27 16:35:22 +0900452
453 @SmallTest
454 public void testBug2111() throws Exception {
455 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
456 // IP header.
457 "4500014c00000000ff119beac3eaf3880a3f5d04" +
458 // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
459 "0043004401387464" +
460 // BOOTP header.
461 "0201060002554812000a0000000000000a3f5d040000000000000000" +
462 // MAC address.
463 "00904c00000000000000000000000000" +
464 // Server name.
465 "0000000000000000000000000000000000000000000000000000000000000000" +
466 "0000000000000000000000000000000000000000000000000000000000000000" +
467 // File.
468 "0000000000000000000000000000000000000000000000000000000000000000" +
469 "0000000000000000000000000000000000000000000000000000000000000000" +
470 "0000000000000000000000000000000000000000000000000000000000000000" +
471 "0000000000000000000000000000000000000000000000000000000000000000" +
472 // Options.
473 "638253633501023604c00002fe33040000bfc60104fffff00003040a3f50010608c0000201c0000202" +
474 "0f0f646f6d61696e3132332e636f2e756b0000000000ff00000000"
475 ).toCharArray(), false));
476
477 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
478 assertTrue(offerPacket instanceof DhcpOfferPacket);
479 DhcpResults dhcpResults = offerPacket.toDhcpResults();
480 assertDhcpResults("10.63.93.4/20", "10.63.80.1", "192.0.2.1,192.0.2.2",
481 "domain123.co.uk", "192.0.2.254", null, 49094, false, dhcpResults);
482 }
483
484 @SmallTest
485 public void testBug2136() throws Exception {
486 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
487 // Ethernet header.
488 "bcf5ac000000d0c7890000000800" +
489 // IP header.
490 "4500014c00000000ff119beac3eaf3880a3f5d04" +
491 // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
492 "0043004401387574" +
493 // BOOTP header.
494 "0201060163339a3000050000000000000a209ecd0000000000000000" +
495 // MAC address.
496 "bcf5ac00000000000000000000000000" +
497 // Server name.
498 "0000000000000000000000000000000000000000000000000000000000000000" +
499 "0000000000000000000000000000000000000000000000000000000000000000" +
500 // File.
501 "0000000000000000000000000000000000000000000000000000000000000000" +
502 "0000000000000000000000000000000000000000000000000000000000000000" +
503 "0000000000000000000000000000000000000000000000000000000000000000" +
504 "0000000000000000000000000000000000000000000000000000000000000000" +
505 // Options.
506 "6382536335010236040a20ff80330400001c200104fffff00003040a20900106089458413494584135" +
507 "0f0b6c616e63732e61632e756b000000000000000000ff00000000"
508 ).toCharArray(), false));
509
510 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
511 assertTrue(offerPacket instanceof DhcpOfferPacket);
512 assertEquals("BCF5AC000000", HexDump.toHexString(offerPacket.getClientMac()));
513 DhcpResults dhcpResults = offerPacket.toDhcpResults();
514 assertDhcpResults("10.32.158.205/20", "10.32.144.1", "148.88.65.52,148.88.65.53",
515 "lancs.ac.uk", "10.32.255.128", null, 7200, false, dhcpResults);
516 }
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +0900517}