Allow the initiator to close a connection without writing.

This will make it easier for simplex transports to discard empty
connections.
This commit is contained in:
akwizgran
2012-08-28 11:38:26 +01:00
parent ff73905330
commit 1762eaf3e9
2 changed files with 9 additions and 8 deletions

View File

@@ -32,7 +32,7 @@ class OutgoingEncryptionLayer implements FrameWriter {
AuthenticatedCipher frameCipher, ErasableKey tagKey,
ErasableKey frameKey, boolean writeTag, int maxFrameLength) {
this.out = out;
this.capacity = capacity;
this.capacity = writeTag ? capacity - TAG_LENGTH : capacity;
this.tagCipher = tagCipher;
this.frameCipher = frameCipher;
this.tagKey = tagKey;
@@ -53,6 +53,9 @@ class OutgoingEncryptionLayer implements FrameWriter {
throw new IllegalArgumentException();
if(!lastFrame && ciphertextLength < maxFrameLength)
throw new IllegalArgumentException();
// If the initiator's side of the connection is closed without writing
// any payload or padding, don't write a tag or an empty frame
if(writeTag && lastFrame && payloadLength + paddingLength == 0) return;
// Write the tag if required
if(writeTag) {
TagEncoder.encodeTag(ciphertext, tagCipher, tagKey);
@@ -63,7 +66,6 @@ class OutgoingEncryptionLayer implements FrameWriter {
tagKey.erase();
throw e;
}
capacity -= TAG_LENGTH;
writeTag = false;
}
// Encode the header
@@ -99,6 +101,6 @@ class OutgoingEncryptionLayer implements FrameWriter {
}
public long getRemainingCapacity() {
return writeTag ? capacity - TAG_LENGTH : capacity;
return capacity;
}
}

View File

@@ -131,9 +131,8 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
will(returnValue(null));
}});
connection.write();
// Nothing should have been written except the tag and an empty frame
int nothing = TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH;
assertEquals(nothing, out.size());
// Nothing should have been written
assertEquals(0, out.size());
// The transport should have been disposed with exception == false
assertTrue(transport.getDisposed());
assertFalse(transport.getException());
@@ -183,8 +182,8 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
}});
connection.write();
// Something should have been written
int nothing = TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH;
assertTrue(out.size() > nothing + UniqueId.LENGTH + message.length);
int overhead = TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH;
assertTrue(out.size() > overhead + UniqueId.LENGTH + message.length);
// The transport should have been disposed with exception == false
assertTrue(transport.getDisposed());
assertFalse(transport.getException());