/*
 * Copyright 2014-2018 Andrew Gaul <andrew@gaul.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gaul.s3proxy;

import java.net.URI;
import java.util.Random;

import com.amazonaws.SDKGlobalConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.internal.SkipMd5CheckStrategy;

import org.jclouds.blobstore.BlobStoreContext;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public final class AwsSdkAnonymousTest {
    static {
        System.setProperty(
                SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY,
                "true");
        AwsSdkTest.disableSslVerification();
    }

    private URI s3Endpoint;
    private EndpointConfiguration s3EndpointConfig;
    private S3Proxy s3Proxy;
    private BlobStoreContext context;
    private String blobStoreType;
    private String containerName;
    private AWSCredentials awsCreds;
    private AmazonS3 client;
    private String servicePath;

    @Before
    public void setUp() throws Exception {
        TestUtils.S3ProxyLaunchInfo info = TestUtils.startS3Proxy(
                "s3proxy-anonymous.conf");
        awsCreds = new AnonymousAWSCredentials();
        context = info.getBlobStore().getContext();
        s3Proxy = info.getS3Proxy();
        s3Endpoint = info.getSecureEndpoint();
        servicePath = info.getServicePath();
        s3EndpointConfig = new EndpointConfiguration(
                s3Endpoint.toString() + servicePath, "us-east-1");
        client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .withEndpointConfiguration(s3EndpointConfig)
                .build();

        containerName = createRandomContainerName();
        info.getBlobStore().createContainerInLocation(null, containerName);

        blobStoreType = context.unwrap().getProviderMetadata().getId();
        if (Quirks.OPAQUE_ETAG.contains(blobStoreType)) {
            System.setProperty(
                    SkipMd5CheckStrategy
                            .DISABLE_GET_OBJECT_MD5_VALIDATION_PROPERTY,
                    "true");
            System.setProperty(
                    SkipMd5CheckStrategy
                            .DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY,
                    "true");
        }
    }

    @After
    public void tearDown() throws Exception {
        if (s3Proxy != null) {
            s3Proxy.stop();
        }
        if (context != null) {
            context.getBlobStore().deleteContainer(containerName);
            context.close();
        }
    }

    @Test
    public void testListBuckets() throws Exception {
        client.listBuckets();
    }

    private static String createRandomContainerName() {
        return "s3proxy-" + new Random().nextInt(Integer.MAX_VALUE);
    }
}

@@ -631,24 +631,111 @@
 l.s3proxy;%0A%0A
+import static org.assertj.core.api.Assertions.assertThat;%0A%0Aimport java.io.InputStream;%0A
 import java.
@@ -743,16 +743,16 @@
 et.URI;%0A
-
 import j
@@ -1201,16 +1201,160 @@
 trategy;
+%0Aimport com.amazonaws.services.s3.model.ObjectMetadata;%0Aimport com.amazonaws.services.s3.model.S3Object;%0Aimport com.google.common.io.ByteSource;
 %0A%0Aimport
@@ -1694,31 +1694,112 @@
 fication();%0A
-
     %7D%0A%0A
+    private static final ByteSource BYTE_SOURCE = ByteSource.wrap(new byte%5B1%5D);%0A%0A
     private 
@@ -3821,32 +3821,885 @@
 ckets();%0A    %7D%0A%0A
+    @Test%0A    public void testAwsV4SignatureChunkedAnonymous() throws Exception %7B%0A        client = AmazonS3ClientBuilder.standard()%0A            .withChunkedEncodingDisabled(false)%0A            .withEndpointConfiguration(s3EndpointConfig)%0A            .build();%0A%0A        ObjectMetadata metadata = new ObjectMetadata();%0A        metadata.setContentLength(BYTE_SOURCE.size());%0A        client.putObject(containerName, %22foo%22, BYTE_SOURCE.openStream(),%0A                metadata);%0A%0A        S3Object object = client.getObject(containerName, %22foo%22);%0A        assertThat(object.getObjectMetadata().getContentLength()).isEqualTo(%0A                BYTE_SOURCE.size());%0A        try (InputStream actual = object.getObjectContent();%0A            InputStream expected = BYTE_SOURCE.openStream()) %7B%0A            assertThat(actual).hasContentEqualTo(expected);%0A        %7D%0A    %7D%0A%0A
     private stat

Test for chunked encoding with anonymous access