Improve go apparmor lib. * aa: (62 commits) feat(aa): handle appending value to defined variables. chore(aa): cosmetic. fix: userspace prebuild test. chore: cleanup unit test. feat(aa): improve log conversion. feat(aa): move conversion function to its own file & add unit tests. fix: go linter issue & not defined variables. tests(aa): improve aa unit tests. tests(aa): improve rules unit tests. feat(aa): ensure the prebuild jobs are working. feat(aa): add more unit tests. chore(aa): cleanup. feat(aa): Move sort, merge and format methods to the rules interface. feat(aa): add the hat template. feat(aa): add the Kind struct to manage aa rules. feat(aa): cleanup rules methods. feat(aa): add function to resolve include preamble. feat(aa): updaqte mount flags order. feat(aa): update default tunable selection. feat(aa): parse apparmor preamble files. ...
100 lines
2.1 KiB
Go
100 lines
2.1 KiB
Go
// apparmor.d - Full set of apparmor profiles
|
|
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
package directive
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/roddhjav/apparmor.d/pkg/paths"
|
|
)
|
|
|
|
func TestNewOption(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
file *paths.Path
|
|
match []string
|
|
want *Option
|
|
}{
|
|
{
|
|
name: "dbus",
|
|
file: nil,
|
|
match: []string{
|
|
" #aa:dbus own bus=system name=org.gnome.DisplayManager",
|
|
"dbus",
|
|
"own bus=system name=org.gnome.DisplayManager",
|
|
},
|
|
want: &Option{
|
|
Name: "dbus",
|
|
ArgMap: map[string]string{
|
|
"bus": "system",
|
|
"name": "org.gnome.DisplayManager",
|
|
"own": "",
|
|
},
|
|
ArgList: []string{"own", "bus=system", "name=org.gnome.DisplayManager"},
|
|
File: nil,
|
|
Raw: " #aa:dbus own bus=system name=org.gnome.DisplayManager",
|
|
},
|
|
},
|
|
{
|
|
name: "only",
|
|
file: nil,
|
|
match: []string{
|
|
" #aa:only opensuse",
|
|
"only",
|
|
"opensuse",
|
|
},
|
|
want: &Option{
|
|
Name: "only",
|
|
ArgMap: map[string]string{"opensuse": ""},
|
|
ArgList: []string{"opensuse"},
|
|
File: nil,
|
|
Raw: " #aa:only opensuse",
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := NewOption(tt.file, tt.match); !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("NewOption() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRun(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
file *paths.Path
|
|
profile string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "none",
|
|
file: nil,
|
|
profile: ` `,
|
|
want: ` `,
|
|
},
|
|
{
|
|
name: "present",
|
|
file: nil,
|
|
profile: ` #aa:dbus own bus=system name=org.freedesktop.systemd1`,
|
|
want: dbusOwnSystemd1,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := Run(tt.file, tt.profile)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("Run() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|