storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/name_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 TestNameIsValid(t *testing.T) {
    26  	testCases := []struct {
    27  		n              name
    28  		expectedResult bool
    29  	}{
    30  		{stringEquals, true},
    31  		{stringNotEquals, true},
    32  		{stringLike, true},
    33  		{stringNotLike, true},
    34  		{ipAddress, true},
    35  		{notIPAddress, true},
    36  		{null, true},
    37  		{name("foo"), false},
    38  	}
    39  
    40  	for i, testCase := range testCases {
    41  		result := testCase.n.IsValid()
    42  
    43  		if testCase.expectedResult != result {
    44  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    45  		}
    46  	}
    47  }
    48  
    49  func TestNameMarshalJSON(t *testing.T) {
    50  	testCases := []struct {
    51  		n              name
    52  		expectedResult []byte
    53  		expectErr      bool
    54  	}{
    55  		{stringEquals, []byte(`"StringEquals"`), false},
    56  		{stringNotEquals, []byte(`"StringNotEquals"`), false},
    57  		{stringLike, []byte(`"StringLike"`), false},
    58  		{stringNotLike, []byte(`"StringNotLike"`), false},
    59  		{ipAddress, []byte(`"IpAddress"`), false},
    60  		{notIPAddress, []byte(`"NotIpAddress"`), false},
    61  		{null, []byte(`"Null"`), false},
    62  		{name("foo"), nil, true},
    63  	}
    64  
    65  	for i, testCase := range testCases {
    66  		result, err := json.Marshal(testCase.n)
    67  		expectErr := (err != nil)
    68  
    69  		if testCase.expectErr != expectErr {
    70  			t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
    71  		}
    72  
    73  		if !testCase.expectErr {
    74  			if !reflect.DeepEqual(result, testCase.expectedResult) {
    75  				t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result))
    76  			}
    77  		}
    78  	}
    79  }
    80  
    81  func TestNameUnmarshalJSON(t *testing.T) {
    82  	testCases := []struct {
    83  		data           []byte
    84  		expectedResult name
    85  		expectErr      bool
    86  	}{
    87  		{[]byte(`"StringEquals"`), stringEquals, false},
    88  		{[]byte(`"foo"`), name(""), true},
    89  	}
    90  
    91  	for i, testCase := range testCases {
    92  		var result name
    93  		err := json.Unmarshal(testCase.data, &result)
    94  		expectErr := (err != nil)
    95  
    96  		if testCase.expectErr != expectErr {
    97  			t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
    98  		}
    99  
   100  		if !testCase.expectErr {
   101  			if testCase.expectedResult != result {
   102  				t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   103  			}
   104  		}
   105  	}
   106  }