storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/iam/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 iampolicy
    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 TestActionSetMatches(t *testing.T) {
    45  	testCases := []struct {
    46  		set            ActionSet
    47  		action         Action
    48  		expectedResult bool
    49  	}{
    50  		{NewActionSet(AllActions), AbortMultipartUploadAction, true},
    51  		{NewActionSet(PutObjectAction), PutObjectAction, true},
    52  		{NewActionSet(PutObjectAction, GetObjectAction), PutObjectAction, true},
    53  		{NewActionSet(PutObjectAction, GetObjectAction), AbortMultipartUploadAction, false},
    54  	}
    55  
    56  	for i, testCase := range testCases {
    57  		result := testCase.set.Match(testCase.action)
    58  
    59  		if result != testCase.expectedResult {
    60  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
    61  		}
    62  	}
    63  }
    64  
    65  func TestActionSetIntersection(t *testing.T) {
    66  	testCases := []struct {
    67  		set            ActionSet
    68  		setToIntersect ActionSet
    69  		expectedResult ActionSet
    70  	}{
    71  		{NewActionSet(), NewActionSet(PutObjectAction), NewActionSet()},
    72  		{NewActionSet(PutObjectAction), NewActionSet(), NewActionSet()},
    73  		{NewActionSet(PutObjectAction), NewActionSet(PutObjectAction, GetObjectAction), NewActionSet(PutObjectAction)},
    74  	}
    75  
    76  	for i, testCase := range testCases {
    77  		result := testCase.set.Intersection(testCase.setToIntersect)
    78  
    79  		if !reflect.DeepEqual(result, testCase.expectedResult) {
    80  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set)
    81  		}
    82  	}
    83  }
    84  
    85  func TestActionSetMarshalJSON(t *testing.T) {
    86  	testCases := []struct {
    87  		actionSet      ActionSet
    88  		expectedResult []byte
    89  		expectErr      bool
    90  	}{
    91  		{NewActionSet(PutObjectAction), []byte(`["s3:PutObject"]`), false},
    92  		{NewActionSet(), nil, true},
    93  	}
    94  
    95  	for i, testCase := range testCases {
    96  		result, err := json.Marshal(testCase.actionSet)
    97  		expectErr := (err != nil)
    98  
    99  		if expectErr != testCase.expectErr {
   100  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
   101  		}
   102  
   103  		if !testCase.expectErr {
   104  			if !reflect.DeepEqual(result, testCase.expectedResult) {
   105  				t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, string(testCase.expectedResult), string(result))
   106  			}
   107  		}
   108  	}
   109  }
   110  
   111  func TestActionSetToSlice(t *testing.T) {
   112  	testCases := []struct {
   113  		actionSet      ActionSet
   114  		expectedResult []Action
   115  	}{
   116  		{NewActionSet(PutObjectAction), []Action{PutObjectAction}},
   117  		{NewActionSet(), []Action{}},
   118  	}
   119  
   120  	for i, testCase := range testCases {
   121  		result := testCase.actionSet.ToSlice()
   122  
   123  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   124  			t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   125  		}
   126  	}
   127  }
   128  
   129  func TestActionSetUnmarshalJSON(t *testing.T) {
   130  	testCases := []struct {
   131  		data               []byte
   132  		expectedResult     ActionSet
   133  		expectUnmarshalErr bool
   134  		expectValidateErr  bool
   135  	}{
   136  		{[]byte(`"s3:PutObject"`), NewActionSet(PutObjectAction), false, false},
   137  		{[]byte(`["s3:PutObject"]`), NewActionSet(PutObjectAction), false, false},
   138  		{[]byte(`["s3:PutObject", "s3:GetObject"]`), NewActionSet(PutObjectAction, GetObjectAction), false, false},
   139  		{[]byte(`["s3:PutObject", "s3:GetObject", "s3:PutObject"]`), NewActionSet(PutObjectAction, GetObjectAction), false, false},
   140  		{[]byte(`[]`), NewActionSet(), true, false},           // Empty array.
   141  		{[]byte(`"foo"`), nil, false, true},                   // Invalid action.
   142  		{[]byte(`["s3:PutObject", "foo"]`), nil, false, true}, // Invalid action.
   143  	}
   144  
   145  	for i, testCase := range testCases {
   146  		result := make(ActionSet)
   147  		err := json.Unmarshal(testCase.data, &result)
   148  		expectErr := (err != nil)
   149  
   150  		if expectErr != testCase.expectUnmarshalErr {
   151  			t.Fatalf("case %v: error during unmarshal: expected: %v, got: %v\n", i+1, testCase.expectUnmarshalErr, expectErr)
   152  		}
   153  
   154  		err = result.Validate()
   155  		expectErr = (err != nil)
   156  		if expectErr != testCase.expectValidateErr {
   157  			t.Fatalf("case %v: error during validation: expected: %v, got: %v\n", i+1, testCase.expectValidateErr, expectErr)
   158  		}
   159  
   160  		if !testCase.expectUnmarshalErr && !testCase.expectValidateErr {
   161  			if !reflect.DeepEqual(result, testCase.expectedResult) {
   162  				t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   163  			}
   164  		}
   165  	}
   166  }