hns-cache 0.2.2

Bounded generation- and chain-bound HNS browser cache
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
//! Bounded positive and negative cache for HNS browser resolution.
//!
//! Keys are session-secret-derived SHA-256 values rather than qnames. Entries
//! are bound to runtime generation, policy generation, and exact Handshake
//! chain anchor. Expired or mismatched entries are removed before use.

#![forbid(unsafe_code)]
#![allow(
    clippy::doc_markdown,
    clippy::missing_errors_doc,
    clippy::module_name_repetitions,
    reason = "HNS and DNS protocol names are intentional"
)]

use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::time::{Duration, Instant};

use hns_dns_wire::{Name, RecordType};
use sha2::{Digest, Sha256};
use thiserror::Error;

const CACHE_KEY_DOMAIN: &[u8] = b"hns-browser-cache-key-v1";

/// Bounds for one cache instance.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CacheLimits {
    /// Maximum resident entries.
    pub max_entries: usize,
    /// Maximum caller-reported bytes across resident values.
    pub max_total_value_bytes: usize,
    /// Maximum caller-reported bytes for one value.
    pub max_value_bytes: usize,
    /// Maximum positive TTL in seconds.
    pub max_positive_ttl: u32,
    /// Maximum negative TTL in seconds.
    pub max_negative_ttl: u32,
}

impl Default for CacheLimits {
    fn default() -> Self {
        Self {
            max_entries: 4_096,
            max_total_value_bytes: 16 * 1024 * 1024,
            max_value_bytes: 256 * 1024,
            max_positive_ttl: 86_400,
            max_negative_ttl: 3_600,
        }
    }
}

impl CacheLimits {
    fn validate(self) -> Result<Self, CacheError> {
        if self.max_entries == 0
            || self.max_total_value_bytes == 0
            || self.max_value_bytes == 0
            || self.max_value_bytes > self.max_total_value_bytes
            || self.max_positive_ttl == 0
            || self.max_negative_ttl == 0
        {
            return Err(CacheError::InvalidLimits);
        }
        Ok(self)
    }
}

/// Exact authority state under which an entry was produced.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct CacheGeneration {
    /// Runtime generation.
    pub runtime_generation: u64,
    /// Policy generation.
    pub policy_generation: u64,
    /// Validated Handshake height.
    pub chain_height: u32,
    /// Exact committed Handshake name-tree root.
    pub tree_root: [u8; 32],
}

impl CacheGeneration {
    fn validate(self) -> Result<Self, CacheError> {
        if self.runtime_generation == 0 || self.policy_generation == 0 {
            return Err(CacheError::InvalidGeneration);
        }
        Ok(self)
    }
}

/// Negative cache result.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NegativeKind {
    /// DNSSEC-authenticated name error.
    NameError = 0,
    /// DNSSEC-authenticated no-data result.
    NoData = 1,
    /// Current HNS Urkel non-inclusion.
    HnsNameAbsent = 2,
}

/// Positive or authenticated-negative cache disposition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CacheDisposition {
    /// Positive validated data.
    Positive,
    /// Cryptographically authenticated absence.
    Negative(NegativeKind),
}

/// Metadata checked when admitting one cache value.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CacheAdmission {
    /// Conservative encoded/in-memory value byte estimate.
    pub value_bytes: usize,
    /// Positive or authenticated-negative result.
    pub disposition: CacheDisposition,
    /// Exact runtime, policy, and chain authority.
    pub generation: CacheGeneration,
    /// Current monotonic or Unix time in seconds.
    pub now: u64,
    /// Requested TTL in seconds.
    pub ttl_seconds: u32,
}

/// Opaque per-session query key.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct CacheKey([u8; 32]);

impl CacheKey {
    /// Derive a key from a secret runtime salt and canonical query identity.
    pub fn derive(
        session_secret: &[u8; 32],
        network_id: u8,
        generation: CacheGeneration,
        name: &Name,
        record_type: RecordType,
    ) -> Result<Self, CacheError> {
        if session_secret == &[0; 32] {
            return Err(CacheError::WeakSessionSecret);
        }
        let generation = generation.validate()?;
        let mut wire_name = Vec::with_capacity(name.wire_len());
        name.encode(&mut wire_name)?;
        let mut hasher = Sha256::new();
        hasher.update(CACHE_KEY_DOMAIN);
        hasher.update(session_secret);
        hasher.update([network_id]);
        hasher.update(generation.runtime_generation.to_be_bytes());
        hasher.update(generation.policy_generation.to_be_bytes());
        hasher.update(generation.chain_height.to_be_bytes());
        hasher.update(generation.tree_root);
        hasher.update(record_type.code().to_be_bytes());
        hasher.update(
            u16::try_from(wire_name.len())
                .map_err(|_| CacheError::InvalidQuery)?
                .to_be_bytes(),
        );
        hasher.update(&wire_name);
        Ok(Self(hasher.finalize().into()))
    }
}

impl fmt::Debug for CacheKey {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("CacheKey([redacted])")
    }
}

/// Name-free cache counters.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct CacheStats {
    /// Resident entries.
    pub entries: usize,
    /// Caller-reported resident value bytes.
    pub value_bytes: usize,
    /// Successful current-generation reads.
    pub hits: u64,
    /// Absent, expired, or generation-mismatched reads.
    pub misses: u64,
    /// Capacity-driven removals.
    pub evictions: u64,
    /// TTL-driven removals.
    pub expirations: u64,
    /// Generation-mismatch removals.
    pub stale_removals: u64,
}

#[derive(Clone, Debug)]
struct Entry<Value> {
    value: Value,
    value_bytes: usize,
    disposition: CacheDisposition,
    generation: CacheGeneration,
    expires_at: u64,
    last_access: u64,
}

/// Bounded LRU cache with explicit TTL and authority-generation binding.
#[derive(Clone, Debug)]
pub struct SecureCache<Value> {
    limits: CacheLimits,
    entries: HashMap<CacheKey, Entry<Value>>,
    sequence: u64,
    stats: CacheStats,
}

#[derive(Clone, Debug)]
struct TimedEntry<Value> {
    value: Value,
    expires_at: Instant,
}

/// Small process-local expiring LRU for data that is already bound to an
/// authenticated authority fingerprint by its key.
///
/// Browser adapters use this for short-lived connection-plan and delegated
/// answer reuse. Consensus, proof, DNSSEC, and DANE caches should use
/// [`SecureCache`] so the engine also binds their generation and chain anchor.
#[derive(Clone, Debug)]
pub struct ExpiringLru<Key, Value> {
    max_entries: NonZeroUsize,
    entries: HashMap<Key, TimedEntry<Value>>,
    order: VecDeque<Key>,
}

impl<Key, Value> ExpiringLru<Key, Value>
where
    Key: Clone + Eq + Hash,
    Value: Clone,
{
    /// Construct an empty cache with a nonzero hard entry bound.
    #[must_use]
    pub fn new(max_entries: NonZeroUsize) -> Self {
        Self {
            max_entries,
            entries: HashMap::with_capacity(max_entries.get()),
            order: VecDeque::with_capacity(max_entries.get()),
        }
    }

    /// Insert or replace one entry for a finite positive lifetime.
    pub fn insert(&mut self, key: Key, value: Value, ttl: Duration) -> Result<(), CacheError> {
        if ttl.is_zero() {
            return Err(CacheError::TtlLimit);
        }
        let expires_at = Instant::now()
            .checked_add(ttl)
            .ok_or(CacheError::TimeOverflow)?;
        self.prune_expired();
        self.promote(&key);
        self.order.push_back(key.clone());
        self.entries.insert(key, TimedEntry { value, expires_at });
        self.evict_over_limit();
        Ok(())
    }

    /// Return a cloned live entry and promote it to most recently used.
    pub fn get(&mut self, key: &Key) -> Option<Value> {
        self.prune_expired();
        let value = self.entries.get(key).map(|entry| entry.value.clone())?;
        self.promote(key);
        self.order.push_back(key.clone());
        Some(value)
    }

    /// Number of live entries.
    pub fn len(&mut self) -> usize {
        self.prune_expired();
        self.entries.len()
    }

    /// Whether no live entries remain.
    pub fn is_empty(&mut self) -> bool {
        self.len() == 0
    }

    /// Remove all entries.
    pub fn clear(&mut self) {
        self.entries.clear();
        self.order.clear();
    }

    fn prune_expired(&mut self) {
        let now = Instant::now();
        self.entries.retain(|_, entry| entry.expires_at > now);
        self.order.retain(|key| self.entries.contains_key(key));
    }

    fn evict_over_limit(&mut self) {
        while self.entries.len() > self.max_entries.get() {
            let Some(key) = self.order.pop_front() else {
                break;
            };
            self.entries.remove(&key);
        }
    }

    fn promote(&mut self, key: &Key) {
        self.order.retain(|candidate| candidate != key);
    }
}

impl<Value> SecureCache<Value> {
    /// Create an empty cache after validating all bounds.
    pub fn new(limits: CacheLimits) -> Result<Self, CacheError> {
        let limits = limits.validate()?;
        Ok(Self {
            limits,
            entries: HashMap::with_capacity(limits.max_entries),
            sequence: 0,
            stats: CacheStats::default(),
        })
    }

    /// Insert one positive or authenticated-negative result.
    ///
    /// `value_bytes` is the caller's conservative encoded/in-memory estimate.
    pub fn insert(
        &mut self,
        key: CacheKey,
        value: Value,
        admission: CacheAdmission,
    ) -> Result<(), CacheError> {
        let generation = admission.generation.validate()?;
        let value_bytes = admission.value_bytes;
        if value_bytes == 0
            || value_bytes > self.limits.max_value_bytes
            || value_bytes > self.limits.max_total_value_bytes
        {
            return Err(CacheError::ValueLimit);
        }
        let maximum_ttl = match admission.disposition {
            CacheDisposition::Positive => self.limits.max_positive_ttl,
            CacheDisposition::Negative(_) => self.limits.max_negative_ttl,
        };
        if admission.ttl_seconds == 0 || admission.ttl_seconds > maximum_ttl {
            return Err(CacheError::TtlLimit);
        }
        let expires_at = admission
            .now
            .checked_add(u64::from(admission.ttl_seconds))
            .ok_or(CacheError::TimeOverflow)?;
        let sequence = self.next_sequence()?;
        self.remove_expired(admission.now);
        self.remove_internal(&key);
        while self.entries.len() >= self.limits.max_entries
            || self
                .stats
                .value_bytes
                .checked_add(value_bytes)
                .is_none_or(|bytes| bytes > self.limits.max_total_value_bytes)
        {
            self.evict_lru().ok_or(CacheError::ValueLimit)?;
        }
        self.stats.value_bytes = self
            .stats
            .value_bytes
            .checked_add(value_bytes)
            .ok_or(CacheError::ValueLimit)?;
        self.entries.insert(
            key,
            Entry {
                value,
                value_bytes,
                disposition: admission.disposition,
                generation,
                expires_at,
                last_access: sequence,
            },
        );
        self.stats.entries = self.entries.len();
        Ok(())
    }

    /// Read one current entry and update its LRU position.
    pub fn get(
        &mut self,
        key: &CacheKey,
        generation: CacheGeneration,
        now: u64,
    ) -> Result<Option<(&Value, CacheDisposition)>, CacheError> {
        let generation = generation.validate()?;
        let Some(metadata) = self
            .entries
            .get(key)
            .map(|entry| (entry.generation, entry.expires_at, entry.disposition))
        else {
            self.stats.misses = self.stats.misses.saturating_add(1);
            return Ok(None);
        };
        if metadata.0 != generation {
            self.remove_internal(key);
            self.stats.stale_removals = self.stats.stale_removals.saturating_add(1);
            self.stats.misses = self.stats.misses.saturating_add(1);
            return Ok(None);
        }
        if now >= metadata.1 {
            self.remove_internal(key);
            self.stats.expirations = self.stats.expirations.saturating_add(1);
            self.stats.misses = self.stats.misses.saturating_add(1);
            return Ok(None);
        }
        let sequence = self.next_sequence()?;
        let entry = self
            .entries
            .get_mut(key)
            .ok_or(CacheError::InternalInvariant)?;
        entry.last_access = sequence;
        self.stats.hits = self.stats.hits.saturating_add(1);
        Ok(Some((&entry.value, metadata.2)))
    }

    /// Remove every entry on a generation, policy, or chain-anchor transition.
    pub fn clear(&mut self) {
        self.entries.clear();
        self.stats.entries = 0;
        self.stats.value_bytes = 0;
    }

    /// Remove one opaque key.
    pub fn remove(&mut self, key: &CacheKey) -> bool {
        self.remove_internal(key).is_some()
    }

    /// Name-free counters.
    #[must_use]
    pub const fn stats(&self) -> CacheStats {
        self.stats
    }

    /// Configured hard bounds.
    #[must_use]
    pub const fn limits(&self) -> CacheLimits {
        self.limits
    }

    fn next_sequence(&mut self) -> Result<u64, CacheError> {
        self.sequence = self
            .sequence
            .checked_add(1)
            .ok_or(CacheError::SequenceExhausted)?;
        Ok(self.sequence)
    }

    fn remove_expired(&mut self, now: u64) {
        let expired = self
            .entries
            .iter()
            .filter_map(|(key, entry)| (now >= entry.expires_at).then_some(*key))
            .collect::<Vec<_>>();
        for key in expired {
            if self.remove_internal(&key).is_some() {
                self.stats.expirations = self.stats.expirations.saturating_add(1);
            }
        }
    }

    fn evict_lru(&mut self) -> Option<()> {
        let key = self
            .entries
            .iter()
            .min_by_key(|(_, entry)| entry.last_access)
            .map(|(key, _)| *key)?;
        self.remove_internal(&key)?;
        self.stats.evictions = self.stats.evictions.saturating_add(1);
        Some(())
    }

    fn remove_internal(&mut self, key: &CacheKey) -> Option<Entry<Value>> {
        let entry = self.entries.remove(key)?;
        self.stats.value_bytes = self.stats.value_bytes.saturating_sub(entry.value_bytes);
        self.stats.entries = self.entries.len();
        Some(entry)
    }
}

/// Cache configuration, key, bound, or invariant failure.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CacheError {
    /// DNS name encoding failed.
    #[error("DNS query encoding failed: {0}")]
    Wire(#[from] hns_dns_wire::Error),
    /// A configured cache bound is zero or inconsistent.
    #[error("invalid cache limits")]
    InvalidLimits,
    /// Runtime or policy generation is zero.
    #[error("invalid cache generation")]
    InvalidGeneration,
    /// Runtime cache-key secret is all zero.
    #[error("cache session secret must be randomly generated")]
    WeakSessionSecret,
    /// Canonical query identity cannot be represented.
    #[error("invalid cache query identity")]
    InvalidQuery,
    /// Caller-reported value size exceeds a bound.
    #[error("cache value exceeds its bound")]
    ValueLimit,
    /// TTL is zero or exceeds its positive/negative bound.
    #[error("cache TTL exceeds its bound")]
    TtlLimit,
    /// Expiration time overflowed.
    #[error("cache expiration time overflow")]
    TimeOverflow,
    /// LRU sequence cannot advance.
    #[error("cache LRU sequence exhausted")]
    SequenceExhausted,
    /// Internal entry disappeared during one exclusive operation.
    #[error("cache internal invariant failed")]
    InternalInvariant,
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    reason = "tests fail immediately on invalid local cache fixtures"
)]
mod tests {
    use super::*;
    use std::num::NonZeroUsize;

    fn cache_generation(value: u8) -> CacheGeneration {
        CacheGeneration {
            runtime_generation: u64::from(value),
            policy_generation: 1,
            chain_height: 100,
            tree_root: [value; 32],
        }
    }

    fn key(secret: u8, name: &str, generation: CacheGeneration) -> CacheKey {
        CacheKey::derive(
            &[secret; 32],
            0,
            generation,
            &Name::from_ascii(name).unwrap(),
            RecordType::Tlsa,
        )
        .unwrap()
    }

    #[test]
    fn expiring_lru_rejects_zero_ttl_and_evicts_least_recently_used() {
        let mut cache = ExpiringLru::new(NonZeroUsize::new(2).unwrap());
        assert!(matches!(
            cache.insert("zero", 0, Duration::ZERO),
            Err(CacheError::TtlLimit)
        ));
        cache.insert("a", 1, Duration::from_secs(60)).unwrap();
        cache.insert("b", 2, Duration::from_secs(60)).unwrap();
        assert_eq!(cache.get(&"a"), Some(1));
        cache.insert("c", 3, Duration::from_secs(60)).unwrap();

        assert_eq!(cache.get(&"a"), Some(1));
        assert_eq!(cache.get(&"b"), None);
        assert_eq!(cache.get(&"c"), Some(3));
    }

    #[test]
    fn keys_are_qname_free_session_and_generation_specific() {
        let generation = cache_generation(1);
        let first = key(7, "_443._tcp.alpha.", generation);
        let same = key(7, "_443._tcp.alpha.", generation);
        let other_secret = key(8, "_443._tcp.alpha.", generation);
        let other_generation = key(7, "_443._tcp.alpha.", cache_generation(2));
        assert_eq!(first, same);
        assert_ne!(first, other_secret);
        assert_ne!(first, other_generation);
        assert_eq!(format!("{first:?}"), "CacheKey([redacted])");
        assert!(matches!(
            CacheKey::derive(
                &[0; 32],
                0,
                generation,
                &Name::from_ascii("alpha.").unwrap(),
                RecordType::A,
            ),
            Err(CacheError::WeakSessionSecret)
        ));
    }

    #[test]
    fn expiration_and_generation_mismatch_remove_before_use() {
        let generation = cache_generation(1);
        let cache_key = key(7, "alpha.", generation);
        let mut cache = SecureCache::new(CacheLimits::default()).unwrap();
        cache
            .insert(
                cache_key,
                "secure",
                CacheAdmission {
                    value_bytes: 6,
                    disposition: CacheDisposition::Positive,
                    generation,
                    now: 100,
                    ttl_seconds: 10,
                },
            )
            .unwrap();
        assert_eq!(
            cache.get(&cache_key, generation, 109).unwrap(),
            Some((&"secure", CacheDisposition::Positive))
        );
        assert_eq!(cache.get(&cache_key, generation, 110).unwrap(), None);
        assert_eq!(cache.stats().expirations, 1);

        cache
            .insert(
                cache_key,
                "negative",
                CacheAdmission {
                    value_bytes: 8,
                    disposition: CacheDisposition::Negative(NegativeKind::NoData),
                    generation,
                    now: 200,
                    ttl_seconds: 10,
                },
            )
            .unwrap();
        assert_eq!(
            cache.get(&cache_key, cache_generation(2), 201).unwrap(),
            None
        );
        assert_eq!(cache.stats().stale_removals, 1);
    }

    #[test]
    fn entry_and_byte_bounds_evict_least_recently_used() {
        let limits = CacheLimits {
            max_entries: 2,
            max_total_value_bytes: 8,
            max_value_bytes: 8,
            max_positive_ttl: 100,
            max_negative_ttl: 10,
        };
        let generation = cache_generation(1);
        let first = key(7, "one.", generation);
        let second = key(7, "two.", generation);
        let third = key(7, "three.", generation);
        let mut cache = SecureCache::new(limits).unwrap();
        for (key, value) in [(first, "1111"), (second, "2222")] {
            cache
                .insert(
                    key,
                    value,
                    CacheAdmission {
                        value_bytes: 4,
                        disposition: CacheDisposition::Positive,
                        generation,
                        now: 0,
                        ttl_seconds: 100,
                    },
                )
                .unwrap();
        }
        assert!(cache.get(&first, generation, 1).unwrap().is_some());
        cache
            .insert(
                third,
                "3333",
                CacheAdmission {
                    value_bytes: 4,
                    disposition: CacheDisposition::Positive,
                    generation,
                    now: 2,
                    ttl_seconds: 100,
                },
            )
            .unwrap();
        assert!(cache.get(&second, generation, 3).unwrap().is_none());
        assert!(cache.get(&first, generation, 3).unwrap().is_some());
        assert!(cache.get(&third, generation, 3).unwrap().is_some());
        assert_eq!(cache.stats().evictions, 1);
        assert_eq!(cache.stats().entries, 2);
        assert_eq!(cache.stats().value_bytes, 8);
    }

    #[test]
    fn invalid_limits_sizes_and_ttls_fail_without_insertion() {
        assert!(matches!(
            SecureCache::<()>::new(CacheLimits {
                max_entries: 0,
                ..CacheLimits::default()
            }),
            Err(CacheError::InvalidLimits)
        ));
        let mut cache = SecureCache::new(CacheLimits {
            max_entries: 1,
            max_total_value_bytes: 8,
            max_value_bytes: 4,
            max_positive_ttl: 10,
            max_negative_ttl: 2,
        })
        .unwrap();
        let generation = cache_generation(1);
        let cache_key = key(7, "alpha.", generation);
        assert!(matches!(
            cache.insert(
                cache_key,
                (),
                CacheAdmission {
                    value_bytes: 5,
                    disposition: CacheDisposition::Positive,
                    generation,
                    now: 0,
                    ttl_seconds: 1,
                },
            ),
            Err(CacheError::ValueLimit)
        ));
        assert!(matches!(
            cache.insert(
                cache_key,
                (),
                CacheAdmission {
                    value_bytes: 1,
                    disposition: CacheDisposition::Negative(NegativeKind::NameError),
                    generation,
                    now: 0,
                    ttl_seconds: 3,
                },
            ),
            Err(CacheError::TtlLimit)
        ));
        assert_eq!(cache.stats().entries, 0);
    }
}