11/*
22 * Copyright (c) 2010 Google Inc.
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5- * use this file except in compliance with the License. You may obtain a copy of
6- * the License at
7- *
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+ * in compliance with the License. You may obtain a copy of the License at
6+ *
87 * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13- * License for the specific language governing permissions and limitations under
8+ *
9+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+ * or implied. See the License for the specific language governing permissions and limitations under
1412 * the License.
1513 */
1614
1715package com .google .api .client .googleapis .auth .storage ;
1816
17+ import com .google .api .client .auth .HmacSha ;
1918import com .google .api .client .googleapis .GoogleHeaders ;
2019import com .google .api .client .googleapis .GoogleTransport ;
2120import com .google .api .client .http .HttpExecuteIntercepter ;
2524
2625import junit .framework .TestCase ;
2726
28- import java .io .IOException ;
2927import java .util .Arrays ;
3028
3129/**
3230 * Tests {@link GoogleStorageAuthentication}.
33- *
31+ *
3432 * @author Yaniv Inbar
3533 */
3634public class GoogleStorageAuthenticationTest extends TestCase {
3735
38- public GoogleStorageAuthenticationTest () {
39- }
36+ private static final String ACCESS_KEY = "GOOGTS7C7FUP3AIRVJTE" ;
37+ private static final String SECRET = "abc" ;
4038
4139 public GoogleStorageAuthenticationTest (String name ) {
4240 super (name );
4341 }
4442
45- public void test () throws IOException {
43+ public void test () throws Exception {
44+ subtest ("http://travel-maps.commondatastorage.googleapis.com/europe/france/paris.jpg" ,
45+ "PUT\n " + "\n " + "image/jpg\n " + "Mon, 15 Feb 2010 21:30:39 GMT\n "
46+ + "x-goog-acl:public-read\n " + "x-goog-meta-reviewer:bob,jane\n "
47+ + "/travel-maps/europe/france/paris.jpg" );
48+ subtest ("http://travel-maps.commondatastorage.googleapis.com/europe/france/paris.jpg?acl" ,
49+ "PUT\n " + "\n " + "image/jpg\n " + "Mon, 15 Feb 2010 21:30:39 GMT\n "
50+ + "x-goog-acl:public-read\n " + "x-goog-meta-reviewer:bob,jane\n "
51+ + "/travel-maps/europe/france/paris.jpg?acl" );
52+ subtest ("http://travel-maps.commondatastorage.googleapis.com?acl" ,
53+ "PUT\n " + "\n " + "image/jpg\n " + "Mon, 15 Feb 2010 21:30:39 GMT\n "
54+ + "x-goog-acl:public-read\n " + "x-goog-meta-reviewer:bob,jane\n " + "/travel-maps?acl" );
55+ subtest ("http://travel-maps.commondatastorage.googleapis.com" ,
56+ "PUT\n " + "\n " + "image/jpg\n " + "Mon, 15 Feb 2010 21:30:39 GMT\n "
57+ + "x-goog-acl:public-read\n " + "x-goog-meta-reviewer:bob,jane\n " + "/travel-maps" );
58+ subtest ("http://travel-maps.commondatastorage.googleapis.com/" ,
59+ "PUT\n " + "\n " + "image/jpg\n " + "Mon, 15 Feb 2010 21:30:39 GMT\n "
60+ + "x-goog-acl:public-read\n " + "x-goog-meta-reviewer:bob,jane\n " + "/travel-maps/" );
61+ }
62+
63+ private void subtest (String url , String messageToSign ) throws Exception {
4664 HttpTransport transport = GoogleTransport .create ();
47- GoogleStorageAuthentication .authorize (transport , "GOOGTS7C7FUP3AIRVJTE" ,
48- "abc" );
65+ GoogleStorageAuthentication .authorize (transport , ACCESS_KEY , SECRET );
4966 HttpExecuteIntercepter intercepter = transport .intercepters .get (1 );
5067 HttpRequest request = transport .buildPutRequest ();
51- request
52- .setUrl ("http://travel-maps.commondatastorage.googleapis.com/europe/france/paris.jpg" );
68+ request .setUrl (url );
5369 GoogleHeaders headers = (GoogleHeaders ) request .headers ;
5470 headers .date = "Mon, 15 Feb 2010 21:30:39 GMT" ;
5571 MockHttpContent content = new MockHttpContent ();
@@ -59,7 +75,7 @@ public void test() throws IOException {
5975 headers .googAcl = "public-read" ;
6076 headers .set ("x-goog-meta-reviewer" , Arrays .asList ("bob" , "jane" ));
6177 intercepter .intercept (request );
62- assertEquals ("GOOG1 GOOGTS7C7FUP3AIRVJTE:ovyTUuOaD+E6l/Xu+eOAhZ/8LKk=" ,
78+ assertEquals (messageToSign , "GOOG1 " + ACCESS_KEY + ":" + HmacSha . sign ( SECRET , messageToSign ) ,
6379 request .headers .authorization );
6480 }
6581}
0 commit comments