mib-rs 0.9.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
use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};

use mib_rs::source;

use mib_rs::source::{FindResult, Source};
use mib_rs::{DiagCode, DiagnosticConfig, Loader, Severity};

struct AdvertisedSource {
    modules: Vec<String>,
    content: HashMap<String, Vec<u8>>,
    label: &'static str,
}

struct DuplicateCandidateSource {
    name: &'static str,
    candidates: Vec<Vec<u8>>,
    label: &'static str,
}

struct ErrorSource;

struct SharedPathSource;

impl AdvertisedSource {
    fn new(label: &'static str, modules: &[&str], content: &[(&str, &[u8])]) -> Self {
        Self {
            modules: modules.iter().map(|name| (*name).to_string()).collect(),
            content: content
                .iter()
                .map(|(name, bytes)| ((*name).to_string(), bytes.to_vec()))
                .collect(),
            label,
        }
    }
}

impl Source for AdvertisedSource {
    fn find(&self, name: &str) -> io::Result<Option<FindResult>> {
        Ok(self.content.get(name).map(|content| FindResult {
            content: content.clone(),
            path: PathBuf::from(format!("<{}:{name}>", self.label)),
        }))
    }

    fn list_modules(&self) -> io::Result<Vec<String>> {
        Ok(self.modules.clone())
    }
}

impl Source for DuplicateCandidateSource {
    fn find(&self, name: &str) -> io::Result<Option<FindResult>> {
        self.find_candidates(name).next().transpose()
    }

    fn find_candidates<'a>(
        &'a self,
        name: &'a str,
    ) -> Box<dyn Iterator<Item = io::Result<FindResult>> + 'a> {
        Box::new(
            self.candidates
                .iter()
                .filter(move |_| name == self.name)
                .map(move |content| {
                    Ok(FindResult {
                        content: content.clone(),
                        path: PathBuf::from(format!("<{}:{name}>", self.label)),
                    })
                }),
        )
    }

    fn list_modules(&self) -> io::Result<Vec<String>> {
        Ok(vec![self.name.to_string()])
    }
}

impl Source for ErrorSource {
    fn find(&self, _name: &str) -> io::Result<Option<FindResult>> {
        Err(io::Error::other("lower-priority source accessed"))
    }

    fn list_modules(&self) -> io::Result<Vec<String>> {
        Ok(vec!["REAL-MIB".to_string()])
    }
}

impl Source for SharedPathSource {
    fn find(&self, name: &str) -> io::Result<Option<FindResult>> {
        let content = match name {
            "A-MIB" => b"A-MIB DEFINITIONS ::= BEGIN\nEND\n".as_slice(),
            "B-MIB" => b"B-MIB DEFINITIONS ::= BEGIN\nEND\n".as_slice(),
            _ => return Ok(None),
        };
        Ok(Some(FindResult {
            content: content.to_vec(),
            path: PathBuf::from("<shared>"),
        }))
    }

    fn list_modules(&self) -> io::Result<Vec<String>> {
        Ok(vec!["A-MIB".to_string(), "B-MIB".to_string()])
    }
}

fn permissive_diagnostics() -> DiagnosticConfig {
    let mut config = DiagnosticConfig::verbose();
    config.fail_at = Severity::Fatal;
    config
}

static TEMP_ID: AtomicUsize = AtomicUsize::new(0);

struct TempDir(PathBuf);

impl TempDir {
    fn new() -> Self {
        let id = TEMP_ID.fetch_add(1, Ordering::Relaxed);
        let path =
            std::env::temp_dir().join(format!("mib-rs-source-scanner-{}-{id}", std::process::id()));
        std::fs::create_dir(&path).expect("create test directory");
        Self(path)
    }

    fn path(&self) -> &Path {
        &self.0
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

const MALFORMED: &[u8] = b"LEADING-TOKEN REAL-MIB DEFINITIONS ::= BEGIN\nEND\n";
const PHANTOM: &[u8] = b"OTHER-MIB DEFINITIONS ::= BEGIN\nEND\n";
const VALID: &[u8] = b"REAL-MIB DEFINITIONS ::= BEGIN\nEND\n";

fn assert_real_mib_loaded(loader: Loader, expected_path: &Path) {
    let mib = loader
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("valid candidate should load");
    let module = mib.module("REAL-MIB").expect("REAL-MIB should be loaded");
    assert_eq!(module.source_path(), expected_path.to_string_lossy());
}

#[test]
fn file_source_defers_reserved_module_name_to_parser_policy() {
    let dir = TempDir::new();
    let path = dir.path().join("reserved-name.mib");
    std::fs::write(&path, b"TRUE DEFINITIONS ::= BEGIN\nEND\n")
        .expect("write reserved-name module");

    let file = source::file(&path).expect("scanner should retain parser-accepted module name");
    let mib = Loader::new()
        .source(file)
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("permissive parser policy should allow reserved module name");

    assert!(mib.module("TRUE").is_some());
    assert!(
        mib.diagnostics()
            .iter()
            .any(|diagnostic| diagnostic.code == DiagCode::KeywordReserved),
        "parser should diagnose the reserved module name"
    );
}

#[test]
fn explicit_load_continues_after_phantom_source_candidate() {
    let phantom = AdvertisedSource::new(
        "phantom",
        &["REAL-MIB"],
        &[(
            "REAL-MIB",
            br#"DESCRIPTION "REAL-MIB DEFINITIONS ::= BEGIN"
OTHER-MIB DEFINITIONS ::= BEGIN
END
"#,
        )],
    );
    let valid = AdvertisedSource::new(
        "valid",
        &["REAL-MIB"],
        &[("REAL-MIB", b"REAL-MIB DEFINITIONS ::= BEGIN\nEND\n")],
    );

    let mib = Loader::new()
        .source(Box::new(phantom))
        .source(Box::new(valid))
        .modules(["REAL-MIB"])
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("later valid source should satisfy the requested module");

    let module = mib.module("REAL-MIB").expect("REAL-MIB should be loaded");
    assert_eq!(module.source_path(), "<valid:REAL-MIB>");
}

#[test]
fn explicit_chain_load_continues_after_phantom_child() {
    let phantom = AdvertisedSource::new(
        "phantom",
        &["REAL-MIB"],
        &[("REAL-MIB", b"OTHER-MIB DEFINITIONS ::= BEGIN\nEND\n")],
    );
    let valid = AdvertisedSource::new(
        "valid",
        &["REAL-MIB"],
        &[("REAL-MIB", b"REAL-MIB DEFINITIONS ::= BEGIN\nEND\n")],
    );

    let mib = Loader::new()
        .source(source::chain(vec![Box::new(phantom), Box::new(valid)]))
        .modules(["REAL-MIB"])
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("later valid chain child should satisfy the requested module");

    let module = mib.module("REAL-MIB").expect("REAL-MIB should be loaded");
    assert_eq!(module.source_path(), "<valid:REAL-MIB>");
}

#[test]
fn load_all_chain_continues_after_phantom_child() {
    let phantom = AdvertisedSource::new(
        "phantom",
        &["REAL-MIB"],
        &[("REAL-MIB", b"OTHER-MIB DEFINITIONS ::= BEGIN\nEND\n")],
    );
    let valid = AdvertisedSource::new(
        "valid",
        &["REAL-MIB"],
        &[("REAL-MIB", b"REAL-MIB DEFINITIONS ::= BEGIN\nEND\n")],
    );

    let mib = Loader::new()
        .source(source::chain(vec![Box::new(phantom), Box::new(valid)]))
        .parallelism(2)
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("later valid chain child should win after candidate validation");

    let module = mib.module("REAL-MIB").expect("REAL-MIB should be loaded");
    assert_eq!(module.source_path(), "<valid:REAL-MIB>");
}

#[test]
fn load_all_continues_after_phantom_source_candidate() {
    let phantom = AdvertisedSource::new(
        "phantom",
        &["REAL-MIB"],
        &[(
            "REAL-MIB",
            br#"DESCRIPTION "REAL-MIB DEFINITIONS ::= BEGIN"
OTHER-MIB DEFINITIONS ::= BEGIN
END
"#,
        )],
    );
    let valid = AdvertisedSource::new(
        "valid",
        &["REAL-MIB"],
        &[("REAL-MIB", b"REAL-MIB DEFINITIONS ::= BEGIN\nEND\n")],
    );

    let mib = Loader::new()
        .source(Box::new(phantom))
        .source(Box::new(valid))
        .parallelism(2)
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("later valid source should win after candidate validation");

    let module = mib.module("REAL-MIB").expect("REAL-MIB should be loaded");
    assert_eq!(module.source_path(), "<valid:REAL-MIB>");
    assert!(mib.module("OTHER-MIB").is_none());
}

fn write_malformed_and_valid_files(dir: &TempDir) -> (PathBuf, PathBuf) {
    let malformed = dir.path().join("01-malformed.mib");
    let valid = dir.path().join("02-valid.mib");
    std::fs::write(&malformed, MALFORMED).expect("write malformed candidate");
    std::fs::write(&valid, VALID).expect("write valid candidate");
    (malformed, valid)
}

fn duplicate_candidate_source() -> DuplicateCandidateSource {
    DuplicateCandidateSource {
        name: "REAL-MIB",
        candidates: vec![PHANTOM.to_vec(), VALID.to_vec()],
        label: "duplicate-path",
    }
}

#[test]
fn explicit_load_continues_after_duplicate_source_candidate() {
    let mib = Loader::new()
        .source(Box::new(duplicate_candidate_source()))
        .modules(["REAL-MIB"])
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("later candidate in one source should satisfy the request");

    assert!(mib.module("REAL-MIB").is_some());
}

#[test]
fn load_all_continues_after_duplicate_source_candidate() {
    let mib = Loader::new()
        .source(Box::new(duplicate_candidate_source()))
        .parallelism(2)
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("later candidate in one source should load");

    assert!(mib.module("REAL-MIB").is_some());
}

#[test]
fn built_in_sources_retain_duplicate_candidates_in_order() {
    let dir = TempDir::new();
    let first = dir.path().join("01-first.mib");
    let second = dir.path().join("02-second.mib");
    std::fs::write(&first, VALID).expect("write first candidate");
    std::fs::write(&second, VALID).expect("write second candidate");

    let files = source::files([first.clone(), second.clone()]).expect("build file source");
    let file_candidates = files
        .find_candidates("REAL-MIB")
        .collect::<io::Result<Vec<_>>>()
        .expect("find file candidates");
    assert_eq!(
        file_candidates
            .iter()
            .map(|candidate| candidate.path.as_path())
            .collect::<Vec<_>>(),
        vec![first.as_path(), second.as_path()]
    );

    let directory = source::dir(dir.path()).expect("build directory source");
    let directory_candidates = directory
        .find_candidates("REAL-MIB")
        .collect::<io::Result<Vec<_>>>()
        .expect("find directory candidates");
    assert_eq!(directory_candidates.len(), 2);
    let directory_paths = directory_candidates
        .iter()
        .map(|candidate| candidate.path.as_path())
        .collect::<Vec<_>>();
    assert!(directory_paths.contains(&first.as_path()));
    assert!(directory_paths.contains(&second.as_path()));
}

#[test]
fn explicit_files_load_skips_malformed_first_path() {
    let dir = TempDir::new();
    let (malformed, valid) = write_malformed_and_valid_files(&dir);
    let files = source::files([malformed, valid.clone()]).expect("build file source");

    assert_real_mib_loaded(Loader::new().source(files).modules(["REAL-MIB"]), &valid);
}

#[test]
fn load_all_files_skips_malformed_first_path() {
    let dir = TempDir::new();
    let (malformed, valid) = write_malformed_and_valid_files(&dir);
    let files = source::files([malformed, valid.clone()]).expect("build file source");

    assert_real_mib_loaded(Loader::new().source(files).parallelism(2), &valid);
}

#[test]
fn explicit_directory_load_skips_malformed_first_path() {
    let dir = TempDir::new();
    let (_, valid) = write_malformed_and_valid_files(&dir);
    let directory = source::dir(dir.path()).expect("build directory source");

    assert_real_mib_loaded(
        Loader::new().source(directory).modules(["REAL-MIB"]),
        &valid,
    );
}

#[test]
fn load_all_directory_skips_malformed_first_path() {
    let dir = TempDir::new();
    let (_, valid) = write_malformed_and_valid_files(&dir);
    let directory = source::dir(dir.path()).expect("build directory source");

    assert_real_mib_loaded(Loader::new().source(directory).parallelism(2), &valid);
}

#[test]
fn chained_valid_candidate_stops_before_later_io_error() {
    let valid = AdvertisedSource::new("valid", &["REAL-MIB"], &[("REAL-MIB", VALID)]);
    let chained = source::chain(vec![Box::new(valid), Box::new(ErrorSource)]);

    assert_real_mib_loaded(
        Loader::new().source(chained).modules(["REAL-MIB"]),
        Path::new("<valid:REAL-MIB>"),
    );
}

#[test]
fn load_all_chain_stops_before_later_io_error() {
    let valid = AdvertisedSource::new("valid", &["REAL-MIB"], &[("REAL-MIB", VALID)]);
    let chained = source::chain(vec![Box::new(valid), Box::new(ErrorSource)]);

    assert_real_mib_loaded(
        Loader::new().source(chained).parallelism(1),
        Path::new("<valid:REAL-MIB>"),
    );
}

fn directory_with_later_stale_candidate(dir: &TempDir) -> (Box<dyn Source>, PathBuf) {
    let first = dir.path().join("01-first.mib");
    let second = dir.path().join("02-second.mib");
    std::fs::write(&first, VALID).expect("write first candidate");
    std::fs::write(&second, VALID).expect("write second candidate");
    let directory = source::dir(dir.path()).expect("build directory source");

    let candidate_paths = directory
        .find_candidates("REAL-MIB")
        .collect::<io::Result<Vec<_>>>()
        .expect("inspect indexed candidate order")
        .into_iter()
        .map(|candidate| candidate.path)
        .collect::<Vec<_>>();
    assert_eq!(candidate_paths.len(), 2);
    std::fs::remove_file(&candidate_paths[1]).expect("make later index entry stale");
    (directory, candidate_paths[0].clone())
}

#[test]
fn directory_valid_candidate_stops_before_later_stale_path() {
    let dir = TempDir::new();
    let (directory, valid_path) = directory_with_later_stale_candidate(&dir);

    assert_real_mib_loaded(
        Loader::new().source(directory).modules(["REAL-MIB"]),
        &valid_path,
    );
}

#[test]
fn load_all_directory_stops_before_later_stale_path() {
    let dir = TempDir::new();
    let (directory, valid_path) = directory_with_later_stale_candidate(&dir);

    assert_real_mib_loaded(Loader::new().source(directory).parallelism(1), &valid_path);
}

#[test]
fn explicit_load_cache_distinguishes_module_names_with_same_path() {
    let mib = Loader::new()
        .source(Box::new(SharedPathSource))
        .modules(["A-MIB", "B-MIB"])
        .parallelism(1)
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("both same-path candidates should decode independently");

    assert!(mib.module("A-MIB").is_some());
    assert!(mib.module("B-MIB").is_some());
}

#[test]
fn load_all_cache_distinguishes_module_names_with_same_path() {
    let mib = Loader::new()
        .source(Box::new(SharedPathSource))
        .parallelism(1)
        .diagnostic_config(permissive_diagnostics())
        .load()
        .expect("both same-path candidates should decode independently");

    assert!(mib.module("A-MIB").is_some());
    assert!(mib.module("B-MIB").is_some());
}