Go version
go version go1.24.0 windows/amd64
Output of go env
in your module/workspace:
set AR=ar
set CC=gcc
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=1
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set CXX=g++
set GCCGO=gccgo
set GO111MODULE=on
set GOAMD64=v1
set GOARCH=amd64
set GOAUTH=netrc
set GOBIN=
set GOCACHE=C:\Users\USERNAME\AppData\Local\go-build
set GOCACHEPROG=
set GODEBUG=
set GOENV=C:\Users\USERNAME\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFIPS140=off
set GOFLAGS=
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\USERNAME\AppData\Local\Temp\go-build2713624686=/tmp/go-build -gno-record-gcc-switches
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMOD=C:\Users\USERNAME\Desktop\XXXXX\XXXXX\go.mod
set GOMODCACHE=C:\Users\USERNAME\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\USERNAME\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:/Program Files/Go
set GOSUMDB=sum.golang.org
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\USERNAME\AppData\Roaming\go\telemetry
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.24.0
set GOWORK=
set PKG_CONFIG=pkg-config
What did you do?
https://go.dev/play/p/WKEBVh3pZmq I am trying to test a file read operation. The file open operation is really simple to test, I am missing (or maybe not finding) a way to declare files as being invalid or having permission errors. Maybe Is there a simple way that I am missing here?
What did you see happen?
not being able to create a Mode for a single file having a permission error or being invalid.
What did you expect to see?
An invalid file.
Comment From: seankhliao
I'd say it's just not an use case fstest.MapFS is designed for. The code you wrote seems fine. Note that I don't think we can enforce permissions on MapFS #50787
Comment From: mark-pictor-csec
not a use case fstest.MapFS is designed for
I do not understand how this can be. Testing behavior with unreadable files is a must for anyone attempting to write (and test) code that is robust in the face of unexpected (i.e. real world) filesystem behavior.
I know a comment on the linked issue says
[...] adding new checks to MapFS will potentially break too many users [...]
... but my response is, how many of those tests are passing when they shouldn't because the user expected filesystem-like behavior since it looks like a filesystem?
It is possible to work around, but its absence is very surprising.
The following MapFS is sufficient for my needs (returning an error other than os.ErrNotExist
)
// fstest.MapFS ignores file permission bits. This overrides the
// Open method with one that honors the owner read bit, though
// it assumes the current user is the file's owner.
type MapFS struct{ fstest.MapFS }
func (fsys MapFS) Open(name string) (fs.File, error) {
f, err := fsys.MapFS.Open(name)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
if fi.Mode()&0o400 == 0 {
f.Close()
return nil, os.ErrPermission
}
return f, nil
}