apparmor.d/pkg/util/tools_test.go

154 lines
4.1 KiB
Go
Raw Normal View History

// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package util
import (
"reflect"
"regexp"
"testing"
"github.com/arduino/go-paths-helper"
)
2023-10-11 00:47:31 +02:00
func TestDecodeHexInString(t *testing.T) {
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"`,
},
{
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`,
},
}
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)
}
})
}
}
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)
}
})
}
}
func TestToRegexRepl(t *testing.T) {
tests := []struct {
name string
in []string
2024-03-10 20:07:55 +01:00
want RegexReplList
}{
{
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)
}
})
}
}
func TestCopyTo(t *testing.T) {
tests := []struct {
name string
src *paths.Path
dst *paths.Path
wantErr bool
}{
{
name: "default",
src: paths.New("../../apparmor.d/groups/_full/"),
2024-03-27 17:26:01 +01:00
dst: paths.New("/tmp/test/apparmor.d/groups/_full/"),
wantErr: false,
},
{
name: "issue-source",
src: paths.New("../../apparmor.d/groups/nope/"),
2024-03-27 17:26:01 +01:00
dst: paths.New("/tmp/test/apparmor.d/groups/_full/"),
wantErr: true,
},
{
name: "issue-dest-1",
src: paths.New("../../apparmor.d/groups/_full/"),
dst: paths.New("/"),
wantErr: true,
},
{
name: "issue-dest-2",
src: paths.New("../../apparmor.d/groups/_full/"),
dst: paths.New("/_full/"),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CopyTo(tt.src, tt.dst); (err != nil) != tt.wantErr {
t.Errorf("CopyTo() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}