Add support for long values in StructNlAttr
Test: atest NetworkStaticLibTests:com.android.net.moduletests.util.netlink.StructNlAttrTest Change-Id: Iaaa7f63a2b550f017a8c75f84b9d69bf9e829a8c
This commit is contained in:
parent
0b2d75c09a
commit
d4195824ab
2 changed files with 58 additions and 4 deletions
|
|
@ -21,6 +21,8 @@ import android.net.MacAddress;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import static java.nio.ByteOrder.nativeOrder;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
|
@ -152,12 +154,12 @@ public class StructNlAttr {
|
|||
nla_type = type;
|
||||
setValue(new byte[Short.BYTES]);
|
||||
final ByteBuffer buf = getValueAsByteBuffer();
|
||||
final ByteOrder originalOrder = buf.order();
|
||||
// ByteBuffer returned by getValueAsByteBuffer is always in native byte order.
|
||||
try {
|
||||
buf.order(order);
|
||||
buf.putShort(value);
|
||||
} finally {
|
||||
buf.order(originalOrder);
|
||||
buf.order(nativeOrder());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -169,12 +171,29 @@ public class StructNlAttr {
|
|||
nla_type = type;
|
||||
setValue(new byte[Integer.BYTES]);
|
||||
final ByteBuffer buf = getValueAsByteBuffer();
|
||||
final ByteOrder originalOrder = buf.order();
|
||||
// ByteBuffer returned by getValueAsByteBuffer is always in native byte order.
|
||||
try {
|
||||
buf.order(order);
|
||||
buf.putInt(value);
|
||||
} finally {
|
||||
buf.order(originalOrder);
|
||||
buf.order(nativeOrder());
|
||||
}
|
||||
}
|
||||
|
||||
public StructNlAttr(short type, long value) {
|
||||
this(type, value, ByteOrder.nativeOrder());
|
||||
}
|
||||
|
||||
public StructNlAttr(short type, long value, ByteOrder order) {
|
||||
nla_type = type;
|
||||
setValue(new byte[Long.BYTES]);
|
||||
final ByteBuffer buf = getValueAsByteBuffer();
|
||||
// ByteBuffer returned by getValueAsByteBuffer is always in native byte order.
|
||||
try {
|
||||
buf.order(order);
|
||||
buf.putLong(value);
|
||||
} finally {
|
||||
buf.order(nativeOrder());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -288,6 +307,7 @@ public class StructNlAttr {
|
|||
|
||||
/**
|
||||
* Get attribute value as Integer, or null if malformed (e.g., length is not 4 bytes).
|
||||
* The attribute value is assumed to be in native byte order.
|
||||
*/
|
||||
public Integer getValueAsInteger() {
|
||||
final ByteBuffer byteBuffer = getValueAsByteBuffer();
|
||||
|
|
@ -297,6 +317,18 @@ public class StructNlAttr {
|
|||
return byteBuffer.getInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute value as Long, or null if malformed (e.g., length is not 8 bytes).
|
||||
* The attribute value is assumed to be in native byte order.
|
||||
*/
|
||||
public Long getValueAsLong() {
|
||||
final ByteBuffer byteBuffer = getValueAsByteBuffer();
|
||||
if (byteBuffer == null || byteBuffer.remaining() != Long.BYTES) {
|
||||
return null;
|
||||
}
|
||||
return byteBuffer.getLong();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute value as Int, default value if malformed.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -92,4 +92,26 @@ public class StructNlAttrTest {
|
|||
assertNull(integer3);
|
||||
assertEquals(int3, 0x08 /* default value */);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetValueAsLong() {
|
||||
final Long input = 1234567L;
|
||||
// Not a real netlink attribute, just for testing
|
||||
final StructNlAttr attr = new StructNlAttr(IFA_FLAGS, input);
|
||||
|
||||
final Long output = attr.getValueAsLong();
|
||||
|
||||
assertEquals(input, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetValueAsLong_malformed() {
|
||||
final int input = 1234567;
|
||||
// Not a real netlink attribute, just for testing
|
||||
final StructNlAttr attr = new StructNlAttr(IFA_FLAGS, input);
|
||||
|
||||
final Long output = attr.getValueAsLong();
|
||||
|
||||
assertNull(output);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue