1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
package iovec
import (
"bytes"
"fmt"
"syscall"
"testing"
"unsafe"
"golang.org/x/sys/unix"
)
func TestBuilderEmpty(t *testing.T) {
var builder Builder
iovecs := builder.Build()
if got, want := len(iovecs), 0; got != want {
t.Errorf("len(iovecs) = %d, want %d", got, want)
}
}
func TestBuilderBuild(t *testing.T) {
a := []byte{1, 2}
b := []byte{3, 4, 5}
var builder Builder
builder.Add(a)
builder.Add(b)
builder.Add(nil) // Nil slice won't be added.
builder.Add([]byte{}) // Empty slice won't be added.
iovecs := builder.Build()
if got, want := len(iovecs), 2; got != want {
t.Fatalf("len(iovecs) = %d, want %d", got, want)
}
for i, data := range [][]byte{a, b} {
if got, want := *iovecs[i].Base, data[0]; got != want {
t.Fatalf("*iovecs[%d].Base = %d, want %d", i, got, want)
}
if got, want := iovecs[i].Len, uint64(len(data)); got != want {
t.Fatalf("iovecs[%d].Len = %d, want %d", i, got, want)
}
}
}
func TestBuilderBuildMaxIov(t *testing.T) {
for _, test := range []struct {
numIov int
}{
{
numIov: MaxIovs - 1,
},
{
numIov: MaxIovs,
},
{
numIov: MaxIovs + 1,
},
{
numIov: MaxIovs + 10,
},
} {
name := fmt.Sprintf("numIov=%v", test.numIov)
t.Run(name, func(t *testing.T) {
var data []byte
var builder Builder
for i := 0; i < test.numIov; i++ {
buf := []byte{byte(i)}
builder.Add(buf)
data = append(data, buf...)
}
iovec := builder.Build()
// Check the expected length of iovec.
wantNum := test.numIov
if wantNum > MaxIovs {
wantNum = MaxIovs
}
if got, want := len(iovec), wantNum; got != want {
t.Errorf("len(iovec) = %d, want %d", got, want)
}
// Test a real read-write.
var fds [2]int
if err := unix.Pipe(fds[:]); err != nil {
t.Fatalf("Pipe: %v", err)
}
defer syscall.Close(fds[0])
defer syscall.Close(fds[1])
wrote, _, e := syscall.RawSyscall(syscall.SYS_WRITEV, uintptr(fds[1]), uintptr(unsafe.Pointer(&iovec[0])), uintptr(len(iovec)))
if int(wrote) != len(data) || e != 0 {
t.Fatalf("writev: %v, %v; want %v, 0", wrote, e, len(data))
}
got := make([]byte, len(data))
if n, err := syscall.Read(fds[0], got); n != len(got) || err != nil {
t.Fatalf("read: %v, %v; want %v, nil", n, err, len(got))
}
if !bytes.Equal(got, data) {
t.Errorf("read: got data %v, want %v", got, data)
}
})
}
}
|