aria2-core 0.2.2

High-performance download engine core: multi-protocol segmented downloads, rate limiting, config management, 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
//! DNS Cache Module
//!
//! Provides DNS resolution caching with TTL support, negative caching for failed
//! lookups (to prevent retry storms), and IPv4/IPv6 preference sorting.
//!
//! # Features
//!
//! - **TTL-based expiration**: Cached entries expire after a configurable time-to-live
//! - **Negative caching**: Failed lookups are remembered to avoid immediate retries
//! - **IPv4 preference**: Addresses can be sorted with IPv4 first (matching C++ aria2 behavior)
//! - **Dependency injection**: Cache instances are created during engine initialization
//!   and passed down, avoiding global mutable state
//!
//! # Example
//!
//! ```rust,no_run
//! use aria2_core::dns::dns_cache::DnsCache;
//!
//! #[tokio::main]
//! async fn main() {
//!     let mut cache = DnsCache::with_ttl(300, 60);
//!     match cache.resolve("example.com", 80).await {
//!         Ok(addrs) => println!("Resolved: {:?}", addrs),
//!         Err(e) => eprintln!("DNS error: {}", e),
//!     }
//! }
//! ```

use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};
use std::time::{Duration, Instant};

use hickory_resolver::TokioAsyncResolver;
use hickory_resolver::config::{ResolverConfig, ResolverOpts};

/// A single cached DNS entry containing resolved addresses and metadata.
///
/// Each entry stores the resolved socket addresses for a hostname,
/// along with when it was resolved and its time-to-live duration.
#[derive(Debug, Clone)]
pub struct DnsEntry {
    /// The hostname this entry was resolved for
    pub hostname: String,
    /// Resolved socket addresses (sorted by preference)
    pub addresses: Vec<SocketAddr>,
    /// Timestamp when this entry was created/resolved
    pub resolved_at: Instant,
    /// Time-to-live for this entry before it's considered stale
    pub ttl: Duration,
    /// Whether IPv4 addresses should be preferred in ordering
    pub ipv4_preferred: bool,
}

impl DnsEntry {
    /// Check if this DNS entry has expired based on its TTL.
    ///
    /// Returns `true` if the elapsed time since resolution exceeds the TTL,
    /// meaning the entry should be re-resolved.
    pub fn is_expired(&self) -> bool {
        self.resolved_at.elapsed() > self.ttl
    }

    /// Get the best address from this entry.
    ///
    /// If IPv4 is preferred, returns the first IPv4 address if available,
    /// otherwise falls back to the first address in the list.
    /// Returns `None` if there are no addresses.
    pub fn best_address(&self) -> Option<SocketAddr> {
        if self.addresses.is_empty() {
            return None;
        }
        if self.ipv4_preferred {
            self.addresses
                .iter()
                .find(|a| matches!(a.ip(), IpAddr::V4(_)))
                .copied()
                .or_else(|| self.addresses.first().copied())
        } else {
            Some(self.addresses[0])
        }
    }

    /// Return a clone of all cached addresses for this entry.
    pub fn all_addresses(&self) -> Vec<SocketAddr> {
        self.addresses.clone()
    }
}

/// A DNS resolution cache with TTL support and negative caching.
///
/// This cache stores resolved DNS entries and avoids repeated lookups
/// for the same hostname within the TTL window. It also implements
/// negative caching for failed lookups to prevent retry storms.
///
/// # Thread Safety
///
/// For use in async contexts, wrap with `tokio::sync::Mutex`.
/// Instances should be created during engine initialization and passed
/// down via dependency injection rather than using global singletons.
pub struct DnsCache {
    /// Cache of successful DNS resolutions: hostname -> DnsEntry
    cache: HashMap<String, DnsEntry>,
    /// Default TTL for successfully resolved entries
    default_ttl: Duration,
    /// TTL for failed/negative lookups (prevents retry storms)
    negative_ttl: Duration,
    /// Negative cache: hostname -> timestamp of last failed lookup
    negative_entries: HashMap<String, Instant>,
    /// Whether to prefer IPv4 addresses when sorting results
    ipv4_preference: bool,
    /// Fully-async hickory DNS resolver. When `None` (or on lookup error) resolution
    /// falls back to `tokio::net::lookup_host`. `TokioAsyncResolver` is `Arc`-backed
    /// internally, so cloning is cheap and no outer `Arc` is required.
    resolver: Option<TokioAsyncResolver>,
}

impl DnsCache {
    /// Create a new DNS cache with default settings.
    ///
    /// Default values:
    /// - TTL: 300 seconds (5 minutes)
    /// - Negative TTL: 60 seconds (1 minute)
    /// - IPv4 preference: enabled
    pub fn new() -> Self {
        Self {
            cache: HashMap::new(),
            default_ttl: Duration::from_secs(300), // 5 minutes default
            negative_ttl: Duration::from_secs(60), // 1 minute for failures
            negative_entries: HashMap::new(),
            ipv4_preference: true, // Prefer IPv4 by default (like C++ aria2)
            resolver: build_default_resolver(),
        }
    }

    /// Create a new DNS cache with custom TTL values.
    ///
    /// # Arguments
    ///
    /// * `default_ttl_secs` - Time-to-live for successful resolutions (in seconds)
    /// * `negative_ttl_secs` - Time-to-live for failed lookups (in seconds)
    pub fn with_ttl(default_ttl_secs: u64, negative_ttl_secs: u64) -> Self {
        Self {
            default_ttl: Duration::from_secs(default_ttl_secs),
            negative_ttl: Duration::from_secs(negative_ttl_secs),
            ..Self::new()
        }
    }

    /// Inject a custom hickory resolver (useful for testing with a configured/mock resolver).
    ///
    /// This replaces the default resolver built during construction. The TTL settings and
    /// IPv4 preference are preserved.
    pub fn with_resolver(mut self, resolver: TokioAsyncResolver) -> Self {
        self.resolver = Some(resolver);
        self
    }

    /// Resolve a hostname to socket addresses, using cache if valid.
    ///
    /// Resolution strategy:
    /// 1. Check positive cache — return immediately if entry exists and is not expired
    /// 2. Check negative cache — return error if lookup recently failed
    /// 3. Try the fully-async hickory resolver (no blocking getaddrinfo); on success the
    ///    record TTL is capped at `default_ttl` and the result is cached
    /// 4. If hickory is unavailable or errors, fall back to `tokio::net::lookup_host`
    ///    (blocking getaddrinfo on a tokio thread pool) so behavior stays robust
    /// 5. On success: sort addresses by IPv4 preference, cache result, return
    /// 6. On failure: record in negative cache, return error
    ///
    /// # Arguments
    ///
    /// * `hostname` - The hostname to resolve (e.g., "example.com")
    /// * `port` - The port number to include in resolved addresses
    ///
    /// # Returns
    ///
    /// A vector of resolved `SocketAddr` on success, or an error string on failure.
    pub async fn resolve(&mut self, hostname: &str, port: u16) -> Result<Vec<SocketAddr>, String> {
        // 1. Check positive cache first
        if let Some(entry) = self.cache.get(hostname)
            && !entry.is_expired()
        {
            return Ok(entry.all_addresses());
        }

        // 2. Check negative cache (recently failed lookup)
        if let Some(failed_at) = self.negative_entries.get(hostname)
            && failed_at.elapsed() < self.negative_ttl
        {
            return Err(format!(
                "DNS lookup recently failed for {} (retry after {:?})",
                hostname,
                self.negative_ttl.saturating_sub(failed_at.elapsed())
            ));
        }

        // 3. Try the fully-async hickory resolver first (avoids blocking getaddrinfo).
        if let Some(resolver) = self.resolver.as_ref() {
            match resolver.lookup_ip(hostname).await {
                Ok(lookup) => {
                    let mut addrs: Vec<SocketAddr> =
                        lookup.iter().map(|ip| SocketAddr::new(ip, port)).collect();

                    if !addrs.is_empty() {
                        // Derive TTL from the lookup's remaining validity, capped at default_ttl.
                        let record_ttl = lookup
                            .valid_until()
                            .saturating_duration_since(Instant::now());
                        let actual_ttl = record_ttl.min(self.default_ttl);

                        if self.ipv4_preference {
                            addrs.sort_by_key(|a| match a.ip() {
                                IpAddr::V4(_) => 0u8,
                                IpAddr::V6(_) => 1u8,
                            });
                        }

                        let hostname_owned = hostname.to_string();
                        let entry = DnsEntry {
                            hostname: hostname_owned.clone(),
                            addresses: addrs.clone(),
                            resolved_at: Instant::now(),
                            ttl: actual_ttl,
                            ipv4_preferred: self.ipv4_preference,
                        };
                        self.cache.insert(hostname_owned, entry);
                        self.negative_entries.remove(hostname);

                        tracing::trace!(
                            hostname = hostname,
                            addr_count = addrs.len(),
                            ttl_secs = actual_ttl.as_secs(),
                            "DNS resolved via hickory async resolver"
                        );
                        return Ok(addrs);
                    }

                    tracing::debug!(
                        hostname = hostname,
                        "hickory returned no addresses, falling back to tokio::net::lookup_host"
                    );
                    // Fall through to the OS-level fallback below.
                }
                Err(e) => {
                    tracing::debug!(
                        hostname = hostname,
                        error = %e,
                        "hickory DNS lookup failed, falling back to tokio::net::lookup_host"
                    );
                    // Fall through to the OS-level fallback below.
                }
            }
        }

        // 4. Fallback: OS-level resolution via tokio (blocking getaddrinfo on a thread pool).
        //    Used when the hickory resolver is unavailable, returns no addresses, or errors.
        let addr_str = format!("{}:{}", hostname, port);
        match tokio::net::lookup_host(&addr_str).await {
            Ok(addrs) => {
                let mut sorted: Vec<SocketAddr> = addrs.collect();

                // Sort: IPv4 first if preferred, then by address family
                if self.ipv4_preference {
                    sorted.sort_by_key(|a| match a.ip() {
                        IpAddr::V4(_) => 0u8,
                        IpAddr::V6(_) => 1u8,
                    });
                }

                let hostname_owned = hostname.to_string();
                let entry = DnsEntry {
                    hostname: hostname_owned.clone(),
                    addresses: sorted.clone(),
                    resolved_at: Instant::now(),
                    ttl: self.default_ttl,
                    ipv4_preferred: self.ipv4_preference,
                };
                self.cache.insert(hostname_owned, entry);
                self.negative_entries.remove(hostname);

                tracing::trace!(
                    hostname = hostname,
                    addr_count = sorted.len(),
                    "DNS resolved via tokio::net::lookup_host fallback"
                );
                Ok(sorted)
            }
            Err(e) => {
                // Record failure in negative cache to prevent retry storms
                self.negative_entries
                    .insert(hostname.to_string(), Instant::now());
                tracing::debug!(
                    hostname = hostname,
                    error = %e,
                    "DNS resolution failed via fallback"
                );
                Err(e.to_string())
            }
        }
    }

    /// Force refresh a specific hostname, bypassing any cached entry.
    ///
    /// This removes any existing cache entry for the hostname and performs
    /// a fresh DNS resolution. Useful when you know the DNS records may have changed.
    ///
    /// # Arguments
    ///
    /// * `hostname` - The hostname to re-resolve
    /// * `port` - The port number for resolved addresses
    pub async fn force_refresh(
        &mut self,
        hostname: &str,
        port: u16,
    ) -> Result<Vec<SocketAddr>, String> {
        self.cache.remove(hostname);
        self.resolve(hostname, port).await
    }

    /// Clear all cached entries (both positive and negative).
    pub fn clear(&mut self) {
        self.cache.clear();
        self.negative_entries.clear();
    }

    /// Remove expired entries from the cache.
    ///
    /// Call this periodically (e.g., every few minutes) to reclaim memory
    /// from stale entries. Also cleans up expired negative cache entries.
    ///
    /// # Returns
    ///
    /// The number of entries that were removed.
    pub fn purge_expired(&mut self) -> usize {
        let before = self.cache.len();
        self.cache.retain(|_, v| !v.is_expired());
        self.negative_entries
            .retain(|_, t| t.elapsed() < self.negative_ttl);
        before - self.cache.len()
    }

    /// Set whether IPv4 addresses should be preferred over IPv6.
    ///
    /// When enabled, resolved addresses are sorted with IPv4 addresses first.
    /// This matches the behavior of C++ aria2 which prefers IPv4 by default.
    pub fn set_ipv4_preference(&mut self, prefer_ipv4: bool) {
        self.ipv4_preference = prefer_ipv4;
    }

    /// Get the number of currently cached (non-expired) entries.
    pub fn len(&self) -> usize {
        self.cache.len()
    }

    /// Check if the cache contains no entries.
    pub fn is_empty(&self) -> bool {
        self.cache.is_empty()
    }

    /// Get the default TTL setting.
    pub fn default_ttl(&self) -> Duration {
        self.default_ttl
    }

    /// Get the negative TTL setting.
    pub fn negative_ttl(&self) -> Duration {
        self.negative_ttl
    }

    /// Manually record a failed lookup in the negative cache.
    ///
    /// This is useful for testing negative cache behavior without
    /// depending on network DNS resolution.
    pub fn record_failure(&mut self, hostname: &str) {
        self.negative_entries
            .insert(hostname.to_string(), Instant::now());
    }

    /// Check cache only (no network resolution).
    ///
    /// Returns cached addresses if available, or an error if the entry
    /// is in the negative cache or not cached at all. This is useful
    /// for testing cache behavior without network dependencies.
    pub fn resolve_no_network(
        &mut self,
        hostname: &str,
        port: u16,
    ) -> Result<Vec<SocketAddr>, String> {
        // 1. Check positive cache first
        if let Some(entry) = self.cache.get(hostname)
            && !entry.is_expired()
        {
            return Ok(entry.all_addresses());
        }

        // 2. Check negative cache (recently failed lookup)
        if let Some(failed_at) = self.negative_entries.get(hostname)
            && failed_at.elapsed() < self.negative_ttl
        {
            return Err(format!(
                "DNS lookup recently failed for {} (retry after {:?})",
                hostname,
                self.negative_ttl.saturating_sub(failed_at.elapsed())
            ));
        }

        Err(format!("No cached entry for {}:{}", hostname, port))
    }
}

/// Build the default hickory async resolver.
///
/// Uses the default upstream configuration (Google Public DNS via `ResolverConfig::default()`)
/// and enables `use_hosts_file` so that names present in the system hosts file (e.g.
/// `localhost`) are answered immediately without any network I/O — mirroring the behavior of
/// `getaddrinfo` used by `tokio::net::lookup_host`.
///
/// `TokioAsyncResolver::tokio` returns a ready handle (it does not spawn a background task at
/// construction time and captures no runtime handle), so this is safe to call outside of an
/// async context. The returned value is wrapped in `Some`; callers fall back to
/// `tokio::net::lookup_host` whenever the resolver is `None`.
fn build_default_resolver() -> Option<TokioAsyncResolver> {
    let mut opts = ResolverOpts::default();
    opts.use_hosts_file = true;
    Some(TokioAsyncResolver::tokio(ResolverConfig::default(), opts))
}

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

// ==================== Tests ====================

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

    /// Helper: create a test cache with very short TTLs for fast testing
    fn create_test_cache() -> DnsCache {
        DnsCache::with_ttl(10, 1) // 10s positive TTL, 1s negative TTL
    }

    #[test]
    fn test_dns_entry_is_expired() {
        let entry = DnsEntry {
            hostname: "test.com".to_string(),
            addresses: vec![],
            resolved_at: Instant::now(),
            ttl: Duration::from_secs(60),
            ipv4_preferred: true,
        };
        assert!(!entry.is_expired());

        let expired_entry = DnsEntry {
            hostname: "old.com".to_string(),
            addresses: vec![],
            resolved_at: Instant::now() - Duration::from_secs(61),
            ttl: Duration::from_secs(60),
            ipv4_preferred: false,
        };
        assert!(expired_entry.is_expired());
    }

    #[test]
    fn test_dns_entry_best_address_ipv4_preferred() {
        let ipv6_addr: SocketAddr = "[::1]:8080".parse().unwrap();
        let ipv4_addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();

        let entry = DnsEntry {
            hostname: "mixed.com".to_string(),
            addresses: vec![ipv6_addr, ipv4_addr], // IPv6 first
            resolved_at: Instant::now(),
            ttl: Duration::from_secs(60),
            ipv4_preferred: true,
        };

        // Should prefer IPv4 even though it's second in list
        let best = entry.best_address().unwrap();
        assert_eq!(best, ipv4_addr);
    }

    #[test]
    fn test_dns_entry_best_address_no_ipv4() {
        let ipv6_addr: SocketAddr = "[::1]:8080".parse().unwrap();

        let entry = DnsEntry {
            hostname: "ipv6only.com".to_string(),
            addresses: vec![ipv6_addr],
            resolved_at: Instant::now(),
            ttl: Duration::from_secs(60),
            ipv4_preferred: true,
        };

        let best = entry.best_address().unwrap();
        assert_eq!(best, ipv6_addr);
    }

    #[test]
    fn test_dns_entry_best_address_empty() {
        let entry = DnsEntry {
            hostname: "empty.com".to_string(),
            addresses: vec![],
            resolved_at: Instant::now(),
            ttl: Duration::from_secs(60),
            ipv4_preferred: true,
        };

        assert!(entry.best_address().is_none());
    }

    #[test]
    fn test_dns_cache_creation() {
        let cache = DnsCache::new();
        assert!(cache.is_empty());
        assert_eq!(cache.default_ttl(), Duration::from_secs(300));
        assert_eq!(cache.negative_ttl(), Duration::from_secs(60));
    }

    #[test]
    fn test_dns_cache_with_custom_ttl() {
        let cache = DnsCache::with_ttl(600, 30);
        assert_eq!(cache.default_ttl(), Duration::from_secs(600));
        assert_eq!(cache.negative_ttl(), Duration::from_secs(30));
    }

    #[test]
    fn test_dns_cache_clear() {
        let mut cache = create_test_cache();
        // Manually insert something into the cache
        let entry = DnsEntry {
            hostname: "example.com".to_string(),
            addresses: vec!["127.0.0.1:80".parse().unwrap()],
            resolved_at: Instant::now(),
            ttl: Duration::from_secs(60),
            ipv4_preferred: true,
        };
        cache.cache.insert("example.com".to_string(), entry);
        cache
            .negative_entries
            .insert("failed.com".to_string(), Instant::now());

        assert_eq!(cache.len(), 1);
        cache.clear();
        assert!(cache.is_empty());
        assert!(cache.negative_entries.is_empty());
    }

    /// Test J3.4 #1: Second call returns cached result without network I/O.
    ///
    /// We use localhost which resolves instantly and verify that once cached,
    /// subsequent calls return the same data without needing actual network calls.
    /// Since we can't easily mock tokio::net::lookup_host in unit tests,
    /// we verify the caching mechanism directly by manipulating the internal state.
    #[tokio::test]
    async fn test_resolve_caches_result() {
        let mut cache = create_test_cache();

        // Resolve localhost (should always succeed)
        let result1 = cache.resolve("localhost", 80).await;
        assert!(
            result1.is_ok(),
            "First resolve of localhost should succeed: {:?}",
            result1.err()
        );
        let addrs1 = result1.unwrap();
        assert!(
            !addrs1.is_empty(),
            "localhost should resolve to at least one address"
        );

        // Second resolve should hit cache (same result, no network call)
        let result2 = cache.resolve("localhost", 80).await;
        assert!(result2.is_ok(), "Second resolve should succeed from cache");
        let addrs2 = result2.unwrap();
        assert_eq!(
            addrs1, addrs2,
            "Cached result should match original resolution"
        );

        // Verify cache now has exactly one entry
        assert_eq!(cache.len(), 1);
    }

    /// Test J3.4 #2: Failed lookup blocks retry for negative_ttl duration.
    ///
    /// We inject a negative cache entry directly (without network DNS)
    /// and verify that subsequent attempts within the negative TTL window
    /// fail immediately with the "recently failed" message.
    #[test]
    fn test_negative_cache_blocks_retry() {
        let mut cache = DnsCache::with_ttl(300, 2); // 2-second negative TTL

        // Inject a negative cache entry directly (no network dependency)
        cache.record_failure("test-host.invalid");

        // Immediate retry should be blocked by negative cache
        let result = cache.resolve_no_network("test-host.invalid", 80);
        assert!(
            result.is_err(),
            "Lookup should be blocked by negative cache"
        );
        let err_msg = result.unwrap_err();
        assert!(
            err_msg.contains("recently failed"),
            "Error should mention recent failure: {}",
            err_msg
        );
    }

    /// Test J3.4 #3: Expired entries are removed by purge_expired().
    ///
    /// We insert entries with already-expired timestamps and verify that
    /// purge_expired removes them while keeping valid entries intact.
    #[test]
    fn test_purge_expired_removes_old() {
        let mut cache = DnsCache::with_ttl(1, 60); // 1-second TTL

        // Insert an already-expired entry
        let expired_entry = DnsEntry {
            hostname: "expired.example.com".to_string(),
            addresses: vec!["10.0.0.1:80".parse().unwrap()],
            resolved_at: Instant::now() - Duration::from_secs(5), // Expired 5 seconds ago
            ttl: Duration::from_secs(1),
            ipv4_preferred: true,
        };
        cache
            .cache
            .insert("expired.example.com".to_string(), expired_entry);

        // Insert a still-valid entry
        let fresh_entry = DnsEntry {
            hostname: "fresh.example.com".to_string(),
            addresses: vec!["10.0.0.2:80".parse().unwrap()],
            resolved_at: Instant::now(),
            ttl: Duration::from_secs(3600), // 1 hour TTL
            ipv4_preferred: true,
        };
        cache
            .cache
            .insert("fresh.example.com".to_string(), fresh_entry);

        assert_eq!(cache.len(), 2, "Should have 2 entries before purge");

        let removed = cache.purge_expired();
        assert_eq!(removed, 1, "Should remove exactly 1 expired entry");
        assert_eq!(cache.len(), 1, "Should have 1 entry remaining");
        assert!(
            cache.cache.contains_key("fresh.example.com"),
            "Fresh entry should still exist"
        );
        assert!(
            !cache.cache.contains_key("expired.example.com"),
            "Expired entry should be removed"
        );
    }

    /// Test J3.4 #4: IPv4 addresses come first when ipv4_preference is enabled.
    ///
    /// Verifies that when IPv4 preference is set, the DnsCache sorts resolved
    /// addresses so that IPv4 addresses appear before IPv6 addresses.
    #[tokio::test]
    async fn test_ipv4_preferred_sorting() {
        let mut cache = create_test_cache();

        // Resolve localhost which typically returns both [::1] and 127.0.0.1
        let result = cache.resolve("localhost", 8080).await;
        assert!(
            result.is_ok(),
            "localhost resolution should succeed: {:?}",
            result.err()
        );
        let addrs = result.unwrap();

        // If we have both IPv4 and IPv6 addresses, IPv4 should come first
        let has_ipv4 = addrs.iter().any(|a| matches!(a.ip(), IpAddr::V4(_)));
        let has_ipv6 = addrs.iter().any(|a| matches!(a.ip(), IpAddr::V6(_)));

        if has_ipv4 && has_ipv6 {
            let first_ipv4_pos = addrs
                .iter()
                .position(|a| matches!(a.ip(), IpAddr::V4(_)))
                .unwrap();
            let first_ipv6_pos = addrs
                .iter()
                .position(|a| matches!(a.ip(), IpAddr::V6(_)))
                .unwrap();
            assert!(
                first_ipv4_pos < first_ipv6_pos,
                "IPv4 addresses should come before IPv6 when preferred. Got order: {:?}",
                addrs
            );
        }

        // Verify we can also disable IPv4 preference
        cache.set_ipv4_preference(false);
        // Re-resolve with different preference
        let result2 = cache.force_refresh("localhost", 8080).await;
        assert!(result2.is_ok(), "Force refresh should succeed");
    }

    #[test]
    fn test_force_refresh_clears_cache_entry() {
        // Note: This test only verifies the cache clearing logic,
        // not the full async resolution (which requires tokio runtime)
        let mut cache = create_test_cache();

        // Manually pre-populate cache
        let entry = DnsEntry {
            hostname: "preloaded.com".to_string(),
            addresses: vec!["192.168.1.1:443".parse().unwrap()],
            resolved_at: Instant::now(),
            ttl: Duration::from_secs(3600),
            ipv4_preferred: true,
        };
        cache.cache.insert("preloaded.com".to_string(), entry);
        assert_eq!(cache.len(), 1);

        // force_refresh should remove the existing entry (we don't await here
        // because this is a sync test; the removal happens before the async resolve)
        // In practice, the cache.remove() is called synchronously at the start
        // of force_refresh, so we can verify the entry would be removed
    }

    #[test]
    fn test_default_impl() {
        let cache = DnsCache::default();
        assert!(cache.is_empty());
        assert_eq!(cache.default_ttl(), Duration::from_secs(300));
    }

    /// Test D1 #1: the hickory async resolver resolves "localhost" and caches it.
    ///
    /// "localhost" is answered from the system hosts file (use_hosts_file is enabled), so this
    /// exercises the hickory code path without depending on external network reachability.
    #[tokio::test]
    async fn test_async_dns_resolves_localhost() {
        let mut cache = DnsCache::with_ttl(300, 60);
        let result = cache.resolve("localhost", 80).await;
        assert!(
            result.is_ok(),
            "localhost should resolve: {:?}",
            result.err()
        );
        let addrs = result.unwrap();
        assert!(!addrs.is_empty(), "should have at least one address");
        // Verify it's cached.
        assert_eq!(cache.len(), 1, "localhost should be cached after resolve");
    }

    /// Test D1 #2: the TTL reported by the resolver is capped at default_ttl.
    ///
    /// Resolves "localhost" (from the hosts file, whose entries carry a very large TTL) with a
    /// 60s default TTL and verifies the cached entry's TTL does not exceed the default.
    #[tokio::test]
    async fn test_dns_ttl_capped_at_default() {
        let mut cache = DnsCache::with_ttl(60, 10); // default 60s
        let _ = cache.resolve("localhost", 80).await;
        // Verify the cached entry has ttl <= 60s (capped at default_ttl).
        if let Some(entry) = cache.cache.get("localhost") {
            assert!(
                entry.ttl <= Duration::from_secs(60),
                "ttl should be capped at default, got {:?}",
                entry.ttl
            );
        }
    }
}