Skip to content

Commit c68443a

Browse files
authored
Integration test for S3 SDK (#746)
* Integration test for S3 SDK
1 parent 9be63aa commit c68443a

File tree

58 files changed

+10556
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+10556
-0
lines changed

CircleciScripts/run_integrationtest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"aws-android-sdk-mobile-client",
2121
"aws-android-sdk-rekognition-test",
2222
"aws-android-sdk-polly-test",
23+
"aws-android-sdk-s3-test",
2324
"aws-android-sdk-sdb-test",
2425
"aws-android-sdk-sqs-test",
2526
"aws-android-sdk-sns-test",

aws-android-sdk-s3-test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 27
5+
6+
7+
8+
defaultConfig {
9+
minSdkVersion 21
10+
targetSdkVersion 27
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
25+
useLibrary 'org.apache.http.legacy'
26+
packagingOptions {
27+
exclude 'META-INF/DEPENDENCIES'
28+
}
29+
}
30+
31+
32+
dependencies {
33+
implementation fileTree(dir: 'libs', include: ['*.jar'])
34+
api (project(":aws-android-sdk-s3")){
35+
exclude group: "com.google.android", module: "android"
36+
}
37+
api (project(":aws-android-sdk-sns")){
38+
exclude group: "com.google.android", module: "android"
39+
}
40+
testImplementation 'junit:junit:4.12'
41+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
42+
androidTestImplementation project(":aws-android-sdk-testutils")
43+
androidTestImplementation 'org.apache.commons:commons-io:1.3.2'
44+
androidTestImplementation 'org.robolectric:robolectric:2.4'
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
11+
* OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
* License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.amazonaws.auth.policy;
17+
18+
import com.amazonaws.auth.policy.actions.S3Actions;
19+
import com.amazonaws.auth.policy.conditions.ConditionFactory;
20+
import com.amazonaws.auth.policy.conditions.S3ConditionFactory;
21+
import com.amazonaws.auth.policy.conditions.StringCondition;
22+
import com.amazonaws.auth.policy.conditions.StringCondition.StringComparisonType;
23+
import com.amazonaws.auth.policy.resources.S3BucketResource;
24+
import com.amazonaws.services.s3.S3IntegrationTestBase;
25+
import com.amazonaws.services.s3.model.CannedAccessControlList;
26+
27+
import org.junit.After;
28+
import org.junit.Test;
29+
30+
/**
31+
* Integration tests for the service specific access control policy code
32+
* provided by the S3 client.
33+
*/
34+
public class S3PolicyIntegrationTest extends S3IntegrationTestBase {
35+
36+
/** Name of the bucket created by this test */
37+
private final String bucketName = "android-custom-bucket-policy-integ-test-"
38+
+ System.currentTimeMillis();
39+
40+
/** Release all allocated resources */
41+
@After
42+
public void tearDown() {
43+
s3.deleteBucket(bucketName);
44+
}
45+
46+
/**
47+
* Tests that the S3 specific access control policy code works as expected.
48+
*/
49+
@Test
50+
public void testPolicies() throws Exception {
51+
s3.createBucket(bucketName);
52+
53+
Policy policy = new Policy().withStatements(
54+
new Statement(Statement.Effect.Allow)
55+
.withActions(S3Actions.AllS3Actions)
56+
.withPrincipals(Principal.AllUsers)
57+
.withResources(new S3BucketResource(bucketName))
58+
.withConditions(
59+
S3ConditionFactory
60+
.newCannedACLCondition(CannedAccessControlList.Private)));
61+
s3.setBucketPolicy(bucketName, policy.toJson());
62+
63+
policy = new Policy()
64+
.withStatements(
65+
new Statement(Statement.Effect.Allow)
66+
.withActions(S3Actions.AllS3Actions)
67+
.withPrincipals(Principal.AllUsers)
68+
.withResources(new S3BucketResource(bucketName))
69+
.withConditions(
70+
S3ConditionFactory
71+
.newCannedACLCondition(CannedAccessControlList.AuthenticatedRead),
72+
new StringCondition(StringComparisonType.StringEquals,
73+
ConditionFactory.USER_AGENT_CONDITION_KEY,
74+
"foo*")),
75+
new Statement(Statement.Effect.Allow)
76+
.withActions(S3Actions.ListObjectVersions)
77+
.withResources(new S3BucketResource(bucketName))
78+
.withPrincipals(Principal.AllUsers));
79+
s3.setBucketPolicy(bucketName, policy.toJson());
80+
}
81+
82+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
11+
* OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
* License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.amazonaws.services.s3;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
21+
import com.amazonaws.AmazonClientException;
22+
import com.amazonaws.regions.Region;
23+
import com.amazonaws.regions.Regions;
24+
import com.amazonaws.services.s3.model.AmazonS3Exception;
25+
import com.amazonaws.services.s3.model.BucketAccelerateConfiguration;
26+
import com.amazonaws.services.s3.model.BucketAccelerateStatus;
27+
import com.amazonaws.services.s3.model.GetObjectRequest;
28+
import com.amazonaws.services.s3.model.S3Object;
29+
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
34+
import java.io.File;
35+
import java.util.Date;
36+
37+
public class AccelerateIntegrationTest extends S3IntegrationTestBase {
38+
39+
/** The name of the bucket these tests will create, test on and delete */
40+
private static final String BUCKET = "acceleration-integration-test-" + new Date().getTime();
41+
42+
/** The key of the object these tests will create, test on and delete */
43+
private static final String KEY = "key";
44+
private static AmazonS3Client accelerateS3;
45+
46+
@Before
47+
public void setupBucket() throws Exception {
48+
s3.createBucket(BUCKET);
49+
S3IntegrationTestBase.waitForBucketCreation(BUCKET);
50+
accelerateS3 = new AmazonS3Client(credentials, Region.getRegion(Regions.US_WEST_1));
51+
accelerateS3.setS3ClientOptions(
52+
S3ClientOptions.builder().setAccelerateModeEnabled(true).build());
53+
}
54+
55+
@After
56+
public void teardown() throws Exception {
57+
try {
58+
if (s3.doesObjectExist(BUCKET, KEY)) {
59+
s3.deleteObject(BUCKET, KEY);
60+
}
61+
} catch (AmazonClientException ace) {
62+
ace.printStackTrace();
63+
}
64+
65+
try {
66+
s3.deleteBucket(BUCKET);
67+
} catch (AmazonClientException ace) {
68+
ace.printStackTrace();
69+
}
70+
}
71+
72+
@Test
73+
public void testGetAndSetAcceleration() throws Exception {
74+
s3.setBucketAccelerateConfiguration(BUCKET,
75+
new BucketAccelerateConfiguration(BucketAccelerateStatus.Enabled));
76+
Thread.sleep(2500);
77+
assertEquals(BucketAccelerateStatus.Enabled.toString(),
78+
s3.getBucketAccelerateConfiguration(BUCKET).getStatus());
79+
80+
s3.setBucketAccelerateConfiguration(BUCKET,
81+
new BucketAccelerateConfiguration(BucketAccelerateStatus.Suspended));
82+
Thread.sleep(2500);
83+
assertEquals(BucketAccelerateStatus.Suspended.toString(),
84+
s3.getBucketAccelerateConfiguration(BUCKET).getStatus());
85+
}
86+
87+
@Test
88+
public void testSetAccelerationWithAccelerateClient() throws Exception {
89+
accelerateS3.setBucketAccelerateConfiguration(BUCKET,
90+
new BucketAccelerateConfiguration(BucketAccelerateStatus.Enabled));
91+
Thread.sleep(2500);
92+
assertEquals(BucketAccelerateStatus.Enabled.toString(),
93+
accelerateS3.getBucketAccelerateConfiguration(BUCKET).getStatus());
94+
}
95+
96+
@Test(expected = AmazonS3Exception.class)
97+
public void testAccessWithAccelerateClient() throws Exception {
98+
s3.setBucketAccelerateConfiguration(BUCKET,
99+
new BucketAccelerateConfiguration(BucketAccelerateStatus.Suspended));
100+
// it takes time to propagate the configuration
101+
Thread.sleep(2500);
102+
103+
try {
104+
accelerateS3.getBucketAccelerateConfiguration(BUCKET);
105+
} catch (AmazonS3Exception ase) {
106+
System.out.println(ase.toString());
107+
ase.printStackTrace();
108+
assertEquals("InvalidRequest", ase.getErrorCode());
109+
throw ase;
110+
}
111+
}
112+
113+
@Test
114+
public void testPutAndGetObjectWithAccelerateClient() throws Exception {
115+
accelerateS3.setBucketAccelerateConfiguration(BUCKET,
116+
new BucketAccelerateConfiguration(BucketAccelerateStatus.Enabled));
117+
long contentLength = 128 * 1024;
118+
File file = getRandomTempFile(KEY, contentLength);
119+
accelerateS3.putObject(BUCKET, KEY, file);
120+
GetObjectRequest req = new GetObjectRequest(BUCKET, KEY);
121+
File output = new File("output");
122+
output.deleteOnExit();
123+
S3Object obj = accelerateS3.getObject(req);
124+
assertNotNull(obj);
125+
assertFileEqualsStream(file, obj.getObjectContent());
126+
obj.getObjectContent().close();
127+
}
128+
}

0 commit comments

Comments
 (0)