github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/ramfs/tree_test.go (about)

     1  // Copyright 2018 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 ramfs
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/SagerNet/gvisor/pkg/sentry/contexttest"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    22  )
    23  
    24  func TestMakeDirectoryTree(t *testing.T) {
    25  
    26  	for _, test := range []struct {
    27  		name    string
    28  		subdirs []string
    29  	}{
    30  		{
    31  			name: "abs paths",
    32  			subdirs: []string{
    33  				"/tmp",
    34  				"/tmp/a/b",
    35  				"/tmp/a/c/d",
    36  				"/tmp/c",
    37  				"/proc",
    38  				"/dev/a/b",
    39  				"/tmp",
    40  			},
    41  		},
    42  		{
    43  			name: "rel paths",
    44  			subdirs: []string{
    45  				"tmp",
    46  				"tmp/a/b",
    47  				"tmp/a/c/d",
    48  				"tmp/c",
    49  				"proc",
    50  				"dev/a/b",
    51  				"tmp",
    52  			},
    53  		},
    54  	} {
    55  		ctx := contexttest.Context(t)
    56  		mount := fs.NewPseudoMountSource(ctx)
    57  		tree, err := MakeDirectoryTree(ctx, mount, test.subdirs)
    58  		if err != nil {
    59  			t.Errorf("%s: failed to make ramfs tree, got error %v, want nil", test.name, err)
    60  			continue
    61  		}
    62  
    63  		// Expect to be able to find each of the paths.
    64  		mm, err := fs.NewMountNamespace(ctx, tree)
    65  		if err != nil {
    66  			t.Errorf("%s: failed to create mount manager: %v", test.name, err)
    67  			continue
    68  		}
    69  		root := mm.Root()
    70  		defer mm.DecRef(ctx)
    71  
    72  		for _, p := range test.subdirs {
    73  			maxTraversals := uint(0)
    74  			if _, err := mm.FindInode(ctx, root, nil, p, &maxTraversals); err != nil {
    75  				t.Errorf("%s: failed to find node %s: %v", test.name, p, err)
    76  				break
    77  			}
    78  		}
    79  	}
    80  }