storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/nullfunc_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 condition
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestNullFuncEvaluate(t *testing.T) {
    25  	case1Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(true)))
    26  	if err != nil {
    27  		t.Fatalf("unexpected error. %v\n", err)
    28  	}
    29  
    30  	case2Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(false)))
    31  	if err != nil {
    32  		t.Fatalf("unexpected error. %v\n", err)
    33  	}
    34  
    35  	testCases := []struct {
    36  		function       Function
    37  		values         map[string][]string
    38  		expectedResult bool
    39  	}{
    40  		{case1Function, map[string][]string{"prefix": {"true"}}, false},
    41  		{case1Function, map[string][]string{"prefix": {"false"}}, false},
    42  		{case1Function, map[string][]string{"prefix": {"mybucket/foo"}}, false},
    43  		{case1Function, map[string][]string{}, true},
    44  		{case1Function, map[string][]string{"delimiter": {"/"}}, true},
    45  		{case2Function, map[string][]string{"prefix": {"true"}}, true},
    46  		{case2Function, map[string][]string{"prefix": {"false"}}, true},
    47  		{case2Function, map[string][]string{"prefix": {"mybucket/foo"}}, true},
    48  		{case2Function, map[string][]string{}, false},
    49  		{case2Function, map[string][]string{"delimiter": {"/"}}, false},
    50  	}
    51  
    52  	for i, testCase := range testCases {
    53  		result := testCase.function.evaluate(testCase.values)
    54  
    55  		if result != testCase.expectedResult {
    56  			t.Errorf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
    57  		}
    58  	}
    59  }
    60  
    61  func TestNullFuncKey(t *testing.T) {
    62  	case1Function, err := newNullFunc(S3XAmzCopySource, NewValueSet(NewBoolValue(true)))
    63  	if err != nil {
    64  		t.Fatalf("unexpected error. %v\n", err)
    65  	}
    66  
    67  	testCases := []struct {
    68  		function       Function
    69  		expectedResult Key
    70  	}{
    71  		{case1Function, S3XAmzCopySource},
    72  	}
    73  
    74  	for i, testCase := range testCases {
    75  		result := testCase.function.key()
    76  
    77  		if result != testCase.expectedResult {
    78  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
    79  		}
    80  	}
    81  }
    82  
    83  func TestNullFuncToMap(t *testing.T) {
    84  	case1Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(true)))
    85  	if err != nil {
    86  		t.Fatalf("unexpected error. %v\n", err)
    87  	}
    88  
    89  	case1Result := map[Key]ValueSet{
    90  		S3Prefix: NewValueSet(NewBoolValue(true)),
    91  	}
    92  
    93  	case2Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(false)))
    94  	if err != nil {
    95  		t.Fatalf("unexpected error. %v\n", err)
    96  	}
    97  
    98  	case2Result := map[Key]ValueSet{
    99  		S3Prefix: NewValueSet(NewBoolValue(false)),
   100  	}
   101  
   102  	testCases := []struct {
   103  		f              Function
   104  		expectedResult map[Key]ValueSet
   105  	}{
   106  		{case1Function, case1Result},
   107  		{case2Function, case2Result},
   108  		{&nullFunc{}, nil},
   109  	}
   110  
   111  	for i, testCase := range testCases {
   112  		result := testCase.f.toMap()
   113  
   114  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   115  			t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   116  		}
   117  	}
   118  }
   119  
   120  func TestNewNullFunc(t *testing.T) {
   121  	case1Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(true)))
   122  	if err != nil {
   123  		t.Fatalf("unexpected error. %v\n", err)
   124  	}
   125  
   126  	case2Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(false)))
   127  	if err != nil {
   128  		t.Fatalf("unexpected error. %v\n", err)
   129  	}
   130  
   131  	testCases := []struct {
   132  		key            Key
   133  		values         ValueSet
   134  		expectedResult Function
   135  		expectErr      bool
   136  	}{
   137  		{S3Prefix, NewValueSet(NewBoolValue(true)), case1Function, false},
   138  		{S3Prefix, NewValueSet(NewStringValue("false")), case2Function, false},
   139  		// Multiple values error.
   140  		{S3Prefix, NewValueSet(NewBoolValue(true), NewBoolValue(false)), nil, true},
   141  		// Invalid boolean string error.
   142  		{S3Prefix, NewValueSet(NewStringValue("foo")), nil, true},
   143  		// Invalid value error.
   144  		{S3Prefix, NewValueSet(NewIntValue(7)), nil, true},
   145  	}
   146  
   147  	for i, testCase := range testCases {
   148  		result, err := newNullFunc(testCase.key, testCase.values)
   149  		expectErr := (err != nil)
   150  
   151  		if expectErr != testCase.expectErr {
   152  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
   153  		}
   154  
   155  		if !testCase.expectErr {
   156  			if !reflect.DeepEqual(result, testCase.expectedResult) {
   157  				t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   158  			}
   159  		}
   160  	}
   161  }