From cb6b60324c1dbf206c41be8f9d3dbd8229cda1f6 Mon Sep 17 00:00:00 2001 From: jonaustin09 Date: Mon, 18 Sep 2023 10:46:02 -0400 Subject: [PATCH] fix: Fixes #243, fixed the error case for CopyObject to copy the object into itself --- backend/posix/posix.go | 4 ++++ integration/action-tests.go | 1 + integration/tests.go | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/backend/posix/posix.go b/backend/posix/posix.go index c2a006c..1af92fb 100644 --- a/backend/posix/posix.go +++ b/backend/posix/posix.go @@ -1239,6 +1239,10 @@ func (p *Posix) CopyObject(ctx context.Context, input *s3.CopyObjectInput) (*s3. dstBucket := *input.Bucket dstObject := *input.Key + if fmt.Sprintf("%v/%v", srcBucket, srcObject) == fmt.Sprintf("%v/%v", dstBucket, dstObject) { + return &s3.CopyObjectOutput{}, s3err.GetAPIError(s3err.ErrInvalidCopyDest) + } + _, err := os.Stat(srcBucket) if errors.Is(err, fs.ErrNotExist) { return nil, s3err.GetAPIError(s3err.ErrNoSuchBucket) diff --git a/integration/action-tests.go b/integration/action-tests.go index bb9593b..128e092 100644 --- a/integration/action-tests.go +++ b/integration/action-tests.go @@ -88,6 +88,7 @@ func TestDeleteObjects(s *S3Conf) { func TestCopyObject(s *S3Conf) { CopyObject_non_existing_dst_bucket(s) + CopyObject_copy_to_itself(s) CopyObject_success(s) } diff --git a/integration/tests.go b/integration/tests.go index d3e7ee7..9f7865d 100644 --- a/integration/tests.go +++ b/integration/tests.go @@ -1676,6 +1676,28 @@ func CopyObject_non_existing_dst_bucket(s *S3Conf) { }) } +func CopyObject_copy_to_itself(s *S3Conf) { + testName := "CopyObject_copy_to_itself" + actionHandler(s, testName, func(s3client *s3.Client, bucket string) error { + obj := "my-obj" + err := putObjects(s3client, []string{obj}, bucket) + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(context.Background(), shortTimeout) + _, err = s3client.CopyObject(ctx, &s3.CopyObjectInput{ + Bucket: &bucket, + Key: &obj, + CopySource: getPtr(fmt.Sprintf("%v/%v", bucket, obj)), + }) + cancel() + if err := checkApiErr(err, s3err.GetAPIError(s3err.ErrInvalidCopyDest)); err != nil { + return err + } + return nil + }) +} + func CopyObject_success(s *S3Conf) { testName := "CopyObject_success" actionHandler(s, testName, func(s3client *s3.Client, bucket string) error {