| 
									
										
										
										
											2016-08-06 04:48:31 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-08-06 04:48:31 +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-06 04:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 00:38:07 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2016-09-10 00:38:07 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2016-10-27 18:30:52 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2016-09-10 00:38:07 +08:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-09-10 00:38:07 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests http.Header clone.
 | 
					
						
							|  |  |  | func TestCloneHeader(t *testing.T) { | 
					
						
							|  |  |  | 	headers := []http.Header{ | 
					
						
							| 
									
										
										
										
											2016-09-10 15:51:25 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2016-09-10 00:38:07 +08:00
										 |  |  | 			"Content-Type":   {"text/html; charset=UTF-8"}, | 
					
						
							|  |  |  | 			"Content-Length": {"0"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-09-10 15:51:25 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2016-09-10 00:38:07 +08:00
										 |  |  | 			"Content-Length": {"0", "1", "2"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-09-10 15:51:25 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2016-09-10 00:38:07 +08:00
										 |  |  | 			"Expires":          {"-1"}, | 
					
						
							|  |  |  | 			"Content-Length":   {"0"}, | 
					
						
							|  |  |  | 			"Content-Encoding": {"gzip"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, header := range headers { | 
					
						
							|  |  |  | 		clonedHeader := cloneHeader(header) | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(header, clonedHeader) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d failed", i+1) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-06 04:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 16:59:28 +08:00
										 |  |  | // Tests maximum object size.
 | 
					
						
							|  |  |  | func TestMaxObjectSize(t *testing.T) { | 
					
						
							|  |  |  | 	sizes := []struct { | 
					
						
							|  |  |  | 		isMax bool | 
					
						
							|  |  |  | 		size  int64 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// Test - 1 - maximum object size.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:14:17 +08:00
										 |  |  | 			globalMaxObjectSize + 1, | 
					
						
							| 
									
										
										
										
											2016-08-15 16:59:28 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test - 2 - not maximum object size.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:14:17 +08:00
										 |  |  | 			globalMaxObjectSize - 1, | 
					
						
							| 
									
										
										
										
											2016-08-15 16:59:28 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, s := range sizes { | 
					
						
							|  |  |  | 		isMax := isMaxObjectSize(s.size) | 
					
						
							|  |  |  | 		if isMax != s.isMax { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %t, got %t", i+1, s.isMax, isMax) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests minimum allowed part size.
 | 
					
						
							|  |  |  | func TestMinAllowedPartSize(t *testing.T) { | 
					
						
							|  |  |  | 	sizes := []struct { | 
					
						
							|  |  |  | 		isMin bool | 
					
						
							|  |  |  | 		size  int64 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// Test - 1 - within minimum part size.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:14:17 +08:00
										 |  |  | 			globalMinPartSize + 1, | 
					
						
							| 
									
										
										
										
											2016-08-15 16:59:28 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test - 2 - smaller than minimum part size.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:14:17 +08:00
										 |  |  | 			globalMinPartSize - 1, | 
					
						
							| 
									
										
										
										
											2016-08-15 16:59:28 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, s := range sizes { | 
					
						
							|  |  |  | 		isMin := isMinAllowedPartSize(s.size) | 
					
						
							|  |  |  | 		if isMin != s.isMin { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %t, got %t", i+1, s.isMin, isMin) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests maximum allowed part number.
 | 
					
						
							|  |  |  | func TestMaxPartID(t *testing.T) { | 
					
						
							|  |  |  | 	sizes := []struct { | 
					
						
							|  |  |  | 		isMax bool | 
					
						
							|  |  |  | 		partN int | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// Test - 1 part number within max part number.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			false, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:14:17 +08:00
										 |  |  | 			globalMaxPartID - 1, | 
					
						
							| 
									
										
										
										
											2016-08-15 16:59:28 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Test - 2 part number bigger than max part number.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			true, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:14:17 +08:00
										 |  |  | 			globalMaxPartID + 1, | 
					
						
							| 
									
										
										
										
											2016-08-15 16:59:28 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, s := range sizes { | 
					
						
							|  |  |  | 		isMax := isMaxPartID(s.partN) | 
					
						
							|  |  |  | 		if isMax != s.isMax { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %t, got %t", i+1, s.isMax, isMax) | 
					
						
							| 
									
										
										
										
											2016-08-07 14:53:10 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-06 04:48:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-12 16:03:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | // Tests extracting bucket and objectname from various types of URL paths.
 | 
					
						
							|  |  |  | func TestURL2BucketObjectName(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		u              *url.URL | 
					
						
							|  |  |  | 		bucket, object string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// Test case 1 normal case.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u: &url.URL{ | 
					
						
							|  |  |  | 				Path: "/bucket/object", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 2 where url only has separator.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u: &url.URL{ | 
					
						
							|  |  |  | 				Path: "/", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket: "", | 
					
						
							|  |  |  | 			object: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 3 only bucket is present.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u: &url.URL{ | 
					
						
							|  |  |  | 				Path: "/bucket", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 4 many separators and object is a directory.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u: &url.URL{ | 
					
						
							|  |  |  | 				Path: "/bucket/object/1/", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object/1/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 5 object has many trailing separators.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u: &url.URL{ | 
					
						
							|  |  |  | 				Path: "/bucket/object/1///", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object/1///", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 6 object has only trailing separators.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u: &url.URL{ | 
					
						
							|  |  |  | 				Path: "/bucket/object///////", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object///////", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 7 object has preceding separators.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u: &url.URL{ | 
					
						
							|  |  |  | 				Path: "/bucket////object////", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "///object////", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 8 url is not allocated.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u:      nil, | 
					
						
							|  |  |  | 			bucket: "", | 
					
						
							|  |  |  | 			object: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 9 url path is empty.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			u:      &url.URL{}, | 
					
						
							|  |  |  | 			bucket: "", | 
					
						
							|  |  |  | 			object: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Validate all test cases.
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		bucketName, objectName := urlPath2BucketObjectName(testCase.u) | 
					
						
							|  |  |  | 		if bucketName != testCase.bucket { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: failed expected bucket name \"%s\", got \"%s\"", i+1, testCase.bucket, bucketName) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if objectName != testCase.object { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: failed expected bucket name \"%s\", got \"%s\"", i+1, testCase.object, objectName) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | // Add tests for starting and stopping different profilers.
 | 
					
						
							|  |  |  | func TestStartProfiler(t *testing.T) { | 
					
						
							|  |  |  | 	if startProfiler("") != nil { | 
					
						
							|  |  |  | 		t.Fatal("Expected nil, but non-nil value returned for invalid profiler.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 19:47:40 +08:00
										 |  |  | // TestCheckURL tests valid url.
 | 
					
						
							| 
									
										
										
										
											2017-03-17 02:44:01 +08:00
										 |  |  | func TestCheckURL(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2017-03-31 19:47:40 +08:00
										 |  |  | 		urlStr     string | 
					
						
							| 
									
										
										
										
											2017-03-17 02:44:01 +08:00
										 |  |  | 		shouldPass bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"", false}, | 
					
						
							|  |  |  | 		{":", false}, | 
					
						
							|  |  |  | 		{"http://localhost/", true}, | 
					
						
							|  |  |  | 		{"http://127.0.0.1/", true}, | 
					
						
							|  |  |  | 		{"proto://myhostname/path", true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Validates fetching local address.
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2017-03-31 19:47:40 +08:00
										 |  |  | 		_, err := checkURL(testCase.urlStr) | 
					
						
							| 
									
										
										
										
											2017-03-17 02:44:01 +08:00
										 |  |  | 		if testCase.shouldPass && err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: expected to pass but got an error: %v\n", i+1, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !testCase.shouldPass && err == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: expected to fail but passed.", i+1) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Testing dumping request function.
 | 
					
						
							|  |  |  | func TestDumpRequest(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-05-31 15:11:06 +08:00
										 |  |  | 	req, err := http.NewRequest("GET", "http://localhost:9000?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=USWUXHGYZQYFYFFIT3RE%2F20170529%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20170529T190139Z&X-Amz-Expires=600&X-Amz-Signature=19b58080999df54b446fc97304eb8dda60d3df1812ae97f3e8783351bfd9781d&X-Amz-SignedHeaders=host&prefix=Hello%2AWorld%2A", nil) | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-31 15:11:06 +08:00
										 |  |  | 	req.RequestURI = "/?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=USWUXHGYZQYFYFFIT3RE%2F20170529%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20170529T190139Z&X-Amz-Expires=600&X-Amz-Signature=19b58080999df54b446fc97304eb8dda60d3df1812ae97f3e8783351bfd9781d&X-Amz-SignedHeaders=host&prefix=Hello%2AWorld%2A" | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 	req.Header.Set("content-md5", "====test") | 
					
						
							|  |  |  | 	jsonReq := dumpRequest(req) | 
					
						
							|  |  |  | 	type jsonResult struct { | 
					
						
							| 
									
										
										
										
											2017-05-31 15:11:06 +08:00
										 |  |  | 		Method     string      `json:"method"` | 
					
						
							|  |  |  | 		RequestURI string      `json:"reqURI"` | 
					
						
							|  |  |  | 		Header     http.Header `json:"header"` | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	jsonReq = strings.Replace(jsonReq, "%%", "%", -1) | 
					
						
							|  |  |  | 	res := jsonResult{} | 
					
						
							|  |  |  | 	if err = json.Unmarshal([]byte(jsonReq), &res); err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Look for expected method.
 | 
					
						
							|  |  |  | 	if res.Method != "GET" { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected method %s, expected 'GET'", res.Method) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Look for expected query values
 | 
					
						
							|  |  |  | 	expectedQuery := url.Values{} | 
					
						
							|  |  |  | 	expectedQuery.Set("prefix", "Hello*World*") | 
					
						
							| 
									
										
										
										
											2017-05-31 15:11:06 +08:00
										 |  |  | 	expectedQuery.Set("X-Amz-Algorithm", "AWS4-HMAC-SHA256") | 
					
						
							|  |  |  | 	expectedQuery.Set("X-Amz-Credential", "USWUXHGYZQYFYFFIT3RE/20170529/us-east-1/s3/aws4_request") | 
					
						
							|  |  |  | 	expectedQuery.Set("X-Amz-Date", "20170529T190139Z") | 
					
						
							|  |  |  | 	expectedQuery.Set("X-Amz-Expires", "600") | 
					
						
							|  |  |  | 	expectedQuery.Set("X-Amz-SignedHeaders", "host") | 
					
						
							|  |  |  | 	expectedQuery.Set("X-Amz-Signature", "19b58080999df54b446fc97304eb8dda60d3df1812ae97f3e8783351bfd9781d") | 
					
						
							|  |  |  | 	expectedRequestURI := "/?" + expectedQuery.Encode() | 
					
						
							|  |  |  | 	if !reflect.DeepEqual(res.RequestURI, expectedRequestURI) { | 
					
						
							|  |  |  | 		t.Fatalf("Expected %#v, got %#v", expectedRequestURI, res.RequestURI) | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Look for expected header.
 | 
					
						
							|  |  |  | 	expectedHeader := http.Header{} | 
					
						
							|  |  |  | 	expectedHeader.Set("content-md5", "====test") | 
					
						
							|  |  |  | 	expectedHeader.Set("host", "localhost:9000") | 
					
						
							|  |  |  | 	if !reflect.DeepEqual(res.Header, expectedHeader) { | 
					
						
							|  |  |  | 		t.Fatalf("Expected %#v, got %#v", expectedHeader, res.Header) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |