From bcd1b0958dd3302d503a06797fdd211bb3e48d42 Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Sun, 12 Dec 2021 12:35:08 +0000 Subject: [PATCH] aa-log: add missing unit test. --- cmd/aa-log/main.go | 25 +++++++++++++++++-------- cmd/aa-log/main_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/cmd/aa-log/main.go b/cmd/aa-log/main.go index 44d3c2a8..3a5c8e9c 100644 --- a/cmd/aa-log/main.go +++ b/cmd/aa-log/main.go @@ -8,6 +8,7 @@ import ( "bufio" "fmt" "os" + "path/filepath" "regexp" "strings" ) @@ -156,24 +157,32 @@ func (aaLogs AppArmorLogs) String() string { return res } -func main() { +func aaLog(args []string, path string) error { profile := "" - if len(os.Args) >= 2 { - profile = os.Args[1] + if len(args) >= 2 { + profile = args[1] } - file, err := os.Open(LogFile) + file, err := os.Open(filepath.Clean(path)) if err != nil { - fmt.Println(err) - os.Exit(1) + return err } + /* #nosec G307 */ defer func() { if err := file.Close(); err != nil { - fmt.Println("Error closing file:", err) - os.Exit(1) + fmt.Println(err) } }() aaLogs := NewApparmorLogs(file, profile) fmt.Print(aaLogs.String()) + return err +} + +func main() { + err := aaLog(os.Args, LogFile) + if err != nil { + fmt.Println(err) + os.Exit(1) + } } diff --git a/cmd/aa-log/main_test.go b/cmd/aa-log/main_test.go index 861e861a..ff4320bd 100644 --- a/cmd/aa-log/main_test.go +++ b/cmd/aa-log/main_test.go @@ -127,3 +127,32 @@ func TestAppArmorLogs_String(t *testing.T) { }) } } + +func Test_app(t *testing.T) { + tests := []struct { + name string + args []string + path string + wantErr bool + }{ + { + name: "OK", + args: []string{"aa-log", ""}, + path: "../../tests/audit.log", + wantErr: false, + }, + { + name: "No logfile", + args: []string{"aa-log", ""}, + path: "../../tests/log", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := aaLog(tt.args, tt.path); (err != nil) != tt.wantErr { + t.Errorf("aaLog() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}