2023-04-17 00:26:46 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
2024-02-07 00:16:21 +01:00
|
|
|
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
|
2023-04-17 00:26:46 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2023-09-30 16:36:01 +02:00
|
|
|
"regexp"
|
2023-04-17 00:26:46 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-10-11 00:47:31 +02:00
|
|
|
func TestDecodeHexInString(t *testing.T) {
|
2023-04-17 00:26:46 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
str string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Hexa",
|
2023-10-11 00:47:31 +02:00
|
|
|
str: `apparmor="ALLOWED" operation="rename_dest" parent=6974 profile="/usr/sbin/httpd2-prefork//vhost_foo" name=2F686F6D652F7777772F666F6F2E6261722E696E2F68747470646F63732F61707061726D6F722F696D616765732F746573742F696D61676520312E6A7067 pid=20143 comm="httpd2-prefork" requested_mask="wc"`,
|
|
|
|
want: `apparmor="ALLOWED" operation="rename_dest" parent=6974 profile="/usr/sbin/httpd2-prefork//vhost_foo" name="/home/www/foo.bar.in/httpdocs/apparmor/images/test/image 1.jpg" pid=20143 comm="httpd2-prefork" requested_mask="wc"`,
|
2023-04-17 00:26:46 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Not Hexa",
|
2023-10-11 00:47:31 +02:00
|
|
|
str: `type=AVC msg=audit(1424425690.883:716630): apparmor="ALLOWED" operation="file_mmap" info="Failed name lookup - disconnected path" error=-13 profile="/sbin/klogd" name="var/run/nscd/passwd" pid=25333 comm="id" requested_mask="r" denied_mask="r" fsuid=1002 ouid=0`,
|
|
|
|
want: `type=AVC msg=audit(1424425690.883:716630): apparmor="ALLOWED" operation="file_mmap" info="Failed name lookup - disconnected path" error=-13 profile="/sbin/klogd" name="var/run/nscd/passwd" pid=25333 comm="id" requested_mask="r" denied_mask="r" fsuid=1002 ouid=0`,
|
2023-04-17 00:26:46 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-10-11 00:47:31 +02:00
|
|
|
if got := DecodeHexInString(tt.str); got != tt.want {
|
|
|
|
t.Errorf("DecodeHexInString() = %v, want %v", got, tt.want)
|
2023-04-17 00:26:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveDuplicate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
inlist []string
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Duplicate",
|
|
|
|
inlist: []string{"foo", "bar", "foo", "bar", ""},
|
|
|
|
want: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := RemoveDuplicate(tt.inlist); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("RemoveDuplicate() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 16:36:01 +02:00
|
|
|
|
|
|
|
func TestToRegexRepl(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
in []string
|
2024-03-10 20:07:55 +01:00
|
|
|
want RegexReplList
|
2023-09-30 16:36:01 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-03-10 20:07:55 +01:00
|
|
|
|
|
|
|
func TestRegexReplList_Replace(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
rr RegexReplList
|
|
|
|
str string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default",
|
|
|
|
rr: []RegexRepl{
|
|
|
|
{Regex: regexp.MustCompile(`^/foo`), Repl: "/bar"},
|
|
|
|
},
|
|
|
|
str: "/foo",
|
|
|
|
want: "/bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := tt.rr.Replace(tt.str); got != tt.want {
|
|
|
|
t.Errorf("RegexReplList.Replace() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|