Path: blob/main/components/ee/agent-smith/pkg/classifier/classifier_test.go
2501 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package classifier_test56import (7"testing"89"github.com/gitpod-io/gitpod/agent-smith/pkg/classifier"10"github.com/google/go-cmp/cmp"11)1213func TestCommandlineClassifier(t *testing.T) {14var (15allowed = []string{"allowed"}16blocked = []string{"blocked"}17)1819type Input struct {20Executable string21Cmdline []string22}23tests := []struct {24Name string25AllowList []string26BlockList []string27Input Input28Expectation *classifier.Classification29}{30{31Name: "empty lists",32Expectation: &classifier.Classification{Level: classifier.LevelNoMatch, Classifier: classifier.ClassifierCommandline},33},34{35Name: "blocked cmd positive",36BlockList: blocked,37Input: Input{Cmdline: blocked},38Expectation: &classifier.Classification{Level: classifier.LevelAudit, Classifier: classifier.ClassifierCommandline, Message: `matched "blocked"`},39},40{41Name: "blocked exec positive",42BlockList: blocked,43Input: Input{Executable: "./" + blocked[0]},44Expectation: &classifier.Classification{Level: classifier.LevelAudit, Classifier: classifier.ClassifierCommandline, Message: `matched "blocked"`},45},46{47Name: "blocked negative",48BlockList: blocked,49Input: Input{Cmdline: allowed},50Expectation: &classifier.Classification{Level: classifier.LevelNoMatch, Classifier: classifier.ClassifierCommandline},51},52{53Name: "alowed positive",54AllowList: allowed,55BlockList: blocked,56Input: Input{Cmdline: []string{allowed[0], blocked[0]}},57Expectation: &classifier.Classification{Level: classifier.LevelNoMatch, Classifier: classifier.ClassifierCommandline},58},59{60Name: "alowed cmd positive",61AllowList: allowed,62BlockList: blocked,63Input: Input{Executable: allowed[0] + "/" + blocked[0]},64Expectation: &classifier.Classification{Level: classifier.LevelNoMatch, Classifier: classifier.ClassifierCommandline},65},66{67Name: "alowed negative",68AllowList: blocked,69BlockList: blocked,70Input: Input{Cmdline: []string{allowed[0], blocked[0]}},71Expectation: &classifier.Classification{Level: classifier.LevelNoMatch, Classifier: classifier.ClassifierCommandline},72},73}74for _, test := range tests {75t.Run(test.Name, func(t *testing.T) {76class, err := classifier.NewCommandlineClassifier("test", classifier.LevelAudit, test.AllowList, test.BlockList)77if err != nil {78t.Fatal(err)79}8081act, err := class.Matches(test.Input.Executable, test.Input.Cmdline)82if err != nil {83t.Error(err)84return85}8687if diff := cmp.Diff(test.Expectation, act); diff != "" {88t.Errorf("unexpected CommandlineClassifier (-want +got):\n%s", diff)89}90})91}92}939495