storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/value_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  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestValueGetBool(t *testing.T) {
    26  	testCases := []struct {
    27  		value          Value
    28  		expectedResult bool
    29  		expectErr      bool
    30  	}{
    31  		{NewBoolValue(true), true, false},
    32  		{NewIntValue(7), false, true},
    33  		{Value{}, false, true},
    34  	}
    35  
    36  	for i, testCase := range testCases {
    37  		result, err := testCase.value.GetBool()
    38  		expectErr := (err != nil)
    39  
    40  		if expectErr != testCase.expectErr {
    41  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
    42  		}
    43  
    44  		if !testCase.expectErr {
    45  			if result != testCase.expectedResult {
    46  				t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
    47  			}
    48  		}
    49  	}
    50  }
    51  
    52  func TestValueGetInt(t *testing.T) {
    53  	testCases := []struct {
    54  		value          Value
    55  		expectedResult int
    56  		expectErr      bool
    57  	}{
    58  		{NewIntValue(7), 7, false},
    59  		{NewBoolValue(true), 0, true},
    60  		{Value{}, 0, true},
    61  	}
    62  
    63  	for i, testCase := range testCases {
    64  		result, err := testCase.value.GetInt()
    65  		expectErr := (err != nil)
    66  
    67  		if expectErr != testCase.expectErr {
    68  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
    69  		}
    70  
    71  		if !testCase.expectErr {
    72  			if result != testCase.expectedResult {
    73  				t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
    74  			}
    75  		}
    76  	}
    77  }
    78  
    79  func TestValueGetString(t *testing.T) {
    80  	testCases := []struct {
    81  		value          Value
    82  		expectedResult string
    83  		expectErr      bool
    84  	}{
    85  		{NewStringValue("foo"), "foo", false},
    86  		{NewBoolValue(true), "", true},
    87  		{Value{}, "", true},
    88  	}
    89  
    90  	for i, testCase := range testCases {
    91  		result, err := testCase.value.GetString()
    92  		expectErr := (err != nil)
    93  
    94  		if expectErr != testCase.expectErr {
    95  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
    96  		}
    97  
    98  		if !testCase.expectErr {
    99  			if result != testCase.expectedResult {
   100  				t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   101  			}
   102  		}
   103  	}
   104  }
   105  
   106  func TestValueGetType(t *testing.T) {
   107  	testCases := []struct {
   108  		value          Value
   109  		expectedResult reflect.Kind
   110  	}{
   111  		{NewBoolValue(true), reflect.Bool},
   112  		{NewIntValue(7), reflect.Int},
   113  		{NewStringValue("foo"), reflect.String},
   114  		{Value{}, reflect.Invalid},
   115  	}
   116  
   117  	for i, testCase := range testCases {
   118  		result := testCase.value.GetType()
   119  
   120  		if result != testCase.expectedResult {
   121  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   122  		}
   123  	}
   124  }
   125  
   126  func TestValueMarshalJSON(t *testing.T) {
   127  	testCases := []struct {
   128  		value          Value
   129  		expectedResult []byte
   130  		expectErr      bool
   131  	}{
   132  		{NewBoolValue(true), []byte("true"), false},
   133  		{NewIntValue(7), []byte("7"), false},
   134  		{NewStringValue("foo"), []byte(`"foo"`), false},
   135  		{Value{}, nil, true},
   136  	}
   137  
   138  	for i, testCase := range testCases {
   139  		result, err := json.Marshal(testCase.value)
   140  		expectErr := (err != nil)
   141  
   142  		if expectErr != testCase.expectErr {
   143  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
   144  		}
   145  
   146  		if !testCase.expectErr {
   147  			if !reflect.DeepEqual(result, testCase.expectedResult) {
   148  				t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   149  			}
   150  		}
   151  	}
   152  }
   153  
   154  func TestValueStoreBool(t *testing.T) {
   155  	testCases := []struct {
   156  		value          bool
   157  		expectedResult Value
   158  	}{
   159  		{false, NewBoolValue(false)},
   160  		{true, NewBoolValue(true)},
   161  	}
   162  
   163  	for i, testCase := range testCases {
   164  		var result Value
   165  		result.StoreBool(testCase.value)
   166  
   167  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   168  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   169  		}
   170  	}
   171  }
   172  
   173  func TestValueStoreInt(t *testing.T) {
   174  	testCases := []struct {
   175  		value          int
   176  		expectedResult Value
   177  	}{
   178  		{0, NewIntValue(0)},
   179  		{7, NewIntValue(7)},
   180  	}
   181  
   182  	for i, testCase := range testCases {
   183  		var result Value
   184  		result.StoreInt(testCase.value)
   185  
   186  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   187  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   188  		}
   189  	}
   190  }
   191  
   192  func TestValueStoreString(t *testing.T) {
   193  	testCases := []struct {
   194  		value          string
   195  		expectedResult Value
   196  	}{
   197  		{"", NewStringValue("")},
   198  		{"foo", NewStringValue("foo")},
   199  	}
   200  
   201  	for i, testCase := range testCases {
   202  		var result Value
   203  		result.StoreString(testCase.value)
   204  
   205  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   206  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   207  		}
   208  	}
   209  }
   210  
   211  func TestValueString(t *testing.T) {
   212  	testCases := []struct {
   213  		value          Value
   214  		expectedResult string
   215  	}{
   216  		{NewBoolValue(true), "true"},
   217  		{NewIntValue(7), "7"},
   218  		{NewStringValue("foo"), "foo"},
   219  		{Value{}, ""},
   220  	}
   221  
   222  	for i, testCase := range testCases {
   223  		result := testCase.value.String()
   224  
   225  		if result != testCase.expectedResult {
   226  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   227  		}
   228  	}
   229  }
   230  
   231  func TestValueUnmarshalJSON(t *testing.T) {
   232  	testCases := []struct {
   233  		data           []byte
   234  		expectedResult Value
   235  		expectErr      bool
   236  	}{
   237  		{[]byte("true"), NewBoolValue(true), false},
   238  		{[]byte("7"), NewIntValue(7), false},
   239  		{[]byte(`"foo"`), NewStringValue("foo"), false},
   240  		{[]byte("True"), Value{}, true},
   241  		{[]byte("7.1"), Value{}, true},
   242  		{[]byte(`["foo"]`), Value{}, true},
   243  	}
   244  
   245  	for i, testCase := range testCases {
   246  		var result Value
   247  		err := json.Unmarshal(testCase.data, &result)
   248  		expectErr := (err != nil)
   249  
   250  		if expectErr != testCase.expectErr {
   251  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
   252  		}
   253  
   254  		if !testCase.expectErr {
   255  			if !reflect.DeepEqual(result, testCase.expectedResult) {
   256  				t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   257  			}
   258  		}
   259  	}
   260  }