sandlock-cli 0.9.1

CLI for sandlock process sandbox
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
#![cfg(all(feature = "profiles", feature = "cow"))]

// Each test here spawns a full `sandlock` process.
// Running all tests in parallel exhausts kernel resources on most hosts.
// Limit parallelism when running this suite:
//   cargo test -p sandlock-cli --test learn_integration -- --test-threads=4

use std::process::Command;
use std::time::Duration;

fn sandlock_bin() -> Command {
    Command::new(env!("CARGO_BIN_EXE_sandlock"))
}

fn udp_sink(expected_datagrams: usize) -> (u16, std::thread::JoinHandle<usize>) {
    let sock = std::net::UdpSocket::bind("127.0.0.1:0").expect("udp sink bind");
    sock.set_read_timeout(Some(Duration::from_secs(5)))
        .expect("udp sink timeout");
    let port = sock.local_addr().expect("udp sink addr").port();
    let handle = std::thread::spawn(move || {
        let mut buf = [0_u8; 64];
        let mut received = 0;
        while received < expected_datagrams {
            match sock.recv_from(&mut buf) {
                Ok(_) => received += 1,
                Err(_) => break,
            }
        }
        received
    });
    (port, handle)
}

fn learn_then_run_script(profile_path: &str, script: &str) {
    let learn = sandlock_bin()
        .args(["learn", "-o", profile_path, "--", "python3", "-c", script])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            profile_path,
            "--",
            "python3",
            "-c",
            script,
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&run.stderr)
    );
}

/// Learn → run with a read-only workload.
#[test]
fn test_learn_then_run() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();

    let learn = sandlock_bin()
        .args(["learn", "-o", &profile_path, "--", "cat", "/etc/hostname"])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: stderr={}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "cat",
            "/etc/hostname",
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed: stderr={}",
        String::from_utf8_lossy(&run.stderr)
    );
    assert!(
        !String::from_utf8_lossy(&run.stdout).trim().is_empty(),
        "expected output from cat /etc/hostname"
    );
}

/// Connected UDP send: an address-less send confirms the earlier UDP connect
/// as real traffic, so the generated profile must replay.
#[test]
fn test_learn_then_run_connected_udp_send() {
    let (port, sink) = udp_sink(2);
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let script = format!(
        "import socket\n\
         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n\
         s.connect(('127.0.0.1', {port}))\n\
         s.send(b'x')\n\
         s.close()\n"
    );

    learn_then_run_script(&profile_path, &script);
    let profile_toml = std::fs::read_to_string(&profile_path).expect("profile");
    assert!(
        profile_toml.contains(&format!("udp://127.0.0.1:{port}")),
        "expected connected UDP endpoint in profile:\n{profile_toml}"
    );
    assert_eq!(
        sink.join().unwrap(),
        2,
        "learn and run should each send one UDP datagram"
    );
}

/// A connect observed on one thread and a send observed on a sibling thread
/// still refer to the same process fd table.
#[test]
fn test_learn_then_run_connected_udp_send_from_thread() {
    let (port, sink) = udp_sink(2);
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let script = format!(
        "import socket, threading\n\
         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n\
         s.connect(('127.0.0.1', {port}))\n\
         t = threading.Thread(target=lambda: s.send(b'x'))\n\
         t.start()\n\
         t.join()\n\
         s.close()\n"
    );

    learn_then_run_script(&profile_path, &script);
    let profile_toml = std::fs::read_to_string(&profile_path).expect("profile");
    assert!(
        profile_toml.contains(&format!("udp://127.0.0.1:{port}")),
        "expected connected UDP endpoint in profile:\n{profile_toml}"
    );
    assert_eq!(
        sink.join().unwrap(),
        2,
        "learn and run should each send one UDP datagram"
    );
}

/// Write path: COW isolates during learn; run creates the file for real.
#[test]
fn test_learn_then_run_write() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let write_dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
    let write_path = write_dir.path().join("run-write-test.txt");
    let write_path_str = write_path.to_str().unwrap();

    let learn = sandlock_bin()
        .args([
            "learn",
            "-o",
            &profile_path,
            "--",
            "sh",
            "-c",
            &format!("echo hello > {write_path_str}"),
        ])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );
    assert!(!write_path.exists(), "COW isolation failed during learn");

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "sh",
            "-c",
            &format!("echo hello > {write_path_str}"),
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&run.stderr)
    );
    assert_eq!(
        std::fs::read_to_string(&write_path)
            .unwrap_or_default()
            .trim(),
        "hello",
        "file not written during run"
    );
    let _ = std::fs::remove_file(&write_path);
}

/// Write then read in the same run: both grants coexist without Landlock conflicts.
#[test]
fn test_learn_then_run_write_and_read() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let write_dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
    let file = write_dir.path().join("rw-test.txt");
    let file_str = file.to_str().unwrap();
    let script = format!("echo hello > {file_str} && cat {file_str}");

    let learn = sandlock_bin()
        .args(["learn", "-o", &profile_path, "--", "sh", "-c", &script])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "sh",
            "-c",
            &script,
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&run.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&run.stdout).trim(),
        "hello",
        "expected workload output"
    );
    let _ = std::fs::remove_file(&file);
}

/// TCP connect: learned endpoint is allowed during run.
#[test]
fn test_learn_then_run_network() {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    std::thread::spawn(move || {
        let _ = listener.accept();
        let _ = listener.accept();
    });

    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let script =
        format!("import socket; s=socket.socket(); s.connect(('127.0.0.1',{port})); s.close()");

    let learn = sandlock_bin()
        .args(["learn", "-o", &profile_path, "--", "python3", "-c", &script])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "python3",
            "-c",
            &script,
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&run.stderr)
    );
}

/// Bind: learned allow_bind port is permitted during run.
#[test]
fn test_learn_then_run_bind() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let script = concat!(
        "import socket\n",
        "s = socket.socket()\n",
        "s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n",
        "s.bind(('127.0.0.1', 19878))\n",
        "s.close()\n",
    );

    let learn = sandlock_bin()
        .args(["learn", "-o", &profile_path, "--", "python3", "-c", script])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "python3",
            "-c",
            script,
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&run.stderr)
    );
}

/// AF_UNIX bind: socket parent observed during learn, bind succeeds during run.
#[test]
fn test_learn_then_run_unix_bind() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
    let sock = dir.path().join("rndtrip.sock");
    let sock_str = sock.to_str().unwrap();
    let script = format!(
        "import socket; s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM); \
         s.bind('{sock_str}'); s.close()"
    );

    let learn = sandlock_bin()
        .args(["learn", "-o", &profile_path, "--", "python3", "-c", &script])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    // bind() during learn is executed on-behalf by the supervisor (outside the
    // COW overlay), so the socket file exists on the real FS after learn.
    // Remove it so run can bind to the same path without EADDRINUSE.
    let _ = std::fs::remove_file(&sock);

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "python3",
            "-c",
            &script,
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed (unix bind parent not in writes?): {}",
        String::from_utf8_lossy(&run.stderr)
    );
}

/// Collapsed profile: directory grant covers files not individually observed during learn.
///
/// Uses a private tempdir rather than /usr/bin: learn canonicalizes observed
/// paths, and on hosts where coreutils are symlinks into a multicall link farm
/// (e.g. Ubuntu rust-coreutils on riscv64) the /usr/bin names resolve to
/// parents that never reach the collapse threshold (issue #170).
#[test]
fn test_learn_then_run_collapse() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
    let dir_path = dir.path().canonicalize().expect("canonicalize tempdir");

    // Four observed files meet the pinned collapse threshold.
    let observed: Vec<String> = (1..=4)
        .map(|i| {
            let p = dir_path.join(format!("observed{i}.txt"));
            std::fs::write(&p, "collapse me\n").expect("write observed file");
            p.to_str().unwrap().to_owned()
        })
        .collect();
    let extra = dir_path.join("unobserved.txt");
    std::fs::write(&extra, "covered by directory grant\n").expect("write unobserved file");

    let mut learn_cmd = sandlock_bin();
    learn_cmd.args(["learn", "--collapse=4", "-o", &profile_path, "--", "cat"]);
    learn_cmd.args(&observed);
    let learn = learn_cmd.output().expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    // Run reads a file that was not individually observed; the collapsed
    // directory grant covers it.
    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "cat",
            extra.to_str().unwrap(),
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed with collapsed profile: {}",
        String::from_utf8_lossy(&run.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&run.stdout).trim(),
        "covered by directory grant",
        "unexpected output from unobserved file"
    );
}

/// Learned memory limit reflects actual anonymous allocation, not just the floor.
#[test]
fn test_learn_then_run_memory_limit() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();

    // Allocate 20 MiB, learned limit must exceed the 16M floor.
    let script = "import time; x = bytearray(20 * 1024 * 1024); time.sleep(0.05)";
    let learn = sandlock_bin()
        .args(["learn", "-o", &profile_path, "--", "python3", "-c", script])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let profile_toml = std::fs::read_to_string(&profile_path).expect("read profile");
    let mem_line = profile_toml
        .lines()
        .find(|l| l.starts_with("memory"))
        .unwrap();
    let mib: u64 = mem_line
        .trim_end_matches('"')
        .split('"')
        .nth(1)
        .and_then(|s| s.trim_end_matches('M').parse().ok())
        .unwrap_or(0);
    assert!(
        mib >= 25,
        "expected memory >= 25M (20M + 25% headroom), got {mib}M"
    );

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "python3",
            "-c",
            script,
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&run.stderr)
    );
}

/// Learned process limit reflects observed child processes.
#[test]
fn test_learn_then_run_process_limit() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();

    // sh forks cat as a child, observed peak is 2 processes.
    let learn = sandlock_bin()
        .args([
            "learn",
            "-o",
            &profile_path,
            "--",
            "sh",
            "-c",
            "cat /etc/hostname & wait",
        ])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let profile_toml = std::fs::read_to_string(&profile_path).expect("read profile");
    let proc_line = profile_toml
        .lines()
        .find(|l| l.starts_with("processes"))
        .unwrap();
    let procs: u32 = proc_line
        .split('=')
        .nth(1)
        .and_then(|s| s.trim().parse().ok())
        .unwrap_or(0);
    assert!(
        procs >= 4,
        "expected processes >= 4 (2 observed * 2), got {procs}"
    );

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "sh",
            "-c",
            "cat /etc/hostname & wait",
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&run.stderr)
    );
}

/// mknod: FIFO created during learn round-trips to run.
#[test]
fn test_learn_then_run_mknod() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let dir = tempfile::TempDir::new_in("/var/tmp").expect("tempdir in /var/tmp");
    let fifo = dir.path().join("rndtrip.fifo");
    let fifo_str = fifo.to_str().unwrap();
    let script = format!("import os; os.mkfifo('{fifo_str}')");

    let learn = sandlock_bin()
        .args(["learn", "-o", &profile_path, "--", "python3", "-c", &script])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );
    assert!(!fifo.exists(), "COW isolation failed during learn");

    let run = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "python3",
            "-c",
            &script,
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run.status.success(),
        "run failed (mknod not in profile writes?): {}",
        String::from_utf8_lossy(&run.stderr)
    );
    assert!(fifo.exists(), "FIFO not created during run");
    let _ = std::fs::remove_file(&fifo);
}

/// Merged profile covers paths from both learn runs.
#[test]
fn test_learn_then_run_merge() {
    // Two separate workdirs so paths don't collapse into a shared ancestor.
    let dir_a = tempfile::TempDir::new_in("/var/tmp").expect("tempdir a");
    let dir_b = tempfile::TempDir::new_in("/var/tmp").expect("tempdir b");
    let file_a = dir_a.path().join("input.txt");
    let file_b = dir_b.path().join("input.txt");
    let secret = dir_a.path().join("secret.txt");
    std::fs::write(&file_a, "data-a").expect("write file_a");
    std::fs::write(&file_b, "data-b").expect("write file_b");
    std::fs::write(&secret, "sensitive").expect("write secret");

    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();

    // Phase 1: learn access to dir_a/input.txt.
    let learn1 = sandlock_bin()
        .args([
            "learn",
            "-o",
            &profile_path,
            "--",
            "cat",
            file_a.to_str().unwrap(),
        ])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn1.status.success(),
        "learn1 failed: {}",
        String::from_utf8_lossy(&learn1.stderr)
    );

    // Operator hardens the profile: deny the secret file in dir_a.
    // Insert deny into the existing [filesystem] section.
    let existing = std::fs::read_to_string(&profile_path).expect("read profile");
    let deny_line = format!("deny = [\"{}\"]", secret.display());
    let hardened = existing.replace("[filesystem]", &format!("[filesystem]\n{deny_line}"));
    std::fs::write(&profile_path, &hardened).expect("write hardened profile");

    // Phase 2: merge, learn access to dir_b/input.txt.
    let learn2 = sandlock_bin()
        .args([
            "learn",
            "--merge",
            &profile_path,
            "--",
            "cat",
            file_b.to_str().unwrap(),
        ])
        .output()
        .expect("failed to run sandlock learn --merge");
    assert!(
        learn2.status.success(),
        "learn2 failed: {}",
        String::from_utf8_lossy(&learn2.stderr)
    );

    // Both files are readable, paths from both learn runs are in the profile.
    let run_ok = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "cat",
            file_a.to_str().unwrap(),
            file_b.to_str().unwrap(),
        ])
        .output()
        .expect("failed to run sandlock run");
    assert!(
        run_ok.status.success(),
        "run with merged profile failed: {}",
        String::from_utf8_lossy(&run_ok.stderr)
    );

    // The deny rule survived the merge: the secret is blocked.
    let run_deny = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "cat",
            secret.to_str().unwrap(),
        ])
        .output()
        .expect("failed to run sandlock run (deny check)");
    assert!(
        !run_deny.status.success(),
        "secret should be blocked by deny rule after merge"
    );
}

// ── HTTP learning ─────────────────────────────────────────────────────────────

fn spawn_http_server() -> u16 {
    use std::io::{Read, Write};
    use std::net::TcpListener;
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind test http server");
    let port = listener.local_addr().unwrap().port();
    std::thread::spawn(move || {
        for stream in listener.incoming() {
            let mut stream = match stream {
                Ok(s) => s,
                Err(_) => break,
            };
            let mut buf = [0u8; 4096];
            let _ = stream.read(&mut buf);
            let _ = stream
                .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok");
        }
    });
    port
}

/// Learn HTTP traffic then run with the learned profile:
/// - the learned path is allowed and returns the real response
/// - a different path that was not observed is blocked (403 from proxy)
#[cfg(feature = "http")]
#[test]
fn test_learn_then_run_http() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let port = spawn_http_server();
    let url_learned = format!("http://127.0.0.1:{port}/data");
    let url_other = format!("http://127.0.0.1:{port}/other");
    let port_str = port.to_string();

    // Learn only GET /data.
    let learn = sandlock_bin()
        .args([
            "learn",
            "-o",
            &profile_path,
            "--http-port",
            &port_str,
            "--",
            "curl",
            "-sf",
            &url_learned,
        ])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let profile_content = std::fs::read_to_string(&profile_path).expect("read profile");
    assert!(
        profile_content.contains("[http]"),
        "profile must have [http] section: {profile_content}"
    );
    assert!(
        profile_content.contains("/data"),
        "profile must record /data: {profile_content}"
    );

    // Allowed: the learned path returns the real server response.
    let run_allow = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "curl",
            "-s",
            &url_learned,
        ])
        .output()
        .expect("failed to run sandlock run (allow)");
    assert!(
        run_allow.status.success(),
        "run failed for learned path: {}",
        String::from_utf8_lossy(&run_allow.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&run_allow.stdout).trim(),
        "ok",
        "expected 'ok' from test server for learned path"
    );

    // Blocked: a path not in the learned allow list gets 403 from the proxy.
    // curl -f exits non-zero on HTTP 4xx/5xx.
    let run_deny = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "curl",
            "-sf",
            &url_other,
        ])
        .output()
        .expect("failed to run sandlock run (deny)");
    assert!(
        !run_deny.status.success(),
        "run should have been blocked for non-learned path /other"
    );
}

fn system_ca_bundle() -> Option<&'static str> {
    for path in &[
        "/etc/ssl/certs/ca-certificates.crt",
        "/etc/pki/tls/certs/ca-bundle.crt",
        "/etc/ssl/ca-bundle.pem",
        "/etc/ca-certificates/extracted/tls-ca-bundle.pem",
    ] {
        if std::path::Path::new(path).exists() {
            return Some(path);
        }
    }
    None
}

/// Returns true if host:443 accepts a TCP connection within 3 seconds.
fn https_reachable(host: &str) -> bool {
    use std::net::ToSocketAddrs;
    let Ok(mut it) = format!("{host}:443").to_socket_addrs() else {
        return false;
    };
    let Some(addr) = it.next() else { return false };
    std::net::TcpStream::connect_timeout(&addr, std::time::Duration::from_secs(3)).is_ok()
}

/// HTTPS learn -> run round-trip against a real HTTPS server (example.com).
/// Exercises TLS termination, MITM cert signing, CA injection, and ACL enforcement.
/// - the learned HTTPS path is allowed
/// - an unlearned path is blocked (403 from proxy)
/// Skipped if example.com is unreachable or no system CA bundle is found.
#[cfg(feature = "http")]
#[test]
fn test_learn_then_run_https() {
    if !https_reachable("example.com") {
        eprintln!("skipping test_learn_then_run_https: example.com unreachable");
        return;
    }
    let Some(ca_bundle) = system_ca_bundle() else {
        eprintln!("skipping test_learn_then_run_https: no system CA bundle found");
        return;
    };

    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();

    let learn = sandlock_bin()
        .args([
            "learn",
            "-o",
            &profile_path,
            "--http-inject-ca",
            ca_bundle,
            "--",
            "curl",
            "-sf",
            "https://example.com/",
        ])
        .output()
        .expect("failed to run sandlock learn");
    assert!(
        learn.status.success(),
        "learn failed: {}",
        String::from_utf8_lossy(&learn.stderr)
    );

    let profile_content = std::fs::read_to_string(&profile_path).expect("read profile");
    assert!(
        profile_content.contains("http_inject_ca"),
        "profile must record http_inject_ca in [config]: {profile_content}"
    );
    assert!(
        profile_content.contains("example.com"),
        "profile must record example.com in [http].allow: {profile_content}"
    );

    // Allowed: sandlock run picks up http_inject_ca from [config], no extra flags.
    let run_allow = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "curl",
            "-sf",
            "https://example.com/",
        ])
        .output()
        .expect("failed to run sandlock run (allow)");
    assert!(
        run_allow.status.success(),
        "run failed for learned path: {}",
        String::from_utf8_lossy(&run_allow.stderr)
    );

    // Blocked: unlearned path returns 403 from proxy (curl -f exits non-zero on 4xx).
    let run_deny = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "curl",
            "-sf",
            "https://example.com/not-learned-path",
        ])
        .output()
        .expect("failed to run sandlock run (deny)");
    assert!(
        !run_deny.status.success(),
        "run should have been blocked for non-learned path /not-learned-path"
    );
}

/// --merge carries [http].allow and [http].ports from the observed run into the
/// existing profile.
#[cfg(feature = "http")]
#[test]
fn test_learn_then_run_merge_http() {
    let profile = tempfile::NamedTempFile::new().expect("tempfile");
    let profile_path = profile.path().to_str().unwrap().to_owned();
    let port = spawn_http_server();
    let url_a = format!("http://127.0.0.1:{port}/path_a");
    let url_b = format!("http://127.0.0.1:{port}/path_b");
    let url_other = format!("http://127.0.0.1:{port}/other");
    let port_str = port.to_string();

    // Run 1: learn /path_a.
    let learn1 = sandlock_bin()
        .args([
            "learn",
            "-o",
            &profile_path,
            "--http-port",
            &port_str,
            "--",
            "curl",
            "-sf",
            &url_a,
        ])
        .output()
        .expect("failed to run sandlock learn (run 1)");
    assert!(
        learn1.status.success(),
        "learn run 1 failed: {}",
        String::from_utf8_lossy(&learn1.stderr)
    );

    // Run 2: merge, learn /path_b.
    let learn2 = sandlock_bin()
        .args([
            "learn",
            "--merge",
            &profile_path,
            "--http-port",
            &port_str,
            "--",
            "curl",
            "-sf",
            &url_b,
        ])
        .output()
        .expect("failed to run sandlock learn --merge (run 2)");
    assert!(
        learn2.status.success(),
        "learn run 2 (merge) failed: {}",
        String::from_utf8_lossy(&learn2.stderr)
    );

    let merged = std::fs::read_to_string(&profile_path).expect("read merged profile");
    assert!(
        merged.contains("/path_a"),
        "merged profile must contain /path_a: {merged}"
    );
    assert!(
        merged.contains("/path_b"),
        "merged profile must contain /path_b: {merged}"
    );

    // Both learned paths are allowed.
    let run_a = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "curl",
            "-s",
            &url_a,
        ])
        .output()
        .expect("failed to run sandlock run (path_a)");
    assert!(
        run_a.status.success(),
        "run failed for /path_a: {}",
        String::from_utf8_lossy(&run_a.stderr)
    );

    let run_b = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "curl",
            "-s",
            &url_b,
        ])
        .output()
        .expect("failed to run sandlock run (path_b)");
    assert!(
        run_b.status.success(),
        "run failed for /path_b: {}",
        String::from_utf8_lossy(&run_b.stderr)
    );

    // Unlearned path is blocked.
    let run_deny = sandlock_bin()
        .args([
            "run",
            "--profile-file",
            &profile_path,
            "--",
            "curl",
            "-sf",
            &url_other,
        ])
        .output()
        .expect("failed to run sandlock run (deny)");
    assert!(
        !run_deny.status.success(),
        "run should have been blocked for non-learned path /other"
    );
}