tests(aa): improve rules unit tests.

This commit is contained in:
Alexandre Pujol 2024-05-29 21:17:21 +01:00
parent 23eaa20fb7
commit 5f64bb4e0c
Failed to generate hash of commit
3 changed files with 441 additions and 661 deletions

View file

@ -12,7 +12,7 @@ import (
) )
func Test_tokenizeRule(t *testing.T) { func Test_tokenizeRule(t *testing.T) {
for _, tt := range testRules { for _, tt := range testTokenRules {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := tokenize(tt.raw); !reflect.DeepEqual(got, tt.tokens) { if got := tokenize(tt.raw); !reflect.DeepEqual(got, tt.tokens) {
t.Errorf("tokenize() = %v, want %v", got, tt.tokens) t.Errorf("tokenize() = %v, want %v", got, tt.tokens)
@ -37,7 +37,7 @@ func Test_AppArmorProfileFile_Parse(t *testing.T) {
var ( var (
// Test cases for tokenize // Test cases for tokenize
testRules = []struct { testTokenRules = []struct {
name string name string
raw string raw string
tokens []string tokens []string

View file

@ -54,6 +54,9 @@ type Rules []Rule
func (r Rules) Validate() error { func (r Rules) Validate() error {
for _, rule := range r { for _, rule := range r {
if rule == nil {
continue
}
if err := rule.Validate(); err != nil { if err := rule.Validate(); err != nil {
return err return err
} }
@ -66,9 +69,12 @@ func (r Rules) String() string {
} }
// Index returns the index of the first occurrence of rule rin r, or -1 if not present. // Index returns the index of the first occurrence of rule rin r, or -1 if not present.
func (r Rules) Index(rule Rule) int { func (r Rules) Index(item Rule) int {
for idx, rr := range r { for idx, rule := range r {
if rr.Kind() == rule.Kind() && rr.Equals(rule) { if rule == nil {
continue
}
if rule.Kind() == item.Kind() && rule.Equals(item) {
return idx return idx
} }
} }
@ -94,6 +100,9 @@ func (r Rules) Delete(i int) Rules {
func (r Rules) DeleteKind(kind Kind) Rules { func (r Rules) DeleteKind(kind Kind) Rules {
res := make(Rules, 0) res := make(Rules, 0)
for _, rule := range r { for _, rule := range r {
if rule == nil {
continue
}
if rule.Kind() != kind { if rule.Kind() != kind {
res = append(res, rule) res = append(res, rule)
} }
@ -104,6 +113,9 @@ func (r Rules) DeleteKind(kind Kind) Rules {
func (r Rules) Filter(filter Kind) Rules { func (r Rules) Filter(filter Kind) Rules {
res := make(Rules, 0) res := make(Rules, 0)
for _, rule := range r { for _, rule := range r {
if rule == nil {
continue
}
if rule.Kind() != filter { if rule.Kind() != filter {
res = append(res, rule) res = append(res, rule)
} }

File diff suppressed because it is too large Load diff