github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/misc/amazon/s3/auth_test.go (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 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, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package s3 18 19 import ( 20 "bufio" 21 "fmt" 22 "net/http" 23 "strings" 24 "testing" 25 ) 26 27 type reqAndExpected struct { 28 req, expected string 29 } 30 31 func req(s string) *http.Request { 32 req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(s))) 33 if err != nil { 34 panic(fmt.Sprintf("bad request in test: %q (error: %v)", req, err)) 35 } 36 return req 37 } 38 39 func TestStringToSign(t *testing.T) { 40 var a Auth 41 tests := []reqAndExpected{ 42 {`GET /photos/puppy.jpg HTTP/1.1 43 Host: johnsmith.s3.amazonaws.com 44 Date: Tue, 27 Mar 2007 19:36:42 +0000 45 46 `, 47 "GET\n\n\nTue, 27 Mar 2007 19:36:42 +0000\n/johnsmith/photos/puppy.jpg"}, 48 {`PUT /photos/puppy.jpg HTTP/1.1 49 Content-Type: image/jpeg 50 Content-Length: 94328 51 Host: johnsmith.s3.amazonaws.com 52 Date: Tue, 27 Mar 2007 21:15:45 +0000 53 54 `, 55 "PUT\n\nimage/jpeg\nTue, 27 Mar 2007 21:15:45 +0000\n/johnsmith/photos/puppy.jpg"}, 56 {`GET /?prefix=photos&max-keys=50&marker=puppy HTTP/1.1 57 User-Agent: Mozilla/5.0 58 Host: johnsmith.s3.amazonaws.com 59 Date: Tue, 27 Mar 2007 19:42:41 +0000 60 61 `, 62 "GET\n\n\nTue, 27 Mar 2007 19:42:41 +0000\n/johnsmith/"}, 63 {`DELETE /johnsmith/photos/puppy.jpg HTTP/1.1 64 User-Agent: dotnet 65 Host: s3.amazonaws.com 66 Date: Tue, 27 Mar 2007 21:20:27 +0000 67 x-amz-date: Tue, 27 Mar 2007 21:20:26 +0000 68 69 `, 70 "DELETE\n\n\n\nx-amz-date:Tue, 27 Mar 2007 21:20:26 +0000\n/johnsmith/photos/puppy.jpg"}, 71 {`PUT /db-backup.dat.gz HTTP/1.1 72 User-Agent: curl/7.15.5 73 Host: static.johnsmith.net:8080 74 Date: Tue, 27 Mar 2007 21:06:08 +0000 75 x-amz-acl: public-read 76 content-type: application/x-download 77 Content-MD5: 4gJE4saaMU4BqNR0kLY+lw== 78 X-Amz-Meta-ReviewedBy: joe@johnsmith.net 79 X-Amz-Meta-ReviewedBy: jane@johnsmith.net 80 X-Amz-Meta-FileChecksum: 0x02661779 81 X-Amz-Meta-ChecksumAlgorithm: crc32 82 Content-Disposition: attachment; filename=database.dat 83 Content-Encoding: gzip 84 Content-Length: 5913339 85 86 `, 87 "PUT\n4gJE4saaMU4BqNR0kLY+lw==\napplication/x-download\nTue, 27 Mar 2007 21:06:08 +0000\nx-amz-acl:public-read\nx-amz-meta-checksumalgorithm:crc32\nx-amz-meta-filechecksum:0x02661779\nx-amz-meta-reviewedby:joe@johnsmith.net,jane@johnsmith.net\n/static.johnsmith.net/db-backup.dat.gz"}, 88 } 89 for idx, test := range tests { 90 got := a.stringToSign(req(test.req)) 91 if got != test.expected { 92 t.Errorf("test %d: expected %q", idx, test.expected) 93 t.Errorf("test %d: got %q", idx, got) 94 } 95 } 96 } 97 98 func TestBucketFromHostname(t *testing.T) { 99 var a Auth 100 tests := []reqAndExpected{ 101 {"GET / HTTP/1.0\n\n", ""}, 102 {"GET / HTTP/1.0\nHost: s3.amazonaws.com\n\n", ""}, 103 {"GET / HTTP/1.0\nHost: foo.s3.amazonaws.com\n\n", "foo"}, 104 {"GET / HTTP/1.0\nHost: foo.com:123\n\n", "foo.com"}, 105 {"GET / HTTP/1.0\nHost: bar.com\n\n", "bar.com"}, 106 } 107 for idx, test := range tests { 108 got := a.bucketFromHostname(req(test.req)) 109 if got != test.expected { 110 t.Errorf("test %d: expected %q; got %q", idx, test.expected, got) 111 } 112 } 113 } 114 115 func TestSignRequest(t *testing.T) { 116 r := req("GET /foo HTTP/1.1\n\n") 117 auth := &Auth{AccessKey: "key", SecretAccessKey: "secretkey"} 118 auth.SignRequest(r) 119 if r.Header.Get("Date") == "" { 120 t.Error("expected a Date set") 121 } 122 r.Header.Set("Date", "Sat, 02 Apr 2011 04:23:52 GMT") 123 auth.SignRequest(r) 124 if e, g := r.Header.Get("Authorization"), "AWS key:kHpCR/N7Rw3PwRlDd8+5X40CFVc="; e != g { 125 t.Errorf("got header %q; expected %q", g, e) 126 } 127 } 128 129 func TestHasDotSuffix(t *testing.T) { 130 if !hasDotSuffix("foo.com", "com") { 131 t.Error() 132 } 133 if hasDotSuffix("foocom", "com") { 134 t.Error() 135 } 136 if hasDotSuffix("com", "com") { 137 t.Error() 138 } 139 }