github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/api-errors_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 cmd 19 20 import ( 21 "context" 22 "errors" 23 "testing" 24 25 "github.com/minio/minio/internal/crypto" 26 "github.com/minio/minio/internal/hash" 27 ) 28 29 var toAPIErrorTests = []struct { 30 err error 31 errCode APIErrorCode 32 }{ 33 {err: hash.BadDigest{}, errCode: ErrBadDigest}, 34 {err: hash.SHA256Mismatch{}, errCode: ErrContentSHA256Mismatch}, 35 {err: IncompleteBody{}, errCode: ErrIncompleteBody}, 36 {err: ObjectExistsAsDirectory{}, errCode: ErrObjectExistsAsDirectory}, 37 {err: BucketNameInvalid{}, errCode: ErrInvalidBucketName}, 38 {err: BucketExists{}, errCode: ErrBucketAlreadyOwnedByYou}, 39 {err: ObjectNotFound{}, errCode: ErrNoSuchKey}, 40 {err: ObjectNameInvalid{}, errCode: ErrInvalidObjectName}, 41 {err: InvalidUploadID{}, errCode: ErrNoSuchUpload}, 42 {err: InvalidPart{}, errCode: ErrInvalidPart}, 43 {err: InsufficientReadQuorum{}, errCode: ErrSlowDownRead}, 44 {err: InsufficientWriteQuorum{}, errCode: ErrSlowDownWrite}, 45 {err: InvalidUploadIDKeyCombination{}, errCode: ErrNotImplemented}, 46 {err: MalformedUploadID{}, errCode: ErrNoSuchUpload}, 47 {err: PartTooSmall{}, errCode: ErrEntityTooSmall}, 48 {err: BucketNotEmpty{}, errCode: ErrBucketNotEmpty}, 49 {err: BucketNotFound{}, errCode: ErrNoSuchBucket}, 50 {err: StorageFull{}, errCode: ErrStorageFull}, 51 {err: NotImplemented{}, errCode: ErrNotImplemented}, 52 {err: errSignatureMismatch, errCode: ErrSignatureDoesNotMatch}, 53 54 // SSE-C errors 55 {err: crypto.ErrInvalidCustomerAlgorithm, errCode: ErrInvalidSSECustomerAlgorithm}, 56 {err: crypto.ErrMissingCustomerKey, errCode: ErrMissingSSECustomerKey}, 57 {err: crypto.ErrInvalidCustomerKey, errCode: ErrAccessDenied}, 58 {err: crypto.ErrMissingCustomerKeyMD5, errCode: ErrMissingSSECustomerKeyMD5}, 59 {err: crypto.ErrCustomerKeyMD5Mismatch, errCode: ErrSSECustomerKeyMD5Mismatch}, 60 {err: errObjectTampered, errCode: ErrObjectTampered}, 61 62 {err: nil, errCode: ErrNone}, 63 {err: errors.New("Custom error"), errCode: ErrInternalError}, // Case where err type is unknown. 64 } 65 66 func TestAPIErrCode(t *testing.T) { 67 ctx := context.Background() 68 for i, testCase := range toAPIErrorTests { 69 errCode := toAPIErrorCode(ctx, testCase.err) 70 if errCode != testCase.errCode { 71 t.Errorf("Test %d: Expected error code %d, got %d", i+1, testCase.errCode, errCode) 72 } 73 } 74 } 75 76 // Check if an API error is properly defined 77 func TestAPIErrCodeDefinition(t *testing.T) { 78 for errAPI := ErrNone + 1; errAPI < apiErrCodeEnd; errAPI++ { 79 errCode, ok := errorCodes[errAPI] 80 if !ok { 81 t.Fatal(errAPI, "error code is not defined in the API error code table") 82 } 83 if errCode.Code == "" { 84 t.Fatal(errAPI, "error code has an empty XML code") 85 } 86 if errCode.HTTPStatusCode == 0 { 87 t.Fatal(errAPI, "error code has a zero HTTP status code") 88 } 89 } 90 }