2016-03-07 22:30:30 -05:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-03-07 22:30:30 -05:00
|
|
|
|
|
|
|
import (
|
2018-01-04 01:14:45 -05:00
|
|
|
"reflect"
|
2016-03-07 22:30:30 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-04-20 20:35:38 -04:00
|
|
|
// Tests validate bucket name.
|
2016-03-07 22:30:30 -05:00
|
|
|
func TestIsValidBucketName(t *testing.T) {
|
2016-03-11 03:14:29 -05:00
|
|
|
testCases := []struct {
|
|
|
|
bucketName string
|
|
|
|
shouldPass bool
|
|
|
|
}{
|
2016-04-27 22:28:13 -04:00
|
|
|
// cases which should pass the test.
|
|
|
|
// passing in valid bucket names.
|
2016-03-11 03:14:29 -05: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-03 13:23:41 -05:00
|
|
|
{"a.b", true},
|
|
|
|
{"ab.a.bc", true},
|
2016-04-27 22:28:13 -04:00
|
|
|
// cases for which test should fail.
|
|
|
|
// passing invalid bucket names.
|
2016-03-11 03:14:29 -05:00
|
|
|
{"------", false},
|
2016-07-15 20:30:37 -04:00
|
|
|
{"my..bucket", false},
|
|
|
|
{"192.168.1.1", false},
|
2016-03-11 03:14:29 -05:00
|
|
|
{"$this-is-not-valid-too", false},
|
|
|
|
{"contains-$-dollar", false},
|
2016-09-30 19:56:36 -04:00
|
|
|
{"contains-^-carret", false},
|
2016-03-11 03:14:29 -05: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-03 13:23:41 -05:00
|
|
|
{"THIS-BEGINS-WITH-UPPERCASe", false},
|
2016-03-11 03:14:29 -05:00
|
|
|
{"tHIS-ENDS-WITH-UPPERCASE", false},
|
2017-03-03 13:23:41 -05:00
|
|
|
{"ThisBeginsAndEndsWithUpperCasE", false},
|
2016-03-11 03:14:29 -05:00
|
|
|
{"una ñina", false},
|
2017-03-03 13:23:41 -05: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 03:14:29 -05:00
|
|
|
}
|
2016-03-07 22:30:30 -05:00
|
|
|
|
2016-03-11 03:14:29 -05:00
|
|
|
for i, testCase := range testCases {
|
2016-04-20 20:35:38 -04: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 03:14:29 -05:00
|
|
|
}
|
2016-03-07 22:30:30 -05:00
|
|
|
}
|
|
|
|
|
2016-04-20 20:35:38 -04:00
|
|
|
// Tests for validate object name.
|
2016-03-07 22:30:30 -05:00
|
|
|
func TestIsValidObjectName(t *testing.T) {
|
2016-03-11 03:14:29 -05:00
|
|
|
testCases := []struct {
|
|
|
|
objectName string
|
|
|
|
shouldPass bool
|
|
|
|
}{
|
2016-04-27 22:28:13 -04:00
|
|
|
// cases which should pass the test.
|
|
|
|
// passing in valid object name.
|
2016-03-11 03:14:29 -05:00
|
|
|
{"object", true},
|
|
|
|
{"The Shining Script <v1>.pdf", true},
|
|
|
|
{"Cost Benefit Analysis (2009-2010).pptx", true},
|
|
|
|
{"117Gn8rfHL2ACARPAhaFd0AGzic9pUbIA/5OCn5A", true},
|
|
|
|
{"SHØRT", true},
|
2016-08-18 15:00:01 -04:00
|
|
|
{"f*le", true},
|
2016-09-30 19:56:36 -04:00
|
|
|
{"contains-^-carret", true},
|
|
|
|
{"contains-|-pipe", true},
|
|
|
|
{"contains-\"-quote", true},
|
|
|
|
{"contains-`-tick", true},
|
2017-04-24 21:13:46 -04:00
|
|
|
{"..test", true},
|
|
|
|
{".. test", true},
|
|
|
|
{". test", true},
|
|
|
|
{".test", true},
|
2016-03-11 03:14:29 -05:00
|
|
|
{"There are far too many object names, and far too few bucket names!", true},
|
2016-04-27 22:28:13 -04:00
|
|
|
// cases for which test should fail.
|
|
|
|
// passing invalid object names.
|
2016-03-11 03:14:29 -05:00
|
|
|
{"", false},
|
2016-05-13 14:43:06 -04:00
|
|
|
{"a/b/c/", false},
|
|
|
|
{"/a/b/c", false},
|
2017-04-24 21:13:46 -04:00
|
|
|
{"../../etc", false},
|
|
|
|
{"../../", false},
|
|
|
|
{"/../../etc", false},
|
|
|
|
{" ../etc", false},
|
|
|
|
{"./././", false},
|
|
|
|
{"./etc", false},
|
2016-09-30 19:56:36 -04:00
|
|
|
{"contains-\\-backslash", false},
|
2016-03-11 03:14:29 -05:00
|
|
|
{string([]byte{0xff, 0xfe, 0xfd}), false},
|
|
|
|
}
|
2016-03-07 22:30:30 -05:00
|
|
|
|
2016-03-11 03:14:29 -05:00
|
|
|
for i, testCase := range testCases {
|
2016-04-20 20:35:38 -04: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-22 12:05:01 -04:00
|
|
|
|
2016-10-24 16:56:13 -04:00
|
|
|
// Tests getCompleteMultipartMD5
|
|
|
|
func TestGetCompleteMultipartMD5(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2017-11-14 03:25:10 -05:00
|
|
|
parts []CompletePart
|
2016-10-24 16:56:13 -04:00
|
|
|
expectedResult string
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
// Wrong MD5 hash string
|
2017-11-14 03:25:10 -05:00
|
|
|
{[]CompletePart{{ETag: "wrong-md5-hash-string"}}, "", "encoding/hex: odd length hex string"},
|
2016-10-24 16:56:13 -04:00
|
|
|
|
2017-11-14 03:25:10 -05:00
|
|
|
// Single CompletePart with valid MD5 hash string.
|
|
|
|
{[]CompletePart{{ETag: "cf1f738a5924e645913c984e0fe3d708"}}, "10dc1617fbcf0bd0858048cb96e6bd77-1", ""},
|
2016-10-24 16:56:13 -04:00
|
|
|
|
2017-11-14 03:25:10 -05:00
|
|
|
// Multiple CompletePart with valid MD5 hash string.
|
|
|
|
{[]CompletePart{{ETag: "cf1f738a5924e645913c984e0fe3d708"}, {ETag: "9ccbc9a80eee7fb6fdd22441db2aedbd"}}, "0239a86b5266bb624f0ac60ba2aed6c8-2", ""},
|
2016-10-24 16:56:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
2016-11-10 10:41:02 -05:00
|
|
|
result, err := getCompleteMultipartMD5(test.parts)
|
2016-10-24 16:56:13 -04:00
|
|
|
if result != test.expectedResult {
|
|
|
|
t.Fatalf("test %d failed: expected: result=%v, got=%v", i+1, test.expectedResult, result)
|
|
|
|
}
|
|
|
|
errString := ""
|
|
|
|
if err != nil {
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
if errString != test.expectedErr {
|
|
|
|
t.Fatalf("test %d failed: expected: err=%v, got=%v", i+1, test.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 13:02:58 -05:00
|
|
|
|
|
|
|
// TestIsMinioBucketName - Tests isMinioBucketName helper function.
|
|
|
|
func TestIsMinioMetaBucketName(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
bucket string
|
|
|
|
result bool
|
|
|
|
}{
|
|
|
|
// Minio meta bucket.
|
|
|
|
{
|
|
|
|
bucket: minioMetaBucket,
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
// Minio meta bucket.
|
|
|
|
{
|
|
|
|
bucket: minioMetaMultipartBucket,
|
|
|
|
result: true,
|
|
|
|
},
|
|
|
|
// Minio meta bucket.
|
|
|
|
{
|
|
|
|
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 01:14:45 -05: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|