github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/bucket/lifecycle/expiration_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package lifecycle
    19  
    20  import (
    21  	"encoding/xml"
    22  	"fmt"
    23  	"testing"
    24  )
    25  
    26  // appropriate errors on validation
    27  func TestInvalidExpiration(t *testing.T) {
    28  	testCases := []struct {
    29  		inputXML    string
    30  		expectedErr error
    31  	}{
    32  		{ // Expiration with zero days
    33  			inputXML: ` <Expiration>
    34                                      <Days>0</Days>
    35                                      </Expiration>`,
    36  			expectedErr: errLifecycleInvalidDays,
    37  		},
    38  		{ // Expiration with invalid date
    39  			inputXML: ` <Expiration>
    40                                      <Date>invalid date</Date>
    41                                      </Expiration>`,
    42  			expectedErr: errLifecycleInvalidDate,
    43  		},
    44  		{ // Expiration with both number of days nor a date
    45  			inputXML: `<Expiration>
    46  		                    <Date>2019-04-20T00:01:00Z</Date>
    47  		                    </Expiration>`,
    48  			expectedErr: errLifecycleDateNotMidnight,
    49  		},
    50  	}
    51  
    52  	for i, tc := range testCases {
    53  		t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) {
    54  			var expiration Expiration
    55  			err := xml.Unmarshal([]byte(tc.inputXML), &expiration)
    56  			if err != tc.expectedErr {
    57  				t.Fatalf("%d: Expected %v but got %v", i+1, tc.expectedErr, err)
    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  		{ // Expiration with a valid number of days and ExpiredObjectAllVersions
    98  			inputXML: `<Expiration>
    99  									<Days>3</Days>
   100  			            <ExpiredObjectAllVersions>true</ExpiredObjectAllVersions>
   101                                      </Expiration>`,
   102  			expectedErr: nil,
   103  		},
   104  		{ // Expiration with a valid ISO 8601 date and ExpiredObjectAllVersions
   105  			inputXML: `<Expiration>
   106  									<Date>2019-04-20T00:00:00Z</Date>
   107  			            <ExpiredObjectAllVersions>true</ExpiredObjectAllVersions>
   108                                      </Expiration>`,
   109  			expectedErr: errLifecycleInvalidDeleteAll,
   110  		},
   111  	}
   112  	for i, tc := range validationTestCases {
   113  		t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) {
   114  			var expiration Expiration
   115  			err := xml.Unmarshal([]byte(tc.inputXML), &expiration)
   116  			if err != nil {
   117  				t.Fatalf("%d: %v", i+1, err)
   118  			}
   119  
   120  			err = expiration.Validate()
   121  			if err != tc.expectedErr {
   122  				t.Fatalf("%d: got: %v, expected: %v", i+1, err, tc.expectedErr)
   123  			}
   124  		})
   125  	}
   126  }