2016-04-06 00:30:38 +00:00
|
|
|
package policyutil
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2016-05-13 16:20:10 +00:00
|
|
|
func TestSanitizePolicies(t *testing.T) {
|
2016-05-13 15:50:00 +00:00
|
|
|
expected := []string{"foo", "bar"}
|
2016-05-13 16:20:10 +00:00
|
|
|
actual := SanitizePolicies([]string{"foo", "bar"}, false)
|
2016-05-13 15:50:00 +00:00
|
|
|
if !EquivalentPolicies(expected, actual) {
|
|
|
|
t.Fatal("bad: expected:%s\ngot:%s\n", expected, actual)
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:20:10 +00:00
|
|
|
// If 'default' is already added, do not remove it.
|
2016-05-13 15:50:00 +00:00
|
|
|
expected = []string{"foo", "bar", "default"}
|
2016-05-13 16:20:10 +00:00
|
|
|
actual = SanitizePolicies([]string{"foo", "bar", "default"}, false)
|
2016-04-06 00:30:38 +00:00
|
|
|
if !EquivalentPolicies(expected, actual) {
|
|
|
|
t.Fatal("bad: expected:%s\ngot:%s\n", expected, actual)
|
|
|
|
}
|
2016-05-13 16:20:10 +00:00
|
|
|
}
|
2016-04-06 00:30:38 +00:00
|
|
|
|
2016-05-13 16:20:10 +00:00
|
|
|
func TestParsePolicies(t *testing.T) {
|
|
|
|
expected := []string{"foo", "bar", "default"}
|
|
|
|
actual := ParsePolicies("foo,bar")
|
|
|
|
// add default if not present.
|
2016-05-13 15:50:00 +00:00
|
|
|
if !EquivalentPolicies(expected, actual) {
|
|
|
|
t.Fatal("bad: expected:%s\ngot:%s\n", expected, actual)
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:20:10 +00:00
|
|
|
// 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.Fatal("bad: expected:%s\ngot:%s\n", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle spaces and tabs.
|
2016-05-13 16:20:10 +00:00
|
|
|
actual = ParsePolicies(" foo , bar , default")
|
2016-04-06 00:30:38 +00:00
|
|
|
if !EquivalentPolicies(expected, actual) {
|
|
|
|
t.Fatal("bad: expected:%s\ngot:%s\n", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore all others if root is present.
|
|
|
|
expected = []string{"root"}
|
2016-05-13 16:20:10 +00:00
|
|
|
actual = ParsePolicies("foo,bar,root")
|
2016-04-06 00:30:38 +00:00
|
|
|
if !EquivalentPolicies(expected, actual) {
|
|
|
|
t.Fatal("bad: expected:%s\ngot:%s\n", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// with spaces and tabs.
|
|
|
|
expected = []string{"root"}
|
2016-05-13 16:20:10 +00:00
|
|
|
actual = ParsePolicies("foo ,bar, root ")
|
2016-04-06 00:30:38 +00:00
|
|
|
if !EquivalentPolicies(expected, actual) {
|
|
|
|
t.Fatal("bad: expected:%s\ngot:%s\n", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|