mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Test cleanup. #280
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user