blob: cd3b8bb7e0b985998533f6f99aa5f885f06f5d8e [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,
120 String expectedVendorInfo, byte[] vendorInfoBytes) {
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,
161 long leaseTimeMillis, byte[] leaseTimeBytes) {
162 TestDhcpPacket testPacket = new TestDhcpPacket(DHCP_MESSAGE_TYPE_OFFER);
163 if (leaseTimeBytes != null) {
164 testPacket.setLeaseTimeBytes(leaseTimeBytes);
165 }
166 ByteBuffer packet = testPacket.build();
167 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
168 if (!expectValid) {
169 assertNull(offerPacket);
170 return;
171 }
172 assertEquals(rawLeaseTime, offerPacket.mLeaseTime);
173 DhcpResults dhcpResults = offerPacket.toDhcpResults(); // Just check this doesn't crash.
174 assertEquals(leaseTimeMillis, offerPacket.getLeaseTimeMillis());
175 }
176
177 @SmallTest
178 public void testLeaseTime() throws Exception {
179 byte[] noLease = null;
180 byte[] tooShortLease = new byte[] { 0x00, 0x00 };
181 byte[] tooLongLease = new byte[] { 0x00, 0x00, 0x00, 60, 0x01 };
182 byte[] zeroLease = new byte[] { 0x00, 0x00, 0x00, 0x00 };
183 byte[] tenSecondLease = new byte[] { 0x00, 0x00, 0x00, 10 };
184 byte[] oneMinuteLease = new byte[] { 0x00, 0x00, 0x00, 60 };
185 byte[] fiveMinuteLease = new byte[] { 0x00, 0x00, 0x01, 0x2c };
186 byte[] oneDayLease = new byte[] { 0x00, 0x01, 0x51, (byte) 0x80 };
187 byte[] maxIntPlusOneLease = new byte[] { (byte) 0x80, 0x00, 0x00, 0x01 };
188 byte[] infiniteLease = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
189
190 assertLeaseTimeParses(true, null, 0, noLease);
191 assertLeaseTimeParses(false, null, 0, tooShortLease);
192 assertLeaseTimeParses(false, null, 0, tooLongLease);
193 assertLeaseTimeParses(true, 0, 60 * 1000, zeroLease);
194 assertLeaseTimeParses(true, 10, 60 * 1000, tenSecondLease);
195 assertLeaseTimeParses(true, 60, 60 * 1000, oneMinuteLease);
196 assertLeaseTimeParses(true, 300, 300 * 1000, fiveMinuteLease);
197 assertLeaseTimeParses(true, 86400, 86400 * 1000, oneDayLease);
198 assertLeaseTimeParses(true, -2147483647, 2147483649L * 1000, maxIntPlusOneLease);
199 assertLeaseTimeParses(true, DhcpPacket.INFINITE_LEASE, 0, infiniteLease);
200 }
Lorenzo Colittif68edb12015-06-02 17:46:24 +0900201
202 private void checkIpAddress(String expected, Inet4Address clientIp, Inet4Address yourIp,
203 byte[] netmaskBytes) {
204 checkIpAddress(expected, DHCP_MESSAGE_TYPE_OFFER, clientIp, yourIp, netmaskBytes);
205 checkIpAddress(expected, DHCP_MESSAGE_TYPE_ACK, clientIp, yourIp, netmaskBytes);
206 }
207
208 private void checkIpAddress(String expected, byte type,
209 Inet4Address clientIp, Inet4Address yourIp,
210 byte[] netmaskBytes) {
211 ByteBuffer packet = new TestDhcpPacket(type, clientIp, yourIp)
212 .setNetmaskBytes(netmaskBytes)
213 .build();
214 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
215 DhcpResults results = offerPacket.toDhcpResults();
216
217 if (expected != null) {
218 LinkAddress expectedAddress = new LinkAddress(expected);
219 assertEquals(expectedAddress, results.ipAddress);
220 } else {
221 assertNull(results);
222 }
223 }
224
225 @SmallTest
226 public void testIpAddress() throws Exception {
227 byte[] slash11Netmask = new byte[] { (byte) 0xff, (byte) 0xe0, 0x00, 0x00 };
228 byte[] slash24Netmask = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00 };
229 byte[] invalidNetmask = new byte[] { (byte) 0xff, (byte) 0xfb, (byte) 0xff, 0x00 };
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +0900230 Inet4Address example1 = v4Address("192.0.2.1");
231 Inet4Address example2 = v4Address("192.0.2.43");
Lorenzo Colittif68edb12015-06-02 17:46:24 +0900232
233 // A packet without any addresses is not valid.
234 checkIpAddress(null, ANY, ANY, slash24Netmask);
235
236 // ClientIP is used iff YourIP is not present.
237 checkIpAddress("192.0.2.1/24", example2, example1, slash24Netmask);
238 checkIpAddress("192.0.2.43/11", example2, ANY, slash11Netmask);
239 checkIpAddress("192.0.2.43/11", ANY, example2, slash11Netmask);
240
241 // Invalid netmasks are ignored.
242 checkIpAddress(null, example2, ANY, invalidNetmask);
243
244 // If there is no netmask, implicit netmasks are used.
245 checkIpAddress("192.0.2.43/24", ANY, example2, null);
246 }
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +0900247
248 private void assertDhcpResults(String ipAddress, String gateway, String dnsServersString,
249 String domains, String serverAddress, String vendorInfo, int leaseDuration,
250 boolean hasMeteredHint, DhcpResults dhcpResults) throws Exception {
251 assertEquals(new LinkAddress(ipAddress), dhcpResults.ipAddress);
252 assertEquals(v4Address(gateway), dhcpResults.gateway);
253
254 String[] dnsServerStrings = dnsServersString.split(",");
255 ArrayList dnsServers = new ArrayList();
256 for (String dnsServerString : dnsServerStrings) {
257 dnsServers.add(v4Address(dnsServerString));
258 }
259 assertEquals(dnsServers, dhcpResults.dnsServers);
260
261 assertEquals(domains, dhcpResults.domains);
262 assertEquals(v4Address(serverAddress), dhcpResults.serverAddress);
263 assertEquals(vendorInfo, dhcpResults.vendorInfo);
264 assertEquals(leaseDuration, dhcpResults.leaseDuration);
265 assertEquals(hasMeteredHint, dhcpResults.hasMeteredHint());
266 }
267
268 @SmallTest
269 public void testOffer1() throws Exception {
270 // TODO: Turn all of these into golden files. This will probably require modifying
271 // Android.mk appropriately, making this into an AndroidTestCase, and adding code to read
272 // the golden files from the test APK's assets via mContext.getAssets().
273 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
274 // IP header.
275 "451001480000000080118849c0a89003c0a89ff7" +
276 // UDP header.
277 "004300440134dcfa" +
278 // BOOTP header.
279 "02010600c997a63b0000000000000000c0a89ff70000000000000000" +
280 // MAC address.
281 "30766ff2a90c00000000000000000000" +
282 // Server name.
283 "0000000000000000000000000000000000000000000000000000000000000000" +
284 "0000000000000000000000000000000000000000000000000000000000000000" +
285 // File.
286 "0000000000000000000000000000000000000000000000000000000000000000" +
287 "0000000000000000000000000000000000000000000000000000000000000000" +
288 "0000000000000000000000000000000000000000000000000000000000000000" +
289 "0000000000000000000000000000000000000000000000000000000000000000" +
290 // Options
291 "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" +
292 "3a0400000e103b040000189cff00000000000000000000"
293 ).toCharArray(), false));
294
295 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
296 assertTrue(offerPacket instanceof DhcpOfferPacket); // Implicitly checks it's non-null.
297 DhcpResults dhcpResults = offerPacket.toDhcpResults();
298 assertDhcpResults("192.168.159.247/20", "192.168.159.254", "8.8.8.8,8.8.4.4",
299 null, "192.168.144.3", null, 7200, false, dhcpResults);
300 }
301
302 @SmallTest
303 public void testOffer2() throws Exception {
304 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
305 // IP header.
306 "450001518d0600004011144dc0a82b01c0a82bf7" +
307 // UDP header.
308 "00430044013d9ac7" +
309 // BOOTP header.
310 "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
311 // MAC address.
312 "30766ff2a90c00000000000000000000" +
313 // Server name.
314 "0000000000000000000000000000000000000000000000000000000000000000" +
315 "0000000000000000000000000000000000000000000000000000000000000000" +
316 // File.
317 "0000000000000000000000000000000000000000000000000000000000000000" +
318 "0000000000000000000000000000000000000000000000000000000000000000" +
319 "0000000000000000000000000000000000000000000000000000000000000000" +
320 "0000000000000000000000000000000000000000000000000000000000000000" +
321 // Options
322 "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
323 "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"
324 ).toCharArray(), false));
325
326 assertEquals(337, packet.limit());
327 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
328 assertTrue(offerPacket instanceof DhcpOfferPacket); // Implicitly checks it's non-null.
329 DhcpResults dhcpResults = offerPacket.toDhcpResults();
330 assertDhcpResults("192.168.43.247/24", "192.168.43.1", "192.168.43.1",
331 null, "192.168.43.1", "ANDROID_METERED", 3600, true, dhcpResults);
332 assertTrue(dhcpResults.hasMeteredHint());
333 }
334
335 @SmallTest
Lorenzo Colittid64144a2015-09-03 17:36:20 +0900336 public void testBadHwaddrLength() throws Exception {
337 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
338 // IP header.
339 "450001518d0600004011144dc0a82b01c0a82bf7" +
340 // UDP header.
341 "00430044013d9ac7" +
342 // BOOTP header.
343 "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
344 // MAC address.
345 "30766ff2a90c00000000000000000000" +
346 // Server name.
347 "0000000000000000000000000000000000000000000000000000000000000000" +
348 "0000000000000000000000000000000000000000000000000000000000000000" +
349 // File.
350 "0000000000000000000000000000000000000000000000000000000000000000" +
351 "0000000000000000000000000000000000000000000000000000000000000000" +
352 "0000000000000000000000000000000000000000000000000000000000000000" +
353 "0000000000000000000000000000000000000000000000000000000000000000" +
354 // Options
355 "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
356 "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"
357 ).toCharArray(), false));
358 String expectedClientMac = "30766FF2A90C";
359
360 final int hwAddrLenOffset = 20 + 8 + 2;
361 assertEquals(6, packet.get(hwAddrLenOffset));
362
363 // Expect the expected.
364 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
365 assertNotNull(offerPacket);
366 assertEquals(6, offerPacket.getClientMac().length);
367 assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
368
369 // Reduce the hardware address length and verify that it shortens the client MAC.
370 packet.flip();
371 packet.put(hwAddrLenOffset, (byte) 5);
372 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
373 assertNotNull(offerPacket);
374 assertEquals(5, offerPacket.getClientMac().length);
375 assertEquals(expectedClientMac.substring(0, 10),
376 HexDump.toHexString(offerPacket.getClientMac()));
377
378 packet.flip();
379 packet.put(hwAddrLenOffset, (byte) 3);
380 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
381 assertNotNull(offerPacket);
382 assertEquals(3, offerPacket.getClientMac().length);
383 assertEquals(expectedClientMac.substring(0, 6),
384 HexDump.toHexString(offerPacket.getClientMac()));
385
386 // Set the the hardware address length to 0xff and verify that we a) don't treat it as -1
387 // and crash, and b) hardcode it to 6.
388 packet.flip();
389 packet.put(hwAddrLenOffset, (byte) -1);
390 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
391 assertNotNull(offerPacket);
392 assertEquals(6, offerPacket.getClientMac().length);
393 assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
394
395 // Set the the hardware address length to a positive invalid value (> 16) and verify that we
396 // hardcode it to 6.
397 packet.flip();
398 packet.put(hwAddrLenOffset, (byte) 17);
399 offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
400 assertNotNull(offerPacket);
401 assertEquals(6, offerPacket.getClientMac().length);
402 assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
403 }
404
405 @SmallTest
Lorenzo Colittib0b3d0b2015-07-06 12:29:43 +0900406 public void testPadAndOverloadedOptionsOffer() throws Exception {
407 // A packet observed in the real world that is interesting for two reasons:
408 //
409 // 1. It uses pad bytes, which we previously didn't support correctly.
410 // 2. It uses DHCP option overloading, which we don't currently support (but it doesn't
411 // store any information in the overloaded fields).
412 //
413 // For now, we just check that it parses correctly.
414 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
415 // Ethernet header.
416 "b4cef6000000e80462236e300800" +
417 // IP header.
418 "4500014c00000000ff11741701010101ac119876" +
419 // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
420 "004300440138ae5a" +
421 // BOOTP header.
422 "020106000fa0059f0000000000000000ac1198760000000000000000" +
423 // MAC address.
424 "b4cef600000000000000000000000000" +
425 // Server name.
426 "ff00000000000000000000000000000000000000000000000000000000000000" +
427 "0000000000000000000000000000000000000000000000000000000000000000" +
428 // File.
429 "ff00000000000000000000000000000000000000000000000000000000000000" +
430 "0000000000000000000000000000000000000000000000000000000000000000" +
431 "0000000000000000000000000000000000000000000000000000000000000000" +
432 "0000000000000000000000000000000000000000000000000000000000000000" +
433 // Options
434 "638253633501023604010101010104ffff000033040000a8c03401030304ac1101010604ac110101" +
435 "0000000000000000000000000000000000000000000000ff000000"
436 ).toCharArray(), false));
437
438 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
439 assertTrue(offerPacket instanceof DhcpOfferPacket);
440 DhcpResults dhcpResults = offerPacket.toDhcpResults();
441 assertDhcpResults("172.17.152.118/16", "172.17.1.1", "172.17.1.1",
442 null, "1.1.1.1", null, 43200, false, dhcpResults);
443 }
Lorenzo Colittif28e62b2015-07-27 16:35:22 +0900444
445 @SmallTest
446 public void testBug2111() throws Exception {
447 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
448 // IP header.
449 "4500014c00000000ff119beac3eaf3880a3f5d04" +
450 // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
451 "0043004401387464" +
452 // BOOTP header.
453 "0201060002554812000a0000000000000a3f5d040000000000000000" +
454 // MAC address.
455 "00904c00000000000000000000000000" +
456 // Server name.
457 "0000000000000000000000000000000000000000000000000000000000000000" +
458 "0000000000000000000000000000000000000000000000000000000000000000" +
459 // File.
460 "0000000000000000000000000000000000000000000000000000000000000000" +
461 "0000000000000000000000000000000000000000000000000000000000000000" +
462 "0000000000000000000000000000000000000000000000000000000000000000" +
463 "0000000000000000000000000000000000000000000000000000000000000000" +
464 // Options.
465 "638253633501023604c00002fe33040000bfc60104fffff00003040a3f50010608c0000201c0000202" +
466 "0f0f646f6d61696e3132332e636f2e756b0000000000ff00000000"
467 ).toCharArray(), false));
468
469 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
470 assertTrue(offerPacket instanceof DhcpOfferPacket);
471 DhcpResults dhcpResults = offerPacket.toDhcpResults();
472 assertDhcpResults("10.63.93.4/20", "10.63.80.1", "192.0.2.1,192.0.2.2",
473 "domain123.co.uk", "192.0.2.254", null, 49094, false, dhcpResults);
474 }
475
476 @SmallTest
477 public void testBug2136() throws Exception {
478 final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
479 // Ethernet header.
480 "bcf5ac000000d0c7890000000800" +
481 // IP header.
482 "4500014c00000000ff119beac3eaf3880a3f5d04" +
483 // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
484 "0043004401387574" +
485 // BOOTP header.
486 "0201060163339a3000050000000000000a209ecd0000000000000000" +
487 // MAC address.
488 "bcf5ac00000000000000000000000000" +
489 // Server name.
490 "0000000000000000000000000000000000000000000000000000000000000000" +
491 "0000000000000000000000000000000000000000000000000000000000000000" +
492 // File.
493 "0000000000000000000000000000000000000000000000000000000000000000" +
494 "0000000000000000000000000000000000000000000000000000000000000000" +
495 "0000000000000000000000000000000000000000000000000000000000000000" +
496 "0000000000000000000000000000000000000000000000000000000000000000" +
497 // Options.
498 "6382536335010236040a20ff80330400001c200104fffff00003040a20900106089458413494584135" +
499 "0f0b6c616e63732e61632e756b000000000000000000ff00000000"
500 ).toCharArray(), false));
501
502 DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
503 assertTrue(offerPacket instanceof DhcpOfferPacket);
504 assertEquals("BCF5AC000000", HexDump.toHexString(offerPacket.getClientMac()));
505 DhcpResults dhcpResults = offerPacket.toDhcpResults();
506 assertDhcpResults("10.32.158.205/20", "10.32.144.1", "148.88.65.52,148.88.65.53",
507 "lancs.ac.uk", "10.32.255.128", null, 7200, false, dhcpResults);
508 }
Lorenzo Colittic2abb2b2015-03-31 16:26:57 +0900509}