| 
									
										
										
										
											2016-03-08 11:30:30 +08: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-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-03-08 11:30:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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}, | 
					
						
							| 
									
										
										
										
											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}, | 
					
						
							|  |  |  | 		{"THIS-BEINGS-WITH-UPPERCASe", false}, | 
					
						
							|  |  |  | 		{"tHIS-ENDS-WITH-UPPERCASE", false}, | 
					
						
							|  |  |  | 		{"ThisBeginsAndEndsWithUpperCase", false}, | 
					
						
							|  |  |  | 		{"una ñina", false}, | 
					
						
							|  |  |  | 		{"lalalallalallalalalallalallalala-theString-size-is-greater-than-64", 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
										 |  |  | 		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}, | 
					
						
							| 
									
										
										
										
											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}, | 
					
						
							|  |  |  | 		{"/a/b/c", 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) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |