github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/bucket/lifecycle/rule_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 // TestInvalidRules checks if Rule xml with invalid elements returns 27 // appropriate errors on validation 28 func TestInvalidRules(t *testing.T) { 29 invalidTestCases := []struct { 30 inputXML string 31 expectedErr error 32 }{ 33 { // Rule with ID longer than 255 characters 34 inputXML: ` <Rule> 35 <ID> babababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab </ID> 36 </Rule>`, 37 expectedErr: errInvalidRuleID, 38 }, 39 { // Rule with empty ID 40 inputXML: `<Rule> 41 <ID></ID> 42 <Filter><Prefix></Prefix></Filter> 43 <Expiration> 44 <Days>365</Days> 45 </Expiration> 46 <Status>Enabled</Status> 47 </Rule>`, 48 expectedErr: nil, 49 }, 50 { // Rule with empty status 51 inputXML: ` <Rule> 52 <ID>rule with empty status</ID> 53 <Status></Status> 54 </Rule>`, 55 expectedErr: errEmptyRuleStatus, 56 }, 57 { // Rule with invalid status 58 inputXML: ` <Rule> 59 <ID>rule with invalid status</ID> 60 <Status>OK</Status> 61 </Rule>`, 62 expectedErr: errInvalidRuleStatus, 63 }, 64 { // Rule with negative values for ObjectSizeLessThan 65 inputXML: `<Rule> 66 <ID>negative-obj-size-less-than</ID> 67 <Filter><ObjectSizeLessThan>-1</ObjectSizeLessThan></Filter> 68 <Expiration> 69 <Days>365</Days> 70 </Expiration> 71 <Status>Enabled</Status> 72 </Rule>`, 73 expectedErr: errXMLNotWellFormed, 74 }, 75 { // Rule with negative values for And>ObjectSizeLessThan 76 inputXML: `<Rule> 77 <ID>negative-and-obj-size-less-than</ID> 78 <Filter><And><ObjectSizeLessThan>-1</ObjectSizeLessThan></And></Filter> 79 <Expiration> 80 <Days>365</Days> 81 </Expiration> 82 <Status>Enabled</Status> 83 </Rule>`, 84 expectedErr: errXMLNotWellFormed, 85 }, 86 { // Rule with negative values for ObjectSizeGreaterThan 87 inputXML: `<Rule> 88 <ID>negative-obj-size-greater-than</ID> 89 <Filter><ObjectSizeGreaterThan>-1</ObjectSizeGreaterThan></Filter> 90 <Expiration> 91 <Days>365</Days> 92 </Expiration> 93 <Status>Enabled</Status> 94 </Rule>`, 95 expectedErr: errXMLNotWellFormed, 96 }, 97 { // Rule with negative values for And>ObjectSizeGreaterThan 98 inputXML: `<Rule> 99 <ID>negative-and-obj-size-greater-than</ID> 100 <Filter><And><ObjectSizeGreaterThan>-1</ObjectSizeGreaterThan></And></Filter> 101 <Expiration> 102 <Days>365</Days> 103 </Expiration> 104 <Status>Enabled</Status> 105 </Rule>`, 106 expectedErr: errXMLNotWellFormed, 107 }, 108 } 109 110 for i, tc := range invalidTestCases { 111 t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) { 112 var rule Rule 113 err := xml.Unmarshal([]byte(tc.inputXML), &rule) 114 if err != nil { 115 t.Fatal(err) 116 } 117 118 if err := rule.Validate(); err != tc.expectedErr { 119 t.Fatalf("%d: Expected %v but got %v", i+1, tc.expectedErr, err) 120 } 121 }) 122 } 123 }