storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/lifecycle/expiration_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  // appropriate errors on validation
    26  func TestInvalidExpiration(t *testing.T) {
    27  	testCases := []struct {
    28  		inputXML    string
    29  		expectedErr error
    30  	}{
    31  		{ // Expiration with zero days
    32  			inputXML: ` <Expiration>
    33                                      <Days>0</Days>
    34                                      </Expiration>`,
    35  			expectedErr: errLifecycleInvalidDays,
    36  		},
    37  		{ // Expiration with invalid date
    38  			inputXML: ` <Expiration>
    39                                      <Date>invalid date</Date>
    40                                      </Expiration>`,
    41  			expectedErr: errLifecycleInvalidDate,
    42  		},
    43  		{ // Expiration with both number of days nor a date
    44  			inputXML: `<Expiration>
    45  		                    <Date>2019-04-20T00:01:00Z</Date>
    46  		                    </Expiration>`,
    47  			expectedErr: errLifecycleDateNotMidnight,
    48  		},
    49  	}
    50  
    51  	for i, tc := range testCases {
    52  		t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) {
    53  			var expiration Expiration
    54  			err := xml.Unmarshal([]byte(tc.inputXML), &expiration)
    55  			if err != tc.expectedErr {
    56  				t.Fatalf("%d: Expected %v but got %v", i+1, tc.expectedErr, err)
    57  			}
    58  		})
    59  
    60  	}
    61  
    62  	validationTestCases := []struct {
    63  		inputXML    string
    64  		expectedErr error
    65  	}{
    66  		{ // Expiration with a valid ISO 8601 date
    67  			inputXML: `<Expiration>
    68                                      <Date>2019-04-20T00:00:00Z</Date>
    69                                      </Expiration>`,
    70  			expectedErr: nil,
    71  		},
    72  		{ // Expiration with a valid number of days
    73  			inputXML: `<Expiration>
    74                                      <Days>3</Days>
    75                                      </Expiration>`,
    76  			expectedErr: nil,
    77  		},
    78  		{ // Expiration with neither number of days nor a date
    79  			inputXML: `<Expiration>
    80                                      </Expiration>`,
    81  			expectedErr: errXMLNotWellFormed,
    82  		},
    83  		{ // Expiration with both number of days and a date
    84  			inputXML: `<Expiration>
    85                                      <Days>3</Days>
    86                                      <Date>2019-04-20T00:00:00Z</Date>
    87                                      </Expiration>`,
    88  			expectedErr: errLifecycleInvalidExpiration,
    89  		},
    90  		{ // Expiration with both ExpiredObjectDeleteMarker and days
    91  			inputXML: `<Expiration>
    92                                      <Days>3</Days>
    93  			            <ExpiredObjectDeleteMarker>false</ExpiredObjectDeleteMarker>
    94                                      </Expiration>`,
    95  			expectedErr: errLifecycleInvalidDeleteMarker,
    96  		},
    97  	}
    98  	for i, tc := range validationTestCases {
    99  		t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) {
   100  			var expiration Expiration
   101  			err := xml.Unmarshal([]byte(tc.inputXML), &expiration)
   102  			if err != nil {
   103  				t.Fatalf("%d: %v", i+1, err)
   104  			}
   105  
   106  			err = expiration.Validate()
   107  			if err != tc.expectedErr {
   108  				t.Fatalf("%d: got: %v, expected: %v", i+1, err, tc.expectedErr)
   109  			}
   110  		})
   111  	}
   112  }