Skip to content

Commit

Permalink
allow empty chunks, so UVF's EOF-chunks can be added
Browse files Browse the repository at this point in the history
  • Loading branch information
overheadhunter committed Nov 29, 2024
1 parent f47b27b commit f07ef0e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/*******************************************************************************
* Copyright (c) 2016 Sebastian Stenzel and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the accompanying LICENSE.txt.
*
* Contributors:
* Sebastian Stenzel - initial API and implementation
*******************************************************************************/
package org.cryptomator.cryptolib.common;

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

private void encryptAndFlushBuffer() throws IOException {
cleartextBuffer.flip();
if (cleartextBuffer.hasRemaining()) {
ByteBuffer ciphertextBuffer = cryptor.fileContentCryptor().encryptChunk(cleartextBuffer, chunkNumber++, header);
delegate.write(ciphertextBuffer);
}
ByteBuffer ciphertextBuffer = cryptor.fileContentCryptor().encryptChunk(cleartextBuffer, chunkNumber++, header);
delegate.write(ciphertextBuffer);
cleartextBuffer.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ByteBuffer encryptChunk(ByteBuffer cleartextChunk, long chunkNumber, File

@Override
public void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, FileHeader header) {
if (cleartextChunk.remaining() <= 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) {
if (cleartextChunk.remaining() < 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) {
throw new IllegalArgumentException("Invalid cleartext chunk size: " + cleartextChunk.remaining() + ", expected range [1, " + PAYLOAD_SIZE + "]");
}
if (ciphertextChunk.remaining() < CHUNK_SIZE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ByteBuffer encryptChunk(ByteBuffer cleartextChunk, long chunkNumber, File

@Override
public void encryptChunk(ByteBuffer cleartextChunk, ByteBuffer ciphertextChunk, long chunkNumber, FileHeader header) {
if (cleartextChunk.remaining() <= 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) {
if (cleartextChunk.remaining() < 0 || cleartextChunk.remaining() > PAYLOAD_SIZE) {
throw new IllegalArgumentException("Invalid cleartext chunk size: " + cleartextChunk.remaining() + ", expected range [1, " + PAYLOAD_SIZE + "]");
}
if (ciphertextChunk.remaining() < CHUNK_SIZE) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/*******************************************************************************
* Copyright (c) 2016 Sebastian Stenzel and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the accompanying LICENSE.txt.
*
* Contributors:
* Sebastian Stenzel - initial API and implementation
*******************************************************************************/
package org.cryptomator.cryptolib.common;

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

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

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

}

0 comments on commit f07ef0e

Please sign in to comment.