mirror of
https://github.com/minio/minio.git
synced 2025-11-07 21:02:58 -05:00
return an error in CopyAligned upon premature EOF (#18110)
add a unit-test to capture this corner case
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2015-2021 MinIO, Inc.
|
||||
// Copyright (c) 2015-2023 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
@@ -20,8 +20,10 @@ package ioutil
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -205,3 +207,36 @@ func TestSameFile(t *testing.T) {
|
||||
t.Fatal("Expected the files not to be same")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyAligned(t *testing.T) {
|
||||
f, err := os.CreateTemp("", "")
|
||||
if err != nil {
|
||||
t.Errorf("Error creating tmp file: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
r := strings.NewReader("hello world")
|
||||
|
||||
bufp := ODirectPoolSmall.Get().(*[]byte)
|
||||
defer ODirectPoolSmall.Put(bufp)
|
||||
|
||||
written, err := CopyAligned(f, io.LimitReader(r, 5), *bufp, r.Size(), f)
|
||||
if !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
t.Errorf("Expected io.ErrUnexpectedEOF, but got %v", err)
|
||||
}
|
||||
if written != 5 {
|
||||
t.Errorf("Expected written to be '5', but got %v", written)
|
||||
}
|
||||
|
||||
f.Seek(0, io.SeekStart)
|
||||
r.Seek(0, io.SeekStart)
|
||||
|
||||
written, err = CopyAligned(f, r, *bufp, r.Size(), f)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("Expected nil, but got %v", err)
|
||||
}
|
||||
if written != r.Size() {
|
||||
t.Errorf("Expected written to be '%v', but got %v", r.Size(), written)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user