storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/actionset_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package policy 18 19 import ( 20 "encoding/json" 21 "reflect" 22 "testing" 23 ) 24 25 func TestActionSetAdd(t *testing.T) { 26 testCases := []struct { 27 set ActionSet 28 action Action 29 expectedResult ActionSet 30 }{ 31 {NewActionSet(), PutObjectAction, NewActionSet(PutObjectAction)}, 32 {NewActionSet(PutObjectAction), PutObjectAction, NewActionSet(PutObjectAction)}, 33 } 34 35 for i, testCase := range testCases { 36 testCase.set.Add(testCase.action) 37 38 if !reflect.DeepEqual(testCase.expectedResult, testCase.set) { 39 t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) 40 } 41 } 42 } 43 44 func TestActionSetContains(t *testing.T) { 45 testCases := []struct { 46 set ActionSet 47 action Action 48 expectedResult bool 49 }{ 50 {NewActionSet(PutObjectAction), PutObjectAction, true}, 51 {NewActionSet(PutObjectAction, GetObjectAction), PutObjectAction, true}, 52 {NewActionSet(PutObjectAction, GetObjectAction), AbortMultipartUploadAction, false}, 53 } 54 55 for i, testCase := range testCases { 56 result := testCase.set.Contains(testCase.action) 57 58 if result != testCase.expectedResult { 59 t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) 60 } 61 } 62 } 63 64 func TestActionSetIntersection(t *testing.T) { 65 testCases := []struct { 66 set ActionSet 67 setToIntersect ActionSet 68 expectedResult ActionSet 69 }{ 70 {NewActionSet(), NewActionSet(PutObjectAction), NewActionSet()}, 71 {NewActionSet(PutObjectAction), NewActionSet(), NewActionSet()}, 72 {NewActionSet(PutObjectAction), NewActionSet(PutObjectAction, GetObjectAction), NewActionSet(PutObjectAction)}, 73 } 74 75 for i, testCase := range testCases { 76 result := testCase.set.Intersection(testCase.setToIntersect) 77 78 if !reflect.DeepEqual(result, testCase.expectedResult) { 79 t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) 80 } 81 } 82 } 83 84 func TestActionSetMarshalJSON(t *testing.T) { 85 testCases := []struct { 86 actionSet ActionSet 87 expectedResult []byte 88 expectErr bool 89 }{ 90 {NewActionSet(PutObjectAction), []byte(`["s3:PutObject"]`), false}, 91 {NewActionSet(), nil, true}, 92 } 93 94 for i, testCase := range testCases { 95 result, err := json.Marshal(testCase.actionSet) 96 expectErr := (err != nil) 97 98 if expectErr != testCase.expectErr { 99 t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) 100 } 101 102 if !testCase.expectErr { 103 if !reflect.DeepEqual(result, testCase.expectedResult) { 104 t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, string(testCase.expectedResult), string(result)) 105 } 106 } 107 } 108 } 109 110 func TestActionSetToSlice(t *testing.T) { 111 testCases := []struct { 112 actionSet ActionSet 113 expectedResult []Action 114 }{ 115 {NewActionSet(PutObjectAction), []Action{PutObjectAction}}, 116 {NewActionSet(), []Action{}}, 117 } 118 119 for i, testCase := range testCases { 120 result := testCase.actionSet.ToSlice() 121 122 if !reflect.DeepEqual(result, testCase.expectedResult) { 123 t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) 124 } 125 } 126 } 127 128 func TestActionSetUnmarshalJSON(t *testing.T) { 129 testCases := []struct { 130 data []byte 131 expectedResult ActionSet 132 expectErr bool 133 }{ 134 {[]byte(`"s3:PutObject"`), NewActionSet(PutObjectAction), false}, 135 {[]byte(`["s3:PutObject"]`), NewActionSet(PutObjectAction), false}, 136 {[]byte(`["s3:PutObject", "s3:GetObject"]`), NewActionSet(PutObjectAction, GetObjectAction), false}, 137 {[]byte(`["s3:PutObject", "s3:GetObject", "s3:PutObject"]`), NewActionSet(PutObjectAction, GetObjectAction), false}, 138 {[]byte(`[]`), NewActionSet(), true}, // Empty array. 139 {[]byte(`"foo"`), nil, true}, // Invalid action. 140 {[]byte(`["s3:PutObject", "foo"]`), nil, true}, // Invalid action. 141 } 142 143 for i, testCase := range testCases { 144 result := make(ActionSet) 145 err := json.Unmarshal(testCase.data, &result) 146 expectErr := (err != nil) 147 148 if expectErr != testCase.expectErr { 149 t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) 150 } 151 152 if !testCase.expectErr { 153 if !reflect.DeepEqual(result, testCase.expectedResult) { 154 t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) 155 } 156 } 157 } 158 }