| 
									
										
										
										
											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/>.
 | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"reflect" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 00:38:05 +08:00
										 |  |  | 	miniogopolicy "github.com/minio/minio-go/v7/pkg/policy" | 
					
						
							|  |  |  | 	"github.com/minio/minio-go/v7/pkg/set" | 
					
						
							| 
									
										
										
										
											2024-05-25 07:05:23 +08:00
										 |  |  | 	"github.com/minio/pkg/v3/policy" | 
					
						
							|  |  |  | 	"github.com/minio/pkg/v3/policy/condition" | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestPolicySysIsAllowed(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	p := &policy.BucketPolicy{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		Version: policy.DefaultVersion, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		Statements: []policy.BPStatement{ | 
					
						
							|  |  |  | 			policy.NewBPStatement("", | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				policy.Allow, | 
					
						
							|  |  |  | 				policy.NewPrincipal("*"), | 
					
						
							|  |  |  | 				policy.NewActionSet(policy.GetBucketLocationAction), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 				policy.NewResourceSet(policy.NewResource("mybucket")), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				condition.NewFunctions(), | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 			policy.NewBPStatement("", | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				policy.Allow, | 
					
						
							|  |  |  | 				policy.NewPrincipal("*"), | 
					
						
							|  |  |  | 				policy.NewActionSet(policy.PutObjectAction), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 				policy.NewResourceSet(policy.NewResource("mybucket/myobject*")), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				condition.NewFunctions(), | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-05-20 04:53:54 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	anonGetBucketLocationArgs := policy.BucketPolicyArgs{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		AccountName:     "Q3AM3UQ867SPQQA43P2F", | 
					
						
							|  |  |  | 		Action:          policy.GetBucketLocationAction, | 
					
						
							|  |  |  | 		BucketName:      "mybucket", | 
					
						
							|  |  |  | 		ConditionValues: map[string][]string{}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	anonPutObjectActionArgs := policy.BucketPolicyArgs{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		AccountName: "Q3AM3UQ867SPQQA43P2F", | 
					
						
							|  |  |  | 		Action:      policy.PutObjectAction, | 
					
						
							|  |  |  | 		BucketName:  "mybucket", | 
					
						
							|  |  |  | 		ConditionValues: map[string][]string{ | 
					
						
							|  |  |  | 			"x-amz-copy-source": {"mybucket/myobject"}, | 
					
						
							|  |  |  | 			"SourceIp":          {"192.168.1.10"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		ObjectName: "myobject", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	anonGetObjectActionArgs := policy.BucketPolicyArgs{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		AccountName:     "Q3AM3UQ867SPQQA43P2F", | 
					
						
							|  |  |  | 		Action:          policy.GetObjectAction, | 
					
						
							|  |  |  | 		BucketName:      "mybucket", | 
					
						
							|  |  |  | 		ConditionValues: map[string][]string{}, | 
					
						
							|  |  |  | 		ObjectName:      "myobject", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	getBucketLocationArgs := policy.BucketPolicyArgs{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		AccountName:     "Q3AM3UQ867SPQQA43P2F", | 
					
						
							|  |  |  | 		Action:          policy.GetBucketLocationAction, | 
					
						
							|  |  |  | 		BucketName:      "mybucket", | 
					
						
							|  |  |  | 		ConditionValues: map[string][]string{}, | 
					
						
							|  |  |  | 		IsOwner:         true, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	putObjectActionArgs := policy.BucketPolicyArgs{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		AccountName: "Q3AM3UQ867SPQQA43P2F", | 
					
						
							|  |  |  | 		Action:      policy.PutObjectAction, | 
					
						
							|  |  |  | 		BucketName:  "mybucket", | 
					
						
							|  |  |  | 		ConditionValues: map[string][]string{ | 
					
						
							|  |  |  | 			"x-amz-copy-source": {"mybucket/myobject"}, | 
					
						
							|  |  |  | 			"SourceIp":          {"192.168.1.10"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		IsOwner:    true, | 
					
						
							|  |  |  | 		ObjectName: "myobject", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	getObjectActionArgs := policy.BucketPolicyArgs{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		AccountName:     "Q3AM3UQ867SPQQA43P2F", | 
					
						
							|  |  |  | 		Action:          policy.GetObjectAction, | 
					
						
							|  |  |  | 		BucketName:      "mybucket", | 
					
						
							|  |  |  | 		ConditionValues: map[string][]string{}, | 
					
						
							|  |  |  | 		IsOwner:         true, | 
					
						
							|  |  |  | 		ObjectName:      "myobject", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	yourbucketAnonGetObjectActionArgs := policy.BucketPolicyArgs{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		AccountName:     "Q3AM3UQ867SPQQA43P2F", | 
					
						
							|  |  |  | 		Action:          policy.GetObjectAction, | 
					
						
							|  |  |  | 		BucketName:      "yourbucket", | 
					
						
							|  |  |  | 		ConditionValues: map[string][]string{}, | 
					
						
							|  |  |  | 		ObjectName:      "yourobject", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	yourbucketGetObjectActionArgs := policy.BucketPolicyArgs{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		AccountName:     "Q3AM3UQ867SPQQA43P2F", | 
					
						
							|  |  |  | 		Action:          policy.GetObjectAction, | 
					
						
							|  |  |  | 		BucketName:      "yourbucket", | 
					
						
							|  |  |  | 		ConditionValues: map[string][]string{}, | 
					
						
							|  |  |  | 		IsOwner:         true, | 
					
						
							|  |  |  | 		ObjectName:      "yourobject", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		args           policy.BucketPolicyArgs | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		expectedResult bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2020-05-21 01:18:15 +08:00
										 |  |  | 		{anonGetBucketLocationArgs, true}, | 
					
						
							|  |  |  | 		{anonPutObjectActionArgs, true}, | 
					
						
							|  |  |  | 		{anonGetObjectActionArgs, false}, | 
					
						
							|  |  |  | 		{getBucketLocationArgs, true}, | 
					
						
							|  |  |  | 		{putObjectActionArgs, true}, | 
					
						
							|  |  |  | 		{getObjectActionArgs, true}, | 
					
						
							|  |  |  | 		{yourbucketAnonGetObjectActionArgs, false}, | 
					
						
							|  |  |  | 		{yourbucketGetObjectActionArgs, true}, | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2020-05-21 01:18:15 +08:00
										 |  |  | 		result := p.IsAllowed(testCase.args) | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if result != testCase.expectedResult { | 
					
						
							|  |  |  | 			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getReadOnlyStatement(bucketName, prefix string) []miniogopolicy.Statement { | 
					
						
							|  |  |  | 	return []miniogopolicy.Statement{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Effect:    string(policy.Allow), | 
					
						
							|  |  |  | 			Principal: miniogopolicy.User{AWS: set.CreateStringSet("*")}, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 			Resources: set.CreateStringSet(policy.NewResource(bucketName).String()), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 			Actions:   set.CreateStringSet("s3:GetBucketLocation", "s3:ListBucket"), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Effect:    string(policy.Allow), | 
					
						
							|  |  |  | 			Principal: miniogopolicy.User{AWS: set.CreateStringSet("*")}, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 			Resources: set.CreateStringSet(policy.NewResource(bucketName + "/" + prefix).String()), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 			Actions:   set.CreateStringSet("s3:GetObject"), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestPolicyToBucketAccessPolicy(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	case1Policy := &policy.BucketPolicy{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		Version: policy.DefaultVersion, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		Statements: []policy.BPStatement{ | 
					
						
							|  |  |  | 			policy.NewBPStatement("", | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				policy.Allow, | 
					
						
							|  |  |  | 				policy.NewPrincipal("*"), | 
					
						
							|  |  |  | 				policy.NewActionSet(policy.GetBucketLocationAction, policy.ListBucketAction), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 				policy.NewResourceSet(policy.NewResource("mybucket")), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				condition.NewFunctions(), | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 			policy.NewBPStatement("", | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				policy.Allow, | 
					
						
							|  |  |  | 				policy.NewPrincipal("*"), | 
					
						
							|  |  |  | 				policy.NewActionSet(policy.GetObjectAction), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 				policy.NewResourceSet(policy.NewResource("mybucket/myobject*")), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				condition.NewFunctions(), | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case1Result := &miniogopolicy.BucketAccessPolicy{ | 
					
						
							|  |  |  | 		Version:    policy.DefaultVersion, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		Statements: getReadOnlyStatement("mybucket", "myobject*"), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	case2Policy := &policy.BucketPolicy{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		Version:    policy.DefaultVersion, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		Statements: []policy.BPStatement{}, | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case2Result := &miniogopolicy.BucketAccessPolicy{ | 
					
						
							|  |  |  | 		Version:    policy.DefaultVersion, | 
					
						
							|  |  |  | 		Statements: []miniogopolicy.Statement{}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	case3Policy := &policy.BucketPolicy{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		Version: "12-10-2012", | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		Statements: []policy.BPStatement{ | 
					
						
							|  |  |  | 			policy.NewBPStatement("", | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				policy.Allow, | 
					
						
							|  |  |  | 				policy.NewPrincipal("*"), | 
					
						
							|  |  |  | 				policy.NewActionSet(policy.PutObjectAction), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 				policy.NewResourceSet(policy.NewResource("mybucket/myobject*")), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				condition.NewFunctions(), | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		bucketPolicy   *policy.BucketPolicy | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		expectedResult *miniogopolicy.BucketAccessPolicy | 
					
						
							|  |  |  | 		expectErr      bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{case1Policy, case1Result, false}, | 
					
						
							|  |  |  | 		{case2Policy, case2Result, false}, | 
					
						
							|  |  |  | 		{case3Policy, nil, true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		result, err := PolicyToBucketAccessPolicy(testCase.bucketPolicy) | 
					
						
							|  |  |  | 		expectErr := (err != nil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if expectErr != testCase.expectErr { | 
					
						
							|  |  |  | 			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !testCase.expectErr { | 
					
						
							|  |  |  | 			if !reflect.DeepEqual(result, testCase.expectedResult) { | 
					
						
							|  |  |  | 				t.Fatalf("case %v: result: expected: %+v, got: %+v\n", i+1, testCase.expectedResult, result) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestBucketAccessPolicyToPolicy(t *testing.T) { | 
					
						
							|  |  |  | 	case1PolicyInfo := &miniogopolicy.BucketAccessPolicy{ | 
					
						
							|  |  |  | 		Version:    policy.DefaultVersion, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		Statements: getReadOnlyStatement("mybucket", "myobject*"), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	case1Result := &policy.BucketPolicy{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		Version: policy.DefaultVersion, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		Statements: []policy.BPStatement{ | 
					
						
							|  |  |  | 			policy.NewBPStatement("", | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				policy.Allow, | 
					
						
							|  |  |  | 				policy.NewPrincipal("*"), | 
					
						
							|  |  |  | 				policy.NewActionSet(policy.GetBucketLocationAction, policy.ListBucketAction), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 				policy.NewResourceSet(policy.NewResource("mybucket")), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				condition.NewFunctions(), | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 			policy.NewBPStatement("", | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				policy.Allow, | 
					
						
							|  |  |  | 				policy.NewPrincipal("*"), | 
					
						
							|  |  |  | 				policy.NewActionSet(policy.GetObjectAction), | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 				policy.NewResourceSet(policy.NewResource("mybucket/myobject*")), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 				condition.NewFunctions(), | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case2PolicyInfo := &miniogopolicy.BucketAccessPolicy{ | 
					
						
							|  |  |  | 		Version:    policy.DefaultVersion, | 
					
						
							|  |  |  | 		Statements: []miniogopolicy.Statement{}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 	case2Result := &policy.BucketPolicy{ | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		Version:    policy.DefaultVersion, | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		Statements: []policy.BPStatement{}, | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case3PolicyInfo := &miniogopolicy.BucketAccessPolicy{ | 
					
						
							|  |  |  | 		Version:    "12-10-2012", | 
					
						
							|  |  |  | 		Statements: getReadOnlyStatement("mybucket", "/myobject*"), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		policyInfo     *miniogopolicy.BucketAccessPolicy | 
					
						
							| 
									
										
										
										
											2023-09-05 03:57:37 +08:00
										 |  |  | 		expectedResult *policy.BucketPolicy | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 		expectErr      bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{case1PolicyInfo, case1Result, false}, | 
					
						
							|  |  |  | 		{case2PolicyInfo, case2Result, false}, | 
					
						
							|  |  |  | 		{case3PolicyInfo, nil, true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		result, err := BucketAccessPolicyToPolicy(testCase.policyInfo) | 
					
						
							|  |  |  | 		expectErr := (err != nil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if expectErr != testCase.expectErr { | 
					
						
							|  |  |  | 			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !testCase.expectErr { | 
					
						
							|  |  |  | 			if !reflect.DeepEqual(result, testCase.expectedResult) { | 
					
						
							|  |  |  | 				t.Fatalf("case %v: result: expected: %+v, got: %+v\n", i+1, testCase.expectedResult, result) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |