Merge "Add some useful helpers and constants." am: ed8d236319 am: 0f4421873e
am: 06d401ae73
Change-Id: Iaf611b8e39080d677c9cfe7edb124c11bbdf8a81
This commit is contained in:
@@ -93,6 +93,10 @@ public final class BitUtils {
|
|||||||
return s & 0xffff;
|
return s & 0xffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int uint16(byte hi, byte lo) {
|
||||||
|
return ((hi & 0xff) << 8) | (lo & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
public static long uint32(int i) {
|
public static long uint32(int i) {
|
||||||
return i & 0xffffffffL;
|
return i & 0xffffffffL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,6 +106,20 @@ public final class NetworkConstants {
|
|||||||
public static final int RFC7421_PREFIX_LENGTH = 64;
|
public static final int RFC7421_PREFIX_LENGTH = 64;
|
||||||
public static final int RFC6177_MIN_PREFIX_LENGTH = 48;
|
public static final int RFC6177_MIN_PREFIX_LENGTH = 48;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ICMP common (v4/v6) constants.
|
||||||
|
*
|
||||||
|
* See also:
|
||||||
|
* - https://tools.ietf.org/html/rfc792
|
||||||
|
* - https://tools.ietf.org/html/rfc4443
|
||||||
|
*/
|
||||||
|
public static final int ICMP_HEADER_TYPE_OFFSET = 0;
|
||||||
|
public static final int ICMP_HEADER_CODE_OFFSET = 1;
|
||||||
|
public static final int ICMP_HEADER_CHECKSUM_OFFSET = 2;
|
||||||
|
public static final int ICMP_ECHO_IDENTIFIER_OFFSET = 4;
|
||||||
|
public static final int ICMP_ECHO_SEQUENCE_NUMBER_OFFSET = 6;
|
||||||
|
public static final int ICMP_ECHO_DATA_OFFSET = 8;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ICMPv6 constants.
|
* ICMPv6 constants.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -55,6 +55,25 @@ public class BitUtilsTest {
|
|||||||
assertEquals(65535, uint16((short)65535));
|
assertEquals(65535, uint16((short)65535));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnsignedShortComposition() {
|
||||||
|
byte b0 = 0;
|
||||||
|
byte b1 = 1;
|
||||||
|
byte b2 = 2;
|
||||||
|
byte b10 = 10;
|
||||||
|
byte b16 = 16;
|
||||||
|
byte b128 = -128;
|
||||||
|
byte b224 = -32;
|
||||||
|
byte b255 = -1;
|
||||||
|
assertEquals(0x0000, uint16(b0, b0));
|
||||||
|
assertEquals(0xffff, uint16(b255, b255));
|
||||||
|
assertEquals(0x0a01, uint16(b10, b1));
|
||||||
|
assertEquals(0x8002, uint16(b128, b2));
|
||||||
|
assertEquals(0x01ff, uint16(b1, b255));
|
||||||
|
assertEquals(0x80ff, uint16(b128, b255));
|
||||||
|
assertEquals(0xe010, uint16(b224, b16));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnsignedIntWideningConversions() {
|
public void testUnsignedIntWideningConversions() {
|
||||||
assertEquals(0, uint32(0));
|
assertEquals(0, uint32(0));
|
||||||
|
|||||||
Reference in New Issue
Block a user