Debian Codegraph @

unix/readv_unix_test.go

raw
1 // Copyright 2026 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 //go:build darwin || linux || openbsd
6
7 package unix_test
8
9 import (
10 "os"
11 "path/filepath"
12 "testing"
13
14 "golang.org/x/sys/unix"
15 )
16
17 func TestWritevReadv(t *testing.T) {
18 r, w, err := os.Pipe()
19 if err != nil {
20 t.Fatal(err)
21 }
22 defer r.Close()
23 defer w.Close()
24
25 data := [][]byte{[]byte("hello"), []byte(", "), []byte("world")}
26 wn, err := unix.Writev(int(w.Fd()), data)
27 if err != nil {
28 t.Fatal(err)
29 }
30 if wn != 12 {
31 t.Fatalf("Writev wrote %d bytes, want 12", wn)
32 }
33 w.Close()
34
35 buf1 := make([]byte, 5)
36 buf2 := make([]byte, 2)
37 buf3 := make([]byte, 5)
38 rn, err := unix.Readv(int(r.Fd()), [][]byte{buf1, buf2, buf3})
39 if err != nil {
40 t.Fatal(err)
41 }
42 if rn != 12 {
43 t.Fatalf("Readv read %d bytes, want 12", rn)
44 }
45 if string(buf1) != "hello" || string(buf2) != ", " || string(buf3[:5]) != "world" {
46 t.Fatalf("Readv got %q %q %q, want %q %q %q",
47 buf1, buf2, buf3, "hello", ", ", "world")
48 }
49 }
50
51 func TestPwritevPreadv(t *testing.T) {
52 path := filepath.Join(t.TempDir(), "preadv_test")
53 f, err := os.Create(path)
54 if err != nil {
55 t.Fatal(err)
56 }
57 t.Cleanup(func() { f.Close() })
58
59 const off = 16
60 data := [][]byte{[]byte("foo"), []byte("bar")}
61 wn, err := unix.Pwritev(int(f.Fd()), data, off)
62 if err != nil {
63 t.Fatal(err)
64 }
65 if wn != 6 {
66 t.Fatalf("Pwritev wrote %d bytes, want 6", wn)
67 }
68
69 got1 := make([]byte, 3)
70 got2 := make([]byte, 3)
71 rn, err := unix.Preadv(int(f.Fd()), [][]byte{got1, got2}, off)
72 if err != nil {
73 t.Fatal(err)
74 }
75 if rn != 6 {
76 t.Fatalf("Preadv read %d bytes, want 6", rn)
77 }
78 if string(got1) != "foo" || string(got2) != "bar" {
79 t.Fatalf("Preadv got %q %q, want %q %q", got1, got2, "foo", "bar")
80 }
81 }
82
83 func TestPwritevOffset(t *testing.T) {
84 // Regression guard: off_t encoding must not overflow on large offsets.
85 // Mirrors the Linux test for golang.org/issues/57291.
86 path := filepath.Join(t.TempDir(), "offset_test")
87 f, err := os.Create(path)
88 if err != nil {
89 t.Fatal(err)
90 }
91 t.Cleanup(func() { f.Close() })
92
93 const off = 20
94 b := [][]byte{{byte(0)}}
95 n, err := unix.Pwritev(int(f.Fd()), b, off)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if n != 1 {
100 t.Fatalf("Pwritev wrote %d bytes, want 1", n)
101 }
102 info, err := f.Stat()
103 if err != nil {
104 t.Fatal(err)
105 }
106 if want := int64(off + 1); info.Size() != want {
107 t.Fatalf("file size %d, want %d", info.Size(), want)
108 }
109 }
110
111 func TestWritevReadvAllocations(t *testing.T) {
112 // Verify no heap allocations when iovec count <= minIovec (8).
113 // Allocations in this path indicate a regression in the fast path.
114 f, err := os.Create(filepath.Join(t.TempDir(), "alloc_test"))
115 if err != nil {
116 t.Fatal(err)
117 }
118 t.Cleanup(func() { f.Close() })
119
120 iovs := make([][]byte, 8)
121 for i := range iovs {
122 iovs[i] = []byte{'A'}
123 }
124
125 check := func(name string, fn func()) {
126 t.Helper()
127 n := int(testing.AllocsPerRun(100, fn))
128 if n != 0 {
129 t.Errorf("%s: got %d allocations, want 0", name, n)
130 }
131 }
132
133 check("Writev", func() { unix.Writev(int(f.Fd()), iovs) })
134 check("Pwritev", func() { unix.Pwritev(int(f.Fd()), iovs, 0) })
135 check("Readv", func() { unix.Readv(int(f.Fd()), iovs) })
136 check("Preadv", func() { unix.Preadv(int(f.Fd()), iovs, 0) })
137 }