codewalk 0.2.2

Walk code trees with binary detection, bounded reads, and scanner-oriented filtering
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Adversarial tests for codewalk - designed to BREAK the code
//!
//! Tests: symlink loops, permission denied dirs, 10K files, non-UTF8 paths,
//! empty dirs nested 100 deep, files changing during walk

use crate::{CodeWalker, WalkConfig};
use std::fs;

#[cfg(unix)]
use std::os::unix::ffi::OsStringExt;
#[cfg(unix)]
use std::os::unix::fs::symlink as unix_symlink;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

#[cfg(windows)]
use std::os::windows::fs::symlink_dir as windows_symlink;

/// Create a symlink loop: A -> B -> C -> A
#[cfg(unix)]
#[test]
fn adversarial_symlink_loop_detection() {
    let dir = tempfile::tempdir().unwrap();
    let a = dir.path().join("a");
    let b = dir.path().join("b");
    let c = dir.path().join("c");

    fs::create_dir(&a).unwrap();
    fs::create_dir(&b).unwrap();
    fs::create_dir(&c).unwrap();

    // Create loop: a/link -> b, b/link -> c, c/link -> a
    unix_symlink(&b, a.join("link")).unwrap();
    unix_symlink(&c, b.join("link")).unwrap();
    unix_symlink(&a, c.join("link")).unwrap();

    let config = WalkConfig {
        follow_symlinks: true,
        ..WalkConfig::default()
    };

    let walker = CodeWalker::new(dir.path(), config);
    // Should not hang or crash on symlink loop
    let results: Vec<_> = walker.walk_iter().collect();
    assert!(
        results.iter().any(Result::is_err),
        "symlink loop should surface an error instead of hanging"
    );
    let entries: Vec<_> = results.into_iter().filter_map(Result::ok).collect();
    assert!(entries.len() <= 100); // Sanity check - shouldn't explode
}

/// Test walking through permission denied directories
#[cfg(unix)]
#[test]
fn adversarial_permission_denied_directory() {
    let dir = tempfile::tempdir().unwrap();

    // Create accessible file
    let accessible = dir.path().join("accessible.txt");
    fs::write(&accessible, "accessible").unwrap();

    // Create directory with no permissions
    let blocked = dir.path().join("blocked");
    fs::create_dir(&blocked).unwrap();
    let blocked_file = blocked.join("secret.txt");
    fs::write(&blocked_file, "secret").unwrap();

    // Remove all permissions
    let mut perms = fs::metadata(&blocked).unwrap().permissions();
    perms.set_mode(0o000);
    fs::set_permissions(&blocked, perms).unwrap();

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());
    let results: Vec<_> = walker.walk_iter().collect();

    // Restore permissions for cleanup
    let mut perms = fs::metadata(&blocked).unwrap().permissions();
    perms.set_mode(0o755);
    fs::set_permissions(&blocked, perms).unwrap();

    let entries: Vec<_> = results
        .iter()
        .filter_map(|result| result.as_ref().ok())
        .collect();
    let paths: Vec<_> = entries.iter().map(|e| e.path.clone()).collect();
    assert!(paths.contains(&accessible));
    assert!(!paths.contains(&blocked_file));
}

/// Test walking with 10,000 files in a single directory
#[test]
fn adversarial_10k_files_in_directory() {
    let dir = tempfile::tempdir().unwrap();
    let files_dir = dir.path().join("files");
    fs::create_dir(&files_dir).unwrap();

    // Create 10,000 files
    for i in 0..10_000 {
        let path = files_dir.join(format!("file_{:05}.txt", i));
        fs::write(&path, format!("content {}", i)).unwrap();
    }

    let walker = CodeWalker::new(&files_dir, WalkConfig::default());
    let entries = walker.walk().unwrap();

    assert_eq!(entries.len(), 10_000);
}

/// Test walking with non-UTF8 paths
#[cfg(unix)]
#[test]
fn adversarial_non_utf8_paths() {
    use std::ffi::OsString;

    let dir = tempfile::tempdir().unwrap();

    // Create file with invalid UTF-8 in name
    let bad_bytes = {
        let mut raw = b"file-\xff\xfe".to_vec();
        raw.extend_from_slice(b".txt");
        OsString::from_vec(raw)
    };
    let bad_path = dir.path().join(&bad_bytes);
    fs::write(&bad_path, "content").unwrap();

    // Create nested directory with bad name
    let bad_dir = dir.path().join(OsString::from_vec(b"dir-\xff".to_vec()));
    fs::create_dir(&bad_dir).unwrap();
    fs::write(bad_dir.join("inside.txt"), "inside").unwrap();

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());
    // Should not panic on non-UTF8 paths
    let entries = walker.walk().unwrap();

    // Should find both files
    assert_eq!(entries.len(), 2);
}

/// Test walking 100 levels of nested empty directories
#[test]
fn adversarial_deeply_nested_empty_dirs() {
    let dir = tempfile::tempdir().unwrap();

    // Create 100 levels of nesting
    let mut current = dir.path().to_path_buf();
    for i in 0..100 {
        current = current.join(format!("level_{:03}", i));
        fs::create_dir(&current).unwrap();
    }

    // Add a file at the deepest level
    fs::write(current.join("deep.txt"), "deep content").unwrap();

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());
    let entries = walker.walk().unwrap();

    assert_eq!(entries.len(), 1);
    assert!(entries[0].path.ends_with("deep.txt"));
}

/// Test walking while files are being modified
#[test]
fn adversarial_files_changing_during_walk() {
    let dir = tempfile::tempdir().unwrap();

    // Create initial files
    for i in 0..100 {
        fs::write(dir.path().join(format!("file_{}.txt", i)), "initial").unwrap();
    }

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());

    // Spawn a thread that modifies files during walk
    let dir_path = dir.path().to_path_buf();
    let modifier = std::thread::spawn(move || {
        for i in 0..100 {
            let _ = fs::write(dir_path.join(format!("file_{}.txt", i)), "modified");
            std::thread::sleep(std::time::Duration::from_millis(1));
        }
    });

    // Walk while modifications are happening
    let entries = walker.walk().unwrap();

    modifier.join().unwrap();

    // Should complete without crashing
    assert_eq!(entries.len(), 100);
}

/// Test walking a directory that gets deleted during walk
#[test]
fn adversarial_directory_deleted_during_walk() {
    let base = tempfile::tempdir().unwrap();
    let victim = base.path().join("victim");
    fs::create_dir(&victim).unwrap();

    // Create files in subdirectories
    for i in 0..50 {
        let sub = victim.join(format!("sub_{}", i));
        fs::create_dir(&sub).unwrap();
        fs::write(sub.join("file.txt"), "content").unwrap();
    }

    let walker = CodeWalker::new(&victim, WalkConfig::default());

    // Walk - this should handle any race conditions gracefully
    let entries = walker.walk().unwrap_or_default();
    assert!(
        entries.len() <= 50,
        "walk should not duplicate entries excessively"
    );
}

/// Test with circular symlink that points to parent
#[cfg(unix)]
#[test]
fn adversarial_circular_symlink_to_parent() {
    let dir = tempfile::tempdir().unwrap();
    let parent = dir.path().join("parent");
    fs::create_dir(&parent).unwrap();

    // Create symlink that points to its own parent
    unix_symlink(&parent, parent.join("loop")).unwrap();

    // Also put a real file there
    fs::write(parent.join("real.txt"), "real").unwrap();

    let config = WalkConfig {
        follow_symlinks: true,
        ..WalkConfig::default()
    };

    let walker = CodeWalker::new(&parent, config);
    let results: Vec<_> = walker.walk_iter().collect();
    let entries: Vec<_> = results.into_iter().filter_map(Result::ok).collect();

    // Should not hang, should find real.txt
    assert!(entries.iter().any(|e| e.path.ends_with("real.txt")));
}

/// Test with extremely long file paths (near OS limit)
#[test]
fn adversarial_very_long_file_paths() {
    let dir = tempfile::tempdir().unwrap();

    // Create a path with a very long filename
    let long_name = "a".repeat(200);
    let long_path = dir.path().join(format!("{}.txt", long_name));
    fs::write(&long_path, "content").unwrap();

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());
    let entries = walker.walk().unwrap();

    assert_eq!(entries.len(), 1);
}

/// Test walking a mix of files and symlinks interleaved
#[cfg(unix)]
#[test]
fn adversarial_mixed_symlinks_and_files() {
    let dir = tempfile::tempdir().unwrap();
    let real_dir = dir.path().join("real");
    fs::create_dir(&real_dir).unwrap();

    // Create real files and symlinks interleaved
    for i in 0..50 {
        let real_file = real_dir.join(format!("real_{}.txt", i));
        fs::write(&real_file, format!("content {}", i)).unwrap();

        // Create symlink to this file
        let link = dir.path().join(format!("link_{}.txt", i));
        unix_symlink(&real_file, &link).unwrap();
    }

    let config = WalkConfig {
        follow_symlinks: true,
        ..WalkConfig::default()
    };

    let walker = CodeWalker::new(dir.path(), config);
    let entries = walker.walk().unwrap();

    // Should find all files (both real and through symlinks)
    assert!(entries.len() >= 50);
}

/// Test with files containing null bytes in content (binary detection)
#[test]
fn adversarial_binary_files_with_nulls() {
    let dir = tempfile::tempdir().unwrap();

    // Create files with varying amounts of null bytes
    for i in 0..10 {
        let mut content = vec![b'A'; 100];
        // Insert enough nulls in the first 16 bytes to trigger binary detection (>30% of 16 bytes = 5 bytes)
        for j in 0..=5 {
            content[j * 2] = 0;
        }
        fs::write(dir.path().join(format!("binary_{}.bin", i)), content).unwrap();
    }

    let config = WalkConfig::default();
    let walker = CodeWalker::new(dir.path(), config);
    let entries = walker.walk().unwrap();

    // Binary files should be skipped with default config
    assert_eq!(entries.len(), 0);
}

/// Test parallel walk with contention
#[test]
fn adversarial_parallel_walk_contention() {
    let dir = tempfile::tempdir().unwrap();

    // Create many files for parallel processing
    for i in 0..1000 {
        fs::write(
            dir.path().join(format!("file_{}.txt", i)),
            format!("content {}", i),
        )
        .unwrap();
    }

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());

    // Start multiple parallel walks simultaneously
    let mut handles = vec![];
    for _ in 0..5 {
        let rx = walker.walk_parallel(4);
        handles.push(std::thread::spawn(move || {
            rx.iter().collect::<Result<Vec<_>, _>>().unwrap().len()
        }));
    }

    let counts: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
    // Each walk should get its own results
    for count in counts {
        assert_eq!(count, 1000);
    }
}

/// Test with maximum file size of 0 (should include all files)
#[test]
fn adversarial_zero_max_file_size() {
    let dir = tempfile::tempdir().unwrap();

    // Create files of various sizes
    fs::write(dir.path().join("small.txt"), "x").unwrap();
    fs::write(dir.path().join("medium.txt"), "x".repeat(1000)).unwrap();
    fs::write(dir.path().join("large.txt"), "x".repeat(1000000)).unwrap();

    let config = WalkConfig {
        max_file_size: 0, // 0 means unlimited
        ..WalkConfig::default()
    };

    let walker = CodeWalker::new(dir.path(), config);
    let entries = walker.walk().unwrap();

    // Should include all files regardless of size
    assert_eq!(entries.len(), 3);
}

/// Test with all extension filters active simultaneously
#[test]
fn adversarial_conflicting_extension_filters() {
    let dir = tempfile::tempdir().unwrap();

    // Create files with different extensions
    fs::write(dir.path().join("a.rs"), "rust").unwrap();
    fs::write(dir.path().join("b.py"), "python").unwrap();
    fs::write(dir.path().join("c.js"), "javascript").unwrap();
    fs::write(dir.path().join("d.txt"), "text").unwrap();

    let config = WalkConfig {
        include_extensions: ["rs".to_string(), "py".to_string()].into_iter().collect(),
        exclude_extensions: ["py".to_string()].into_iter().collect(), // Conflicts with include
        ..WalkConfig::default()
    };

    let walker = CodeWalker::new(dir.path(), config);
    let entries = walker.walk().unwrap();

    // Should find only .rs files (include applies first, then exclude)
    let paths: Vec<_> = entries.iter().map(|e| e.path.clone()).collect();
    assert!(paths.iter().any(|p| p.to_string_lossy().ends_with(".rs")));
    assert!(!paths.iter().any(|p| p.to_string_lossy().ends_with(".py")));
}

/// Regression test for removed mmap config knobs.
#[test]
fn adversarial_small_file_reads_without_mmap_config() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(dir.path().join("test.txt"), "small content").unwrap();

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());
    let entries = walker.walk().unwrap();

    assert_eq!(entries.len(), 1);

    // Try to read content
    let content = entries[0].content();
    assert!(content.is_ok());
}

/// Test with files getting deleted right after being discovered
#[test]
fn adversarial_files_deleted_after_discovery() {
    let dir = tempfile::tempdir().unwrap();

    // Create temporary files
    for i in 0..20 {
        fs::write(
            dir.path().join(format!("temp_{}.txt", i)),
            format!("temp {}", i),
        )
        .unwrap();
    }

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());
    let entries = walker.walk().unwrap();

    // Delete all files
    for i in 0..20 {
        let _ = fs::remove_file(dir.path().join(format!("temp_{}.txt", i)));
    }

    // Try to read content of deleted files
    for entry in entries {
        let error = entry.content().unwrap_err();
        match error {
            crate::error::CodewalkError::Io(io_error) => {
                assert_eq!(io_error.kind(), std::io::ErrorKind::NotFound);
            }
            other => panic!("expected io error, got {other:?}"),
        }
    }
}

/// Test walking with hidden files and special names
#[test]
fn adversarial_hidden_and_special_files() {
    let dir = tempfile::tempdir().unwrap();

    // Create various special files
    fs::write(dir.path().join(".hidden"), "hidden").unwrap();
    fs::write(dir.path().join("..double_dot"), "double").unwrap();
    fs::write(dir.path().join("-dash_start"), "dash").unwrap();
    fs::write(dir.path().join(" space file "), "space").unwrap();
    fs::write(dir.path().join("special!@#$%"), "special").unwrap();

    let config = WalkConfig {
        skip_hidden: false, // Include hidden files
        ..WalkConfig::default()
    };

    let walker = CodeWalker::new(dir.path(), config);
    let entries = walker.walk().unwrap();

    // Should find files (excluding .hidden if skip_hidden is true, but we set false)
    assert!(entries.len() >= 4);
}

/// Test with concurrent modification of root directory
#[test]
fn adversarial_concurrent_directory_modification() {
    let dir = tempfile::tempdir().unwrap();

    let walker = CodeWalker::new(dir.path(), WalkConfig::default());

    // Start modifying directory
    let dir_path = dir.path().to_path_buf();
    let modifier = std::thread::spawn(move || {
        for i in 0..50 {
            let file = dir_path.join(format!("concurrent_{}.txt", i));
            fs::write(&file, "content").unwrap();
            std::thread::sleep(std::time::Duration::from_millis(5));
        }
    });

    // Walk while modifications are happening
    let entries = walker.walk().unwrap_or_default();

    modifier.join().unwrap();
    assert!(
        entries.len() <= 50,
        "concurrent creation should not produce duplicates"
    );
}