storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/lifecycle/filter_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 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 lifecycle
    18  
    19  import (
    20  	"encoding/xml"
    21  	"fmt"
    22  	"testing"
    23  )
    24  
    25  // TestUnsupportedFilters checks if parsing Filter xml with
    26  // unsupported elements returns appropriate errors
    27  func TestUnsupportedFilters(t *testing.T) {
    28  	testCases := []struct {
    29  		inputXML    string
    30  		expectedErr error
    31  	}{
    32  		{ // Filter with And tags
    33  			inputXML: ` <Filter>
    34  						<And>
    35  							<Prefix>key-prefix</Prefix>
    36  						</And>
    37  						</Filter>`,
    38  			expectedErr: errXMLNotWellFormed,
    39  		},
    40  		{ // Filter with Tag tags
    41  			inputXML: ` <Filter>
    42  						<Tag>
    43  							<Key>key1</Key>
    44  							<Value>value1</Value>
    45  						</Tag>
    46  						</Filter>`,
    47  			expectedErr: nil,
    48  		},
    49  		{ // Filter with Prefix tag
    50  			inputXML: ` <Filter>
    51  							<Prefix>key-prefix</Prefix>
    52  						</Filter>`,
    53  			expectedErr: nil,
    54  		},
    55  		{ // Filter without And and multiple Tag tags
    56  			inputXML: ` <Filter>
    57  							<Prefix>key-prefix</Prefix>
    58  							<Tag>
    59  								<Key>key1</Key>
    60  								<Value>value1</Value>
    61  							</Tag>
    62  							<Tag>
    63  								<Key>key2</Key>
    64  								<Value>value2</Value>
    65  							</Tag>
    66  						</Filter>`,
    67  			expectedErr: errInvalidFilter,
    68  		},
    69  		{ // Filter with And, Prefix & multiple Tag tags
    70  			inputXML: ` <Filter>
    71  							<And>
    72  							<Prefix>key-prefix</Prefix>
    73  							<Tag>
    74  								<Key>key1</Key>
    75  								<Value>value1</Value>
    76  							</Tag>
    77  							<Tag>
    78  								<Key>key2</Key>
    79  								<Value>value2</Value>
    80  							</Tag>
    81  							</And>
    82  						</Filter>`,
    83  			expectedErr: nil,
    84  		},
    85  		{ // Filter with And and multiple Tag tags
    86  			inputXML: ` <Filter>
    87  							<And>
    88  							<Prefix></Prefix>
    89  							<Tag>
    90  								<Key>key1</Key>
    91  								<Value>value1</Value>
    92  							</Tag>
    93  							<Tag>
    94  								<Key>key2</Key>
    95  								<Value>value2</Value>
    96  							</Tag>
    97  							</And>
    98  						</Filter>`,
    99  			expectedErr: nil,
   100  		},
   101  		{ // Filter without And and single Tag tag
   102  			inputXML: ` <Filter>
   103  							<Prefix>key-prefix</Prefix>
   104  							<Tag>
   105  								<Key>key1</Key>
   106  								<Value>value1</Value>
   107  							</Tag>
   108  						</Filter>`,
   109  			expectedErr: errInvalidFilter,
   110  		},
   111  	}
   112  	for i, tc := range testCases {
   113  		t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) {
   114  			var filter Filter
   115  			err := xml.Unmarshal([]byte(tc.inputXML), &filter)
   116  			if err != nil {
   117  				t.Fatalf("%d: Expected no error but got %v", i+1, err)
   118  			}
   119  			err = filter.Validate()
   120  			if err != tc.expectedErr {
   121  				t.Fatalf("%d: Expected %v but got %v", i+1, tc.expectedErr, err)
   122  			}
   123  		})
   124  	}
   125  }