| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2017 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2020-04-15 08:52:38 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Tests for if parent directory is object
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | func TestErasureParentDirIsObject(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-04-15 08:52:38 +08:00
										 |  |  | 	ctx, cancel := context.WithCancel(context.Background()) | 
					
						
							|  |  |  | 	defer cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	obj, fsDisks, err := prepareErasure16(ctx) | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 		t.Fatalf("Unable to initialize 'Erasure' object layer.") | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-11 00:18:19 +08:00
										 |  |  | 	defer obj.Shutdown(context.Background()) | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Remove all disks.
 | 
					
						
							|  |  |  | 	for _, disk := range fsDisks { | 
					
						
							|  |  |  | 		defer os.RemoveAll(disk) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bucketName := "testbucket" | 
					
						
							|  |  |  | 	objectName := "object" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 11:04:01 +08:00
										 |  |  | 	if err = obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil { | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	objectContent := "12345" | 
					
						
							| 
									
										
										
										
											2020-04-10 00:30:02 +08:00
										 |  |  | 	objInfo, err := obj.PutObject(GlobalContext, bucketName, objectName, | 
					
						
							| 
									
										
										
										
											2019-02-09 13:31:06 +08:00
										 |  |  | 		mustGetPutObjReader(t, bytes.NewReader([]byte(objectContent)), int64(len(objectContent)), "", ""), ObjectOptions{}) | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if objInfo.Name != objectName { | 
					
						
							|  |  |  | 		t.Fatalf("Unexpected object name returned got %s, expected %s", objInfo.Name, objectName) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-16 05:28:50 +08:00
										 |  |  | 	z := obj.(*erasureServerSets) | 
					
						
							|  |  |  | 	xl := z.serverSets[0].sets[0] | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		parentIsObject bool | 
					
						
							|  |  |  | 		objectName     string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// parentIsObject is true if object is available.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			parentIsObject: true, | 
					
						
							|  |  |  | 			objectName:     objectName, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			parentIsObject: false, | 
					
						
							|  |  |  | 			objectName:     "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			parentIsObject: false, | 
					
						
							|  |  |  | 			objectName:     ".", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Should not cause infinite loop.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			parentIsObject: false, | 
					
						
							| 
									
										
										
										
											2019-08-07 03:08:58 +08:00
										 |  |  | 			objectName:     SlashSeparator, | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			parentIsObject: false, | 
					
						
							|  |  |  | 			objectName:     "\\", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Should not cause infinite loop with double forward slash.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			parentIsObject: false, | 
					
						
							|  |  |  | 			objectName:     "//", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2020-04-10 00:30:02 +08:00
										 |  |  | 		gotValue := xl.parentDirIsObject(GlobalContext, bucketName, testCase.objectName) | 
					
						
							| 
									
										
										
										
											2017-09-26 05:47:58 +08:00
										 |  |  | 		if testCase.parentIsObject != gotValue { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Unexpected value returned got %t, expected %t", i+1, gotValue, testCase.parentIsObject) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |