github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/gofer/gofer_test.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gofer
    16  
    17  import (
    18  	"sync/atomic"
    19  	"testing"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/p9"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/contexttest"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/pgalloc"
    24  )
    25  
    26  func TestDestroyIdempotent(t *testing.T) {
    27  	ctx := contexttest.Context(t)
    28  	fs := filesystem{
    29  		mfp: pgalloc.MemoryFileProviderFromContext(ctx),
    30  		opts: filesystemOptions{
    31  			// Test relies on no dentry being held in the cache.
    32  			maxCachedDentries: 0,
    33  		},
    34  		syncableDentries: make(map[*dentry]struct{}),
    35  		inoByQIDPath:     make(map[uint64]uint64),
    36  	}
    37  
    38  	attr := &p9.Attr{
    39  		Mode: p9.ModeRegular,
    40  	}
    41  	mask := p9.AttrMask{
    42  		Mode: true,
    43  		Size: true,
    44  	}
    45  	parent, err := fs.newDentry(ctx, p9file{}, p9.QID{}, mask, attr)
    46  	if err != nil {
    47  		t.Fatalf("fs.newDentry(): %v", err)
    48  	}
    49  
    50  	child, err := fs.newDentry(ctx, p9file{}, p9.QID{}, mask, attr)
    51  	if err != nil {
    52  		t.Fatalf("fs.newDentry(): %v", err)
    53  	}
    54  	parent.cacheNewChildLocked(child, "child")
    55  
    56  	fs.renameMu.Lock()
    57  	defer fs.renameMu.Unlock()
    58  	child.checkCachingLocked(ctx, true /* renameMuWriteLocked */)
    59  	if got := atomic.LoadInt64(&child.refs); got != -1 {
    60  		t.Fatalf("child.refs=%d, want: -1", got)
    61  	}
    62  	// Parent will also be destroyed when child reference is removed.
    63  	if got := atomic.LoadInt64(&parent.refs); got != -1 {
    64  		t.Fatalf("parent.refs=%d, want: -1", got)
    65  	}
    66  	child.checkCachingLocked(ctx, true /* renameMuWriteLocked */)
    67  	child.checkCachingLocked(ctx, true /* renameMuWriteLocked */)
    68  }