Skip to content

Commit

Permalink
os: add a test case of copying a file itself via io.Copy
Browse files Browse the repository at this point in the history
Change-Id: Ib9746cb4f27625cb22620271b280d2da242b2fba
Reviewed-on: https://go-review.googlesource.com/c/go/+/428437
Run-TryBot: Ian Lance Taylor <[email protected]>
Run-TryBot: Andy Pan <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Michael Knyszek <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
  • Loading branch information
panjf2000 authored and gopherbot committed Sep 9, 2022
1 parent bca17d1 commit 86477e5
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/os/readfrom_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
. "os"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -74,6 +75,56 @@ func TestCopyFileRange(t *testing.T) {
mustSeekStart(t, dst2)
mustContainData(t, dst2, data) // through traditional means
})
t.Run("CopyFileItself", func(t *testing.T) {
hook := hookCopyFileRange(t)

f, err := os.CreateTemp("", "file-readfrom-itself-test")
if err != nil {
t.Fatalf("failed to create tmp file: %v", err)
}
t.Cleanup(func() {
f.Close()
os.Remove(f.Name())
})

data := []byte("hello world!")
if _, err := f.Write(data); err != nil {
t.Fatalf("failed to create and feed the file: %v", err)
}

if err := f.Sync(); err != nil {
t.Fatalf("failed to save the file: %v", err)
}

// Rewind it.
if _, err := f.Seek(0, io.SeekStart); err != nil {
t.Fatalf("failed to rewind the file: %v", err)
}

// Read data from the file itself.
if _, err := io.Copy(f, f); err != nil {
t.Fatalf("failed to read from the file: %v", err)
}

if !hook.called || hook.written != 0 || hook.handled || hook.err != nil {
t.Fatalf("poll.CopyFileRange should be called and return the EINVAL error, but got hook.called=%t, hook.err=%v", hook.called, hook.err)
}

// Rewind it.
if _, err := f.Seek(0, io.SeekStart); err != nil {
t.Fatalf("failed to rewind the file: %v", err)
}

data2, err := io.ReadAll(f)
if err != nil {
t.Fatalf("failed to read from the file: %v", err)
}

// It should wind up a double of the original data.
if strings.Repeat(string(data), 2) != string(data2) {
t.Fatalf("data mismatch: %s != %s", string(data), string(data2))
}
})
t.Run("NotRegular", func(t *testing.T) {
t.Run("BothPipes", func(t *testing.T) {
hook := hookCopyFileRange(t)
Expand Down Expand Up @@ -344,6 +395,10 @@ type copyFileRangeHook struct {
srcfd int
remain int64

written int64
handled bool
err error

original func(dst, src *poll.FD, remain int64) (int64, bool, error)
}

Expand All @@ -354,7 +409,8 @@ func (h *copyFileRangeHook) install() {
h.dstfd = dst.Sysfd
h.srcfd = src.Sysfd
h.remain = remain
return h.original(dst, src, remain)
h.written, h.handled, h.err = h.original(dst, src, remain)
return h.written, h.handled, h.err
}
}

Expand Down

0 comments on commit 86477e5

Please sign in to comment.