storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/lifecycle/rule_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  // TestInvalidRules checks if Rule xml with invalid elements returns
    26  // appropriate errors on validation
    27  func TestInvalidRules(t *testing.T) {
    28  	invalidTestCases := []struct {
    29  		inputXML    string
    30  		expectedErr error
    31  	}{
    32  		{ // Rule with ID longer than 255 characters
    33  			inputXML: ` <Rule>
    34  	                    <ID> babababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab </ID>
    35  	                    </Rule>`,
    36  			expectedErr: errInvalidRuleID,
    37  		},
    38  		{ // Rule with empty ID
    39  			inputXML: `<Rule>
    40  							<ID></ID>
    41  							<Filter><Prefix></Prefix></Filter>
    42  							<Expiration>
    43  								<Days>365</Days>
    44  							</Expiration>
    45                              <Status>Enabled</Status>
    46  	                    </Rule>`,
    47  			expectedErr: nil,
    48  		},
    49  		{ // Rule with empty status
    50  			inputXML: ` <Rule>
    51  			                  <ID>rule with empty status</ID>
    52                                <Status></Status>
    53  	                    </Rule>`,
    54  			expectedErr: errEmptyRuleStatus,
    55  		},
    56  		{ // Rule with invalid status
    57  			inputXML: ` <Rule>
    58  			                  <ID>rule with invalid status</ID>
    59                                <Status>OK</Status>
    60  	                    </Rule>`,
    61  			expectedErr: errInvalidRuleStatus,
    62  		},
    63  	}
    64  
    65  	for i, tc := range invalidTestCases {
    66  		t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) {
    67  			var rule Rule
    68  			err := xml.Unmarshal([]byte(tc.inputXML), &rule)
    69  			if err != nil {
    70  				t.Fatal(err)
    71  			}
    72  
    73  			if err := rule.Validate(); err != tc.expectedErr {
    74  				t.Fatalf("%d: Expected %v but got %v", i+1, tc.expectedErr, err)
    75  			}
    76  		})
    77  	}
    78  }