| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2015, 2016, 2017 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +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-04-29 11:01:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 	"encoding/xml" | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2017-03-14 05:41:13 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests validate bucket LocationConstraint.
 | 
					
						
							|  |  |  | func TestIsValidLocationContraint(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 	obj, fsDir, err := prepareFS() | 
					
						
							| 
									
										
										
										
											2016-07-19 12:20:17 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-08-15 12:41:47 +08:00
										 |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer os.RemoveAll(fsDir) | 
					
						
							|  |  |  | 	if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							| 
									
										
										
										
											2016-07-19 12:20:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 	// Corrupted XML
 | 
					
						
							| 
									
										
										
										
											2016-09-21 01:40:46 +08:00
										 |  |  | 	malformedReq := &http.Request{ | 
					
						
							|  |  |  | 		Body:          ioutil.NopCloser(bytes.NewBuffer([]byte("<>"))), | 
					
						
							|  |  |  | 		ContentLength: int64(len("<>")), | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Not an XML
 | 
					
						
							|  |  |  | 	badRequest := &http.Request{ | 
					
						
							|  |  |  | 		Body:          ioutil.NopCloser(bytes.NewReader([]byte("garbage"))), | 
					
						
							|  |  |  | 		ContentLength: int64(len("garbage")), | 
					
						
							| 
									
										
										
										
											2016-09-21 01:40:46 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 	// generates the input request with XML bucket configuration set to the request body.
 | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 	createExpectedRequest := func(req *http.Request, location string) *http.Request { | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 		createBucketConfig := createBucketLocationConfiguration{} | 
					
						
							|  |  |  | 		createBucketConfig.Location = location | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 		createBucketConfigBytes, _ := xml.Marshal(createBucketConfig) | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 		createBucketConfigBuffer := bytes.NewBuffer(createBucketConfigBytes) | 
					
						
							|  |  |  | 		req.Body = ioutil.NopCloser(createBucketConfigBuffer) | 
					
						
							| 
									
										
										
										
											2016-07-19 12:20:17 +08:00
										 |  |  | 		req.ContentLength = int64(createBucketConfigBuffer.Len()) | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 		return req | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 		request            *http.Request | 
					
						
							|  |  |  | 		serverConfigRegion string | 
					
						
							|  |  |  | 		expectedCode       APIErrorCode | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		// Test case - 1.
 | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 		{createExpectedRequest(&http.Request{}, "eu-central-1"), globalMinioDefaultRegion, ErrNone}, | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 		// Test case - 2.
 | 
					
						
							|  |  |  | 		// In case of empty request body ErrNone is returned.
 | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 		{createExpectedRequest(&http.Request{}, ""), globalMinioDefaultRegion, ErrNone}, | 
					
						
							|  |  |  | 		// Test case - 3
 | 
					
						
							|  |  |  | 		// In case of garbage request body ErrMalformedXML is returned.
 | 
					
						
							|  |  |  | 		{badRequest, globalMinioDefaultRegion, ErrMalformedXML}, | 
					
						
							|  |  |  | 		// Test case - 4
 | 
					
						
							|  |  |  | 		// In case of invalid XML request body ErrMalformedXML is returned.
 | 
					
						
							|  |  |  | 		{malformedReq, globalMinioDefaultRegion, ErrMalformedXML}, | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2017-11-30 05:12:47 +08:00
										 |  |  | 		globalServerConfig.SetRegion(testCase.serverConfigRegion) | 
					
						
							| 
									
										
										
										
											2019-01-31 23:19:09 +08:00
										 |  |  | 		_, actualCode := parseLocationConstraint(testCase.request) | 
					
						
							| 
									
										
										
										
											2016-04-29 11:01:11 +08:00
										 |  |  | 		if testCase.expectedCode != actualCode { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected the APIErrCode to be %d, but instead found %d", i+1, testCase.expectedCode, actualCode) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 05:41:13 +08:00
										 |  |  | // Test validate form field size.
 | 
					
						
							|  |  |  | func TestValidateFormFieldSize(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		header http.Header | 
					
						
							|  |  |  | 		err    error | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// Empty header returns error as nil,
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			header: nil, | 
					
						
							|  |  |  | 			err:    nil, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Valid header returns error as nil.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"Content-Type": []string{"image/png"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			err: nil, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Invalid header value > maxFormFieldSize+1
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"Garbage": []string{strings.Repeat("a", int(maxFormFieldSize)+1)}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			err: errSizeUnexpected, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Run validate form field size check under all test cases.
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 		err := validateFormFieldSize(context.Background(), testCase.header) | 
					
						
							| 
									
										
										
										
											2017-03-14 05:41:13 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-04-11 00:36:37 +08:00
										 |  |  | 			if err.Error() != testCase.err.Error() { | 
					
						
							| 
									
										
										
										
											2017-03-14 05:41:13 +08:00
										 |  |  | 				t.Errorf("Test %d: Expected error %s, got %s", i+1, testCase.err, err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | // Tests validate metadata extraction from http headers.
 | 
					
						
							|  |  |  | func TestExtractMetadataHeaders(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 		header     http.Header | 
					
						
							|  |  |  | 		metadata   map[string]string | 
					
						
							|  |  |  | 		shouldFail bool | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		// Validate if there a known 'content-type'.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"Content-Type": []string{"image/png"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			metadata: map[string]string{ | 
					
						
							|  |  |  | 				"content-type": "image/png", | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 			shouldFail: false, | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// Validate if there are no keys to extract.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 				"Test-1": []string{"123"}, | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 			metadata:   map[string]string{}, | 
					
						
							|  |  |  | 			shouldFail: false, | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 		// Validate that there are all headers extracted
 | 
					
						
							| 
									
										
										
										
											2016-09-21 01:40:46 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"X-Amz-Meta-Appid":   []string{"amz-meta"}, | 
					
						
							|  |  |  | 				"X-Minio-Meta-Appid": []string{"minio-meta"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			metadata: map[string]string{ | 
					
						
							|  |  |  | 				"X-Amz-Meta-Appid":   "amz-meta", | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 				"X-Minio-Meta-Appid": "minio-meta", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			shouldFail: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Fail if header key is not in canonicalized form
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"x-amz-meta-appid": []string{"amz-meta"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			metadata: map[string]string{ | 
					
						
							| 
									
										
										
										
											2018-07-11 11:27:10 +08:00
										 |  |  | 				"x-amz-meta-appid": "amz-meta", | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2018-07-11 11:27:10 +08:00
										 |  |  | 			shouldFail: false, | 
					
						
							| 
									
										
										
										
											2016-09-21 01:40:46 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-11-03 02:32:18 +08:00
										 |  |  | 		// Support multiple values
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"x-amz-meta-key": []string{"amz-meta1", "amz-meta2"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			metadata: map[string]string{ | 
					
						
							|  |  |  | 				"x-amz-meta-key": "amz-meta1,amz-meta2", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			shouldFail: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-03-14 05:41:13 +08:00
										 |  |  | 		// Empty header input returns empty metadata.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 			header:     nil, | 
					
						
							|  |  |  | 			metadata:   nil, | 
					
						
							|  |  |  | 			shouldFail: true, | 
					
						
							| 
									
										
										
										
											2017-03-14 05:41:13 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Validate if the extracting headers.
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2018-07-11 11:27:10 +08:00
										 |  |  | 		metadata := make(map[string]string) | 
					
						
							|  |  |  | 		err := extractMetadataFromMap(context.Background(), testCase.header, metadata) | 
					
						
							| 
									
										
										
										
											2017-07-06 07:56:10 +08:00
										 |  |  | 		if err != nil && !testCase.shouldFail { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d failed to extract metadata: %v", i+1, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err == nil && testCase.shouldFail { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d should fail, but it passed", i+1) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err == nil && !reflect.DeepEqual(metadata, testCase.metadata) { | 
					
						
							| 
									
										
										
										
											2016-07-23 11:31:45 +08:00
										 |  |  | 			t.Fatalf("Test %d failed: Expected \"%#v\", got \"%#v\"", i+1, testCase.metadata, metadata) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-15 08:56:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Test getResource()
 | 
					
						
							|  |  |  | func TestGetResource(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		p                string | 
					
						
							|  |  |  | 		host             string | 
					
						
							| 
									
										
										
										
											2019-02-23 11:18:01 +08:00
										 |  |  | 		domains          []string | 
					
						
							| 
									
										
										
										
											2017-11-15 08:56:24 +08:00
										 |  |  | 		expectedResource string | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2019-02-23 11:18:01 +08:00
										 |  |  | 		{"/a/b/c", "test.mydomain.com", []string{"mydomain.com"}, "/test/a/b/c"}, | 
					
						
							|  |  |  | 		{"/a/b/c", "test.mydomain.com", []string{"notmydomain.com"}, "/a/b/c"}, | 
					
						
							|  |  |  | 		{"/a/b/c", "test.mydomain.com", nil, "/a/b/c"}, | 
					
						
							| 
									
										
										
										
											2017-11-15 08:56:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							| 
									
										
										
										
											2019-02-23 11:18:01 +08:00
										 |  |  | 		gotResource, err := getResource(test.p, test.host, test.domains) | 
					
						
							| 
									
										
										
										
											2017-11-15 08:56:24 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if gotResource != test.expectedResource { | 
					
						
							|  |  |  | 			t.Fatalf("test %d: expected %s got %s", i+1, test.expectedResource, gotResource) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |