@@ -1155,6 +1155,36 @@ public void testSignUrl() throws NoSuchAlgorithmException, InvalidKeyException,
11551155 URLDecoder .decode (signature , UTF_8 .name ()))));
11561156 }
11571157
1158+ @ Test
1159+ public void testSignUrlLeadingSlash () throws NoSuchAlgorithmException , InvalidKeyException ,
1160+ SignatureException , UnsupportedEncodingException {
1161+ String blobName = "/b1" ;
1162+ EasyMock .replay (storageRpcMock );
1163+ ServiceAccountAuthCredentials authCredentials =
1164+ ServiceAccountAuthCredentials .createFor (ACCOUNT , privateKey );
1165+ storage = options .toBuilder ().authCredentials (authCredentials ).build ().service ();
1166+ URL url = storage .signUrl (BlobInfo .builder (BUCKET_NAME1 , blobName ).build (), 14 , TimeUnit .DAYS );
1167+ String stringUrl = url .toString ();
1168+ String expectedUrl =
1169+ new StringBuilder ("https://storage.googleapis.com/" ).append (BUCKET_NAME1 ).append (blobName )
1170+ .append ("?GoogleAccessId=" ).append (ACCOUNT ).append ("&Expires=" ).append (42L + 1209600 )
1171+ .append ("&Signature=" ).toString ();
1172+ System .out .println (stringUrl );
1173+ System .out .println (expectedUrl );
1174+ assertTrue (stringUrl .startsWith (expectedUrl ));
1175+ String signature = stringUrl .substring (expectedUrl .length ());
1176+
1177+ StringBuilder signedMessageBuilder = new StringBuilder ();
1178+ signedMessageBuilder .append (HttpMethod .GET ).append ('\n' ).append ('\n' ).append ('\n' )
1179+ .append (42L + 1209600 ).append ('\n' ).append ("/" ).append (BUCKET_NAME1 ).append (blobName );
1180+
1181+ Signature signer = Signature .getInstance ("SHA256withRSA" );
1182+ signer .initVerify (publicKey );
1183+ signer .update (signedMessageBuilder .toString ().getBytes (UTF_8 ));
1184+ assertTrue (signer .verify (BaseEncoding .base64 ().decode (
1185+ URLDecoder .decode (signature , UTF_8 .name ()))));
1186+ }
1187+
11581188 @ Test
11591189 public void testSignUrlWithOptions () throws NoSuchAlgorithmException , InvalidKeyException ,
11601190 SignatureException , UnsupportedEncodingException {
0 commit comments