-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig_validation_security_test.go
More file actions
330 lines (294 loc) · 10.6 KB
/
Copy pathconfig_validation_security_test.go
File metadata and controls
330 lines (294 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// config_validation_security_test.go
//
// Copyright (c) 2025 AGILira - A. Giordano
// Series: an AGILira fragment
// SPDX-License-Identifier: MPL-2.0
package argus
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestValidationSecurity_MissingCoverage(t *testing.T) {
// Test setRemoteConfigDefaults comprehensive coverage
t.Run("setRemoteConfigDefaults_comprehensive", func(t *testing.T) {
// Test disabled remote config (early return)
t.Run("disabled_remote_config", func(t *testing.T) {
config := Config{
Remote: RemoteConfig{
Enabled: false,
SyncInterval: 123 * time.Second, // Should remain unchanged
Timeout: 456 * time.Second, // Should remain unchanged
},
}
original := config.Remote
config.setRemoteConfigDefaults()
if config.Remote != original {
t.Errorf("Disabled remote config should remain unchanged")
}
})
// Test enabled with zero/negative values (sets defaults)
t.Run("enabled_sets_defaults", func(t *testing.T) {
config := Config{
Remote: RemoteConfig{
Enabled: true,
SyncInterval: 0, // Should be set to default
Timeout: 0, // Should be set to default
MaxRetries: 0, // Should remain 0 (not negative)
RetryDelay: 0, // Should be set to default
},
}
config.setRemoteConfigDefaults()
if config.Remote.SyncInterval <= 0 {
t.Error("SyncInterval should be set to positive default")
}
if config.Remote.Timeout <= 0 {
t.Error("Timeout should be set to positive default")
}
if config.Remote.RetryDelay <= 0 {
t.Error("RetryDelay should be set to positive default")
}
})
// Test negative MaxRetries gets set to default
t.Run("negative_max_retries_fixed", func(t *testing.T) {
config := Config{
Remote: RemoteConfig{
Enabled: true,
MaxRetries: -1, // Should be set to default (2)
},
}
config.setRemoteConfigDefaults()
if config.Remote.MaxRetries != 2 {
t.Errorf("MaxRetries should be 2, got %d", config.Remote.MaxRetries)
}
})
// Test dynamic adjustments (timeout/syncInterval logic)
t.Run("dynamic_adjustments", func(t *testing.T) {
config := Config{
Remote: RemoteConfig{
Enabled: true,
MaxRetries: 10, // Large retries trigger timeout adjustment
RetryDelay: 1 * time.Second,
Timeout: 1 * time.Second, // Too small for retry delay
},
}
originalTimeout := config.Remote.Timeout
config.setRemoteConfigDefaults()
// Timeout should be adjusted to accommodate retries
if config.Remote.Timeout <= originalTimeout {
t.Error("Timeout should be increased to accommodate retry attempts")
}
})
// Test SyncInterval adjustment
t.Run("sync_interval_adjustment", func(t *testing.T) {
config := Config{
Remote: RemoteConfig{
Enabled: true,
SyncInterval: 5 * time.Second, // Small interval
Timeout: 10 * time.Second, // Larger timeout
},
}
config.setRemoteConfigDefaults()
// SyncInterval should be adjusted to be > Timeout
if config.Remote.SyncInterval <= config.Remote.Timeout {
t.Error("SyncInterval should be greater than Timeout to prevent overlap")
}
})
// Test overflow protection for large MaxRetries
t.Run("overflow_protection", func(t *testing.T) {
config := Config{
Remote: RemoteConfig{
Enabled: true,
MaxRetries: 50, // Very large value should be capped
RetryDelay: 1 * time.Second,
},
}
config.setRemoteConfigDefaults()
// Should handle gracefully without overflow
if config.Remote.Timeout <= 0 {
t.Error("Timeout should remain positive even with large MaxRetries")
}
})
})
// Test validateSymlinks comprehensive coverage
t.Run("validateSymlinks_comprehensive", func(t *testing.T) {
// Create test environment
tmpDir, err := os.MkdirTemp("", "argus_symlink_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Logf("Warning: Failed to cleanup temp dir: %v", err)
}
}()
// Create a regular file
regularFile := filepath.Join(tmpDir, "regular.json")
if err := os.WriteFile(regularFile, []byte(`{"test": true}`), 0644); err != nil {
t.Fatalf("Failed to create regular file: %v", err)
}
// Create watcher for testing
auditLogger, _ := NewAuditLogger(AuditConfig{
Enabled: false, // Disabled to avoid file operations during test
})
watcher := &Watcher{
auditLogger: auditLogger,
}
// Test case 1: Regular file (no symlinks)
err = watcher.validateSymlinks(regularFile, "regular.json")
if err != nil {
t.Errorf("validateSymlinks should pass for regular file, got: %v", err)
}
// Test case 2: Nonexistent file (should not error on symlink validation)
nonexistent := filepath.Join(tmpDir, "nonexistent.json")
err = watcher.validateSymlinks(nonexistent, "nonexistent.json")
if err != nil {
t.Errorf("validateSymlinks should not error for nonexistent file, got: %v", err)
}
// Test case 3: Create a valid symlink to regular file
validSymlink := filepath.Join(tmpDir, "valid_symlink.json")
if err := os.Symlink(regularFile, validSymlink); err != nil {
t.Logf("Skipping symlink test, cannot create symlinks: %v", err)
return // Skip symlink tests if we can't create them (e.g., Windows without permissions)
}
err = watcher.validateSymlinks(validSymlink, "valid_symlink.json")
if err != nil {
t.Errorf("validateSymlinks should pass for valid symlink, got: %v", err)
}
// Test case 4: Create symlink to system directory (if possible)
systemSymlink := filepath.Join(tmpDir, "system_symlink")
systemTarget := "/etc" // Common system directory on Unix-like systems
if _, statErr := os.Stat(systemTarget); statErr == nil {
if symlinkErr := os.Symlink(systemTarget, systemSymlink); symlinkErr == nil {
err = watcher.validateSymlinks(systemSymlink, "system_symlink")
// This should trigger the system directory check
if err == nil {
t.Logf("System directory check may not be triggered or system path not considered restricted")
}
}
}
// Test case 5: Broken symlink (points to nonexistent target)
brokenSymlink := filepath.Join(tmpDir, "broken_symlink")
brokenTarget := filepath.Join(tmpDir, "nonexistent_target")
if err := os.Symlink(brokenTarget, brokenSymlink); err == nil {
err = watcher.validateSymlinks(brokenSymlink, "broken_symlink")
// This tests the EvalSymlinks error path
if err != nil {
t.Logf("Broken symlink correctly handled: %v", err)
}
}
})
// Test Validate function with individual error conditions
t.Run("Validate_individual_errors", func(t *testing.T) {
// Test each validation error in isolation with valid base config
baseConfig := Config{
PollInterval: 1 * time.Second, // Valid base
CacheTTL: 500 * time.Millisecond, // Valid base
MaxWatchedFiles: 100, // Valid base
}
t.Run("invalid_cache_ttl", func(t *testing.T) {
config := baseConfig
config.CacheTTL = -1 * time.Second // Invalid
err := config.Validate()
if err == nil || !containsSubstr(err.Error(), "cache TTL must be positive") {
t.Errorf("Expected cache TTL error, got: %v", err)
}
})
t.Run("invalid_max_watched_files", func(t *testing.T) {
config := baseConfig
config.MaxWatchedFiles = -5 // Invalid
err := config.Validate()
if err == nil || !containsSubstr(err.Error(), "max watched files must be positive") {
t.Errorf("Expected max watched files error, got: %v", err)
}
})
t.Run("invalid_optimization_strategy", func(t *testing.T) {
config := baseConfig
config.OptimizationStrategy = 99 // Invalid strategy
err := config.Validate()
if err == nil || !containsSubstr(err.Error(), "unknown optimization strategy") {
t.Errorf("Expected optimization strategy error, got: %v", err)
}
})
t.Run("invalid_boreas_capacity", func(t *testing.T) {
config := baseConfig
config.BoreasLiteCapacity = 100 // Not power of 2
err := config.Validate()
if err == nil || !containsSubstr(err.Error(), "BoreasLite capacity must be power of 2") {
t.Errorf("Expected boreas capacity error, got: %v", err)
}
})
t.Run("invalid_audit_buffer_size", func(t *testing.T) {
config := baseConfig
config.Audit = AuditConfig{
Enabled: true,
BufferSize: -100, // Invalid
OutputFile: "/tmp/test.log", // Valid to avoid output file errors
}
err := config.Validate()
if err == nil || !containsSubstr(err.Error(), "buffer size must be positive") {
t.Errorf("Expected buffer size error, got: %v", err)
}
})
t.Run("invalid_audit_flush_interval", func(t *testing.T) {
config := baseConfig
config.Audit = AuditConfig{
Enabled: true,
FlushInterval: -2 * time.Second, // Invalid
OutputFile: "/tmp/test.log", // Valid to avoid output file errors
}
err := config.Validate()
if err == nil || !containsSubstr(err.Error(), "flush interval must be positive") {
t.Errorf("Expected flush interval error, got: %v", err)
}
})
})
// Test Validate function error precedence
t.Run("Validate_error_precedence", func(t *testing.T) {
// Test that PollInterval error comes first (as seen in real behavior)
t.Run("poll_interval_negative", func(t *testing.T) {
config := Config{
PollInterval: -1 * time.Second, // Invalid
}
err := config.Validate()
if err == nil || !containsSubstr(err.Error(), "poll interval must be positive") {
t.Errorf("Expected poll interval error, got: %v", err)
}
})
t.Run("poll_interval_too_small", func(t *testing.T) {
config := Config{
PollInterval: 5 * time.Millisecond, // Too small
}
err := config.Validate()
if err == nil || !containsSubstr(err.Error(), "poll interval should be at least") {
t.Errorf("Expected poll interval too small error, got: %v", err)
}
})
// Multiple errors should return the first one (poll interval precedence)
t.Run("multiple_errors_poll_interval_first", func(t *testing.T) {
config := Config{
PollInterval: -1 * time.Second, // This should be the error returned
MaxWatchedFiles: -5, // Secondary error
}
err := config.Validate()
if err == nil {
t.Error("Expected validation error for multiple invalid fields")
return
}
// Should return the first error (poll interval)
if !containsSubstr(err.Error(), "poll interval") {
t.Errorf("Expected first error to be about poll interval, got: %v", err)
}
})
})
}
// Helper function to check if string contains substring
func containsSubstr(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}