storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/api-errors_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2016 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 cmd
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"storj.io/minio/cmd/crypto"
    27  	"storj.io/minio/pkg/hash"
    28  )
    29  
    30  var toAPIErrorTests = []struct {
    31  	err     error
    32  	errCode APIErrorCode
    33  }{
    34  	{err: hash.BadDigest{}, errCode: ErrBadDigest},
    35  	{err: hash.SHA256Mismatch{}, errCode: ErrContentSHA256Mismatch},
    36  	{err: IncompleteBody{}, errCode: ErrIncompleteBody},
    37  	{err: ObjectExistsAsDirectory{}, errCode: ErrObjectExistsAsDirectory},
    38  	{err: BucketNameInvalid{}, errCode: ErrInvalidBucketName},
    39  	{err: BucketExists{}, errCode: ErrBucketAlreadyOwnedByYou},
    40  	{err: ObjectNotFound{}, errCode: ErrNoSuchKey},
    41  	{err: ObjectNameInvalid{}, errCode: ErrInvalidObjectName},
    42  	{err: InvalidUploadID{}, errCode: ErrNoSuchUpload},
    43  	{err: InvalidPart{}, errCode: ErrInvalidPart},
    44  	{err: InsufficientReadQuorum{}, errCode: ErrSlowDown},
    45  	{err: InsufficientWriteQuorum{}, errCode: ErrSlowDown},
    46  	{err: InvalidMarkerPrefixCombination{}, errCode: ErrNotImplemented},
    47  	{err: InvalidUploadIDKeyCombination{}, errCode: ErrNotImplemented},
    48  	{err: MalformedUploadID{}, errCode: ErrNoSuchUpload},
    49  	{err: PartTooSmall{}, errCode: ErrEntityTooSmall},
    50  	{err: BucketNotEmpty{}, errCode: ErrBucketNotEmpty},
    51  	{err: BucketNotFound{}, errCode: ErrNoSuchBucket},
    52  	{err: StorageFull{}, errCode: ErrStorageFull},
    53  	{err: NotImplemented{}, errCode: ErrNotImplemented},
    54  	{err: errSignatureMismatch, errCode: ErrSignatureDoesNotMatch},
    55  
    56  	// SSE-C errors
    57  	{err: crypto.ErrInvalidCustomerAlgorithm, errCode: ErrInvalidSSECustomerAlgorithm},
    58  	{err: crypto.ErrMissingCustomerKey, errCode: ErrMissingSSECustomerKey},
    59  	{err: crypto.ErrInvalidCustomerKey, errCode: ErrAccessDenied},
    60  	{err: crypto.ErrMissingCustomerKeyMD5, errCode: ErrMissingSSECustomerKeyMD5},
    61  	{err: crypto.ErrCustomerKeyMD5Mismatch, errCode: ErrSSECustomerKeyMD5Mismatch},
    62  	{err: errObjectTampered, errCode: ErrObjectTampered},
    63  
    64  	{err: nil, errCode: ErrNone},
    65  	{err: errors.New("Custom error"), errCode: ErrInternalError}, // Case where err type is unknown.
    66  }
    67  
    68  func TestAPIErrCode(t *testing.T) {
    69  	disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix())
    70  	defer os.RemoveAll(disk)
    71  
    72  	initFSObjects(disk, t)
    73  
    74  	ctx := context.Background()
    75  	for i, testCase := range toAPIErrorTests {
    76  		errCode := toAPIErrorCode(ctx, testCase.err)
    77  		if errCode != testCase.errCode {
    78  			t.Errorf("Test %d: Expected error code %d, got %d", i+1, testCase.errCode, errCode)
    79  		}
    80  	}
    81  }