From b0f6f15a9e371469697206281f02c9352ac41550 Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Wed, 19 Jun 2024 23:55:45 +0100 Subject: [PATCH] refractor: use internal Intersect function. --- pkg/aa/rules.go | 6 +++--- pkg/util/tools.go | 19 +++++++++++++++++ pkg/util/tools_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/pkg/aa/rules.go b/pkg/aa/rules.go index f292ee553..409b48618 100644 --- a/pkg/aa/rules.go +++ b/pkg/aa/rules.go @@ -8,7 +8,7 @@ import ( "slices" "strings" - "github.com/samber/lo" + "github.com/roddhjav/apparmor.d/pkg/util" ) type requirement map[string][]string @@ -225,7 +225,7 @@ func (r Rules) Format() Rules { rule := r[i].(*File) // Add padding to align with other transition rule - isTransition := lo.Intersect(transitions, rule.Access) + isTransition := util.Intersect(transitions, rule.Access) if len(isTransition) > 0 { ruleLen := len(rule.Path) + 1 paddingMaxLenght = max(ruleLen, paddingMaxLenght) @@ -265,7 +265,7 @@ func (r Rules) Format() Rules { } // Do not add new line on executable rule - isTransition := lo.Intersect(transitions, rule.Access) + isTransition := util.Intersect(transitions, rule.Access) if len(isTransition) > 0 { continue } diff --git a/pkg/util/tools.go b/pkg/util/tools.go index 96cffb36a..30d5251d6 100644 --- a/pkg/util/tools.go +++ b/pkg/util/tools.go @@ -82,6 +82,25 @@ func RemoveDuplicate[T comparable](inlist []T) []T { return list } +// Intersect returns the intersection between two collections. +// From https://github.com/samber/lo +func Intersect[T comparable](list1 []T, list2 []T) []T { + result := []T{} + seen := map[T]struct{}{} + + for _, elem := range list1 { + seen[elem] = struct{}{} + } + + for _, elem := range list2 { + if _, ok := seen[elem]; ok { + result = append(result, elem) + } + } + + return result +} + // CopyTo recursivelly copy all files from a source path to a destination path. func CopyTo(src *paths.Path, dst *paths.Path) error { files, err := src.ReadDirRecursiveFiltered(nil, diff --git a/pkg/util/tools_test.go b/pkg/util/tools_test.go index b6f85a386..4d5cade6a 100644 --- a/pkg/util/tools_test.go +++ b/pkg/util/tools_test.go @@ -59,6 +59,53 @@ func TestRemoveDuplicate(t *testing.T) { } } +func TestIntersect(t *testing.T) { + tests := []struct { + name string + list1 []int + list2 []int + want []int + }{ + { + name: "1", + list1: []int{0, 1, 2, 3, 4, 5}, + list2: []int{0, 2}, + want: []int{0, 2}, + }, + { + name: "2", + list1: []int{0, 1, 2, 3, 4, 5}, + list2: []int{0, 6}, + want: []int{0}, + }, + { + name: "3", + list1: []int{0, 1, 2, 3, 4, 5}, + list2: []int{-1, 6}, + want: []int{}, + }, + { + name: "4", + list1: []int{0, 6}, + list2: []int{0, 1, 2, 3, 4, 5}, + want: []int{0}, + }, + { + name: "5", + list1: []int{0, 6, 0}, + list2: []int{0, 1, 2, 3, 4, 5}, + want: []int{0}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Intersect(tt.list1, tt.list2); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Intersect() = %v, want %v", got, tt.want) + } + }) + } +} + func TestToRegexRepl(t *testing.T) { tests := []struct { name string