Merge "Move byte logics from ApfFilter into BitsUtil"

am: 21b9b3a8ff

Change-Id: I9234b9dcb527de8a67bebab8cebc35e5b96aa986
This commit is contained in:
Hugo Benichi
2017-04-24 18:45:23 +00:00
committed by android-build-merger
4 changed files with 169 additions and 45 deletions

View File

@@ -21,10 +21,16 @@ import android.annotation.Nullable;
import libcore.util.Objects;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.UUID;
public class BitUtils {
/**
* A utility class for handling unsigned integers and unsigned arithmetics, as well as syntactic
* sugar methods for ByteBuffer. Useful for networking and packet manipulations.
* {@hide}
*/
public final class BitUtils {
private BitUtils() {}
public static boolean maskedEquals(long a, long b, long mask) {
@@ -76,4 +82,46 @@ public class BitUtils {
}
return packed;
}
public static int uint8(byte b) {
return b & 0xff;
}
public static int uint16(short s) {
return s & 0xffff;
}
public static long uint32(int i) {
return i & 0xffffffffL;
}
public static int bytesToBEInt(byte[] bytes) {
return (uint8(bytes[0]) << 24)
+ (uint8(bytes[1]) << 16)
+ (uint8(bytes[2]) << 8)
+ (uint8(bytes[3]));
}
public static int bytesToLEInt(byte[] bytes) {
return Integer.reverseBytes(bytesToBEInt(bytes));
}
public static int getUint8(ByteBuffer buffer, int position) {
return uint8(buffer.get(position));
}
public static int getUint16(ByteBuffer buffer, int position) {
return uint16(buffer.getShort(position));
}
public static long getUint32(ByteBuffer buffer, int position) {
return uint32(buffer.getInt(position));
}
public static void put(ByteBuffer buffer, int position, byte[] bytes) {
final int original = buffer.position();
buffer.position(position);
buffer.put(bytes);
buffer.position(original);
}
}

View File

@@ -18,6 +18,14 @@ package android.net.apf;
import static android.system.OsConstants.*;
import static com.android.internal.util.BitUtils.bytesToBEInt;
import static com.android.internal.util.BitUtils.getUint16;
import static com.android.internal.util.BitUtils.getUint32;
import static com.android.internal.util.BitUtils.getUint8;
import static com.android.internal.util.BitUtils.uint16;
import static com.android.internal.util.BitUtils.uint32;
import static com.android.internal.util.BitUtils.uint8;
import android.os.SystemClock;
import android.net.LinkAddress;
import android.net.LinkProperties;
@@ -1157,41 +1165,9 @@ public class ApfFilter {
}
}
private static int uint8(byte b) {
return b & 0xff;
}
private static int uint16(short s) {
return s & 0xffff;
}
private static long uint32(int i) {
return i & 0xffffffffL;
}
private static int getUint8(ByteBuffer buffer, int position) {
return uint8(buffer.get(position));
}
private static int getUint16(ByteBuffer buffer, int position) {
return uint16(buffer.getShort(position));
}
private static long getUint32(ByteBuffer buffer, int position) {
return uint32(buffer.getInt(position));
}
// TODO: move to android.net.NetworkUtils
@VisibleForTesting
public static int ipv4BroadcastAddress(byte[] addrBytes, int prefixLength) {
return bytesToInt(addrBytes) | (int) (uint32(-1) >>> prefixLength);
}
@VisibleForTesting
public static int bytesToInt(byte[] addrBytes) {
return (uint8(addrBytes[0]) << 24)
+ (uint8(addrBytes[1]) << 16)
+ (uint8(addrBytes[2]) << 8)
+ (uint8(addrBytes[3]));
return bytesToBEInt(addrBytes) | (int) (uint32(-1) >>> prefixLength);
}
}

View File

@@ -39,6 +39,8 @@ import static android.system.OsConstants.*;
import com.android.frameworks.tests.net.R;
import com.android.internal.util.HexDump;
import static com.android.internal.util.BitUtils.bytesToBEInt;
import static com.android.internal.util.BitUtils.put;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
@@ -65,7 +67,7 @@ import libcore.io.Streams;
* Tests for APF program generator and interpreter.
*
* Build, install and run with:
* runtest frameworks-services -c android.net.apf.ApfTest
* runtest frameworks-net -c android.net.apf.ApfTest
*/
public class ApfTest extends AndroidTestCase {
private static final int TIMEOUT_MS = 500;
@@ -1235,15 +1237,6 @@ public class ApfTest extends AndroidTestCase {
byte[] apf_program);
@SmallTest
public void testBytesToInt() {
assertEquals(0x00000000, ApfFilter.bytesToInt(IPV4_ANY_HOST_ADDR));
assertEquals(0xffffffff, ApfFilter.bytesToInt(IPV4_BROADCAST_ADDRESS));
assertEquals(0x0a000001, ApfFilter.bytesToInt(MOCK_IPV4_ADDR));
assertEquals(0x0a000002, ApfFilter.bytesToInt(ANOTHER_IPV4_ADDR));
assertEquals(0x0a001fff, ApfFilter.bytesToInt(MOCK_BROADCAST_IPV4_ADDR));
assertEquals(0xe0000001, ApfFilter.bytesToInt(MOCK_MULTICAST_IPV4_ADDR));
}
public void testBroadcastAddress() throws Exception {
assertEqualsIp("255.255.255.255", ApfFilter.ipv4BroadcastAddress(IPV4_ANY_HOST_ADDR, 0));
assertEqualsIp("0.0.0.0", ApfFilter.ipv4BroadcastAddress(IPV4_ANY_HOST_ADDR, 32));
@@ -1257,7 +1250,7 @@ public class ApfTest extends AndroidTestCase {
}
public void assertEqualsIp(String expected, int got) throws Exception {
int want = ApfFilter.bytesToInt(InetAddress.getByName(expected).getAddress());
int want = bytesToBEInt(InetAddress.getByName(expected).getAddress());
assertEquals(want, got);
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.util;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import java.nio.ByteBuffer;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static com.android.internal.util.BitUtils.*;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class BitUtilsTest {
@Test
public void testUnsignedByteWideningConversions() {
byte b0 = 0;
byte b1 = 1;
byte bm1 = -1;
assertEquals(0, uint8(b0));
assertEquals(1, uint8(b1));
assertEquals(127, uint8(Byte.MAX_VALUE));
assertEquals(128, uint8(Byte.MIN_VALUE));
assertEquals(255, uint8(bm1));
assertEquals(255, uint8((byte)255));
}
@Test
public void testUnsignedShortWideningConversions() {
short s0 = 0;
short s1 = 1;
short sm1 = -1;
assertEquals(0, uint16(s0));
assertEquals(1, uint16(s1));
assertEquals(32767, uint16(Short.MAX_VALUE));
assertEquals(32768, uint16(Short.MIN_VALUE));
assertEquals(65535, uint16(sm1));
assertEquals(65535, uint16((short)65535));
}
@Test
public void testUnsignedIntWideningConversions() {
assertEquals(0, uint32(0));
assertEquals(1, uint32(1));
assertEquals(2147483647L, uint32(Integer.MAX_VALUE));
assertEquals(2147483648L, uint32(Integer.MIN_VALUE));
assertEquals(4294967295L, uint32(-1));
assertEquals(4294967295L, uint32((int)4294967295L));
}
@Test
public void testBytesToInt() {
assertEquals(0x00000000, bytesToBEInt(bytes(0, 0, 0, 0)));
assertEquals(0xffffffff, bytesToBEInt(bytes(255, 255, 255, 255)));
assertEquals(0x0a000001, bytesToBEInt(bytes(10, 0, 0, 1)));
assertEquals(0x0a000002, bytesToBEInt(bytes(10, 0, 0, 2)));
assertEquals(0x0a001fff, bytesToBEInt(bytes(10, 0, 31, 255)));
assertEquals(0xe0000001, bytesToBEInt(bytes(224, 0, 0, 1)));
assertEquals(0x00000000, bytesToLEInt(bytes(0, 0, 0, 0)));
assertEquals(0x01020304, bytesToLEInt(bytes(4, 3, 2, 1)));
assertEquals(0xffff0000, bytesToLEInt(bytes(0, 0, 255, 255)));
}
@Test
public void testUnsignedGetters() {
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(0xffff);
assertEquals(0x0, getUint8(b, 0));
assertEquals(0x0, getUint8(b, 1));
assertEquals(0xff, getUint8(b, 2));
assertEquals(0xff, getUint8(b, 3));
assertEquals(0x0, getUint16(b, 0));
assertEquals(0xffff, getUint16(b, 2));
b.rewind();
b.putInt(0xffffffff);
assertEquals(0xffffffffL, getUint32(b, 0));
}
static byte[] bytes(int b1, int b2, int b3, int b4) {
return new byte[] {b(b1), b(b2), b(b3), b(b4)};
}
static byte b(int i) {
return (byte) i;
}
}