Skip to content

Commit

Permalink
further tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Nov 7, 2023
1 parent be92e9e commit 8f92d9d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
6 changes: 3 additions & 3 deletions acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func (a *ACL) DeleteEntry(e *ACLEntry) *ACLEntry {
// deleteEntryPos delete the entry at a given position
// used internally
func (a *ACL) deleteEntryPos(pos int) *ACLEntry {
tmp := a.entries
a.entries = append(tmp[:pos], tmp[pos+1:]...)
return tmp[pos]
result := a.entries[pos]
a.entries = append(a.entries[:pos], a.entries[pos+1:]...)
return result
}

// EntryExists checks if an entry with the given Tag and ID already exist
Expand Down
60 changes: 60 additions & 0 deletions acls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"
"os"
"os/user"
"reflect"
"strconv"
"testing"
)
Expand Down Expand Up @@ -500,3 +501,62 @@ func TestLoadApplyLoad(t *testing.T) {
t.Errorf("failed removing temp file %s", f.Name())
}
}

func TestACL_DeleteEntry(t *testing.T) {

tests := []struct {
name string
acl *ACL
deleteEntry *ACLEntry // tag and id must match the one on the acl
deletedEntry *ACLEntry // is the one deleted from the ACL
shouldSucceed bool
}{
{
name: "Delete an entry",
acl: &ACL{
version: 2,
entries: []*ACLEntry{
unsortedACLEntries[0],
unsortedACLEntries[1],
unsortedACLEntries[2],
unsortedACLEntries[3],
unsortedACLEntries[4],
},
},
deleteEntry: NewEntry(unsortedACLEntries[2].tag, unsortedACLEntries[2].id, unsortedACLEntries[2].perm),
deletedEntry: unsortedACLEntries[2],
shouldSucceed: true,
},
{
name: "Delete non-existing",
acl: &ACL{
version: 2,
entries: []*ACLEntry{
unsortedACLEntries[0],
unsortedACLEntries[1],
unsortedACLEntries[2],
unsortedACLEntries[3],
unsortedACLEntries[4],
},
},
deleteEntry: NewEntry(TAG_ACL_GROUP, 32456, 7),
deletedEntry: nil,
shouldSucceed: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

preLen := len(tt.acl.entries)

if got := tt.acl.DeleteEntry(tt.deleteEntry); !reflect.DeepEqual(got, tt.deletedEntry) {
t.Errorf("ACL.DeleteEntry() = %v, want %v", got, tt.deletedEntry)
}
if tt.shouldSucceed {
if preLen-1 != len(tt.acl.entries) {
t.Errorf("expected %d entries after delete, got %d", preLen-1, len(tt.acl.entries))
}
}
})
}
}

0 comments on commit 8f92d9d

Please sign in to comment.