use of ch.cyberduck.core.sds.SDSSession in project cyberduck by iterate-ch.
the class TripleCryptEncryptingInputStreamTest method testEncryptDecryptZeroBytes.
@Test
public void testEncryptDecryptZeroBytes() throws Exception {
final byte[] content = RandomUtils.nextBytes(0);
final ByteArrayInputStream plain = new ByteArrayInputStream(content);
final PlainFileKey key = Crypto.generateFileKey(PlainFileKey.Version.AES256GCM);
final SDSSession session = new SDSSession(new Host(new TestProtocol()), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {
@Override
public SDSApiClient getClient() {
return new SDSApiClient(new MockHttpClient());
}
};
final TransferStatus status = new TransferStatus();
final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeValue(out, TripleCryptConverter.toSwaggerFileKey(key));
status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
final TripleCryptEncryptingInputStream encryptInputStream = new TripleCryptEncryptingInputStream(session, plain, Crypto.createFileEncryptionCipher(key), status);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
IOUtils.copy(encryptInputStream, os, 42);
encryptInputStream.close();
out.close();
final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
final FileKey fileKey = reader.readValue(status.getFilekey().array());
final TripleCryptDecryptingInputStream cryptInputStream = new TripleCryptDecryptingInputStream(is, Crypto.createFileDecryptionCipher(TripleCryptConverter.toCryptoPlainFileKey(fileKey)), CryptoUtils.stringToByteArray(fileKey.getTag()));
final byte[] compare = new byte[content.length];
IOUtils.read(cryptInputStream, compare);
assertArrayEquals(content, compare);
}
use of ch.cyberduck.core.sds.SDSSession in project cyberduck by iterate-ch.
the class TripleCryptEncryptingInputStreamTest method testEncryptDecryptWithContentSizeMultipleOfEncryptingBufferSize.
@Test
public void testEncryptDecryptWithContentSizeMultipleOfEncryptingBufferSize() throws Exception {
final byte[] content = RandomUtils.nextBytes(1024 * 1024);
final ByteArrayInputStream plain = new ByteArrayInputStream(content);
final PlainFileKey key = Crypto.generateFileKey(PlainFileKey.Version.AES256GCM);
final SDSSession session = new SDSSession(new Host(new TestProtocol()), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {
@Override
public SDSApiClient getClient() {
return new SDSApiClient(new MockHttpClient());
}
};
final TransferStatus status = new TransferStatus();
final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeValue(out, TripleCryptConverter.toSwaggerFileKey(key));
status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
final TripleCryptEncryptingInputStream encryptInputStream = new TripleCryptEncryptingInputStream(session, plain, Crypto.createFileEncryptionCipher(key), status);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
new StreamCopier(StreamCancelation.noop, StreamProgress.noop).withLimit((long) content.length).withChunksize(32768).transfer(encryptInputStream, os);
encryptInputStream.close();
out.close();
final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
final FileKey fileKey = reader.readValue(status.getFilekey().array());
assertNotNull(fileKey.getTag());
final TripleCryptDecryptingInputStream cryptInputStream = new TripleCryptDecryptingInputStream(is, Crypto.createFileDecryptionCipher(TripleCryptConverter.toCryptoPlainFileKey(fileKey)), CryptoUtils.stringToByteArray(fileKey.getTag()));
final byte[] compare = new byte[content.length];
IOUtils.read(cryptInputStream, compare);
assertArrayEquals(content, compare);
}
use of ch.cyberduck.core.sds.SDSSession in project cyberduck by iterate-ch.
the class SDSSingleTransferWorkerTest method testTransferredSizeRepeat.
@Test
public void testTransferredSizeRepeat() throws Exception {
final SDSNodeIdProvider fileid = new SDSNodeIdProvider(session);
final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
// chunk size 32768
final byte[] content = new byte[98305];
new Random().nextBytes(content);
final OutputStream out = local.getOutputStream(false);
IOUtils.write(content, out);
out.close();
final AtomicBoolean failed = new AtomicBoolean();
final BytecountStreamListener counter = new BytecountStreamListener();
final SDSSession conn = new SDSSession(session.getHost().withCredentials(new Credentials(System.getProperties().getProperty("sds.user"), System.getProperties().getProperty("sds.key"))), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {
@Override
@SuppressWarnings("unchecked")
public <T> T _getFeature(final Class<T> type) {
if (type == Upload.class) {
return (T) new DefaultUploadFeature(new SDSMultipartWriteFeature(this, fileid) {
@Override
public HttpResponseOutputStream<Node> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
final HttpResponseOutputStream<Node> proxy = super.write(file, status, callback);
if (failed.get()) {
// Second attempt successful
return proxy;
}
return new HttpResponseOutputStream<Node>(new CountingOutputStream(proxy) {
@Override
protected void afterWrite(final int n) throws IOException {
super.afterWrite(n);
if (this.getByteCount() >= 42768L) {
// Buffer size
assertEquals(32768L, counter.getSent());
failed.set(true);
throw new SocketTimeoutException();
}
}
}, new SDSAttributesAdapter(session), status) {
@Override
public Node getStatus() throws BackgroundException {
return proxy.getStatus();
}
};
}
});
}
return super._getFeature(type);
}
};
conn.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
conn.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
final Path room = new SDSDirectoryFeature(conn, fileid).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory, Path.Type.volume)), new TransferStatus());
final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final Transfer t = new UploadTransfer(session.getHost(), test, local);
assertTrue(new SingleTransferWorker(conn, conn, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledProgressListener(), counter, new DisabledLoginCallback(), new DisabledNotificationService()) {
}.run(session));
local.delete();
assertTrue(t.isComplete());
assertEquals(content.length, new SDSAttributesFinderFeature(conn, fileid).find(test).getSize());
assertEquals(content.length, counter.getRecv(), 0L);
assertEquals(content.length, counter.getSent(), 0L);
assertTrue(failed.get());
new SDSDeleteFeature(conn, fileid).delete(Arrays.asList(test, room), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of ch.cyberduck.core.sds.SDSSession in project cyberduck by iterate-ch.
the class SDSSingleTransferWorkerTest method testTransferredSizeRepeatFailureOnClose.
@Test
public void testTransferredSizeRepeatFailureOnClose() throws Exception {
final SDSNodeIdProvider fileid = new SDSNodeIdProvider(session);
final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
// chunk size 32768
final byte[] content = new byte[98305];
new Random().nextBytes(content);
final OutputStream out = local.getOutputStream(false);
IOUtils.write(content, out);
out.close();
final AtomicBoolean failed = new AtomicBoolean();
final SDSSession conn = new SDSSession(session.getHost().withCredentials(new Credentials(System.getProperties().getProperty("sds.user"), System.getProperties().getProperty("sds.key"))), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {
@Override
@SuppressWarnings("unchecked")
public <T> T _getFeature(final Class<T> type) {
if (type == Upload.class) {
return (T) new DefaultUploadFeature<>(new SDSMultipartWriteFeature(this, fileid) {
@Override
public HttpResponseOutputStream<Node> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
final HttpResponseOutputStream<Node> proxy = super.write(file, status, callback);
if (failed.get()) {
// Second attempt successful
return proxy;
}
return new HttpResponseOutputStream<Node>(new CountingOutputStream(proxy) {
@Override
public void close() throws IOException {
if (!failed.get()) {
failed.set(true);
throw new SocketTimeoutException();
}
super.close();
}
}, new SDSAttributesAdapter(session), status) {
@Override
public Node getStatus() throws BackgroundException {
return proxy.getStatus();
}
};
}
});
}
return super._getFeature(type);
}
};
conn.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
conn.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
final Path room = new SDSDirectoryFeature(conn, fileid).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory, Path.Type.volume)), new TransferStatus());
final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final Transfer t = new UploadTransfer(session.getHost(), test, local);
final BytecountStreamListener counter = new BytecountStreamListener();
assertTrue(new SingleTransferWorker(conn, conn, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledProgressListener(), counter, new DisabledLoginCallback(), new DisabledNotificationService()) {
}.run(conn));
local.delete();
assertTrue(t.isComplete());
assertEquals(98305L, counter.getSent(), 0L);
assertTrue(failed.get());
assertEquals(98305L, new SDSAttributesFinderFeature(conn, fileid).find(test).getSize());
new SDSDeleteFeature(conn, fileid).delete(Arrays.asList(test, room), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of ch.cyberduck.core.sds.SDSSession in project cyberduck by iterate-ch.
the class TripleCryptEncryptingInputStreamTest method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws Exception {
final byte[] content = RandomUtils.nextBytes(1024 * 1024 + 1);
final ByteArrayInputStream plain = new ByteArrayInputStream(content);
final PlainFileKey key = Crypto.generateFileKey(PlainFileKey.Version.AES256GCM);
final SDSSession session = new SDSSession(new Host(new TestProtocol()), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {
@Override
public SDSApiClient getClient() {
return new SDSApiClient(new MockHttpClient());
}
};
final TransferStatus status = new TransferStatus();
final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeValue(out, TripleCryptConverter.toSwaggerFileKey(key));
status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
final TripleCryptEncryptingInputStream encryptInputStream = new TripleCryptEncryptingInputStream(session, plain, Crypto.createFileEncryptionCipher(key), status);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
new StreamCopier(StreamCancelation.noop, StreamProgress.noop).withLimit((long) content.length).withChunksize(32768).transfer(encryptInputStream, os);
encryptInputStream.close();
out.close();
final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
final FileKey fileKey = reader.readValue(status.getFilekey().array());
assertNotNull(fileKey.getTag());
final TripleCryptDecryptingInputStream cryptInputStream = new TripleCryptDecryptingInputStream(is, Crypto.createFileDecryptionCipher(TripleCryptConverter.toCryptoPlainFileKey(fileKey)), CryptoUtils.stringToByteArray(fileKey.getTag()));
final byte[] compare = new byte[content.length];
IOUtils.read(cryptInputStream, compare);
assertArrayEquals(content, compare);
}
Aggregations