test(aa-log): add some missing unit tests.

This commit is contained in:
Alexandre Pujol 2023-09-30 15:36:01 +01:00
parent cd80a7d919
commit 0068c1b9a3
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
3 changed files with 91 additions and 4 deletions

View file

@ -9,6 +9,8 @@ import (
"reflect"
"strings"
"testing"
"github.com/roddhjav/apparmor.d/pkg/aa"
)
var (
@ -37,9 +39,9 @@ var (
"denied_mask": "x",
"error": "-1",
"fsuid": "1000",
"ouid": "0",
"ouid": "1000",
"FSUID": "user",
"OUID": "root",
"OUID": "user",
},
}
refPowerProfiles = AppArmorLogs{
@ -272,7 +274,7 @@ func TestAppArmorLogs_String(t *testing.T) {
{
name: "man",
aaLogs: refMan,
want: "\033[1;32mALLOWED\033[0m \033[34mman\033[0m \033[33mexec\033[0m \033[35m@{bin}/preconv\033[0m -> \033[35mman_groff\033[0m info=\"no new privs\" comm=man requested_mask=\033[1;31mx\033[0m denied_mask=\033[1;31mx\033[0m error=-1\n",
want: "\033[1;32mALLOWED\033[0m \033[34mman\033[0m \033[33mexec\033[0m\033[35m owner\033[0m \033[35m@{bin}/preconv\033[0m -> \033[35mman_groff\033[0m info=\"no new privs\" comm=man requested_mask=\033[1;31mx\033[0m denied_mask=\033[1;31mx\033[0m error=-1\n",
},
{
name: "power-profiles-daemon",
@ -288,3 +290,60 @@ func TestAppArmorLogs_String(t *testing.T) {
})
}
}
func TestAppArmorLogs_ParseToProfiles(t *testing.T) {
tests := []struct {
name string
aaLogs AppArmorLogs
want aa.AppArmorProfiles
}{
{
name: "",
aaLogs: append(append(refKmod, refPowerProfiles...), refKmod...),
want: aa.AppArmorProfiles{
"kmod": &aa.AppArmorProfile{
Profile: aa.Profile{
Name: "kmod",
Rules: aa.Rules{
&aa.Unix{
Qualifier: aa.Qualifier{FileInherit: true},
Access: "send receive",
Type: "stream",
Protocol: "0",
},
&aa.Unix{
Qualifier: aa.Qualifier{FileInherit: true},
Access: "send receive",
Type: "stream",
Protocol: "0",
},
},
},
},
"power-profiles-daemon": &aa.AppArmorProfile{
Profile: aa.Profile{
Name: "power-profiles-daemon",
Rules: aa.Rules{
&aa.Dbus{
Access: "send",
Bus: "system",
Name: "org.freedesktop.DBus",
Path: "/org/freedesktop/DBus",
Interface: "org.freedesktop.DBus",
Member: "AddMatch",
Label: "dbus-daemon",
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.aaLogs.ParseToProfiles(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppArmorLogs.ParseToProfiles() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -6,6 +6,7 @@ package util
import (
"reflect"
"regexp"
"testing"
)
@ -55,3 +56,30 @@ func TestRemoveDuplicate(t *testing.T) {
})
}
}
func TestToRegexRepl(t *testing.T) {
tests := []struct {
name string
in []string
want []RegexRepl
}{
{
name: "",
in: []string{
"^/foo/bar", "/foo/bar",
"^/foo/bar", "/foo/bar",
},
want: []RegexRepl{
{Regex: regexp.MustCompile("^/foo/bar"), Repl: "/foo/bar"},
{Regex: regexp.MustCompile("^/foo/bar"), Repl: "/foo/bar"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToRegexRepl(tt.in); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToRegexRepl() = %v, want %v", got, tt.want)
}
})
}
}