mirror of
https://github.com/golang/go
synced 2024-11-19 00:04:40 -07:00
936084890a
The existing implementation of NameSpace implicitly assumes that a FileSystem with a directory at the top will be mounted at the root mount point "/" of the NameSpace. If this is not the case, then Stat("/") will fail even if ReadDir("/") succeedes. This is unexpected behavior which breaks directory traversal routines (eg. http.FileServer). This CL adds an unexported implementation of FileSystem called emptyVFS that emulates an empty directory and adds a NewNameSpace() function that binds emptyVFS to "/" so that unexpected behavior does not arise even if the use does not mount anything explicitly at "/". Latest patch set causes the FileInfo of the empty top level emulated directory to return "/" for Name() and Zero Time for ModTime() and removes the related struct state fields being used in the previous implementation. Fixes golang/go#14190 Change-Id: Idce2fc3c9b81206847a33840d76b660059d53d18 Reviewed-on: https://go-review.googlesource.com/19445 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
// Copyright 2016 The Go 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 vfs_test
|
|
|
|
import (
|
|
"golang.org/x/tools/godoc/vfs"
|
|
"golang.org/x/tools/godoc/vfs/mapfs"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNewNameSpace(t *testing.T) {
|
|
|
|
// We will mount this filesystem under /fs1
|
|
mount := mapfs.New(map[string]string{"fs1file": "abcdefgh"})
|
|
|
|
// Existing process. This should give error on Stat("/")
|
|
t1 := vfs.NameSpace{}
|
|
t1.Bind("/fs1", mount, "/", vfs.BindReplace)
|
|
|
|
// using NewNameSpace. This should work fine.
|
|
t2 := vfs.NewNameSpace()
|
|
t2.Bind("/fs1", mount, "/", vfs.BindReplace)
|
|
|
|
testcases := map[string][]bool{
|
|
"/": []bool{false, true},
|
|
"/fs1": []bool{true, true},
|
|
"/fs1/fs1file": []bool{true, true},
|
|
}
|
|
|
|
fss := []vfs.FileSystem{t1, t2}
|
|
|
|
for j, fs := range fss {
|
|
for k, v := range testcases {
|
|
_, err := fs.Stat(k)
|
|
result := err == nil
|
|
if result != v[j] {
|
|
t.Errorf("fs: %d, testcase: %s, want: %v, got: %v, err: %s", j, k, v[j], result, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
fi, err := t2.Stat("/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if fi.Name() != "/" {
|
|
t.Errorf("t2.Name() : want:%s got:%s", "/", fi.Name())
|
|
}
|
|
|
|
if !fi.ModTime().IsZero() {
|
|
t.Errorf("t2.Modime() : want:%v got:%v", time.Time{}, fi.ModTime())
|
|
}
|
|
}
|