2014-01-15 22:17:40 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2015-01-20 22:13:36 +00:00
|
|
|
"os"
|
2015-05-29 19:03:55 +00:00
|
|
|
"runtime"
|
2014-01-15 22:17:40 +00:00
|
|
|
"testing"
|
2017-05-12 13:41:13 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
2014-01-15 22:17:40 +00:00
|
|
|
)
|
|
|
|
|
2015-01-08 03:11:21 +00:00
|
|
|
func TestStringHash(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-01-08 03:11:21 +00:00
|
|
|
in := "hello world"
|
|
|
|
expected := "5eb63bbbe01eeed093cb22bb8f5acdc3"
|
|
|
|
|
|
|
|
if out := stringHash(in); out != expected {
|
|
|
|
t.Fatalf("bad: %s", out)
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 22:13:36 +00:00
|
|
|
|
|
|
|
func TestSetFilePermissions(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-05-29 19:03:55 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2017-05-12 13:41:13 +00:00
|
|
|
tempFile := testutil.TempFile(t, "consul")
|
2015-01-20 22:13:36 +00:00
|
|
|
path := tempFile.Name()
|
|
|
|
defer os.Remove(path)
|
|
|
|
|
|
|
|
// Bad UID fails
|
2017-09-25 18:40:42 +00:00
|
|
|
if err := setFilePermissions(path, "%", "", ""); err == nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad GID fails
|
2017-09-25 18:40:42 +00:00
|
|
|
if err := setFilePermissions(path, "", "%", ""); err == nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad mode fails
|
2017-09-25 18:40:42 +00:00
|
|
|
if err := setFilePermissions(path, "", "", "%"); err == nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allows omitting user/group/mode
|
2017-09-25 18:40:42 +00:00
|
|
|
if err := setFilePermissions(path, "", "", ""); err != nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't change mode if not given
|
|
|
|
if err := os.Chmod(path, 0700); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
if err := setFilePermissions(path, "", "", ""); err != nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if fi.Mode().String() != "-rwx------" {
|
|
|
|
t.Fatalf("bad: %s", fi.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes mode if given
|
2017-09-25 18:40:42 +00:00
|
|
|
if err := setFilePermissions(path, "", "", "0777"); err != nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
fi, err = os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if fi.Mode().String() != "-rwxrwxrwx" {
|
|
|
|
t.Fatalf("bad: %s", fi.Mode())
|
|
|
|
}
|
|
|
|
}
|