Path: blob/main/components/ee/agent-smith/pkg/config/config_test.go
2501 views
// Copyright (c) 2024 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 config56import (7"testing"89"github.com/gitpod-io/gitpod/agent-smith/pkg/classifier"10)1112func TestFileClassifierIndependence(t *testing.T) {13// Create a blocklist with both process and filesystem signatures14blocklists := &Blocklists{15Audit: &PerLevelBlocklist{16Binaries: []string{"malware"}, // Process-related17Signatures: []*classifier.Signature{18{19Name: "process-sig",20Domain: classifier.DomainProcess,21Pattern: []byte("process-pattern"),22Filename: []string{"malware.exe"},23},24{25Name: "filesystem-sig",26Domain: classifier.DomainFileSystem,27Pattern: []byte("filesystem-pattern"),28Filename: []string{"virus.exe"},29},30},31},32Very: &PerLevelBlocklist{33Signatures: []*classifier.Signature{34{35Name: "filesystem-sig-2",36Domain: classifier.DomainFileSystem,37Pattern: []byte("another-pattern"),38Filename: []string{"trojan.exe"},39},40},41},42}4344// Test process classifier (existing functionality - should be unchanged)45processClass, err := blocklists.Classifier()46if err != nil {47t.Fatalf("Failed to create process classifier: %v", err)48}49if processClass == nil {50t.Fatal("Process classifier should not be nil")51}5253// Test new filesystem classifier54filesystemClass, err := blocklists.FileClassifier()55if err != nil {56t.Fatalf("Failed to create filesystem classifier: %v", err)57}58if filesystemClass == nil {59t.Fatal("Filesystem classifier should not be nil")60}6162// Verify filesystem classifier has the right signatures63fsSignatures := filesystemClass.GetFileSignatures()64if len(fsSignatures) != 2 {65t.Errorf("Expected 2 filesystem signatures, got %d", len(fsSignatures))66}6768// Verify signatures are filesystem domain only69for _, sig := range fsSignatures {70if sig.Domain != classifier.DomainFileSystem {71t.Errorf("Expected filesystem domain signature, got %s", sig.Domain)72}73}7475// Verify they are completely independent objects (can't directly compare different interface types)76// Instead, verify they have different behaviors77processSignatures := 078if pc, ok := processClass.(*classifier.CountingMetricsClassifier); ok {79// Process classifier is wrapped in CountingMetricsClassifier80_ = pc // Just verify the type cast works81processSignatures = 1 // We know it exists because we created it82}8384filesystemSignatures := len(filesystemClass.GetFileSignatures())85if filesystemSignatures == 0 {86t.Error("Filesystem classifier should have signatures")87}8889// They should serve different purposes90if processSignatures == 0 && filesystemSignatures == 0 {91t.Error("At least one classifier should have content")92}9394// Test filesystem classifier functionality95result, err := filesystemClass.MatchesFile("/nonexistent/virus.exe")96if err != nil {97t.Fatalf("Filesystem classification failed: %v", err)98}99if result == nil {100t.Error("Expected non-nil classification result")101}102}103104func TestFileClassifierEmptyConfig(t *testing.T) {105// Test with nil blocklists106var blocklists *Blocklists107filesystemClass, err := blocklists.FileClassifier()108if err != nil {109t.Fatalf("Failed to create filesystem classifier from nil config: %v", err)110}111if filesystemClass == nil {112t.Fatal("Filesystem classifier should not be nil even with empty config")113}114115// Should have no signatures116signatures := filesystemClass.GetFileSignatures()117if len(signatures) != 0 {118t.Errorf("Expected 0 signatures from empty config, got %d", len(signatures))119}120}121122func TestFileClassifierNoFilesystemSignatures(t *testing.T) {123// Test with blocklists that have no filesystem signatures124blocklists := &Blocklists{125Audit: &PerLevelBlocklist{126Binaries: []string{"malware"},127Signatures: []*classifier.Signature{128{129Name: "process-only",130Domain: classifier.DomainProcess,131Pattern: []byte("process-pattern"),132Filename: []string{"malware.exe"},133},134},135},136}137138filesystemClass, err := blocklists.FileClassifier()139if err != nil {140t.Fatalf("Failed to create filesystem classifier: %v", err)141}142143// Should have no filesystem signatures144signatures := filesystemClass.GetFileSignatures()145if len(signatures) != 0 {146t.Errorf("Expected 0 filesystem signatures, got %d", len(signatures))147}148}149150151