Skip to content

Commit

Permalink
Add test for Storage.signUrl with object names starting with /
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed May 16, 2016
1 parent bf2f213 commit 1c80edb
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,36 @@ public void testSignUrl() throws NoSuchAlgorithmException, InvalidKeyException,
URLDecoder.decode(signature, UTF_8.name()))));
}

@Test
public void testSignUrlLeadingSlash() throws NoSuchAlgorithmException, InvalidKeyException,
SignatureException, UnsupportedEncodingException {
String blobName = "/b1";
EasyMock.replay(storageRpcMock);
ServiceAccountAuthCredentials authCredentials =
ServiceAccountAuthCredentials.createFor(ACCOUNT, privateKey);
storage = options.toBuilder().authCredentials(authCredentials).build().service();
URL url = storage.signUrl(BlobInfo.builder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS);
String stringUrl = url.toString();
String expectedUrl =
new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1).append(blobName)
.append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=").append(42L + 1209600)
.append("&Signature=").toString();
System.out.println(stringUrl);
System.out.println(expectedUrl);
assertTrue(stringUrl.startsWith(expectedUrl));
String signature = stringUrl.substring(expectedUrl.length());

StringBuilder signedMessageBuilder = new StringBuilder();
signedMessageBuilder.append(HttpMethod.GET).append('\n').append('\n').append('\n')
.append(42L + 1209600).append('\n').append("/").append(BUCKET_NAME1).append(blobName);

Signature signer = Signature.getInstance("SHA256withRSA");
signer.initVerify(publicKey);
signer.update(signedMessageBuilder.toString().getBytes(UTF_8));
assertTrue(signer.verify(BaseEncoding.base64().decode(
URLDecoder.decode(signature, UTF_8.name()))));
}

@Test
public void testSignUrlWithOptions() throws NoSuchAlgorithmException, InvalidKeyException,
SignatureException, UnsupportedEncodingException {
Expand Down

0 comments on commit 1c80edb

Please sign in to comment.