storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/valueset_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 TestValueSetAdd(t *testing.T) { 26 testCases := []struct { 27 value Value 28 expectedResult ValueSet 29 }{ 30 {NewBoolValue(true), NewValueSet(NewBoolValue(true))}, 31 {NewIntValue(7), NewValueSet(NewIntValue(7))}, 32 {NewStringValue("foo"), NewValueSet(NewStringValue("foo"))}, 33 } 34 35 for i, testCase := range testCases { 36 result := NewValueSet() 37 result.Add(testCase.value) 38 39 if !reflect.DeepEqual(result, testCase.expectedResult) { 40 t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) 41 } 42 } 43 } 44 45 func TestValueSetMarshalJSON(t *testing.T) { 46 testCases := []struct { 47 set ValueSet 48 expectedResult string 49 expectErr bool 50 }{ 51 {NewValueSet(NewBoolValue(true)), `[true]`, false}, 52 {NewValueSet(NewIntValue(7)), `[7]`, false}, 53 {NewValueSet(NewStringValue("foo")), `["foo"]`, false}, 54 {NewValueSet(NewBoolValue(true)), `[true]`, false}, 55 {NewValueSet(NewStringValue("7")), `["7"]`, false}, 56 {NewValueSet(NewStringValue("foo")), `["foo"]`, false}, 57 {make(ValueSet), "", true}, 58 } 59 60 for i, testCase := range testCases { 61 result, err := json.Marshal(testCase.set) 62 expectErr := (err != nil) 63 64 if expectErr != testCase.expectErr { 65 t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) 66 } 67 68 if !testCase.expectErr { 69 if string(result) != testCase.expectedResult { 70 t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, string(result)) 71 } 72 } 73 } 74 } 75 76 func TestValueSetUnmarshalJSON(t *testing.T) { 77 set1 := NewValueSet( 78 NewBoolValue(true), 79 NewStringValue("false"), 80 NewIntValue(7), 81 NewStringValue("7"), 82 NewStringValue("foo"), 83 NewStringValue("192.168.1.100/24"), 84 ) 85 86 testCases := []struct { 87 data []byte 88 expectedResult ValueSet 89 expectErr bool 90 }{ 91 {[]byte(`true`), NewValueSet(NewBoolValue(true)), false}, 92 {[]byte(`7`), NewValueSet(NewIntValue(7)), false}, 93 {[]byte(`"foo"`), NewValueSet(NewStringValue("foo")), false}, 94 {[]byte(`[true]`), NewValueSet(NewBoolValue(true)), false}, 95 {[]byte(`[7]`), NewValueSet(NewIntValue(7)), false}, 96 {[]byte(`["foo"]`), NewValueSet(NewStringValue("foo")), false}, 97 {[]byte(`[true, "false", 7, "7", "foo", "192.168.1.100/24"]`), set1, false}, 98 {[]byte(`{}`), nil, true}, // Unsupported data. 99 {[]byte(`[]`), nil, true}, // Empty array. 100 {[]byte(`[7, 7, true]`), nil, true}, // Duplicate value. 101 } 102 103 for i, testCase := range testCases { 104 result := make(ValueSet) 105 err := json.Unmarshal(testCase.data, &result) 106 expectErr := (err != nil) 107 108 if expectErr != testCase.expectErr { 109 t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) 110 } 111 112 if !testCase.expectErr { 113 if !reflect.DeepEqual(result, testCase.expectedResult) { 114 t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) 115 } 116 } 117 } 118 }