summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/safemem/io_test.go
blob: 629741beecfe78cc978a0974b4aa1b51abaa1a15 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2018 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.

package safemem

import (
	"bytes"
	"io"
	"testing"
)

func makeBlocks(slices ...[]byte) []Block {
	blocks := make([]Block, 0, len(slices))
	for _, s := range slices {
		blocks = append(blocks, BlockFromSafeSlice(s))
	}
	return blocks
}

func TestFromIOReaderFullRead(t *testing.T) {
	r := FromIOReader{bytes.NewBufferString("foobar")}
	dsts := makeBlocks(make([]byte, 3), make([]byte, 3))
	n, err := r.ReadToBlocks(BlockSeqFromSlice(dsts))
	if wantN := uint64(6); n != wantN || err != nil {
		t.Errorf("ReadToBlocks: got (%v, %v), wanted (%v, nil)", n, err, wantN)
	}
	for i, want := range [][]byte{[]byte("foo"), []byte("bar")} {
		if got := dsts[i].ToSlice(); !bytes.Equal(got, want) {
			t.Errorf("dsts[%d]: got %q, wanted %q", i, got, want)
		}
	}
}

type eofHidingReader struct {
	Reader io.Reader
}

func (r eofHidingReader) Read(dst []byte) (int, error) {
	n, err := r.Reader.Read(dst)
	if err == io.EOF {
		return n, nil
	}
	return n, err
}

func TestFromIOReaderPartialRead(t *testing.T) {
	r := FromIOReader{eofHidingReader{bytes.NewBufferString("foob")}}
	dsts := makeBlocks(make([]byte, 3), make([]byte, 3))
	n, err := r.ReadToBlocks(BlockSeqFromSlice(dsts))
	// FromIOReader should stop after the eofHidingReader returns (1, nil)
	// for a 3-byte read.
	if wantN := uint64(4); n != wantN || err != nil {
		t.Errorf("ReadToBlocks: got (%v, %v), wanted (%v, nil)", n, err, wantN)
	}
	for i, want := range [][]byte{[]byte("foo"), []byte("b\x00\x00")} {
		if got := dsts[i].ToSlice(); !bytes.Equal(got, want) {
			t.Errorf("dsts[%d]: got %q, wanted %q", i, got, want)
		}
	}
}

type singleByteReader struct {
	Reader io.Reader
}

func (r singleByteReader) Read(dst []byte) (int, error) {
	if len(dst) == 0 {
		return r.Reader.Read(dst)
	}
	return r.Reader.Read(dst[:1])
}

func TestSingleByteReader(t *testing.T) {
	r := FromIOReader{singleByteReader{bytes.NewBufferString("foobar")}}
	dsts := makeBlocks(make([]byte, 3), make([]byte, 3))
	n, err := r.ReadToBlocks(BlockSeqFromSlice(dsts))
	// FromIOReader should stop after the singleByteReader returns (1, nil)
	// for a 3-byte read.
	if wantN := uint64(1); n != wantN || err != nil {
		t.Errorf("ReadToBlocks: got (%v, %v), wanted (%v, nil)", n, err, wantN)
	}
	for i, want := range [][]byte{[]byte("f\x00\x00"), []byte("\x00\x00\x00")} {
		if got := dsts[i].ToSlice(); !bytes.Equal(got, want) {
			t.Errorf("dsts[%d]: got %q, wanted %q", i, got, want)
		}
	}
}

func TestReadFullToBlocks(t *testing.T) {
	r := FromIOReader{singleByteReader{bytes.NewBufferString("foobar")}}
	dsts := makeBlocks(make([]byte, 3), make([]byte, 3))
	n, err := ReadFullToBlocks(r, BlockSeqFromSlice(dsts))
	// ReadFullToBlocks should call into FromIOReader => singleByteReader
	// repeatedly until dsts is exhausted.
	if wantN := uint64(6); n != wantN || err != nil {
		t.Errorf("ReadFullToBlocks: got (%v, %v), wanted (%v, nil)", n, err, wantN)
	}
	for i, want := range [][]byte{[]byte("foo"), []byte("bar")} {
		if got := dsts[i].ToSlice(); !bytes.Equal(got, want) {
			t.Errorf("dsts[%d]: got %q, wanted %q", i, got, want)
		}
	}
}

func TestFromIOWriterFullWrite(t *testing.T) {
	srcs := makeBlocks([]byte("foo"), []byte("bar"))
	var dst bytes.Buffer
	w := FromIOWriter{&dst}
	n, err := w.WriteFromBlocks(BlockSeqFromSlice(srcs))
	if wantN := uint64(6); n != wantN || err != nil {
		t.Errorf("WriteFromBlocks: got (%v, %v), wanted (%v, nil)", n, err, wantN)
	}
	if got, want := dst.Bytes(), []byte("foobar"); !bytes.Equal(got, want) {
		t.Errorf("dst: got %q, wanted %q", got, want)
	}
}

type limitedWriter struct {
	Writer io.Writer
	Done   int
	Limit  int
}

func (w *limitedWriter) Write(src []byte) (int, error) {
	count := len(src)
	if count > (w.Limit - w.Done) {
		count = w.Limit - w.Done
	}
	n, err := w.Writer.Write(src[:count])
	w.Done += n
	return n, err
}

func TestFromIOWriterPartialWrite(t *testing.T) {
	srcs := makeBlocks([]byte("foo"), []byte("bar"))
	var dst bytes.Buffer
	w := FromIOWriter{&limitedWriter{&dst, 0, 4}}
	n, err := w.WriteFromBlocks(BlockSeqFromSlice(srcs))
	// FromIOWriter should stop after the limitedWriter returns (1, nil) for a
	// 3-byte write.
	if wantN := uint64(4); n != wantN || err != nil {
		t.Errorf("WriteFromBlocks: got (%v, %v), wanted (%v, nil)", n, err, wantN)
	}
	if got, want := dst.Bytes(), []byte("foob"); !bytes.Equal(got, want) {
		t.Errorf("dst: got %q, wanted %q", got, want)
	}
}

type singleByteWriter struct {
	Writer io.Writer
}

func (w singleByteWriter) Write(src []byte) (int, error) {
	if len(src) == 0 {
		return w.Writer.Write(src)
	}
	return w.Writer.Write(src[:1])
}

func TestSingleByteWriter(t *testing.T) {
	srcs := makeBlocks([]byte("foo"), []byte("bar"))
	var dst bytes.Buffer
	w := FromIOWriter{singleByteWriter{&dst}}
	n, err := w.WriteFromBlocks(BlockSeqFromSlice(srcs))
	// FromIOWriter should stop after the singleByteWriter returns (1, nil)
	// for a 3-byte write.
	if wantN := uint64(1); n != wantN || err != nil {
		t.Errorf("WriteFromBlocks: got (%v, %v), wanted (%v, nil)", n, err, wantN)
	}
	if got, want := dst.Bytes(), []byte("f"); !bytes.Equal(got, want) {
		t.Errorf("dst: got %q, wanted %q", got, want)
	}
}

func TestWriteFullToBlocks(t *testing.T) {
	srcs := makeBlocks([]byte("foo"), []byte("bar"))
	var dst bytes.Buffer
	w := FromIOWriter{singleByteWriter{&dst}}
	n, err := WriteFullFromBlocks(w, BlockSeqFromSlice(srcs))
	// WriteFullToBlocks should call into FromIOWriter => singleByteWriter
	// repeatedly until srcs is exhausted.
	if wantN := uint64(6); n != wantN || err != nil {
		t.Errorf("WriteFullFromBlocks: got (%v, %v), wanted (%v, nil)", n, err, wantN)
	}
	if got, want := dst.Bytes(), []byte("foobar"); !bytes.Equal(got, want) {
		t.Errorf("dst: got %q, wanted %q", got, want)
	}
}