Skip to content

Commit f07ef0e

Browse files
allow empty chunks, so UVF's EOF-chunks can be added
1 parent f47b27b commit f07ef0e

File tree

4 files changed

+8
-25
lines changed

4 files changed

+8
-25
lines changed

src/main/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannel.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/*******************************************************************************
2-
* Copyright (c) 2016 Sebastian Stenzel and others.
3-
* All rights reserved. This program and the accompanying materials
4-
* are made available under the terms of the accompanying LICENSE.txt.
5-
*
6-
* Contributors:
7-
* Sebastian Stenzel - initial API and implementation
8-
*******************************************************************************/
91
package org.cryptomator.cryptolib.common;
102

113
import org.cryptomator.cryptolib.api.Cryptor;
@@ -66,10 +58,8 @@ private void writeHeaderOnFirstWrite() throws IOException {
6658

6759
private void encryptAndFlushBuffer() throws IOException {
6860
cleartextBuffer.flip();
69-
if (cleartextBuffer.hasRemaining()) {
70-
ByteBuffer ciphertextBuffer = cryptor.fileContentCryptor().encryptChunk(cleartextBuffer, chunkNumber++, header);
71-
delegate.write(ciphertextBuffer);
72-
}
61+
ByteBuffer ciphertextBuffer = cryptor.fileContentCryptor().encryptChunk(cleartextBuffer, chunkNumber++, header);
62+
delegate.write(ciphertextBuffer);
7363
cleartextBuffer.clear();
7464
}
7565

src/main/java/org/cryptomator/cryptolib/v1/FileContentCryptorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public ByteBuffer encryptChunk(ByteBuffer cleartextChunk, long chunkNumber, File
6565

6666
@Override
6767
public void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, FileHeader header) {
68-
if (cleartextChunk.remaining() <= 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) {
68+
if (cleartextChunk.remaining() < 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) {
6969
throw new IllegalArgumentException("Invalid cleartext chunk size: " + cleartextChunk.remaining() + ", expected range [1, " + PAYLOAD_SIZE + "]");
7070
}
7171
if (ciphertextChunk.remaining() < CHUNK_SIZE) {

src/main/java/org/cryptomator/cryptolib/v2/FileContentCryptorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ByteBuffer encryptChunk(ByteBuffer cleartextChunk, long chunkNumber, File
6363

6464
@Override
6565
public void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, FileHeader header) {
66-
if (cleartextChunk.remaining() <= 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) {
66+
if (cleartextChunk.remaining() < 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) {
6767
throw new IllegalArgumentException("Invalid cleartext chunk size: " + cleartextChunk.remaining() + ", expected range [1, " + PAYLOAD_SIZE + "]");
6868
}
6969
if (ciphertextChunk.remaining() < CHUNK_SIZE) {

src/test/java/org/cryptomator/cryptolib/common/EncryptingWritableByteChannelTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/*******************************************************************************
2-
* Copyright (c) 2016 Sebastian Stenzel and others.
3-
* All rights reserved. This program and the accompanying materials
4-
* are made available under the terms of the accompanying LICENSE.txt.
5-
*
6-
* Contributors:
7-
* Sebastian Stenzel - initial API and implementation
8-
*******************************************************************************/
91
package org.cryptomator.cryptolib.common;
102

113
import org.cryptomator.cryptolib.api.Cryptor;
@@ -49,7 +41,8 @@ public void setup() {
4941
Mockito.when(contentCryptor.encryptChunk(Mockito.any(ByteBuffer.class), Mockito.anyLong(), Mockito.any(FileHeader.class))).thenAnswer(invocation -> {
5042
ByteBuffer input = invocation.getArgument(0);
5143
String inStr = UTF_8.decode(input).toString();
52-
return ByteBuffer.wrap(inStr.toUpperCase().getBytes(UTF_8));
44+
String outStr = "<" + inStr.toUpperCase() + ">";
45+
return UTF_8.encode(outStr);
5346
});
5447
}
5548

@@ -60,7 +53,7 @@ public void testEncryption() throws IOException {
6053
ch.write(UTF_8.encode("hello world 2"));
6154
}
6255
dstFile.flip();
63-
Assertions.assertArrayEquals("hhhhhHELLO WORLD 1HELLO WORLD 2".getBytes(), Arrays.copyOfRange(dstFile.array(), 0, dstFile.remaining()));
56+
Assertions.assertEquals("hhhhh<HELLO WORL><D 1HELLO W><ORLD 2>", UTF_8.decode(dstFile).toString());
6457
}
6558

6659
@Test
@@ -69,7 +62,7 @@ public void testEncryptionOfEmptyFile() throws IOException {
6962
// empty, so write nothing
7063
}
7164
dstFile.flip();
72-
Assertions.assertArrayEquals("hhhhh".getBytes(), Arrays.copyOfRange(dstFile.array(), 0, dstFile.remaining()));
65+
Assertions.assertEquals("hhhhh<>", UTF_8.decode(dstFile).toString());
7366
}
7467

7568
}

0 commit comments

Comments
 (0)