Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ByteUtilsTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private final byte[] empty = new byte[0];
private final byte[] noSplits = "nosplits".getBytes();
private final byte[] splitAt5 = ("1234" + (char) 0x00 + "56789").getBytes();
Expand Down Expand Up @@ -93,8 +89,7 @@ public void testEscape() {
@Test
public void testIllegalArgument() {
// incomplete bytes would cause an ArrayIndexOutOfBounds in the past
exception.expect(IllegalArgumentException.class);
byte[] errorBytes = {0x01};
ByteUtils.unescape(errorBytes);
assertThrows(IllegalArgumentException.class, () -> ByteUtils.unescape(errorBytes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
Expand All @@ -32,15 +33,10 @@

import org.apache.accumulo.core.conf.AccumuloConfiguration.ScanExecutorConfig;
import org.apache.accumulo.core.spi.scan.SimpleScanDispatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class AccumuloConfigurationTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testGetPropertyByString() {
AccumuloConfiguration c = DefaultConfiguration.getInstance();
Expand Down Expand Up @@ -196,8 +192,7 @@ public void testMutatePrefixMap() {
expected1.put(Property.TABLE_ARBITRARY_PROP_PREFIX.getKey() + "a2", "asg34");
assertEquals(expected1, pm1);

thrown.expect(UnsupportedOperationException.class);
pm1.put("k9", "v3");
assertThrows(UnsupportedOperationException.class, () -> pm1.put("k9", "v3"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
package org.apache.accumulo.core.conf;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.util.Properties;

import org.apache.accumulo.core.client.security.tokens.AuthenticationToken;
import org.apache.accumulo.core.client.security.tokens.PasswordToken;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ClientPropertyTest {

Expand Down Expand Up @@ -59,9 +58,6 @@ public void testAuthentication() {
assertEquals("/path/to/keytab", ClientProperty.AUTH_TOKEN.getValue(props));
}

@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void testTypes() {
Properties props = new Properties();
Expand All @@ -81,7 +77,7 @@ public void testTypes() {
value = ClientProperty.BATCH_WRITER_LATENCY_MAX.getTimeInMillis(props);
assertEquals(1234L, value.longValue());

exception.expect(IllegalStateException.class);
ClientProperty.BATCH_WRITER_LATENCY_MAX.getBytes(props);
assertThrows(IllegalStateException.class,
() -> ClientProperty.BATCH_WRITER_LATENCY_MAX.getBytes(props));
}
}
49 changes: 22 additions & 27 deletions core/src/test/java/org/apache/accumulo/core/crypto/CryptoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -73,9 +73,7 @@
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.google.common.collect.Iterables;

Expand All @@ -93,9 +91,6 @@ public class CryptoTest {
System.getProperty("user.dir") + "/target/CryptoTest-emptykeyfile";
private static Configuration hadoopConf = new Configuration();

@Rule
public ExpectedException exception = ExpectedException.none();

@BeforeClass
public static void setupKeyFiles() throws Exception {
FileSystem fs = FileSystem.getLocal(hadoopConf);
Expand Down Expand Up @@ -279,25 +274,31 @@ public void testMissingConfigProperties() throws ReflectiveOperationException {
AccumuloVFSClassLoader.loadClass(configuredClass, CryptoService.class);
CryptoService cs = clazz.getDeclaredConstructor().newInstance();

exception.expect(NullPointerException.class);
cs.init(aconf.getAllPropertiesWithPrefix(Property.TABLE_PREFIX));
assertEquals(AESCryptoService.class, cs.getClass());
assertThrows(NullPointerException.class,
() -> cs.init(aconf.getAllPropertiesWithPrefix(Property.TABLE_PREFIX)));
}

@SuppressFBWarnings(value = "CIPHER_INTEGRITY", justification = "CBC is being tested")
@Test
public void testAESKeyUtilsGeneratesKey() throws NoSuchAlgorithmException,
NoSuchProviderException, NoSuchPaddingException, InvalidKeyException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
java.security.Key key;
key = AESKeyUtils.generateKey(sr, 16);
Cipher.getInstance("AES/CBC/NoPadding").init(Cipher.ENCRYPT_MODE, key);

key = AESKeyUtils.generateKey(sr, 24);
key = AESKeyUtils.generateKey(sr, 32);
key = AESKeyUtils.generateKey(sr, 11);
// verify valid key sizes (corresponds to 128, 192, and 256 bits)
for (int i : new int[] {16, 24, 32}) {
verifyKeySizeForCBC(sr, i);
}
// verify invalid key sizes
for (int i : new int[] {1, 2, 8, 11, 15, 64, 128}) {
assertThrows(InvalidKeyException.class, () -> verifyKeySizeForCBC(sr, i));
}
}

exception.expect(InvalidKeyException.class);
// this has to be a separate method, for spotbugs, because spotbugs annotation doesn't seem to
// apply to the lambda inline
@SuppressFBWarnings(value = "CIPHER_INTEGRITY", justification = "CBC is being tested")
private void verifyKeySizeForCBC(SecureRandom sr, int sizeInBytes)
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
java.security.Key key = AESKeyUtils.generateKey(sr, sizeInBytes);
Cipher.getInstance("AES/CBC/NoPadding").init(Cipher.ENCRYPT_MODE, key);
}

Expand All @@ -324,9 +325,7 @@ public void testAESKeyUtilsFailUnwrapWithWrongKEK()
java.security.Key wrongKek = new SecretKeySpec(wrongBytes, "AES");

byte[] wrapped = AESKeyUtils.wrapKey(fek, kek);
exception.expect(CryptoException.class);
java.security.Key unwrapped = AESKeyUtils.unwrapKey(wrapped, wrongKek);
fail("creation of " + unwrapped + " should fail");
assertThrows(CryptoException.class, () -> AESKeyUtils.unwrapKey(wrapped, wrongKek));
}

@Test
Expand All @@ -341,17 +340,13 @@ public void testAESKeyUtilsLoadKekFromUri() throws IOException {

@Test
public void testAESKeyUtilsLoadKekFromUriInvalidUri() {
exception.expect(CryptoException.class);
SecretKeySpec fileKey = AESKeyUtils.loadKekFromUri(
System.getProperty("user.dir") + "/target/CryptoTest-testkeyfile-doesnt-exist");
fail("creation of " + fileKey + " should fail");
assertThrows(CryptoException.class, () -> AESKeyUtils.loadKekFromUri(
System.getProperty("user.dir") + "/target/CryptoTest-testkeyfile-doesnt-exist"));
}

@Test
public void testAESKeyUtilsLoadKekFromEmptyFile() {
exception.expect(CryptoException.class);
SecretKeySpec fileKey = AESKeyUtils.loadKekFromUri(emptyKeyPath);
fail("creation of " + fileKey + " should fail");
assertThrows(CryptoException.class, () -> AESKeyUtils.loadKekFromUri(emptyKeyPath));
}

private ArrayList<Key> testData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;

import java.util.Iterator;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class PreAllocatedArrayTest {

@Rule
public ExpectedException exception = ExpectedException.none();

/**
* Test method for {@link org.apache.accumulo.core.util.PreAllocatedArray#PreAllocatedArray(int)}.
*/
Expand All @@ -50,8 +46,7 @@ public void testPreAllocatedArray() {

@Test
public void testPreAllocatedArray_Fail() {
exception.expect(IllegalArgumentException.class);
new PreAllocatedArray<String>(-5);
assertThrows(IllegalArgumentException.class, () -> new PreAllocatedArray<String>(-5));
}

/**
Expand Down Expand Up @@ -89,31 +84,27 @@ public void testSet() {
public void testSetIndexHigh() {
PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
strings.set(2, "in bounds");
exception.expect(IndexOutOfBoundsException.class);
strings.set(3, "out of bounds");
assertThrows(IndexOutOfBoundsException.class, () -> strings.set(3, "out of bounds"));
}

@Test
public void testSetIndexNegative() {
PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
strings.set(0, "in bounds");
exception.expect(IndexOutOfBoundsException.class);
strings.set(-3, "out of bounds");
assertThrows(IndexOutOfBoundsException.class, () -> strings.set(-3, "out of bounds"));
}

@Test
public void testGetIndexHigh() {
PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
strings.get(2);
exception.expect(IndexOutOfBoundsException.class);
strings.get(3);
assertThrows(IndexOutOfBoundsException.class, () -> strings.get(3));
}

@Test
public void testGetIndexNegative() {
PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
strings.get(0);
exception.expect(IndexOutOfBoundsException.class);
strings.get(-3);
assertThrows(IndexOutOfBoundsException.class, () -> strings.get(-3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
Expand All @@ -28,15 +29,10 @@
import java.util.Arrays;

import org.apache.hadoop.io.WritableUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class UnsynchronizedBufferTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testByteBufferConstructor() {
byte[] test = "0123456789".getBytes(UTF_8);
Expand All @@ -57,8 +53,9 @@ public void testByteBufferConstructor() {

buf = new byte[6];
// the byte buffer has the extra byte, but should not be able to read it...
thrown.expect(ArrayIndexOutOfBoundsException.class);
ub.readBytes(buf);
final UnsynchronizedBuffer.Reader ub2 = ub;
final byte[] buf2 = buf;
assertThrows(ArrayIndexOutOfBoundsException.class, () -> ub2.readBytes(buf2));
}

@Test
Expand Down
Loading