asterust-junos 0.5.6

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
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
//! `firewall`
//! `firewall { ... }`.
//!
//! # See also
//!
//! - [`[edit firewall]` hierarchy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/topic-map/hierarchy-edit-firewall.html)

mod family;
mod hierarchical_policer;
mod policer;
mod prefix_action;
mod three_color_policer;

pub use family::{FilterAction, FilterActions, FilterKind, FilterTerm, FirewallFilter, MatchCondition};
pub use hierarchical_policer::{HierarchicalLimit, HierarchicalPolicer};
pub use policer::Policer;
pub use prefix_action::PrefixAction;
pub use three_color_policer::{ThreeColorPolicer, ThreeColorRate};

use crate::{named_children, top_level};
use asterust::config::Config;
use family::filters;
use hierarchical_policer::parse_hierarchical_policer;
use policer::parse_policer;
use prefix_action::prefix_actions;
use three_color_policer::parse_three_color_policer;

/// The top-level `firewall` configuration.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Firewall {
    pub filters: Vec<FirewallFilter>,
    pub policers: Vec<Policer>,
    pub three_color_policers: Vec<ThreeColorPolicer>,
    pub hierarchical_policers: Vec<HierarchicalPolicer>,
    pub prefix_actions: Vec<PrefixAction>,
}

/// Returns the top-level `firewall` block, or `None` if there is none.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::firewall::firewall;
///
/// let config = juniper::parse_braces("firewall {\n    policer LIMIT {\n        if-exceeding {\n            bandwidth-limit 1m;\n        }\n    }\n}\n");
/// let firewall = firewall(&config).unwrap();
/// assert_eq!(firewall.policers[0].name, "LIMIT");
/// assert_eq!(firewall.policers[0].bandwidth_limit.as_deref(), Some("1m"));
/// ```
pub fn firewall(config: &Config) -> Option<Firewall> {
    let root = top_level(config, "firewall")?;
    Some(Firewall {
        filters: filters(root),
        policers: named_children(root, "policer")
            .map(|(name, line)| parse_policer(name, line))
            .collect(),
        three_color_policers: named_children(root, "three-color-policer")
            .map(|(name, line)| parse_three_color_policer(name, line))
            .collect(),
        hierarchical_policers: named_children(root, "hierarchical-policer")
            .map(|(name, line)| parse_hierarchical_policer(name, line))
            .collect(),
        prefix_actions: prefix_actions(root),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use asterust::config::juniper;

    fn parse(text: &str) -> Firewall {
        firewall(&juniper::parse_braces(text)).expect("fixture has a firewall block")
    }

    // Adapted from Juniper's BGP User Guide "TCP Access Restriction for BGP" (Device E and the prefix-list example);
    // see the README "Sources".
    const DOC_BGP179_PEERS: &str = r#"
firewall {
    family inet {
        filter filter_bgp179 {
            term 1 {
                from {
                    source-address {
                        10.10.10.2/32;
                        10.10.10.6/32;
                    }
                    destination-port bgp;
                }
                then accept;
            }
            term 2 {
                then {
                    reject;
                }
            }
        }
    }
}
"#;

    const DOC_BGP179_PREFIX_LIST: &str = r#"
firewall {
    family inet {
        filter filter_bgp179 {
            term 1 {
                from {
                    source-address {
                        0.0.0.0/0;
                    }
                    source-prefix-list {
                        plist_bgp179 except;
                    }
                    destination-port bgp;
                }
                then {
                    reject;
                }
            }
            term 2 {
                then {
                    accept;
                }
            }
        }
    }
}
"#;

    #[test]
    fn doc_filter_with_address_block_and_leaf_then() {
        let firewall = parse(DOC_BGP179_PEERS);
        assert_eq!(firewall.filters.len(), 1);
        let filter = &firewall.filters[0];
        assert_eq!(filter.family, "inet");
        assert_eq!(filter.name, "filter_bgp179");
        assert_eq!(filter.terms.len(), 2);

        let term = &filter.terms[0];
        assert_eq!(term.name, "1");
        assert_eq!(
            term.condition("source-address").unwrap().values,
            ["10.10.10.2/32", "10.10.10.6/32"]
        );
        assert_eq!(term.condition("destination-port").unwrap().values, ["bgp"]);
        assert_eq!(term.then.action, Some(FilterAction::Accept));
    }

    #[test]
    fn doc_term_without_from_and_block_then() {
        let filter = &parse(DOC_BGP179_PEERS).filters[0];
        assert!(filter.terms[1].from.is_empty());
        assert_eq!(filter.terms[1].then.action, Some(FilterAction::Reject));
    }

    #[test]
    fn doc_prefix_list_with_except_modifier() {
        let filter = &parse(DOC_BGP179_PREFIX_LIST).filters[0];
        let term = &filter.terms[0];
        assert_eq!(term.condition("source-address").unwrap().values, ["0.0.0.0/0"]);
        assert_eq!(
            term.condition("source-prefix-list").unwrap().values,
            ["plist_bgp179 except"]
        );
        assert_eq!(term.then.action, Some(FilterAction::Reject));
        assert_eq!(filter.terms[1].then.action, Some(FilterAction::Accept));
    }

    // Not from documentation: covers statements the Juniper examples do not use. See the README "Sources".
    const CONFIG: &str = r#"
firewall {
    family inet {
        filter PROTECT-RE {
            term ICMP {
                from {
                    protocol icmp;
                    icmp-type [ echo-request echo-reply ];
                    first-fragment;
                }
                then {
                    policer ICMP-LIMIT;
                    count icmp-packets;
                    log;
                    accept;
                }
            }
            term SSH {
                from destination-port ssh;
                then {
                    forwarding-class network-control;
                    loss-priority low;
                    next term;
                }
            }
            inactive: term OLD {
                then discard;
            }
            term DEFAULT {
                then {
                    syslog;
                    discard;
                }
            }
        }
    }
    family inet6 {
        filter V6 {
            term ALL {
                then accept;
            }
        }
    }
    policer ICMP-LIMIT {
        if-exceeding {
            bandwidth-limit 1m;
            burst-size-limit 15k;
        }
        then discard;
    }
    policer PERCENT {
        if-exceeding {
            bandwidth-percent 10;
            burst-size-limit 10k;
        }
        then {
            forwarding-class best-effort;
            loss-priority high;
        }
    }
}
"#;

    #[test]
    fn lists_flags_and_nonterminating_actions() {
        let firewall = parse(CONFIG);
        let names: Vec<_> = firewall
            .filters
            .iter()
            .map(|f| (f.family.as_str(), f.name.as_str()))
            .collect();
        assert_eq!(names, [("inet", "PROTECT-RE"), ("inet6", "V6")]);

        let icmp = &firewall.filters[0].terms[0];
        assert_eq!(icmp.condition("protocol").unwrap().values, ["icmp"]);
        assert_eq!(
            icmp.condition("icmp-type").unwrap().values,
            ["echo-request", "echo-reply"]
        );
        assert!(icmp.condition("first-fragment").unwrap().values.is_empty());
        assert_eq!(icmp.then.policer.as_deref(), Some("ICMP-LIMIT"));
        assert_eq!(icmp.then.count.as_deref(), Some("icmp-packets"));
        assert!(icmp.then.log);
        assert!(!icmp.then.syslog);
    }

    #[test]
    fn leaf_from_next_term_and_skipped_inactive_term() {
        let terms = &parse(CONFIG).filters[0].terms;
        let names: Vec<_> = terms.iter().map(|t| t.name.as_str()).collect();
        assert_eq!(names, ["ICMP", "SSH", "DEFAULT"]);

        let ssh = &terms[1];
        assert_eq!(ssh.condition("destination-port").unwrap().values, ["ssh"]);
        assert_eq!(ssh.then.action, Some(FilterAction::NextTerm));
        assert_eq!(ssh.then.forwarding_class.as_deref(), Some("network-control"));
        assert_eq!(ssh.then.loss_priority.as_deref(), Some("low"));

        assert_eq!(terms[2].then.action, Some(FilterAction::Discard));
        assert!(terms[2].then.syslog);
    }

    #[test]
    fn extracts_policers() {
        let policers = parse(CONFIG).policers;
        assert_eq!(policers.len(), 2);

        assert_eq!(policers[0].name, "ICMP-LIMIT");
        assert_eq!(policers[0].bandwidth_limit.as_deref(), Some("1m"));
        assert_eq!(policers[0].burst_size_limit.as_deref(), Some("15k"));
        assert_eq!(policers[0].bandwidth_percent, None);
        assert!(policers[0].discard);

        assert_eq!(policers[1].bandwidth_percent.as_deref(), Some("10"));
        assert!(!policers[1].discard);
        assert_eq!(policers[1].forwarding_class.as_deref(), Some("best-effort"));
        assert_eq!(policers[1].loss_priority.as_deref(), Some("high"));
    }

    #[test]
    fn no_firewall_is_none() {
        assert!(firewall(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_none());
    }

    // Adapted from the Routing Policies, Firewall Filters, and Traffic Policers User Guide: the `show firewall` output
    // of the service filter example, the prefix-specific policing example (Configuring Prefix-Specific Counting and
    // Policing Actions) and the hierarchical policer example; see the README "Sources".
    const DOC_ACTIONS: &str = r#"
firewall {
    family inet {
        prefix-action psa-1Mbps-per-source-24-32-256 {
            policer 1Mbps-policer;
            subnet-prefix-length 24;
            source-prefix-length 32;
        }
        filter limit-source-one-24 {
            term one {
                from {
                    source-address {
                        10.10.10.0/24;
                    }
                }
                then prefix-action psa-1Mbps-per-source-24-32-256;
            }
        }
        service-filter in_filter_presvc {
            term t1 {
                from {
                    protocol tcp;
                    source-port bgp;
                }
                then {
                    count svc_in_pkts;
                    service;
                }
            }
        }
        service-filter in_filter_postsvc {
            term t2 {
                from {
                    protocol tcp;
                    source-port bgp;
                }
                then {
                    count svc_in_pkts_rtn;
                    skip;
                }
            }
        }
    }
    family inet {
        filter sam {
            term all {
                then {
                    count count_sam;
                    sample;
                }
            }
        }
        filter "$upstream-inet" {
            interface-specific;
            term P0-Aggregate {
                from {
                    dscp 46;
                }
                then {
                    hierarchical-policer "$P0-IN";
                    next term;
                }
            }
            term P1-Premium {
                from {
                    dscp [ 46 22 ];
                }
                then {
                    force-premium;
                    next term;
                }
            }
        }
    }
}
"#;

    fn find<'a>(firewall: &'a Firewall, name: &str) -> &'a FirewallFilter {
        firewall.filters.iter().find(|f| f.name == name).unwrap()
    }

    #[test]
    fn reads_the_guide_prefix_action() {
        let firewall = parse(DOC_ACTIONS);
        let action = &firewall.prefix_actions[0];
        assert_eq!(action.family, "inet");
        assert_eq!(action.name, "psa-1Mbps-per-source-24-32-256");
        assert_eq!(action.policer.as_deref(), Some("1Mbps-policer"));
        assert_eq!(action.subnet_prefix_length.as_deref(), Some("24"));
        assert_eq!(action.source_prefix_length.as_deref(), Some("32"));
        assert!(!action.count);
        assert_eq!(
            find(&firewall, "limit-source-one-24").terms[0]
                .then
                .prefix_action
                .as_deref(),
            Some("psa-1Mbps-per-source-24-32-256")
        );
    }

    #[test]
    fn reads_the_guide_service_filters() {
        let firewall = parse(DOC_ACTIONS);
        let pre = find(&firewall, "in_filter_presvc");
        assert_eq!(pre.kind, FilterKind::ServiceFilter);
        assert_eq!(pre.family, "inet");
        assert!(pre.terms[0].then.service);
        assert_eq!(pre.terms[0].then.count.as_deref(), Some("svc_in_pkts"));
        assert!(!pre.terms[0].then.skip);

        let post = find(&firewall, "in_filter_postsvc");
        assert!(post.terms[0].then.skip);
        assert!(!post.terms[0].then.service);
        assert_eq!(find(&firewall, "sam").kind, FilterKind::Filter);
    }

    #[test]
    fn reads_the_guide_sample_and_hierarchical_policer() {
        let firewall = parse(DOC_ACTIONS);
        assert!(find(&firewall, "sam").terms[0].then.sample);

        let upstream = find(&firewall, "$upstream-inet");
        assert!(upstream.interface_specific);
        assert_eq!(upstream.terms[0].then.hierarchical_policer.as_deref(), Some("$P0-IN"));
        assert_eq!(upstream.terms[0].then.action, Some(FilterAction::NextTerm));
        assert!(upstream.terms[1].then.force_premium);
        assert_eq!(upstream.terms[1].from[0].values, ["46", "22"]);
    }

    // Not from documentation: the actions and filter statements of the CLI Reference and the actions tables of the
    // guide. See the README "Sources".
    #[test]
    fn reads_the_other_actions_and_filter_statements() {
        let firewall = parse(
            r#"
firewall {
    filter DIRECT {
        accounting-profile PROFILE;
        enhanced-mode;
        fast-lookup-filter;
        filter-list-template;
        interface-shared;
        physical-interface-filter;
        promote gre-key;
        term t {
            then {
                reject tcp-reset;
            }
        }
        term u {
            then {
                bgp-output-queue-priority 5;
                dont-fragment set;
                dscp af11;
                enhanced-hierarchical-policer;
                ipsec-sa SA;
                load-balance GROUP;
                logical-system LS1;
                next-hop-group NHG;
                next-ip 192.0.2.1;
                next-ip6 2001:db8::1;
                packet-mode;
                policy-map MAP;
                port-mirror-instance MIRROR;
                routing-instance VRF-A;
                service-accounting;
                service-filter-hit;
                slice SLICE1;
                source-class SC;
                destination-class DC;
                topology TOPO;
                traffic-class 3;
                exclude-accounting;
                decapsulate gre routing-instance VRF-B;
                encapsulate TEMPLATE;
            }
        }
        term v {
            then {
                next-interface {
                    xe-0/1/0.1;
                    routing-instance rins1;
                }
                three-color-policer {
                    single-rate srTCM1;
                }
            }
        }
        term w {
            then next-interface ge-0/0/0.0;
        }
        term x {
            then port-mirror;
        }
    }
}
"#,
        );
        let filter = &firewall.filters[0];
        assert_eq!(filter.family, "inet");
        assert_eq!(filter.kind, FilterKind::Filter);
        assert_eq!(filter.accounting_profile.as_deref(), Some("PROFILE"));
        assert!(filter.enhanced_mode && filter.fast_lookup_filter && filter.filter_list_template);
        assert!(filter.interface_shared && filter.physical_interface_filter && filter.promote_gre_key);
        assert!(!filter.interface_specific);

        let reject = &filter.terms[0].then;
        assert_eq!(reject.action, Some(FilterAction::Reject));
        assert_eq!(reject.reject_message.as_deref(), Some("tcp-reset"));

        let then = &filter.terms[1].then;
        assert_eq!(then.bgp_output_queue_priority.as_deref(), Some("5"));
        assert_eq!(then.dont_fragment.as_deref(), Some("set"));
        assert_eq!(then.dscp.as_deref(), Some("af11"));
        assert!(then.enhanced_hierarchical_policer);
        assert_eq!(then.ipsec_sa.as_deref(), Some("SA"));
        assert_eq!(then.load_balance.as_deref(), Some("GROUP"));
        assert_eq!(then.logical_system.as_deref(), Some("LS1"));
        assert_eq!(then.next_hop_group.as_deref(), Some("NHG"));
        assert_eq!(then.next_ip.as_deref(), Some("192.0.2.1"));
        assert_eq!(then.next_ip6.as_deref(), Some("2001:db8::1"));
        assert!(then.packet_mode);
        assert_eq!(then.policy_map.as_deref(), Some("MAP"));
        assert!(then.port_mirror);
        assert_eq!(then.port_mirror_instance.as_deref(), Some("MIRROR"));
        assert_eq!(then.routing_instance.as_deref(), Some("VRF-A"));
        assert!(then.service_accounting && then.service_filter_hit && then.exclude_accounting);
        assert_eq!(then.slice.as_deref(), Some("SLICE1"));
        assert_eq!(then.source_class.as_deref(), Some("SC"));
        assert_eq!(then.destination_class.as_deref(), Some("DC"));
        assert_eq!(then.topology.as_deref(), Some("TOPO"));
        assert_eq!(then.traffic_class.as_deref(), Some("3"));
        assert_eq!(then.decapsulate.as_deref(), Some("gre routing-instance VRF-B"));
        assert_eq!(then.encapsulate.as_deref(), Some("TEMPLATE"));

        let block = &filter.terms[2].then;
        assert_eq!(block.next_interface.as_deref(), Some("xe-0/1/0.1"));
        assert_eq!(block.next_interface_routing_instance.as_deref(), Some("rins1"));
        assert_eq!(block.three_color_policer.as_deref(), Some("single-rate srTCM1"));

        assert_eq!(filter.terms[3].then.next_interface.as_deref(), Some("ge-0/0/0.0"));
        assert!(filter.terms[4].then.port_mirror);
        assert_eq!(filter.terms[4].then.port_mirror_instance, None);
    }

    // Not from documentation: the `simple-filter` of the CLI Reference page.
    #[test]
    fn reads_simple_filters() {
        let firewall = parse(
            "firewall {\n    family inet {\n        simple-filter SF {\n            term t {\n                from {\n                    source-address 10.0.0.0/8;\n                }\n                then {\n                    forwarding-class expedited-forwarding;\n                    loss-priority low;\n                }\n            }\n        }\n    }\n}\n",
        );
        let filter = &firewall.filters[0];
        assert_eq!(filter.kind, FilterKind::SimpleFilter);
        assert_eq!(
            filter.terms[0].then.forwarding_class.as_deref(),
            Some("expedited-forwarding")
        );
        assert_eq!(filter.terms[0].then.loss_priority.as_deref(), Some("low"));
    }

    // Adapted from the Routing Policies, Firewall Filters, and Traffic Policers User Guide: the `show firewall` output
    // of the single-rate, two-rate and logical interface three-color policer examples, the packets-per-second policer
    // example, the policers of Applying Policers to Interfaces and the hierarchical policer example; see the README
    // "Sources".
    const DOC_POLICERS: &str = r#"
firewall {
    three-color-policer srTCM1-ca {
        action {
            loss-priority high then discard;
        }
        single-rate {
            color-aware;
            committed-information-rate 40m;
            committed-burst-size 100k;
            excess-burst-size 200k;
        }
    }
    three-color-policer trTCM2-cb {
        logical-interface-policer;
        action {
            loss-priority high then discard;
        }
        two-rate {
            color-blind;
            committed-information-rate 40m;
            committed-burst-size 100k;
            peak-information-rate 60m;
            peak-burst-size 200k;
        }
    }
    policer police_pps {
        if-exceeding-pps {
            pps-limit 1k;
            packet-burst 150;
        }
        then discard;
    }
    policer aggregate_police1 {
        logical-interface-policer;
        if-exceeding {
            bandwidth-limit 100m;
            burst-size-limit 500k;
        }
        then {
            discard;
        }
    }
    hierarchical-policer policer1 {
        aggregate {
            if-exceeding {
                bandwidth-limit 300m;
                burst-size-limit 30k;
            }
            then {
                forwarding-class fc1;
            }
        }
        premium {
            if-exceeding {
                bandwidth-limit 100m;
                burst-size-limit 50k;
            }
            then {
                discard;
            }
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_three_color_policers() {
        let firewall = parse(DOC_POLICERS);
        assert_eq!(firewall.three_color_policers.len(), 2);

        let single = &firewall.three_color_policers[0];
        assert_eq!(single.name, "srTCM1-ca");
        assert_eq!(single.actions, ["loss-priority high then discard"]);
        assert!(!single.logical_interface_policer);
        assert!(single.two_rate.is_none());
        let rate = single.single_rate.as_ref().unwrap();
        assert_eq!(rate.color_aware, Some(true));
        assert_eq!(rate.committed_information_rate.as_deref(), Some("40m"));
        assert_eq!(rate.committed_burst_size.as_deref(), Some("100k"));
        assert_eq!(rate.excess_burst_size.as_deref(), Some("200k"));
        assert_eq!(rate.peak_information_rate, None);

        let two = &firewall.three_color_policers[1];
        assert!(two.logical_interface_policer);
        assert!(two.single_rate.is_none());
        let rate = two.two_rate.as_ref().unwrap();
        assert_eq!(rate.color_aware, Some(false));
        assert_eq!(rate.peak_information_rate.as_deref(), Some("60m"));
        assert_eq!(rate.peak_burst_size.as_deref(), Some("200k"));
        assert_eq!(rate.excess_burst_size, None);
    }

    #[test]
    fn reads_the_guide_packet_rate_and_interface_policers() {
        let firewall = parse(DOC_POLICERS);
        let pps = &firewall.policers[0];
        assert_eq!(pps.name, "police_pps");
        assert_eq!(pps.pps_limit.as_deref(), Some("1k"));
        assert_eq!(pps.packet_burst.as_deref(), Some("150"));
        assert_eq!(pps.bandwidth_limit, None);
        assert!(pps.discard);
        assert!(!pps.logical_interface_policer);

        let aggregate = &firewall.policers[1];
        assert!(aggregate.logical_interface_policer);
        assert_eq!(aggregate.bandwidth_limit.as_deref(), Some("100m"));
        assert_eq!(aggregate.pps_limit, None);
    }

    #[test]
    fn reads_the_guide_hierarchical_policer() {
        let firewall = parse(DOC_POLICERS);
        let policer = &firewall.hierarchical_policers[0];
        assert_eq!(policer.name, "policer1");
        let aggregate = policer.aggregate.as_ref().unwrap();
        assert_eq!(aggregate.bandwidth_limit.as_deref(), Some("300m"));
        assert_eq!(aggregate.burst_size_limit.as_deref(), Some("30k"));
        assert_eq!(aggregate.forwarding_class.as_deref(), Some("fc1"));
        assert!(!aggregate.discard);
        let premium = policer.premium.as_ref().unwrap();
        assert_eq!(premium.bandwidth_limit.as_deref(), Some("100m"));
        assert!(premium.discard);
    }

    // Not from documentation: the remaining policer statements of the CLI Reference pages. See the README "Sources".
    #[test]
    fn reads_the_other_policer_statements() {
        let firewall = parse(
            r#"
firewall {
    policer P {
        filter-specific;
        physical-interface-policer;
        logical-bandwidth-policer;
        shared-bandwidth-policer;
        if-exceeding {
            bandwidth-percent 20;
        }
    }
    three-color-policer T {
        filter-specific;
        physical-interface-policer;
        shared-bandwidth-policer;
    }
    hierarchical-policer H {
        aggregate {
            if-exceeding-pps {
                pps-limit 5k;
                packet-burst 100;
            }
            then {
                loss-priority low;
            }
        }
    }
}
"#,
        );
        let policer = &firewall.policers[0];
        assert!(policer.filter_specific && policer.physical_interface_policer);
        assert!(policer.logical_bandwidth_policer && policer.shared_bandwidth_policer);
        assert!(!policer.logical_interface_policer);

        let three = &firewall.three_color_policers[0];
        assert!(three.filter_specific && three.physical_interface_policer && three.shared_bandwidth_policer);
        assert!(three.actions.is_empty());
        assert!(three.single_rate.is_none());

        let aggregate = firewall.hierarchical_policers[0].aggregate.as_ref().unwrap();
        assert_eq!(aggregate.pps_limit.as_deref(), Some("5k"));
        assert_eq!(aggregate.packet_burst.as_deref(), Some("100"));
        assert_eq!(aggregate.loss_priority.as_deref(), Some("low"));
        assert!(firewall.hierarchical_policers[0].premium.is_none());
    }
}