github.com/google/osv-scalibr@v0.4.1/detector/cis/generic_linux/etcpasswdpermissions/detector_helper_test.go (about) 1 // Copyright 2025 Google LLC 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 //go:build linux || darwin 16 17 package etcpasswdpermissions_test 18 19 import ( 20 "errors" 21 "io/fs" 22 "os" 23 "syscall" 24 "time" 25 ) 26 27 type fakeFile struct { 28 perms fs.FileMode 29 uid uint32 30 gid uint32 31 } 32 33 type fakeFileInfo struct { 34 perms fs.FileMode 35 uid uint32 36 gid uint32 37 } 38 39 func (f *fakeFS) Open(name string) (fs.File, error) { 40 if name == "etc/passwd" { 41 if f.exists { 42 return &fakeFile{perms: f.perms, uid: f.uid, gid: f.gid}, nil 43 } 44 return nil, os.ErrNotExist 45 } 46 return nil, errors.New("failed to open") 47 } 48 func (fakeFS) ReadDir(name string) ([]fs.DirEntry, error) { 49 return nil, errors.New("not implemented") 50 } 51 func (fakeFS) Stat(name string) (fs.FileInfo, error) { 52 return nil, errors.New("not implemented") 53 } 54 55 func (f *fakeFile) Stat() (fs.FileInfo, error) { 56 return &fakeFileInfo{perms: f.perms, uid: f.uid, gid: f.gid}, nil 57 } 58 func (fakeFile) Read([]byte) (int, error) { return 0, errors.New("failed to read") } 59 func (fakeFile) Close() error { return nil } 60 61 func (fakeFileInfo) Name() string { return "/etc/passwd" } 62 func (fakeFileInfo) Size() int64 { return 1 } 63 func (i *fakeFileInfo) Mode() fs.FileMode { return i.perms } 64 func (fakeFileInfo) ModTime() time.Time { return time.Now() } 65 func (i *fakeFileInfo) IsDir() bool { return false } 66 func (i *fakeFileInfo) Sys() any { return &syscall.Stat_t{Uid: i.uid, Gid: i.gid} }