2020-04-16 20:33:46 +08:00
|
|
|
package vfstest
|
2016-07-18 06:03:23 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-09-11 05:25:26 +08:00
|
|
|
"io/ioutil"
|
2016-07-18 06:03:23 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// TestReadByByte reads by byte including don't read any bytes
|
2016-07-18 06:03:23 +08:00
|
|
|
func TestReadByByte(t *testing.T) {
|
2016-08-20 19:43:33 +08:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2016-07-18 06:03:23 +08:00
|
|
|
var data = []byte("hellohello")
|
|
|
|
run.createFile(t, "testfile", string(data))
|
|
|
|
run.checkDir(t, "testfile 10")
|
|
|
|
|
|
|
|
for i := 0; i < len(data); i++ {
|
2020-04-16 20:33:46 +08:00
|
|
|
fd, err := run.os.Open(run.path("testfile"))
|
2016-07-18 06:03:23 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
n, err := io.ReadFull(fd, buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, n)
|
|
|
|
assert.Equal(t, buf[0], data[j])
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
run.rm(t, "testfile")
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// TestReadChecksum checks the checksum reading is working
|
2017-05-09 00:47:22 +08:00
|
|
|
func TestReadChecksum(t *testing.T) {
|
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
|
|
|
// create file big enough so we exceed any single FUSE read
|
|
|
|
// request
|
|
|
|
b := make([]rune, 3*128*1024)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = 'r'
|
|
|
|
}
|
|
|
|
run.createFile(t, "bigfile", string(b))
|
|
|
|
|
|
|
|
// The hash comparison would fail in Flush, if we did not
|
|
|
|
// ensure we read the whole file
|
2020-04-16 20:33:46 +08:00
|
|
|
fd, err := run.os.Open(run.path("bigfile"))
|
2017-05-09 00:47:22 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
buf := make([]byte, 10)
|
|
|
|
_, err = io.ReadFull(fd, buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = fd.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// The hash comparison would fail, because we only read parts
|
|
|
|
// of the file
|
2020-04-16 20:33:46 +08:00
|
|
|
fd, err = run.os.Open(run.path("bigfile"))
|
2017-05-09 00:47:22 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// read at start
|
|
|
|
_, err = io.ReadFull(fd, buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// read at end
|
2018-04-07 02:53:06 +08:00
|
|
|
_, err = fd.Seek(int64(len(b)-len(buf)), io.SeekStart)
|
2017-05-09 00:47:22 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = io.ReadFull(fd, buf)
|
2017-06-13 18:22:16 +08:00
|
|
|
assert.NoError(t, err)
|
2017-05-09 00:47:22 +08:00
|
|
|
// ensure we don't compare hashes
|
|
|
|
err = fd.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
run.rm(t, "bigfile")
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:12 +08:00
|
|
|
// TestReadSeek test seeking
|
2016-09-11 05:25:26 +08:00
|
|
|
func TestReadSeek(t *testing.T) {
|
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
|
|
|
var data = []byte("helloHELLO")
|
|
|
|
run.createFile(t, "testfile", string(data))
|
|
|
|
run.checkDir(t, "testfile 10")
|
|
|
|
|
2020-04-16 20:33:46 +08:00
|
|
|
fd, err := run.os.Open(run.path("testfile"))
|
2016-09-11 05:25:26 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2016-11-05 17:59:36 +08:00
|
|
|
// Seek to half way
|
2018-04-07 02:53:06 +08:00
|
|
|
_, err = fd.Seek(5, io.SeekStart)
|
2016-09-11 05:25:26 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(fd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, buf, []byte("HELLO"))
|
|
|
|
|
2016-11-05 17:59:36 +08:00
|
|
|
// Test seeking to the end
|
2018-04-07 02:53:06 +08:00
|
|
|
_, err = fd.Seek(10, io.SeekStart)
|
2016-11-05 17:59:36 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
buf, err = ioutil.ReadAll(fd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, buf, []byte(""))
|
|
|
|
|
|
|
|
// Test seeking beyond the end
|
2018-04-07 02:53:06 +08:00
|
|
|
_, err = fd.Seek(1000000, io.SeekStart)
|
2016-11-05 17:59:36 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
buf, err = ioutil.ReadAll(fd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, buf, []byte(""))
|
|
|
|
|
|
|
|
// Now back to the start
|
2018-04-07 02:53:06 +08:00
|
|
|
_, err = fd.Seek(0, io.SeekStart)
|
2016-09-11 05:25:26 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
buf, err = ioutil.ReadAll(fd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, buf, []byte("helloHELLO"))
|
|
|
|
|
|
|
|
err = fd.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
run.rm(t, "testfile")
|
|
|
|
}
|