ios-core 0.1.7

High-level device API, pairing transport, and discovery for iOS devices
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
//! AFC (Apple File Conduit) protocol – direct async I/O implementation.
//!
//! Wire protocol reference: go-ios/ios/afc/afc.go + client.go
//!
//! Frame structure:
//!   [AfcHeader: 40 bytes LE]
//!   [header_payload: (this_len - 40) bytes]  – null-terminated paths, file handles, status etc.
//!   [payload: (entire_len - this_len) bytes] – file data for reads/writes
//!
//! For ReadDir responses, the device puts filenames in the header_payload section.
//! For FileRead responses, the data is in the payload section.

use std::collections::HashMap;

use crate::proto::afc::{AfcHeader, AfcOpcode, AFC_MAGIC};
use bytes::{Bytes, BytesMut};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use zerocopy::{FromBytes, IntoBytes};

#[cfg(feature = "house_arrest")]
pub use super::house_arrest;

pub mod protocol; // kept for re-export compatibility

// ── error ─────────────────────────────────────────────────────────────────────

#[derive(Debug, thiserror::Error)]
pub enum AfcError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("AFC error: {0}")]
    Status(AfcStatusCode),
    #[error("protocol error: {0}")]
    Protocol(String),
}

/// AFC status codes from the device (matches go-ios/ios/afc/errors.go).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AfcStatusCode {
    Success,
    Unknown,
    OperationHeaderInvalid,
    NoResources,
    ReadError,
    WriteError,
    UnknownPacketType,
    InvalidArgument,
    ObjectNotFound,
    ObjectIsDir,
    PermDenied,
    ServiceNotConnected,
    Timeout,
    TooMuchData,
    EndOfData,
    OpNotSupported,
    ObjectExists,
    ObjectBusy,
    NoSpaceLeft,
    OpWouldBlock,
    IoError,
    OpInterrupted,
    OpInProgress,
    InternalError,
    MuxError,
    NoMem,
    NotEnoughData,
    DirNotEmpty,
    Other(u64),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AfcFileInfo {
    pub name: Option<String>,
    pub file_type: Option<String>,
    pub size: Option<u64>,
    pub mode: Option<u32>,
    pub link_target: Option<String>,
    pub raw: HashMap<String, String>,
}

impl AfcStatusCode {
    pub fn from_u64(code: u64) -> Self {
        match code {
            0 => Self::Success,
            1 => Self::Unknown,
            2 => Self::OperationHeaderInvalid,
            3 => Self::NoResources,
            4 => Self::ReadError,
            5 => Self::WriteError,
            6 => Self::UnknownPacketType,
            7 => Self::InvalidArgument,
            8 => Self::ObjectNotFound,
            9 => Self::ObjectIsDir,
            10 => Self::PermDenied,
            11 => Self::ServiceNotConnected,
            12 => Self::Timeout,
            13 => Self::TooMuchData,
            14 => Self::EndOfData,
            15 => Self::OpNotSupported,
            16 => Self::ObjectExists,
            17 => Self::ObjectBusy,
            18 => Self::NoSpaceLeft,
            19 => Self::OpWouldBlock,
            20 => Self::IoError,
            21 => Self::OpInterrupted,
            22 => Self::OpInProgress,
            23 => Self::InternalError,
            30 => Self::MuxError,
            31 => Self::NoMem,
            32 => Self::NotEnoughData,
            33 => Self::DirNotEmpty,
            _ => Self::Other(code),
        }
    }
}

impl std::fmt::Display for AfcStatusCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Success => write!(f, "success"),
            Self::Unknown => write!(f, "unknown error (1)"),
            Self::OperationHeaderInvalid => write!(f, "operation header invalid (2)"),
            Self::NoResources => write!(f, "no resources (3)"),
            Self::ReadError => write!(f, "read error (4)"),
            Self::WriteError => write!(f, "write error (5)"),
            Self::UnknownPacketType => write!(f, "unknown packet type (6)"),
            Self::InvalidArgument => write!(f, "invalid argument (7)"),
            Self::ObjectNotFound => write!(f, "object not found (8)"),
            Self::ObjectIsDir => write!(f, "object is directory (9)"),
            Self::PermDenied => write!(f, "permission denied (10)"),
            Self::ServiceNotConnected => write!(f, "service not connected (11)"),
            Self::Timeout => write!(f, "timeout (12)"),
            Self::TooMuchData => write!(f, "too much data (13)"),
            Self::EndOfData => write!(f, "end of data (14)"),
            Self::OpNotSupported => write!(f, "operation not supported (15)"),
            Self::ObjectExists => write!(f, "object exists (16)"),
            Self::ObjectBusy => write!(f, "object busy (17)"),
            Self::NoSpaceLeft => write!(f, "no space left (18)"),
            Self::OpWouldBlock => write!(f, "operation would block (19)"),
            Self::IoError => write!(f, "I/O error (20)"),
            Self::OpInterrupted => write!(f, "operation interrupted (21)"),
            Self::OpInProgress => write!(f, "operation in progress (22)"),
            Self::InternalError => write!(f, "internal error (23)"),
            Self::MuxError => write!(f, "mux error (30)"),
            Self::NoMem => write!(f, "no memory (31)"),
            Self::NotEnoughData => write!(f, "not enough data (32)"),
            Self::DirNotEmpty => write!(f, "directory not empty (33)"),
            Self::Other(code) => write!(f, "unknown status ({code})"),
        }
    }
}

// ── raw packet ────────────────────────────────────────────────────────────────

struct Packet {
    #[allow(dead_code)]
    opcode: u64,
    /// Embedded data within the "header" section (this_len - 40 bytes).
    /// Used for: directory listings, file info, status codes, file handles.
    header_payload: Bytes,
    /// Extra data after the header section (entire_len - this_len bytes).
    /// Used for: file read data.
    payload: Bytes,
}

// ── client ────────────────────────────────────────────────────────────────────

/// AFC file-system client.
///
/// `S` must be a full-duplex async stream (e.g. the raw usbmux stream from lockdown).
pub struct AfcClient<S> {
    stream: S,
    packet_num: u64,
}

impl<S: AsyncRead + AsyncWrite + Unpin> AfcClient<S> {
    pub const FILE_MODE_READ_ONLY: u64 = 0x00000001;
    pub const FILE_MODE_READ_WRITE: u64 = 0x00000002;
    pub const FILE_MODE_WRITE_ONLY_CREATE_TRUNC: u64 = 0x00000003;
    pub const LOCK_EXCLUSIVE: u64 = 2 | 4;
    pub const LOCK_UNLOCK: u64 = 8 | 4;

    pub fn new(stream: S) -> Self {
        // go-ios uses atomic.Add(1) which returns 1 on first call.
        // Devices may reject packet_num=0 for some operations.
        Self {
            stream,
            packet_num: 1,
        }
    }

    fn next_pnum(&mut self) -> u64 {
        let n = self.packet_num;
        self.packet_num += 1;
        n
    }

    // ── send ─────────────────────────────────────────────────────────────────

    async fn send(
        &mut self,
        opcode: AfcOpcode,
        header_payload: &[u8],
        payload: &[u8],
    ) -> Result<(), AfcError> {
        let pnum = self.next_pnum();
        let hdr = AfcHeader::new(pnum, opcode, header_payload.len(), payload.len());
        self.stream.write_all(hdr.as_bytes()).await?;
        if !header_payload.is_empty() {
            self.stream.write_all(header_payload).await?;
        }
        if !payload.is_empty() {
            self.stream.write_all(payload).await?;
        }
        self.stream.flush().await?;
        Ok(())
    }

    // ── recv ─────────────────────────────────────────────────────────────────

    async fn recv(&mut self) -> Result<Packet, AfcError> {
        let mut hdr_buf = [0u8; AfcHeader::SIZE];
        self.stream.read_exact(&mut hdr_buf).await?;

        let hdr = AfcHeader::ref_from_bytes(&hdr_buf)
            .map_err(|_| AfcError::Protocol("bad AFC header".into()))?;

        if hdr.magic.get() != AFC_MAGIC {
            return Err(AfcError::Protocol(format!(
                "bad AFC magic: 0x{:016X}",
                hdr.magic.get()
            )));
        }

        let entire_len = hdr.entire_len.get() as usize;
        let this_len = hdr.this_len.get() as usize;
        let opcode = hdr.operation.get();

        let header_payload_len = this_len.saturating_sub(AfcHeader::SIZE);
        let payload_len = entire_len.saturating_sub(this_len);

        // Sanity check against DoS
        const MAX_AFC_MSG: usize = 256 * 1024 * 1024; // 256 MiB
        if header_payload_len > MAX_AFC_MSG || payload_len > MAX_AFC_MSG {
            return Err(AfcError::Protocol(format!(
                "AFC frame too large: header_payload={header_payload_len} payload={payload_len}"
            )));
        }

        let mut header_payload = vec![0u8; header_payload_len];
        let mut payload = vec![0u8; payload_len];

        if header_payload_len > 0 {
            self.stream.read_exact(&mut header_payload).await?;
        }
        if payload_len > 0 {
            self.stream.read_exact(&mut payload).await?;
        }

        // Status opcode (1): header_payload[0..8] = LE u64 error code
        if opcode == AfcOpcode::Status as u64 {
            let code = AfcStatusCode::from_u64(if header_payload.len() >= 8 {
                u64::from_le_bytes(
                    header_payload[..8]
                        .try_into()
                        .map_err(|_| AfcError::Protocol("bad status code".into()))?,
                )
            } else {
                0
            });
            if code != AfcStatusCode::Success {
                return Err(AfcError::Status(code));
            }
        }

        Ok(Packet {
            opcode,
            header_payload: Bytes::from(header_payload),
            payload: Bytes::from(payload),
        })
    }

    // ── public API ────────────────────────────────────────────────────────────

    /// List directory entries (excludes "." and "..").
    ///
    /// The AFC device returns null-separated filenames in the payload section
    /// of the response frame (entire_len > this_len).
    pub async fn list_dir(&mut self, path: &str) -> Result<Vec<String>, AfcError> {
        let mut hp = path.as_bytes().to_vec();
        hp.push(0);
        self.send(AfcOpcode::ReadDir, &hp, &[]).await?;
        let pkt = self.recv().await?;
        // Filenames come in the payload section (consistent with go-ios pack.Payload)
        let entries = split_null_strings(&pkt.payload)
            .into_iter()
            .filter(|s| s != "." && s != "..")
            .collect();
        Ok(entries)
    }

    /// Get key-value file info for a path.
    pub async fn stat(&mut self, path: &str) -> Result<HashMap<String, String>, AfcError> {
        let mut hp = path.as_bytes().to_vec();
        hp.push(0);
        self.send(AfcOpcode::GetFileInfo, &hp, &[]).await?;
        let pkt = self.recv().await?;
        Ok(parse_kv_pairs(&pkt.payload))
    }

    /// Get parsed file info for a path.
    ///
    /// AFC reports `st_mode` as an octal string. This helper parses it into a
    /// `u32`, matching go-ios behavior.
    pub async fn stat_info(&mut self, path: &str) -> Result<AfcFileInfo, AfcError> {
        let raw = self.stat(path).await?;
        Ok(parse_file_info(path, raw))
    }

    /// Create a directory.
    pub async fn make_dir(&mut self, path: &str) -> Result<(), AfcError> {
        let mut hp = path.as_bytes().to_vec();
        hp.push(0);
        self.send(AfcOpcode::MakePath, &hp, &[]).await?;
        self.recv().await?;
        Ok(())
    }

    /// Remove a path (file or empty directory).
    pub async fn remove(&mut self, path: &str) -> Result<(), AfcError> {
        let mut hp = path.as_bytes().to_vec();
        hp.push(0);
        self.send(AfcOpcode::RemovePath, &hp, &[]).await?;
        self.recv().await?;
        Ok(())
    }

    /// Remove a path and all contents recursively.
    pub async fn remove_all(&mut self, path: &str) -> Result<(), AfcError> {
        let mut hp = path.as_bytes().to_vec();
        hp.push(0);
        self.send(AfcOpcode::RemovePathAndContents, &hp, &[])
            .await?;
        self.recv().await?;
        Ok(())
    }

    /// Rename / move a path.
    pub async fn rename(&mut self, from: &str, to: &str) -> Result<(), AfcError> {
        let mut hp = from.as_bytes().to_vec();
        hp.push(0);
        hp.extend_from_slice(to.as_bytes());
        hp.push(0);
        self.send(AfcOpcode::RenamePath, &hp, &[]).await?;
        self.recv().await?;
        Ok(())
    }

    /// Read an entire file into memory.
    pub async fn read_file(&mut self, path: &str) -> Result<Bytes, AfcError> {
        let fd = self.file_open(path, Self::FILE_MODE_READ_ONLY).await?; // READ_ONLY
        let mut data = BytesMut::new();
        let chunk = 65536u64;
        loop {
            let buf = self.file_read(fd, chunk).await?;
            if buf.is_empty() {
                break;
            }
            data.extend_from_slice(&buf);
        }
        self.file_close(fd).await?;
        Ok(data.freeze())
    }

    /// Read an entire file into memory, following a single AFC symlink.
    ///
    /// This matches go-ios PullSingleFile behavior: if the source path is a
    /// symlink, AFC reports the target in `st_linktarget` and the file is read
    /// from that target path instead.
    pub async fn read_file_follow_links(&mut self, path: &str) -> Result<Bytes, AfcError> {
        let target = self.resolve_read_path(path).await?;
        self.read_file(&target).await
    }

    /// Write data to a file (creates or truncates).
    pub async fn write_file(&mut self, path: &str, data: &[u8]) -> Result<(), AfcError> {
        let mut reader = std::io::Cursor::new(data);
        self.write_file_from_reader(path, &mut reader).await
    }

    /// Write data from an async reader to a file (creates or truncates).
    pub async fn write_file_from_reader<R>(
        &mut self,
        path: &str,
        reader: &mut R,
    ) -> Result<(), AfcError>
    where
        R: AsyncRead + Unpin,
    {
        let fd = self
            .file_open(path, Self::FILE_MODE_WRITE_ONLY_CREATE_TRUNC)
            .await?;
        let result = async {
            let mut buf = vec![0u8; 1024 * 1024];
            loop {
                let n = reader.read(&mut buf).await?;
                if n == 0 {
                    break;
                }
                self.file_write(fd, &buf[..n]).await?;
            }
            Ok::<(), AfcError>(())
        }
        .await;
        let close_result = self.file_close(fd).await;
        result?;
        close_result?;
        Ok(())
    }

    pub async fn open_file(&mut self, path: &str, mode: u64) -> Result<u64, AfcError> {
        self.file_open(path, mode).await
    }

    pub async fn lock_file(&mut self, fd: u64, operation: u64) -> Result<(), AfcError> {
        let mut hp = [0u8; 16];
        hp[..8].copy_from_slice(&fd.to_le_bytes());
        hp[8..].copy_from_slice(&operation.to_le_bytes());
        self.send(AfcOpcode::FileRefLock, &hp, &[]).await?;
        self.recv().await?;
        Ok(())
    }

    pub async fn close_file(&mut self, fd: u64) -> Result<(), AfcError> {
        self.file_close(fd).await
    }

    /// Get device filesystem info.
    pub async fn device_info(&mut self) -> Result<HashMap<String, String>, AfcError> {
        self.send(AfcOpcode::GetDeviceInfo, &[], &[]).await?;
        let pkt = self.recv().await?;
        Ok(parse_kv_pairs(&pkt.payload))
    }

    // ── file handle ops ───────────────────────────────────────────────────────

    async fn file_open(&mut self, path: &str, mode: u64) -> Result<u64, AfcError> {
        let mut hp = vec![0u8; 8];
        hp[..8].copy_from_slice(&mode.to_le_bytes());
        hp.extend_from_slice(path.as_bytes());
        hp.push(0);
        self.send(AfcOpcode::FileRefOpen, &hp, &[]).await?;
        let pkt = self.recv().await?;
        if pkt.header_payload.len() < 8 {
            return Err(AfcError::Protocol(
                "FileRefOpenResult: short response".into(),
            ));
        }
        let fd = u64::from_le_bytes(
            pkt.header_payload[..8]
                .try_into()
                .map_err(|_| AfcError::Protocol("bad file handle".into()))?,
        );
        Ok(fd)
    }

    async fn file_read(&mut self, fd: u64, size: u64) -> Result<Bytes, AfcError> {
        let mut hp = [0u8; 16];
        hp[..8].copy_from_slice(&fd.to_le_bytes());
        hp[8..].copy_from_slice(&size.to_le_bytes());
        self.send(AfcOpcode::FileRefRead, &hp, &[]).await?;
        let pkt = self.recv().await?;
        Ok(pkt.payload)
    }

    async fn file_write(&mut self, fd: u64, data: &[u8]) -> Result<(), AfcError> {
        let mut hp = [0u8; 8];
        hp.copy_from_slice(&fd.to_le_bytes());
        self.send(AfcOpcode::FileRefWrite, &hp, data).await?;
        self.recv().await?;
        Ok(())
    }

    async fn file_close(&mut self, fd: u64) -> Result<(), AfcError> {
        let hp = fd.to_le_bytes();
        self.send(AfcOpcode::FileRefClose, &hp, &[]).await?;
        self.recv().await?;
        Ok(())
    }

    async fn resolve_read_path(&mut self, path: &str) -> Result<String, AfcError> {
        let info = self.stat_info(path).await?;
        Ok(resolve_link_target(path, &info))
    }
}

// ── helpers ───────────────────────────────────────────────────────────────────

/// Split a null-byte-separated byte slice into owned strings, skipping empty.
fn split_null_strings(data: &[u8]) -> Vec<String> {
    data.split(|&b| b == 0)
        .filter(|s| !s.is_empty())
        .map(|s| String::from_utf8_lossy(s).into_owned())
        .collect()
}

/// Parse null-separated key\0value\0 pairs into a HashMap.
fn parse_kv_pairs(data: &[u8]) -> HashMap<String, String> {
    let parts = split_null_strings(data);
    let mut map = HashMap::new();
    let mut it = parts.into_iter();
    while let (Some(k), Some(v)) = (it.next(), it.next()) {
        map.insert(k, v);
    }
    map
}

fn parse_file_info(path: &str, raw: HashMap<String, String>) -> AfcFileInfo {
    let name = path
        .rsplit('/')
        .next()
        .filter(|s| !s.is_empty())
        .map(str::to_string);
    let file_type = raw.get("st_ifmt").cloned();
    let size = raw.get("st_size").and_then(|s| s.parse::<u64>().ok());
    let mode = raw
        .get("st_mode")
        .and_then(|s| u32::from_str_radix(s, 8).ok());
    let link_target = raw.get("st_linktarget").cloned().filter(|s| !s.is_empty());

    AfcFileInfo {
        name,
        file_type,
        size,
        mode,
        link_target,
        raw,
    }
}

fn resolve_link_target(path: &str, info: &AfcFileInfo) -> String {
    let is_link = matches!(info.file_type.as_deref(), Some("S_IFLNK"));
    if is_link {
        if let Some(target) = &info.link_target {
            return target.clone();
        }
    }
    path.to_string()
}

// ── file mode constants (for callers) ─────────────────────────────────────────
pub mod mode {
    pub const READ_ONLY: u64 = 0x00000001;
    pub const READ_WRITE_CREATE: u64 = 0x00000002;
    pub const WRITE_ONLY_CREATE_TRUNC: u64 = 0x00000003;
    pub const READ_WRITE_CREATE_TRUNC: u64 = 0x00000004;
    pub const WRITE_ONLY_CREATE_APPEND: u64 = 0x00000005;
    pub const READ_WRITE_CREATE_APPEND: u64 = 0x00000006;
}

#[cfg(test)]
mod tests {
    use crate::test_util::MockStream;

    use super::*;
    use zerocopy::FromBytes;

    fn afc_frame(opcode: AfcOpcode, header_payload: &[u8], payload: &[u8]) -> Vec<u8> {
        let header = AfcHeader::new(1, opcode, header_payload.len(), payload.len());
        let mut frame = header.as_bytes().to_vec();
        frame.extend_from_slice(header_payload);
        frame.extend_from_slice(payload);
        frame
    }

    fn afc_status_success_frame() -> Vec<u8> {
        afc_frame(AfcOpcode::Status, &0u64.to_le_bytes(), &[])
    }

    fn afc_open_result_frame(fd: u64) -> Vec<u8> {
        afc_frame(AfcOpcode::FileRefOpenResult, &fd.to_le_bytes(), &[])
    }

    fn afc_write_success_responses(write_count: usize) -> Vec<u8> {
        let mut responses = afc_open_result_frame(42);
        for _ in 0..write_count {
            responses.extend_from_slice(&afc_status_success_frame());
        }
        responses.extend_from_slice(&afc_status_success_frame());
        responses
    }

    fn file_write_payloads(written: &[u8]) -> Vec<Vec<u8>> {
        let mut pos = 0;
        let mut payloads = Vec::new();
        while pos + AfcHeader::SIZE <= written.len() {
            let header = AfcHeader::ref_from_bytes(&written[pos..pos + AfcHeader::SIZE]).unwrap();
            let entire_len = header.entire_len.get() as usize;
            let this_len = header.this_len.get() as usize;
            let payload_start = pos + this_len;
            let payload_end = pos + entire_len;
            if header.operation.get() == AfcOpcode::FileRefWrite as u64 {
                payloads.push(written[payload_start..payload_end].to_vec());
            }
            pos += entire_len;
        }
        assert_eq!(pos, written.len());
        payloads
    }

    #[test]
    fn test_split_null_strings() {
        let data = b"foo\0bar\0baz\0";
        let result = split_null_strings(data);
        assert_eq!(result, vec!["foo", "bar", "baz"]);
    }

    #[test]
    fn test_parse_kv_pairs() {
        let data = b"st_size\x0012345\0st_ifmt\0S_IFREG\0";
        let map = parse_kv_pairs(data);
        assert_eq!(map["st_size"], "12345");
        assert_eq!(map["st_ifmt"], "S_IFREG");
    }

    #[test]
    fn test_afc_header_size() {
        assert_eq!(std::mem::size_of::<AfcHeader>(), 40);
    }

    #[test]
    fn test_afc_header_new() {
        let hdr = AfcHeader::new(7, AfcOpcode::ReadDir, 5, 10);
        assert_eq!(hdr.magic.get(), AFC_MAGIC);
        assert_eq!(hdr.packet_num.get(), 7);
        assert_eq!(hdr.this_len.get(), 45); // 40 + 5
        assert_eq!(hdr.entire_len.get(), 55); // 45 + 10
        assert_eq!(hdr.operation.get(), AfcOpcode::ReadDir as u64);
    }

    /// Simulate list_dir: the device returns filenames in the payload section.
    #[tokio::test]
    async fn test_list_dir_roundtrip() {
        use zerocopy::IntoBytes;

        // ReadDir response: filenames are in the payload section
        // (this_len = 40, entire_len = 40 + names.len())
        let names = b".\0..\0Photos\0Downloads\0";
        let hdr = AfcHeader::new(
            1,                  // packet_num
            AfcOpcode::ReadDir, // opcode
            0,                  // header_payload = 0 bytes (this_len = 40)
            names.len(),        // payload = filenames (entire_len = 40 + names.len())
        );
        let mut server_resp = hdr.as_bytes().to_vec();
        server_resp.extend_from_slice(names); // payload section follows header

        let (client_side, mut server_side) = tokio::io::duplex(4096);
        tokio::spawn(async move {
            use tokio::io::{AsyncReadExt, AsyncWriteExt};
            let mut buf = vec![0u8; 256];
            let _ = server_side.read(&mut buf).await;
            server_side.write_all(&server_resp).await.unwrap();
        });

        let mut afc = AfcClient::new(client_side);
        let entries = afc.list_dir("/").await.unwrap();
        // "." and ".." are filtered out
        assert_eq!(entries, vec!["Photos", "Downloads"]);
    }

    #[test]
    fn test_resolve_link_target_uses_st_linktarget_for_symlink() {
        let mut raw = HashMap::new();
        raw.insert("st_ifmt".to_string(), "S_IFLNK".to_string());
        raw.insert(
            "st_linktarget".to_string(),
            "/var/mobile/real-file".to_string(),
        );
        let info = parse_file_info("/var/mobile/link", raw);

        let resolved = resolve_link_target("/var/mobile/link", &info);
        assert_eq!(resolved, "/var/mobile/real-file");
    }

    #[test]
    fn test_resolve_link_target_keeps_original_path_for_regular_file() {
        let mut raw = HashMap::new();
        raw.insert("st_ifmt".to_string(), "S_IFREG".to_string());
        let info = parse_file_info("/var/mobile/file", raw);

        let resolved = resolve_link_target("/var/mobile/file", &info);
        assert_eq!(resolved, "/var/mobile/file");
    }

    #[test]
    fn test_parse_file_info_parses_st_mode_from_octal() {
        let mut raw = HashMap::new();
        raw.insert("st_ifmt".to_string(), "S_IFREG".to_string());
        raw.insert("st_mode".to_string(), "100644".to_string());
        raw.insert("st_size".to_string(), "12".to_string());

        let info = parse_file_info("/var/mobile/file.txt", raw);
        assert_eq!(info.name.as_deref(), Some("file.txt"));
        assert_eq!(info.file_type.as_deref(), Some("S_IFREG"));
        assert_eq!(info.size, Some(12));
        assert_eq!(info.mode, Some(0o100644));
    }

    #[test]
    fn test_afc_status_code_mapping_matches_go_ios_upper_status_codes() {
        assert_eq!(AfcStatusCode::from_u64(24), AfcStatusCode::Other(24));
        assert_eq!(AfcStatusCode::from_u64(25), AfcStatusCode::Other(25));
        assert_eq!(AfcStatusCode::from_u64(26), AfcStatusCode::Other(26));
        assert_eq!(AfcStatusCode::from_u64(27), AfcStatusCode::Other(27));
        assert_eq!(AfcStatusCode::from_u64(30), AfcStatusCode::MuxError);
        assert_eq!(AfcStatusCode::from_u64(31), AfcStatusCode::NoMem);
        assert_eq!(AfcStatusCode::from_u64(32), AfcStatusCode::NotEnoughData);
        assert_eq!(AfcStatusCode::from_u64(33), AfcStatusCode::DirNotEmpty);
    }

    #[tokio::test]
    async fn write_file_from_reader_sends_chunks_without_prebuffering() {
        let payload = vec![0xAB; 1024 * 1024 + 17];
        let mut reader = std::io::Cursor::new(payload.clone());
        let mut stream = MockStream::new(afc_write_success_responses(2));
        let mut client = AfcClient::new(&mut stream);

        client
            .write_file_from_reader("/PublicStaging/app.ipa", &mut reader)
            .await
            .unwrap();

        let payloads = file_write_payloads(&stream.written);
        assert_eq!(payloads.len(), 2);
        assert_eq!(payloads[0].len(), 1024 * 1024);
        assert_eq!(payloads[1].len(), 17);
        assert_eq!(
            payloads.concat(),
            payload,
            "AFC file writes should preserve the streamed payload"
        );
    }
}