| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2016-2019 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +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-03-08 11:30:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-03-06 00:35:37 +08:00
										 |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2018-01-04 14:14:45 +08:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2019-03-06 00:35:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 	"github.com/klauspost/compress/s2" | 
					
						
							|  |  |  | 	"github.com/minio/minio/cmd/crypto" | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 08:35:38 +08:00
										 |  |  | // Tests validate bucket name.
 | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | func TestIsValidBucketName(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		bucketName string | 
					
						
							|  |  |  | 		shouldPass bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2016-04-28 10:28:13 +08:00
										 |  |  | 		// cases which should pass the test.
 | 
					
						
							|  |  |  | 		// passing in valid bucket names.
 | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"lol", true}, | 
					
						
							|  |  |  | 		{"1-this-is-valid", true}, | 
					
						
							|  |  |  | 		{"1-this-too-is-valid-1", true}, | 
					
						
							|  |  |  | 		{"this.works.too.1", true}, | 
					
						
							|  |  |  | 		{"1234567", true}, | 
					
						
							|  |  |  | 		{"123", true}, | 
					
						
							|  |  |  | 		{"s3-eu-west-1.amazonaws.com", true}, | 
					
						
							|  |  |  | 		{"ideas-are-more-powerful-than-guns", true}, | 
					
						
							|  |  |  | 		{"testbucket", true}, | 
					
						
							|  |  |  | 		{"1bucket", true}, | 
					
						
							|  |  |  | 		{"bucket1", true}, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:23:41 +08:00
										 |  |  | 		{"a.b", true}, | 
					
						
							|  |  |  | 		{"ab.a.bc", true}, | 
					
						
							| 
									
										
										
										
											2016-04-28 10:28:13 +08:00
										 |  |  | 		// cases for which test should fail.
 | 
					
						
							|  |  |  | 		// passing invalid bucket names.
 | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"------", false}, | 
					
						
							| 
									
										
										
										
											2016-07-16 08:30:37 +08:00
										 |  |  | 		{"my..bucket", false}, | 
					
						
							|  |  |  | 		{"192.168.1.1", false}, | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"$this-is-not-valid-too", false}, | 
					
						
							|  |  |  | 		{"contains-$-dollar", false}, | 
					
						
							| 
									
										
										
										
											2016-10-01 07:56:36 +08:00
										 |  |  | 		{"contains-^-carret", false}, | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"contains-$-dollar", false}, | 
					
						
							|  |  |  | 		{"contains-$-dollar", false}, | 
					
						
							|  |  |  | 		{"......", false}, | 
					
						
							|  |  |  | 		{"", false}, | 
					
						
							|  |  |  | 		{"a", false}, | 
					
						
							|  |  |  | 		{"ab", false}, | 
					
						
							|  |  |  | 		{".starts-with-a-dot", false}, | 
					
						
							|  |  |  | 		{"ends-with-a-dot.", false}, | 
					
						
							|  |  |  | 		{"ends-with-a-dash-", false}, | 
					
						
							|  |  |  | 		{"-starts-with-a-dash", false}, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:23:41 +08:00
										 |  |  | 		{"THIS-BEGINS-WITH-UPPERCASe", false}, | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"tHIS-ENDS-WITH-UPPERCASE", false}, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:23:41 +08:00
										 |  |  | 		{"ThisBeginsAndEndsWithUpperCasE", false}, | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"una ñina", false}, | 
					
						
							| 
									
										
										
										
											2017-03-04 02:23:41 +08:00
										 |  |  | 		{"dash-.may-not-appear-next-to-dot", false}, | 
					
						
							|  |  |  | 		{"dash.-may-not-appear-next-to-dot", false}, | 
					
						
							|  |  |  | 		{"dash-.-may-not-appear-next-to-dot", false}, | 
					
						
							|  |  |  | 		{"lalalallalallalalalallalallalala-thestring-size-is-greater-than-63", false}, | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2016-04-21 08:35:38 +08:00
										 |  |  | 		isValidBucketName := IsValidBucketName(testCase.bucketName) | 
					
						
							|  |  |  | 		if testCase.shouldPass && !isValidBucketName { | 
					
						
							|  |  |  | 			t.Errorf("Test case %d: Expected \"%s\" to be a valid bucket name", i+1, testCase.bucketName) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !testCase.shouldPass && isValidBucketName { | 
					
						
							|  |  |  | 			t.Errorf("Test case %d: Expected bucket name \"%s\" to be invalid", i+1, testCase.bucketName) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 08:35:38 +08:00
										 |  |  | // Tests for validate object name.
 | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | func TestIsValidObjectName(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		objectName string | 
					
						
							|  |  |  | 		shouldPass bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2016-04-28 10:28:13 +08:00
										 |  |  | 		// cases which should pass the test.
 | 
					
						
							|  |  |  | 		// passing in valid object name.
 | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"object", true}, | 
					
						
							|  |  |  | 		{"The Shining Script <v1>.pdf", true}, | 
					
						
							|  |  |  | 		{"Cost Benefit Analysis (2009-2010).pptx", true}, | 
					
						
							|  |  |  | 		{"117Gn8rfHL2ACARPAhaFd0AGzic9pUbIA/5OCn5A", true}, | 
					
						
							|  |  |  | 		{"SHØRT", true}, | 
					
						
							| 
									
										
										
										
											2016-08-19 03:00:01 +08:00
										 |  |  | 		{"f*le", true}, | 
					
						
							| 
									
										
										
										
											2016-10-01 07:56:36 +08:00
										 |  |  | 		{"contains-^-carret", true}, | 
					
						
							|  |  |  | 		{"contains-|-pipe", true}, | 
					
						
							|  |  |  | 		{"contains-\"-quote", true}, | 
					
						
							|  |  |  | 		{"contains-`-tick", true}, | 
					
						
							| 
									
										
										
										
											2017-04-25 09:13:46 +08:00
										 |  |  | 		{"..test", true}, | 
					
						
							|  |  |  | 		{".. test", true}, | 
					
						
							|  |  |  | 		{". test", true}, | 
					
						
							|  |  |  | 		{".test", true}, | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"There are far too many object names, and far too few bucket names!", true}, | 
					
						
							| 
									
										
										
										
											2016-04-28 10:28:13 +08:00
										 |  |  | 		// cases for which test should fail.
 | 
					
						
							|  |  |  | 		// passing invalid object names.
 | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{"", false}, | 
					
						
							| 
									
										
										
										
											2016-05-14 02:43:06 +08:00
										 |  |  | 		{"a/b/c/", false}, | 
					
						
							| 
									
										
										
										
											2017-04-25 09:13:46 +08:00
										 |  |  | 		{"../../etc", false}, | 
					
						
							|  |  |  | 		{"../../", false}, | 
					
						
							|  |  |  | 		{"/../../etc", false}, | 
					
						
							|  |  |  | 		{" ../etc", false}, | 
					
						
							|  |  |  | 		{"./././", false}, | 
					
						
							|  |  |  | 		{"./etc", false}, | 
					
						
							| 
									
										
										
										
											2016-10-01 07:56:36 +08:00
										 |  |  | 		{"contains-\\-backslash", false}, | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 		{string([]byte{0xff, 0xfe, 0xfd}), false}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-11 16:14:29 +08:00
										 |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2016-04-21 08:35:38 +08:00
										 |  |  | 		isValidObjectName := IsValidObjectName(testCase.objectName) | 
					
						
							|  |  |  | 		if testCase.shouldPass && !isValidObjectName { | 
					
						
							|  |  |  | 			t.Errorf("Test case %d: Expected \"%s\" to be a valid object name", i+1, testCase.objectName) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !testCase.shouldPass && isValidObjectName { | 
					
						
							|  |  |  | 			t.Errorf("Test case %d: Expected object name \"%s\" to be invalid", i+1, testCase.objectName) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-23 00:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 04:56:13 +08:00
										 |  |  | // Tests getCompleteMultipartMD5
 | 
					
						
							|  |  |  | func TestGetCompleteMultipartMD5(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2017-11-14 16:25:10 +08:00
										 |  |  | 		parts          []CompletePart | 
					
						
							| 
									
										
										
										
											2016-10-25 04:56:13 +08:00
										 |  |  | 		expectedResult string | 
					
						
							|  |  |  | 		expectedErr    string | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2019-05-09 09:35:40 +08:00
										 |  |  | 		// Wrong MD5 hash string, returns md5um of hash
 | 
					
						
							|  |  |  | 		{[]CompletePart{{ETag: "wrong-md5-hash-string"}}, "0deb8cb07527b4b2669c861cb9653607-1", ""}, | 
					
						
							| 
									
										
										
										
											2016-10-25 04:56:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 16:25:10 +08:00
										 |  |  | 		// Single CompletePart with valid MD5 hash string.
 | 
					
						
							|  |  |  | 		{[]CompletePart{{ETag: "cf1f738a5924e645913c984e0fe3d708"}}, "10dc1617fbcf0bd0858048cb96e6bd77-1", ""}, | 
					
						
							| 
									
										
										
										
											2016-10-25 04:56:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 16:25:10 +08:00
										 |  |  | 		// Multiple CompletePart with valid MD5 hash string.
 | 
					
						
							|  |  |  | 		{[]CompletePart{{ETag: "cf1f738a5924e645913c984e0fe3d708"}, {ETag: "9ccbc9a80eee7fb6fdd22441db2aedbd"}}, "0239a86b5266bb624f0ac60ba2aed6c8-2", ""}, | 
					
						
							| 
									
										
										
										
											2016-10-25 04:56:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							| 
									
										
										
										
											2019-05-09 09:35:40 +08:00
										 |  |  | 		result := getCompleteMultipartMD5(test.parts) | 
					
						
							| 
									
										
										
										
											2016-10-25 04:56:13 +08:00
										 |  |  | 		if result != test.expectedResult { | 
					
						
							|  |  |  | 			t.Fatalf("test %d failed: expected: result=%v, got=%v", i+1, test.expectedResult, result) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-18 02:02:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TestIsMinioBucketName - Tests isMinioBucketName helper function.
 | 
					
						
							|  |  |  | func TestIsMinioMetaBucketName(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		bucket string | 
					
						
							|  |  |  | 		result bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  | 		// MinIO meta bucket.
 | 
					
						
							| 
									
										
										
										
											2017-01-18 02:02:58 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			bucket: minioMetaBucket, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  | 		// MinIO meta bucket.
 | 
					
						
							| 
									
										
										
										
											2017-01-18 02:02:58 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			bucket: minioMetaMultipartBucket, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  | 		// MinIO meta bucket.
 | 
					
						
							| 
									
										
										
										
											2017-01-18 02:02:58 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			bucket: minioMetaTmpBucket, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Normal bucket
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bucket: "mybucket", | 
					
						
							|  |  |  | 			result: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							|  |  |  | 		actual := isMinioMetaBucketName(test.bucket) | 
					
						
							|  |  |  | 		if actual != test.result { | 
					
						
							|  |  |  | 			t.Errorf("Test %d - expected %v but received %v", | 
					
						
							|  |  |  | 				i+1, test.result, actual) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-04 14:14:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Tests RemoveStandardStorageClass method. Expectation is metadata map
 | 
					
						
							|  |  |  | // should be cleared of x-amz-storage-class, if it is set to STANDARD
 | 
					
						
							|  |  |  | func TestRemoveStandardStorageClass(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name     string | 
					
						
							|  |  |  | 		metadata map[string]string | 
					
						
							|  |  |  | 		want     map[string]string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "1", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "x-amz-storage-class": "STANDARD"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "2", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "x-amz-storage-class": "REDUCED_REDUNDANCY"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "x-amz-storage-class": "REDUCED_REDUNDANCY"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "3", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							|  |  |  | 		if got := removeStandardStorageClass(tt.metadata); !reflect.DeepEqual(got, tt.want) { | 
					
						
							|  |  |  | 			t.Errorf("Test %s failed, expected %v, got %v", tt.name, tt.want, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests CleanMetadata method. Expectation is metadata map
 | 
					
						
							|  |  |  | // should be cleared of etag, md5Sum and x-amz-storage-class, if it is set to STANDARD
 | 
					
						
							|  |  |  | func TestCleanMetadata(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name     string | 
					
						
							|  |  |  | 		metadata map[string]string | 
					
						
							|  |  |  | 		want     map[string]string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "1", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "x-amz-storage-class": "STANDARD"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "2", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "x-amz-storage-class": "REDUCED_REDUNDANCY"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream", "x-amz-storage-class": "REDUCED_REDUNDANCY"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "3", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "md5Sum": "abcde"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							|  |  |  | 		if got := cleanMetadata(tt.metadata); !reflect.DeepEqual(got, tt.want) { | 
					
						
							|  |  |  | 			t.Errorf("Test %s failed, expected %v, got %v", tt.name, tt.want, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests CleanMetadataKeys method. Expectation is metadata map
 | 
					
						
							|  |  |  | // should be cleared of keys passed to CleanMetadataKeys method
 | 
					
						
							|  |  |  | func TestCleanMetadataKeys(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name     string | 
					
						
							|  |  |  | 		metadata map[string]string | 
					
						
							|  |  |  | 		keys     []string | 
					
						
							|  |  |  | 		want     map[string]string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "1", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "x-amz-storage-class": "STANDARD", "md5": "abcde"}, | 
					
						
							|  |  |  | 			keys:     []string{"etag", "md5"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream", "x-amz-storage-class": "STANDARD"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "2", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "x-amz-storage-class": "REDUCED_REDUNDANCY", "md5sum": "abcde"}, | 
					
						
							|  |  |  | 			keys:     []string{"etag", "md5sum"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream", "x-amz-storage-class": "REDUCED_REDUNDANCY"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name:     "3", | 
					
						
							|  |  |  | 			metadata: map[string]string{"content-type": "application/octet-stream", "etag": "de75a98baf2c6aef435b57dd0fc33c86", "xyz": "abcde"}, | 
					
						
							|  |  |  | 			keys:     []string{"etag", "xyz"}, | 
					
						
							|  |  |  | 			want:     map[string]string{"content-type": "application/octet-stream"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							|  |  |  | 		if got := cleanMetadataKeys(tt.metadata, tt.keys...); !reflect.DeepEqual(got, tt.want) { | 
					
						
							|  |  |  | 			t.Errorf("Test %s failed, expected %v, got %v", tt.name, tt.want, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Tests isCompressed method
 | 
					
						
							|  |  |  | func TestIsCompressed(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		objInfo ObjectInfo | 
					
						
							|  |  |  | 		result  bool | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 		err     bool | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 				UserDefined: map[string]string{"X-Minio-Internal-compression": compressionAlgorithmV1, | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					"content-type": "application/octet-stream", | 
					
						
							|  |  |  | 					"etag":         "b3ff3ef3789147152fbfbc50efba4bfd-2"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 				UserDefined: map[string]string{"X-Minio-Internal-compression": compressionAlgorithmV2, | 
					
						
							|  |  |  | 					"content-type": "application/octet-stream", | 
					
						
							|  |  |  | 					"etag":         "b3ff3ef3789147152fbfbc50efba4bfd-2"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							|  |  |  | 				UserDefined: map[string]string{"X-Minio-Internal-compression": "unknown/compression/type", | 
					
						
							|  |  |  | 					"content-type": "application/octet-stream", | 
					
						
							|  |  |  | 					"etag":         "b3ff3ef3789147152fbfbc50efba4bfd-2"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 			err:    true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							|  |  |  | 				UserDefined: map[string]string{"X-Minio-Internal-compression": compressionAlgorithmV2, | 
					
						
							|  |  |  | 					"content-type": "application/octet-stream", | 
					
						
							|  |  |  | 					"etag":         "b3ff3ef3789147152fbfbc50efba4bfd-2", | 
					
						
							|  |  |  | 					crypto.SSEIV:   "yes", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 			err:    true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							|  |  |  | 				UserDefined: map[string]string{"X-Minio-Internal-XYZ": "klauspost/compress/s2", | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					"content-type": "application/octet-stream", | 
					
						
							|  |  |  | 					"etag":         "b3ff3ef3789147152fbfbc50efba4bfd-2"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							|  |  |  | 				UserDefined: map[string]string{"content-type": "application/octet-stream", | 
					
						
							|  |  |  | 					"etag": "b3ff3ef3789147152fbfbc50efba4bfd-2"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 		t.Run(strconv.Itoa(i), func(t *testing.T) { | 
					
						
							|  |  |  | 			got := test.objInfo.IsCompressed() | 
					
						
							|  |  |  | 			if got != test.result { | 
					
						
							|  |  |  | 				t.Errorf("IsCompressed: Expected %v but received %v", | 
					
						
							|  |  |  | 					test.result, got) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			got, gErr := test.objInfo.IsCompressedOK() | 
					
						
							|  |  |  | 			if got != test.result { | 
					
						
							|  |  |  | 				t.Errorf("IsCompressedOK: Expected %v but received %v", | 
					
						
							|  |  |  | 					test.result, got) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if gErr != nil != test.err { | 
					
						
							|  |  |  | 				t.Errorf("IsCompressedOK: want error: %t, got error: %v", test.err, gErr) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests excludeForCompression.
 | 
					
						
							|  |  |  | func TestExcludeForCompression(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		object string | 
					
						
							|  |  |  | 		header http.Header | 
					
						
							|  |  |  | 		result bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			object: "object.txt", | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"Content-Type": []string{"application/zip"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			object: "object.zip", | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"Content-Type": []string{"application/XYZ"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			object: "object.json", | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"Content-Type": []string{"application/json"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			object: "object.txt", | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"Content-Type": []string{"text/plain"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			object: "object", | 
					
						
							|  |  |  | 			header: http.Header{ | 
					
						
							|  |  |  | 				"Content-Type": []string{"text/something"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							|  |  |  | 		globalIsCompressionEnabled = true | 
					
						
							|  |  |  | 		got := excludeForCompression(test.header, test.object) | 
					
						
							|  |  |  | 		globalIsCompressionEnabled = false | 
					
						
							|  |  |  | 		if got != test.result { | 
					
						
							|  |  |  | 			t.Errorf("Test %d - expected %v but received %v", | 
					
						
							|  |  |  | 				i+1, test.result, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test getPartFile function.
 | 
					
						
							|  |  |  | func TestGetPartFile(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		entries    []string | 
					
						
							|  |  |  | 		partNumber int | 
					
						
							|  |  |  | 		etag       string | 
					
						
							|  |  |  | 		result     string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			entries:    []string{"00001.8a034f82cb9cb31140d87d3ce2a9ede3.67108864", "fs.json", "00002.d73d8ab724016dfb051e2d3584495c54.32891137"}, | 
					
						
							|  |  |  | 			partNumber: 1, | 
					
						
							|  |  |  | 			etag:       "8a034f82cb9cb31140d87d3ce2a9ede3", | 
					
						
							|  |  |  | 			result:     "00001.8a034f82cb9cb31140d87d3ce2a9ede3.67108864", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			entries:    []string{"00001.8a034f82cb9cb31140d87d3ce2a9ede3.67108864", "fs.json", "00002.d73d8ab724016dfb051e2d3584495c54.32891137"}, | 
					
						
							|  |  |  | 			partNumber: 2, | 
					
						
							|  |  |  | 			etag:       "d73d8ab724016dfb051e2d3584495c54", | 
					
						
							|  |  |  | 			result:     "00002.d73d8ab724016dfb051e2d3584495c54.32891137", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			entries:    []string{"00001.8a034f82cb9cb31140d87d3ce2a9ede3.67108864", "fs.json", "00002.d73d8ab724016dfb051e2d3584495c54.32891137"}, | 
					
						
							|  |  |  | 			partNumber: 1, | 
					
						
							|  |  |  | 			etag:       "d73d8ab724016dfb051e2d3584495c54", | 
					
						
							|  |  |  | 			result:     "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							|  |  |  | 		got := getPartFile(test.entries, test.partNumber, test.etag) | 
					
						
							|  |  |  | 		if got != test.result { | 
					
						
							|  |  |  | 			t.Errorf("Test %d - expected %s but received %s", | 
					
						
							|  |  |  | 				i+1, test.result, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetActualSize(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		objInfo ObjectInfo | 
					
						
							|  |  |  | 		result  int64 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 				UserDefined: map[string]string{"X-Minio-Internal-compression": "klauspost/compress/s2", | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					"X-Minio-Internal-actual-size": "100000001", | 
					
						
							|  |  |  | 					"content-type":                 "application/octet-stream", | 
					
						
							|  |  |  | 					"etag":                         "b3ff3ef3789147152fbfbc50efba4bfd-2"}, | 
					
						
							| 
									
										
										
										
											2019-01-06 06:16:43 +08:00
										 |  |  | 				Parts: []ObjectPartInfo{ | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					{ | 
					
						
							|  |  |  | 						Size:       39235668, | 
					
						
							|  |  |  | 						ActualSize: 67108864, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						Size:       19177372, | 
					
						
							|  |  |  | 						ActualSize: 32891137, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			result: 100000001, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 				UserDefined: map[string]string{"X-Minio-Internal-compression": "klauspost/compress/s2", | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					"X-Minio-Internal-actual-size": "841", | 
					
						
							|  |  |  | 					"content-type":                 "application/octet-stream", | 
					
						
							|  |  |  | 					"etag":                         "b3ff3ef3789147152fbfbc50efba4bfd-2"}, | 
					
						
							| 
									
										
										
										
											2019-01-06 06:16:43 +08:00
										 |  |  | 				Parts: []ObjectPartInfo{}, | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			result: 841, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 				UserDefined: map[string]string{"X-Minio-Internal-compression": "klauspost/compress/s2", | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					"content-type": "application/octet-stream", | 
					
						
							|  |  |  | 					"etag":         "b3ff3ef3789147152fbfbc50efba4bfd-2"}, | 
					
						
							| 
									
										
										
										
											2019-01-06 06:16:43 +08:00
										 |  |  | 				Parts: []ObjectPartInfo{}, | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			result: -1, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							|  |  |  | 		got := test.objInfo.GetActualSize() | 
					
						
							|  |  |  | 		if got != test.result { | 
					
						
							|  |  |  | 			t.Errorf("Test %d - expected %d but received %d", | 
					
						
							|  |  |  | 				i+1, test.result, got) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetCompressedOffsets(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		objInfo           ObjectInfo | 
					
						
							|  |  |  | 		offset            int64 | 
					
						
							|  |  |  | 		startOffset       int64 | 
					
						
							|  |  |  | 		snappyStartOffset int64 | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							| 
									
										
										
										
											2019-01-06 06:16:43 +08:00
										 |  |  | 				Parts: []ObjectPartInfo{ | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					{ | 
					
						
							|  |  |  | 						Size:       39235668, | 
					
						
							|  |  |  | 						ActualSize: 67108864, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						Size:       19177372, | 
					
						
							|  |  |  | 						ActualSize: 32891137, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			offset:            79109865, | 
					
						
							|  |  |  | 			startOffset:       39235668, | 
					
						
							|  |  |  | 			snappyStartOffset: 12001001, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							| 
									
										
										
										
											2019-01-06 06:16:43 +08:00
										 |  |  | 				Parts: []ObjectPartInfo{ | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					{ | 
					
						
							|  |  |  | 						Size:       39235668, | 
					
						
							|  |  |  | 						ActualSize: 67108864, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						Size:       19177372, | 
					
						
							|  |  |  | 						ActualSize: 32891137, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			offset:            19109865, | 
					
						
							|  |  |  | 			startOffset:       0, | 
					
						
							|  |  |  | 			snappyStartOffset: 19109865, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			objInfo: ObjectInfo{ | 
					
						
							| 
									
										
										
										
											2019-01-06 06:16:43 +08:00
										 |  |  | 				Parts: []ObjectPartInfo{ | 
					
						
							| 
									
										
										
										
											2018-09-28 11:36:17 +08:00
										 |  |  | 					{ | 
					
						
							|  |  |  | 						Size:       39235668, | 
					
						
							|  |  |  | 						ActualSize: 67108864, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						Size:       19177372, | 
					
						
							|  |  |  | 						ActualSize: 32891137, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			offset:            0, | 
					
						
							|  |  |  | 			startOffset:       0, | 
					
						
							|  |  |  | 			snappyStartOffset: 0, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range testCases { | 
					
						
							|  |  |  | 		startOffset, snappyStartOffset := getCompressedOffsets(test.objInfo, test.offset) | 
					
						
							|  |  |  | 		if startOffset != test.startOffset { | 
					
						
							|  |  |  | 			t.Errorf("Test %d - expected startOffset %d but received %d", | 
					
						
							|  |  |  | 				i+1, test.startOffset, startOffset) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if snappyStartOffset != test.snappyStartOffset { | 
					
						
							|  |  |  | 			t.Errorf("Test %d - expected snappyOffset %d but received %d", | 
					
						
							|  |  |  | 				i+1, test.snappyStartOffset, snappyStartOffset) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-06 00:35:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | func TestS2CompressReader(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-03-06 00:35:37 +08:00
										 |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name string | 
					
						
							|  |  |  | 		data []byte | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{name: "empty", data: nil}, | 
					
						
							|  |  |  | 		{name: "small", data: []byte("hello, world")}, | 
					
						
							|  |  |  | 		{name: "large", data: bytes.Repeat([]byte("hello, world"), 1000)}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							|  |  |  | 		t.Run(tt.name, func(t *testing.T) { | 
					
						
							|  |  |  | 			buf := make([]byte, 100) // make small buffer to ensure multiple reads are required for large case
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 			r := newS2CompressReader(bytes.NewReader(tt.data)) | 
					
						
							|  |  |  | 			defer r.Close() | 
					
						
							| 
									
										
										
										
											2019-03-06 00:35:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			var rdrBuf bytes.Buffer | 
					
						
							|  |  |  | 			_, err := io.CopyBuffer(&rdrBuf, r, buf) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatal(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var stdBuf bytes.Buffer | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 			w := s2.NewWriter(&stdBuf) | 
					
						
							| 
									
										
										
										
											2019-03-06 00:35:37 +08:00
										 |  |  | 			_, err = io.CopyBuffer(w, bytes.NewReader(tt.data), buf) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatal(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			err = w.Close() | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatal(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var ( | 
					
						
							|  |  |  | 				got  = rdrBuf.Bytes() | 
					
						
							|  |  |  | 				want = stdBuf.Bytes() | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 			if !bytes.Equal(got, want) { | 
					
						
							|  |  |  | 				t.Errorf("encoded data does not match\n\t%q\n\t%q", got, want) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var decBuf bytes.Buffer | 
					
						
							| 
									
										
										
										
											2019-09-26 14:08:24 +08:00
										 |  |  | 			decRdr := s2.NewReader(&rdrBuf) | 
					
						
							| 
									
										
										
										
											2019-03-06 00:35:37 +08:00
										 |  |  | 			_, err = io.Copy(&decBuf, decRdr) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatal(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if !bytes.Equal(tt.data, decBuf.Bytes()) { | 
					
						
							|  |  |  | 				t.Errorf("roundtrip failed\n\t%q\n\t%q", tt.data, decBuf.Bytes()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |