/
V4PostPolicyTest.java
243 lines (211 loc) · 8.73 KB
/
V4PostPolicyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright 2020 Google LLC
*
* 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 com.google.cloud.storage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.google.api.core.ApiClock;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.conformance.storage.v1.PolicyConditions;
import com.google.cloud.conformance.storage.v1.PolicyInput;
import com.google.cloud.conformance.storage.v1.PostPolicyV4Test;
import com.google.cloud.conformance.storage.v1.TestFile;
import com.google.cloud.conformance.storage.v1.UrlStyle;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.base.Charsets;
import com.google.common.io.BaseEncoding;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class V4PostPolicyTest {
private static final String SERVICE_ACCOUNT_JSON_RESOURCE =
"com/google/cloud/conformance/storage/v1/test_service_account.not-a-test.json";
private static final String TEST_DATA_JSON_RESOURCE =
"com/google/cloud/conformance/storage/v1/v4_signatures.json";
private static class FakeClock implements ApiClock {
private final AtomicLong currentNanoTime;
public FakeClock(Timestamp timestamp) {
this.currentNanoTime =
new AtomicLong(
TimeUnit.NANOSECONDS.convert(timestamp.getSeconds(), TimeUnit.SECONDS)
+ timestamp.getNanos());
}
public long nanoTime() {
return this.currentNanoTime.get();
}
public long millisTime() {
return TimeUnit.MILLISECONDS.convert(this.nanoTime(), TimeUnit.NANOSECONDS);
}
}
@Rule public TestName testName = new TestName();
private final PostPolicyV4Test testData;
private final ServiceAccountCredentials serviceAccountCredentials;
/**
* @param testData the serialized test data representing the test case.
* @param serviceAccountCredentials The credentials to use in this test.
* @param description Not used by the test, but used by the parameterized test runner as the name
* of the test.
*/
public V4PostPolicyTest(
PostPolicyV4Test testData,
ServiceAccountCredentials serviceAccountCredentials,
@SuppressWarnings("unused") String description) {
this.testData = testData;
this.serviceAccountCredentials = serviceAccountCredentials;
}
@Test
public void test() {
Storage storage =
RemoteStorageHelper.create()
.getOptions()
.toBuilder()
.setCredentials(serviceAccountCredentials)
.setClock(new FakeClock(testData.getPolicyInput().getTimestamp()))
.build()
.getService();
BlobInfo blob =
BlobInfo.newBuilder(
testData.getPolicyInput().getBucket(), testData.getPolicyInput().getObject())
.build();
PolicyInput policyInput = testData.getPolicyInput();
PostPolicyV4.PostConditionsV4.Builder builder = PostPolicyV4.PostConditionsV4.newBuilder();
Map<String, String> fields = policyInput.getFieldsMap();
PolicyConditions conditions = policyInput.getConditions();
if (conditions != null) {
if (!conditions.getStartsWithList().isEmpty()) {
builder.addCustomCondition(
PostPolicyV4.ConditionV4Type.STARTS_WITH,
conditions.getStartsWith(0).replace("$", ""),
conditions.getStartsWith(1));
}
if (!conditions.getContentLengthRangeList().isEmpty()) {
builder.addContentLengthRangeCondition(
conditions.getContentLengthRange(0), conditions.getContentLengthRange(1));
}
}
PostPolicyV4.PostFieldsV4 v4Fields = PostPolicyV4.PostFieldsV4.of(fields);
Storage.PostPolicyV4Option style = Storage.PostPolicyV4Option.withPathStyle();
if (policyInput.getUrlStyle().equals(UrlStyle.VIRTUAL_HOSTED_STYLE)) {
style = Storage.PostPolicyV4Option.withVirtualHostedStyle();
} else if (policyInput.getUrlStyle().equals(UrlStyle.PATH_STYLE)) {
style = Storage.PostPolicyV4Option.withPathStyle();
} else if (policyInput.getUrlStyle().equals(UrlStyle.BUCKET_BOUND_HOSTNAME)) {
style =
Storage.PostPolicyV4Option.withBucketBoundHostname(
policyInput.getBucketBoundHostname(),
Storage.UriScheme.valueOf(policyInput.getScheme().toUpperCase()));
}
PostPolicyV4 policy =
storage.generateSignedPostPolicyV4(
blob,
testData.getPolicyInput().getExpiration(),
TimeUnit.SECONDS,
v4Fields,
builder.build(),
style);
String expectedPolicy = testData.getPolicyOutput().getExpectedDecodedPolicy();
StringBuilder escapedPolicy = new StringBuilder();
// Java automatically unescapes the unicode escapes in the conformance tests, so we need to
// manually re-escape them
char[] expectedPolicyArray = expectedPolicy.toCharArray();
for (int i = 0; i < expectedPolicyArray.length; i++) {
char c = expectedPolicyArray[i];
if (c >= 128) {
escapedPolicy.append(String.format("\\u%04x", (int) c));
} else {
switch (c) {
case '\\':
// quotes aren't unescaped, so leave any "\"" found alone
if (expectedPolicyArray[i + 1] == '"') {
escapedPolicy.append("\\");
} else {
escapedPolicy.append("\\\\");
}
break;
case '\b':
escapedPolicy.append("\\b");
break;
case '\f':
escapedPolicy.append("\\f");
break;
case '\n':
escapedPolicy.append("\\n");
break;
case '\r':
escapedPolicy.append("\\r");
break;
case '\t':
escapedPolicy.append("\\t");
break;
case '\u000b':
escapedPolicy.append("\\v");
break;
default:
escapedPolicy.append(c);
}
}
}
assertEquals(testData.getPolicyOutput().getFieldsMap(), policy.getFields());
assertEquals(
escapedPolicy.toString(),
new String(BaseEncoding.base64().decode(policy.getFields().get("policy"))));
assertEquals(testData.getPolicyOutput().getUrl(), policy.getUrl());
}
/**
* Loads all of the tests and return a {@code Collection<Object[]>} representing the set of tests.
* Each entry in the returned collection is the set of parameters to the constructor of this test
* class.
*
* <p>The results of this method will then be run by JUnit's Parameterized test runner
*/
@Parameters(name = "{2}")
public static Collection<Object[]> testCases() throws IOException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream credentialsStream = cl.getResourceAsStream(SERVICE_ACCOUNT_JSON_RESOURCE);
assertNotNull(
String.format("Unable to load service account json: %s", SERVICE_ACCOUNT_JSON_RESOURCE),
credentialsStream);
InputStream dataJson = cl.getResourceAsStream(TEST_DATA_JSON_RESOURCE);
assertNotNull(
String.format("Unable to load test definition: %s", TEST_DATA_JSON_RESOURCE), dataJson);
ServiceAccountCredentials serviceAccountCredentials =
ServiceAccountCredentials.fromStream(credentialsStream);
InputStreamReader reader = new InputStreamReader(dataJson, Charsets.UTF_8);
TestFile.Builder testBuilder = TestFile.newBuilder();
JsonFormat.parser().merge(reader, testBuilder);
TestFile testFile = testBuilder.build();
List<PostPolicyV4Test> tests = testFile.getPostPolicyV4TestsList();
ArrayList<Object[]> data = new ArrayList<>(tests.size());
for (PostPolicyV4Test test : tests) {
data.add(new Object[] {test, serviceAccountCredentials, test.getDescription()});
}
return data;
}
}