Search in sources :

Example 1 with MockHttpClient

use of com.google.api.client.testing.http.apache.MockHttpClient 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);
}
Also used : PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DisabledX509TrustManager(ch.cyberduck.core.ssl.DisabledX509TrustManager) TestProtocol(ch.cyberduck.core.TestProtocol) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Host(ch.cyberduck.core.Host) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) SDSApiClient(ch.cyberduck.core.sds.SDSApiClient) SDSSession(ch.cyberduck.core.sds.SDSSession) ByteArrayInputStream(java.io.ByteArrayInputStream) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) DefaultX509KeyManager(ch.cyberduck.core.ssl.DefaultX509KeyManager) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) Test(org.junit.Test)

Example 2 with MockHttpClient

use of com.google.api.client.testing.http.apache.MockHttpClient 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);
}
Also used : PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DisabledX509TrustManager(ch.cyberduck.core.ssl.DisabledX509TrustManager) TestProtocol(ch.cyberduck.core.TestProtocol) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Host(ch.cyberduck.core.Host) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) SDSApiClient(ch.cyberduck.core.sds.SDSApiClient) SDSSession(ch.cyberduck.core.sds.SDSSession) ByteArrayInputStream(java.io.ByteArrayInputStream) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) DefaultX509KeyManager(ch.cyberduck.core.ssl.DefaultX509KeyManager) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) StreamCopier(ch.cyberduck.core.io.StreamCopier) Test(org.junit.Test)

Example 3 with MockHttpClient

use of com.google.api.client.testing.http.apache.MockHttpClient in project google-http-java-client by googleapis.

the class ApacheHttpRequestTest method testChunked.

@Test
public void testChunked() throws Exception {
    byte[] buf = new byte[300];
    Arrays.fill(buf, (byte) ' ');
    HttpExtensionMethod base = new HttpExtensionMethod("POST", "http://www.google.com");
    ApacheHttpRequest request = new ApacheHttpRequest(new MockHttpClient(), base);
    HttpContent content = new InputStreamContent("text/plain", new ByteArrayInputStream(buf));
    request.setStreamingContent(content);
    request.execute();
    assertTrue(base.getEntity().isChunked());
    assertEquals(-1, base.getEntity().getContentLength());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamContent(com.google.api.client.http.InputStreamContent) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) HttpContent(com.google.api.client.http.HttpContent) Test(org.junit.Test)

Example 4 with MockHttpClient

use of com.google.api.client.testing.http.apache.MockHttpClient in project google-http-java-client by googleapis.

the class ApacheHttpTransportTest method testRequestsWithContent.

@Test
public void testRequestsWithContent() throws IOException {
    HttpClient mockClient = new MockHttpClient() {

        @Override
        public CloseableHttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
            return new MockHttpResponse();
        }
    };
    ApacheHttpTransport transport = new ApacheHttpTransport(mockClient);
    // Test GET.
    subtestUnsupportedRequestsWithContent(transport.buildRequest("GET", "http://www.test.url"), "GET");
    // Test DELETE.
    subtestUnsupportedRequestsWithContent(transport.buildRequest("DELETE", "http://www.test.url"), "DELETE");
    // Test HEAD.
    subtestUnsupportedRequestsWithContent(transport.buildRequest("HEAD", "http://www.test.url"), "HEAD");
    // Test PATCH.
    execute(transport.buildRequest("PATCH", "http://www.test.url"));
    // Test PUT.
    execute(transport.buildRequest("PUT", "http://www.test.url"));
    // Test POST.
    execute(transport.buildRequest("POST", "http://www.test.url"));
    // Test PATCH.
    execute(transport.buildRequest("PATCH", "http://www.test.url"));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpClient(org.apache.http.client.HttpClient) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) Test(org.junit.Test)

Example 5 with MockHttpClient

use of com.google.api.client.testing.http.apache.MockHttpClient 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);
}
Also used : PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DisabledX509TrustManager(ch.cyberduck.core.ssl.DisabledX509TrustManager) TestProtocol(ch.cyberduck.core.TestProtocol) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Host(ch.cyberduck.core.Host) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) SDSApiClient(ch.cyberduck.core.sds.SDSApiClient) SDSSession(ch.cyberduck.core.sds.SDSSession) ByteArrayInputStream(java.io.ByteArrayInputStream) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) DefaultX509KeyManager(ch.cyberduck.core.ssl.DefaultX509KeyManager) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) StreamCopier(ch.cyberduck.core.io.StreamCopier) Test(org.junit.Test)

Aggregations

MockHttpClient (com.google.api.client.testing.http.apache.MockHttpClient)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Host (ch.cyberduck.core.Host)3 TestProtocol (ch.cyberduck.core.TestProtocol)3 SDSApiClient (ch.cyberduck.core.sds.SDSApiClient)3 SDSSession (ch.cyberduck.core.sds.SDSSession)3 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)3 DefaultX509KeyManager (ch.cyberduck.core.ssl.DefaultX509KeyManager)3 DisabledX509TrustManager (ch.cyberduck.core.ssl.DisabledX509TrustManager)3 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)3 PlainFileKey (com.dracoon.sdk.crypto.model.PlainFileKey)3 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 StreamCopier (ch.cyberduck.core.io.StreamCopier)2 HttpContent (com.google.api.client.http.HttpContent)2 ByteArrayContent (com.google.api.client.http.ByteArrayContent)1 InputStreamContent (com.google.api.client.http.InputStreamContent)1 HttpClient (org.apache.http.client.HttpClient)1