2023-05-06 14:23:16 +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-05-06 14:23:16 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2023-09-30 19:15:55 +02:00
|
|
|
"os"
|
|
|
|
|
2023-05-06 14:23:16 +02:00
|
|
|
"github.com/arduino/go-paths-helper"
|
|
|
|
"github.com/roddhjav/apparmor.d/pkg/logs"
|
2023-09-10 13:21:55 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/util"
|
2024-04-28 01:04:42 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-05-06 14:23:16 +02:00
|
|
|
)
|
|
|
|
|
2023-09-30 19:15:55 +02:00
|
|
|
var (
|
|
|
|
// Integration tests standard output
|
|
|
|
Stdout *os.File
|
|
|
|
|
|
|
|
// Integration tests standard error output
|
|
|
|
Stderr *os.File
|
|
|
|
|
|
|
|
stdoutPath = paths.New("tests/out.log")
|
|
|
|
stderrPath = paths.New("tests/err.log")
|
|
|
|
)
|
|
|
|
|
2023-05-06 14:23:16 +02:00
|
|
|
// TestSuite is the apparmod.d integration tests to run
|
|
|
|
type TestSuite struct {
|
2023-09-10 13:21:55 +02:00
|
|
|
Tests []Test // List of tests to run
|
|
|
|
Ignore []string // Do not run some tests
|
|
|
|
Arguments map[string]string // Common arguments used across all tests
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewScenarios returns a new list of scenarios
|
|
|
|
func NewTestSuite() *TestSuite {
|
2023-09-30 19:15:55 +02:00
|
|
|
var err error
|
|
|
|
Stdout, err = stdoutPath.Create()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
Stderr, err = stderrPath.Create()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-05-06 14:23:16 +02:00
|
|
|
return &TestSuite{
|
2023-09-10 13:21:55 +02:00
|
|
|
Tests: []Test{},
|
2023-05-06 14:23:16 +02:00
|
|
|
Ignore: []string{},
|
|
|
|
Arguments: map[string]string{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write export the list of scenarios to a file
|
|
|
|
func (t *TestSuite) Write(path *paths.Path) error {
|
2023-09-10 13:21:55 +02:00
|
|
|
jsonString, err := yaml.Marshal(&t.Tests)
|
2023-05-06 14:23:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path.Clean()
|
|
|
|
file, err := path.Create()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// Cleanup a bit
|
|
|
|
res := string(jsonString)
|
2023-09-10 13:21:55 +02:00
|
|
|
regClean := util.ToRegexRepl([]string{
|
|
|
|
"- name:", "\n- name:",
|
|
|
|
`(?m)^.*stdin: \[\].*$`, ``,
|
|
|
|
`{{`, `{{ `,
|
|
|
|
`}}`, ` }}`,
|
|
|
|
})
|
2024-03-10 16:53:25 +01:00
|
|
|
res = regClean.Replace(res)
|
2023-05-06 14:23:16 +02:00
|
|
|
_, err = file.WriteString("---\n" + res)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-30 19:15:55 +02:00
|
|
|
// ReadTests import the tests from a file
|
|
|
|
func (t *TestSuite) ReadTests(path *paths.Path) error {
|
2023-05-06 14:23:16 +02:00
|
|
|
content, _ := path.ReadFile()
|
2023-09-10 13:21:55 +02:00
|
|
|
return yaml.Unmarshal(content, &t.Tests)
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadSettings import the common argument and ignore list from a file
|
|
|
|
func (t *TestSuite) ReadSettings(path *paths.Path) error {
|
|
|
|
type temp struct {
|
2023-09-30 19:15:55 +02:00
|
|
|
Arguments map[string]string `yaml:"arguments"`
|
2023-05-06 14:23:16 +02:00
|
|
|
Ignore []string `yaml:"ignore"`
|
|
|
|
}
|
|
|
|
tmp := temp{}
|
|
|
|
content, _ := path.ReadFile()
|
|
|
|
if err := yaml.Unmarshal(content, &tmp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Arguments = tmp.Arguments
|
|
|
|
t.Ignore = tmp.Ignore
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Results returns a sum up of the apparmor logs raised by the scenarios
|
|
|
|
func (t *TestSuite) Results() string {
|
|
|
|
file, _ := logs.GetAuditLogs(logs.LogFiles[0])
|
|
|
|
aaLogs := logs.NewApparmorLogs(file, "")
|
|
|
|
return aaLogs.String()
|
|
|
|
}
|
2023-10-01 00:21:26 +02:00
|
|
|
|
|
|
|
func (t *TestSuite) GetDependencies() []string {
|
|
|
|
res := []string{}
|
|
|
|
for _, test := range t.Tests {
|
|
|
|
res = append(res, test.Dependencies...)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|