async-snmp 0.18.0

Modern async-first SNMP client library for Rust
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
//! Request context for MIB handlers.
//!
//! [`RequestContext`] contains incoming request information for handler
//! authorization decisions and cooperative deadline and shutdown handling.

use std::net::SocketAddr;

use bytes::Bytes;
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use subtle::ConstantTimeEq;
use tokio_util::sync::CancellationToken;

use crate::Community;
use crate::DecodeAnomaly;
use crate::message::SecurityLevel;
use crate::pdu::PduType;
use crate::version::{CommunityVersion, Version};

use super::SecurityModel;

/// A request's model-specific security name.
///
/// Community variants retain transitive Debug redaction, while USM usernames
/// remain visible in diagnostics.
#[derive(Debug, Clone)]
pub enum SecurityName {
    /// SNMPv1/v2c community identifier.
    Community(Community),
    /// SNMPv3 USM username.
    Usm(Bytes),
}

impl SecurityName {
    /// Return the protocol bytes for authorization and sizing.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        match self {
            Self::Community(community) => community.as_bytes(),
            Self::Usm(username) => username,
        }
    }

    /// Return the length of the security name in octets.
    #[must_use]
    pub fn len(&self) -> usize {
        self.as_bytes().len()
    }

    /// Return whether the security name is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.as_bytes().is_empty()
    }

    /// Return whether `candidate` is the same kind of security name with the
    /// same protocol octets.
    ///
    /// Community names match only community names, and USM usernames match
    /// only USM usernames. For same-variant, equal-length inputs, comparison
    /// time does not depend on the octet values. Variant and length are not
    /// concealed and a mismatch in either may return sooner.
    ///
    /// This comparison alone is not a complete authorization decision. A
    /// handler must also check [`RequestContext::security_model`] to
    /// distinguish SNMPv1 communities, SNMPv2c communities, and USM users. USM
    /// authorization should additionally require the appropriate
    /// [`RequestContext::security_level`], because `noAuthNoPriv` usernames are
    /// not authenticated.
    #[must_use]
    pub fn matches(&self, candidate: &Self) -> bool {
        match (self, candidate) {
            (Self::Community(expected), Self::Community(actual)) => {
                expected.matches(actual.as_bytes())
            }
            (Self::Usm(expected), Self::Usm(actual)) => {
                expected.len() == actual.len()
                    && bool::from(expected.as_ref().ct_eq(actual.as_ref()))
            }
            (Self::Community(_), Self::Usm(_)) | (Self::Usm(_), Self::Community(_)) => false,
        }
    }
}

/// Request context passed to MIB handlers.
///
/// Contains information about the incoming request for authorization decisions,
/// including VACM-resolved access control information when VACM is enabled.
/// Receipt and admission timestamps expose concurrency queueing, while the
/// optional absolute deadline and level-triggered token let handlers propagate
/// cooperative cancellation into downstream work. Cancellation is advisory:
/// it does not itself drop a handler future.
///
/// # Example
///
/// ```rust
/// use async_snmp::handler::{MibHandler, RequestContext, GetResult, HandlerResult, BoxFuture};
/// use async_snmp::{Oid, Value, oid};
///
/// struct LoggingHandler;
///
/// impl MibHandler for LoggingHandler {
///     fn get<'a>(&'a self, ctx: &'a RequestContext, oid: &'a Oid) -> BoxFuture<'a, HandlerResult<GetResult>> {
///         Box::pin(async move {
///             // Log request details
///             println!(
///                 "GET {} from {} (user: {:?}, version: {:?})",
///                 oid, ctx.source(), ctx.security_name(), ctx.version()
///             );
///
///             if oid == &oid!(1, 3, 6, 1, 4, 1, 99999, 1, 0) {
///                 Ok(GetResult::Value(Value::Integer(42)))
///             } else {
///                 Ok(GetResult::NoSuchObject)
///             }
///         })
///     }
///
///     fn get_next<'a>(
///         &'a self,
///         _ctx: &'a RequestContext,
///         _oid: &'a Oid,
///     ) -> BoxFuture<'a, HandlerResult<async_snmp::handler::GetNextResult>> {
///         Box::pin(async { Ok(async_snmp::handler::GetNextResult::EndOfMibView) })
///     }
/// }
/// ```
///
/// Authorization code should match both the security model and name, and for
/// USM should require a security level that authenticates the username:
///
/// ```rust
/// use async_snmp::{Community, RequestContext, SecurityLevel, SecurityModel, SecurityName};
///
/// fn may_read_v2c(ctx: &RequestContext) -> bool {
///     let reader = SecurityName::Community(Community::from(b"reader\xff"));
///     ctx.security_model() == SecurityModel::V2c && ctx.security_name().matches(&reader)
/// }
///
/// fn may_write(ctx: &RequestContext) -> bool {
///     let operator = SecurityName::Usm(bytes::Bytes::from_static(b"operator\xff"));
///     ctx.security_model() == SecurityModel::Usm
///         && matches!(ctx.security_level(), SecurityLevel::AuthNoPriv | SecurityLevel::AuthPriv)
///         && ctx.security_name().matches(&operator)
/// }
/// ```
///
/// A context is created by the agent from a validated incoming request. Its
/// fields cannot be replaced or combined into states that are impossible on
/// the wire:
///
/// ```compile_fail,E0616
/// use async_snmp::{RequestContext, Version};
///
/// fn change_version(ctx: &mut RequestContext) {
///     ctx.version = Version::V1;
/// }
/// ```
///
/// ```compile_fail,E0599
/// use async_snmp::RequestContext;
///
/// let _ctx = RequestContext::test_context();
/// ```
///
/// ```compile_fail,E0451
/// use async_snmp::RequestContext;
///
/// let _ctx = RequestContext {
///     source: "127.0.0.1:161".parse().unwrap(),
///     ..unimplemented!()
/// };
/// ```
///
/// # Testing handlers
///
/// `RequestContext` has no public constructor because the agent derives it
/// from a validated request and successful authorization. Keep pure handler
/// policy in helpers that take only the values they need. When a test needs a
/// complete context, run an in-process [`crate::Agent`] and capture the context
/// passed to a test handler.
#[derive(Debug, Clone)]
pub struct RequestContext {
    /// Monotonic instant when the datagram was received.
    received_at: tokio::time::Instant,

    /// Monotonic instant when request processing was admitted.
    admitted_at: tokio::time::Instant,

    /// Absolute request deadline measured from receipt.
    deadline: Option<tokio::time::Instant>,

    /// Level-triggered advisory cancellation for this request.
    cancellation: CancellationToken,

    /// Agent-owned phase record used to protect mutation cleanup.
    phase: Arc<AtomicU8>,

    /// Source address of the request.
    ///
    /// Use this for logging or additional access control beyond VACM.
    source: SocketAddr,

    /// SNMP version (V1, V2c, or V3).
    version: Version,

    /// Security model used for this request.
    ///
    /// - `V1` for `SNMPv1` community-based
    /// - `V2c` for `SNMPv2c` community-based
    /// - `Usm` for `SNMPv3` User-based Security Model
    security_model: SecurityModel,

    /// Model-specific community identifier or USM username.
    security_name: SecurityName,

    /// Security level (v3 only, `NoAuthNoPriv` for v1/v2c).
    ///
    /// Indicates whether authentication and/or privacy were used.
    security_level: SecurityLevel,

    /// Context name (v3 only, empty for v1/v2c).
    ///
    /// `SNMPv3` contexts allow partitioning MIB views.
    context_name: Bytes,

    /// Request ID from the PDU.
    ///
    /// Useful for correlating requests with responses in logs.
    request_id: i32,

    /// PDU type (`GetRequest`, `GetNextRequest`, `SetRequest`, etc.).
    pdu_type: PduType,

    /// Resolved group name (if VACM enabled).
    ///
    /// Set when VACM successfully maps the security name to a group.
    group_name: Option<Bytes>,

    /// Read view name (if VACM enabled).
    ///
    /// The view that controls which OIDs can be read.
    read_view: Option<Bytes>,

    /// Write view name (if VACM enabled).
    ///
    /// The view that controls which OIDs can be written.
    write_view: Option<Bytes>,

    /// Client-advertised maximum message size (V3 only).
    ///
    /// For `SNMPv3` requests, this is the msgMaxSize from the V3 message header,
    /// indicating the largest message the client can accept. The agent should
    /// limit response sizes to `min(agent_max, msg_max_size)`.
    ///
    /// None for v1/v2c requests (no msgMaxSize field in those versions).
    msg_max_size: Option<usize>,

    /// Accepted non-canonical encodings in stable wire-processing order.
    ///
    /// Community request anomalies are unauthenticated. V3 anomalies within
    /// the declared envelope are exposed only after USM processing succeeds
    /// and are attributable to the configured user only at `authNoPriv` or
    /// `authPriv`. A top-level trailing-byte anomaly remains unauthenticated.
    decode_anomalies: Vec<DecodeAnomaly>,
}

impl RequestContext {
    #[cfg(test)]
    pub(crate) fn community(
        source: SocketAddr,
        version: CommunityVersion,
        community: Community,
        request_id: i32,
        pdu_type: PduType,
        decode_anomalies: Vec<DecodeAnomaly>,
    ) -> Self {
        Self::community_with_lifecycle(
            source,
            version,
            community,
            request_id,
            pdu_type,
            decode_anomalies,
            RequestLifecycle::for_test(pdu_type),
        )
    }

    pub(crate) fn community_with_lifecycle(
        source: SocketAddr,
        version: CommunityVersion,
        community: Community,
        request_id: i32,
        pdu_type: PduType,
        decode_anomalies: Vec<DecodeAnomaly>,
        lifecycle: RequestLifecycle,
    ) -> Self {
        let (version, security_model) = match version {
            CommunityVersion::V1 => (Version::V1, SecurityModel::V1),
            CommunityVersion::V2c => (Version::V2c, SecurityModel::V2c),
        };
        lifecycle.classify(pdu_type);
        Self {
            received_at: lifecycle.received_at,
            admitted_at: lifecycle.admitted_at,
            deadline: lifecycle.deadline,
            cancellation: lifecycle.cancellation,
            phase: lifecycle.phase,
            source,
            version,
            security_model,
            security_name: SecurityName::Community(community),
            security_level: SecurityLevel::NoAuthNoPriv,
            context_name: Bytes::new(),
            request_id,
            pdu_type,
            group_name: None,
            read_view: None,
            write_view: None,
            msg_max_size: None,
            decode_anomalies,
        }
    }

    #[cfg(test)]
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn usm(
        source: SocketAddr,
        username: Bytes,
        security_level: SecurityLevel,
        context_name: Bytes,
        request_id: i32,
        pdu_type: PduType,
        msg_max_size: usize,
        decode_anomalies: Vec<DecodeAnomaly>,
    ) -> Self {
        Self::usm_with_lifecycle(
            source,
            username,
            security_level,
            context_name,
            request_id,
            pdu_type,
            msg_max_size,
            decode_anomalies,
            RequestLifecycle::for_test(pdu_type),
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn usm_with_lifecycle(
        source: SocketAddr,
        username: Bytes,
        security_level: SecurityLevel,
        context_name: Bytes,
        request_id: i32,
        pdu_type: PduType,
        msg_max_size: usize,
        decode_anomalies: Vec<DecodeAnomaly>,
        lifecycle: RequestLifecycle,
    ) -> Self {
        lifecycle.classify(pdu_type);
        Self {
            received_at: lifecycle.received_at,
            admitted_at: lifecycle.admitted_at,
            deadline: lifecycle.deadline,
            cancellation: lifecycle.cancellation,
            phase: lifecycle.phase,
            source,
            version: Version::V3,
            security_model: SecurityModel::Usm,
            security_name: SecurityName::Usm(username),
            security_level,
            context_name,
            request_id,
            pdu_type,
            group_name: None,
            read_view: None,
            write_view: None,
            msg_max_size: Some(msg_max_size),
            decode_anomalies,
        }
    }

    /// Return the monotonic instant when the request datagram was received.
    #[must_use]
    pub const fn received_at(&self) -> tokio::time::Instant {
        self.received_at
    }

    /// Return the monotonic instant when the request received an execution permit.
    #[must_use]
    pub const fn admitted_at(&self) -> tokio::time::Instant {
        self.admitted_at
    }

    /// Return the absolute request deadline, measured from receipt.
    #[must_use]
    pub const fn deadline(&self) -> Option<tokio::time::Instant> {
        self.deadline
    }

    /// Clone the level-triggered advisory cancellation signal.
    ///
    /// The token is cancelled when the request deadline expires or agent
    /// shutdown begins. Cancellation-aware handlers should return normally
    /// after releasing their own resources. For SET, cancellation is observed
    /// before commit; once commit begins, undo, free, and finalize callbacks
    /// remain protected and must run to completion even though this token stays
    /// cancelled.
    #[must_use]
    pub fn cancellation_token(&self) -> CancellationToken {
        self.cancellation.clone()
    }

    /// Return whether request cancellation has already been signalled.
    #[must_use]
    pub fn is_cancelled(&self) -> bool {
        self.cancellation.is_cancelled()
    }

    /// Wait until request cancellation is signalled.
    ///
    /// This is advisory. In a protected SET terminal callback it remains
    /// signalled but does not authorize abandoning rollback or cleanup.
    pub async fn cancelled(&self) {
        self.cancellation.cancelled().await;
    }

    pub(crate) fn protect_set(&self) {
        self.phase
            .store(RequestTaskPhase::SetProtected as u8, Ordering::Release);
    }

    pub(crate) fn set_vacm_access(
        &mut self,
        group_name: Bytes,
        read_view: Bytes,
        write_view: Bytes,
    ) {
        self.group_name = Some(group_name);
        self.read_view = Some(read_view);
        self.write_view = Some(write_view);
    }

    /// Return the source address of the request.
    #[must_use]
    pub const fn source(&self) -> SocketAddr {
        self.source
    }

    /// Return the SNMP version.
    #[must_use]
    pub const fn version(&self) -> Version {
        self.version
    }

    /// Return the concrete security model used for the request.
    #[must_use]
    pub const fn security_model(&self) -> SecurityModel {
        self.security_model
    }

    /// Return the model-specific community identifier or USM username.
    #[must_use]
    pub const fn security_name(&self) -> &SecurityName {
        &self.security_name
    }

    /// Return the security level used for the request.
    #[must_use]
    pub const fn security_level(&self) -> SecurityLevel {
        self.security_level
    }

    /// Return the SNMPv3 context name as protocol octets.
    ///
    /// This is empty for community-based requests and need not be UTF-8.
    #[must_use]
    pub const fn context_name(&self) -> &Bytes {
        &self.context_name
    }

    /// Return the request ID from the PDU.
    #[must_use]
    pub const fn request_id(&self) -> i32 {
        self.request_id
    }

    /// Return the incoming PDU type.
    #[must_use]
    pub const fn pdu_type(&self) -> PduType {
        self.pdu_type
    }

    /// Return the VACM-resolved group name, if VACM was applied.
    #[must_use]
    pub const fn group_name(&self) -> Option<&Bytes> {
        self.group_name.as_ref()
    }

    /// Return the VACM-resolved read view name, if VACM was applied.
    #[must_use]
    pub const fn read_view(&self) -> Option<&Bytes> {
        self.read_view.as_ref()
    }

    /// Return the VACM-resolved write view name, if VACM was applied.
    #[must_use]
    pub const fn write_view(&self) -> Option<&Bytes> {
        self.write_view.as_ref()
    }

    /// Return the client-advertised maximum message size for SNMPv3.
    ///
    /// Community-based requests do not carry this field and return `None`.
    #[must_use]
    pub const fn msg_max_size(&self) -> Option<usize> {
        self.msg_max_size
    }

    /// Return accepted request decode anomalies in stable processing order.
    ///
    /// Community and `noAuthNoPriv` anomalies are not authenticated. On
    /// `authNoPriv` and `authPriv` requests, anomalies within the declared V3
    /// envelope are exposed only after successful USM authentication and can
    /// be attributed to the configured user. A top-level
    /// [`DecodeAnomaly::TrailingBytes`] describes bytes outside that envelope
    /// and remains unauthenticated.
    #[must_use]
    pub fn decode_anomalies(&self) -> &[DecodeAnomaly] {
        &self.decode_anomalies
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub(crate) enum RequestTaskPhase {
    Unclassified,
    Retrieval,
    SetPreCommit,
    SetProtected,
    Other,
}

#[derive(Debug, Clone)]
pub(crate) struct RequestLifecycle {
    pub(crate) received_at: tokio::time::Instant,
    pub(crate) admitted_at: tokio::time::Instant,
    pub(crate) deadline: Option<tokio::time::Instant>,
    pub(crate) cancellation: CancellationToken,
    pub(crate) phase: Arc<AtomicU8>,
}

impl RequestLifecycle {
    pub(crate) fn new(
        received_at: tokio::time::Instant,
        admitted_at: tokio::time::Instant,
        deadline: Option<tokio::time::Instant>,
        cancellation: CancellationToken,
        phase: Arc<AtomicU8>,
    ) -> Self {
        Self {
            received_at,
            admitted_at,
            deadline,
            cancellation,
            phase,
        }
    }

    #[cfg(test)]
    fn for_test(pdu_type: PduType) -> Self {
        let lifecycle = Self::standalone();
        lifecycle.classify(pdu_type);
        lifecycle
    }

    #[cfg(test)]
    pub(crate) fn standalone() -> Self {
        let now = tokio::time::Instant::now();
        Self::new(
            now,
            now,
            None,
            CancellationToken::new(),
            Arc::new(AtomicU8::new(RequestTaskPhase::Unclassified as u8)),
        )
    }

    fn classify(&self, pdu_type: PduType) {
        let phase = match pdu_type {
            PduType::GetRequest | PduType::GetNextRequest | PduType::GetBulkRequest => {
                RequestTaskPhase::Retrieval
            }
            PduType::SetRequest => RequestTaskPhase::SetPreCommit,
            _ => RequestTaskPhase::Other,
        };
        self.phase.store(phase as u8, Ordering::Release);
    }
}

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

    assert_impl_all!(RequestContext: Send, Sync, Clone, std::fmt::Debug);

    #[test]
    fn community_accessors_cover_v1_v2c_and_non_utf8_names() {
        let source = "192.0.2.55:6161".parse().unwrap();
        let community = Community::from(b"reader\xff".as_slice());

        for (community_version, version, model) in [
            (CommunityVersion::V1, Version::V1, SecurityModel::V1),
            (CommunityVersion::V2c, Version::V2c, SecurityModel::V2c),
        ] {
            let context = RequestContext::community(
                source,
                community_version,
                community.clone(),
                416,
                PduType::GetNextRequest,
                Vec::new(),
            );

            assert_eq!(context.source(), source);
            assert_eq!(context.version(), version);
            assert_eq!(context.security_model(), model);
            assert!(
                context
                    .security_name()
                    .matches(&SecurityName::Community(community.clone()))
            );
            assert_eq!(context.security_level(), SecurityLevel::NoAuthNoPriv);
            assert!(context.context_name().is_empty());
            assert_eq!(context.request_id(), 416);
            assert_eq!(context.pdu_type(), PduType::GetNextRequest);
            assert_eq!(context.group_name(), None);
            assert_eq!(context.read_view(), None);
            assert_eq!(context.write_view(), None);
            assert_eq!(context.msg_max_size(), None);
        }
    }

    #[test]
    fn usm_accessors_cover_every_security_level_and_octet_fields() {
        let source = "[2001:db8::1]:6161".parse().unwrap();
        let username = Bytes::from_static(b"operator\xff");
        let context_name = Bytes::from_static(b"tenant\x80");

        for level in [
            SecurityLevel::NoAuthNoPriv,
            SecurityLevel::AuthNoPriv,
            SecurityLevel::AuthPriv,
        ] {
            let mut context = RequestContext::usm(
                source,
                username.clone(),
                level,
                context_name.clone(),
                -17,
                PduType::SetRequest,
                4096,
                Vec::new(),
            );
            context.set_vacm_access(
                Bytes::from_static(b"operators\xfe"),
                Bytes::from_static(b"read\xfd"),
                Bytes::from_static(b"write\xfc"),
            );

            assert_eq!(context.source(), source);
            assert_eq!(context.version(), Version::V3);
            assert_eq!(context.security_model(), SecurityModel::Usm);
            assert_eq!(context.security_name().as_bytes(), username);
            assert_eq!(context.security_level(), level);
            assert_eq!(context.context_name(), &context_name);
            assert_eq!(context.request_id(), -17);
            assert_eq!(context.pdu_type(), PduType::SetRequest);
            assert_eq!(context.group_name().unwrap(), b"operators\xfe".as_slice());
            assert_eq!(context.read_view().unwrap(), b"read\xfd".as_slice());
            assert_eq!(context.write_view().unwrap(), b"write\xfc".as_slice());
            assert_eq!(context.msg_max_size(), Some(4096));
        }
    }

    #[test]
    fn debug_redacts_community_and_keeps_usm_username() {
        let community = RequestContext::community(
            "192.0.2.55:6161".parse().unwrap(),
            CommunityVersion::V2c,
            Community::from("community-redaction-sentinel-4d91"),
            416,
            PduType::GetRequest,
            Vec::new(),
        );
        let rendered = format!("{community:?}");
        assert!(rendered.contains("REDACTED"));
        assert!(!rendered.contains("community-redaction-sentinel-4d91"));
        assert!(rendered.contains("192.0.2.55:6161"));
        assert!(rendered.contains("416"));

        let usm = RequestContext::usm(
            "192.0.2.55:6161".parse().unwrap(),
            Bytes::from_static(b"visible-usm-user"),
            SecurityLevel::AuthNoPriv,
            Bytes::new(),
            417,
            PduType::GetRequest,
            4096,
            Vec::new(),
        );
        let rendered = format!("{usm:#?}");
        assert!(rendered.contains("visible-usm-user"));
        assert!(rendered.contains("192.0.2.55:6161"));
    }
}