aria2-core 0.3.8

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
//! Tests for Local Peer Discovery (LPD) Manager - Phase 15 H8
//!
//! Comprehensive tests covering:
//! - BEP 14 LPD announcement format validation
//! - Announcement parsing (valid, invalid, edge cases)
//! - Legacy format backward compatibility
//! - Duplicate suppression
//! - LpdPeer equality and hashing
//! - LpdManager lifecycle operations
//! - Private address detection

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::time::Duration;

use super::{LpdAnnouncer, LpdManager, LpdPeer, is_private_address, parse_lpd_announcement};

// =========================================================================
// Helper Functions
// =========================================================================

/// Create a valid 40-character hex info hash for testing
fn test_info_hash() -> &'static str {
    "0123456789abcdef0123456789abcdef01234567"
}

/// Alternative info hash for multi-hash tests
fn test_info_hash_2() -> &'static str {
    "fedcba9876543210fedcba9876543210fedcba98"
}

/// Test IP address
fn test_ip() -> IpAddr {
    IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100))
}

/// Build a BEP 14 compliant LPD message
fn make_bep14_message(info_hash: &str, port: u16) -> Vec<u8> {
    format!(
        "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: {}\r\nInfohash: {}\r\n\r\n\r\n",
        port, info_hash
    )
    .into_bytes()
}

// =========================================================================
// Test: Multicast interface configuration
// =========================================================================

#[test]
fn test_lpd_announcer_exposes_configured_interface() {
    let announcer = LpdAnnouncer::with_interface(30, Some(Ipv4Addr::LOCALHOST)).unwrap();
    assert_eq!(announcer.interface(), Some(Ipv4Addr::LOCALHOST));
}

// =========================================================================
// Test: BEP 14 Announcement Format
// =========================================================================

#[test]
fn test_lpd_bep14_format() {
    // Build a valid BEP 14 LPD announcement and verify format
    let info_hash = test_info_hash();
    let port = 6881u16;

    let msg = format!(
        "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: {}\r\nInfohash: {}\r\n\r\n\r\n",
        port, info_hash
    );

    // Verify message contains BEP 14 request line
    assert!(
        msg.starts_with("BT-SEARCH * HTTP/1.1\r\n"),
        "Should start with BEP 14 request line"
    );
    assert!(
        msg.contains("Host: 239.192.152.143:6771"),
        "Should contain Host header"
    );
    assert!(
        msg.contains(&format!("Port: {}", port)),
        "Should contain Port header"
    );
    assert!(
        msg.contains(&format!("Infohash: {}", info_hash)),
        "Should contain Infohash header"
    );
    assert!(msg.ends_with("\r\n\r\n\r\n"), "Should end with double CRLF");
}

// =========================================================================
// Test: LPD Receive / Parse BEP 14 Announcements
// =========================================================================

#[test]
fn test_lpd_receive_parses_bep14_announcement() {
    let info_hash = test_info_hash();
    let port = 6882u16;
    let sender_ip = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 42));

    let data = make_bep14_message(info_hash, port);
    let result = parse_lpd_announcement(&data, sender_ip);

    assert!(
        result.is_some(),
        "Valid BEP 14 announcement should parse successfully"
    );

    let peer = result.unwrap();
    assert_eq!(peer.info_hash.as_ref(), info_hash.to_lowercase());
    assert_eq!(peer.port, port);
    assert_eq!(peer.addr, sender_ip);
    assert!(peer.is_local, "10.x.x.x should be detected as local");
}

#[test]
fn test_lpd_receive_parses_bep14_case_insensitive_hash() {
    let sender_ip = test_ip();

    // Mixed case info hash in BEP 14 format
    let data = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: 6881\r\nInfohash: ABCDEFabcdefABCDEFabcdefABCDEFabcdefABCD\r\n\r\n\r\n".to_string().into_bytes();

    let result = parse_lpd_announcement(&data, sender_ip);
    assert!(result.is_some());

    let peer = result.unwrap();
    // Should be normalized to lowercase
    assert_eq!(peer.info_hash.as_ref(), peer.info_hash.to_lowercase());
    assert_eq!(peer.port, 6881);
}

#[test]
fn test_lpd_receive_parses_bep14_extra_whitespace() {
    let sender_ip = test_ip();

    // Extra whitespace around values
    let data = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort:   6881   \r\nInfohash:   0123456789abcdef0123456789abcdef01234567   \r\n\r\n\r\n".to_string().into_bytes();

    let result = parse_lpd_announcement(&data, sender_ip);
    assert!(result.is_some(), "Should handle extra whitespace");

    let peer = result.unwrap();
    assert_eq!(
        peer.info_hash.as_ref(),
        "0123456789abcdef0123456789abcdef01234567"
    );
    assert_eq!(peer.port, 6881);
}

#[test]
fn test_lpd_receive_parses_bep14_unordered_headers() {
    let sender_ip = test_ip();

    // Infohash before Port (C++ uses HttpHeaderProcessor which is order-independent)
    let data = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nInfohash: 0123456789abcdef0123456789abcdef01234567\r\nPort: 6999\r\n\r\n\r\n".to_string().into_bytes();

    let result = parse_lpd_announcement(&data, sender_ip);
    assert!(result.is_some(), "Should handle unordered headers");

    let peer = result.unwrap();
    assert_eq!(peer.port, 6999);
    assert_eq!(
        peer.info_hash.as_ref(),
        "0123456789abcdef0123456789abcdef01234567"
    );
}

// =========================================================================
// Test: Legacy Format Backward Compatibility
// =========================================================================

#[test]
fn test_lpd_receive_parses_legacy_format() {
    let sender_ip = test_ip();

    // Old proprietary format (Hash:/Port:/Token:) should still parse
    let data =
        "Hash: 0123456789abcdef0123456789abcdef01234567\nPort: 6881\nToken: abcdef01\n".as_bytes();

    let result = parse_lpd_announcement(data, sender_ip);
    assert!(
        result.is_some(),
        "Legacy format should be parsed for backward compatibility"
    );

    let peer = result.unwrap();
    assert_eq!(
        peer.info_hash.as_ref(),
        "0123456789abcdef0123456789abcdef01234567"
    );
    assert_eq!(peer.port, 6881);
}

#[test]
fn test_lpd_legacy_format_without_token_still_works() {
    let sender_ip = test_ip();

    // Legacy format without Token field should also parse
    let data = "Hash: 0123456789abcdef0123456789abcdef01234567\nPort: 6881\n".as_bytes();

    let result = parse_lpd_announcement(data, sender_ip);
    assert!(result.is_some(), "Legacy format without Token should parse");

    let peer = result.unwrap();
    assert_eq!(peer.port, 6881);
}

// =========================================================================
// Test: LPD Invalid Announcements Rejected
// =========================================================================

#[test]
fn test_lpd_receive_ignores_invalid() {
    let sender_ip = test_ip();

    // Case 1: Non-UTF8 data
    assert!(
        parse_lpd_announcement(&[0xFF, 0xFE, 0xFD], sender_ip).is_none(),
        "Non-UTF8 should return None"
    );

    // Case 2: Missing Infohash field in BEP14 format
    let no_infohash =
        "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: 6881\r\n\r\n\r\n".as_bytes();
    assert!(
        parse_lpd_announcement(no_infohash, sender_ip).is_none(),
        "Missing Infohash should return None"
    );

    // Case 3: Missing Port field in BEP14 format
    let no_port = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nInfohash: 0123456789abcdef0123456789abcdef01234567\r\n\r\n\r\n".as_bytes();
    assert!(
        parse_lpd_announcement(no_port, sender_ip).is_none(),
        "Missing Port should return None"
    );

    // Case 4: Empty announcement
    assert!(
        parse_lpd_announcement(b"", sender_ip).is_none(),
        "Empty data should return None"
    );

    // Case 5: Only whitespace
    assert!(
        parse_lpd_announcement(b"   \n\t\n  ", sender_ip).is_none(),
        "Whitespace-only should return None"
    );
}

#[test]
fn test_lpd_receive_ignores_invalid_hash_format() {
    let sender_ip = test_ip();

    // Too short (39 chars) in BEP14 format
    let short_hash = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: 6881\r\nInfohash: 0123456789abcdef0123456789abcdef0123456\r\n\r\n\r\n".to_string().into_bytes();
    assert!(
        parse_lpd_announcement(&short_hash, sender_ip).is_none(),
        "Too-short hash should fail"
    );

    // Contains non-hex characters
    let bad_chars = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: 6881\r\nInfohash: gggg56789abcdef0123456789abcdef01234567\r\n\r\n\r\n".to_string().into_bytes();
    assert!(
        parse_lpd_announcement(&bad_chars, sender_ip).is_none(),
        "Non-hex hash should fail"
    );
}

#[test]
fn test_lpd_receive_ignores_invalid_port() {
    let sender_ip = test_ip();

    // Port 0 (invalid) in BEP14 format
    let port_zero = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: 0\r\nInfohash: 0123456789abcdef0123456789abcdef01234567\r\n\r\n\r\n".to_string().into_bytes();
    assert!(
        parse_lpd_announcement(&port_zero, sender_ip).is_none(),
        "Port 0 should be invalid"
    );

    // Non-numeric port in BEP14 format
    let bad_port = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: abc\r\nInfohash: 0123456789abcdef0123456789abcdef01234567\r\n\r\n\r\n".to_string().into_bytes();
    assert!(
        parse_lpd_announcement(&bad_port, sender_ip).is_none(),
        "Non-numeric port should fail"
    );
}

#[test]
fn test_lpd_receive_ignores_unknown_fields() {
    let sender_ip = test_ip();

    // Unknown/extra headers should not cause failure
    let with_extra = "BT-SEARCH * HTTP/1.1\r\nHost: 239.192.152.143:6771\r\nPort: 6881\r\nInfohash: 0123456789abcdef0123456789abcdef01234567\r\nExtraField: value\r\nUnknown: data\r\n\r\n\r\n".to_string().into_bytes();

    let result = parse_lpd_announcement(&with_extra, sender_ip);
    assert!(
        result.is_some(),
        "Extra unknown headers should not prevent parsing"
    );
    assert_eq!(result.unwrap().port, 6881);
}

// =========================================================================
// Test: Duplicate Suppression
// =========================================================================

#[test]
fn test_lpd_duplicate_suppression_same_hash_and_ip() {
    let ip = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 50));

    // Same announcement from same IP twice
    let data = make_bep14_message("0123456789abcdef0123456789abcdef01234567", 6881);

    let peer1 = parse_lpd_announcement(&data, ip);
    let peer2 = parse_lpd_announcement(&data, ip);

    assert!(peer1.is_some());
    assert!(peer2.is_some());

    // Same info_hash + same IP = equal peers (for dedup)
    let p1 = peer1.unwrap();
    let p2 = peer2.unwrap();
    assert_eq!(p1, p2, "Same hash+IP should produce equal peers");
}

#[test]
fn test_lpd_duplicate_suppression_different_ports_ok() {
    let ip = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 51));

    let data1 = make_bep14_message("0123456789abcdef0123456789abcdef01234567", 6881);
    let data2 = make_bep14_message("0123456789abcdef0123456789abcdef01234567", 6882);

    let p1 = parse_lpd_announcement(&data1, ip).unwrap();
    let p2 = parse_lpd_announcement(&data2, ip).unwrap();

    // Our Eq implementation uses (info_hash, addr) only, so these are equal
    assert_eq!(
        p1, p2,
        "Same hash+IP with different ports should still be equal for dedup"
    );
}

#[test]
fn test_lpd_different_hashes_not_duplicates() {
    let ip = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 52));

    let data1 = make_bep14_message(test_info_hash(), 6881);
    let data2 = make_bep14_message(test_info_hash_2(), 6881);

    let p1 = parse_lpd_announcement(&data1, ip).unwrap();
    let p2 = parse_lpd_announcement(&data2, ip).unwrap();

    // Different hashes = different peers even from same IP
    assert_ne!(p1, p2, "Different info_hashes should not be duplicates");
}

#[test]
fn test_lpd_different_ips_not_duplicates() {
    let ip1 = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 53));
    let ip2 = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 54));

    let data = make_bep14_message("0123456789abcdef0123456789abcdef01234567", 6881);

    let p1 = parse_lpd_announcement(&data, ip1).unwrap();
    let p2 = parse_lpd_announcement(&data, ip2).unwrap();

    assert_ne!(p1, p2, "Different IPs should not be duplicates");
}

// =========================================================================
// Test: LpdPeer Properties
// =========================================================================

#[test]
fn test_lpd_peer_creation() {
    let peer = LpdPeer::new(
        "abc123def456abc123def456abc123def456abcd",
        6881,
        IpAddr::V4(Ipv4Addr::LOCALHOST),
    );

    assert_eq!(
        peer.info_hash.as_ref(),
        "abc123def456abc123def456abc123def456abcd"
    );
    assert_eq!(peer.port, 6881);
    assert_eq!(peer.addr, IpAddr::V4(Ipv4Addr::LOCALHOST));
    assert!(peer.is_local, "127.0.0.1 should be detected as local");
    assert!(!peer.is_expired(Duration::from_secs(99999)));
}

#[test]
fn test_lpd_peer_socket_addr() {
    let peer = LpdPeer::new("hash", 6881, IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)));
    let sa = peer.socket_addr();
    assert_eq!(sa.ip(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)));
    assert_eq!(sa.port(), 6881);
}

#[test]
fn test_lpd_peer_expiration() {
    let peer = LpdPeer::new("hash", 6881, test_ip());

    // Freshly created peer should not be expired
    assert!(!peer.is_expired(Duration::from_secs(60)));

    // Peer should be "expired" after max_age of 0 seconds (since last_seen is now)
    assert!(peer.is_expired(Duration::ZERO));
}

#[test]
fn test_lpd_peer_hash_equality_for_set_dedup() {
    use std::collections::HashSet;

    let ip = IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4));
    let mut set = HashSet::new();

    let peer1 = LpdPeer::new(test_info_hash(), 6881, ip);
    let peer2 = LpdPeer::new(test_info_hash(), 6882, ip); // Same hash+IP, different port

    set.insert(peer1.clone());
    set.insert(peer2.clone());

    // Should only have one entry due to dedup by (info_hash, addr)
    assert_eq!(set.len(), 1, "Same hash+IP should dedup in HashSet");
}

// =========================================================================
// Test: Private Address Detection
// =========================================================================

#[test]
fn test_is_private_address_ipv4() {
    // 10.0.0.0/8
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))));
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::new(
        10, 255, 255, 255
    ))));

    // 172.16.0.0/12
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::new(
        172, 16, 0, 1
    ))));
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::new(
        172, 31, 255, 255
    ))));

    // 192.168.0.0/16
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::new(
        192, 168, 0, 1
    ))));
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::new(
        192, 168, 255, 255
    ))));

    // 127.0.0.0/8 (loopback)
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::LOCALHOST)));

    // 169.254.0.0/16 (link-local)
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::new(
        169, 254, 0, 1
    ))));
    assert!(is_private_address(&IpAddr::V4(Ipv4Addr::new(
        169, 254, 255, 255
    ))));

    // NOT private
    assert!(!is_private_address(&IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))));
    assert!(!is_private_address(&IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4))));
    assert!(!is_private_address(&IpAddr::V4(Ipv4Addr::new(
        172, 15, 0, 1
    ))));
    assert!(!is_private_address(&IpAddr::V4(Ipv4Addr::new(
        172, 32, 0, 1
    ))));
}

#[test]
fn test_is_private_address_ipv6() {
    // fc00::/7 (unique local)
    assert!(is_private_address(&IpAddr::V6(Ipv6Addr::new(
        0xfc00, 0, 0, 0, 0, 0, 0, 1
    ))));
    assert!(is_private_address(&IpAddr::V6(Ipv6Addr::new(
        0xfdff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
    ))));

    // ::1 (loopback)
    assert!(is_private_address(&IpAddr::V6(Ipv6Addr::LOCALHOST)));

    // NOT private
    assert!(!is_private_address(&IpAddr::V6(Ipv6Addr::new(
        0x2001, 0xdb8, 0, 0, 0, 0, 0, 1
    ))));
}

// =========================================================================
// Test: LpdAnnouncer Validation
// =========================================================================

#[test]
fn test_lpd_announcer_rejects_bad_info_hash() {
    let invalid_hashes = vec![
        "",                                                        // Empty
        "short",                                                   // Too short
        "0123456789012345678901234567890123456789012345678",       // Too long (49)
        "ghijklmnopqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghij", // Non-hex
    ];

    for hash in invalid_hashes {
        let is_valid = hash.len() == 40 && hash.chars().all(|c| c.is_ascii_hexdigit());
        assert!(!is_valid, "Hash '{}' should be invalid", hash);
    }
}

#[test]
fn test_lpd_announcer_accepts_valid_info_hash() {
    let valid_hashes = vec![
        "0123456789abcdef0123456789abcdef01234567",
        "FEDCBA9876543210FEDCBA9876543210FEDCBA98",
        "abcdefABCDEF1234abcdefABCDEF1234abcdef12",
    ];

    for hash in valid_hashes {
        let is_valid = hash.len() == 40 && hash.chars().all(|c| c.is_ascii_hexdigit());
        assert!(is_valid, "Hash '{}' should be valid", hash);
    }
}

// =========================================================================
// Test: LpdManager Operations
// =========================================================================

#[tokio::test]
async fn test_lpd_manager_register_unregister() {
    let manager = LpdManager::default();

    // Register a torrent
    manager
        .register_torrent(test_info_hash(), false)
        .await
        .unwrap();

    // Check active hashes
    let active = manager.active_hashes.read().await;
    assert!(
        active.contains(test_info_hash()),
        "Torrent should be registered"
    );
    drop(active);

    // Unregister
    manager.unregister_torrent(test_info_hash()).await;

    let active = manager.active_hashes.read().await;
    assert!(
        !active.contains(test_info_hash()),
        "Torrent should be unregistered"
    );
    drop(active);

    assert!(
        !manager.peers.read().await.contains_key(test_info_hash()),
        "Peer registry entry should be removed with the torrent"
    );
}

#[tokio::test]
async fn test_lpd_manager_get_peers_empty_initially() {
    let manager = LpdManager::default();

    let peers = manager.get_peers_for(test_info_hash()).await;
    assert!(peers.is_empty(), "No peers should exist initially");
}

#[tokio::test]
async fn test_lpd_manager_update_and_get_peers() {
    let manager = LpdManager::default();

    // Register first
    manager
        .register_torrent(test_info_hash(), false)
        .await
        .unwrap();

    // Add some discovered peers
    let new_peers = vec![
        LpdPeer::new(
            test_info_hash(),
            6881,
            IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)),
        ),
        LpdPeer::new(
            test_info_hash(),
            6882,
            IpAddr::V4(Ipv4Addr::new(10, 0, 0, 2)),
        ),
        LpdPeer::new(
            test_info_hash(),
            6883,
            IpAddr::V4(Ipv4Addr::new(10, 0, 0, 3)),
        ),
    ];

    manager.update_peers(test_info_hash(), new_peers).await;

    let peers = manager.get_peers_for(test_info_hash()).await;
    assert_eq!(peers.len(), 3, "Should have 3 stored peers");
}

#[tokio::test]
async fn test_lpd_manager_multiple_torrents_independent() {
    let manager = LpdManager::default();

    manager
        .register_torrent(test_info_hash(), false)
        .await
        .unwrap();
    manager
        .register_torrent(test_info_hash_2(), false)
        .await
        .unwrap();

    // Add peers to first torrent
    manager
        .update_peers(
            test_info_hash(),
            vec![LpdPeer::new(
                test_info_hash(),
                6881,
                IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)),
            )],
        )
        .await;

    // Add peers to second torrent
    manager
        .update_peers(
            test_info_hash_2(),
            vec![LpdPeer::new(
                test_info_hash_2(),
                6999,
                IpAddr::V4(Ipv4Addr::new(10, 0, 0, 2)),
            )],
        )
        .await;

    let peers1 = manager.get_peers_for(test_info_hash()).await;
    let peers2 = manager.get_peers_for(test_info_hash_2()).await;

    assert_eq!(peers1.len(), 1, "First torrent should have 1 peer");
    assert_eq!(peers2.len(), 1, "Second torrent should have 1 peer");
    assert_ne!(
        peers1[0].info_hash, peers2[0].info_hash,
        "Peers should be for different torrents"
    );
}

#[tokio::test]
async fn test_lpd_manager_cleanup_expired_peers() {
    let manager = LpdManager::default();
    manager
        .register_torrent(test_info_hash(), false)
        .await
        .unwrap();

    // Add a peer that's immediately "expired" (max_age = 0)
    let peer = LpdPeer::new(test_info_hash(), 6881, test_ip());
    manager.update_peers(test_info_hash(), vec![peer]).await;

    // Clean up with zero tolerance
    let removed = manager.cleanup_expired_peers(Duration::ZERO).await;
    assert!(removed > 0, "Should remove expired peers");

    let remaining = manager.get_peers_for(test_info_hash()).await;
    assert!(remaining.is_empty(), "Expired peers should be removed");
}

#[tokio::test]
async fn test_lpd_manager_is_available() {
    let manager = LpdManager::default();
    assert!(
        manager.is_available(),
        "Default manager should be available"
    );
}

#[tokio::test]
async fn test_lpd_registration_rejects_zero_tcp_port() {
    let manager = LpdManager::default();

    let error = manager
        .register_torrent_with_port(test_info_hash(), false, 0)
        .await
        .expect_err("LPD must not register a torrent without a BT TCP port");

    assert!(error.contains("non-zero"));
    assert!(
        !manager
            .active_hashes
            .read()
            .await
            .contains(test_info_hash())
    );
    assert_eq!(manager.announce_port_for(test_info_hash()).await, None);
}

#[tokio::test]
async fn test_lpd_manual_announce_rejects_zero_tcp_port() {
    let manager = LpdManager::default();

    let error = manager
        .announce_torrent(test_info_hash(), 0)
        .await
        .expect_err("LPD must not announce without a BitTorrent TCP port");

    assert!(error.contains("non-zero"));
}

#[tokio::test]
async fn test_lpd_registration_keeps_each_torrents_tcp_port() {
    let manager = LpdManager::default();

    manager
        .register_torrent_with_port(test_info_hash(), false, 41_001)
        .await
        .expect("first torrent should register");
    manager
        .register_torrent_with_port(test_info_hash_2(), false, 41_002)
        .await
        .expect("second torrent should register");

    assert_eq!(
        manager.announce_port_for(test_info_hash()).await,
        Some(41_001)
    );
    assert_eq!(
        manager.announce_port_for(test_info_hash_2()).await,
        Some(41_002)
    );
}

#[tokio::test]
async fn test_lpd_background_announce_has_one_process_task() {
    let manager = LpdManager::with_interval(60).expect("LPD manager should initialize");
    manager
        .register_torrent_with_port(test_info_hash(), false, 41_003)
        .await
        .expect("torrent should register");

    assert!(manager.start_background_announce(0).is_none());
    assert!(manager.is_background_announce_running());

    assert!(
        manager.start_background_announce(0).is_none(),
        "duplicate start must not create another process-level announce task"
    );
    assert!(manager.is_background_announce_running());

    manager.stop_background_announce();
    assert!(!manager.is_background_announce_running());
}

#[tokio::test]
async fn test_lpd_private_torrent_rejected() {
    // Per BEP 0027, private torrents must NOT be announced via LPD
    let manager = LpdManager::default();

    // Public torrent should register fine
    let result = manager.register_torrent(test_info_hash(), false).await;
    assert!(result.is_ok(), "Public torrent should register for LPD");

    // Private torrent should be rejected
    let private_hash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    let result = manager.register_torrent(private_hash, true).await;
    assert!(
        result.is_err(),
        "Private torrent should be rejected from LPD"
    );
    assert!(
        result.unwrap_err().contains("BEP 0027"),
        "Error should reference BEP 0027"
    );

    // Verify the private hash was NOT added to active set
    let active = manager.active_hashes.read().await;
    assert!(
        !active.contains(private_hash),
        "Private hash should not be in active LPD set"
    );
}