| 1 |
// Copyright 2024 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package os_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/poll" |
| 9 |
. "os" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
var ( |
| 14 |
copyFileTests = []copyFileTestFunc{newSendfileTest} |
| 15 |
copyFileHooks = []copyFileTestHook{hookSendFile} |
| 16 |
) |
| 17 |
|
| 18 |
func testCopyFiles(t *testing.T, size, limit int64) { |
| 19 |
testSendfile(t, size, limit) |
| 20 |
} |
| 21 |
|
| 22 |
func testSendfile(t *testing.T, size int64, limit int64) { |
| 23 |
dst, src, data, hook, name := newSendfileTest(t, size) |
| 24 |
testCopyFile(t, dst, src, data, hook, limit, name) |
| 25 |
} |
| 26 |
|
| 27 |
// newSendFileTest initializes a new test for sendfile over copy_file_range. |
| 28 |
// It hooks package os' call to poll.SendFile and returns the hook, |
| 29 |
// so it can be inspected. |
| 30 |
func newSendfileTest(t *testing.T, size int64) (dst, src *File, data []byte, hook *copyFileHook, name string) { |
| 31 |
t.Helper() |
| 32 |
|
| 33 |
name = "newSendfileTest" |
| 34 |
|
| 35 |
dst, src, data = newCopyFileTest(t, size) |
| 36 |
hook, _ = hookSendFile(t) |
| 37 |
|
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
func hookSendFile(t *testing.T) (*copyFileHook, string) { |
| 42 |
return hookSendFileTB(t), "hookSendFile" |
| 43 |
} |
| 44 |
|
| 45 |
func hookSendFileTB(tb testing.TB) *copyFileHook { |
| 46 |
hook := new(copyFileHook) |
| 47 |
orig := poll.TestHookDidSendFile |
| 48 |
tb.Cleanup(func() { |
| 49 |
poll.TestHookDidSendFile = orig |
| 50 |
}) |
| 51 |
poll.TestHookDidSendFile = func(dstFD *poll.FD, src uintptr, written int64, err error, handled bool) { |
| 52 |
hook.called = true |
| 53 |
hook.dstfd = dstFD.Sysfd |
| 54 |
hook.srcfd = int(src) |
| 55 |
hook.written = written |
| 56 |
hook.err = err |
| 57 |
hook.handled = handled |
| 58 |
} |
| 59 |
return hook |
| 60 |
} |