From 0e70d4f01f7e75841984dbacd509737de242c766 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Sun, 21 Dec 2014 11:14:03 -0800 Subject: [PATCH] config: add validity check for file permissions --- config/file.go | 2 +- config/file_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/config/file.go b/config/file.go index 04f12c2..76b5827 100644 --- a/config/file.go +++ b/config/file.go @@ -21,5 +21,5 @@ type File struct { Content string `yaml:"content"` Owner string `yaml:"owner"` Path string `yaml:"path"` - RawFilePermissions string `yaml:"permissions"` + RawFilePermissions string `yaml:"permissions" valid:"^0?[0-7]{3,4}$"` } diff --git a/config/file_test.go b/config/file_test.go index a1e03e2..e3f0871 100644 --- a/config/file_test.go +++ b/config/file_test.go @@ -46,3 +46,26 @@ func TestEncodingValid(t *testing.T) { } } } + +func TestRawFilePermissionsValid(t *testing.T) { + tests := []struct { + value string + + isValid bool + }{ + {value: "744", isValid: true}, + {value: "0744", isValid: true}, + {value: "1744", isValid: true}, + {value: "01744", isValid: true}, + {value: "11744", isValid: false}, + {value: "rwxr--r--", isValid: false}, + {value: "800", isValid: false}, + } + + for _, tt := range tests { + isValid := (nil == AssertStructValid(File{RawFilePermissions: tt.value})) + if tt.isValid != isValid { + t.Errorf("bad assert (%s): want %t, got %t", tt.value, tt.isValid, isValid) + } + } +}