asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
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
//! [br-conformance-17] FS Protocol Conformance Tests
//!
//! Conformance harness covering filesystem protocol requirements:
//! - io_uring readv/writev consistency with vectored operations
//! - VFS canonicalization with symlink resolution and path normalization
//! - buf_reader/buf_writer boundary semantics with read/write buffering
//!
//! Uses Pattern 3 (Round-Trip) and Pattern 4 (Spec-Derived Test Matrix).

#![allow(clippy::missing_panics_doc)]
#![allow(clippy::missing_errors_doc)]

#[cfg(any(test, feature = "test-internals"))]
use std::collections::HashMap;
#[cfg(any(test, feature = "test-internals"))]
use std::io::{IoSlice, IoSliceMut};
#[cfg(any(test, feature = "test-internals"))]
use std::path::{Path, PathBuf};

/// Mock io_uring readv/writev processor for conformance testing
#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug, Clone)]
pub struct MockUringProcessor {
    read_operations: Vec<MockUringRead>,
    write_operations: Vec<MockUringWrite>,
    consistency_mode: UringConsistencyMode,
}

#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug, Clone)]
pub struct MockUringRead {
    pub fd: i32,
    pub buffers: Vec<Vec<u8>>,
    pub offset: u64,
    pub bytes_read: usize,
}

#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug, Clone)]
pub struct MockUringWrite {
    pub fd: i32,
    pub buffers: Vec<Vec<u8>>,
    pub offset: u64,
    pub bytes_written: usize,
}

#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UringConsistencyMode {
    Strict,     // Exact vectored I/O semantics
    Permissive, // Allow reordering within transaction
}

#[cfg(any(test, feature = "test-internals"))]
impl MockUringProcessor {
    pub fn new(mode: UringConsistencyMode) -> Self {
        Self {
            read_operations: Vec::new(),
            write_operations: Vec::new(),
            consistency_mode: mode,
        }
    }

    /// Process vectored read operation - must maintain consistency between buffers
    pub fn readv(
        &mut self,
        fd: i32,
        bufs: &mut [IoSliceMut<'_>],
        offset: u64,
    ) -> Result<usize, std::io::Error> {
        let mut total_read = 0;
        let mut buffer_contents = Vec::new();

        for buf in bufs.iter_mut() {
            let data = vec![0x42; buf.len()]; // Mock data
            let read_len = std::cmp::min(data.len(), buf.len());
            buf[..read_len].copy_from_slice(&data[..read_len]);
            buffer_contents.push(data);
            total_read += read_len;
        }

        self.read_operations.push(MockUringRead {
            fd,
            buffers: buffer_contents,
            offset,
            bytes_read: total_read,
        });

        Ok(total_read)
    }

    /// Process vectored write operation - must maintain ordering and atomicity
    pub fn writev(
        &mut self,
        fd: i32,
        bufs: &[IoSlice<'_>],
        offset: u64,
    ) -> Result<usize, std::io::Error> {
        let mut total_written = 0;
        let mut buffer_contents = Vec::new();

        for buf in bufs {
            buffer_contents.push(buf.to_vec());
            total_written += buf.len();
        }

        self.write_operations.push(MockUringWrite {
            fd,
            buffers: buffer_contents,
            offset,
            bytes_written: total_written,
        });

        if self.consistency_mode == UringConsistencyMode::Strict {
            // Verify atomicity: either all buffers written or none
            if total_written % bufs.len() != 0 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::Interrupted,
                    "Partial vectored write violates atomicity",
                ));
            }
        }

        Ok(total_written)
    }

    /// Validate readv/writev consistency invariants
    pub fn validate_consistency(&self) -> Result<(), String> {
        // Invariant 1: Read/write ordering must be preserved
        let mut current_offset = 0u64;
        for read_op in &self.read_operations {
            if read_op.offset < current_offset {
                return Err(format!(
                    "Read ordering violation: offset {} < current {}",
                    read_op.offset, current_offset
                ));
            }
            current_offset = read_op.offset + read_op.bytes_read as u64;
        }

        // Invariant 2: Vectored operations must be atomic
        for write_op in &self.write_operations {
            if write_op.buffers.is_empty() {
                return Err("Empty buffer vector in write operation".to_string());
            }
        }

        Ok(())
    }
}

/// Mock VFS canonicalization processor for path normalization testing
#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug, Clone)]
pub struct MockVfsCanonicalizer {
    symlinks: HashMap<PathBuf, PathBuf>,
    mount_points: HashMap<PathBuf, PathBuf>,
    canonicalization_cache: HashMap<PathBuf, PathBuf>,
}

#[cfg(any(test, feature = "test-internals"))]
impl MockVfsCanonicalizer {
    pub fn new() -> Self {
        let mut symlinks = HashMap::new();
        let mut mount_points = HashMap::new();

        // Setup mock filesystem structure
        symlinks.insert(PathBuf::from("/link/to/file"), PathBuf::from("/real/file"));
        symlinks.insert(
            PathBuf::from("/recursive/link"),
            PathBuf::from("/another/link"),
        );
        symlinks.insert(
            PathBuf::from("/another/link"),
            PathBuf::from("/final/target"),
        );

        mount_points.insert(PathBuf::from("/mnt"), PathBuf::from("/dev/sda1"));
        mount_points.insert(PathBuf::from("/proc"), PathBuf::from("procfs"));

        Self {
            symlinks,
            mount_points,
            canonicalization_cache: HashMap::new(),
        }
    }

    /// Canonicalize path with symlink resolution and normalization
    pub fn canonicalize<P: AsRef<Path>>(&mut self, path: P) -> Result<PathBuf, std::io::Error> {
        let path = path.as_ref().to_path_buf();

        // Check cache first
        if let Some(canonical) = self.canonicalization_cache.get(&path) {
            return Ok(canonical.clone());
        }

        let mut canonical = self.normalize_components(&path)?;

        // Resolve symlinks (with cycle detection)
        let mut visited = std::collections::HashSet::new();
        while let Some(target) = self.symlinks.get(&canonical) {
            if !visited.insert(canonical.clone()) {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "Symlink cycle detected",
                ));
            }
            canonical = self.normalize_components(target)?;
        }

        // Cache result
        self.canonicalization_cache
            .insert(path.clone(), canonical.clone());
        Ok(canonical)
    }

    /// Return the backing mount source for a canonical path, if it is under a known mount point.
    pub fn mount_source_for_path(&self, path: &Path) -> Option<&Path> {
        self.mount_points
            .iter()
            .filter(|(mount_point, _)| path.starts_with(mount_point.as_path()))
            .max_by_key(|(mount_point, _)| mount_point.components().count())
            .map(|(_, source)| source.as_path())
    }

    /// Normalize path components (remove .., ., duplicate separators)
    fn normalize_components(&self, path: &Path) -> Result<PathBuf, std::io::Error> {
        let mut components = Vec::new();

        for component in path.components() {
            match component {
                std::path::Component::CurDir => continue,
                std::path::Component::ParentDir => {
                    if components.is_empty() {
                        return Err(std::io::Error::new(
                            std::io::ErrorKind::InvalidInput,
                            "Cannot resolve .. beyond root",
                        ));
                    }
                    components.pop();
                }
                _ => components.push(component),
            }
        }

        Ok(components.iter().collect())
    }

    /// Add symlink for testing
    pub fn add_symlink<P: Into<PathBuf>, Q: Into<PathBuf>>(&mut self, link: P, target: Q) {
        self.symlinks.insert(link.into(), target.into());
        self.canonicalization_cache.clear(); // Invalidate cache
    }

    /// Validate canonicalization invariants
    pub fn validate_canonicalization(
        &self,
        _original: &Path,
        canonical: &Path,
    ) -> Result<(), String> {
        // Invariant 1: Canonical paths are absolute
        if !canonical.is_absolute() {
            return Err(format!(
                "Canonical path must be absolute: {}",
                canonical.display()
            ));
        }

        // Invariant 2: No . or .. components in canonical path
        for component in canonical.components() {
            match component {
                std::path::Component::CurDir | std::path::Component::ParentDir => {
                    return Err(format!(
                        "Canonical path contains . or ..: {}",
                        canonical.display()
                    ));
                }
                _ => continue,
            }
        }

        // Invariant 3: Idempotency - canonicalizing a canonical path returns itself
        // (This would require mutable access, so we document the requirement)

        // Invariant 4: Known mount points retain nonempty backing metadata.
        if let Some(mount_source) = self.mount_source_for_path(canonical) {
            if mount_source.as_os_str().is_empty() {
                return Err(format!(
                    "Mount point for {} has empty backing source",
                    canonical.display()
                ));
            }
        }

        Ok(())
    }
}

/// Mock buffered reader/writer for boundary semantics testing
#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug)]
pub struct MockBufProcessor {
    read_buffer: Vec<u8>,
    write_buffer: Vec<u8>,
    buffer_size: usize,
    read_pos: usize,
    write_pos: usize,
    flush_count: usize,
}

#[cfg(any(test, feature = "test-internals"))]
impl MockBufProcessor {
    pub fn new(buffer_size: usize) -> Self {
        Self {
            read_buffer: vec![0; buffer_size],
            write_buffer: Vec::with_capacity(buffer_size),
            buffer_size,
            read_pos: 0,
            write_pos: 0,
            flush_count: 0,
        }
    }

    /// Fill read buffer (simulates underlying read)
    pub fn fill_read_buffer(&mut self, data: &[u8]) {
        self.read_buffer.clear();
        self.read_buffer.extend_from_slice(data);
        self.read_pos = 0;
    }

    /// Read from buffer with boundary handling
    pub fn buf_read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
        let available = self.read_buffer.len().saturating_sub(self.read_pos);
        let to_read = std::cmp::min(buf.len(), available);

        if to_read == 0 {
            return Ok(0); // EOF
        }

        buf[..to_read].copy_from_slice(&self.read_buffer[self.read_pos..self.read_pos + to_read]);
        self.read_pos += to_read;
        Ok(to_read)
    }

    /// Write to buffer with boundary handling and automatic flush
    pub fn buf_write(&mut self, data: &[u8]) -> Result<usize, std::io::Error> {
        let mut written = 0;

        for &byte in data {
            if self.write_buffer.len() >= self.buffer_size {
                self.flush()?;
            }
            self.write_buffer.push(byte);
            written += 1;
        }

        Ok(written)
    }

    /// Flush write buffer
    pub fn flush(&mut self) -> Result<(), std::io::Error> {
        self.write_pos += self.write_buffer.len();
        self.write_buffer.clear();
        self.flush_count += 1;
        Ok(())
    }

    /// Validate buffer boundary semantics
    pub fn validate_boundary_semantics(&self) -> Result<(), String> {
        // Invariant 1: Read position never exceeds buffer size
        if self.read_pos > self.read_buffer.len() {
            return Err(format!(
                "Read position {} exceeds buffer length {}",
                self.read_pos,
                self.read_buffer.len()
            ));
        }

        // Invariant 2: Write buffer never exceeds configured size (except during write)
        if self.write_buffer.len() > self.buffer_size && self.write_buffer.len() > 1 {
            return Err(format!(
                "Write buffer size {} exceeds limit {}",
                self.write_buffer.len(),
                self.buffer_size
            ));
        }

        // Invariant 3: Flush count indicates actual write operations
        if self.flush_count == 0 && self.write_pos > 0 {
            return Err("Write position advanced without flush".to_string());
        }

        Ok(())
    }

    pub fn bytes_written(&self) -> usize {
        self.write_pos
    }

    pub fn pending_bytes(&self) -> usize {
        self.write_buffer.len()
    }
}

/// Conformance test harness for fs protocols
#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug)]
pub struct FsProtocolConformanceHarness {
    uring_processor: MockUringProcessor,
    vfs_canonicalizer: MockVfsCanonicalizer,
    buf_processor: MockBufProcessor,
    test_results: Vec<ConformanceTestResult>,
}

#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug, Clone)]
pub struct ConformanceTestResult {
    pub test_name: String,
    pub requirement_level: RequirementLevel,
    pub status: TestStatus,
    pub error_message: Option<String>,
}

#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequirementLevel {
    Must,   // POSIX/Linux mandated behavior
    Should, // Recommended practice
    May,    // Optional optimization
}

#[cfg(any(test, feature = "test-internals"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TestStatus {
    Pass,
    Fail,
    Skip,
}

#[cfg(any(test, feature = "test-internals"))]
impl FsProtocolConformanceHarness {
    pub fn new() -> Self {
        Self {
            uring_processor: MockUringProcessor::new(UringConsistencyMode::Strict),
            vfs_canonicalizer: MockVfsCanonicalizer::new(),
            buf_processor: MockBufProcessor::new(8192), // 8KB buffer
            test_results: Vec::new(),
        }
    }

    pub fn run_all_tests(&mut self) -> Result<(), String> {
        self.test_uring_readv_writev_consistency()?;
        self.test_vfs_canonicalization_invariants()?;
        self.test_buf_reader_writer_boundaries()?;

        self.generate_compliance_report()
    }

    fn test_uring_readv_writev_consistency(&mut self) -> Result<(), String> {
        // Test 1: Vectored read consistency
        let mut buf1 = [0u8; 1024];
        let mut buf2 = [0u8; 2048];
        let mut buffers = [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)];
        match self.uring_processor.readv(1, &mut buffers, 0) {
            Ok(bytes_read) => {
                self.record_test(
                    "uring_readv_basic",
                    RequirementLevel::Must,
                    TestStatus::Pass,
                    None,
                );
                if bytes_read != 3072 {
                    self.record_test(
                        "uring_readv_byte_count",
                        RequirementLevel::Must,
                        TestStatus::Fail,
                        Some(format!("Expected 3072 bytes, got {}", bytes_read)),
                    );
                }
            }
            Err(e) => {
                self.record_test(
                    "uring_readv_basic",
                    RequirementLevel::Must,
                    TestStatus::Fail,
                    Some(e.to_string()),
                );
            }
        }

        // Test 2: Vectored write atomicity
        let write_bufs = [IoSlice::new(b"hello"), IoSlice::new(b"world")];
        match self.uring_processor.writev(2, &write_bufs, 0) {
            Ok(bytes_written) => {
                if bytes_written == 10 {
                    self.record_test(
                        "uring_writev_atomicity",
                        RequirementLevel::Must,
                        TestStatus::Pass,
                        None,
                    );
                } else {
                    self.record_test(
                        "uring_writev_atomicity",
                        RequirementLevel::Must,
                        TestStatus::Fail,
                        Some(format!("Partial write: {} of 10 bytes", bytes_written)),
                    );
                }
            }
            Err(e) => {
                self.record_test(
                    "uring_writev_atomicity",
                    RequirementLevel::Must,
                    TestStatus::Fail,
                    Some(e.to_string()),
                );
            }
        }

        // Test 3: Operation ordering
        self.uring_processor.validate_consistency().map_err(|e| {
            self.record_test(
                "uring_operation_ordering",
                RequirementLevel::Must,
                TestStatus::Fail,
                Some(e.clone()),
            );
            e
        })?;
        self.record_test(
            "uring_operation_ordering",
            RequirementLevel::Must,
            TestStatus::Pass,
            None,
        );

        Ok(())
    }

    fn test_vfs_canonicalization_invariants(&mut self) -> Result<(), String> {
        // Test 1: Basic canonicalization
        let test_paths = [
            ("/foo/bar/../baz", "/foo/baz"),
            ("/foo/./bar", "/foo/bar"),
            ("//double//slash", "/double/slash"),
            ("/trailing/slash/", "/trailing/slash"),
        ];

        for (input, expected) in &test_paths {
            match self.vfs_canonicalizer.canonicalize(input) {
                Ok(canonical) => {
                    if canonical == Path::new(expected) {
                        self.record_test(
                            &format!("vfs_canonicalize_{}", input.replace('/', "_")),
                            RequirementLevel::Must,
                            TestStatus::Pass,
                            None,
                        );
                    } else {
                        self.record_test(
                            &format!("vfs_canonicalize_{}", input.replace('/', "_")),
                            RequirementLevel::Must,
                            TestStatus::Fail,
                            Some(format!(
                                "Expected {}, got {}",
                                expected,
                                canonical.display()
                            )),
                        );
                    }
                }
                Err(e) => {
                    self.record_test(
                        &format!("vfs_canonicalize_{}", input.replace('/', "_")),
                        RequirementLevel::Must,
                        TestStatus::Fail,
                        Some(e.to_string()),
                    );
                }
            }
        }

        // Test 2: Symlink resolution
        match self.vfs_canonicalizer.canonicalize("/link/to/file") {
            Ok(canonical) => {
                if canonical == Path::new("/real/file") {
                    self.record_test(
                        "vfs_symlink_resolution",
                        RequirementLevel::Must,
                        TestStatus::Pass,
                        None,
                    );
                } else {
                    self.record_test(
                        "vfs_symlink_resolution",
                        RequirementLevel::Must,
                        TestStatus::Fail,
                        Some(format!("Expected /real/file, got {}", canonical.display())),
                    );
                }
            }
            Err(e) => {
                self.record_test(
                    "vfs_symlink_resolution",
                    RequirementLevel::Must,
                    TestStatus::Fail,
                    Some(e.to_string()),
                );
            }
        }

        // Test 3: Cycle detection
        self.vfs_canonicalizer.add_symlink("/cycle/a", "/cycle/b");
        self.vfs_canonicalizer.add_symlink("/cycle/b", "/cycle/a");

        match self.vfs_canonicalizer.canonicalize("/cycle/a") {
            Ok(_) => {
                self.record_test(
                    "vfs_cycle_detection",
                    RequirementLevel::Must,
                    TestStatus::Fail,
                    Some("Cycle not detected".to_string()),
                );
            }
            Err(_) => {
                self.record_test(
                    "vfs_cycle_detection",
                    RequirementLevel::Must,
                    TestStatus::Pass,
                    None,
                );
            }
        }

        Ok(())
    }

    fn test_buf_reader_writer_boundaries(&mut self) -> Result<(), String> {
        // Test 1: Buffer boundary handling
        self.buf_processor
            .fill_read_buffer(b"hello world test data");

        let mut small_buf = [0u8; 5];
        match self.buf_processor.buf_read(&mut small_buf) {
            Ok(bytes_read) => {
                if bytes_read == 5 && &small_buf == b"hello" {
                    self.record_test(
                        "buf_reader_boundary",
                        RequirementLevel::Must,
                        TestStatus::Pass,
                        None,
                    );
                } else {
                    self.record_test(
                        "buf_reader_boundary",
                        RequirementLevel::Must,
                        TestStatus::Fail,
                        Some(format!("Unexpected read result: {} bytes", bytes_read)),
                    );
                }
            }
            Err(e) => {
                self.record_test(
                    "buf_reader_boundary",
                    RequirementLevel::Must,
                    TestStatus::Fail,
                    Some(e.to_string()),
                );
            }
        }

        // Test 2: Write buffering and flush
        let large_data = vec![42u8; 10000]; // Larger than buffer
        match self.buf_processor.buf_write(&large_data) {
            Ok(bytes_written) => {
                if bytes_written == 10000 {
                    self.record_test(
                        "buf_writer_large_write",
                        RequirementLevel::Must,
                        TestStatus::Pass,
                        None,
                    );
                } else {
                    self.record_test(
                        "buf_writer_large_write",
                        RequirementLevel::Must,
                        TestStatus::Fail,
                        Some(format!("Partial write: {} of 10000 bytes", bytes_written)),
                    );
                }
            }
            Err(e) => {
                self.record_test(
                    "buf_writer_large_write",
                    RequirementLevel::Must,
                    TestStatus::Fail,
                    Some(e.to_string()),
                );
            }
        }

        // Test 3: Boundary semantics validation
        self.buf_processor
            .validate_boundary_semantics()
            .map_err(|e| {
                self.record_test(
                    "buf_boundary_semantics",
                    RequirementLevel::Must,
                    TestStatus::Fail,
                    Some(e.clone()),
                );
                e
            })?;
        self.record_test(
            "buf_boundary_semantics",
            RequirementLevel::Must,
            TestStatus::Pass,
            None,
        );

        Ok(())
    }

    fn record_test(
        &mut self,
        name: &str,
        level: RequirementLevel,
        status: TestStatus,
        error: Option<String>,
    ) {
        self.test_results.push(ConformanceTestResult {
            test_name: name.to_string(),
            requirement_level: level,
            status,
            error_message: error,
        });
    }

    fn generate_compliance_report(&self) -> Result<(), String> {
        let mut must_pass = 0;
        let mut must_total = 0;
        for result in &self.test_results {
            if result.requirement_level == RequirementLevel::Must {
                must_total += 1;
                if result.status == TestStatus::Pass {
                    must_pass += 1;
                }
            }
        }

        let must_score = if must_total > 0 {
            (must_pass as f64 / must_total as f64) * 100.0
        } else {
            100.0
        };

        if must_score < 95.0 {
            return Err(format!(
                "MUST requirement compliance below 95%: {:.1}% ({}/{})",
                must_score, must_pass, must_total
            ));
        }

        Ok(())
    }
}

// ─── Conformance Tests ──────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fs_protocol_conformance_comprehensive() {
        let mut harness = FsProtocolConformanceHarness::new();

        harness.run_all_tests().unwrap_or_else(|e| {
            panic!("FS protocol conformance test failed: {}", e);
        });

        // Verify we tested all major components
        let test_names: Vec<&str> = harness
            .test_results
            .iter()
            .map(|r| r.test_name.as_str())
            .collect();

        assert!(test_names.iter().any(|name| name.contains("uring_readv")));
        assert!(test_names.iter().any(|name| name.contains("uring_writev")));
        assert!(
            test_names
                .iter()
                .any(|name| name.contains("vfs_canonicalize"))
        );
        assert!(test_names.iter().any(|name| name.contains("vfs_symlink")));
        assert!(test_names.iter().any(|name| name.contains("buf_reader")));
        assert!(test_names.iter().any(|name| name.contains("buf_writer")));
    }

    #[test]
    fn test_uring_readv_writev_round_trip() {
        let mut processor = MockUringProcessor::new(UringConsistencyMode::Strict);

        // Write then read the same data
        let write_data = [IoSlice::new(b"test"), IoSlice::new(b"data")];
        let bytes_written = processor.writev(1, &write_data, 0).unwrap();
        assert_eq!(bytes_written, 8);

        let mut read_buf1 = [0u8; 4];
        let mut read_buf2 = [0u8; 4];
        let mut read_bufs = [
            IoSliceMut::new(&mut read_buf1),
            IoSliceMut::new(&mut read_buf2),
        ];
        let bytes_read = processor.readv(1, &mut read_bufs, 0).unwrap();
        assert_eq!(bytes_read, 8);

        processor.validate_consistency().unwrap();
    }

    #[test]
    fn test_vfs_canonicalization_edge_cases() {
        let mut canonicalizer = MockVfsCanonicalizer::new();

        // Test empty components
        let result = canonicalizer.canonicalize("/foo//bar").unwrap();
        assert_eq!(result, PathBuf::from("/foo/bar"));

        // Test current directory
        let result = canonicalizer.canonicalize("/foo/./bar").unwrap();
        assert_eq!(result, PathBuf::from("/foo/bar"));

        // Test parent directory
        let result = canonicalizer.canonicalize("/foo/bar/../baz").unwrap();
        assert_eq!(result, PathBuf::from("/foo/baz"));

        // Test symlink resolution
        let result = canonicalizer.canonicalize("/link/to/file").unwrap();
        assert_eq!(result, PathBuf::from("/real/file"));

        // Test mount metadata lookup
        let result = canonicalizer.canonicalize("/mnt/volume/file").unwrap();
        assert_eq!(result, PathBuf::from("/mnt/volume/file"));
        assert_eq!(
            canonicalizer.mount_source_for_path(&result),
            Some(Path::new("/dev/sda1"))
        );
    }

    #[test]
    fn test_buf_processor_boundary_semantics() {
        let mut processor = MockBufProcessor::new(1024);

        // Test read boundary
        processor.fill_read_buffer(b"hello world");
        let mut small_buf = [0u8; 5];
        let bytes_read = processor.buf_read(&mut small_buf).unwrap();
        assert_eq!(bytes_read, 5);
        assert_eq!(&small_buf, b"hello");

        // Test write boundary with flush
        let large_data = vec![1u8; 2048]; // Larger than buffer
        let bytes_written = processor.buf_write(&large_data).unwrap();
        assert_eq!(bytes_written, 2048);
        assert!(processor.bytes_written() > 0); // Should have flushed

        processor.validate_boundary_semantics().unwrap();
    }

    #[test]
    fn test_uring_consistency_modes() {
        let mut strict = MockUringProcessor::new(UringConsistencyMode::Strict);
        let mut permissive = MockUringProcessor::new(UringConsistencyMode::Permissive);

        let bufs = [IoSlice::new(b"test")];

        // Both should succeed for normal operations
        assert!(strict.writev(1, &bufs, 0).is_ok());
        assert!(permissive.writev(1, &bufs, 0).is_ok());

        // Validation should pass for both
        assert!(strict.validate_consistency().is_ok());
        assert!(permissive.validate_consistency().is_ok());
    }

    #[test]
    fn test_conformance_result_tracking() {
        let mut harness = FsProtocolConformanceHarness::new();

        // Record some test results
        harness.record_test("test1", RequirementLevel::Must, TestStatus::Pass, None);
        harness.record_test(
            "test2",
            RequirementLevel::Should,
            TestStatus::Fail,
            Some("error".to_string()),
        );

        assert_eq!(harness.test_results.len(), 2);
        assert_eq!(harness.test_results[0].test_name, "test1");
        assert_eq!(
            harness.test_results[0].requirement_level,
            RequirementLevel::Must
        );
        assert_eq!(harness.test_results[0].status, TestStatus::Pass);
        assert_eq!(harness.test_results[1].status, TestStatus::Fail);
        assert_eq!(
            harness.test_results[1].error_message,
            Some("error".to_string())
        );
    }
}