github.com/hashicorp/vault/sdk@v0.11.0/helper/policyutil/policyutil_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package policyutil
     5  
     6  import "testing"
     7  
     8  func TestSanitizePolicies(t *testing.T) {
     9  	expected := []string{"foo", "bar"}
    10  	actual := SanitizePolicies([]string{"foo", "bar"}, false)
    11  	if !EquivalentPolicies(expected, actual) {
    12  		t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
    13  	}
    14  
    15  	// If 'default' is already added, do not remove it.
    16  	expected = []string{"foo", "bar", "default"}
    17  	actual = SanitizePolicies([]string{"foo", "bar", "default"}, false)
    18  	if !EquivalentPolicies(expected, actual) {
    19  		t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
    20  	}
    21  }
    22  
    23  func TestParsePolicies(t *testing.T) {
    24  	expected := []string{"foo", "bar", "default"}
    25  	actual := ParsePolicies("foo,bar")
    26  	// add default if not present.
    27  	if !EquivalentPolicies(expected, actual) {
    28  		t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
    29  	}
    30  
    31  	// do not add default more than once.
    32  	actual = ParsePolicies("foo,bar,default")
    33  	if !EquivalentPolicies(expected, actual) {
    34  		t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
    35  	}
    36  
    37  	// handle spaces and tabs.
    38  	actual = ParsePolicies(" foo ,	bar	,   default")
    39  	if !EquivalentPolicies(expected, actual) {
    40  		t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
    41  	}
    42  
    43  	// ignore all others if root is present.
    44  	expected = []string{"root"}
    45  	actual = ParsePolicies("foo,bar,root")
    46  	if !EquivalentPolicies(expected, actual) {
    47  		t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
    48  	}
    49  
    50  	// with spaces and tabs.
    51  	expected = []string{"root"}
    52  	actual = ParsePolicies("foo ,bar, root		")
    53  	if !EquivalentPolicies(expected, actual) {
    54  		t.Fatalf("bad: expected:%s\ngot:%s\n", expected, actual)
    55  	}
    56  }
    57  
    58  func TestEquivalentPolicies(t *testing.T) {
    59  	a := []string{"foo", "bar"}
    60  	var b []string
    61  	if EquivalentPolicies(a, b) {
    62  		t.Fatal("bad")
    63  	}
    64  
    65  	b = []string{"foo"}
    66  	if EquivalentPolicies(a, b) {
    67  		t.Fatal("bad")
    68  	}
    69  
    70  	b = []string{"bar", "foo"}
    71  	if !EquivalentPolicies(a, b) {
    72  		t.Fatal("bad")
    73  	}
    74  
    75  	b = []string{"foo", "default", "bar"}
    76  	if !EquivalentPolicies(a, b) {
    77  		t.Fatal("bad")
    78  	}
    79  }