Merge branch 'tests-cleanup' into 'master'

Clean up tests

* Broke up ConstantsTest (#280) - the key encoding parts are now in KeyEncodingAndParsingTest, the message encoding parts are in MessageSizeIntegrationTest
* Renamed the other integration tests in briar-android-tests
* Moved the integration tests in briar-android-tests to the top-level package, as they all involve code from multiple packages
* Separated DatabaseExecutorModule from DatabaseModule so we can use a different @DatabaseExecutor in integration tests
* Merged AppModule with AndroidModule (@ernir, this touches code you're working on but I don't think there are any conflicts)
* Renamed some TestUtils methods for consistency
* Used TestUtils.getRandomBytes() where applicable

Fixes #280.

See merge request !133
This commit is contained in:
akwizgran
2016-04-06 16:03:48 +00:00
42 changed files with 532 additions and 581 deletions

View File

@@ -1,27 +1,28 @@
package org.briarproject;
import org.briarproject.api.db.DatabaseConfig;
import org.briarproject.api.db.DatabaseExecutor;
import org.briarproject.db.DatabaseModule;
import java.io.File;
import java.util.concurrent.Executor;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class TestDatabaseModule {
public class TestDatabaseModule extends DatabaseModule {
private final DatabaseConfig config;
public TestDatabaseModule() {
this(new File("."), Long.MAX_VALUE);
this(new File("."));
}
public TestDatabaseModule(File dir) {
this(dir, Long.MAX_VALUE);
}
public TestDatabaseModule(File dir, long maxSize) {
this.config = new TestDatabaseConfig(dir, maxSize);
config = new TestDatabaseConfig(dir, Long.MAX_VALUE);
}
@Provides
@@ -29,4 +30,10 @@ public class TestDatabaseModule {
return config;
}
@Provides
@Singleton
@DatabaseExecutor
Executor provideDatabaseExecutor() {
return new ImmediateExecutor();
}
}

View File

@@ -2,15 +2,9 @@ package org.briarproject;
import org.briarproject.api.system.SeedProvider;
import java.util.Random;
public class TestSeedProvider implements SeedProvider {
private final Random random = new Random();
public byte[] getSeed() {
byte[] seed = new byte[32];
random.nextBytes(seed);
return seed;
return TestUtils.getRandomBytes(32);
}
}

View File

@@ -1,15 +1,13 @@
package org.briarproject;
import org.briarproject.api.UniqueId;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.util.FileUtils;
import java.io.File;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import org.briarproject.api.UniqueId;
import org.briarproject.api.crypto.SecretKey;
public class TestUtils {
private static final AtomicInteger nextTestDir =
@@ -26,28 +24,24 @@ public class TestUtils {
testDir.getParentFile().delete(); // Delete if empty
}
public static byte[] getRandomId() {
byte[] b = new byte[UniqueId.LENGTH];
random.nextBytes(b);
return b;
}
public static byte[] getRandomBytes(int length) {
byte[] b = new byte[length];
random.nextBytes(b);
return b;
}
public static String createRandomString(int length) {
public static byte[] getRandomId() {
return getRandomBytes(UniqueId.LENGTH);
}
public static String getRandomString(int length) {
char[] c = new char[length];
for (int i = 0; i < length; i++)
c[i] = (char) ('a' + random.nextInt(26));
return new String(c);
}
public static SecretKey createSecretKey() {
byte[] b = new byte[SecretKey.LENGTH];
random.nextBytes(b);
return new SecretKey(b);
public static SecretKey getSecretKey() {
return new SecretKey(getRandomBytes(SecretKey.LENGTH));
}
}

View File

@@ -13,7 +13,7 @@ import static org.junit.Assert.assertArrayEquals;
public class KeyAgreementTest extends BriarTestCase {
@Test
public void testBTKeyAgreement() throws Exception {
public void testDeriveMasterSecret() throws Exception {
SeedProvider seedProvider = new TestSeedProvider();
CryptoComponent crypto = new CryptoComponentImpl(seedProvider);
KeyPair aPair = crypto.generateAgreementKeyPair();
@@ -26,7 +26,7 @@ public class KeyAgreementTest extends BriarTestCase {
}
@Test
public void testKeyAgreement() throws Exception {
public void testDeriveSharedSecret() throws Exception {
SeedProvider seedProvider = new TestSeedProvider();
CryptoComponent crypto = new CryptoComponentImpl(seedProvider);
KeyPair aPair = crypto.generateAgreementKeyPair();

View File

@@ -24,7 +24,7 @@ public class KeyDerivationTest extends BriarTestCase {
public KeyDerivationTest() {
crypto = new CryptoComponentImpl(new TestSeedProvider());
master = TestUtils.createSecretKey();
master = TestUtils.getSecretKey();
}
@Test
@@ -120,7 +120,7 @@ public class KeyDerivationTest extends BriarTestCase {
@Test
public void testMasterKeyAffectsOutput() {
SecretKey master1 = TestUtils.createSecretKey();
SecretKey master1 = TestUtils.getSecretKey();
assertFalse(Arrays.equals(master.getBytes(), master1.getBytes()));
TransportKeys k = crypto.deriveTransportKeys(transportId, master,
123, true);

View File

@@ -2,23 +2,37 @@ package org.briarproject.crypto;
import org.briarproject.BriarTestCase;
import org.briarproject.TestSeedProvider;
import org.briarproject.TestUtils;
import org.briarproject.api.crypto.KeyPair;
import org.briarproject.api.crypto.KeyParser;
import org.briarproject.api.crypto.PrivateKey;
import org.briarproject.api.crypto.PublicKey;
import org.briarproject.api.crypto.Signature;
import org.junit.Test;
import java.security.GeneralSecurityException;
import java.util.Random;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
public class KeyEncodingAndParsingTest extends BriarTestCase {
private final CryptoComponentImpl crypto =
new CryptoComponentImpl(new TestSeedProvider());
@Test
public void testAgreementPublicKeyLength() throws Exception {
// Generate 10 agreement key pairs
for (int i = 0; i < 10; i++) {
KeyPair keyPair = crypto.generateSignatureKeyPair();
// Check the length of the public key
byte[] publicKey = keyPair.getPublic().getEncoded();
assertTrue(publicKey.length <= MAX_PUBLIC_KEY_LENGTH);
}
}
@Test
public void testAgreementPublicKeyEncodingAndParsing() throws Exception {
KeyParser parser = crypto.getAgreementKeyParser();
@@ -61,27 +75,46 @@ public class KeyEncodingAndParsingTest extends BriarTestCase {
int pubLength = p.getPublic().getEncoded().length;
int privLength = p.getPrivate().getEncoded().length;
// Parse some random byte arrays - expect GeneralSecurityException
Random random = new Random();
byte[] pubFuzz = new byte[pubLength];
byte[] privFuzz = new byte[privLength];
for (int i = 0; i < 1000; i++) {
random.nextBytes(pubFuzz);
try {
parser.parsePublicKey(pubFuzz);
parser.parsePublicKey(TestUtils.getRandomBytes(pubLength));
} catch (GeneralSecurityException expected) {
} catch (Exception e) {
fail();
// Expected
}
random.nextBytes(privFuzz);
try {
parser.parsePrivateKey(privFuzz);
parser.parsePrivateKey(TestUtils.getRandomBytes(privLength));
} catch (GeneralSecurityException expected) {
} catch (Exception e) {
fail();
// Expected
}
}
}
@Test
public void testSignaturePublicKeyLength() throws Exception {
// Generate 10 signature key pairs
for (int i = 0; i < 10; i++) {
KeyPair keyPair = crypto.generateSignatureKeyPair();
// Check the length of the public key
byte[] publicKey = keyPair.getPublic().getEncoded();
assertTrue(publicKey.length <= MAX_PUBLIC_KEY_LENGTH);
}
}
@Test
public void testSignatureLength() throws Exception {
Signature sig = crypto.getSignature();
// Generate 10 signature key pairs
for (int i = 0; i < 10; i++) {
KeyPair keyPair = crypto.generateSignatureKeyPair();
// Sign some random data and check the length of the signature
byte[] toBeSigned = TestUtils.getRandomBytes(1234);
sig.initSign(keyPair.getPrivate());
sig.update(toBeSigned);
byte[] signature = sig.sign();
assertTrue(signature.length <= MAX_SIGNATURE_LENGTH);
}
}
@Test
public void testSignaturePublicKeyEncodingAndParsing() throws Exception {
KeyParser parser = crypto.getSignatureKeyParser();
@@ -124,23 +157,16 @@ public class KeyEncodingAndParsingTest extends BriarTestCase {
int pubLength = p.getPublic().getEncoded().length;
int privLength = p.getPrivate().getEncoded().length;
// Parse some random byte arrays - expect GeneralSecurityException
Random random = new Random();
byte[] pubFuzz = new byte[pubLength];
byte[] privFuzz = new byte[privLength];
for (int i = 0; i < 1000; i++) {
random.nextBytes(pubFuzz);
try {
parser.parsePublicKey(pubFuzz);
parser.parsePublicKey(TestUtils.getRandomBytes(pubLength));
} catch (GeneralSecurityException expected) {
} catch (Exception e) {
fail();
// Expected
}
random.nextBytes(privFuzz);
try {
parser.parsePrivateKey(privFuzz);
parser.parsePrivateKey(TestUtils.getRandomBytes(privLength));
} catch (GeneralSecurityException expected) {
} catch (Exception e) {
fail();
// Expected
}
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.crypto;
import org.briarproject.BriarTestCase;
import org.briarproject.TestSeedProvider;
import org.briarproject.TestUtils;
import org.junit.Test;
import java.util.Random;
@@ -18,9 +19,7 @@ public class PasswordBasedKdfTest extends BriarTestCase {
@Test
public void testEncryptionAndDecryption() {
Random random = new Random();
byte[] input = new byte[1234];
random.nextBytes(input);
byte[] input = TestUtils.getRandomBytes(1234);
String password = "password";
byte[] ciphertext = crypto.encryptWithPassword(input, password);
byte[] output = crypto.decryptWithPassword(ciphertext, password);
@@ -29,13 +28,11 @@ public class PasswordBasedKdfTest extends BriarTestCase {
@Test
public void testInvalidCiphertextReturnsNull() {
Random random = new Random();
byte[] input = new byte[1234];
random.nextBytes(input);
byte[] input = TestUtils.getRandomBytes(1234);
String password = "password";
byte[] ciphertext = crypto.encryptWithPassword(input, password);
// Modify the ciphertext
int position = random.nextInt(ciphertext.length);
int position = new Random().nextInt(ciphertext.length);
ciphertext[position] = (byte) (ciphertext[position] ^ 0xFF);
byte[] output = crypto.decryptWithPassword(ciphertext, password);
assertNull(output);

View File

@@ -9,7 +9,6 @@ import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import static junit.framework.Assert.assertEquals;
import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
@@ -18,22 +17,18 @@ import static org.briarproject.api.transport.TransportConstants.MAX_PAYLOAD_LENG
import static org.briarproject.api.transport.TransportConstants.STREAM_HEADER_IV_LENGTH;
import static org.briarproject.util.ByteUtils.INT_16_BYTES;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
public class StreamDecrypterImplTest extends BriarTestCase {
private final AuthenticatedCipher cipher;
private final SecretKey streamHeaderKey, frameKey;
private final byte[] streamHeaderIv;
private final Random random;
public StreamDecrypterImplTest() {
cipher = new TestAuthenticatedCipher(); // Null cipher
streamHeaderKey = TestUtils.createSecretKey();
frameKey = TestUtils.createSecretKey();
streamHeaderIv = new byte[STREAM_HEADER_IV_LENGTH];
random = new Random();
random.nextBytes(streamHeaderIv);
streamHeaderKey = TestUtils.getSecretKey();
frameKey = TestUtils.getSecretKey();
streamHeaderIv = TestUtils.getRandomBytes(STREAM_HEADER_IV_LENGTH);
}
@Test
@@ -42,15 +37,13 @@ public class StreamDecrypterImplTest extends BriarTestCase {
int payloadLength = 123, paddingLength = 234;
FrameEncoder.encodeHeader(frameHeader, false, payloadLength,
paddingLength);
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
byte[] frameHeader1 = new byte[FRAME_HEADER_LENGTH];
int payloadLength1 = 345, paddingLength1 = 456;
FrameEncoder.encodeHeader(frameHeader1, true, payloadLength1,
paddingLength1);
byte[] payload1 = new byte[payloadLength1];
random.nextBytes(payload1);
byte[] payload1 = TestUtils.getRandomBytes(payloadLength1);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(streamHeaderIv);
@@ -88,8 +81,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
int payloadLength = 123, paddingLength = 234;
FrameEncoder.encodeHeader(frameHeader, false, payloadLength,
paddingLength);
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(streamHeaderIv);
out.write(frameKey.getBytes());
@@ -116,8 +108,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
int payloadLength = MAX_PAYLOAD_LENGTH - 1, paddingLength = 2;
ByteUtils.writeUint16(payloadLength, frameHeader, 0);
ByteUtils.writeUint16(paddingLength, frameHeader, INT_16_BYTES);
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(streamHeaderIv);
@@ -143,8 +134,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
int payloadLength = 123, paddingLength = 234;
FrameEncoder.encodeHeader(frameHeader, false, payloadLength,
paddingLength);
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
// Set one of the padding bytes non-zero
byte[] padding = new byte[paddingLength];
padding[paddingLength - 1] = 1;
@@ -173,8 +163,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
int payloadLength = 123, paddingLength = 234;
FrameEncoder.encodeHeader(frameHeader, true, payloadLength,
paddingLength);
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(streamHeaderIv);

View File

@@ -6,7 +6,6 @@ import org.briarproject.api.crypto.SecretKey;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
@@ -19,17 +18,13 @@ public class StreamEncrypterImplTest extends BriarTestCase {
private final AuthenticatedCipher cipher;
private final SecretKey streamHeaderKey, frameKey;
private final byte[] tag, streamHeaderIv;
private final Random random;
public StreamEncrypterImplTest() {
cipher = new TestAuthenticatedCipher(); // Null cipher
streamHeaderKey = TestUtils.createSecretKey();
frameKey = TestUtils.createSecretKey();
tag = new byte[TAG_LENGTH];
streamHeaderIv = new byte[STREAM_HEADER_IV_LENGTH];
random = new Random();
random.nextBytes(tag);
random.nextBytes(streamHeaderIv);
streamHeaderKey = TestUtils.getSecretKey();
frameKey = TestUtils.getSecretKey();
tag = TestUtils.getRandomBytes(TAG_LENGTH);
streamHeaderIv = TestUtils.getRandomBytes(STREAM_HEADER_IV_LENGTH);
}
@Test
@@ -38,8 +33,7 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
s.writeFrame(payload, payloadLength, 0, false);
@@ -64,8 +58,7 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
s.writeFrame(payload, payloadLength, 0, true);
@@ -90,8 +83,7 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
s.writeFrame(payload, payloadLength, 0, false);
@@ -115,8 +107,7 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
s.writeFrame(payload, payloadLength, 0, true);
@@ -140,8 +131,7 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123, paddingLength = 234;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
s.writeFrame(payload, payloadLength, paddingLength, false);
@@ -168,8 +158,7 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123, paddingLength = 234;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
s.writeFrame(payload, payloadLength, paddingLength, true);
@@ -196,8 +185,7 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123, paddingLength = 234;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
s.writeFrame(payload, payloadLength, paddingLength, false);
@@ -223,8 +211,7 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123, paddingLength = 234;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
s.writeFrame(payload, payloadLength, paddingLength, true);
@@ -250,11 +237,9 @@ public class StreamEncrypterImplTest extends BriarTestCase {
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
streamHeaderIv, streamHeaderKey, frameKey);
int payloadLength = 123, paddingLength = 234;
byte[] payload = new byte[payloadLength];
random.nextBytes(payload);
byte[] payload = TestUtils.getRandomBytes(payloadLength);
int payloadLength1 = 345, paddingLength1 = 456;
byte[] payload1 = new byte[payloadLength1];
random.nextBytes(payload1);
byte[] payload1 = TestUtils.getRandomBytes(payloadLength1);
s.writeFrame(payload, payloadLength, paddingLength, false);
s.writeFrame(payload1, payloadLength1, paddingLength1, true);

View File

@@ -158,7 +158,7 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test
public void testReadString8() throws Exception {
String longest = TestUtils.createRandomString(Byte.MAX_VALUE);
String longest = TestUtils.getRandomString(Byte.MAX_VALUE);
String longHex = StringUtils.toHexString(longest.getBytes("UTF-8"));
// "foo", the empty string, and 127 random letters
setContents("41" + "03" + "666F6F" + "41" + "00" +
@@ -180,7 +180,7 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test
public void testSkipString8() throws Exception {
String longest = TestUtils.createRandomString(Byte.MAX_VALUE);
String longest = TestUtils.getRandomString(Byte.MAX_VALUE);
String longHex = StringUtils.toHexString(longest.getBytes("UTF-8"));
// "foo", the empty string, and 127 random letters
setContents("41" + "03" + "666F6F" + "41" + "00" +
@@ -193,9 +193,9 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test
public void testReadString16() throws Exception {
String shortest = TestUtils.createRandomString(Byte.MAX_VALUE + 1);
String shortest = TestUtils.getRandomString(Byte.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
String longest = TestUtils.createRandomString(Short.MAX_VALUE);
String longest = TestUtils.getRandomString(Short.MAX_VALUE);
String longHex = StringUtils.toHexString(longest.getBytes("UTF-8"));
// 128 random letters and 2^15 -1 random letters
setContents("42" + "0080" + shortHex + "42" + "7FFF" + longHex);
@@ -206,7 +206,7 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test(expected = FormatException.class)
public void testReadString16ChecksMaxLength() throws Exception {
String shortest = TestUtils.createRandomString(Byte.MAX_VALUE + 1);
String shortest = TestUtils.getRandomString(Byte.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
// 128 random letters, twice
setContents("42" + "0080" + shortHex + "42" + "0080" + shortHex);
@@ -217,9 +217,9 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test
public void testSkipString16() throws Exception {
String shortest = TestUtils.createRandomString(Byte.MAX_VALUE + 1);
String shortest = TestUtils.getRandomString(Byte.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
String longest = TestUtils.createRandomString(Short.MAX_VALUE);
String longest = TestUtils.getRandomString(Short.MAX_VALUE);
String longHex = StringUtils.toHexString(longest.getBytes("UTF-8"));
// 128 random letters and 2^15 - 1 random letters
setContents("42" + "0080" + shortHex + "42" + "7FFF" + longHex);
@@ -230,7 +230,7 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test
public void testReadString32() throws Exception {
String shortest = TestUtils.createRandomString(Short.MAX_VALUE + 1);
String shortest = TestUtils.getRandomString(Short.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
// 2^15 random letters
setContents("44" + "00008000" + shortHex);
@@ -240,7 +240,7 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test(expected = FormatException.class)
public void testReadString32ChecksMaxLength() throws Exception {
String shortest = TestUtils.createRandomString(Short.MAX_VALUE + 1);
String shortest = TestUtils.getRandomString(Short.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
// 2^15 random letters, twice
setContents("44" + "00008000" + shortHex +
@@ -252,7 +252,7 @@ public class BdfReaderImplTest extends BriarTestCase {
@Test
public void testSkipString32() throws Exception {
String shortest = TestUtils.createRandomString(Short.MAX_VALUE + 1);
String shortest = TestUtils.getRandomString(Short.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
// 2^15 random letters, twice
setContents("44" + "00008000" + shortHex +

View File

@@ -81,7 +81,7 @@ public class BdfWriterImplTest extends BriarTestCase {
@Test
public void testWriteString8() throws IOException {
String longest = TestUtils.createRandomString(Byte.MAX_VALUE);
String longest = TestUtils.getRandomString(Byte.MAX_VALUE);
String longHex = StringUtils.toHexString(longest.getBytes("UTF-8"));
w.writeString("foo bar baz bam ");
w.writeString(longest);
@@ -93,9 +93,9 @@ public class BdfWriterImplTest extends BriarTestCase {
@Test
public void testWriteString16() throws IOException {
String shortest = TestUtils.createRandomString(Byte.MAX_VALUE + 1);
String shortest = TestUtils.getRandomString(Byte.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
String longest = TestUtils.createRandomString(Short.MAX_VALUE);
String longest = TestUtils.getRandomString(Short.MAX_VALUE);
String longHex = StringUtils.toHexString(longest.getBytes("UTF-8"));
w.writeString(shortest);
w.writeString(longest);
@@ -106,7 +106,7 @@ public class BdfWriterImplTest extends BriarTestCase {
@Test
public void testWriteString32() throws IOException {
String shortest = TestUtils.createRandomString(Short.MAX_VALUE + 1);
String shortest = TestUtils.getRandomString(Short.MAX_VALUE + 1);
String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8"));
w.writeString(shortest);
// STRING_32 tag, length 2^15, UTF-8 bytes

View File

@@ -15,7 +15,7 @@ import java.util.Map;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class MetadataEncoderParserImplTest extends BriarTestCase {
public class MetadataEncoderParserIntegrationTest extends BriarTestCase {
MetadataEncoderImpl e;
MetadataParserImpl p;

View File

@@ -15,7 +15,6 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static java.sql.Types.BINARY;
import static org.junit.Assert.assertEquals;
@@ -48,10 +47,9 @@ public class BasicH2Test extends BriarTestCase {
// Create the table
createTable(connection);
// Generate an ID and two names
byte[] id = new byte[32];
new Random().nextBytes(id);
String oldName = TestUtils.createRandomString(50);
String newName = TestUtils.createRandomString(50);
byte[] id = TestUtils.getRandomId();
String oldName = TestUtils.getRandomString(50);
String newName = TestUtils.getRandomString(50);
// Insert the ID and old name into the table
insertRow(id, oldName);
// Check that the old name can be retrieved using the ID
@@ -75,14 +73,13 @@ public class BasicH2Test extends BriarTestCase {
// Create the table
createTable(connection);
// Generate some IDs and two sets of names
byte[][] ids = new byte[BATCH_SIZE][32];
byte[][] ids = new byte[BATCH_SIZE][];
String[] oldNames = new String[BATCH_SIZE];
String[] newNames = new String[BATCH_SIZE];
Random random = new Random();
for (int i = 0; i < BATCH_SIZE; i++) {
random.nextBytes(ids[i]);
oldNames[i] = TestUtils.createRandomString(50);
newNames[i] = TestUtils.createRandomString(50);
ids[i] = TestUtils.getRandomId();
oldNames[i] = TestUtils.getRandomString(50);
newNames[i] = TestUtils.getRandomString(50);
}
// Insert the IDs and old names into the table as a batch
insertBatch(ids, oldNames);

View File

@@ -1382,20 +1382,20 @@ public class DatabaseComponentImplTest extends BriarTestCase {
}
private TransportKeys createTransportKeys() {
SecretKey inPrevTagKey = TestUtils.createSecretKey();
SecretKey inPrevHeaderKey = TestUtils.createSecretKey();
SecretKey inPrevTagKey = TestUtils.getSecretKey();
SecretKey inPrevHeaderKey = TestUtils.getSecretKey();
IncomingKeys inPrev = new IncomingKeys(inPrevTagKey, inPrevHeaderKey,
1, 123, new byte[4]);
SecretKey inCurrTagKey = TestUtils.createSecretKey();
SecretKey inCurrHeaderKey = TestUtils.createSecretKey();
SecretKey inCurrTagKey = TestUtils.getSecretKey();
SecretKey inCurrHeaderKey = TestUtils.getSecretKey();
IncomingKeys inCurr = new IncomingKeys(inCurrTagKey, inCurrHeaderKey,
2, 234, new byte[4]);
SecretKey inNextTagKey = TestUtils.createSecretKey();
SecretKey inNextHeaderKey = TestUtils.createSecretKey();
SecretKey inNextTagKey = TestUtils.getSecretKey();
SecretKey inNextHeaderKey = TestUtils.getSecretKey();
IncomingKeys inNext = new IncomingKeys(inNextTagKey, inNextHeaderKey,
3, 345, new byte[4]);
SecretKey outCurrTagKey = TestUtils.createSecretKey();
SecretKey outCurrHeaderKey = TestUtils.createSecretKey();
SecretKey outCurrTagKey = TestUtils.getSecretKey();
SecretKey outCurrHeaderKey = TestUtils.getSecretKey();
OutgoingKeys outCurr = new OutgoingKeys(outCurrTagKey, outCurrHeaderKey,
2, 456);
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr);

View File

@@ -63,7 +63,6 @@ public class H2DatabaseTest extends BriarTestCase {
private static final int MAX_SIZE = 5 * ONE_MEGABYTE;
private final File testDir = TestUtils.getTestDirectory();
private final Random random = new Random();
private final GroupId groupId;
private final Group group;
private final Author author;
@@ -90,8 +89,7 @@ public class H2DatabaseTest extends BriarTestCase {
new byte[MAX_PUBLIC_KEY_LENGTH], new byte[123], timestamp);
messageId = new MessageId(TestUtils.getRandomId());
size = 1234;
raw = new byte[size];
random.nextBytes(raw);
raw = TestUtils.getRandomBytes(size);
message = new Message(messageId, groupId, timestamp, raw);
transportId = new TransportId("id");
contactId = new ContactId(1);
@@ -747,7 +745,7 @@ public class H2DatabaseTest extends BriarTestCase {
db.updateTransportKeys(txn, Collections.singletonMap(contactId, keys));
// Update the reordering window and retrieve the transport keys
random.nextBytes(bitmap);
new Random().nextBytes(bitmap);
db.setReorderingWindow(txn, contactId, transportId, rotationPeriod,
base + 1, bitmap);
Map<ContactId, TransportKeys> newKeys =
@@ -1166,20 +1164,20 @@ public class H2DatabaseTest extends BriarTestCase {
}
private TransportKeys createTransportKeys() {
SecretKey inPrevTagKey = TestUtils.createSecretKey();
SecretKey inPrevHeaderKey = TestUtils.createSecretKey();
SecretKey inPrevTagKey = TestUtils.getSecretKey();
SecretKey inPrevHeaderKey = TestUtils.getSecretKey();
IncomingKeys inPrev = new IncomingKeys(inPrevTagKey, inPrevHeaderKey,
1, 123, new byte[4]);
SecretKey inCurrTagKey = TestUtils.createSecretKey();
SecretKey inCurrHeaderKey = TestUtils.createSecretKey();
SecretKey inCurrTagKey = TestUtils.getSecretKey();
SecretKey inCurrHeaderKey = TestUtils.getSecretKey();
IncomingKeys inCurr = new IncomingKeys(inCurrTagKey, inCurrHeaderKey,
2, 234, new byte[4]);
SecretKey inNextTagKey = TestUtils.createSecretKey();
SecretKey inNextHeaderKey = TestUtils.createSecretKey();
SecretKey inNextTagKey = TestUtils.getSecretKey();
SecretKey inNextHeaderKey = TestUtils.getSecretKey();
IncomingKeys inNext = new IncomingKeys(inNextTagKey, inNextHeaderKey,
3, 345, new byte[4]);
SecretKey outCurrTagKey = TestUtils.createSecretKey();
SecretKey outCurrHeaderKey = TestUtils.createSecretKey();
SecretKey outCurrTagKey = TestUtils.getSecretKey();
SecretKey outCurrHeaderKey = TestUtils.getSecretKey();
OutgoingKeys outCurr = new OutgoingKeys(outCurrTagKey, outCurrHeaderKey,
2, 456);
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr);

View File

@@ -8,7 +8,6 @@ import org.briarproject.api.crypto.PublicKey;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.keyagreement.Payload;
import org.briarproject.api.keyagreement.PayloadEncoder;
import org.briarproject.util.StringUtils;
import org.jmock.Expectations;
import org.jmock.auto.Mock;
import org.jmock.integration.junit4.JUnitRuleMockery;
@@ -69,8 +68,8 @@ public class KeyAgreementProtocolTest extends BriarTestCase {
final Payload theirPayload = new Payload(BOB_COMMIT, null);
final Payload ourPayload = new Payload(ALICE_COMMIT, null);
final KeyPair ourKeyPair = new KeyPair(ourPubKey, null);
final SecretKey sharedSecret = TestUtils.createSecretKey();
final SecretKey masterSecret = TestUtils.createSecretKey();
final SecretKey sharedSecret = TestUtils.getSecretKey();
final SecretKey masterSecret = TestUtils.getSecretKey();
KeyAgreementProtocol protocol =
new KeyAgreementProtocol(callbacks, crypto, payloadEncoder,
@@ -133,8 +132,8 @@ public class KeyAgreementProtocolTest extends BriarTestCase {
final Payload theirPayload = new Payload(ALICE_COMMIT, null);
final Payload ourPayload = new Payload(BOB_COMMIT, null);
final KeyPair ourKeyPair = new KeyPair(ourPubKey, null);
final SecretKey sharedSecret = TestUtils.createSecretKey();
final SecretKey masterSecret = TestUtils.createSecretKey();
final SecretKey sharedSecret = TestUtils.getSecretKey();
final SecretKey masterSecret = TestUtils.getSecretKey();
KeyAgreementProtocol protocol =
new KeyAgreementProtocol(callbacks, crypto, payloadEncoder,
@@ -274,7 +273,7 @@ public class KeyAgreementProtocolTest extends BriarTestCase {
final Payload theirPayload = new Payload(BOB_COMMIT, null);
final Payload ourPayload = new Payload(ALICE_COMMIT, null);
final KeyPair ourKeyPair = new KeyPair(ourPubKey, null);
final SecretKey sharedSecret = TestUtils.createSecretKey();
final SecretKey sharedSecret = TestUtils.getSecretKey();
KeyAgreementProtocol protocol =
new KeyAgreementProtocol(callbacks, crypto, payloadEncoder,
@@ -339,7 +338,7 @@ public class KeyAgreementProtocolTest extends BriarTestCase {
final Payload theirPayload = new Payload(ALICE_COMMIT, null);
final Payload ourPayload = new Payload(BOB_COMMIT, null);
final KeyPair ourKeyPair = new KeyPair(ourPubKey, null);
final SecretKey sharedSecret = TestUtils.createSecretKey();
final SecretKey sharedSecret = TestUtils.getSecretKey();
KeyAgreementProtocol protocol =
new KeyAgreementProtocol(callbacks, crypto, payloadEncoder,

View File

@@ -118,7 +118,7 @@ public class TransportPropertyValidatorTest extends BriarTestCase {
/* Generate a string or arbitrary length for the transport id*/
String wrongTransportIdString =
TestUtils.createRandomString(MAX_TRANSPORT_ID_LENGTH + 1);
TestUtils.getRandomString(MAX_TRANSPORT_ID_LENGTH + 1);
BdfList body = BdfList.of(deviceId, wrongTransportIdString, 4,
bdfDictionary);
tpv.validateMessage(message, group, body);

View File

@@ -11,7 +11,6 @@ import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import static org.briarproject.api.system.SeedProvider.SEED_BYTES;
@@ -68,8 +67,7 @@ public class LinuxSeedProviderTest extends BriarTestCase {
return;
}
// Generate a seed
byte[] seed = new byte[SEED_BYTES];
new Random().nextBytes(seed);
byte[] seed = TestUtils.getRandomBytes(SEED_BYTES);
// Write the seed to a file
File urandom = new File(testDir, "urandom");
urandom.delete();

View File

@@ -1,12 +1,12 @@
package org.briarproject.transport;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.transport.ReorderingWindow.Change;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import static org.briarproject.api.transport.TransportConstants.REORDERING_WINDOW_SIZE;
import static org.junit.Assert.assertArrayEquals;
@@ -14,12 +14,12 @@ import static org.junit.Assert.assertEquals;
public class ReorderingWindowTest extends BriarTestCase {
private static final int BITMAP_BYTES = REORDERING_WINDOW_SIZE / 8;
@Test
public void testBitmapConversion() {
Random random = new Random();
byte[] bitmap = new byte[REORDERING_WINDOW_SIZE / 8];
for (int i = 0; i < 1000; i++) {
random.nextBytes(bitmap);
byte[] bitmap = TestUtils.getRandomBytes(BITMAP_BYTES);
ReorderingWindow window = new ReorderingWindow(0L, bitmap);
assertArrayEquals(bitmap, window.getBitmap());
}
@@ -27,7 +27,7 @@ public class ReorderingWindowTest extends BriarTestCase {
@Test
public void testWindowSlidesWhenFirstElementIsSeen() {
byte[] bitmap = new byte[REORDERING_WINDOW_SIZE / 8];
byte[] bitmap = new byte[BITMAP_BYTES];
ReorderingWindow window = new ReorderingWindow(0L, bitmap);
// Set the first element seen
Change change = window.setSeen(0L);
@@ -42,7 +42,7 @@ public class ReorderingWindowTest extends BriarTestCase {
@Test
public void testWindowDoesNotSlideWhenElementBelowMidpointIsSeen() {
byte[] bitmap = new byte[REORDERING_WINDOW_SIZE / 8];
byte[] bitmap = new byte[BITMAP_BYTES];
ReorderingWindow window = new ReorderingWindow(0L, bitmap);
// Set an element below the midpoint seen
Change change = window.setSeen(1L);
@@ -57,7 +57,7 @@ public class ReorderingWindowTest extends BriarTestCase {
@Test
public void testWindowSlidesWhenElementAboveMidpointIsSeen() {
byte[] bitmap = new byte[REORDERING_WINDOW_SIZE / 8];
byte[] bitmap = new byte[BITMAP_BYTES];
ReorderingWindow window = new ReorderingWindow(0, bitmap);
long aboveMidpoint = REORDERING_WINDOW_SIZE / 2;
// Set an element above the midpoint seen
@@ -74,7 +74,7 @@ public class ReorderingWindowTest extends BriarTestCase {
@Test
public void testWindowSlidesUntilLowestElementIsUnseenWhenFirstElementIsSeen() {
byte[] bitmap = new byte[REORDERING_WINDOW_SIZE / 8];
byte[] bitmap = new byte[BITMAP_BYTES];
ReorderingWindow window = new ReorderingWindow(0L, bitmap);
window.setSeen(1L);
// Set the first element seen
@@ -90,7 +90,7 @@ public class ReorderingWindowTest extends BriarTestCase {
@Test
public void testWindowSlidesUntilLowestElementIsUnseenWhenElementAboveMidpointIsSeen() {
byte[] bitmap = new byte[REORDERING_WINDOW_SIZE / 8];
byte[] bitmap = new byte[BITMAP_BYTES];
ReorderingWindow window = new ReorderingWindow(0L, bitmap);
window.setSeen(1L);
long aboveMidpoint = REORDERING_WINDOW_SIZE / 2;

View File

@@ -1,6 +1,7 @@
package org.briarproject.transport;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.crypto.StreamDecrypter;
import org.briarproject.api.crypto.StreamEncrypter;
import org.junit.Test;
@@ -10,7 +11,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;
import static org.briarproject.api.transport.TransportConstants.FRAME_HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAC_LENGTH;
@@ -19,20 +19,15 @@ import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class TransportIntegrationTest extends BriarTestCase {
private final Random random = new Random();
public class StreamReaderWriterIntegrationTest extends BriarTestCase {
@Test
public void testWriteAndRead() throws Exception {
// Generate a random tag
byte[] tag = new byte[TAG_LENGTH];
random.nextBytes(tag);
byte[] tag = TestUtils.getRandomBytes(TAG_LENGTH);
// Generate two frames with random payloads
byte[] payload1 = new byte[123];
random.nextBytes(payload1);
byte[] payload2 = new byte[321];
random.nextBytes(payload2);
byte[] payload1 = TestUtils.getRandomBytes(123);
byte[] payload2 = TestUtils.getRandomBytes(321);
// Write the tag and the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamEncrypter encrypter = new TestStreamEncrypter(out, tag);

View File

@@ -46,9 +46,9 @@ public class TransportKeyManagerTest extends BriarTestCase {
private final long rotationPeriodLength = maxLatency + MAX_CLOCK_DIFFERENCE;
private final ContactId contactId = new ContactId(123);
private final ContactId contactId1 = new ContactId(234);
private final SecretKey tagKey = TestUtils.createSecretKey();
private final SecretKey headerKey = TestUtils.createSecretKey();
private final SecretKey masterKey = TestUtils.createSecretKey();
private final SecretKey tagKey = TestUtils.getSecretKey();
private final SecretKey headerKey = TestUtils.getSecretKey();
private final SecretKey masterKey = TestUtils.getSecretKey();
private final Random random = new Random();
@Test