mib-rs 0.10.0

SNMP MIB parser and resolver
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
use mib_rs::mib::Symbol;
use mib_rs::source::{ByteOffset, Position, PositionEncoding, memory_modules};
use mib_rs::{DiagnosticConfig, Loader, ResolverStrictness, SemanticSpan, SemanticSpanKind};

fn accepts_root_reexport(_: SemanticSpan<'_>, _: SemanticSpanKind) {}

const BASE: &[u8] = br#"NAV-BASE-MIB DEFINITIONS ::= BEGIN
IMPORTS
    enterprises
        FROM SNMPv2-SMI;

NavText ::= OCTET STRING
NavInteger ::= INTEGER

baseRoot OBJECT IDENTIFIER ::= { enterprises 91000 }

END
"#;

const FORWARD: &[u8] = br#"NAV-FORWARD-MIB DEFINITIONS ::= BEGIN
IMPORTS
    baseRoot
        FROM NAV-BASE-MIB;
END
"#;

const USER_TEXT: &str = r#"-- 😀 navigation fixture
NAV-USER-MIB DEFINITIONS ::= BEGIN
IMPORTS
    baseRoot
        FROM NAV-FORWARD-MIB
    NavText
        FROM NAV-BASE-MIB
    NavInteger
        FROM NAV-BASE-MIB
    MissingSymbol
        FROM NAV-BASE-MIB;

userRoot OBJECT IDENTIFIER ::= { baseRoot 1 }

qualifiedRoot OBJECT IDENTIFIER ::= { NAV-BASE-MIB.baseRoot 2 }

numberedRoot OBJECT IDENTIFIER ::= { iso(1) 3 }

qualifiedNumbered OBJECT IDENTIFIER ::= { NAV-BASE-MIB.baseRoot(91000) 4 }

unknownQualified OBJECT IDENTIFIER ::= { NAV-BASE-MIB.missingQualified 3 }

typedValue OBJECT-TYPE
    SYNTAX NavText
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "typed"
    ::= { userRoot 1 }

restrictedValue OBJECT-TYPE
    SYNTAX NavInteger { one(1), two(2) }
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "restricted"
    ::= { userRoot 11 }

wrongKindTyped OBJECT-TYPE
    SYNTAX baseRoot
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "wrong kind type"
    ::= { userRoot 16 }

missingImportedTyped OBJECT-TYPE
    SYNTAX MissingSymbol
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "missing imported type"
    ::= { userRoot 17 }

unknownTyped OBJECT-TYPE
    SYNTAX MissingType
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "unknown type"
    ::= { userRoot 2 }

resolvedDefault OBJECT-TYPE
    SYNTAX OBJECT IDENTIFIER
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "resolved default"
    DEFVAL { { baseRoot 7 } }
    ::= { userRoot 12 }

annotatedDefault OBJECT-TYPE
    SYNTAX OBJECT IDENTIFIER
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "annotated default"
    DEFVAL { { baseRoot(91000) 8 } }
    ::= { userRoot 13 }

qualifiedDefault OBJECT-TYPE
    SYNTAX OBJECT IDENTIFIER
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "qualified default"
    DEFVAL { { iso(1) NAV-BASE-MIB.baseRoot 9 } }
    ::= { userRoot 14 }

unresolvedDefault OBJECT-TYPE
    SYNTAX OBJECT IDENTIFIER
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "unresolved default"
    DEFVAL { { missingDefaultRoot 10 } }
    ::= { userRoot 15 }

unknownOid OBJECT IDENTIFIER ::= { missingRoot 1 }
wrongKindOid OBJECT IDENTIFIER ::= { NavText 1 }
missingImportedOid OBJECT IDENTIFIER ::= { MissingSymbol 1 }

END
"#;
const USER: &[u8] = USER_TEXT.as_bytes();

const UNIMPORTED_ROOT: &[u8] = br#"NAV-UNIMPORTED-MIB DEFINITIONS ::= BEGIN
unimportedRoot OBJECT IDENTIFIER ::= { enterprises 91919 }
END
"#;

const REFERENCE_TARGET: &[u8] = br#"NAV-REF-TARGET-MIB DEFINITIONS ::= BEGIN
IMPORTS
    enterprises, OBJECT-TYPE, NOTIFICATION-TYPE, Integer32
        FROM SNMPv2-SMI
    OBJECT-GROUP, NOTIFICATION-GROUP
        FROM SNMPv2-CONF;

targetRoot OBJECT IDENTIFIER ::= { enterprises 92000 }

targetObject OBJECT-TYPE
    SYNTAX Integer32
    MAX-ACCESS read-create
    STATUS current
    DESCRIPTION "object"
    ::= { targetRoot 1 }

targetNotification NOTIFICATION-TYPE
    STATUS current
    DESCRIPTION "notification"
    ::= { targetRoot 2 }

targetObjectGroup OBJECT-GROUP
    OBJECTS { targetObject }
    STATUS current
    DESCRIPTION "objects"
    ::= { targetRoot 3 }

targetNotificationGroup NOTIFICATION-GROUP
    NOTIFICATIONS { targetNotification }
    STATUS current
    DESCRIPTION "notifications"
    ::= { targetRoot 4 }
END
"#;

const REFERENCE_USER: &[u8] = br#"NAV-REF-USER-MIB DEFINITIONS ::= BEGIN
IMPORTS
    enterprises, OBJECT-TYPE, NOTIFICATION-TYPE, Integer32
        FROM SNMPv2-SMI
    OBJECT-GROUP, NOTIFICATION-GROUP, MODULE-COMPLIANCE, AGENT-CAPABILITIES
        FROM SNMPv2-CONF
    targetRoot, targetObject, targetNotification,
    targetObjectGroup, targetNotificationGroup
        FROM NAV-REF-TARGET-MIB;

userRefRoot OBJECT IDENTIFIER ::= { enterprises 92001 }

LocalAlias ::= Integer32

localNotification NOTIFICATION-TYPE
    STATUS current
    DESCRIPTION "shares targetObject OID"
    ::= { enterprises 92000 1 }

localObject OBJECT-TYPE
    SYNTAX Integer32
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "shares targetNotification OID"
    ::= { enterprises 92000 2 }

mixedObjectGroup OBJECT-GROUP
    OBJECTS { localNotification, targetObject, targetNotification, missingMember }
    STATUS current
    DESCRIPTION "mixed"
    ::= { userRefRoot 1 }

mixedNotificationGroup NOTIFICATION-GROUP
    NOTIFICATIONS { localObject, targetNotification, targetObject, missingNotification }
    STATUS current
    DESCRIPTION "mixed"
    ::= { userRefRoot 2 }

referenceCapabilities AGENT-CAPABILITIES
    PRODUCT-RELEASE "test"
    STATUS current
    DESCRIPTION "references"
    SUPPORTS NAV-REF-TARGET-MIB
        INCLUDES { targetObjectGroup, targetObject, missingInclude }
        VARIATION targetObject
            SYNTAX Integer32
            CREATION-REQUIRES { targetObject, targetNotification, missingCreation }
            DEFVAL { { iso(1) targetRoot(92000) NAV-REF-TARGET-MIB.targetRoot missingVariationRoot 21 } }
            DESCRIPTION "variation"
    ::= { userRefRoot 3 }

referenceCompliance MODULE-COMPLIANCE
    STATUS current
    DESCRIPTION "nested type references"
    MODULE NAV-REF-TARGET-MIB
        OBJECT targetObject
            SYNTAX Integer32
            WRITE-SYNTAX Integer32
            MIN-ACCESS read-only
            DESCRIPTION "refinement"
    ::= { userRefRoot 4 }
END
"#;

fn load_fixture() -> mib_rs::Mib {
    Loader::new()
        .source(memory_modules([
            ("NAV-BASE-MIB", BASE),
            ("NAV-FORWARD-MIB", FORWARD),
            ("NAV-USER-MIB", USER),
        ]))
        .modules(["NAV-USER-MIB"])
        .diagnostic_config(DiagnosticConfig::silent())
        .load()
        .expect("navigation fixture should load")
}

fn load_reference_fixture() -> mib_rs::Mib {
    Loader::new()
        .source(memory_modules([
            ("NAV-REF-TARGET-MIB", REFERENCE_TARGET),
            ("NAV-REF-USER-MIB", REFERENCE_USER),
        ]))
        .modules(["NAV-REF-USER-MIB"])
        .resolver_strictness(ResolverStrictness::Permissive)
        .diagnostic_config(DiagnosticConfig::silent())
        .load()
        .expect("reference navigation fixture should load")
}

fn offset(source: &[u8], needle: &[u8], occurrence: usize) -> ByteOffset {
    let start = source
        .windows(needle.len())
        .enumerate()
        .filter(|(_, window)| *window == needle)
        .nth(occurrence)
        .map(|(index, _)| index)
        .unwrap_or_else(|| panic!("missing occurrence {occurrence} of {:?}", needle));
    ByteOffset::try_from(start).unwrap()
}

#[test]
fn resolves_definitions_imports_oid_components_and_type_references() {
    let mib = load_fixture();
    let module = mib.module("NAV-USER-MIB").unwrap();

    let definition = module.semantic_at(offset(USER, b"typedValue", 0)).unwrap();
    accepts_root_reexport(definition, definition.kind);
    assert_eq!(definition.kind, SemanticSpanKind::Definition);
    assert_eq!(definition.declared_name, "typedValue");
    assert!(matches!(definition.symbol, Some(Symbol::Object(_))));
    assert_eq!(
        definition.module.map(|id| mib.raw().module(id).name()),
        Some("NAV-USER-MIB")
    );

    let forwarded_import = module.semantic_at(offset(USER, b"baseRoot", 0)).unwrap();
    assert_eq!(forwarded_import.kind, SemanticSpanKind::Import);
    assert_eq!(forwarded_import.declared_name, "baseRoot");
    assert_eq!(forwarded_import.symbol.unwrap().name(&mib), "baseRoot");
    assert_eq!(
        forwarded_import
            .module
            .map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );

    let direct_import = module.semantic_at(offset(USER, b"NavText", 0)).unwrap();
    assert_eq!(direct_import.kind, SemanticSpanKind::Import);
    assert!(matches!(direct_import.symbol, Some(Symbol::Type(_))));
    assert_eq!(
        direct_import.module.map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );

    let oid = module.semantic_at(offset(USER, b"baseRoot", 1)).unwrap();
    assert_eq!(oid.kind, SemanticSpanKind::OidReference);
    assert_eq!(oid.declared_name, "baseRoot");
    assert_eq!(oid.symbol.unwrap().name(&mib), "baseRoot");

    let qualified = module
        .semantic_at(offset(USER, b"NAV-BASE-MIB.baseRoot", 0))
        .unwrap();
    assert_eq!(qualified.kind, SemanticSpanKind::OidReference);
    assert_eq!(qualified.declared_name, "NAV-BASE-MIB.baseRoot");
    assert_eq!(
        module.source().unwrap().slice(qualified.range).unwrap(),
        b"NAV-BASE-MIB.baseRoot"
    );
    assert_eq!(qualified.symbol.unwrap().name(&mib), "baseRoot");
    assert_eq!(
        qualified.module.map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );

    let named_number = module.semantic_at(offset(USER, b"iso(1)", 0)).unwrap();
    assert_eq!(named_number.kind, SemanticSpanKind::OidReference);
    assert_eq!(named_number.declared_name, "iso");
    assert_eq!(
        module.source().unwrap().slice(named_number.range).unwrap(),
        b"iso"
    );

    let qualified_number = module
        .semantic_at(offset(USER, b"NAV-BASE-MIB.baseRoot(91000)", 0))
        .unwrap();
    assert_eq!(qualified_number.kind, SemanticSpanKind::OidReference);
    assert_eq!(qualified_number.declared_name, "NAV-BASE-MIB.baseRoot");
    assert_eq!(
        module
            .source()
            .unwrap()
            .slice(qualified_number.range)
            .unwrap(),
        b"NAV-BASE-MIB.baseRoot"
    );

    let type_reference = module.semantic_at(offset(USER, b"NavText", 1)).unwrap();
    assert_eq!(type_reference.kind, SemanticSpanKind::TypeReference);
    assert_eq!(type_reference.declared_name, "NavText");
    assert!(matches!(type_reference.symbol, Some(Symbol::Type(_))));
    assert_eq!(
        module
            .source()
            .unwrap()
            .slice(type_reference.range)
            .unwrap(),
        b"NavText"
    );

    let restricted_base = module.semantic_at(offset(USER, b"NavInteger", 1)).unwrap();
    assert_eq!(restricted_base.kind, SemanticSpanKind::TypeReference);
    assert_eq!(restricted_base.declared_name, "NavInteger");
    assert!(matches!(restricted_base.symbol, Some(Symbol::Type(_))));
    assert_eq!(
        module
            .source()
            .unwrap()
            .slice(restricted_base.range)
            .unwrap(),
        b"NavInteger"
    );
}

#[test]
fn unresolved_references_keep_exact_declared_name_and_range() {
    let mib = load_fixture();
    let module = mib.module("NAV-USER-MIB").unwrap();
    let source = module.source().unwrap();

    let missing_import = module
        .semantic_at(offset(USER, b"MissingSymbol", 0))
        .unwrap();
    assert_eq!(missing_import.kind, SemanticSpanKind::Import);
    assert_eq!(missing_import.declared_name, "MissingSymbol");
    assert_eq!(
        source.slice(missing_import.range).unwrap(),
        b"MissingSymbol"
    );
    assert_eq!(missing_import.symbol, None);
    assert_eq!(
        missing_import.module.map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );

    let missing_type = module.semantic_at(offset(USER, b"MissingType", 0)).unwrap();
    assert_eq!(missing_type.kind, SemanticSpanKind::TypeReference);
    assert_eq!(missing_type.declared_name, "MissingType");
    assert_eq!(source.slice(missing_type.range).unwrap(), b"MissingType");
    assert_eq!(missing_type.symbol, None);
    assert_eq!(missing_type.module, None);

    let missing_oid = module.semantic_at(offset(USER, b"missingRoot", 0)).unwrap();
    assert_eq!(missing_oid.kind, SemanticSpanKind::OidReference);
    assert_eq!(missing_oid.declared_name, "missingRoot");
    assert_eq!(source.slice(missing_oid.range).unwrap(), b"missingRoot");
    assert_eq!(missing_oid.symbol, None);
    assert_eq!(missing_oid.module, None);

    let wrong_type = module
        .semantic_at(offset(USER, b"baseRoot\n    MAX-ACCESS", 0))
        .unwrap();
    assert_eq!(wrong_type.kind, SemanticSpanKind::TypeReference);
    assert!(matches!(wrong_type.symbol, Some(Symbol::Node(_))));
    assert_eq!(
        wrong_type.module.map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );

    let missing_imported_type = module
        .semantic_at(offset(USER, b"MissingSymbol\n    MAX-ACCESS", 0))
        .unwrap();
    assert_eq!(missing_imported_type.kind, SemanticSpanKind::TypeReference);
    assert_eq!(missing_imported_type.symbol, None);
    assert_eq!(
        missing_imported_type
            .module
            .map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );

    let wrong_oid = module.semantic_at(offset(USER, b"NavText 1 }", 0)).unwrap();
    assert_eq!(wrong_oid.kind, SemanticSpanKind::OidReference);
    assert!(matches!(wrong_oid.symbol, Some(Symbol::Type(_))));
    assert_eq!(
        wrong_oid.module.map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );

    let missing_imported_oid = module
        .semantic_at(offset(USER, b"MissingSymbol 1 }", 0))
        .unwrap();
    assert_eq!(missing_imported_oid.kind, SemanticSpanKind::OidReference);
    assert_eq!(missing_imported_oid.symbol, None);
    assert_eq!(
        missing_imported_oid
            .module
            .map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );

    let missing_qualified = module
        .semantic_at(offset(USER, b"NAV-BASE-MIB.missingQualified", 0))
        .unwrap();
    assert_eq!(missing_qualified.kind, SemanticSpanKind::OidReference);
    assert_eq!(
        missing_qualified.declared_name,
        "NAV-BASE-MIB.missingQualified"
    );
    assert_eq!(missing_qualified.symbol, None);
    assert_eq!(
        missing_qualified
            .module
            .map(|id| mib.raw().module(id).name()),
        Some("NAV-BASE-MIB")
    );
}

#[test]
fn object_type_defval_oid_components_are_indexed() {
    let mib = load_fixture();
    let module = mib.module("NAV-USER-MIB").unwrap();
    let source = module.source().unwrap();

    let resolved = module.semantic_at(offset(USER, b"baseRoot 7", 0)).unwrap();
    assert_eq!(resolved.kind, SemanticSpanKind::OidReference);
    assert_eq!(resolved.declared_name, "baseRoot");
    assert_eq!(resolved.symbol.unwrap().name(&mib), "baseRoot");

    let annotated = module
        .semantic_at(offset(USER, b"baseRoot(91000) 8", 0))
        .unwrap();
    assert_eq!(annotated.kind, SemanticSpanKind::OidReference);
    assert_eq!(source.slice(annotated.range).unwrap(), b"baseRoot");

    let qualified = module
        .semantic_at(offset(USER, b"NAV-BASE-MIB.baseRoot 9", 0))
        .unwrap();
    assert_eq!(qualified.kind, SemanticSpanKind::OidReference);
    assert_eq!(qualified.declared_name, "NAV-BASE-MIB.baseRoot");
    assert_eq!(qualified.symbol.unwrap().name(&mib), "baseRoot");

    let unresolved = module
        .semantic_at(offset(USER, b"missingDefaultRoot", 0))
        .unwrap();
    assert_eq!(unresolved.kind, SemanticSpanKind::OidReference);
    assert_eq!(unresolved.declared_name, "missingDefaultRoot");
    assert_eq!(
        source.slice(unresolved.range).unwrap(),
        b"missingDefaultRoot"
    );
    assert_eq!(unresolved.symbol, None);
}

#[test]
fn oid_navigation_mirrors_constrained_smi_root_fallbacks() {
    let load = |strictness| {
        Loader::new()
            .source(mib_rs::source::memory(
                "NAV-UNIMPORTED-MIB",
                UNIMPORTED_ROOT,
            ))
            .modules(["NAV-UNIMPORTED-MIB"])
            .resolver_strictness(strictness)
            .diagnostic_config(DiagnosticConfig::silent())
            .load()
            .unwrap()
    };

    for strictness in [ResolverStrictness::Normal, ResolverStrictness::Permissive] {
        let mib = load(strictness);
        let span = mib
            .module("NAV-UNIMPORTED-MIB")
            .unwrap()
            .semantic_at(offset(UNIMPORTED_ROOT, b"enterprises", 0))
            .unwrap();
        assert_eq!(span.kind, SemanticSpanKind::OidReference);
        assert_eq!(span.symbol.unwrap().name(&mib), "enterprises");
        assert_eq!(
            span.module.map(|id| mib.raw().module(id).name()),
            Some("SNMPv2-SMI")
        );
    }

    let strict = load(ResolverStrictness::Strict);
    let span = strict
        .module("NAV-UNIMPORTED-MIB")
        .unwrap()
        .semantic_at(offset(UNIMPORTED_ROOT, b"enterprises", 0))
        .unwrap();
    assert_eq!(span.symbol, None);
    assert_eq!(span.module, None);
}

#[test]
fn retained_name_refs_keep_exact_identity_scope_kind_and_order() {
    let mib = load_reference_fixture();
    let module = mib.module("NAV-REF-USER-MIB").unwrap();
    let target_module = mib.module("NAV-REF-TARGET-MIB").unwrap().id();

    let cases = [
        ("localNotification", 1, "localNotification", "Notification"),
        ("targetObject", 3, "targetObject", "Object"),
        (
            "targetNotification",
            3,
            "targetNotification",
            "Notification",
        ),
        ("missingMember", 0, "missingMember", "Missing"),
        ("localObject", 1, "localObject", "Object"),
        (
            "targetNotification",
            4,
            "targetNotification",
            "Notification",
        ),
        ("targetObject", 4, "targetObject", "Object"),
        ("missingNotification", 0, "missingNotification", "Missing"),
        ("targetObjectGroup", 1, "targetObjectGroup", "Group"),
        ("targetObject", 6, "targetObject", "Object"),
        ("missingInclude", 0, "missingInclude", "MissingTarget"),
        ("targetObject", 8, "targetObject", "Object"),
        (
            "targetNotification",
            5,
            "targetNotification",
            "Notification",
        ),
        ("missingCreation", 0, "missingCreation", "MissingTarget"),
    ];

    for (needle, occurrence, expected_name, expected_kind) in cases {
        let span = module
            .semantic_at(offset(REFERENCE_USER, needle.as_bytes(), occurrence))
            .unwrap_or_else(|| panic!("missing span for {needle} occurrence {occurrence}"));
        assert_eq!(
            span.kind,
            SemanticSpanKind::SymbolReference,
            "{needle} occurrence {occurrence}"
        );
        assert_eq!(span.declared_name, expected_name);
        assert_eq!(
            module.source().unwrap().slice(span.range).unwrap(),
            expected_name.as_bytes()
        );
        match expected_kind {
            "Object" => assert!(matches!(span.symbol, Some(Symbol::Object(_)))),
            "Notification" => {
                assert!(matches!(span.symbol, Some(Symbol::Notification(_))))
            }
            "Group" => assert!(matches!(span.symbol, Some(Symbol::Group(_)))),
            "Missing" => {
                assert_eq!(span.symbol, None);
                assert_eq!(span.module, None);
            }
            "MissingTarget" => {
                assert_eq!(span.symbol, None);
                assert_eq!(span.module, Some(target_module));
            }
            _ => unreachable!(),
        }
    }

    let variation_defval = module
        .semantic_at(offset(
            REFERENCE_USER,
            b"NAV-REF-TARGET-MIB.targetRoot missingVariationRoot",
            0,
        ))
        .unwrap();
    assert_eq!(variation_defval.kind, SemanticSpanKind::OidReference);
    assert_eq!(
        variation_defval.declared_name,
        "NAV-REF-TARGET-MIB.targetRoot"
    );
    assert_eq!(variation_defval.module, Some(target_module));
    assert_eq!(variation_defval.symbol.unwrap().name(&mib), "targetRoot");

    let variation_annotated = module
        .semantic_at(offset(REFERENCE_USER, b"targetRoot(92000)", 0))
        .unwrap();
    assert_eq!(variation_annotated.kind, SemanticSpanKind::OidReference);
    assert_eq!(variation_annotated.declared_name, "targetRoot");
    assert_eq!(variation_annotated.module, Some(target_module));
    assert_eq!(
        module
            .source()
            .unwrap()
            .slice(variation_annotated.range)
            .unwrap(),
        b"targetRoot"
    );

    let variation_unresolved = module
        .semantic_at(offset(REFERENCE_USER, b"missingVariationRoot", 0))
        .unwrap();
    assert_eq!(variation_unresolved.kind, SemanticSpanKind::OidReference);
    assert_eq!(variation_unresolved.declared_name, "missingVariationRoot");
    assert_eq!(variation_unresolved.symbol, None);
    assert_eq!(variation_unresolved.module, None);

    for needle in [
        b"LocalAlias ::= Integer32".as_slice(),
        b"SYNTAX Integer32\n            CREATION".as_slice(),
        b"SYNTAX Integer32\n            WRITE-SYNTAX".as_slice(),
        b"WRITE-SYNTAX Integer32".as_slice(),
    ] {
        let offset = offset(REFERENCE_USER, needle, 0);
        let integer = needle
            .windows(b"Integer32".len())
            .position(|window| window == b"Integer32")
            .unwrap();
        let span = module
            .semantic_at(ByteOffset::new(offset.get() + integer as u32))
            .unwrap();
        assert_eq!(span.kind, SemanticSpanKind::TypeReference);
        assert_eq!(span.declared_name, "Integer32");
        assert!(matches!(span.symbol, Some(Symbol::Type(_))));
    }
}

#[test]
fn boundaries_source_identity_and_editor_positions_are_explicit() {
    let mib = load_fixture();
    let module = mib.module("NAV-USER-MIB").unwrap();
    let source = module.source().unwrap();
    let target = offset(USER, b"NavText", 1);
    let context = module.semantic_at(target).unwrap();

    assert_eq!(
        module.semantic_at(context.range.end()).unwrap().kind,
        SemanticSpanKind::Definition
    );
    assert!(module.semantic_at(source.len()).is_none());
    assert!(
        module
            .semantic_at(ByteOffset::new(source.len().get() + 1))
            .is_none()
    );
    assert!(module.semantic_at(ByteOffset::new(0)).is_none());

    let foreign_source = mib.module("NAV-BASE-MIB").unwrap().source_id().unwrap();
    assert!(module.semantic_at_source(foreign_source, target).is_none());
    assert_eq!(
        module
            .semantic_at_source(module.source_id().unwrap(), target)
            .unwrap()
            .declared_name,
        "NavText"
    );

    let target_position = source.position(target, PositionEncoding::Utf16).unwrap();
    assert_eq!(
        module
            .semantic_at_source_position(
                foreign_source,
                Position::new(u32::MAX, u32::MAX),
                PositionEncoding::Utf16,
            )
            .unwrap(),
        None,
        "a source mismatch is reported before position conversion"
    );
    assert_eq!(
        module
            .semantic_at_source_position(
                module.source_id().unwrap(),
                target_position,
                PositionEncoding::Utf16,
            )
            .unwrap()
            .unwrap()
            .declared_name,
        "NavText"
    );

    for encoding in [
        PositionEncoding::Utf8,
        PositionEncoding::Utf16,
        PositionEncoding::Utf32,
    ] {
        let position = source.position(target, encoding).unwrap();
        assert_eq!(
            module
                .semantic_at_position(position, encoding)
                .unwrap()
                .unwrap()
                .declared_name,
            "NavText"
        );
    }

    assert!(
        module
            .semantic_at_position(Position::new(0, 4), PositionEncoding::Utf8)
            .is_err(),
        "a UTF-8 position in the middle of the emoji must be rejected"
    );
    assert!(
        module
            .semantic_at_position(Position::new(u32::MAX, 0), PositionEncoding::Utf16)
            .is_err()
    );
}

#[test]
fn module_header_trivia_and_embedded_sources_have_defined_behavior() {
    let mib = load_fixture();
    let module = mib.module("NAV-USER-MIB").unwrap();
    assert!(
        module
            .semantic_at(offset(USER, b"NAV-USER-MIB", 0))
            .is_none()
    );
    assert!(module.semantic_at(ByteOffset::new(1)).is_none());
    let between_definitions = offset(USER, b"\n\nqualifiedRoot", 0);
    assert!(
        module
            .semantic_at(ByteOffset::new(between_definitions.get() + 1))
            .is_none()
    );
    assert!(module.semantic_at(offset(USER, b"END", 0)).is_none());

    let embedded = Loader::new()
        .modules(["SNMPv2-SMI"])
        .diagnostic_config(DiagnosticConfig::silent())
        .load()
        .expect("embedded base module should load");
    let base = embedded.module("SNMPv2-SMI").unwrap();
    let source = base.source().expect("embedded source is retained");
    let enterprises = source
        .bytes()
        .windows(b"enterprises".len())
        .position(|window| window == b"enterprises")
        .unwrap();
    assert!(
        base.semantic_at(ByteOffset::try_from(enterprises).unwrap())
            .is_some()
    );
}