github.com/minio/madmin-go@v1.7.5/tier-config_test.go (about) 1 // 2 // MinIO Object Storage (c) 2021 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 madmin 18 19 import ( 20 "encoding/json" 21 "testing" 22 ) 23 24 // TestUnmarshalInvalidTierConfig tests that TierConfig parsing can catch invalid tier configs 25 func TestUnmarshalInvalidTierConfig(t *testing.T) { 26 testCases := []struct { 27 cfg TierConfig 28 err error 29 }{ 30 { 31 cfg: TierConfig{ 32 Version: TierConfigVer, 33 Name: "S3TIER?", 34 Type: S3, 35 GCS: &TierGCS{ 36 Creds: "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo", 37 Bucket: "ilmtesting", 38 Endpoint: "https://storage.googleapis.com/", 39 Prefix: "testprefix", 40 Region: "us-west-2", 41 StorageClass: "", 42 }, 43 }, 44 err: ErrTierInvalidConfig, 45 }, 46 { 47 cfg: TierConfig{ 48 Version: "invalid-version", 49 Name: "INVALIDTIER", 50 Type: GCS, 51 GCS: &TierGCS{ 52 Creds: "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo", 53 Bucket: "ilmtesting", 54 Endpoint: "https://storage.googleapis.com/", 55 Prefix: "testprefix", 56 Region: "us-west-2", 57 StorageClass: "", 58 }, 59 }, 60 err: ErrTierInvalidConfigVersion, 61 }, 62 { 63 cfg: TierConfig{ 64 Version: TierConfigVer, 65 Type: GCS, 66 GCS: &TierGCS{ 67 Creds: "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo", 68 Bucket: "ilmtesting", 69 Endpoint: "https://storage.googleapis.com/", 70 Prefix: "testprefix", 71 Region: "us-west-2", 72 StorageClass: "", 73 }, 74 }, 75 err: ErrTierNameEmpty, 76 }, 77 { 78 cfg: TierConfig{ 79 Version: TierConfigVer, 80 Name: "GCSTIER", 81 Type: GCS, 82 GCS: &TierGCS{ 83 Creds: "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo", 84 Bucket: "ilmtesting", 85 Endpoint: "https://storage.googleapis.com/", 86 Prefix: "testprefix", 87 Region: "us-west-2", 88 StorageClass: "", 89 }, 90 }, 91 err: nil, 92 }, 93 } 94 for i, tc := range testCases { 95 data, err := json.Marshal(tc.cfg) 96 if err != nil { 97 t.Fatalf("Test %d: Failed to marshal tier config %v: %v", i+1, tc.cfg, err) 98 } 99 var cfg TierConfig 100 err = json.Unmarshal(data, &cfg) 101 if err != tc.err { 102 t.Fatalf("Test %d: Failed in unmarshal tier config %s: expected %v got %v", i+1, data, tc.err, err) 103 } 104 } 105 106 // Test invalid tier type 107 evilJSON := []byte(`{ 108 "Version": "v1", 109 "Type" : "not-a-type", 110 "Name" : "GCSTIER3", 111 "GCS" : { 112 "Bucket" : "ilmtesting", 113 "Prefix" : "testprefix3", 114 "Endpoint" : "https://storage.googleapis.com/", 115 "Creds": "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo", 116 "Region" : "us-west-2", 117 "StorageClass" : "" 118 } 119 }`) 120 var cfg TierConfig 121 err := json.Unmarshal(evilJSON, &cfg) 122 if err != ErrTierTypeUnsupported { 123 t.Fatalf("Expected to fail with unsupported type but got %v", err) 124 } 125 }