Renamed some classes.

This commit is contained in:
akwizgran
2012-01-17 13:19:40 +00:00
parent 79814bd406
commit 9bd0b60dec
21 changed files with 101 additions and 99 deletions

View File

@@ -49,8 +49,6 @@
<test name='net.sf.briar.serial.ReaderImplTest'/>
<test name='net.sf.briar.serial.WriterImplTest'/>
<test name='net.sf.briar.setup.SetupWorkerTest'/>
<test name='net.sf.briar.transport.ConnectionDecrypterTest'/>
<test name='net.sf.briar.transport.ConnectionEncrypterImplTest'/>
<test name='net.sf.briar.transport.ConnectionReaderImplTest'/>
<test name='net.sf.briar.transport.ConnectionRecogniserImplTest'/>
<test name='net.sf.briar.transport.ConnectionRegistryImplTest'/>
@@ -58,8 +56,10 @@
<test name='net.sf.briar.transport.ConnectionWriterImplTest'/>
<test name='net.sf.briar.transport.ConnectionWriterTest'/>
<test name='net.sf.briar.transport.FrameReadWriteTest'/>
<test name='net.sf.briar.transport.SegmentedConnectionDecrypterTest'/>
<test name='net.sf.briar.transport.SegmentedConnectionEncrypterTest'/>
<test name='net.sf.briar.transport.IncomingEncryptionLayerImplTest'/>
<test name='net.sf.briar.transport.IncomingSegmentedEncryptionLayerTest'/>
<test name='net.sf.briar.transport.OutgoingEncryptionLayerImplTest'/>
<test name='net.sf.briar.transport.OutgoingSegmentedEncryptionLayerTest'/>
<test name='net.sf.briar.util.ByteUtilsTest'/>
<test name='net.sf.briar.util.FileUtilsTest'/>
<test name='net.sf.briar.util.StringUtilsTest'/>

View File

@@ -31,7 +31,7 @@ public class ConnectionReaderImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
FrameSource d = new NullConnectionDecrypter(in, macLength);
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
// There should be no bytes available before EOF
assertEquals(-1, r.getInputStream().read());
@@ -49,7 +49,7 @@ public class ConnectionReaderImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
FrameSource d = new NullConnectionDecrypter(in, macLength);
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
// There should be one byte available before EOF
assertEquals(0, r.getInputStream().read());
@@ -75,7 +75,7 @@ public class ConnectionReaderImplTest extends TransportTest {
out.write(frame1);
// Read the first frame
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
FrameSource d = new NullConnectionDecrypter(in, macLength);
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
byte[] read = new byte[maxPayloadLength];
TestUtils.readFully(r.getInputStream(), read);
@@ -109,7 +109,7 @@ public class ConnectionReaderImplTest extends TransportTest {
out.write(frame1);
// Read the first frame
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
FrameSource d = new NullConnectionDecrypter(in, macLength);
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
byte[] read = new byte[maxPayloadLength - paddingLength];
TestUtils.readFully(r.getInputStream(), read);
@@ -135,7 +135,7 @@ public class ConnectionReaderImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength + paddingLength);
// Read the frame
ByteArrayInputStream in = new ByteArrayInputStream(frame);
FrameSource d = new NullConnectionDecrypter(in, macLength);
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
// The non-zero padding should be rejected
try {
@@ -167,7 +167,7 @@ public class ConnectionReaderImplTest extends TransportTest {
out.write(frame1);
// Read the frames
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
FrameSource d = new NullConnectionDecrypter(in, macLength);
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
byte[] read = new byte[payloadLength];
TestUtils.readFully(r.getInputStream(), read);
@@ -191,7 +191,7 @@ public class ConnectionReaderImplTest extends TransportTest {
frame[12] ^= 1;
// Try to read the frame - not a single byte should be read
ByteArrayInputStream in = new ByteArrayInputStream(frame);
FrameSource d = new NullConnectionDecrypter(in, macLength);
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
try {
r.getInputStream().read();
@@ -213,7 +213,7 @@ public class ConnectionReaderImplTest extends TransportTest {
frame[17] ^= 1;
// Try to read the frame - not a single byte should be read
ByteArrayInputStream in = new ByteArrayInputStream(frame);
FrameSource d = new NullConnectionDecrypter(in, macLength);
IncomingEncryptionLayer d = new NullConnectionDecrypter(in, macLength);
ConnectionReader r = new ConnectionReaderImpl(d, mac, macKey);
try {
r.getInputStream().read();

View File

@@ -20,7 +20,7 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testFlushWithoutWriteProducesNothing() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConnectionEncrypter e = new NullConnectionEncrypter(out);
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
w.getOutputStream().flush();
w.getOutputStream().flush();
@@ -40,7 +40,7 @@ public class ConnectionWriterImplTest extends TransportTest {
mac.doFinal(frame, FRAME_HEADER_LENGTH + payloadLength);
// Check that the ConnectionWriter gets the same results
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConnectionEncrypter e = new NullConnectionEncrypter(out);
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
w.getOutputStream().write(0);
w.getOutputStream().flush();
@@ -50,7 +50,7 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testWriteByteToMaxLengthWritesFrame() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConnectionEncrypter e = new NullConnectionEncrypter(out);
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
OutputStream out1 = w.getOutputStream();
// The first maxPayloadLength - 1 bytes should be buffered
@@ -64,7 +64,7 @@ public class ConnectionWriterImplTest extends TransportTest {
@Test
public void testWriteArrayToMaxLengthWritesFrame() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConnectionEncrypter e = new NullConnectionEncrypter(out);
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
OutputStream out1 = w.getOutputStream();
// The first maxPayloadLength - 1 bytes should be buffered
@@ -99,7 +99,7 @@ public class ConnectionWriterImplTest extends TransportTest {
byte[] expected = out.toByteArray();
// Check that the ConnectionWriter gets the same results
out.reset();
ConnectionEncrypter e = new NullConnectionEncrypter(out);
OutgoingEncryptionLayer e = new NullConnectionEncrypter(out);
ConnectionWriter w = new ConnectionWriterImpl(e, mac, macKey);
w.getOutputStream().write(new byte[123]);
w.getOutputStream().flush();

View File

@@ -74,7 +74,7 @@ public class FrameReadWriteTest extends BriarTestCase {
ErasableKey macCopy = macKey.copy();
// Write the frames
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagCopy, frameCopy,
false);
ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac,
@@ -91,7 +91,7 @@ public class FrameReadWriteTest extends BriarTestCase {
assertArrayEquals(tag, recoveredTag);
assertTrue(TagEncoder.validateTag(tag, 0, tagCipher, tagKey));
// Read the frames back
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in, tagCipher,
frameCipher, tagKey, frameKey, mac.getMacLength(), false);
ConnectionReader reader = new ConnectionReaderImpl(decrypter, mac,
macKey);

View File

@@ -20,14 +20,14 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConnectionDecrypterTest extends BriarTestCase {
public class IncomingEncryptionLayerImplTest extends BriarTestCase {
private static final int MAC_LENGTH = 32;
private final Cipher tagCipher, frameCipher;
private final ErasableKey tagKey, frameKey;
public ConnectionDecrypterTest() {
public IncomingEncryptionLayerImplTest() {
super();
Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
@@ -59,9 +59,9 @@ public class ConnectionDecrypterTest extends BriarTestCase {
out.write(ciphertext);
out.write(ciphertext1);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Use a ConnectionDecrypter to decrypt the ciphertext
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
frameCipher, tagKey, frameKey, MAC_LENGTH, false);
// Use the encryption layer to decrypt the ciphertext
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
tagCipher, frameCipher, tagKey, frameKey, MAC_LENGTH, false);
// First frame
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
@@ -99,9 +99,9 @@ public class ConnectionDecrypterTest extends BriarTestCase {
out.write(ciphertext);
out.write(ciphertext1);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Use a ConnectionDecrypter to decrypt the ciphertext
FrameSource decrypter = new ConnectionDecrypter(in, tagCipher,
frameCipher, tagKey, frameKey, MAC_LENGTH, true);
// Use the encryption layer to decrypt the ciphertext
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
tagCipher, frameCipher, tagKey, frameKey, MAC_LENGTH, true);
// First frame
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
assertEquals(plaintext.length, decrypter.readFrame(decrypted));

View File

@@ -21,14 +21,14 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class SegmentedConnectionDecrypterTest extends BriarTestCase {
public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
private static final int MAC_LENGTH = 32;
private final Cipher tagCipher, frameCipher;
private final ErasableKey tagKey, frameKey;
public SegmentedConnectionDecrypterTest() {
public IncomingSegmentedEncryptionLayerTest() {
super();
Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
@@ -55,11 +55,12 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0,
plaintext1.length);
// Use a connection decrypter to decrypt the ciphertext
// Use the encryption layer to decrypt the ciphertext
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
SegmentSource in = new ByteArraySegmentSource(frames);
FrameSource decrypter = new SegmentedConnectionDecrypter(in, tagCipher,
frameCipher, tagKey, frameKey, MAC_LENGTH, false);
IncomingEncryptionLayer decrypter =
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
tagKey, frameKey, MAC_LENGTH, false);
// First frame
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
@@ -92,11 +93,12 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
frameCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1,
TAG_LENGTH);
// Use a connection decrypter to decrypt the ciphertext
// Use the encryption layer to decrypt the ciphertext
byte[][] frames = new byte[][] { ciphertext, ciphertext1 };
SegmentSource in = new ByteArraySegmentSource(frames);
FrameSource decrypter = new SegmentedConnectionDecrypter(in, tagCipher,
frameCipher, tagKey, frameKey, MAC_LENGTH, true);
IncomingEncryptionLayer decrypter =
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
tagKey, frameKey, MAC_LENGTH, true);
// First frame
byte[] decrypted = new byte[MAX_FRAME_LENGTH];
assertEquals(plaintext.length, decrypter.readFrame(decrypted));
@@ -112,20 +114,20 @@ public class SegmentedConnectionDecrypterTest extends BriarTestCase {
private static class ByteArraySegmentSource implements SegmentSource {
private final byte[][] frames;
private final byte[][] segments;
private int frame = 0;
private int segmentNumber = 0;
private ByteArraySegmentSource(byte[][] frames) {
this.frames = frames;
this.segments = frames;
}
public boolean readSegment(Segment s) throws IOException {
if(frame == frames.length) return false;
byte[] src = frames[frame];
if(segmentNumber == segments.length) return false;
byte[] src = segments[segmentNumber];
System.arraycopy(src, 0, s.getBuffer(), 0, src.length);
s.setLength(src.length);
frame++;
segmentNumber++;
return true;
}
}

View File

@@ -10,7 +10,7 @@ import java.io.InputStream;
import net.sf.briar.api.FormatException;
/** A connection decrypter that performs no decryption. */
class NullConnectionDecrypter implements FrameSource {
class NullConnectionDecrypter implements IncomingEncryptionLayer {
private final InputStream in;
private final int macLength;

View File

@@ -4,7 +4,7 @@ import java.io.IOException;
import java.io.OutputStream;
/** A ConnectionEncrypter that performs no encryption. */
class NullConnectionEncrypter implements ConnectionEncrypter {
class NullConnectionEncrypter implements OutgoingEncryptionLayer {
private final OutputStream out;

View File

@@ -18,14 +18,14 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ConnectionEncrypterImplTest extends BriarTestCase {
public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
private static final int MAC_LENGTH = 32;
private final Cipher tagCipher, frameCipher;
private final ErasableKey tagKey, frameKey;
public ConnectionEncrypterImplTest() {
public OutgoingEncryptionLayerImplTest() {
super();
Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
@@ -58,16 +58,18 @@ public class ConnectionEncrypterImplTest extends BriarTestCase {
out.write(ciphertext);
out.write(ciphertext1);
byte[] expected = out.toByteArray();
// Use a ConnectionEncrypter to encrypt the plaintext
// Use the encryption layer to encrypt the plaintext
out.reset();
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey, false);
e.writeFrame(plaintext, plaintext.length);
e.writeFrame(plaintext1, plaintext1.length);
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey,
false);
encrypter.writeFrame(plaintext, plaintext.length);
encrypter.writeFrame(plaintext1, plaintext1.length);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);
assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity());
assertEquals(Long.MAX_VALUE - actual.length,
encrypter.getRemainingCapacity());
}
@Test
@@ -97,15 +99,16 @@ public class ConnectionEncrypterImplTest extends BriarTestCase {
out.write(tag1);
out.write(ciphertext1);
byte[] expected = out.toByteArray();
// Use a ConnectionEncrypter to encrypt the plaintext
// Use the encryption layer to encrypt the plaintext
out.reset();
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey, true);
e.writeFrame(plaintext, plaintext.length);
e.writeFrame(plaintext1, plaintext1.length);
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey, true);
encrypter.writeFrame(plaintext, plaintext.length);
encrypter.writeFrame(plaintext1, plaintext1.length);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);
assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity());
assertEquals(Long.MAX_VALUE - actual.length,
encrypter.getRemainingCapacity());
}
}

View File

@@ -21,14 +21,14 @@ import org.junit.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class SegmentedConnectionEncrypterTest extends BriarTestCase {
public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
private static final int MAC_LENGTH = 32;
private final Cipher tagCipher, frameCipher;
private final ErasableKey tagKey, frameKey;
public SegmentedConnectionEncrypterTest() {
public OutgoingSegmentedEncryptionLayerTest() {
super();
Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
@@ -61,18 +61,19 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
out.write(ciphertext);
out.write(ciphertext1);
byte[] expected = out.toByteArray();
// Use a connection encrypter to encrypt the plaintext
// Use the encryption layer to encrypt the plaintext
ByteArraySegmentSink sink = new ByteArraySegmentSink();
ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey,
false);
OutgoingEncryptionLayer encrypter =
new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey, false);
// The first frame's buffer must have enough space for the tag
e.writeFrame(plaintext, plaintext.length);
e.writeFrame(plaintext1, plaintext1.length);
encrypter.writeFrame(plaintext, plaintext.length);
encrypter.writeFrame(plaintext1, plaintext1.length);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);
assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity());
assertEquals(Long.MAX_VALUE - actual.length,
encrypter.getRemainingCapacity());
}
@Test
@@ -102,16 +103,18 @@ public class SegmentedConnectionEncrypterTest extends BriarTestCase {
out.write(tag1);
out.write(ciphertext1);
byte[] expected = out.toByteArray();
// Use a connection encrypter to encrypt the plaintext
// Use the encryption layer to encrypt the plaintext
SegmentSink sink = new ByteArraySegmentSink();
ConnectionEncrypter e = new SegmentedConnectionEncrypter(sink,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey, true);
e.writeFrame(plaintext, plaintext.length);
e.writeFrame(plaintext1, plaintext1.length);
OutgoingEncryptionLayer encrypter =
new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey, true);
encrypter.writeFrame(plaintext, plaintext.length);
encrypter.writeFrame(plaintext1, plaintext1.length);
byte[] actual = out.toByteArray();
// Check that the actual ciphertext matches the expected ciphertext
assertArrayEquals(expected, actual);
assertEquals(Long.MAX_VALUE - actual.length, e.getRemainingCapacity());
assertEquals(Long.MAX_VALUE - actual.length,
encrypter.getRemainingCapacity());
}
private static class ByteArraySegmentSink extends ByteArrayOutputStream