aria2-protocol 0.3.8

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
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
//! BEP 10 Extension Protocol handshake message types for BitTorrent.
//!
//! Implements the wire-format encoding/decoding for the **Extension Handshake**
//! (BEP 10), which negotiates supported extensions (e.g. `ut_metadata`,
//! `ut_pex`) with a peer.
//!
//! This type operates on the *payload* portion of a `BtMessage::Extended`,
//! i.e. the bytes **after** the 1-byte `ext_id` field. The `ext_id` itself
//! is handled at the `BtMessage` layer.

use std::collections::BTreeMap;

use tracing::debug;

use crate::bittorrent::bencode::codec::BencodeValue;

/// BEP 10 extension handshake payload.
///
/// Wire format (bencoded dict):
/// ```text
/// d
///   1:m d
///     10:ut_metadata i<id>e
///     6:ut_pex      i<id>e
///   e
///   1:v            <client version string>
///   12:metadata_size i<size>e
///   1:p            i<port>e
///   4:reqq         i<max_outstanding>e
/// e
/// ```
///
/// Default values use this implementation's peer agent with
/// `ut_metadata = 1`, `ut_pex = 2`, and `reqq = 500`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExtensionHandshake {
    /// The `m` sub-dictionary mapping extension names to their negotiated ext_id.
    m_dict: BTreeMap<Vec<u8>, BencodeValue>,
    /// Client version string (BEP 10 `v` key).
    v: Option<String>,
    /// Total metadata size in bytes for magnet links (BEP 9 `metadata_size` key).
    /// Only set when the sender has metadata to share.
    metadata_size: Option<u32>,
    /// TCP listen port (BEP 10 `p` key). Only included when > 0.
    port: Option<u16>,
    /// Maximum outstanding metadata requests the sender will accept.
    reqq: u32,
}

/// Default ext_id for ut_metadata in the `m` dict.
const DEFAULT_UT_METADATA_ID: u8 = 1;
/// Default ext_id for ut_pex in the `m` dict.
const DEFAULT_UT_PEX_ID: u8 = 2;
/// Default maximum outstanding metadata requests (reqq).
const DEFAULT_REQQ: u32 = 500;
/// Default client version string sent in the `v` key.
const DEFAULT_CLIENT_VERSION: &str = crate::identity::DEFAULT_PEER_AGENT;
/// Maximum metadata size accepted from peers (8 MiB), matching C++ aria2.
const MAX_METADATA_SIZE: u32 = 8 * 1024 * 1024;

impl ExtensionHandshake {
    /// Create a new handshake with default values:
    /// ut_metadata=1, ut_pex=2, reqq=500, and the product peer agent.
    pub fn new() -> Self {
        let mut m_dict = BTreeMap::new();
        m_dict.insert(
            b"ut_metadata".to_vec(),
            BencodeValue::Int(DEFAULT_UT_METADATA_ID as i64),
        );
        m_dict.insert(
            b"ut_pex".to_vec(),
            BencodeValue::Int(DEFAULT_UT_PEX_ID as i64),
        );

        Self {
            m_dict,
            v: Some(DEFAULT_CLIENT_VERSION.to_string()),
            metadata_size: None,
            port: None,
            reqq: DEFAULT_REQQ,
        }
    }

    /// Encode this handshake to its bencoded wire representation.
    ///
    /// Matches the C++ `HandshakeExtensionMessage::getPayload()` serialization
    /// order: `v`, `p`, `m`, `metadata_size`, `reqq`. BTreeMap key ordering
    /// may differ but bencode dicts are order-independent.
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut root = BTreeMap::new();

        // v — client version string (only when present)
        if let Some(ref v) = self.v {
            root.insert(b"v".to_vec(), BencodeValue::Bytes(v.as_bytes().to_vec()));
        }

        // p — TCP listen port (only when present)
        if let Some(p) = self.port {
            root.insert(b"p".to_vec(), BencodeValue::Int(p as i64));
        }

        // m sub-dict
        root.insert(b"m".to_vec(), BencodeValue::Dict(self.m_dict.clone()));

        // metadata_size (only when present)
        if let Some(ms) = self.metadata_size {
            root.insert(b"metadata_size".to_vec(), BencodeValue::Int(ms as i64));
        }

        // reqq
        root.insert(b"reqq".to_vec(), BencodeValue::Int(self.reqq as i64));

        BencodeValue::Dict(root).encode()
    }

    /// Parse a bencoded extension handshake payload.
    ///
    /// Mirrors C++ `HandshakeExtensionMessage::create()` validation:
    /// - `p`: must satisfy `0 < port < 65536`
    /// - `metadata_size`: must satisfy `0 < size <= 8 MiB`
    /// - `v`: client version string (may be non-UTF-8; only valid UTF-8 accepted)
    pub fn from_bytes(data: &[u8]) -> Result<Self, String> {
        let (val, consumed) = BencodeValue::decode(data)
            .map_err(|e| format!("Failed to decode extension handshake: {}", e))?;

        // For handshake payloads, we expect the entire input to be consumed.
        // However, some implementations include trailing data; we only warn.
        if consumed != data.len() {
            debug!(
                "Extension handshake: decoded {} bytes, total {} bytes (trailing data ignored)",
                consumed,
                data.len()
            );
        }

        let dict = val
            .as_dict()
            .ok_or("Extension handshake payload is not a dict")?;

        // BEP 10 makes each top-level field optional. Treat a missing or
        // malformed `m` as an empty extension table, matching aria2's
        // `downcast<Dict>(dict->get("m"))` behavior without discarding other
        // valid handshake fields.
        let m_dict = dict
            .get(b"m".as_slice())
            .and_then(BencodeValue::as_dict)
            .cloned()
            .unwrap_or_default();

        // Parse reqq (default to DEFAULT_REQQ if absent)
        let reqq = dict
            .get(b"reqq".as_slice())
            .and_then(|v| v.as_int())
            .and_then(|i| u32::try_from(i).ok())
            .unwrap_or(DEFAULT_REQQ);

        // Parse v — client version string (optional, UTF-8 only)
        let v = dict
            .get(b"v".as_slice())
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        // Parse p — TCP listen port (optional, must be 1..=65535)
        // Mirrors C++: `0 < port->i() && port->i() < 65536`
        let port = dict
            .get(b"p".as_slice())
            .and_then(|v| v.as_int())
            .filter(|&i| i > 0 && i <= u16::MAX as i64)
            .and_then(|i| u16::try_from(i).ok());

        // Parse metadata_size (optional, must be 1..=8 MiB)
        // Mirrors C++: `0 < size && size <= 8_m`
        let metadata_size = dict
            .get(b"metadata_size".as_slice())
            .and_then(|v| v.as_int())
            .filter(|&i| i > 0 && i <= MAX_METADATA_SIZE as i64)
            .and_then(|i| u32::try_from(i).ok());

        Ok(Self {
            m_dict,
            v,
            metadata_size,
            port,
            reqq,
        })
    }

    /// Get the negotiated ut_metadata ext_id from the `m` dict, if present.
    pub fn ut_metadata_id(&self) -> Option<u8> {
        self.m_dict
            .get(b"ut_metadata".as_slice())
            .and_then(|v| v.as_int())
            .and_then(|i| u8::try_from(i).ok())
    }

    /// Get the negotiated ut_pex ext_id from the `m` dict, if present.
    pub fn ut_pex_id(&self) -> Option<u8> {
        self.m_dict
            .get(b"ut_pex".as_slice())
            .and_then(|v| v.as_int())
            .and_then(|i| u8::try_from(i).ok())
    }

    /// Set the ut_metadata ext_id in the `m` dict. Returns `&mut Self` for chaining.
    pub fn with_ut_metadata(&mut self, id: u8) -> &mut Self {
        self.m_dict
            .insert(b"ut_metadata".to_vec(), BencodeValue::Int(id as i64));
        self
    }

    /// Set the ut_pex ext_id in the `m` dict. Returns `&mut Self` for chaining.
    pub fn with_ut_pex(&mut self, id: u8) -> &mut Self {
        self.m_dict
            .insert(b"ut_pex".to_vec(), BencodeValue::Int(id as i64));
        self
    }

    /// Get the reqq value from the handshake (max outstanding metadata requests).
    pub fn reqq(&self) -> u32 {
        self.reqq
    }

    /// Set the reqq value. Returns `&mut Self` for chaining.
    pub fn with_reqq(&mut self, reqq: u32) -> &mut Self {
        self.reqq = reqq;
        self
    }

    /// Get the client version string (`v` key), if present.
    pub fn v(&self) -> Option<&str> {
        self.v.as_deref()
    }

    /// Set the client version string. Returns `&mut Self` for chaining.
    pub fn with_version(&mut self, v: impl Into<String>) -> &mut Self {
        self.v = Some(v.into());
        self
    }

    /// Clear the client version string (omit `v` from the handshake).
    pub fn without_version(&mut self) -> &mut Self {
        self.v = None;
        self
    }

    /// Get the total metadata size in bytes (`metadata_size` key), if present.
    pub fn metadata_size(&self) -> Option<u32> {
        self.metadata_size
    }

    /// Set the total metadata size. Returns `&mut Self` for chaining.
    pub fn with_metadata_size(&mut self, size: u32) -> &mut Self {
        self.metadata_size = Some(size);
        self
    }

    /// Get the TCP listen port (`p` key), if present.
    pub fn port(&self) -> Option<u16> {
        self.port
    }

    /// Set the TCP listen port. Returns `&mut Self` for chaining.
    pub fn with_port(&mut self, port: u16) -> &mut Self {
        self.port = Some(port);
        self
    }

    /// Access the raw `m` dictionary.
    pub fn m_dict(&self) -> &BTreeMap<Vec<u8>, BencodeValue> {
        &self.m_dict
    }
}

impl Default for ExtensionHandshake {
    fn default() -> Self {
        Self::new()
    }
}

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

    // ======================== ExtensionHandshake tests ========================

    #[test]
    fn test_handshake_default_creation() {
        let hs = ExtensionHandshake::new();
        assert_eq!(hs.ut_metadata_id(), Some(1));
        assert_eq!(hs.ut_pex_id(), Some(2));
        assert_eq!(hs.reqq(), 500);
        assert_eq!(hs.v(), Some(DEFAULT_CLIENT_VERSION));
        assert_eq!(hs.metadata_size(), None);
        assert_eq!(hs.port(), None);
    }

    #[test]
    fn test_handshake_default_trait() {
        let hs = ExtensionHandshake::default();
        assert_eq!(hs.ut_metadata_id(), Some(1));
        assert_eq!(hs.ut_pex_id(), Some(2));
        assert_eq!(hs.v(), Some(DEFAULT_CLIENT_VERSION));
    }

    #[test]
    fn test_handshake_serialization_roundtrip() {
        let hs = ExtensionHandshake::new();
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.ut_metadata_id(), Some(1));
        assert_eq!(parsed.ut_pex_id(), Some(2));
        assert_eq!(parsed.v(), Some(DEFAULT_CLIENT_VERSION));
        assert_eq!(parsed.metadata_size(), None);
        assert_eq!(parsed.port(), None);
    }

    #[test]
    fn test_handshake_with_custom_ext_ids() {
        let mut hs = ExtensionHandshake::new();
        hs.with_ut_metadata(5).with_ut_pex(7);
        assert_eq!(hs.ut_metadata_id(), Some(5));
        assert_eq!(hs.ut_pex_id(), Some(7));

        // Roundtrip
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.ut_metadata_id(), Some(5));
        assert_eq!(parsed.ut_pex_id(), Some(7));
    }

    #[test]
    fn test_handshake_without_m_key_has_no_extension_ids() {
        let mut dict = BTreeMap::new();
        dict.insert(b"reqq".to_vec(), BencodeValue::Int(500));
        let bytes = BencodeValue::Dict(dict).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.ut_metadata_id(), None);
        assert_eq!(parsed.ut_pex_id(), None);
        assert_eq!(parsed.reqq(), 500);
    }

    #[test]
    fn test_handshake_m_not_dict_keeps_other_fields() {
        let mut dict = BTreeMap::new();
        dict.insert(b"m".to_vec(), BencodeValue::Int(42));
        dict.insert(
            b"v".to_vec(),
            BencodeValue::Bytes(b"remote-agent/2.3".to_vec()),
        );
        dict.insert(b"reqq".to_vec(), BencodeValue::Int(17));
        let bytes = BencodeValue::Dict(dict).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.ut_metadata_id(), None);
        assert_eq!(parsed.ut_pex_id(), None);
        assert_eq!(parsed.v(), Some("remote-agent/2.3"));
        assert_eq!(parsed.reqq(), 17);
    }

    #[test]
    fn test_handshake_missing_ut_metadata() {
        // m dict without ut_metadata — ut_metadata_id returns None
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_pex".to_vec(), BencodeValue::Int(3));
        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert!(parsed.ut_metadata_id().is_none());
        assert_eq!(parsed.ut_pex_id(), Some(3));
    }

    #[test]
    fn test_handshake_missing_ut_pex() {
        // m dict without ut_pex — ut_pex_id returns None
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_metadata".to_vec(), BencodeValue::Int(1));
        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.ut_metadata_id(), Some(1));
        assert!(parsed.ut_pex_id().is_none());
    }

    #[test]
    fn test_handshake_invalid_bencode() {
        let result = ExtensionHandshake::from_bytes(b"not bencoded");
        assert!(result.is_err());
    }

    #[test]
    fn test_handshake_empty_input() {
        let result = ExtensionHandshake::from_bytes(b"");
        assert!(result.is_err());
    }

    #[test]
    fn test_handshake_bencode_wire_format() {
        // Verify the actual bytes produced match expected bencode structure
        let hs = ExtensionHandshake::new();
        let bytes = hs.to_bytes();

        // Must start with 'd' and end with 'e'
        assert_eq!(bytes[0], b'd');
        assert_eq!(bytes[bytes.len() - 1], b'e');

        // Must contain "ut_metadata" and "ut_pex" keys
        let s = String::from_utf8_lossy(&bytes);
        assert!(s.contains("ut_metadata"));
        assert!(s.contains("ut_pex"));
        assert!(s.contains("reqq"));
        // Default handshake includes client version string
        assert!(s.contains(DEFAULT_CLIENT_VERSION));
        // No metadata_size or port in default handshake
        assert!(!s.contains("metadata_size"));
    }

    #[test]
    fn test_handshake_reqq_parsed_from_peer() {
        // A peer sends reqq=1000
        let mut hs = ExtensionHandshake::new();
        hs.with_reqq(1000);
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.reqq(), 1000);
    }

    #[test]
    fn test_handshake_reqq_default_when_missing() {
        // A peer sends handshake without reqq — we should default to 500
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_metadata".to_vec(), BencodeValue::Int(1));
        m_dict.insert(b"ut_pex".to_vec(), BencodeValue::Int(2));
        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        // No reqq key
        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.reqq(), 500);
    }

    // ======================== New field tests (v, metadata_size, port) ========================

    #[test]
    fn test_handshake_version_roundtrip() {
        let mut hs = ExtensionHandshake::new();
        hs.with_version("test-peer/1.0");
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.v(), Some("test-peer/1.0"));
    }

    #[test]
    fn test_handshake_version_default() {
        let hs = ExtensionHandshake::new();
        assert_eq!(hs.v(), Some(DEFAULT_CLIENT_VERSION));
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.v(), Some(DEFAULT_CLIENT_VERSION));
    }

    #[test]
    fn test_handshake_without_version() {
        let mut hs = ExtensionHandshake::new();
        hs.without_version();
        assert_eq!(hs.v(), None);
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.v(), None);
    }

    #[test]
    fn test_handshake_metadata_size_roundtrip() {
        let mut hs = ExtensionHandshake::new();
        hs.with_metadata_size(12345);
        assert_eq!(hs.metadata_size(), Some(12345));
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.metadata_size(), Some(12345));
    }

    #[test]
    fn test_handshake_metadata_size_not_present() {
        let hs = ExtensionHandshake::new();
        assert_eq!(hs.metadata_size(), None);
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.metadata_size(), None);
    }

    #[test]
    fn test_handshake_metadata_size_zero_rejected() {
        // metadata_size=0 should be treated as absent (C++ checks size > 0)
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_metadata".to_vec(), BencodeValue::Int(1));
        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        root.insert(b"metadata_size".to_vec(), BencodeValue::Int(0));
        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.metadata_size(), None);
    }

    #[test]
    fn test_handshake_metadata_size_negative_rejected() {
        // Negative metadata_size should be rejected (C++ throws on negative)
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_metadata".to_vec(), BencodeValue::Int(1));
        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        root.insert(b"metadata_size".to_vec(), BencodeValue::Int(-1));
        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.metadata_size(), None);
    }

    #[test]
    fn test_handshake_metadata_size_max_8mib() {
        // 8 MiB is the maximum accepted metadata_size (C++ aria2 limit)
        let max_size = 8 * 1024 * 1024; // 8 MiB
        let mut hs = ExtensionHandshake::new();
        hs.with_metadata_size(max_size);
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.metadata_size(), Some(max_size));
    }

    #[test]
    fn test_handshake_metadata_size_over_8mib_rejected() {
        // metadata_size > 8 MiB should be rejected
        let oversized = 8 * 1024 * 1024 + 1;
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_metadata".to_vec(), BencodeValue::Int(1));
        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        root.insert(
            b"metadata_size".to_vec(),
            BencodeValue::Int(oversized as i64),
        );
        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.metadata_size(), None);
    }

    #[test]
    fn test_handshake_port_roundtrip() {
        let mut hs = ExtensionHandshake::new();
        hs.with_port(6881);
        assert_eq!(hs.port(), Some(6881));
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.port(), Some(6881));
    }

    #[test]
    fn test_handshake_port_not_present() {
        let hs = ExtensionHandshake::new();
        assert_eq!(hs.port(), None);
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.port(), None);
    }

    #[test]
    fn test_handshake_port_zero_rejected() {
        // port=0 should be treated as absent (C++ checks port > 0)
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_metadata".to_vec(), BencodeValue::Int(1));
        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        root.insert(b"p".to_vec(), BencodeValue::Int(0));
        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.port(), None);
    }

    #[test]
    fn test_handshake_port_negative_rejected() {
        // Negative port should be rejected
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_metadata".to_vec(), BencodeValue::Int(1));
        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        root.insert(b"p".to_vec(), BencodeValue::Int(-1));
        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.port(), None);
    }

    #[test]
    fn test_handshake_port_max_valid() {
        // port=65535 should be accepted
        let mut hs = ExtensionHandshake::new();
        hs.with_port(65535);
        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();
        assert_eq!(parsed.port(), Some(65535));
    }

    #[test]
    fn test_handshake_all_fields_roundtrip() {
        // Full handshake with all fields set — mirrors real C++ aria2 output
        let mut hs = ExtensionHandshake::new();
        hs.with_version("test-peer/1.0")
            .with_port(6881)
            .with_metadata_size(45678)
            .with_reqq(1000)
            .with_ut_metadata(3)
            .with_ut_pex(4);

        let bytes = hs.to_bytes();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();

        assert_eq!(parsed.v(), Some("test-peer/1.0"));
        assert_eq!(parsed.port(), Some(6881));
        assert_eq!(parsed.metadata_size(), Some(45678));
        assert_eq!(parsed.reqq(), 1000);
        assert_eq!(parsed.ut_metadata_id(), Some(3));
        assert_eq!(parsed.ut_pex_id(), Some(4));
    }

    #[test]
    fn test_handshake_peer_message_with_all_fields() {
        // Simulate a peer message with all BEP 10 fields, matching C++ wire format
        let mut m_dict = BTreeMap::new();
        m_dict.insert(b"ut_metadata".to_vec(), BencodeValue::Int(2));
        m_dict.insert(b"ut_pex".to_vec(), BencodeValue::Int(3));

        let mut root = BTreeMap::new();
        root.insert(b"m".to_vec(), BencodeValue::Dict(m_dict));
        root.insert(
            b"v".to_vec(),
            BencodeValue::Bytes(b"qBittorrent/4.5.2".to_vec()),
        );
        root.insert(b"p".to_vec(), BencodeValue::Int(6969));
        root.insert(b"metadata_size".to_vec(), BencodeValue::Int(98765));
        root.insert(b"reqq".to_vec(), BencodeValue::Int(250));

        let bytes = BencodeValue::Dict(root).encode();
        let parsed = ExtensionHandshake::from_bytes(&bytes).unwrap();

        assert_eq!(parsed.v(), Some("qBittorrent/4.5.2"));
        assert_eq!(parsed.port(), Some(6969));
        assert_eq!(parsed.metadata_size(), Some(98765));
        assert_eq!(parsed.reqq(), 250);
        assert_eq!(parsed.ut_metadata_id(), Some(2));
        assert_eq!(parsed.ut_pex_id(), Some(3));
    }
}