Files
Jakob UnterwurzacherandHan-Wen Nienhuys a9ddcb8a4b handleMap: track per-handle generation number
We used to hand out a new generation number even
for already-known handles. This does not seem to
cause problems on Linux, but osxfuse throws errors
to userspace with

        osxfuse: vnode changed generation

showing up in the kernel log.

Introduce a per-handle generation number that stays
constant until the handle is forgotten.

Tested on Linux and MacOS via gocryptfs. Add test for handle map
behavior.

Fixes https://github.com/hanwen/go-fuse/issues/204
See also https://github.com/hanwen/go-fuse/pull/205
2018-03-18 12:54:41 +01:00

162 lines
3.2 KiB
Go

// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nodefs
import (
"strings"
"testing"
)
func markSeen(t *testing.T, substr string) {
if r := recover(); r != nil {
s := r.(string)
if strings.Contains(s, substr) {
t.Log("expected recovery from: ", r)
} else {
panic(s)
}
}
}
func TestHandleMapLookupCount(t *testing.T) {
for _, portable := range []bool{true, false} {
t.Log("portable:", portable)
v := new(handled)
hm := newPortableHandleMap()
h1, g1 := hm.Register(v)
h2, g2 := hm.Register(v)
if h1 != h2 {
t.Fatalf("double register should reuse handle: got %d want %d.", h2, h1)
}
if g1 != g2 {
t.Fatalf("double register should reuse generation: got %d want %d.", g2, g1)
}
hm.Register(v)
forgotten, obj := hm.Forget(h1, 1)
if forgotten {
t.Fatalf("single forget unref forget object.")
}
if obj != v {
t.Fatalf("should return input object.")
}
if !hm.Has(h1) {
t.Fatalf("handlemap.Has() returned false for live object.")
}
forgotten, obj = hm.Forget(h1, 2)
if !forgotten {
t.Fatalf("unref did not forget object.")
}
if obj != v {
t.Fatalf("should return input object.")
}
if hm.Has(h1) {
t.Fatalf("handlemap.Has() returned false for live object.")
}
}
}
func TestHandleMapBasic(t *testing.T) {
v := new(handled)
hm := newPortableHandleMap()
h, _ := hm.Register(v)
t.Logf("Got handle 0x%x", h)
if !hm.Has(h) {
t.Fatal("Does not have handle")
}
if hm.Handle(v) != h {
t.Fatalf("handle mismatch, got %x want %x", hm.Handle(v), h)
}
if hm.Decode(h) != v {
t.Fatal("address mismatch")
}
if hm.Count() != 1 {
t.Fatal("count error")
}
hm.Forget(h, 1)
if hm.Count() != 0 {
t.Fatal("count error")
}
if hm.Has(h) {
t.Fatal("Still has handle")
}
}
func TestHandleMapMultiple(t *testing.T) {
hm := newPortableHandleMap()
for i := 0; i < 10; i++ {
v := &handled{}
h, _ := hm.Register(v)
if hm.Decode(h) != v {
t.Fatal("address mismatch")
}
if hm.Count() != i+1 {
t.Fatal("count error")
}
}
}
func TestHandleMapGeneration(t *testing.T) {
hm := newPortableHandleMap()
h1, g1 := hm.Register(&handled{})
forgotten, _ := hm.Forget(h1, 1)
if !forgotten {
t.Fatalf("unref did not forget object.")
}
h2, g2 := hm.Register(&handled{})
if h1 != h2 {
t.Fatalf("register should reuse handle: got %d want %d.", h2, h1)
}
if g1 >= g2 {
t.Fatalf("register should increase generation: got %d want greater than %d.", g2, g1)
}
}
func TestHandleMapGenerationKnown(t *testing.T) {
hm := newPortableHandleMap()
o1 := &handled{}
h1, g1 := hm.Register(o1)
o2 := &handled{}
h2, _ := hm.Register(o2)
h3, g3 := hm.Register(o1)
if h1 != h3 {
t.Fatalf("register known should reuse handle: got %d want %d.", h3, h1)
}
if g1 != g3 {
t.Fatalf("register known should reuse generation: got %d want %d.", g3, g1)
}
hm.Forget(h1, 2)
hm.Forget(h2, 1)
h1, g1 = hm.Register(o1)
h2, _ = hm.Register(o2)
h3, g3 = hm.Register(o1)
if h1 != h3 {
t.Fatalf("register known should reuse handle: got %d want %d.", h3, h1)
}
if g1 != g3 {
t.Fatalf("register known should reuse generation: got %d want %d.", g3, g1)
}
}