Skip to content

Commit 01c5278

Browse files
committed
Add test for Storage.signUrl with object names starting with /
1 parent bf2f213 commit 01c5278

1 file changed

Lines changed: 40 additions & 17 deletions

File tree

gcloud-java-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,17 +1136,42 @@ public void testSignUrl() throws NoSuchAlgorithmException, InvalidKeyException,
11361136
storage = options.toBuilder().authCredentials(authCredentials).build().service();
11371137
URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS);
11381138
String stringUrl = url.toString();
1139-
String expectedUrl =
1140-
new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1).append("/")
1141-
.append(BLOB_NAME1).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=")
1142-
.append(42L + 1209600).append("&Signature=").toString();
1139+
String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1)
1140+
.append('/').append(BLOB_NAME1).append("?GoogleAccessId=").append(ACCOUNT)
1141+
.append("&Expires=").append(42L + 1209600).append("&Signature=").toString();
11431142
assertTrue(stringUrl.startsWith(expectedUrl));
11441143
String signature = stringUrl.substring(expectedUrl.length());
11451144

11461145
StringBuilder signedMessageBuilder = new StringBuilder();
1147-
signedMessageBuilder.append(HttpMethod.GET).append('\n').append('\n').append('\n')
1148-
.append(42L + 1209600).append('\n').append("/").append(BUCKET_NAME1).append("/")
1149-
.append(BLOB_NAME1);
1146+
signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600).append("\n/")
1147+
.append(BUCKET_NAME1).append('/').append(BLOB_NAME1);
1148+
1149+
Signature signer = Signature.getInstance("SHA256withRSA");
1150+
signer.initVerify(publicKey);
1151+
signer.update(signedMessageBuilder.toString().getBytes(UTF_8));
1152+
assertTrue(signer.verify(BaseEncoding.base64().decode(
1153+
URLDecoder.decode(signature, UTF_8.name()))));
1154+
}
1155+
1156+
@Test
1157+
public void testSignUrlLeadingSlash() throws NoSuchAlgorithmException, InvalidKeyException,
1158+
SignatureException, UnsupportedEncodingException {
1159+
String blobName = "/b1";
1160+
EasyMock.replay(storageRpcMock);
1161+
ServiceAccountAuthCredentials authCredentials =
1162+
ServiceAccountAuthCredentials.createFor(ACCOUNT, privateKey);
1163+
storage = options.toBuilder().authCredentials(authCredentials).build().service();
1164+
URL url = storage.signUrl(BlobInfo.builder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS);
1165+
String stringUrl = url.toString();
1166+
String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1)
1167+
.append(blobName).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=")
1168+
.append(42L + 1209600).append("&Signature=").toString();
1169+
assertTrue(stringUrl.startsWith(expectedUrl));
1170+
String signature = stringUrl.substring(expectedUrl.length());
1171+
1172+
StringBuilder signedMessageBuilder = new StringBuilder();
1173+
signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600).append("\n/")
1174+
.append(BUCKET_NAME1).append(blobName);
11501175

11511176
Signature signer = Signature.getInstance("SHA256withRSA");
11521177
signer.initVerify(publicKey);
@@ -1162,22 +1187,20 @@ public void testSignUrlWithOptions() throws NoSuchAlgorithmException, InvalidKey
11621187
ServiceAccountAuthCredentials authCredentials =
11631188
ServiceAccountAuthCredentials.createFor(ACCOUNT, privateKey);
11641189
storage = options.toBuilder().authCredentials(authCredentials).build().service();
1165-
URL url =
1166-
storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS,
1167-
Storage.SignUrlOption.httpMethod(HttpMethod.POST),
1168-
Storage.SignUrlOption.withContentType(), Storage.SignUrlOption.withMd5());
1190+
URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS,
1191+
Storage.SignUrlOption.httpMethod(HttpMethod.POST), Storage.SignUrlOption.withContentType(),
1192+
Storage.SignUrlOption.withMd5());
11691193
String stringUrl = url.toString();
1170-
String expectedUrl =
1171-
new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1).append("/")
1172-
.append(BLOB_NAME1).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=")
1173-
.append(42L + 1209600).append("&Signature=").toString();
1194+
String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1)
1195+
.append('/').append(BLOB_NAME1).append("?GoogleAccessId=").append(ACCOUNT)
1196+
.append("&Expires=").append(42L + 1209600).append("&Signature=").toString();
11741197
assertTrue(stringUrl.startsWith(expectedUrl));
11751198
String signature = stringUrl.substring(expectedUrl.length());
11761199

11771200
StringBuilder signedMessageBuilder = new StringBuilder();
11781201
signedMessageBuilder.append(HttpMethod.POST).append('\n').append(BLOB_INFO1.md5()).append('\n')
1179-
.append(BLOB_INFO1.contentType()).append('\n').append(42L + 1209600).append('\n')
1180-
.append("/").append(BUCKET_NAME1).append("/").append(BLOB_NAME1);
1202+
.append(BLOB_INFO1.contentType()).append('\n').append(42L + 1209600).append("\n/")
1203+
.append(BUCKET_NAME1).append('/').append(BLOB_NAME1);
11811204

11821205
Signature signer = Signature.getInstance("SHA256withRSA");
11831206
signer.initVerify(publicKey);

0 commit comments

Comments
 (0)