open-vault/helper/policyutil/policyutil_test.go

77 lines
2.0 KiB
Go
Raw Normal View History

2016-04-06 00:30:38 +00:00
package policyutil
import "testing"
func TestSanitizePolicies(t *testing.T) {
expected := []string{"foo", "bar"}
actual := SanitizePolicies([]string{"foo", "bar"}, false)
if !EquivalentPolicies(expected, actual) {
t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
}
// If 'default' is already added, do not remove it.
expected = []string{"foo", "bar", "default"}
actual = SanitizePolicies([]string{"foo", "bar", "default"}, false)
2016-04-06 00:30:38 +00:00
if !EquivalentPolicies(expected, actual) {
t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
2016-04-06 00:30:38 +00:00
}
}
2016-04-06 00:30:38 +00:00
func TestParsePolicies(t *testing.T) {
expected := []string{"foo", "bar", "default"}
actual := ParsePolicies("foo,bar")
// add default if not present.
if !EquivalentPolicies(expected, actual) {
t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
}
// do not add default more than once.
actual = ParsePolicies("foo,bar,default")
2016-04-06 00:30:38 +00:00
if !EquivalentPolicies(expected, actual) {
t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
2016-04-06 00:30:38 +00:00
}
// handle spaces and tabs.
actual = ParsePolicies(" foo , bar , default")
2016-04-06 00:30:38 +00:00
if !EquivalentPolicies(expected, actual) {
t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
2016-04-06 00:30:38 +00:00
}
// ignore all others if root is present.
expected = []string{"root"}
actual = ParsePolicies("foo,bar,root")
2016-04-06 00:30:38 +00:00
if !EquivalentPolicies(expected, actual) {
t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
2016-04-06 00:30:38 +00:00
}
// with spaces and tabs.
expected = []string{"root"}
actual = ParsePolicies("foo ,bar, root ")
2016-04-06 00:30:38 +00:00
if !EquivalentPolicies(expected, actual) {
t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
2016-04-06 00:30:38 +00:00
}
}
func TestEquivalentPolicies(t *testing.T) {
a := []string{"foo", "bar"}
var b []string
if EquivalentPolicies(a, b) {
t.Fatal("bad")
}
b = []string{"foo"}
if EquivalentPolicies(a, b) {
t.Fatal("bad")
}
b = []string{"bar", "foo"}
if !EquivalentPolicies(a, b) {
t.Fatal("bad")
}
b = []string{"foo", "default", "bar"}
if !EquivalentPolicies(a, b) {
t.Fatal("bad")
}
}