domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
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
//! WHOIS cache management.

use anyhow::{Context, Result};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::SystemTime;

use crate::clock::{Clock, SystemClock};

/// Process-wide count of WHOIS cache saves. Used to skip the full directory scan
/// when the cache is below the limit (common case).
static WHOIS_SAVE_COUNT: AtomicUsize = AtomicUsize::new(0);

use super::types::{WhoisCacheEntry, WhoisResult};

/// Default cache TTL: 7 days (WHOIS data changes infrequently)
pub(crate) const CACHE_TTL_SECS: u64 = crate::config::WHOIS_CACHE_TTL_SECS;

/// When over the entry limit, run the full directory-scan eviction at most every N saves.
/// This avoids an O(N²) I/O bomb where every save after the limit triggered a scan.
const ENFORCE_INTERVAL_WHEN_OVER_LIMIT: usize = 1000;

/// Cache store with injectable time source for deterministic tests.
pub(crate) struct WhoisCacheStore<C = SystemClock> {
    clock: C,
    max_entries: usize,
}

impl Default for WhoisCacheStore<SystemClock> {
    fn default() -> Self {
        Self::new(SystemClock, crate::config::MAX_WHOIS_CACHE_ENTRIES)
    }
}

impl<C: Clock> WhoisCacheStore<C> {
    pub(crate) fn new(clock: C, max_entries: usize) -> Self {
        Self { clock, max_entries }
    }

    fn cache_file(cache_path: &Path, domain: &str) -> PathBuf {
        cache_path.join(format!("{}.json", domain.replace('.', "_")))
    }

    pub(crate) async fn load(
        &self,
        cache_path: &Path,
        domain: &str,
    ) -> Result<Option<WhoisCacheEntry>> {
        let cache_file = Self::cache_file(cache_path, domain);
        // Distinguish "file doesn't exist" (cache miss, return Ok(None)) from "I/O
        // error checking existence" (e.g. permission denied on the cache dir).
        // The previous `unwrap_or(false)` collapsed both cases into a cache miss,
        // hiding transient failures and bypassing the cache without any signal.
        match tokio::fs::try_exists(&cache_file).await {
            Ok(true) => {}
            Ok(false) => return Ok(None),
            Err(e) => {
                // I/O error: warn and treat as a miss so the scan still progresses,
                // but the operator gets a log entry to investigate.
                log::warn!(
                    "Failed to check WHOIS cache file existence for {} ({}): {e}; treating as cache miss",
                    cache_file.display(),
                    domain
                );
                return Ok(None);
            }
        }

        let metadata = tokio::fs::metadata(&cache_file)
            .await
            .context("Failed to stat cache file")?;
        if metadata.len() > crate::config::MAX_WHOIS_CACHE_FILE_SIZE {
            log::warn!(
                "Skipping oversized WHOIS cache file {} ({} bytes, max: {} bytes)",
                cache_file.display(),
                metadata.len(),
                crate::config::MAX_WHOIS_CACHE_FILE_SIZE
            );
            if let Err(e) = tokio::fs::remove_file(&cache_file).await {
                log::debug!(
                    "Failed to remove oversized WHOIS cache file {}: {}",
                    cache_file.display(),
                    e
                );
            }
            return Ok(None);
        }

        let content = tokio::fs::read_to_string(&cache_file)
            .await
            .context("Failed to read cache file")?;
        let entry: WhoisCacheEntry =
            serde_json::from_str(&content).context("Failed to parse cache file")?;

        let age = self
            .clock
            .now()
            .duration_since(entry.cached_at)
            .unwrap_or_default();
        if crate::utils::cache::duration_exceeds_ttl(age, CACHE_TTL_SECS) {
            if let Err(e) = tokio::fs::remove_file(&cache_file).await {
                log::debug!(
                    "Failed to remove expired WHOIS cache file {}: {}",
                    cache_file.display(),
                    e
                );
            }
            return Ok(None);
        }

        Ok(Some(entry))
    }

    pub(crate) async fn save(
        &self,
        cache_path: &Path,
        domain: &str,
        result: &WhoisResult,
    ) -> Result<()> {
        tokio::fs::create_dir_all(cache_path)
            .await
            .context("Failed to create cache directory")?;

        let cache_file = Self::cache_file(cache_path, domain);
        let entry = WhoisCacheEntry {
            result: result.into(),
            cached_at: self.clock.now(),
            domain: domain.to_string(),
        };

        let content =
            serde_json::to_string_pretty(&entry).context("Failed to serialize cache entry")?;
        crate::utils::cache::write_atomic(&cache_file, content.as_bytes())
            .await
            .context("Failed to write cache file")?;

        let mut count = WHOIS_SAVE_COUNT.fetch_add(1, Ordering::Relaxed) + 1;
        // On first save, seed the counter with the actual number of existing cache files
        // so the quota isn't bypassed after a restart with a pre-populated cache directory.
        if count == 1 {
            let existing = std::fs::read_dir(cache_path)
                .map(|rd| rd.flatten().count())
                .unwrap_or(0);
            if existing > 1 {
                // Set the counter to existing count (atomic, so other threads see it too)
                WHOIS_SAVE_COUNT.store(existing, Ordering::Relaxed);
                count = existing;
            }
        }
        let over_limit = count >= self.max_entries;
        let first_time_over = over_limit && count.saturating_sub(self.max_entries) <= 1;
        let interval_hit = over_limit
            && count > self.max_entries
            && (count - self.max_entries - 1) % ENFORCE_INTERVAL_WHEN_OVER_LIMIT == 0;
        let should_enforce = count == 1 || first_time_over || interval_hit;
        if should_enforce {
            self.enforce_cache_limit(cache_path).await?;
        }
        Ok(())
    }

    async fn enforce_cache_limit(&self, cache_path: &Path) -> Result<()> {
        let cache_path_owned = cache_path.to_path_buf();
        let mut entries =
            tokio::task::spawn_blocking(move || -> Result<Vec<(PathBuf, SystemTime)>> {
                let mut files = Vec::new();
                let dir = std::fs::read_dir(&cache_path_owned)
                    .context("Failed to read cache directory")?;

                for entry in dir.flatten() {
                    let path = entry.path();
                    if path.extension().is_some_and(|ext| ext == "json") {
                        let modified = entry
                            .metadata()
                            .ok()
                            .and_then(|metadata| metadata.modified().ok())
                            .unwrap_or(SystemTime::UNIX_EPOCH);
                        files.push((path, modified));
                    }
                }

                Ok(files)
            })
            .await
            .context("Blocking task panicked")??;

        if entries.len() <= self.max_entries {
            return Ok(());
        }

        entries.sort_by_key(|(_, modified)| *modified);
        let to_delete = entries.len() - self.max_entries;
        for (path, _) in entries.into_iter().take(to_delete) {
            if let Err(e) = tokio::fs::remove_file(&path).await {
                log::debug!("Failed to evict WHOIS cache file {}: {}", path.display(), e);
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};
    use std::time::{Duration, SystemTime};
    use tempfile::TempDir;

    #[derive(Clone)]
    struct TestClock {
        now: Arc<Mutex<SystemTime>>,
    }

    impl TestClock {
        fn new(now: SystemTime) -> Self {
            Self {
                now: Arc::new(Mutex::new(now)),
            }
        }

        fn set(&self, now: SystemTime) {
            *self.now.lock().expect("clock lock") = now;
        }
    }

    impl Clock for TestClock {
        fn now(&self) -> SystemTime {
            *self.now.lock().expect("clock lock")
        }
    }

    fn create_test_whois_result() -> WhoisResult {
        WhoisResult {
            creation_date: Some(chrono::Utc::now()),
            expiration_date: Some(chrono::Utc::now() + chrono::Duration::days(365)),
            updated_date: Some(chrono::Utc::now()),
            registrar: Some("Test Registrar".to_string()),
            registrant_country: Some("US".to_string()),
            registrant_org: Some("Test Org".to_string()),
            status: vec!["clientTransferProhibited".to_string()],
            nameservers: vec!["ns1.example.com".to_string(), "ns2.example.com".to_string()],
            raw_text: Some("Raw WHOIS text".to_string()),
        }
    }

    #[tokio::test]
    async fn test_save_to_cache() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let domain = "example.com";
        let result = create_test_whois_result();
        let store = WhoisCacheStore::new(TestClock::new(SystemTime::UNIX_EPOCH), 10);

        assert!(store.save(cache_path, domain, &result).await.is_ok());

        let cache_file = cache_path.join("example_com.json");
        assert!(cache_file.exists(), "Cache file should be created");
    }

    #[tokio::test]
    async fn test_load_from_cache_not_found() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let store = WhoisCacheStore::new(TestClock::new(SystemTime::UNIX_EPOCH), 10);

        let result = store
            .load(cache_path, "nonexistent.com")
            .await
            .expect("Should not error");
        assert!(
            result.is_none(),
            "Should return None for non-existent cache"
        );
    }

    #[tokio::test]
    async fn test_load_from_cache_found() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let domain = "example.com";
        let result = create_test_whois_result();
        let clock = TestClock::new(SystemTime::UNIX_EPOCH + Duration::from_secs(10));
        let store = WhoisCacheStore::new(clock.clone(), 10);

        store
            .save(cache_path, domain, &result)
            .await
            .expect("Should save to cache");

        let cached = store
            .load(cache_path, domain)
            .await
            .expect("Should load from cache");
        assert!(cached.is_some(), "Should find cached entry");

        let entry = cached.unwrap();
        assert_eq!(entry.domain, domain);

        // Convert to WhoisResult to verify data integrity
        let whois_result: WhoisResult = entry.result.into();
        assert!(whois_result.creation_date.is_some());
        assert_eq!(whois_result.registrar, Some("Test Registrar".to_string()));
    }

    #[tokio::test]
    async fn test_load_from_cache_expired() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let domain = "example.com";
        let result = create_test_whois_result();
        let clock = TestClock::new(SystemTime::UNIX_EPOCH + Duration::from_secs(1));
        let store = WhoisCacheStore::new(clock.clone(), 10);

        store
            .save(cache_path, domain, &result)
            .await
            .expect("Should save to cache");
        clock.set(SystemTime::UNIX_EPOCH + Duration::from_secs(CACHE_TTL_SECS + 2));

        let cached = store
            .load(cache_path, domain)
            .await
            .expect("Should handle expired cache");
        assert!(cached.is_none(), "Should return None for expired cache");
        let cache_file = cache_path.join("example_com.json");
        assert!(!cache_file.exists(), "Expired cache file should be deleted");
    }

    #[tokio::test]
    async fn test_cache_domain_name_sanitization() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let domain = "example.com";
        let result = create_test_whois_result();
        let store = WhoisCacheStore::new(TestClock::new(SystemTime::UNIX_EPOCH), 10);

        store
            .save(cache_path, domain, &result)
            .await
            .expect("Should save to cache");

        let cache_file = cache_path.join("example_com.json");
        assert!(cache_file.exists(), "Cache file should use sanitized name");
    }

    #[tokio::test]
    async fn test_cache_invalid_json() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let domain = "example.com";

        // Create invalid JSON file
        let cache_file = cache_path.join("example_com.json");
        std::fs::create_dir_all(cache_path).expect("Should create directory");
        std::fs::write(&cache_file, "invalid json").expect("Should write file");
        let store = WhoisCacheStore::new(TestClock::new(SystemTime::UNIX_EPOCH), 10);

        let result = store.load(cache_path, domain).await;
        assert!(result.is_err(), "Should error on invalid JSON");
    }

    #[tokio::test]
    async fn test_cache_missing_fields_handles_gracefully() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let domain = "example.com";

        // Create cache file with missing cached_at field
        let cache_file = cache_path.join("example_com.json");
        std::fs::create_dir_all(cache_path).expect("Should create directory");
        std::fs::write(&cache_file, r#"{"domain": "example.com", "result": {}}"#)
            .expect("Should write file");
        let store = WhoisCacheStore::new(TestClock::new(SystemTime::UNIX_EPOCH), 10);

        let result = store.load(cache_path, domain).await;
        assert!(result.is_err(), "Should error on missing required fields");
    }

    #[tokio::test]
    async fn test_cache_fresh_returns_data() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let domain = "example.com";
        let result = create_test_whois_result();
        let clock = TestClock::new(SystemTime::UNIX_EPOCH + Duration::from_secs(60));
        let store = WhoisCacheStore::new(clock.clone(), 10);

        store
            .save(cache_path, domain, &result)
            .await
            .expect("Should save to cache");
        clock.set(SystemTime::UNIX_EPOCH + Duration::from_secs(CACHE_TTL_SECS - 1));

        let cached = store
            .load(cache_path, domain)
            .await
            .expect("Should load from cache");
        assert!(cached.is_some(), "Should return cached data when fresh");

        let entry = cached.unwrap();
        assert_eq!(entry.domain, domain, "Cached domain should match");
        // Verify data integrity
        let whois_result: WhoisResult = entry.result.into();
        assert_eq!(
            whois_result.registrar,
            Some("Test Registrar".to_string()),
            "Cached registrar should match"
        );
    }

    #[tokio::test]
    async fn test_cache_near_expiration_still_valid() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let domain = "example.com";
        let result = create_test_whois_result();
        let clock = TestClock::new(SystemTime::UNIX_EPOCH + Duration::from_secs(5));
        let store = WhoisCacheStore::new(clock.clone(), 10);

        store
            .save(cache_path, domain, &result)
            .await
            .expect("Should save cache entry");
        clock.set(SystemTime::UNIX_EPOCH + Duration::from_secs(CACHE_TTL_SECS - 1));

        let cached = store
            .load(cache_path, domain)
            .await
            .expect("Should load from cache");
        assert!(
            cached.is_some(),
            "Should return cached data when just before expiration"
        );
    }

    /// Depends on global `WHOIS_SAVE_COUNT` being low (1,2,3) so eviction runs.
    /// Run in isolation: `cargo test test_enforce_cache_limit_evicts_oldest_entries`
    #[tokio::test]
    #[ignore = "process-global cache counter; run in isolation"]
    async fn test_enforce_cache_limit_evicts_oldest_entries() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let cache_path = temp_dir.path();
        let result = create_test_whois_result();
        let clock = TestClock::new(SystemTime::UNIX_EPOCH);
        let store = WhoisCacheStore::new(clock.clone(), 2);

        for i in 0..3 {
            let domain = format!("domain{}.com", i);
            store
                .save(cache_path, &domain, &result)
                .await
                .expect("Should save to cache");
            clock.set(
                SystemTime::UNIX_EPOCH
                    + Duration::from_secs(u64::try_from(i + 1).expect("positive index")),
            );
            std::thread::sleep(Duration::from_millis(5));
        }

        let file_count = std::fs::read_dir(cache_path)
            .expect("Should read dir")
            .filter(|e| e.is_ok())
            .count();
        assert_eq!(file_count, 2, "Only the newest entries should remain");
        assert!(!cache_path.join("domain0_com.json").exists());
        assert!(cache_path.join("domain1_com.json").exists());
        assert!(cache_path.join("domain2_com.json").exists());
    }
}