| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2016-08-06 04:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2017-12-29 01:32:48 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2018-04-11 00:36:37 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2021-12-18 02:11:04 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-09-10 00:38:07 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | // Tests extracting bucket and objectname from various types of paths.
 | 
					
						
							|  |  |  | func TestPath2BucketObjectName(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 		path           string | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 		bucket, object string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// Test case 1 normal case.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 			path:   "/bucket/object", | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 2 where url only has separator.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 			path:   SlashSeparator, | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 			bucket: "", | 
					
						
							|  |  |  | 			object: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 3 only bucket is present.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 			path:   "/bucket", | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 4 many separators and object is a directory.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 			path:   "/bucket/object/1/", | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object/1/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 5 object has many trailing separators.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 			path:   "/bucket/object/1///", | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object/1///", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 6 object has only trailing separators.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 			path:   "/bucket/object///////", | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object///////", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Test case 7 object has preceding separators.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 			path:   "/bucket////object////", | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "///object////", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 		// Test case 8 url path is empty.
 | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 			path:   "", | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 			bucket: "", | 
					
						
							|  |  |  | 			object: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Validate all test cases.
 | 
					
						
							| 
									
										
										
										
											2020-01-22 06:07:49 +08:00
										 |  |  | 	for _, testCase := range testCases { | 
					
						
							|  |  |  | 		testCase := testCase | 
					
						
							|  |  |  | 		t.Run("", func(t *testing.T) { | 
					
						
							|  |  |  | 			bucketName, objectName := path2BucketObject(testCase.path) | 
					
						
							|  |  |  | 			if bucketName != testCase.bucket { | 
					
						
							|  |  |  | 				t.Errorf("failed expected bucket name \"%s\", got \"%s\"", testCase.bucket, bucketName) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if objectName != testCase.object { | 
					
						
							|  |  |  | 				t.Errorf("failed expected bucket name \"%s\", got \"%s\"", testCase.object, objectName) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | // Add tests for starting and stopping different profilers.
 | 
					
						
							|  |  |  | func TestStartProfiler(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-01-11 09:19:58 +08:00
										 |  |  | 	_, err := startProfiler("") | 
					
						
							| 
									
										
										
										
											2018-09-19 07:46:35 +08:00
										 |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		t.Fatal("Expected a non nil error, but nil error returned for invalid profiler.") | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 21:56:04 +08:00
										 |  |  | // checkURL - checks if passed address correspond
 | 
					
						
							|  |  |  | func checkURL(urlStr string) (*url.URL, error) { | 
					
						
							|  |  |  | 	if urlStr == "" { | 
					
						
							|  |  |  | 		return nil, errors.New("Address cannot be empty") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	u, err := url.Parse(urlStr) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("`%s` invalid: %s", urlStr, err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return u, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2020-07-21 03:52:49 +08:00
										 |  |  | 	req, err := http.NewRequest(http.MethodGet, "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
										 |  |  | 	} | 
					
						
							|  |  |  | 	res := jsonResult{} | 
					
						
							| 
									
										
										
										
											2021-11-17 01:28:29 +08:00
										 |  |  | 	if err = json.Unmarshal([]byte(strings.ReplaceAll(jsonReq, "%%", "%")), &res); err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Look for expected method.
 | 
					
						
							| 
									
										
										
										
											2020-07-21 03:52:49 +08:00
										 |  |  | 	if res.Method != http.MethodGet { | 
					
						
							| 
									
										
										
										
											2017-04-08 05:37:32 +08:00
										 |  |  | 		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) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-27 01:17:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | // Test ToS3ETag()
 | 
					
						
							| 
									
										
										
										
											2017-10-27 01:17:07 +08:00
										 |  |  | func TestToS3ETag(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		etag         string | 
					
						
							|  |  |  | 		expectedETag string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{`"8019e762"`, `8019e762-1`}, | 
					
						
							|  |  |  | 		{"5d57546eeb86b3eba68967292fba0644", "5d57546eeb86b3eba68967292fba0644-1"}, | 
					
						
							|  |  |  | 		{`"8019e762-1"`, `8019e762-1`}, | 
					
						
							|  |  |  | 		{"5d57546eeb86b3eba68967292fba0644-1", "5d57546eeb86b3eba68967292fba0644-1"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 		etag := ToS3ETag(testCase.etag) | 
					
						
							| 
									
										
										
										
											2017-10-27 01:17:07 +08:00
										 |  |  | 		if etag != testCase.expectedETag { | 
					
						
							|  |  |  | 			t.Fatalf("test %v: expected: %v, got: %v", i+1, testCase.expectedETag, etag) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-29 01:32:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-05 06:16:45 +08:00
										 |  |  | // Test ceilFrac
 | 
					
						
							|  |  |  | func TestCeilFrac(t *testing.T) { | 
					
						
							|  |  |  | 	cases := []struct { | 
					
						
							|  |  |  | 		numerator, denominator, ceiling int64 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{0, 1, 0}, | 
					
						
							|  |  |  | 		{-1, 2, 0}, | 
					
						
							|  |  |  | 		{1, 2, 1}, | 
					
						
							|  |  |  | 		{1, 1, 1}, | 
					
						
							|  |  |  | 		{3, 2, 2}, | 
					
						
							|  |  |  | 		{54, 11, 5}, | 
					
						
							|  |  |  | 		{45, 11, 5}, | 
					
						
							|  |  |  | 		{-4, 3, -1}, | 
					
						
							|  |  |  | 		{4, -3, -1}, | 
					
						
							|  |  |  | 		{-4, -3, 2}, | 
					
						
							|  |  |  | 		{3, 0, 0}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range cases { | 
					
						
							|  |  |  | 		ceiling := ceilFrac(testCase.numerator, testCase.denominator) | 
					
						
							|  |  |  | 		if ceiling != testCase.ceiling { | 
					
						
							|  |  |  | 			t.Errorf("Case %d: Unexpected result: %d", i, ceiling) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-11 00:36:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Test if isErrIgnored works correctly.
 | 
					
						
							|  |  |  | func TestIsErrIgnored(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 	errIgnored := fmt.Errorf("ignored error") | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2018-04-11 00:36:37 +08:00
										 |  |  | 		err     error | 
					
						
							|  |  |  | 		ignored bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			err:     nil, | 
					
						
							|  |  |  | 			ignored: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			err:     errIgnored, | 
					
						
							|  |  |  | 			ignored: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-07-28 06:32:19 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			err:     errFaultyDisk, | 
					
						
							|  |  |  | 			ignored: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-04-11 00:36:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2021-11-17 01:28:29 +08:00
										 |  |  | 		if ok := IsErrIgnored(testCase.err, append(baseIgnoredErrs, errIgnored)...); ok != testCase.ignored { | 
					
						
							| 
									
										
										
										
											2018-04-11 00:36:37 +08:00
										 |  |  | 			t.Errorf("Test: %d, Expected %t, got %t", i+1, testCase.ignored, ok) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-10-05 08:44:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Test queries()
 | 
					
						
							|  |  |  | func TestQueries(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2018-10-05 08:44:06 +08:00
										 |  |  | 		keys      []string | 
					
						
							|  |  |  | 		keyvalues []string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"aaaa", "bbbb"}, | 
					
						
							|  |  |  | 			[]string{"aaaa", "{aaaa:.*}", "bbbb", "{bbbb:.*}"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							|  |  |  | 		keyvalues := restQueries(test.keys...) | 
					
						
							|  |  |  | 		for j := range keyvalues { | 
					
						
							|  |  |  | 			if keyvalues[j] != test.keyvalues[j] { | 
					
						
							|  |  |  | 				t.Fatalf("test %d: keyvalues[%d] does not match", i+1, j) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-10 01:02:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestLCP(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2019-08-10 01:02:41 +08:00
										 |  |  | 		prefixes     []string | 
					
						
							|  |  |  | 		commonPrefix string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{[]string{"", ""}, ""}, | 
					
						
							|  |  |  | 		{[]string{"a", "b"}, ""}, | 
					
						
							|  |  |  | 		{[]string{"a", "a"}, "a"}, | 
					
						
							|  |  |  | 		{[]string{"a/", "a/"}, "a/"}, | 
					
						
							|  |  |  | 		{[]string{"abcd/", ""}, ""}, | 
					
						
							|  |  |  | 		{[]string{"abcd/foo/", "abcd/bar/"}, "abcd/"}, | 
					
						
							|  |  |  | 		{[]string{"abcd/foo/bar/", "abcd/foo/bar/zoo"}, "abcd/foo/bar/"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							| 
									
										
										
										
											2021-01-29 03:44:48 +08:00
										 |  |  | 		foundPrefix := lcp(test.prefixes, true) | 
					
						
							| 
									
										
										
										
											2019-08-10 01:02:41 +08:00
										 |  |  | 		if foundPrefix != test.commonPrefix { | 
					
						
							|  |  |  | 			t.Fatalf("Test %d: Common prefix found: `%v`, expected: `%v`", i+1, foundPrefix, test.commonPrefix) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-15 02:43:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetMinioMode(t *testing.T) { | 
					
						
							|  |  |  | 	testMinioMode := func(expected string) { | 
					
						
							|  |  |  | 		if mode := getMinioMode(); mode != expected { | 
					
						
							|  |  |  | 			t.Fatalf("Expected %s got %s", expected, mode) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	globalIsDistErasure = true | 
					
						
							|  |  |  | 	testMinioMode(globalMinioModeDistErasure) | 
					
						
							| 
									
										
										
										
											2019-08-15 02:43:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	globalIsDistErasure = false | 
					
						
							|  |  |  | 	globalIsErasure = true | 
					
						
							|  |  |  | 	testMinioMode(globalMinioModeErasure) | 
					
						
							| 
									
										
										
										
											2019-08-15 02:43:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	globalIsDistErasure, globalIsErasure = false, false | 
					
						
							| 
									
										
										
										
											2019-08-15 02:43:43 +08:00
										 |  |  | 	testMinioMode(globalMinioModeFS) | 
					
						
							| 
									
										
										
										
											2019-08-10 01:02:41 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-12-18 02:11:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestTimedValue(t *testing.T) { | 
					
						
							|  |  |  | 	var cache timedValue | 
					
						
							|  |  |  | 	t.Parallel() | 
					
						
							|  |  |  | 	cache.Once.Do(func() { | 
					
						
							|  |  |  | 		cache.TTL = 2 * time.Second | 
					
						
							|  |  |  | 		cache.Update = func() (interface{}, error) { | 
					
						
							|  |  |  | 			return time.Now(), nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	i, _ := cache.Get() | 
					
						
							|  |  |  | 	t1 := i.(time.Time) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	j, _ := cache.Get() | 
					
						
							|  |  |  | 	t2 := j.(time.Time) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !t1.Equal(t2) { | 
					
						
							|  |  |  | 		t.Fatalf("expected time to be equal: %s != %s", t1, t2) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	time.Sleep(3 * time.Second) | 
					
						
							|  |  |  | 	k, _ := cache.Get() | 
					
						
							|  |  |  | 	t3 := k.(time.Time) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if t1.Equal(t3) { | 
					
						
							|  |  |  | 		t.Fatalf("expected time to be un-equal: %s == %s", t1, t3) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |