2018-01-03 20:53:45 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-10-16 09:32:38 +00:00
|
|
|
"errors"
|
2022-12-02 18:36:43 +00:00
|
|
|
"io"
|
2018-01-03 20:53:45 +00:00
|
|
|
"os"
|
2019-04-28 19:55:31 +00:00
|
|
|
"path"
|
2018-01-03 20:53:45 +00:00
|
|
|
"sort"
|
2019-01-06 13:31:11 +00:00
|
|
|
"strings"
|
2018-01-03 20:53:45 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func verifyFileContentOpenFile(t testing.TB, fs FS, filename string, want []byte) {
|
|
|
|
f, err := fs.OpenFile(filename, O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-12-02 18:36:43 +00:00
|
|
|
buf, err := io.ReadAll(f)
|
2018-01-03 20:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmp.Equal(want, buf) {
|
|
|
|
t.Error(cmp.Diff(want, buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyDirectoryContents(t testing.TB, fs FS, dir string, want []string) {
|
2024-07-21 14:30:49 +00:00
|
|
|
f, err := fs.OpenFile(dir, os.O_RDONLY, 0)
|
2018-01-03 20:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := f.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-30 20:20:32 +00:00
|
|
|
sort.Strings(want)
|
|
|
|
sort.Strings(entries)
|
2018-01-03 20:53:45 +00:00
|
|
|
|
|
|
|
if !cmp.Equal(want, entries) {
|
|
|
|
t.Error(cmp.Diff(want, entries))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fiSlice []os.FileInfo
|
|
|
|
|
|
|
|
func (s fiSlice) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fiSlice) Less(i, j int) bool {
|
|
|
|
return s[i].Name() < s[j].Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fiSlice) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyDirectoryContentsFI(t testing.TB, fs FS, dir string, want []os.FileInfo) {
|
2024-07-21 14:30:49 +00:00
|
|
|
f, err := fs.OpenFile(dir, os.O_RDONLY, 0)
|
2018-01-03 20:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := f.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(fiSlice(want))
|
|
|
|
sort.Sort(fiSlice(entries))
|
|
|
|
|
|
|
|
if len(want) != len(entries) {
|
|
|
|
t.Errorf("wrong number of entries returned, want %d, got %d", len(want), len(entries))
|
|
|
|
}
|
|
|
|
max := len(want)
|
|
|
|
if len(entries) < max {
|
|
|
|
max = len(entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < max; i++ {
|
|
|
|
fi1 := want[i]
|
|
|
|
fi2 := entries[i]
|
|
|
|
|
|
|
|
if fi1.Name() != fi2.Name() {
|
|
|
|
t.Errorf("entry %d: wrong value for Name: want %q, got %q", i, fi1.Name(), fi2.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi1.IsDir() != fi2.IsDir() {
|
|
|
|
t.Errorf("entry %d: wrong value for IsDir: want %v, got %v", i, fi1.IsDir(), fi2.IsDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi1.Mode() != fi2.Mode() {
|
|
|
|
t.Errorf("entry %d: wrong value for Mode: want %v, got %v", i, fi1.Mode(), fi2.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi1.ModTime() != fi2.ModTime() {
|
|
|
|
t.Errorf("entry %d: wrong value for ModTime: want %v, got %v", i, fi1.ModTime(), fi2.ModTime())
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi1.Size() != fi2.Size() {
|
|
|
|
t.Errorf("entry %d: wrong value for Size: want %v, got %v", i, fi1.Size(), fi2.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi1.Sys() != fi2.Sys() {
|
|
|
|
t.Errorf("entry %d: wrong value for Sys: want %v, got %v", i, fi1.Sys(), fi2.Sys())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFileInfo(t testing.TB, fi os.FileInfo, filename string, modtime time.Time, mode os.FileMode, isdir bool) {
|
2019-03-11 19:31:52 +00:00
|
|
|
if fi.IsDir() != isdir {
|
|
|
|
t.Errorf("IsDir returned %t, want %t", fi.IsDir(), isdir)
|
2018-01-03 20:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Mode() != mode {
|
|
|
|
t.Errorf("Mode() returned wrong value, want 0%o, got 0%o", mode, fi.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !modtime.Equal(time.Time{}) && !fi.ModTime().Equal(modtime) {
|
|
|
|
t.Errorf("ModTime() returned wrong value, want %v, got %v", modtime, fi.ModTime())
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:55:31 +00:00
|
|
|
if path.Base(fi.Name()) != fi.Name() {
|
|
|
|
t.Errorf("Name() returned is not base, want %q, got %q", path.Base(fi.Name()), fi.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Name() != path.Base(filename) {
|
|
|
|
t.Errorf("Name() returned wrong value, want %q, got %q", path.Base(filename), fi.Name())
|
2018-01-03 20:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSReader(t *testing.T) {
|
|
|
|
data := test.Random(55, 1<<18+588)
|
|
|
|
now := time.Now()
|
|
|
|
filename := "foobar"
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
f func(t *testing.T, fs FS)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Readdirnames-slash",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
verifyDirectoryContents(t, fs, "/", []string{filename})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Readdirnames-current",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
verifyDirectoryContents(t, fs, ".", []string{filename})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Readdir-slash",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
fi := fakeFileInfo{
|
|
|
|
mode: 0644,
|
|
|
|
modtime: now,
|
|
|
|
name: filename,
|
|
|
|
size: int64(len(data)),
|
|
|
|
}
|
|
|
|
verifyDirectoryContentsFI(t, fs, "/", []os.FileInfo{fi})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Readdir-current",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
fi := fakeFileInfo{
|
|
|
|
mode: 0644,
|
|
|
|
modtime: now,
|
|
|
|
name: filename,
|
|
|
|
size: int64(len(data)),
|
|
|
|
}
|
|
|
|
verifyDirectoryContentsFI(t, fs, ".", []os.FileInfo{fi})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "file/OpenFile",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
verifyFileContentOpenFile(t, fs, filename, data)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "file/Lstat",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
fi, err := fs.Lstat(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFileInfo(t, fi, filename, now, 0644, false)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "file/Stat",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
2024-07-21 14:30:49 +00:00
|
|
|
f, err := fs.OpenFile(filename, os.O_RDONLY, 0)
|
2018-01-03 20:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFileInfo(t, fi, filename, now, 0644, false)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dir/Lstat-slash",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
fi, err := fs.Lstat("/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:58:10 +00:00
|
|
|
checkFileInfo(t, fi, "/", time.Time{}, os.ModeDir|0755, true)
|
2018-01-03 20:53:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dir/Lstat-current",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
fi, err := fs.Lstat(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:58:10 +00:00
|
|
|
checkFileInfo(t, fi, ".", time.Time{}, os.ModeDir|0755, true)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dir/Lstat-error-not-exist",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
_, err := fs.Lstat("other")
|
2022-10-16 09:32:38 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2019-04-28 19:58:10 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-03 20:53:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dir/Open-slash",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
fi, err := fs.Lstat("/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:58:10 +00:00
|
|
|
checkFileInfo(t, fi, "/", time.Time{}, os.ModeDir|0755, true)
|
2018-01-03 20:53:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dir/Open-current",
|
|
|
|
f: func(t *testing.T, fs FS) {
|
|
|
|
fi, err := fs.Lstat(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:58:10 +00:00
|
|
|
checkFileInfo(t, fi, ".", time.Time{}, os.ModeDir|0755, true)
|
2018-01-03 20:53:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
fs := &Reader{
|
|
|
|
Name: filename,
|
2022-12-02 18:36:43 +00:00
|
|
|
ReadCloser: io.NopCloser(bytes.NewReader(data)),
|
2018-01-03 20:53:45 +00:00
|
|
|
|
|
|
|
Mode: 0644,
|
|
|
|
Size: int64(len(data)),
|
|
|
|
ModTime: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
test.f(t, fs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 13:31:11 +00:00
|
|
|
|
2019-04-28 19:58:10 +00:00
|
|
|
func TestFSReaderDir(t *testing.T) {
|
|
|
|
data := test.Random(55, 1<<18+588)
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
filename string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Lstat-absolute",
|
|
|
|
filename: "/path/to/foobar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Lstat-relative",
|
|
|
|
filename: "path/to/foobar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
fs := &Reader{
|
|
|
|
Name: test.filename,
|
2022-12-02 18:36:43 +00:00
|
|
|
ReadCloser: io.NopCloser(bytes.NewReader(data)),
|
2019-04-28 19:58:10 +00:00
|
|
|
|
|
|
|
Mode: 0644,
|
|
|
|
Size: int64(len(data)),
|
|
|
|
ModTime: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := path.Dir(fs.Name)
|
|
|
|
for {
|
|
|
|
if dir == "/" || dir == "." {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := fs.Lstat(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFileInfo(t, fi, dir, time.Time{}, os.ModeDir|0755, true)
|
|
|
|
|
|
|
|
dir = path.Dir(dir)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:31:11 +00:00
|
|
|
func TestFSReaderMinFileSize(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
data string
|
|
|
|
allowEmpty bool
|
|
|
|
readMustErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "regular",
|
|
|
|
data: "foobar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
data: "",
|
|
|
|
allowEmpty: false,
|
|
|
|
readMustErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty2",
|
|
|
|
data: "",
|
|
|
|
allowEmpty: true,
|
|
|
|
readMustErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
fs := &Reader{
|
|
|
|
Name: "testfile",
|
2022-12-02 18:36:43 +00:00
|
|
|
ReadCloser: io.NopCloser(strings.NewReader(test.data)),
|
2019-01-06 13:31:11 +00:00
|
|
|
Mode: 0644,
|
|
|
|
ModTime: time.Now(),
|
|
|
|
AllowEmptyFile: test.allowEmpty,
|
|
|
|
}
|
|
|
|
|
2024-07-21 14:30:49 +00:00
|
|
|
f, err := fs.OpenFile("testfile", os.O_RDONLY, 0)
|
2019-01-06 13:31:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-12-02 18:36:43 +00:00
|
|
|
buf, err := io.ReadAll(f)
|
2019-01-06 13:31:11 +00:00
|
|
|
if test.readMustErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error not found, got nil")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(buf) != test.data {
|
|
|
|
t.Fatalf("wrong data returned, want %q, got %q", test.data, string(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|