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, AuthenticatedCipher frameCipher, ErasableKey tagKey,
ErasableKey frameKey, boolean writeTag, int maxFrameLength) { ErasableKey frameKey, boolean writeTag, int maxFrameLength) {
this.out = out; this.out = out;
this.capacity = capacity; this.capacity = writeTag ? capacity - TAG_LENGTH : capacity;
this.tagCipher = tagCipher; this.tagCipher = tagCipher;
this.frameCipher = frameCipher; this.frameCipher = frameCipher;
this.tagKey = tagKey; this.tagKey = tagKey;
@@ -53,6 +53,9 @@ class OutgoingEncryptionLayer implements FrameWriter {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if(!lastFrame && ciphertextLength < maxFrameLength) if(!lastFrame && ciphertextLength < maxFrameLength)
throw new IllegalArgumentException(); 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 // Write the tag if required
if(writeTag) { if(writeTag) {
TagEncoder.encodeTag(ciphertext, tagCipher, tagKey); TagEncoder.encodeTag(ciphertext, tagCipher, tagKey);
@@ -63,7 +66,6 @@ class OutgoingEncryptionLayer implements FrameWriter {
tagKey.erase(); tagKey.erase();
throw e; throw e;
} }
capacity -= TAG_LENGTH;
writeTag = false; writeTag = false;
} }
// Encode the header // Encode the header
@@ -99,6 +101,6 @@ class OutgoingEncryptionLayer implements FrameWriter {
} }
public long getRemainingCapacity() { 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)); will(returnValue(null));
}}); }});
connection.write(); connection.write();
// Nothing should have been written except the tag and an empty frame // Nothing should have been written
int nothing = TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH; assertEquals(0, out.size());
assertEquals(nothing, out.size());
// The transport should have been disposed with exception == false // The transport should have been disposed with exception == false
assertTrue(transport.getDisposed()); assertTrue(transport.getDisposed());
assertFalse(transport.getException()); assertFalse(transport.getException());
@@ -183,8 +182,8 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
}}); }});
connection.write(); connection.write();
// Something should have been written // Something should have been written
int nothing = TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH; int overhead = TAG_LENGTH + HEADER_LENGTH + MAC_LENGTH;
assertTrue(out.size() > nothing + UniqueId.LENGTH + message.length); assertTrue(out.size() > overhead + UniqueId.LENGTH + message.length);
// The transport should have been disposed with exception == false // The transport should have been disposed with exception == false
assertTrue(transport.getDisposed()); assertTrue(transport.getDisposed());
assertFalse(transport.getException()); assertFalse(transport.getException());