Search in sources :

Example 51 with DhcpPacket

use of android.net.dhcp.DhcpPacket in project android_frameworks_base by DirtyUnicorns.

the class DhcpPacketTest method checkMtu.

private void checkMtu(ByteBuffer packet, int expectedMtu, byte[] mtuBytes) throws Exception {
    if (mtuBytes != null) {
        packet.position(packet.capacity() - mtuBytes.length);
        packet.put(mtuBytes);
        packet.clear();
    }
    DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
    // Implicitly checks it's non-null.
    assertTrue(offerPacket instanceof DhcpOfferPacket);
    DhcpResults dhcpResults = offerPacket.toDhcpResults();
    assertDhcpResults("192.168.159.247/20", "192.168.159.254", "8.8.8.8,8.8.4.4", null, "192.168.144.3", null, 7200, false, expectedMtu, dhcpResults);
}
Also used : DhcpResults(android.net.DhcpResults) DhcpPacket(android.net.dhcp.DhcpPacket)

Example 52 with DhcpPacket

use of android.net.dhcp.DhcpPacket in project android_frameworks_base by DirtyUnicorns.

the class DhcpPacketTest method testBadOfferWithoutACookie.

@SmallTest
public void testBadOfferWithoutACookie() throws Exception {
    final byte[] packet = HexDump.hexStringToByteArray(// IP header.
    "450001518d0600004011144dc0a82b01c0a82bf7" + // UDP header.
    "00430044013d9ac7" + // BOOTP header.
    "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" + // MAC address.
    "30766ff2a90c00000000000000000000" + // Server name.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // File.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000");
    try {
        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, packet.length, ENCAP_L3);
    } catch (DhcpPacket.ParseException expected) {
        assertDhcpErrorCodes(DhcpErrorEvent.PARSING_ERROR, expected.errorCode);
        return;
    }
    fail("Dhcp packet parsing should have failed");
}
Also used : DhcpPacket(android.net.dhcp.DhcpPacket) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 53 with DhcpPacket

use of android.net.dhcp.DhcpPacket in project android_frameworks_base by DirtyUnicorns.

the class DhcpPacketTest method testBadHwaddrLength.

@SmallTest
public void testBadHwaddrLength() throws Exception {
    final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(// IP header.
    "450001518d0600004011144dc0a82b01c0a82bf7" + // UDP header.
    "00430044013d9ac7" + // BOOTP header.
    "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" + // MAC address.
    "30766ff2a90c00000000000000000000" + // Server name.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // File.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // Options
    "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" + "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"));
    String expectedClientMac = "30766FF2A90C";
    final int hwAddrLenOffset = 20 + 8 + 2;
    assertEquals(6, packet.get(hwAddrLenOffset));
    // Expect the expected.
    DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
    assertNotNull(offerPacket);
    assertEquals(6, offerPacket.getClientMac().length);
    assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
    // Reduce the hardware address length and verify that it shortens the client MAC.
    packet.flip();
    packet.put(hwAddrLenOffset, (byte) 5);
    offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
    assertNotNull(offerPacket);
    assertEquals(5, offerPacket.getClientMac().length);
    assertEquals(expectedClientMac.substring(0, 10), HexDump.toHexString(offerPacket.getClientMac()));
    packet.flip();
    packet.put(hwAddrLenOffset, (byte) 3);
    offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
    assertNotNull(offerPacket);
    assertEquals(3, offerPacket.getClientMac().length);
    assertEquals(expectedClientMac.substring(0, 6), HexDump.toHexString(offerPacket.getClientMac()));
    // Set the the hardware address length to 0xff and verify that we a) don't treat it as -1
    // and crash, and b) hardcode it to 6.
    packet.flip();
    packet.put(hwAddrLenOffset, (byte) -1);
    offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
    assertNotNull(offerPacket);
    assertEquals(6, offerPacket.getClientMac().length);
    assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
    // Set the the hardware address length to a positive invalid value (> 16) and verify that we
    // hardcode it to 6.
    packet.flip();
    packet.put(hwAddrLenOffset, (byte) 17);
    offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
    assertNotNull(offerPacket);
    assertEquals(6, offerPacket.getClientMac().length);
    assertEquals(expectedClientMac, HexDump.toHexString(offerPacket.getClientMac()));
}
Also used : ByteBuffer(java.nio.ByteBuffer) DhcpPacket(android.net.dhcp.DhcpPacket) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 54 with DhcpPacket

use of android.net.dhcp.DhcpPacket in project android_frameworks_base by DirtyUnicorns.

the class DhcpPacketTest method testOffer1.

@SmallTest
public void testOffer1() throws Exception {
    // TODO: Turn all of these into golden files. This will probably require modifying
    // Android.mk appropriately, making this into an AndroidTestCase, and adding code to read
    // the golden files from the test APK's assets via mContext.getAssets().
    final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(// IP header.
    "451001480000000080118849c0a89003c0a89ff7" + // UDP header.
    "004300440134dcfa" + // BOOTP header.
    "02010600c997a63b0000000000000000c0a89ff70000000000000000" + // MAC address.
    "30766ff2a90c00000000000000000000" + // Server name.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // File.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // Options
    "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" + "3a0400000e103b040000189cff00000000000000000000"));
    DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
    // Implicitly checks it's non-null.
    assertTrue(offerPacket instanceof DhcpOfferPacket);
    DhcpResults dhcpResults = offerPacket.toDhcpResults();
    assertDhcpResults("192.168.159.247/20", "192.168.159.254", "8.8.8.8,8.8.4.4", null, "192.168.144.3", null, 7200, false, 0, dhcpResults);
}
Also used : DhcpResults(android.net.DhcpResults) ByteBuffer(java.nio.ByteBuffer) DhcpPacket(android.net.dhcp.DhcpPacket) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 55 with DhcpPacket

use of android.net.dhcp.DhcpPacket in project android_frameworks_base by DirtyUnicorns.

the class DhcpPacketTest method testOfferWithBadCookie.

@SmallTest
public void testOfferWithBadCookie() throws Exception {
    final byte[] packet = HexDump.hexStringToByteArray(// IP header.
    "450001518d0600004011144dc0a82b01c0a82bf7" + // UDP header.
    "00430044013d9ac7" + // BOOTP header.
    "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" + // MAC address.
    "30766ff2a90c00000000000000000000" + // Server name.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // File.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // Bad cookie
    "DEADBEEF3501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" + "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff");
    try {
        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, packet.length, ENCAP_L3);
    } catch (DhcpPacket.ParseException expected) {
        assertDhcpErrorCodes(DhcpErrorEvent.DHCP_BAD_MAGIC_COOKIE, expected.errorCode);
        return;
    }
    fail("Dhcp packet parsing should have failed");
}
Also used : DhcpPacket(android.net.dhcp.DhcpPacket) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

DhcpPacket (android.net.dhcp.DhcpPacket)72 SmallTest (android.test.suitebuilder.annotation.SmallTest)52 ByteBuffer (java.nio.ByteBuffer)44 DhcpResults (android.net.DhcpResults)40 LinkAddress (android.net.LinkAddress)4