Check for file perms as provided in the conf module (#784)

* Check for file perms as provided in the conf module

* Fix whitespace

* Check for file permissions

* Use only Lstat for error handling and continue if file does not exist

* Update comments to make sure that the todo is more understandable

* conflicts resolved
This commit is contained in:
Manu Gupta
2018-10-26 15:53:05 -04:00
committed by Aaron Schlesinger
parent 3a190df2db
commit b600753926
2 changed files with 88 additions and 4 deletions
+55
View File
@@ -1,6 +1,7 @@
package config
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -357,3 +358,57 @@ func restoreEnv(envVars map[string]string) {
}
}
}
func Test_checkFilePerms(t *testing.T) {
f1, err := ioutil.TempFile(os.TempDir(), "prefix-")
if err != nil {
t.FailNow()
}
defer os.Remove(f1.Name())
err = os.Chmod(f1.Name(), 0777)
f2, err := ioutil.TempFile(os.TempDir(), "prefix-")
if err != nil {
t.FailNow()
}
defer os.Remove(f2.Name())
err = os.Chmod(f2.Name(), 0600)
type args struct {
files []string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"should not have an error on empty file name",
args{
[]string{"", f2.Name()},
},
false,
},
{
"should have an error if all the files have incorrect permissions",
args{
[]string{f1.Name(), f1.Name(), f1.Name()},
},
true,
},
{
"should have an error when at least 1 file has wrong permissions",
args{
[]string{f2.Name(), f1.Name()},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := checkFilePerms(tt.args.files...); (err != nil) != tt.wantErr {
t.Errorf("checkFilePerms() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}