Move net unit tests to packages/Connectivity
Move the tests together with packages/Connectivity code, so both can be moved to packages/modules/Connectivity together. Also reorganize unit tests in a unit/ directory, as other tests (integration/, common/ etc.) have been added in tests/net since they were created. This makes the directory structure consistent. Test: atest FrameworksNetTests Bug: 187814163 Merged-In: I254ffd1c08ec058d594b4ea55cbae5505f8497cc Change-Id: I254ffd1c08ec058d594b4ea55cbae5505f8497cc
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2015 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.net;
|
||||
|
||||
import static android.system.OsConstants.AF_INET;
|
||||
import static android.system.OsConstants.AF_INET6;
|
||||
import static android.system.OsConstants.AF_UNIX;
|
||||
import static android.system.OsConstants.EPERM;
|
||||
import static android.system.OsConstants.SOCK_DGRAM;
|
||||
import static android.system.OsConstants.SOCK_STREAM;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.system.ErrnoException;
|
||||
import android.system.Os;
|
||||
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import libcore.io.IoUtils;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@androidx.test.filters.SmallTest
|
||||
public class NetworkUtilsInternalTest {
|
||||
|
||||
private static void expectSocketSuccess(String msg, int domain, int type) {
|
||||
try {
|
||||
IoUtils.closeQuietly(Os.socket(domain, type, 0));
|
||||
} catch (ErrnoException e) {
|
||||
fail(msg + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static void expectSocketPemissionError(String msg, int domain, int type) {
|
||||
try {
|
||||
IoUtils.closeQuietly(Os.socket(domain, type, 0));
|
||||
fail(msg);
|
||||
} catch (ErrnoException e) {
|
||||
assertEquals(msg, e.errno, EPERM);
|
||||
}
|
||||
}
|
||||
|
||||
private static void expectHasNetworking() {
|
||||
expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException",
|
||||
AF_UNIX, SOCK_STREAM);
|
||||
expectSocketSuccess("Creating a AF_INET socket shouldn't have thrown ErrnoException",
|
||||
AF_INET, SOCK_DGRAM);
|
||||
expectSocketSuccess("Creating a AF_INET6 socket shouldn't have thrown ErrnoException",
|
||||
AF_INET6, SOCK_DGRAM);
|
||||
}
|
||||
|
||||
private static void expectNoNetworking() {
|
||||
expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException",
|
||||
AF_UNIX, SOCK_STREAM);
|
||||
expectSocketPemissionError(
|
||||
"Creating a AF_INET socket should have thrown ErrnoException(EPERM)",
|
||||
AF_INET, SOCK_DGRAM);
|
||||
expectSocketPemissionError(
|
||||
"Creating a AF_INET6 socket should have thrown ErrnoException(EPERM)",
|
||||
AF_INET6, SOCK_DGRAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAllowNetworkingForProcess() {
|
||||
expectHasNetworking();
|
||||
NetworkUtilsInternal.setAllowNetworkingForProcess(false);
|
||||
expectNoNetworking();
|
||||
NetworkUtilsInternal.setAllowNetworkingForProcess(true);
|
||||
expectHasNetworking();
|
||||
}
|
||||
}
|
||||
218
tests/unit/java/com/android/internal/net/VpnProfileTest.java
Normal file
218
tests/unit/java/com/android/internal/net/VpnProfileTest.java
Normal file
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.net;
|
||||
|
||||
import static com.android.testutils.ParcelUtils.assertParcelSane;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.net.IpSecAlgorithm;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/** Unit tests for {@link VpnProfile}. */
|
||||
@SmallTest
|
||||
@RunWith(JUnit4.class)
|
||||
public class VpnProfileTest {
|
||||
private static final String DUMMY_PROFILE_KEY = "Test";
|
||||
|
||||
private static final int ENCODED_INDEX_AUTH_PARAMS_INLINE = 23;
|
||||
private static final int ENCODED_INDEX_RESTRICTED_TO_TEST_NETWORKS = 24;
|
||||
|
||||
@Test
|
||||
public void testDefaults() throws Exception {
|
||||
final VpnProfile p = new VpnProfile(DUMMY_PROFILE_KEY);
|
||||
|
||||
assertEquals(DUMMY_PROFILE_KEY, p.key);
|
||||
assertEquals("", p.name);
|
||||
assertEquals(VpnProfile.TYPE_PPTP, p.type);
|
||||
assertEquals("", p.server);
|
||||
assertEquals("", p.username);
|
||||
assertEquals("", p.password);
|
||||
assertEquals("", p.dnsServers);
|
||||
assertEquals("", p.searchDomains);
|
||||
assertEquals("", p.routes);
|
||||
assertTrue(p.mppe);
|
||||
assertEquals("", p.l2tpSecret);
|
||||
assertEquals("", p.ipsecIdentifier);
|
||||
assertEquals("", p.ipsecSecret);
|
||||
assertEquals("", p.ipsecUserCert);
|
||||
assertEquals("", p.ipsecCaCert);
|
||||
assertEquals("", p.ipsecServerCert);
|
||||
assertEquals(null, p.proxy);
|
||||
assertTrue(p.getAllowedAlgorithms() != null && p.getAllowedAlgorithms().isEmpty());
|
||||
assertFalse(p.isBypassable);
|
||||
assertFalse(p.isMetered);
|
||||
assertEquals(1360, p.maxMtu);
|
||||
assertFalse(p.areAuthParamsInline);
|
||||
assertFalse(p.isRestrictedToTestNetworks);
|
||||
}
|
||||
|
||||
private VpnProfile getSampleIkev2Profile(String key) {
|
||||
final VpnProfile p = new VpnProfile(key, true /* isRestrictedToTestNetworks */);
|
||||
|
||||
p.name = "foo";
|
||||
p.type = VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS;
|
||||
p.server = "bar";
|
||||
p.username = "baz";
|
||||
p.password = "qux";
|
||||
p.dnsServers = "8.8.8.8";
|
||||
p.searchDomains = "";
|
||||
p.routes = "0.0.0.0/0";
|
||||
p.mppe = false;
|
||||
p.l2tpSecret = "";
|
||||
p.ipsecIdentifier = "quux";
|
||||
p.ipsecSecret = "quuz";
|
||||
p.ipsecUserCert = "corge";
|
||||
p.ipsecCaCert = "grault";
|
||||
p.ipsecServerCert = "garply";
|
||||
p.proxy = null;
|
||||
p.setAllowedAlgorithms(
|
||||
Arrays.asList(
|
||||
IpSecAlgorithm.AUTH_CRYPT_AES_GCM,
|
||||
IpSecAlgorithm.AUTH_HMAC_SHA512,
|
||||
IpSecAlgorithm.CRYPT_AES_CBC));
|
||||
p.isBypassable = true;
|
||||
p.isMetered = true;
|
||||
p.maxMtu = 1350;
|
||||
p.areAuthParamsInline = true;
|
||||
|
||||
// Not saved, but also not compared.
|
||||
p.saveLogin = true;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
assertEquals(
|
||||
getSampleIkev2Profile(DUMMY_PROFILE_KEY), getSampleIkev2Profile(DUMMY_PROFILE_KEY));
|
||||
|
||||
final VpnProfile modified = getSampleIkev2Profile(DUMMY_PROFILE_KEY);
|
||||
modified.maxMtu--;
|
||||
assertNotEquals(getSampleIkev2Profile(DUMMY_PROFILE_KEY), modified);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelUnparcel() {
|
||||
assertParcelSane(getSampleIkev2Profile(DUMMY_PROFILE_KEY), 23);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInvalidAlgorithmValueDelimiter() {
|
||||
final VpnProfile profile = getSampleIkev2Profile(DUMMY_PROFILE_KEY);
|
||||
|
||||
try {
|
||||
profile.setAllowedAlgorithms(
|
||||
Arrays.asList("test" + VpnProfile.VALUE_DELIMITER + "test"));
|
||||
fail("Expected failure due to value separator in algorithm name");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInvalidAlgorithmListDelimiter() {
|
||||
final VpnProfile profile = getSampleIkev2Profile(DUMMY_PROFILE_KEY);
|
||||
|
||||
try {
|
||||
profile.setAllowedAlgorithms(
|
||||
Arrays.asList("test" + VpnProfile.LIST_DELIMITER + "test"));
|
||||
fail("Expected failure due to value separator in algorithm name");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeDecode() {
|
||||
final VpnProfile profile = getSampleIkev2Profile(DUMMY_PROFILE_KEY);
|
||||
final VpnProfile decoded = VpnProfile.decode(DUMMY_PROFILE_KEY, profile.encode());
|
||||
assertEquals(profile, decoded);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeDecodeTooManyValues() {
|
||||
final VpnProfile profile = getSampleIkev2Profile(DUMMY_PROFILE_KEY);
|
||||
final byte[] tooManyValues =
|
||||
(new String(profile.encode()) + VpnProfile.VALUE_DELIMITER + "invalid").getBytes();
|
||||
|
||||
assertNull(VpnProfile.decode(DUMMY_PROFILE_KEY, tooManyValues));
|
||||
}
|
||||
|
||||
private String getEncodedDecodedIkev2ProfileMissingValues(int... missingIndices) {
|
||||
// Sort to ensure when we remove, we can do it from greatest first.
|
||||
Arrays.sort(missingIndices);
|
||||
|
||||
final String encoded = new String(getSampleIkev2Profile(DUMMY_PROFILE_KEY).encode());
|
||||
final List<String> parts =
|
||||
new ArrayList<>(Arrays.asList(encoded.split(VpnProfile.VALUE_DELIMITER)));
|
||||
|
||||
// Remove from back first to ensure indexing is consistent.
|
||||
for (int i = missingIndices.length - 1; i >= 0; i--) {
|
||||
parts.remove(missingIndices[i]);
|
||||
}
|
||||
|
||||
return String.join(VpnProfile.VALUE_DELIMITER, parts.toArray(new String[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeDecodeInvalidNumberOfValues() {
|
||||
final String tooFewValues =
|
||||
getEncodedDecodedIkev2ProfileMissingValues(
|
||||
ENCODED_INDEX_AUTH_PARAMS_INLINE,
|
||||
ENCODED_INDEX_RESTRICTED_TO_TEST_NETWORKS /* missingIndices */);
|
||||
|
||||
assertNull(VpnProfile.decode(DUMMY_PROFILE_KEY, tooFewValues.getBytes()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeDecodeMissingIsRestrictedToTestNetworks() {
|
||||
final String tooFewValues =
|
||||
getEncodedDecodedIkev2ProfileMissingValues(
|
||||
ENCODED_INDEX_RESTRICTED_TO_TEST_NETWORKS /* missingIndices */);
|
||||
|
||||
// Verify decoding without isRestrictedToTestNetworks defaults to false
|
||||
final VpnProfile decoded = VpnProfile.decode(DUMMY_PROFILE_KEY, tooFewValues.getBytes());
|
||||
assertFalse(decoded.isRestrictedToTestNetworks);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeDecodeLoginsNotSaved() {
|
||||
final VpnProfile profile = getSampleIkev2Profile(DUMMY_PROFILE_KEY);
|
||||
profile.saveLogin = false;
|
||||
|
||||
final VpnProfile decoded = VpnProfile.decode(DUMMY_PROFILE_KEY, profile.encode());
|
||||
assertNotEquals(profile, decoded);
|
||||
|
||||
// Add the username/password back, everything else must be equal.
|
||||
decoded.username = profile.username;
|
||||
decoded.password = profile.password;
|
||||
assertEquals(profile, decoded);
|
||||
}
|
||||
}
|
||||
201
tests/unit/java/com/android/internal/util/BitUtilsTest.java
Normal file
201
tests/unit/java/com/android/internal/util/BitUtilsTest.java
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* 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 static com.android.internal.util.BitUtils.bytesToBEInt;
|
||||
import static com.android.internal.util.BitUtils.bytesToLEInt;
|
||||
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.packBits;
|
||||
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 static com.android.internal.util.BitUtils.unpackBits;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
@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 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
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBitsPacking() {
|
||||
BitPackingTestCase[] testCases = {
|
||||
new BitPackingTestCase(0, ints()),
|
||||
new BitPackingTestCase(1, ints(0)),
|
||||
new BitPackingTestCase(2, ints(1)),
|
||||
new BitPackingTestCase(3, ints(0, 1)),
|
||||
new BitPackingTestCase(4, ints(2)),
|
||||
new BitPackingTestCase(6, ints(1, 2)),
|
||||
new BitPackingTestCase(9, ints(0, 3)),
|
||||
new BitPackingTestCase(~Long.MAX_VALUE, ints(63)),
|
||||
new BitPackingTestCase(~Long.MAX_VALUE + 1, ints(0, 63)),
|
||||
new BitPackingTestCase(~Long.MAX_VALUE + 2, ints(1, 63)),
|
||||
};
|
||||
for (BitPackingTestCase tc : testCases) {
|
||||
int[] got = unpackBits(tc.packedBits);
|
||||
assertTrue(
|
||||
"unpackBits("
|
||||
+ tc.packedBits
|
||||
+ "): expected "
|
||||
+ Arrays.toString(tc.bits)
|
||||
+ " but got "
|
||||
+ Arrays.toString(got),
|
||||
Arrays.equals(tc.bits, got));
|
||||
}
|
||||
for (BitPackingTestCase tc : testCases) {
|
||||
long got = packBits(tc.bits);
|
||||
assertEquals(
|
||||
"packBits("
|
||||
+ Arrays.toString(tc.bits)
|
||||
+ "): expected "
|
||||
+ tc.packedBits
|
||||
+ " but got "
|
||||
+ got,
|
||||
tc.packedBits,
|
||||
got);
|
||||
}
|
||||
|
||||
long[] moreTestCases = {
|
||||
0, 1, -1, 23895, -908235, Long.MAX_VALUE, Long.MIN_VALUE, new Random().nextLong(),
|
||||
};
|
||||
for (long l : moreTestCases) {
|
||||
assertEquals(l, packBits(unpackBits(l)));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static int[] ints(int... array) {
|
||||
return array;
|
||||
}
|
||||
|
||||
static class BitPackingTestCase {
|
||||
final int[] bits;
|
||||
final long packedBits;
|
||||
|
||||
BitPackingTestCase(long packedBits, int[] bits) {
|
||||
this.bits = bits;
|
||||
this.packedBits = packedBits;
|
||||
}
|
||||
}
|
||||
}
|
||||
178
tests/unit/java/com/android/internal/util/RingBufferTest.java
Normal file
178
tests/unit/java/com/android/internal/util/RingBufferTest.java
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* 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 static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@SmallTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class RingBufferTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyRingBuffer() {
|
||||
RingBuffer<String> buffer = new RingBuffer<>(String.class, 100);
|
||||
|
||||
assertArrayEquals(new String[0], buffer.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncorrectConstructorArguments() {
|
||||
try {
|
||||
RingBuffer<String> buffer = new RingBuffer<>(String.class, -10);
|
||||
fail("Should not be able to create a negative capacity RingBuffer");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
|
||||
try {
|
||||
RingBuffer<String> buffer = new RingBuffer<>(String.class, 0);
|
||||
fail("Should not be able to create a 0 capacity RingBuffer");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRingBufferWithNoWrapping() {
|
||||
RingBuffer<String> buffer = new RingBuffer<>(String.class, 100);
|
||||
|
||||
buffer.append("a");
|
||||
buffer.append("b");
|
||||
buffer.append("c");
|
||||
buffer.append("d");
|
||||
buffer.append("e");
|
||||
|
||||
String[] expected = {"a", "b", "c", "d", "e"};
|
||||
assertArrayEquals(expected, buffer.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRingBufferWithCapacity1() {
|
||||
RingBuffer<String> buffer = new RingBuffer<>(String.class, 1);
|
||||
|
||||
buffer.append("a");
|
||||
assertArrayEquals(new String[]{"a"}, buffer.toArray());
|
||||
|
||||
buffer.append("b");
|
||||
assertArrayEquals(new String[]{"b"}, buffer.toArray());
|
||||
|
||||
buffer.append("c");
|
||||
assertArrayEquals(new String[]{"c"}, buffer.toArray());
|
||||
|
||||
buffer.append("d");
|
||||
assertArrayEquals(new String[]{"d"}, buffer.toArray());
|
||||
|
||||
buffer.append("e");
|
||||
assertArrayEquals(new String[]{"e"}, buffer.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRingBufferWithWrapping() {
|
||||
int capacity = 100;
|
||||
RingBuffer<String> buffer = new RingBuffer<>(String.class, capacity);
|
||||
|
||||
buffer.append("a");
|
||||
buffer.append("b");
|
||||
buffer.append("c");
|
||||
buffer.append("d");
|
||||
buffer.append("e");
|
||||
|
||||
String[] expected1 = {"a", "b", "c", "d", "e"};
|
||||
assertArrayEquals(expected1, buffer.toArray());
|
||||
|
||||
String[] expected2 = new String[capacity];
|
||||
int firstIndex = 0;
|
||||
int lastIndex = capacity - 1;
|
||||
|
||||
expected2[firstIndex] = "e";
|
||||
for (int i = 1; i < capacity; i++) {
|
||||
buffer.append("x");
|
||||
expected2[i] = "x";
|
||||
}
|
||||
assertArrayEquals(expected2, buffer.toArray());
|
||||
|
||||
buffer.append("x");
|
||||
expected2[firstIndex] = "x";
|
||||
assertArrayEquals(expected2, buffer.toArray());
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (String s : expected2) {
|
||||
buffer.append(s);
|
||||
}
|
||||
}
|
||||
assertArrayEquals(expected2, buffer.toArray());
|
||||
|
||||
buffer.append("a");
|
||||
expected2[lastIndex] = "a";
|
||||
assertArrayEquals(expected2, buffer.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSlot() {
|
||||
int capacity = 100;
|
||||
RingBuffer<DummyClass1> buffer = new RingBuffer<>(DummyClass1.class, capacity);
|
||||
|
||||
final DummyClass1[] actual = new DummyClass1[capacity];
|
||||
final DummyClass1[] expected = new DummyClass1[capacity];
|
||||
for (int i = 0; i < capacity; ++i) {
|
||||
final DummyClass1 obj = buffer.getNextSlot();
|
||||
obj.x = capacity * i;
|
||||
actual[i] = obj;
|
||||
expected[i] = new DummyClass1();
|
||||
expected[i].x = capacity * i;
|
||||
}
|
||||
assertArrayEquals(expected, buffer.toArray());
|
||||
|
||||
for (int i = 0; i < capacity; ++i) {
|
||||
if (actual[i] != buffer.getNextSlot()) {
|
||||
fail("getNextSlot() should re-use objects if available");
|
||||
}
|
||||
}
|
||||
|
||||
RingBuffer<DummyClass2> buffer2 = new RingBuffer<>(DummyClass2.class, capacity);
|
||||
assertNull("getNextSlot() should return null if the object can't be initiated "
|
||||
+ "(No nullary constructor)", buffer2.getNextSlot());
|
||||
|
||||
RingBuffer<DummyClass3> buffer3 = new RingBuffer<>(DummyClass3.class, capacity);
|
||||
assertNull("getNextSlot() should return null if the object can't be initiated "
|
||||
+ "(Inaccessible class)", buffer3.getNextSlot());
|
||||
}
|
||||
|
||||
public static final class DummyClass1 {
|
||||
int x;
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof DummyClass1) {
|
||||
final DummyClass1 other = (DummyClass1) o;
|
||||
return other.x == this.x;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class DummyClass2 {
|
||||
public DummyClass2(int x) {}
|
||||
}
|
||||
|
||||
private static final class DummyClass3 {}
|
||||
}
|
||||
Reference in New Issue
Block a user