Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing/fstest: cannot test no permission on folder #50787

Open
staticdev opened this issue Jan 24, 2022 · 5 comments
Open

testing/fstest: cannot test no permission on folder #50787

staticdev opened this issue Jan 24, 2022 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@staticdev
Copy link

staticdev commented Jan 24, 2022

What version of Go are you using (go version)?

$ go version
go version go1.17.3 linux/amd64

Does this issue reproduce with the latest release?

Yes. Not totally sure if it is related to #46776

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/user/gomodule/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3099453485=/tmp/go-build -gno-record-gcc-switches"

What did you do?

tested.go

package x

import (
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
)

func fileExists(fileSystem fs.FS, fileName string) (bool, error) {
	_, err := fs.Stat(fileSystem, fileName)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, fmt.Errorf("File %q does not exist.", fileName)
	}
	return false, err // test case should return here
}

func FileValid(fileSystem fs.FS, fileName string) (string, error) {
	fileExists, err := fileExists(fileSystem, fileName)
	if !fileExists || err != nil {
		return "", err
	}

	return "success", nil
}

tested_test.go

package x

import (
	"io/fs"
	"testing"
	"testing/fstest"
)

var (
	fakeFS = fstest.MapFS{
		"secret-folder":          {Mode: 0000},
		"secret-folder/readme.md": {},
	}
)

func TestFileValidPermissionDenied(t *testing.T) {
	fileName := "readme.md"
	fsys, _ := fs.Sub(fakeFS, "secret-folder")
	wantOut := ""

	out, err := FileValid(fsys, fileName)
	if out != wantOut || err == nil {
		t.Errorf("want %q, nil got %q, %v", wantOut, out, err)
	}
}

What did you expect to see?

This test should pass, returning "", and error that permission is denied.

What did you see instead?

It can Stat the file, even with no permission to the folder.

@staticdev staticdev changed the title affected/package: testing/fstest cannot test permission denied on folder testing/fstest: cannot test permission denied on folder Jan 24, 2022
@seankhliao
Copy link
Member

seankhliao commented Jan 24, 2022

note 0000 == 0 == unset

@staticdev
Copy link
Author

staticdev commented Jan 24, 2022

Didn't find that in the docs. But I found a Go tutorial explaining 0000 as no permissions https://schadokar.dev/to-the-point/how-to-read-and-write-a-file-in-golang/

Does this make sense? How do I give equivalent to chmod 0000 or 000?

@staticdev staticdev changed the title testing/fstest: cannot test permission denied on folder testing/fstest: cannot test no permission on folder Jan 25, 2022
@mknyszek mknyszek added this to the Backlog milestone Jan 25, 2022
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 25, 2022
@mknyszek
Copy link
Contributor

mknyszek commented Jan 25, 2022

CC @bcmills I think?

@bcmills
Copy link
Member

bcmills commented Jan 25, 2022

(Not really my area, but I've been looking at io/fs today anyway.)

Looks like the fast-path for files is here:
https://cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/testing/fstest/mapfs.go;l=51-55;drc=d6aa162f30d63f21f5f4db75e302dbb90595bbe2

It indeed does not check parent permissions at all, although note that the fs.FS.Open method also doesn't specify detailed semantics for ModePerm bits.

@staticdev
Copy link
Author

staticdev commented Jan 27, 2022

@mknyszek can I help with something else? I am new to Golang, maybe would need some guidance to have a fix PR if that is the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants