Commit c9fd0958 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys Committed by Han-Wen Nienhuys

fs: check outcome of mode fixup result

Add TestReaddirTypeFixup(). This test currently fails
because FixMode is broken. Will be fixed in a later commit.

Change-Id: Ie4eab9c5817ddb772b73aa22e8f75f5d4f2ad49c
parent 692d9bbe
...@@ -6,7 +6,8 @@ package fs ...@@ -6,7 +6,8 @@ package fs
import ( import (
"context" "context"
"math/rand" "fmt"
"hash/crc32"
"os" "os"
"syscall" "syscall"
"testing" "testing"
...@@ -26,7 +27,9 @@ func (fn *randomTypeTest) Lookup(ctx context.Context, name string, out *fuse.Ent ...@@ -26,7 +27,9 @@ func (fn *randomTypeTest) Lookup(ctx context.Context, name string, out *fuse.Ent
stable := StableAttr{ stable := StableAttr{
Mode: fuse.S_IFDIR, Mode: fuse.S_IFDIR,
} }
if rand.Intn(2) == 0 {
// Override the file type on a pseudo-random subset of entries
if crc32.ChecksumIEEE([]byte(name))%2 == 0 {
stable.Mode = fuse.S_IFREG stable.Mode = fuse.S_IFREG
} }
...@@ -41,13 +44,14 @@ func (fn *randomTypeTest) Readdir(ctx context.Context) (DirStream, syscall.Errno ...@@ -41,13 +44,14 @@ func (fn *randomTypeTest) Readdir(ctx context.Context) (DirStream, syscall.Errno
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
entries = append(entries, fuse.DirEntry{ entries = append(entries, fuse.DirEntry{
Name: "one", Name: fmt.Sprintf("%d", i),
Mode: fuse.S_IFDIR, Mode: fuse.S_IFDIR,
}) })
} }
return NewListDirStream(entries), syscall.F_OK return NewListDirStream(entries), syscall.F_OK
} }
// TestReaddirTypeFixup tests that DirEntryList.FixMode() works as expected.
func TestReaddirTypeFixup(t *testing.T) { func TestReaddirTypeFixup(t *testing.T) {
root := &randomTypeTest{} root := &randomTypeTest{}
...@@ -60,8 +64,24 @@ func TestReaddirTypeFixup(t *testing.T) { ...@@ -60,8 +64,24 @@ func TestReaddirTypeFixup(t *testing.T) {
} }
defer f.Close() defer f.Close()
// No panic. // (Ab)use loopbackDirStream to call and parse getdents(2) on mntDir.
if _, err := f.Readdir(-1); err != nil { // This makes the kernel call READDIRPLUS, which ultimately calls
// randomTypeTest.Readdir() and randomTypeTest.Lookup() above.
ds, errno := NewLoopbackDirStream(mntDir)
if errno != 0 {
t.Fatalf("readdir: %v", err) t.Fatalf("readdir: %v", err)
} }
for ds.HasNext() {
e, err := ds.Next()
if err != 0 {
t.Errorf("Next: %d", err)
}
t.Logf("%q: mode=0x%x", e.Name, e.Mode)
gotIsDir := (e.Mode & syscall.S_IFDIR) != 0
wantIsdir := (crc32.ChecksumIEEE([]byte(e.Name)) % 2) == 1
if gotIsDir != wantIsdir {
t.Errorf("%q: isdir %v, want %v", e.Name, gotIsDir, wantIsdir)
}
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment