return an error in CopyAligned upon premature EOF (#18110)

add a unit-test to capture this corner case
This commit is contained in:
Harshavardhana
2023-09-26 11:20:06 -07:00
committed by GitHub
parent cdeab19673
commit d9f1df01eb
2 changed files with 57 additions and 7 deletions

View File

@@ -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)
}
}