| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2016, 2017, 2018 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 	"archive/zip" | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2018-03-16 04:27:16 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 	"crypto/md5" | 
					
						
							|  |  |  | 	"encoding/hex" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2016-09-23 14:06:45 +08:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2016-08-31 01:04:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 	jwtgo "github.com/dgrijalva/jwt-go" | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 	humanize "github.com/dustin/go-humanize" | 
					
						
							| 
									
										
										
										
											2020-01-31 10:59:22 +08:00
										 |  |  | 	xjwt "github.com/minio/minio/cmd/jwt" | 
					
						
							| 
									
										
										
										
											2017-10-22 13:30:34 +08:00
										 |  |  | 	"github.com/minio/minio/pkg/hash" | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-16 04:03:41 +08:00
										 |  |  | // Implement a dummy flush writer.
 | 
					
						
							|  |  |  | type flushWriter struct { | 
					
						
							|  |  |  | 	io.Writer | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Flush writer is a dummy writer compatible with http.Flusher and http.ResponseWriter.
 | 
					
						
							|  |  |  | func (f *flushWriter) Flush()                            {} | 
					
						
							|  |  |  | func (f *flushWriter) Write(b []byte) (n int, err error) { return f.Writer.Write(b) } | 
					
						
							|  |  |  | func (f *flushWriter) Header() http.Header               { return http.Header{} } | 
					
						
							|  |  |  | func (f *flushWriter) WriteHeader(code int)              {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newFlushWriter(writer io.Writer) http.ResponseWriter { | 
					
						
							|  |  |  | 	return &flushWriter{writer} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | // Tests private function writeWebErrorResponse.
 | 
					
						
							|  |  |  | func TestWriteWebErrorResponse(t *testing.T) { | 
					
						
							|  |  |  | 	var buffer bytes.Buffer | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		webErr     error | 
					
						
							|  |  |  | 		apiErrCode APIErrorCode | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// List of various errors and their corresponding API errors.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     StorageFull{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrStorageFull, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     BucketNotFound{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrNoSuchBucket, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     BucketNameInvalid{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrInvalidBucketName, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-10-22 13:30:34 +08:00
										 |  |  | 			webErr:     hash.BadDigest{}, | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 			apiErrCode: ErrBadDigest, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     IncompleteBody{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrIncompleteBody, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     ObjectExistsAsDirectory{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrObjectExistsAsDirectory, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     ObjectNotFound{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrNoSuchKey, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     ObjectNameInvalid{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrNoSuchKey, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     InsufficientWriteQuorum{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrWriteQuorum, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     InsufficientReadQuorum{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrReadQuorum, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-06-08 10:31:23 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			webErr:     NotImplemented{}, | 
					
						
							|  |  |  | 			apiErrCode: ErrNotImplemented, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Validate all the test cases.
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		writeWebErrorResponse(newFlushWriter(&buffer), testCase.webErr) | 
					
						
							|  |  |  | 		desc := getAPIError(testCase.apiErrCode).Description | 
					
						
							| 
									
										
										
										
											2017-06-08 10:31:23 +08:00
										 |  |  | 		if testCase.apiErrCode == ErrNotImplemented { | 
					
						
							|  |  |  | 			desc = "Functionality not implemented" | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 		recvDesc := buffer.Bytes() | 
					
						
							|  |  |  | 		// Check if the written desc is same as the one expected.
 | 
					
						
							|  |  |  | 		if !bytes.Equal(recvDesc, []byte(desc)) { | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 			t.Errorf("Test %d: Unexpected response, expecting %s, got %s", i+1, desc, buffer.String()) | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		buffer.Reset() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | // Authenticate and get JWT token - will be called before every webrpc handler invocation
 | 
					
						
							|  |  |  | func getWebRPCToken(apiRouter http.Handler, accessKey, secretKey string) (token string, err error) { | 
					
						
							| 
									
										
										
										
											2018-06-06 16:51:56 +08:00
										 |  |  | 	return authenticateWeb(accessKey, secretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper for calling Login Web Handler
 | 
					
						
							|  |  |  | func TestWebHandlerLogin(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testLoginWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testLoginWebHandler - Test login web handler
 | 
					
						
							|  |  |  | func testLoginWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// test cases with sample input and expected output.
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		username string | 
					
						
							|  |  |  | 		password string | 
					
						
							|  |  |  | 		success  bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"", "", false}, | 
					
						
							|  |  |  | 		{"azerty", "foo", false}, | 
					
						
							|  |  |  | 		{"", "foo", false}, | 
					
						
							|  |  |  | 		{"azerty", "", false}, | 
					
						
							|  |  |  | 		{"azerty", "foo", false}, | 
					
						
							| 
									
										
										
										
											2017-01-03 17:33:00 +08:00
										 |  |  | 		{"azerty", "azerty123", false}, | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 		{credentials.AccessKey, credentials.SecretKey, true}, | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Iterating over the test cases, calling the function under test and asserting the response.
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		_, err := getWebRPCToken(apiRouter, testCase.username, testCase.password) | 
					
						
							|  |  |  | 		if err != nil && testCase.success { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: Expected to succeed but it failed, %v", i+1, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err == nil && !testCase.success { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: Expected to fail but it didn't, %v", i+1, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper for calling StorageInfo Web Handler
 | 
					
						
							|  |  |  | func TestWebHandlerStorageInfo(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testStorageInfoWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testStorageInfoWebHandler - Test StorageInfo web handler
 | 
					
						
							|  |  |  | func testStorageInfoWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							|  |  |  | 	// get random bucket name.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 14:16:27 +08:00
										 |  |  | 	storageInfoRequest := &WebGenericArgs{} | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	storageInfoReply := &StorageInfoRep{} | 
					
						
							|  |  |  | 	req, err := newTestWebRPCRequest("Web.StorageInfo", authorization, storageInfoRequest) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-24 08:30:25 +08:00
										 |  |  | 	if err = getTestWebRPCResponse(rec, &storageInfoReply); err != nil { | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 		t.Fatalf("Failed %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper for calling ServerInfo Web Handler
 | 
					
						
							|  |  |  | func TestWebHandlerServerInfo(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testServerInfoWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testServerInfoWebHandler - Test ServerInfo web handler
 | 
					
						
							|  |  |  | func testServerInfoWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 14:16:27 +08:00
										 |  |  | 	serverInfoRequest := &WebGenericArgs{} | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	serverInfoReply := &ServerInfoRep{} | 
					
						
							|  |  |  | 	req, err := newTestWebRPCRequest("Web.ServerInfo", authorization, serverInfoRequest) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = getTestWebRPCResponse(rec, &serverInfoReply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | 	if serverInfoReply.MinioVersion != Version { | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 		t.Fatalf("Cannot get minio version from server info handler") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-05-02 14:55:53 +08:00
										 |  |  | 	serverInfoReply.MinioGlobalInfo["domains"] = []string(nil) | 
					
						
							| 
									
										
										
										
											2017-05-25 12:09:23 +08:00
										 |  |  | 	globalInfo := getGlobalInfo() | 
					
						
							|  |  |  | 	if !reflect.DeepEqual(serverInfoReply.MinioGlobalInfo, globalInfo) { | 
					
						
							|  |  |  | 		t.Fatalf("Global info did not match got %#v, expected %#v", serverInfoReply.MinioGlobalInfo, globalInfo) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper for calling MakeBucket Web Handler
 | 
					
						
							|  |  |  | func TestWebHandlerMakeBucket(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testMakeBucketWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testMakeBucketWebHandler - Test MakeBucket web handler
 | 
					
						
							|  |  |  | func testMakeBucketWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		bucketName string | 
					
						
							|  |  |  | 		success    bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"", false}, | 
					
						
							|  |  |  | 		{".", false}, | 
					
						
							|  |  |  | 		{"ab", false}, | 
					
						
							| 
									
										
										
										
											2017-03-03 19:01:42 +08:00
										 |  |  | 		{"minio", false}, | 
					
						
							| 
									
										
										
										
											2017-06-13 08:40:28 +08:00
										 |  |  | 		{minioMetaBucket, false}, | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 		{bucketName, true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		makeBucketRequest := MakeBucketArgs{BucketName: testCase.bucketName} | 
					
						
							|  |  |  | 		makeBucketReply := &WebGenericRep{} | 
					
						
							|  |  |  | 		req, err := newTestWebRPCRequest("Web.MakeBucket", authorization, makeBucketRequest) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: Failed to create HTTP request: <ERROR> %v", i+1, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 		if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: Expected the response status to be 200, but instead found `%d`", i+1, rec.Code) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = getTestWebRPCResponse(rec, &makeBucketReply) | 
					
						
							|  |  |  | 		if testCase.success && err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: Should succeed but it didn't, %v", i+1, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !testCase.success && err == nil { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: Should fail but it didn't (%s)", i+1, testCase.bucketName) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | // Wrapper for calling DeleteBucket handler
 | 
					
						
							|  |  |  | func TestWebHandlerDeleteBucket(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testDeleteBucketWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testDeleteBucketWebHandler - Test DeleteBucket web handler
 | 
					
						
							|  |  |  | func testDeleteBucketWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							|  |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 	token, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("could not get RPC token, %s", err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							| 
									
										
										
										
											2018-09-11 00:42:43 +08:00
										 |  |  | 	var opts ObjectOptions | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to create bucket: %s (%s)", err.Error(), instanceType) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		bucketName string | 
					
						
							|  |  |  | 		// Whether or not to put an object into the bucket.
 | 
					
						
							|  |  |  | 		initWithObject bool | 
					
						
							|  |  |  | 		token          string | 
					
						
							|  |  |  | 		// Expected error (error must only contain this string to pass test)
 | 
					
						
							|  |  |  | 		// Empty string = no error
 | 
					
						
							|  |  |  | 		expect string | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2019-04-04 14:10:37 +08:00
										 |  |  | 		{"", false, token, "The specified bucket is not valid"}, | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 		{".", false, "auth", "Authentication failed"}, | 
					
						
							| 
									
										
										
										
											2019-04-04 14:10:37 +08:00
										 |  |  | 		{".", false, token, "The specified bucket is not valid"}, | 
					
						
							|  |  |  | 		{"..", false, token, "The specified bucket is not valid"}, | 
					
						
							|  |  |  | 		{"ab", false, token, "The specified bucket is not valid"}, | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 		{"minio", false, "false token", "Authentication failed"}, | 
					
						
							| 
									
										
										
										
											2019-04-04 14:10:37 +08:00
										 |  |  | 		{"minio", false, token, "The specified bucket is not valid"}, | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 		{bucketName, false, token, ""}, | 
					
						
							| 
									
										
										
										
											2019-08-22 04:01:46 +08:00
										 |  |  | 		{bucketName, true, token, "The bucket you tried to delete is not empty"}, | 
					
						
							| 
									
										
										
										
											2018-10-18 07:23:09 +08:00
										 |  |  | 		{bucketName, false, "", "JWT token missing"}, | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range testCases { | 
					
						
							|  |  |  | 		if test.initWithObject { | 
					
						
							|  |  |  | 			data := bytes.NewBufferString("hello") | 
					
						
							| 
									
										
										
										
											2019-02-09 13:31:06 +08:00
										 |  |  | 			_, err = obj.PutObject(context.Background(), test.bucketName, "object", mustGetPutObjReader(t, data, int64(data.Len()), "", ""), opts) | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 			// _, err = obj.PutObject(test.bucketName, "object", int64(data.Len()), data, nil, "")
 | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatalf("could not put object to %s, %s", test.bucketName, err.Error()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		makeBucketRequest := MakeBucketArgs{BucketName: test.bucketName} | 
					
						
							|  |  |  | 		makeBucketReply := &WebGenericRep{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		req, err := newTestWebRPCRequest("Web.DeleteBucket", test.token, makeBucketRequest) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Errorf("failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 		if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 			t.Errorf("expected the response status to be `%d`, but instead found `%d`", http.StatusOK, rec.Code) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = getTestWebRPCResponse(rec, &makeBucketReply) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if test.expect != "" { | 
					
						
							|  |  |  | 			if err == nil { | 
					
						
							|  |  |  | 				// If we expected an error, but didn't get one.
 | 
					
						
							|  |  |  | 				t.Errorf("expected `..%s..` but got nil error", test.expect) | 
					
						
							|  |  |  | 			} else if !strings.Contains(err.Error(), test.expect) { | 
					
						
							|  |  |  | 				// If we got an error that wasn't what we expected.
 | 
					
						
							|  |  |  | 				t.Errorf("expected `..%s..` but got `%s`", test.expect, err.Error()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else if test.expect == "" && err != nil { | 
					
						
							|  |  |  | 			t.Errorf("expected test success, but got `%s`", err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If we created the bucket with an object, now delete the object to cleanup.
 | 
					
						
							|  |  |  | 		if test.initWithObject { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 			_, err = obj.DeleteObject(context.Background(), test.bucketName, "object", ObjectOptions{}) | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatalf("could not delete object, %s", err.Error()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// If it did not succeed in deleting the bucket, don't try and recreate it.
 | 
					
						
							|  |  |  | 		// Or, it'll fail if there was an object.
 | 
					
						
							|  |  |  | 		if err != nil || test.initWithObject { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 		err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2017-12-29 21:15:44 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			// failed to create new bucket, abort.
 | 
					
						
							|  |  |  | 			t.Fatalf("failed to create new bucket (%s): %s", instanceType, err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | // Wrapper for calling ListBuckets Web Handler
 | 
					
						
							|  |  |  | func TestWebHandlerListBuckets(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testListBucketsWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testListBucketsHandler - Test ListBuckets web handler
 | 
					
						
							|  |  |  | func testListBucketsWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							|  |  |  | 	// Create bucket.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// failed to create newbucket, abort.
 | 
					
						
							|  |  |  | 		t.Fatalf("%s : %s", instanceType, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	listBucketsRequest := WebGenericArgs{} | 
					
						
							|  |  |  | 	listBucketsReply := &ListBucketsRep{} | 
					
						
							|  |  |  | 	req, err := newTestWebRPCRequest("Web.ListBuckets", authorization, listBucketsRequest) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = getTestWebRPCResponse(rec, &listBucketsReply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(listBucketsReply.Buckets) == 0 { | 
					
						
							|  |  |  | 		t.Fatalf("Cannot find the bucket already created by MakeBucket") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if listBucketsReply.Buckets[0].Name != bucketName { | 
					
						
							| 
									
										
										
										
											2020-03-19 07:19:29 +08:00
										 |  |  | 		t.Fatalf("Found another bucket %q other than already created by MakeBucket", listBucketsReply.Buckets[0].Name) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper for calling ListObjects Web Handler
 | 
					
						
							|  |  |  | func TestWebHandlerListObjects(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testListObjectsWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testListObjectsHandler - Test ListObjects web handler
 | 
					
						
							|  |  |  | func testListObjectsWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							|  |  |  | 	objectName := "object" | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 	objectSize := 1 * humanize.KiByte | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create bucket.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// failed to create newbucket, abort.
 | 
					
						
							|  |  |  | 		t.Fatalf("%s : %s", instanceType, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	data := bytes.Repeat([]byte("a"), objectSize) | 
					
						
							| 
									
										
										
										
											2017-09-20 03:40:27 +08:00
										 |  |  | 	metadata := map[string]string{"etag": "c9a34cfc85d982698c6ac89f76071abd"} | 
					
						
							| 
									
										
										
										
											2019-02-09 13:31:06 +08:00
										 |  |  | 	_, err = obj.PutObject(context.Background(), bucketName, objectName, mustGetPutObjReader(t, bytes.NewReader(data), int64(len(data)), metadata["etag"], ""), ObjectOptions{UserDefined: metadata}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Was not able to upload an object, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 	test := func(token string) (*ListObjectsRep, error) { | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		listObjectsRequest := ListObjectsArgs{BucketName: bucketName, Prefix: ""} | 
					
						
							|  |  |  | 		listObjectsReply := &ListObjectsRep{} | 
					
						
							|  |  |  | 		var req *http.Request | 
					
						
							|  |  |  | 		req, err = newTestWebRPCRequest("Web.ListObjects", token, listObjectsRequest) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 		if rec.Code != http.StatusOK { | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 			return listObjectsReply, fmt.Errorf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		err = getTestWebRPCResponse(rec, &listObjectsReply) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 			return listObjectsReply, err | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 		return listObjectsReply, nil | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	verifyReply := func(reply *ListObjectsRep) { | 
					
						
							|  |  |  | 		if len(reply.Objects) == 0 { | 
					
						
							|  |  |  | 			t.Fatalf("Cannot find the object") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if reply.Objects[0].Key != objectName { | 
					
						
							|  |  |  | 			t.Fatalf("Found another object other than already created by PutObject") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if reply.Objects[0].Size != int64(objectSize) { | 
					
						
							|  |  |  | 			t.Fatalf("Found a object with the same name but with a different size") | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Authenticated ListObjects should succeed.
 | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 	reply, err := test(authorization) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		t.Fatal(err) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	verifyReply(reply) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Unauthenticated ListObjects should fail.
 | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 	_, err = test("") | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		t.Fatalf("Expected error `%s`", err) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper for calling RemoveObject Web Handler
 | 
					
						
							|  |  |  | func TestWebHandlerRemoveObject(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testRemoveObjectWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 14:27:48 +08:00
										 |  |  | // testRemoveObjectWebHandler - Test RemoveObjectObject web handler
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | func testRemoveObjectWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							|  |  |  | 	objectName := "object" | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 	objectSize := 1 * humanize.KiByte | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create bucket.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// failed to create newbucket, abort.
 | 
					
						
							|  |  |  | 		t.Fatalf("%s : %s", instanceType, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	data := bytes.Repeat([]byte("a"), objectSize) | 
					
						
							| 
									
										
										
										
											2017-09-20 03:40:27 +08:00
										 |  |  | 	metadata := map[string]string{"etag": "c9a34cfc85d982698c6ac89f76071abd"} | 
					
						
							| 
									
										
										
										
											2019-02-09 13:31:06 +08:00
										 |  |  | 	_, err = obj.PutObject(context.Background(), bucketName, objectName, mustGetPutObjReader(t, bytes.NewReader(data), int64(len(data)), metadata["etag"], ""), ObjectOptions{UserDefined: metadata}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Was not able to upload an object, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:07:28 +08:00
										 |  |  | 	objectName = "a/object" | 
					
						
							| 
									
										
										
										
											2017-09-20 03:40:27 +08:00
										 |  |  | 	metadata = map[string]string{"etag": "c9a34cfc85d982698c6ac89f76071abd"} | 
					
						
							| 
									
										
										
										
											2019-02-09 13:31:06 +08:00
										 |  |  | 	_, err = obj.PutObject(context.Background(), bucketName, objectName, mustGetPutObjReader(t, bytes.NewReader(data), int64(len(data)), metadata["etag"], ""), ObjectOptions{UserDefined: metadata}) | 
					
						
							| 
									
										
										
										
											2017-03-01 11:07:28 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Was not able to upload an object, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 14:27:48 +08:00
										 |  |  | 	removeRequest := RemoveObjectArgs{BucketName: bucketName, Objects: []string{"a/", "object"}} | 
					
						
							|  |  |  | 	removeReply := &WebGenericRep{} | 
					
						
							|  |  |  | 	req, err := newTestWebRPCRequest("Web.RemoveObject", authorization, removeRequest) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-27 14:27:48 +08:00
										 |  |  | 	err = getTestWebRPCResponse(rec, &removeReply) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-23 03:12:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 14:27:48 +08:00
										 |  |  | 	removeRequest = RemoveObjectArgs{BucketName: bucketName, Objects: []string{"a/", "object"}} | 
					
						
							|  |  |  | 	removeReply = &WebGenericRep{} | 
					
						
							|  |  |  | 	req, err = newTestWebRPCRequest("Web.RemoveObject", authorization, removeRequest) | 
					
						
							| 
									
										
										
										
											2016-11-23 03:12:38 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-27 14:27:48 +08:00
										 |  |  | 	err = getTestWebRPCResponse(rec, &removeReply) | 
					
						
							| 
									
										
										
										
											2016-11-23 03:12:38 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-27 14:27:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	removeRequest = RemoveObjectArgs{BucketName: bucketName} | 
					
						
							|  |  |  | 	removeReply = &WebGenericRep{} | 
					
						
							|  |  |  | 	req, err = newTestWebRPCRequest("Web.RemoveObject", authorization, removeRequest) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	b, err := ioutil.ReadAll(rec.Body) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !bytes.Contains(b, []byte("Invalid arguments specified")) { | 
					
						
							|  |  |  | 		t.Fatalf("Expected response wrong %s", string(b)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper for calling Generate Auth Handler
 | 
					
						
							|  |  |  | func TestWebHandlerGenerateAuth(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testGenerateAuthWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testGenerateAuthWebHandler - Test GenerateAuth web handler
 | 
					
						
							|  |  |  | func testGenerateAuthWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	generateAuthRequest := WebGenericArgs{} | 
					
						
							|  |  |  | 	generateAuthReply := &GenerateAuthReply{} | 
					
						
							|  |  |  | 	req, err := newTestWebRPCRequest("Web.GenerateAuth", authorization, generateAuthRequest) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = getTestWebRPCResponse(rec, &generateAuthReply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if generateAuthReply.AccessKey == "" || generateAuthReply.SecretKey == "" { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to generate auth keys") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 03:46:37 +08:00
										 |  |  | func TestWebCreateURLToken(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testCreateURLToken) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | func getTokenString(accessKey, secretKey string) (string, error) { | 
					
						
							| 
									
										
										
										
											2020-01-31 10:59:22 +08:00
										 |  |  | 	claims := xjwt.NewMapClaims() | 
					
						
							|  |  |  | 	claims.SetExpiry(UTCNow().Add(defaultJWTExpiry)) | 
					
						
							|  |  |  | 	claims.SetAccessKey(accessKey) | 
					
						
							|  |  |  | 	token := jwtgo.NewWithClaims(jwtgo.SigningMethodHS512, claims) | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 	return token.SignedString([]byte(secretKey)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 03:46:37 +08:00
										 |  |  | func testCreateURLToken(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							|  |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2017-07-25 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	args := WebGenericArgs{} | 
					
						
							|  |  |  | 	tokenReply := &URLTokenReply{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	req, err := newTestWebRPCRequest("Web.CreateURLToken", authorization, args) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = getTestWebRPCResponse(rec, &tokenReply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Ensure the token is valid now. It will expire later.
 | 
					
						
							|  |  |  | 	if !isAuthTokenValid(tokenReply.Token) { | 
					
						
							|  |  |  | 		t.Fatalf("token is not valid") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Token is invalid.
 | 
					
						
							|  |  |  | 	if isAuthTokenValid("") { | 
					
						
							|  |  |  | 		t.Fatalf("token shouldn't be valid, but it is") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	token, err := getTokenString("invalid-access", credentials.SecretKey) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Token has invalid access key.
 | 
					
						
							|  |  |  | 	if isAuthTokenValid(token) { | 
					
						
							|  |  |  | 		t.Fatalf("token shouldn't be valid, but it is") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-25 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | // Wrapper for calling Upload Handler
 | 
					
						
							|  |  |  | func TestWebHandlerUpload(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testUploadWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testUploadWebHandler - Test Upload web handler
 | 
					
						
							|  |  |  | func testUploadWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 04:51:43 +08:00
										 |  |  | 	content := []byte("temporary file's content") | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	objectName := "test.file" | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 02:45:00 +08:00
										 |  |  | 	test := func(token string, sendContentLength bool) int { | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		rec := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2019-08-07 03:08:58 +08:00
										 |  |  | 		req, rErr := http.NewRequest("PUT", "/minio/upload/"+bucketName+SlashSeparator+objectName, nil) | 
					
						
							| 
									
										
										
										
											2017-02-03 02:45:00 +08:00
										 |  |  | 		if rErr != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Cannot create upload request, %v", rErr) | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		req.Header.Set("x-amz-date", "20160814T114029Z") | 
					
						
							|  |  |  | 		req.Header.Set("Accept", "*/*") | 
					
						
							| 
									
										
										
										
											2019-08-13 08:05:30 +08:00
										 |  |  | 		req.Header.Set("User-Agent", "Mozilla") | 
					
						
							| 
									
										
										
										
											2017-02-03 02:45:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		req.Body = ioutil.NopCloser(bytes.NewReader(content)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 02:45:00 +08:00
										 |  |  | 		if !sendContentLength { | 
					
						
							|  |  |  | 			req.ContentLength = -1 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			req.ContentLength = int64(len(content)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		if token != "" { | 
					
						
							|  |  |  | 			req.Header.Set("Authorization", "Bearer "+authorization) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 		return rec.Code | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	// Create bucket.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// failed to create newbucket, abort.
 | 
					
						
							|  |  |  | 		t.Fatalf("%s : %s", instanceType, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	// Authenticated upload should succeed.
 | 
					
						
							| 
									
										
										
										
											2017-02-03 02:45:00 +08:00
										 |  |  | 	code := test(authorization, true) | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	if code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", code) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var byteBuffer bytes.Buffer | 
					
						
							| 
									
										
										
										
											2018-09-11 00:42:43 +08:00
										 |  |  | 	err = obj.GetObject(context.Background(), bucketName, objectName, 0, int64(len(content)), &byteBuffer, "", ObjectOptions{}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 02:00:47 +08:00
										 |  |  | 	if !bytes.Equal(byteBuffer.Bytes(), content) { | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 		t.Fatalf("The upload file is different from the download file") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 02:45:00 +08:00
										 |  |  | 	// Authenticated upload without content-length should fail
 | 
					
						
							|  |  |  | 	code = test(authorization, false) | 
					
						
							|  |  |  | 	if code != http.StatusBadRequest { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	// Unauthenticated upload should fail.
 | 
					
						
							| 
									
										
										
										
											2017-02-03 02:45:00 +08:00
										 |  |  | 	code = test("", true) | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	if code != http.StatusForbidden { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 403, but instead found `%d`", code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 02:45:00 +08:00
										 |  |  | // Wrapper for calling Download Handler
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | func TestWebHandlerDownload(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testDownloadWebHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // testDownloadWebHandler - Test Download web handler
 | 
					
						
							|  |  |  | func testDownloadWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	objectName := "test.file" | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	test := func(token string) (int, []byte) { | 
					
						
							|  |  |  | 		rec := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2019-08-07 03:08:58 +08:00
										 |  |  | 		path := "/minio/download/" + bucketName + SlashSeparator + objectName + "?token=" | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		if token != "" { | 
					
						
							|  |  |  | 			path = path + token | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		var req *http.Request | 
					
						
							|  |  |  | 		req, err = http.NewRequest("GET", path, nil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Cannot create upload request, %v", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 08:05:30 +08:00
										 |  |  | 		req.Header.Set("User-Agent", "Mozilla") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 		return rec.Code, rec.Body.Bytes() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	// Create bucket.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// failed to create newbucket, abort.
 | 
					
						
							|  |  |  | 		t.Fatalf("%s : %s", instanceType, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	content := []byte("temporary file's content") | 
					
						
							| 
									
										
										
										
											2017-09-20 03:40:27 +08:00
										 |  |  | 	metadata := map[string]string{"etag": "01ce59706106fe5e02e7f55fffda7f34"} | 
					
						
							| 
									
										
										
										
											2019-02-09 13:31:06 +08:00
										 |  |  | 	_, err = obj.PutObject(context.Background(), bucketName, objectName, mustGetPutObjReader(t, bytes.NewReader(content), int64(len(content)), metadata["etag"], ""), ObjectOptions{UserDefined: metadata}) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Was not able to upload an object, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	// Authenticated download should succeed.
 | 
					
						
							|  |  |  | 	code, bodyContent := test(authorization) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	if code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", code) | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 02:00:47 +08:00
										 |  |  | 	if !bytes.Equal(bodyContent, content) { | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 		t.Fatalf("The downloaded file is corrupted") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 03:46:37 +08:00
										 |  |  | 	// Temporary token should succeed.
 | 
					
						
							|  |  |  | 	tmpToken, err := authenticateURL(credentials.AccessKey, credentials.SecretKey) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	code, bodyContent = test(tmpToken) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !bytes.Equal(bodyContent, content) { | 
					
						
							|  |  |  | 		t.Fatalf("The downloaded file is corrupted") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Old token should fail.
 | 
					
						
							|  |  |  | 	code, bodyContent = test("eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDAzMzIwOTUsImlhdCI6MTUwMDMzMjAzNSwic3ViIjoiRFlLSU01VlRZNDBJMVZQSE5VMTkifQ.tXQ45GJc8eOFet_a4VWVyeqJEOPWybotQYNr2zVxBpEOICkGbu_YWGhd9TkLLe1E65oeeiLHPdXSN8CzcbPoRA") | 
					
						
							|  |  |  | 	if code != http.StatusForbidden { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 403, but instead found `%d`", code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !bytes.Equal(bodyContent, bytes.NewBufferString("Authentication failed, check your access credentials").Bytes()) { | 
					
						
							| 
									
										
										
										
											2020-01-31 10:59:22 +08:00
										 |  |  | 		t.Fatalf("Expected authentication error message, got %s", string(bodyContent)) | 
					
						
							| 
									
										
										
										
											2017-07-25 03:46:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	// Unauthenticated download should fail.
 | 
					
						
							| 
									
										
										
										
											2017-01-17 09:05:00 +08:00
										 |  |  | 	code, _ = test("") | 
					
						
							| 
									
										
										
										
											2017-01-12 05:26:42 +08:00
										 |  |  | 	if code != http.StatusForbidden { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 403, but instead found `%d`", code) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-16 07:13:03 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-31 01:04:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | // Test web.DownloadZip
 | 
					
						
							|  |  |  | func TestWebHandlerDownloadZip(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testWebHandlerDownloadZip) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func testWebHandlerDownloadZip(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							|  |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2018-09-11 00:42:43 +08:00
										 |  |  | 	var opts ObjectOptions | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 03:46:37 +08:00
										 |  |  | 	authorization, err := authenticateURL(credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucket := getRandomBucketName() | 
					
						
							|  |  |  | 	fileOne := "aaaaaaaaaaaaaa" | 
					
						
							|  |  |  | 	fileTwo := "bbbbbbbbbbbbbb" | 
					
						
							|  |  |  | 	fileThree := "cccccccccccccc" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create bucket.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucket, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// failed to create newbucket, abort.
 | 
					
						
							|  |  |  | 		t.Fatalf("%s : %s", instanceType, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 13:31:06 +08:00
										 |  |  | 	obj.PutObject(context.Background(), bucket, "a/one", mustGetPutObjReader(t, strings.NewReader(fileOne), int64(len(fileOne)), "", ""), opts) | 
					
						
							|  |  |  | 	obj.PutObject(context.Background(), bucket, "a/b/two", mustGetPutObjReader(t, strings.NewReader(fileTwo), int64(len(fileTwo)), "", ""), opts) | 
					
						
							|  |  |  | 	obj.PutObject(context.Background(), bucket, "a/c/three", mustGetPutObjReader(t, strings.NewReader(fileThree), int64(len(fileThree)), "", ""), opts) | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	test := func(token string) (int, []byte) { | 
					
						
							|  |  |  | 		rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 		path := "/minio/zip" + "?token=" | 
					
						
							|  |  |  | 		if token != "" { | 
					
						
							|  |  |  | 			path = path + token | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		args := DownloadZipArgs{ | 
					
						
							|  |  |  | 			Objects:    []string{"one", "b/", "c/"}, | 
					
						
							|  |  |  | 			Prefix:     "a/", | 
					
						
							|  |  |  | 			BucketName: bucket, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var argsData []byte | 
					
						
							|  |  |  | 		argsData, err = json.Marshal(args) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return 0, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		var req *http.Request | 
					
						
							| 
									
										
										
										
											2017-02-23 06:51:38 +08:00
										 |  |  | 		req, err = http.NewRequest("POST", path, bytes.NewBuffer(argsData)) | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Cannot create upload request, %v", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 08:05:30 +08:00
										 |  |  | 		req.Header.Set("User-Agent", "Mozilla") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 		apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 		return rec.Code, rec.Body.Bytes() | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-14 23:00:04 +08:00
										 |  |  | 	code, _ := test("") | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 	if code != 403 { | 
					
						
							|  |  |  | 		t.Fatal("Expected to receive authentication error") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-14 23:00:04 +08:00
										 |  |  | 	code, data := test(authorization) | 
					
						
							| 
									
										
										
										
											2017-02-09 15:39:08 +08:00
										 |  |  | 	if code != 200 { | 
					
						
							|  |  |  | 		t.Fatal("web.DownloadsZip() failed") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	h := md5.New() | 
					
						
							|  |  |  | 	for _, file := range reader.File { | 
					
						
							|  |  |  | 		fileReader, err := file.Open() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		io.Copy(h, fileReader) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Verify the md5 of the response.
 | 
					
						
							|  |  |  | 	if hex.EncodeToString(h.Sum(nil)) != "ac7196449b14bea42775d29e8bb29f50" { | 
					
						
							|  |  |  | 		t.Fatal("Incorrect zip contents") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | // Wrapper for calling PresignedGet handler
 | 
					
						
							|  |  |  | func TestWebHandlerPresignedGetHandler(t *testing.T) { | 
					
						
							|  |  |  | 	ExecObjectLayerTest(t, testWebPresignedGetHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func testWebPresignedGetHandler(obj ObjectLayer, instanceType string, t TestErrHandler) { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := getRandomBucketName() | 
					
						
							|  |  |  | 	objectName := "object" | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 	objectSize := 1 * humanize.KiByte | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create bucket.
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// failed to create newbucket, abort.
 | 
					
						
							|  |  |  | 		t.Fatalf("%s : %s", instanceType, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	data := bytes.Repeat([]byte("a"), objectSize) | 
					
						
							| 
									
										
										
										
											2017-09-20 03:40:27 +08:00
										 |  |  | 	metadata := map[string]string{"etag": "c9a34cfc85d982698c6ac89f76071abd"} | 
					
						
							| 
									
										
										
										
											2019-02-09 13:31:06 +08:00
										 |  |  | 	_, err = obj.PutObject(context.Background(), bucketName, objectName, mustGetPutObjReader(t, bytes.NewReader(data), int64(len(data)), metadata["etag"], ""), ObjectOptions{UserDefined: metadata}) | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Was not able to upload an object, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	presignGetReq := PresignedGetArgs{ | 
					
						
							|  |  |  | 		HostName:   "", | 
					
						
							|  |  |  | 		BucketName: bucketName, | 
					
						
							|  |  |  | 		ObjectName: objectName, | 
					
						
							| 
									
										
										
										
											2016-11-23 03:12:38 +08:00
										 |  |  | 		Expiry:     1000, | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	presignGetRep := &PresignedGetRep{} | 
					
						
							|  |  |  | 	req, err := newTestWebRPCRequest("Web.PresignedGet", authorization, presignGetReq) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = getTestWebRPCResponse(rec, &presignGetRep) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | 	apiRouter = initTestAPIEndPoints(obj, []string{"GetObject"}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize a new api recorder.
 | 
					
						
							|  |  |  | 	arec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	req, err = newTestRequest("GET", presignGetRep.URL, 0, nil) | 
					
						
							|  |  |  | 	req.Header.Del("x-amz-content-sha256") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Failed to initialized a new request", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(arec, req) | 
					
						
							|  |  |  | 	if arec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", arec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	savedData, err := ioutil.ReadAll(arec.Body) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Reading body failed", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !bytes.Equal(data, savedData) { | 
					
						
							|  |  |  | 		t.Fatal("Read data is not equal was what was expected") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-24 03:32:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-10-24 03:32:35 +08:00
										 |  |  | 	apiRouter = initTestWebRPCEndPoint(obj) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	presignGetReq = PresignedGetArgs{ | 
					
						
							|  |  |  | 		HostName:   "", | 
					
						
							|  |  |  | 		BucketName: "", | 
					
						
							|  |  |  | 		ObjectName: "", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	presignGetRep = &PresignedGetRep{} | 
					
						
							|  |  |  | 	req, err = newTestWebRPCRequest("Web.PresignedGet", authorization, presignGetReq) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = getTestWebRPCResponse(rec, &presignGetRep) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-23 03:12:38 +08:00
										 |  |  | 	if err.Error() != "Bucket and Object are mandatory arguments." { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected, expected `Bucket and Object are mandatory arguments`, got %s", err) | 
					
						
							| 
									
										
										
										
											2016-10-24 03:32:35 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-23 16:24:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | // TestWebCheckAuthorization - Test Authorization for all web handlers
 | 
					
						
							|  |  |  | func TestWebCheckAuthorization(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-04-15 08:52:38 +08:00
										 |  |  | 	ctx, cancel := context.WithCancel(context.Background()) | 
					
						
							|  |  |  | 	defer cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Prepare Erasure backend
 | 
					
						
							|  |  |  | 	obj, fsDirs, err := prepareErasure16(ctx) | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 		t.Fatalf("Initialization of object layer failed for Erasure setup: %s", err) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Executing the object layer tests for Erasure.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	defer removeRoots(fsDirs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Register the API end points with Erasure/FS object layer.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	// initialize the server and obtain the credentials and root.
 | 
					
						
							|  |  |  | 	// credentials are necessary to sign the HTTP request.
 | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	err = newTestConfig(globalMinioDefaultRegion, obj) | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Init Test config failed", err) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check if web rpc calls return unauthorized request with an incorrect token
 | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 	webRPCs := []string{ | 
					
						
							|  |  |  | 		"ServerInfo", "StorageInfo", "MakeBucket", | 
					
						
							|  |  |  | 		"ListBuckets", "ListObjects", "RemoveObject", | 
					
						
							| 
									
										
										
										
											2019-05-30 04:18:46 +08:00
										 |  |  | 		"GenerateAuth", "SetAuth", | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 		"GetBucketPolicy", "SetBucketPolicy", "ListAllBucketPolicies", | 
					
						
							|  |  |  | 		"PresignedGet", | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	for _, rpcCall := range webRPCs { | 
					
						
							|  |  |  | 		reply := &WebGenericRep{} | 
					
						
							| 
									
										
										
										
											2019-04-18 14:16:27 +08:00
										 |  |  | 		req, nerr := newTestWebRPCRequest("Web."+rpcCall, "Bearer fooauthorization", &WebGenericArgs{}) | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 		if nerr != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Test %s: Failed to create HTTP request: <ERROR> %v", rpcCall, nerr) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 		if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 			t.Fatalf("Test %s: Expected the response status to be 200, but instead found `%d`", rpcCall, rec.Code) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = getTestWebRPCResponse(rec, &reply) | 
					
						
							|  |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			t.Fatalf("Test %s: Should fail", rpcCall) | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 			if !strings.Contains(err.Error(), errAuthentication.Error()) { | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 				t.Fatalf("Test %s: should fail with Unauthorized request. Found error: %v", rpcCall, err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 	rec = httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	// Test authorization of Web.Download
 | 
					
						
							|  |  |  | 	req, err := http.NewRequest("GET", "/minio/download/bucket/object?token=wrongauth", nil) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Cannot create upload request, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-13 08:05:30 +08:00
										 |  |  | 	req.Header.Set("User-Agent", "Mozilla") | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 	if rec.Code != http.StatusForbidden { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 403, but instead found `%d`", rec.Code) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 	resp := rec.Body.String() | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 	if !strings.EqualFold(resp, errAuthentication.Error()) { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected error message, expected: %s, found: `%s`", errAuthentication, resp) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 	rec = httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	// Test authorization of Web.Upload
 | 
					
						
							|  |  |  | 	content := []byte("temporary file's content") | 
					
						
							|  |  |  | 	req, err = http.NewRequest("PUT", "/minio/upload/bucket/object", nil) | 
					
						
							|  |  |  | 	req.Header.Set("Authorization", "Bearer foo-authorization") | 
					
						
							| 
									
										
										
										
											2019-08-13 08:05:30 +08:00
										 |  |  | 	req.Header.Set("User-Agent", "Mozilla") | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	req.Header.Set("Content-Length", strconv.Itoa(len(content))) | 
					
						
							|  |  |  | 	req.Header.Set("x-amz-date", "20160814T114029Z") | 
					
						
							|  |  |  | 	req.Header.Set("Accept", "*/*") | 
					
						
							|  |  |  | 	req.Body = ioutil.NopCloser(bytes.NewReader(content)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Cannot create upload request, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 	if rec.Code != http.StatusForbidden { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 403, but instead found `%d`", rec.Code) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 	resp = rec.Body.String() | 
					
						
							| 
									
										
										
										
											2016-11-03 05:45:11 +08:00
										 |  |  | 	if !strings.EqualFold(resp, errAuthentication.Error()) { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected error message, expected: `%s`, found: `%s`", errAuthentication, resp) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TestWebObjectLayerFaultyDisks - Test Web RPC responses with faulty disks
 | 
					
						
							|  |  |  | func TestWebObjectLayerFaultyDisks(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-04-15 08:52:38 +08:00
										 |  |  | 	ctx, cancel := context.WithCancel(context.Background()) | 
					
						
							|  |  |  | 	defer cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Prepare Erasure backend
 | 
					
						
							|  |  |  | 	obj, fsDirs, err := prepareErasure16(ctx) | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 		t.Fatalf("Initialization of object layer failed for Erasure setup: %s", err) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Executing the object layer tests for Erasure.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	defer removeRoots(fsDirs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	// initialize the server and obtain the credentials and root.
 | 
					
						
							|  |  |  | 	// credentials are necessary to sign the HTTP request.
 | 
					
						
							|  |  |  | 	err = newTestConfig(globalMinioDefaultRegion, obj) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Init Test config failed", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-04 07:46:28 +08:00
										 |  |  | 	bucketName := "mybucket" | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	err = obj.MakeBucketWithLocation(context.Background(), bucketName, BucketOptions{}) | 
					
						
							| 
									
										
										
										
											2017-03-04 07:46:28 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot make bucket:", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	// Set faulty disks to Erasure backend
 | 
					
						
							|  |  |  | 	z := obj.(*erasureZones) | 
					
						
							| 
									
										
										
										
											2019-11-20 09:42:27 +08:00
										 |  |  | 	xl := z.zones[0].sets[0] | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	erasureDisks := xl.getDisks() | 
					
						
							|  |  |  | 	z.zones[0].erasureDisksMu.Lock() | 
					
						
							| 
									
										
										
										
											2019-11-20 09:42:27 +08:00
										 |  |  | 	xl.getDisks = func() []StorageAPI { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 		for i, d := range erasureDisks { | 
					
						
							|  |  |  | 			erasureDisks[i] = newNaughtyDisk(d, nil, errFaultyDisk) | 
					
						
							| 
									
										
										
										
											2019-11-20 09:42:27 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 		return erasureDisks | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	z.zones[0].erasureDisksMu.Unlock() | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 	// Initialize web rpc endpoint.
 | 
					
						
							|  |  |  | 	apiRouter := initTestWebRPCEndPoint(obj) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	credentials := globalActiveCred | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	authorization, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey) | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Cannot authenticate", err) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check if web rpc calls return errors with faulty disks.  ServerInfo, GenerateAuth, SetAuth, GetAuth are not concerned
 | 
					
						
							| 
									
										
										
										
											2017-03-04 07:46:28 +08:00
										 |  |  | 	// RemoveObject is also not concerned since it always returns success.
 | 
					
						
							|  |  |  | 	webRPCs := []struct { | 
					
						
							|  |  |  | 		webRPCName string | 
					
						
							|  |  |  | 		ReqArgs    interface{} | 
					
						
							|  |  |  | 		RepArgs    interface{} | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"MakeBucket", MakeBucketArgs{BucketName: bucketName}, WebGenericRep{}}, | 
					
						
							| 
									
										
										
										
											2019-04-18 14:16:27 +08:00
										 |  |  | 		{"ListBuckets", WebGenericArgs{}, ListBucketsRep{}}, | 
					
						
							| 
									
										
										
										
											2017-03-04 07:46:28 +08:00
										 |  |  | 		{"ListObjects", ListObjectsArgs{BucketName: bucketName, Prefix: ""}, ListObjectsRep{}}, | 
					
						
							|  |  |  | 		{"GetBucketPolicy", GetBucketPolicyArgs{BucketName: bucketName, Prefix: ""}, GetBucketPolicyRep{}}, | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		{"SetBucketPolicy", SetBucketPolicyWebArgs{BucketName: bucketName, Prefix: "", Policy: "none"}, WebGenericRep{}}, | 
					
						
							| 
									
										
										
										
											2017-03-04 07:46:28 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for _, rpcCall := range webRPCs { | 
					
						
							| 
									
										
										
										
											2017-03-04 07:46:28 +08:00
										 |  |  | 		args := &rpcCall.ReqArgs | 
					
						
							|  |  |  | 		reply := &rpcCall.RepArgs | 
					
						
							|  |  |  | 		req, nerr := newTestWebRPCRequest("Web."+rpcCall.webRPCName, authorization, args) | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 		if nerr != nil { | 
					
						
							|  |  |  | 			t.Fatalf("Test %s: Failed to create HTTP request: <ERROR> %v", rpcCall, nerr) | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 		if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 			t.Fatalf("Test %s: Expected the response status to be 200, but instead found `%d`", rpcCall, rec.Code) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = getTestWebRPCResponse(rec, &reply) | 
					
						
							|  |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %s: Should fail", rpcCall) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Test Web.StorageInfo
 | 
					
						
							| 
									
										
										
										
											2019-04-18 14:16:27 +08:00
										 |  |  | 	storageInfoRequest := &WebGenericArgs{} | 
					
						
							| 
									
										
										
										
											2016-09-23 07:35:12 +08:00
										 |  |  | 	storageInfoReply := &StorageInfoRep{} | 
					
						
							|  |  |  | 	req, err := newTestWebRPCRequest("Web.StorageInfo", authorization, storageInfoRequest) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create HTTP request: <ERROR> %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = getTestWebRPCResponse(rec, &storageInfoReply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Test authorization of Web.Download
 | 
					
						
							|  |  |  | 	req, err = http.NewRequest("GET", "/minio/download/bucket/object?token="+authorization, nil) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Cannot create upload request, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Test authorization of Web.Upload
 | 
					
						
							|  |  |  | 	content := []byte("temporary file's content") | 
					
						
							|  |  |  | 	req, err = http.NewRequest("PUT", "/minio/upload/bucket/object", nil) | 
					
						
							|  |  |  | 	req.Header.Set("Authorization", "Bearer "+authorization) | 
					
						
							|  |  |  | 	req.Header.Set("Content-Length", strconv.Itoa(len(content))) | 
					
						
							|  |  |  | 	req.Header.Set("x-amz-date", "20160814T114029Z") | 
					
						
							|  |  |  | 	req.Header.Set("Accept", "*/*") | 
					
						
							|  |  |  | 	req.Body = ioutil.NopCloser(bytes.NewReader(content)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Cannot create upload request, %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiRouter.ServeHTTP(rec, req) | 
					
						
							|  |  |  | 	if rec.Code != http.StatusOK { | 
					
						
							|  |  |  | 		t.Fatalf("Expected the response status to be 200, but instead found `%d`", rec.Code) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |