Skip to content

Commit 6aee17c

Browse files
committed
Improve scan performances and add unit testing
1 parent 0427f79 commit 6aee17c

20 files changed

+1508
-985
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
.idea/
3+
*.iml
4+
.vscode/
5+
.history/

common_utils_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
// TestFileHashFunctions tests hash calculation functions
9+
func TestFileSHA256Sum(t *testing.T) {
10+
// Test with a known file
11+
hash := FileSHA256Sum("go.mod")
12+
13+
if hash == "" {
14+
t.Fatal("FileSHA256Sum returned empty string")
15+
}
16+
17+
// SHA256 hash should be 64 characters (hex)
18+
if len(hash) != 64 {
19+
t.Fatalf("SHA256 hash should be 64 characters, got %d", len(hash))
20+
}
21+
}
22+
23+
// TestContainsHelper tests the Contains utility function
24+
func TestContainsHelper(t *testing.T) {
25+
list := []string{"apple", "banana", "cherry"}
26+
27+
if !Contains(list, "banana") {
28+
t.Fatal("Contains failed to find existing element")
29+
}
30+
31+
if Contains(list, "date") {
32+
t.Fatal("Contains incorrectly reported finding non-existent element")
33+
}
34+
35+
if Contains([]string{}, "apple") {
36+
t.Fatal("Contains should return false for empty list")
37+
}
38+
}
39+
40+
// TestFileCopy creates a temporary test scenario for file operations
41+
func TestFileCopyOperation(t *testing.T) {
42+
// This test verifies that FileCopy function can be called without panic
43+
// Actual file operations are tested with temporary directories
44+
tempSrc := t.TempDir() + "/source.txt"
45+
tempDst := t.TempDir() + "/dest"
46+
47+
// Create source file
48+
err := writeTestFile(tempSrc, "test content")
49+
if err != nil {
50+
t.Fatalf("Failed to create test file: %v", err)
51+
}
52+
53+
// FileCopy should not panic
54+
FileCopy(tempSrc, tempDst, false)
55+
}
56+
57+
// TestGetHostname verifies hostname retrieval
58+
func TestGetHostname(t *testing.T) {
59+
hostname := GetHostname()
60+
61+
if hostname == "" {
62+
t.Fatal("GetHostname returned empty string")
63+
}
64+
}
65+
66+
// TestGetUsername verifies username retrieval
67+
func TestGetUsername(t *testing.T) {
68+
username := GetUsername()
69+
70+
if username == "" {
71+
t.Fatal("GetUsername returned empty string")
72+
}
73+
}
74+
75+
// TestGetCurrentDirectory verifies current directory retrieval
76+
func TestGetCurrentDirectory(t *testing.T) {
77+
dir := GetCurrentDirectory()
78+
79+
if dir == "" {
80+
t.Fatal("GetCurrentDirectory returned empty string")
81+
}
82+
}
83+
84+
// TestRenderFastfinderLogo verifies logo rendering
85+
func TestRenderFastfinderLogo(t *testing.T) {
86+
logo := RenderFastfinderLogo()
87+
88+
if logo == "" {
89+
t.Fatal("RenderFastfinderLogo returned empty string")
90+
}
91+
92+
// Logo should contain the program name
93+
if len(logo) < 10 {
94+
t.Fatal("Logo seems too short")
95+
}
96+
}
97+
98+
// TestRenderFastfinderVersion verifies version info rendering
99+
func TestRenderFastfinderVersion(t *testing.T) {
100+
version := RenderFastfinderVersion()
101+
102+
if version == "" {
103+
t.Fatal("RenderFastfinderVersion returned empty string")
104+
}
105+
106+
// Version should mention the version number
107+
if len(version) < 5 {
108+
t.Fatal("Version string seems too short")
109+
}
110+
}
111+
112+
// Helper function to write test files
113+
func writeTestFile(path string, content string) error {
114+
return os.WriteFile(path, []byte(content), 0644)
115+
}

config_integration_test.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
// TestConfigurationStandard tests loading a standard (non-encrypted) configuration
10+
func TestConfigurationStandard(t *testing.T) {
11+
var config Configuration
12+
config.getConfiguration("tests/config_test_standard.yml")
13+
14+
// Verify basic structure is accessible (paths may be empty in test file)
15+
t.Log("Configuration loaded successfully")
16+
}
17+
18+
// TestConfigurationCiphered tests loading an encrypted (RC4) configuration
19+
func TestConfigurationCiphered(t *testing.T) {
20+
var config Configuration
21+
config.getConfiguration("tests/config_test_ciphered.yml")
22+
23+
// Verify basic structure is accessible (paths may be empty in test file)
24+
t.Log("Ciphered configuration loaded successfully")
25+
}
26+
27+
// TestConfigurationPaths verifies path configuration structure
28+
func TestConfigurationPaths(t *testing.T) {
29+
var config Configuration
30+
config.getConfiguration("tests/config_test_standard.yml")
31+
32+
if len(config.Input.Path) > 0 {
33+
// Verify first path is not empty
34+
if config.Input.Path[0] == "" {
35+
t.Fatal("Path should not be empty")
36+
}
37+
}
38+
}
39+
40+
// TestConfigurationContent verifies content patterns (grep, yara, checksum)
41+
func TestConfigurationContent(t *testing.T) {
42+
var config Configuration
43+
config.getConfiguration("tests/config_test_standard.yml")
44+
45+
if config.Input.Content.Grep != nil {
46+
// Grep patterns should be readable
47+
for _, pattern := range config.Input.Content.Grep {
48+
if pattern == "" {
49+
t.Fatal("Grep pattern should not be empty")
50+
}
51+
}
52+
}
53+
}
54+
55+
// TestConfigurationOptions verifies options configuration
56+
func TestConfigurationOptions(t *testing.T) {
57+
var config Configuration
58+
config.getConfiguration("tests/config_test_standard.yml")
59+
60+
// Options structure should exist (may be all false)
61+
t.Log("Options section loaded successfully")
62+
}
63+
64+
// TestConfigurationOutput verifies output configuration
65+
func TestConfigurationOutput(t *testing.T) {
66+
var config Configuration
67+
config.getConfiguration("tests/config_test_standard.yml")
68+
69+
// Output structure should exist
70+
t.Log("Output section loaded successfully")
71+
}
72+
73+
// TestConfigurationEventForwarding verifies event forwarding configuration
74+
func TestConfigurationEventForwarding(t *testing.T) {
75+
var config Configuration
76+
config.getConfiguration("tests/config_test_standard.yml")
77+
78+
// Event forwarding section should be accessible
79+
t.Log("Event forwarding section loaded successfully")
80+
}
81+
82+
// TestConfigurationAdvancedParameters verifies advanced parameters
83+
func TestConfigurationAdvancedParameters(t *testing.T) {
84+
var config Configuration
85+
config.getConfiguration("tests/config_test_standard.yml")
86+
87+
// Advanced parameters should exist
88+
t.Log("Advanced parameters section loaded successfully")
89+
}
90+
91+
// TestConfigurationMissingRequired tests handling of incomplete config
92+
func TestConfigurationMissingRequired(t *testing.T) {
93+
// Create a minimal temporary config file
94+
tmpFile := filepath.Join(t.TempDir(), "test_config.yml")
95+
tmpContent := `input:
96+
path:
97+
- /tmp
98+
`
99+
os.WriteFile(tmpFile, []byte(tmpContent), 0644)
100+
101+
var config Configuration
102+
config.getConfiguration(tmpFile)
103+
104+
if len(config.Input.Path) == 0 {
105+
t.Fatal("Minimal config should have at least path section")
106+
}
107+
}
108+
109+
// TestConfigurationEmpty tests handling of empty configuration
110+
func TestConfigurationEmpty(t *testing.T) {
111+
tmpFile := filepath.Join(t.TempDir(), "empty_config.yml")
112+
os.WriteFile(tmpFile, []byte(""), 0644)
113+
114+
var config Configuration
115+
config.getConfiguration(tmpFile)
116+
117+
// Verify no panic occurred
118+
t.Log("Empty configuration handled gracefully")
119+
}
120+
121+
// TestConfigurationYARAWithRC4 tests YARA section with RC4 encryption
122+
func TestConfigurationYARAWithRC4(t *testing.T) {
123+
var config Configuration
124+
config.getConfiguration("tests/config_test_ciphered.yml")
125+
126+
// Ciphered config should load without panicking
127+
if config.Input.Content.Yara != nil {
128+
t.Log("YARA rules loaded from ciphered configuration")
129+
}
130+
}
131+
132+
// TestConfigurationChecksums tests checksum patterns
133+
func TestConfigurationChecksums(t *testing.T) {
134+
var config Configuration
135+
config.getConfiguration("tests/config_test_standard.yml")
136+
137+
if len(config.Input.Content.Checksum) > 0 {
138+
// Verify checksums are readable
139+
for _, cs := range config.Input.Content.Checksum {
140+
if cs == "" {
141+
t.Fatal("Checksum should not be empty")
142+
}
143+
}
144+
}
145+
}
146+
147+
// TestConfigurationMultiplePaths tests multiple path handling
148+
func TestConfigurationMultiplePaths(t *testing.T) {
149+
var config Configuration
150+
config.getConfiguration("tests/config_test_standard.yml")
151+
152+
if len(config.Input.Path) > 1 {
153+
t.Logf("Configuration loaded with %d paths", len(config.Input.Path))
154+
}
155+
}

0 commit comments

Comments
 (0)