ostrya 0.18.0

Pure-Rust async reimplementation of the ostree repository library
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
//! The access modes, the allow items, and the effective rights of a ref.

use std::collections::BTreeMap;
use std::fmt;

use ostrya_core::{MAX_COLLECTION_ID_LEN, RefPattern, RefSelector, is_valid_collection_id};

use super::Namespace;
use crate::error::{Error, Result};

/// A set of access modes.
///
/// A mode is one letter of an allow item:
///
/// - `r` ([`READ`](AccessModes::READ)) -- read the ref.
/// - `w` ([`WRITE`](AccessModes::WRITE)) -- write a client-built commit to
///   the ref.
/// - `c` ([`COMMIT`](AccessModes::COMMIT)) -- the server makes a commit on
///   the branch.
/// - `t` ([`TAG`](AccessModes::TAG)) -- create the ref as a tag.
/// - `d` ([`DELETE`](AccessModes::DELETE)) -- delete the ref.
///
/// [`Display`](fmt::Display) writes the letters in the order `rwctd`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct AccessModes {
    inner: u8,
}

/// The letters of the modes, in the order of their bits.
const LETTERS: [(char, AccessModes); 5] = [
    ('r', AccessModes::READ),
    ('w', AccessModes::WRITE),
    ('c', AccessModes::COMMIT),
    ('t', AccessModes::TAG),
    ('d', AccessModes::DELETE),
];

impl AccessModes {
    /// The set with no mode.
    pub const NONE: AccessModes = AccessModes { inner: 0 };
    /// `r`: read the ref.
    pub const READ: AccessModes = AccessModes { inner: 1 };
    /// `w`: write a client-built commit to the ref.
    pub const WRITE: AccessModes = AccessModes { inner: 1 << 1 };
    /// `c`: the server makes a commit on the branch.
    pub const COMMIT: AccessModes = AccessModes { inner: 1 << 2 };
    /// `t`: create the ref as a tag.
    pub const TAG: AccessModes = AccessModes { inner: 1 << 3 };
    /// `d`: delete the ref.
    pub const DELETE: AccessModes = AccessModes { inner: 1 << 4 };

    /// Parses the modes of an allow item: one or more of `r`, `w`, `c`, `t`,
    /// and `d`, each at most one time, in any order.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidFormat`] if `text` is empty, holds another character,
    /// or holds a letter two times.
    pub fn parse(text: &str) -> Result<AccessModes> {
        parse_modes(text)
            .map_err(|why| Error::InvalidFormat(format!("malformed access modes '{text}': {why}")))
    }

    /// Returns `true` if the set holds no mode.
    pub fn is_empty(self) -> bool {
        self.inner == 0
    }

    /// Returns `true` if the set holds each mode of `other`.
    pub fn contains(self, other: AccessModes) -> bool {
        self.inner & other.inner == other.inner
    }

    /// Returns `true` if the set holds a mode of `other`.
    pub fn intersects(self, other: AccessModes) -> bool {
        self.inner & other.inner != 0
    }

    /// Returns the set with the modes of `self` and of `other`.
    pub fn union(self, other: AccessModes) -> AccessModes {
        AccessModes {
            inner: self.inner | other.inner,
        }
    }
}

impl fmt::Display for AccessModes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (letter, mode) in LETTERS {
            if self.contains(mode) {
                write!(f, "{letter}")?;
            }
        }
        Ok(())
    }
}

/// Parses the modes of an allow item. The error is the reason.
fn parse_modes(text: &str) -> std::result::Result<AccessModes, &'static str> {
    if text.is_empty() {
        return Err("the modes are one or more of r, w, c, t, and d");
    }
    let mut modes = AccessModes::NONE;
    for c in text.chars() {
        let Some(&(_, mode)) = LETTERS.iter().find(|(letter, _)| *letter == c) else {
            return Err("the modes are one or more of r, w, c, t, and d");
        };
        if modes.contains(mode) {
            return Err("each mode occurs at most one time");
        }
        modes = modes.union(mode);
    }
    Ok(modes)
}

/// One allow item: the modes that a group has on the refs that a pattern
/// matches.
///
/// The text form is `[MODES:]PATTERN`. `MODES` is the text that
/// [`AccessModes::parse`] reads, and an item with no `MODES` has the mode
/// `r`. `PATTERN` is a [`RefPattern`]: literal parts, `*` for exactly one
/// part, and `**` for one or more parts. [`Display`](fmt::Display) writes the
/// form with `MODES`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AllowItem {
    /// The modes of the item.
    pub modes: AccessModes,
    /// The refs of the item.
    pub pattern: RefPattern,
}

impl AllowItem {
    /// Parses an allow item, `[MODES:]PATTERN`.
    ///
    /// The text splits at the first `:`.
    ///
    /// # Errors
    ///
    /// [`Error::InvalidFormat`] if the modes or the pattern are malformed.
    pub fn parse(text: &str) -> Result<AllowItem> {
        parse_allow(text)
            .map_err(|why| Error::InvalidFormat(format!("malformed allow item '{text}': {why}")))
    }
}

impl AllowItem {
    /// Returns `true` if the item gives `r`: it has `r`, `w`, `c`, or `t`.
    /// An item with `d` alone gives no `r`.
    pub fn gives_read(&self) -> bool {
        self.modes
            .intersects(push_modes_with_read().union(AccessModes::READ))
    }
}

impl fmt::Display for AllowItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.modes, self.pattern.as_str())
    }
}

/// Parses an allow item. The error is the reason.
fn parse_allow(text: &str) -> std::result::Result<AllowItem, String> {
    let (modes, pattern) = match text.split_once(':') {
        Some((modes, pattern)) => (parse_modes(modes).map_err(str::to_owned)?, pattern),
        None => (AccessModes::READ, text),
    };
    Ok(AllowItem {
        modes,
        pattern: parse_pattern(pattern)?,
    })
}

/// Parses an allow item of a client group or of a claim, `[MODES:]PATTERN`
/// or `[MODES:]@ID:PATTERN`. The result holds the collection id of an item
/// with `@ID:`. The error is the reason.
///
/// The text splits at the first `:` when the text before it does not start
/// with `@`. Then `@ID:` splits at the first `:` after the `@`.
pub(crate) fn parse_allow_item(
    text: &str,
) -> std::result::Result<(Option<String>, AllowItem), String> {
    let (modes, rest) = match text.split_once(':') {
        Some((modes, rest)) if !modes.starts_with('@') => {
            (parse_modes(modes).map_err(str::to_owned)?, rest)
        }
        _ => (AccessModes::READ, text),
    };
    let (id, pattern) = split_collection(rest)?;
    let pattern = parse_pattern(pattern)?;
    Ok((id, AllowItem { modes, pattern }))
}

/// Parses a deny item of a client group or of a claim, `PATTERN` or
/// `@ID:PATTERN`. The result holds the collection id of an item with `@ID:`.
/// The error is the reason.
pub(crate) fn parse_deny_item(
    text: &str,
) -> std::result::Result<(Option<String>, RefPattern), String> {
    let (id, pattern) = split_collection(text)?;
    Ok((id, parse_pattern(pattern)?))
}

/// Splits `@ID:REST` at the first `:` after the `@`, and checks `ID`. Text
/// that does not start with `@` gives no id. The error is the reason.
fn split_collection(text: &str) -> std::result::Result<(Option<String>, &str), String> {
    if !text.starts_with('@') {
        return Ok((None, text));
    }
    let Some((id, rest)) = ostrya_core::split_collection(text) else {
        return Err("a collection id is @ID: before the pattern".to_owned());
    };
    if id.len() > MAX_COLLECTION_ID_LEN {
        return Err(format!(
            "the collection id is longer than {MAX_COLLECTION_ID_LEN} bytes"
        ));
    }
    if !is_valid_collection_id(id) {
        return Err("the collection id fails the collection-id rule".to_owned());
    }
    Ok((Some(id.to_owned()), rest))
}

/// Parses a ref pattern of an allow item or a deny item. The error is the
/// reason, without the pattern text.
fn parse_pattern(text: &str) -> std::result::Result<RefPattern, String> {
    RefPattern::parse(text).map_err(|err| match err {
        ostrya_core::Error::InvalidRefPattern(reason) => {
            let quoted = format!("{text:?}: ");
            reason.strip_prefix(&quoted).unwrap_or(&reason).to_owned()
        }
        other => other.to_string(),
    })
}

/// The allow items and the deny items of a group.
///
/// [`rights`](AccessRules::rights) gives the effective rights of one ref.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AccessRules {
    /// The allow items, in the order of the config.
    pub allow: Vec<AllowItem>,
    /// The deny items, in the order of the config.
    pub deny: Vec<RefPattern>,
}

impl AccessRules {
    /// Returns the effective rights of the ref `name`.
    ///
    /// When a deny item matches `name`, the result is empty. Else the result
    /// is the union of the modes of each allow item that matches `name`. When
    /// the union holds `w`, `c`, or `t`, the result also holds `r`. `d` alone
    /// does not add `r`.
    pub fn rights(&self, name: &str) -> AccessModes {
        if self.deny.iter().any(|pattern| pattern.matches(name)) {
            return AccessModes::NONE;
        }
        let modes = self
            .allow
            .iter()
            .filter(|item| item.pattern.matches(name))
            .fold(AccessModes::NONE, |modes, item| modes.union(item.modes));
        if modes.intersects(push_modes_with_read()) {
            modes.union(AccessModes::READ)
        } else {
            modes
        }
    }

    /// Returns `true` if the rules let a client open a push session.
    ///
    /// The result is `true` when an allow item has `w`, `c`, `t`, or `d`. The
    /// deny items have no effect on the result.
    pub fn opens_push_session(&self) -> bool {
        let push = push_modes_with_read().union(AccessModes::DELETE);
        self.allow.iter().any(|item| item.modes.intersects(push))
    }

    /// Returns `true` if an allow item gives `r`: it has `r`, `w`, `c`, or
    /// `t`. The deny items have no effect on the result.
    pub fn gives_read(&self) -> bool {
        self.allow.iter().any(AllowItem::gives_read)
    }

    /// Returns the selector of the refs that the rules give `r` on, or
    /// `None` if no allow item gives `r`.
    ///
    /// The selector has a positive entry for the pattern of each allow item
    /// that gives `r`, in the order of [`allow`](AccessRules::allow). It has
    /// a negative entry for each deny item, in the order of
    /// [`deny`](AccessRules::deny). A selector with no positive entry
    /// selects each name, so the result is `None` when no allow item gives
    /// `r`. With `Some`, the selector selects a name exactly when
    /// [`rights`](AccessRules::rights) of the name holds `r`.
    ///
    /// # Examples
    ///
    /// ```
    /// use ostrya::RefPattern;
    /// use ostrya::access::{AccessRules, AllowItem};
    ///
    /// let rules = AccessRules {
    ///     allow: vec![AllowItem::parse("w:os/**")?, AllowItem::parse("d:tmp/**")?],
    ///     deny: vec![RefPattern::parse("os/secret")?],
    /// };
    /// let selector = rules.read_selector().unwrap();
    /// assert_eq!(selector.positive().len(), 1);
    /// assert!(selector.matches("os/stable"));
    /// assert!(!selector.matches("os/secret"));
    /// assert!(!selector.matches("tmp/a"));
    ///
    /// let delete_only = AccessRules {
    ///     allow: vec![AllowItem::parse("d:**")?],
    ///     deny: Vec::new(),
    /// };
    /// assert!(delete_only.read_selector().is_none());
    /// # Ok::<(), ostrya::Error>(())
    /// ```
    pub fn read_selector(&self) -> Option<RefSelector> {
        let positive: Vec<RefPattern> = self
            .allow
            .iter()
            .filter(|item| item.gives_read())
            .map(|item| item.pattern.clone())
            .collect();
        if positive.is_empty() {
            return None;
        }
        Some(RefSelector::new(positive, self.deny.clone()))
    }
}

/// The allow items and the deny items of a group, for each namespace.
///
/// [`get`](NamespaceRules::get) gives the rules of one namespace. The rules
/// of two namespaces never combine. A collection with no entry in
/// [`collections`](NamespaceRules::collections) has no rules, so a client has
/// no right on its refs. The one exception is the implicit group of a config
/// with no client group, whose rules apply to each collection.
///
/// `From<AccessRules>` gives the rules of the local refs alone.
///
/// # Examples
///
/// ```
/// use ostrya::access::{AccessModes, AccessRules, AllowItem, Namespace, NamespaceRules};
///
/// let mut rules = NamespaceRules::from(AccessRules {
///     allow: vec![AllowItem::parse("r:**")?],
///     deny: Vec::new(),
/// });
/// rules.collections.insert(
///     "com.example.os".into(),
///     AccessRules {
///         allow: vec![AllowItem::parse("w:os/**")?],
///         deny: Vec::new(),
///     },
/// );
/// let os = Namespace::Collection("com.example.os".into());
/// assert_eq!(rules.rights(&os, "os/stable").to_string(), "rw");
/// assert_eq!(rules.rights(&os, "main"), AccessModes::NONE);
/// assert_eq!(rules.rights(&Namespace::Local, "main"), AccessModes::READ);
/// let other = Namespace::Collection("org.example.other".into());
/// assert!(rules.get(&other).is_none());
/// assert!(rules.opens_push_session());
/// # Ok::<(), ostrya::Error>(())
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NamespaceRules {
    /// The rules of the local refs: the items with no `@ID:`, and the items
    /// with `@ID:` of `[core] collection-id`.
    pub local: AccessRules,
    /// The rules of each collection, by collection id: the items with
    /// `@ID:`.
    pub collections: BTreeMap<String, AccessRules>,
    /// The rules of each collection with no entry in `collections`. Only the
    /// implicit group has them.
    pub(crate) other: Option<AccessRules>,
}

impl NamespaceRules {
    /// Returns the rules of `namespace`, or `None` if the rules name no ref
    /// of it.
    ///
    /// [`Local`](Namespace::Local) gives
    /// [`local`](NamespaceRules::local). A
    /// [`Collection`](Namespace::Collection) gives its entry of
    /// [`collections`](NamespaceRules::collections). An id that fails the
    /// collection-id rule or is longer than [`MAX_COLLECTION_ID_LEN`] bytes
    /// gives `None`.
    pub fn get(&self, namespace: &Namespace) -> Option<&AccessRules> {
        match namespace {
            Namespace::Local => Some(&self.local),
            Namespace::Collection(id) => self.collection(id),
        }
    }

    /// Returns the rules of the collection `id`, as [`get`](Self::get) gives
    /// them.
    pub(crate) fn collection(&self, id: &str) -> Option<&AccessRules> {
        self.collections
            .get(id)
            .or_else(|| self.other.as_ref().filter(|_| is_valid_collection_id(id)))
    }

    /// Returns the effective rights of the ref `name` of `namespace`, as
    /// [`AccessRules::rights`] gives them. A namespace with no rules gives no
    /// right.
    pub fn rights(&self, namespace: &Namespace, name: &str) -> AccessModes {
        self.get(namespace)
            .map_or(AccessModes::NONE, |rules| rules.rights(name))
    }

    /// Returns `true` if the rules of a namespace let a client open a push
    /// session, as [`AccessRules::opens_push_session`] states.
    pub fn opens_push_session(&self) -> bool {
        self.namespaces().any(AccessRules::opens_push_session)
    }

    /// Returns the rules of each namespace.
    fn namespaces(&self) -> impl Iterator<Item = &AccessRules> {
        std::iter::once(&self.local)
            .chain(self.collections.values())
            .chain(self.other.as_ref())
    }
}

impl From<AccessRules> for NamespaceRules {
    /// Returns the rules of the local refs `local`, with no rules of a
    /// collection.
    fn from(local: AccessRules) -> NamespaceRules {
        NamespaceRules {
            local,
            ..NamespaceRules::default()
        }
    }
}

/// The modes that add `r`: `w`, `c`, and `t`.
fn push_modes_with_read() -> AccessModes {
    AccessModes::WRITE
        .union(AccessModes::COMMIT)
        .union(AccessModes::TAG)
}

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

    fn modes(text: &str) -> AccessModes {
        AccessModes::parse(text).unwrap()
    }

    fn rules(allow: &[&str], deny: &[&str]) -> AccessRules {
        AccessRules {
            allow: allow.iter().map(|t| AllowItem::parse(t).unwrap()).collect(),
            deny: deny.iter().map(|t| RefPattern::parse(t).unwrap()).collect(),
        }
    }

    /// An id over the limit and an id that is no collection id each give
    /// their own reason.
    #[test]
    fn the_collection_id_of_an_item_is_checked() {
        let longest = format!("a.{}", "b".repeat(MAX_COLLECTION_ID_LEN - 2));
        assert_eq!(
            split_collection(&format!("@{longest}:x")),
            Ok((Some(longest.clone()), "x"))
        );
        assert_eq!(
            split_collection(&format!("@{longest}b:x")),
            Err(format!(
                "the collection id is longer than {MAX_COLLECTION_ID_LEN} bytes"
            ))
        );
        assert_eq!(
            split_collection("@nodot:x"),
            Err("the collection id fails the collection-id rule".to_owned())
        );
        assert_eq!(split_collection("x"), Ok((None, "x")));
    }

    /// The rules of each other collection apply only to an id that passes
    /// the collection-id rule and is at most `MAX_COLLECTION_ID_LEN` bytes.
    #[test]
    fn the_other_rules_refuse_a_collection_id_over_the_limit() {
        let ns = NamespaceRules {
            other: Some(rules(&["r:**"], &[])),
            ..NamespaceRules::default()
        };
        let longest = format!("a.{}", "b".repeat(MAX_COLLECTION_ID_LEN - 2));
        let over = format!("{longest}b");
        assert_eq!(over.len(), MAX_COLLECTION_ID_LEN + 1);
        assert!(ns.collection(&longest).is_some());
        assert!(ns.collection(&over).is_none());
        assert!(ns.get(&Namespace::Collection(over.clone())).is_none());
        assert_eq!(
            ns.rights(&Namespace::Collection(over), "x"),
            AccessModes::NONE
        );
        assert!(ns.collection("nodot").is_none());
    }

    /// The modes parse in any order, and `Display` writes them in the order
    /// `rwctd`.
    #[test]
    fn the_modes_parse_in_any_order() {
        assert_eq!(modes("r"), AccessModes::READ);
        assert_eq!(modes("dtcwr").to_string(), "rwctd");
        assert_eq!(modes("cd").to_string(), "cd");
        assert!(modes("wd").contains(AccessModes::DELETE));
        assert!(!modes("wd").contains(AccessModes::READ));
        assert!(AccessModes::NONE.is_empty());
        assert_eq!(AccessModes::NONE.to_string(), "");
    }

    /// An empty text, an unknown letter, an uppercase letter, and a letter
    /// that occurs two times are refused.
    #[test]
    fn the_other_modes_are_refused() {
        for text in ["", "x", "R", "rr", "rwr", "r w", "rw ", "r,w"] {
            let err = AccessModes::parse(text).unwrap_err();
            assert!(matches!(err, Error::InvalidFormat(_)), "{text:?}: {err}");
        }
    }

    /// An allow item splits at the first `:`, and an item with no modes has
    /// the mode `r`.
    #[test]
    fn an_allow_item_splits_at_the_first_colon() {
        let item = AllowItem::parse("os/**").unwrap();
        assert_eq!(item.modes, AccessModes::READ);
        assert_eq!(item.pattern.as_str(), "os/**");
        assert_eq!(item.to_string(), "r:os/**");
        let item = AllowItem::parse("tw:os/*/stable").unwrap();
        assert_eq!(item.modes, modes("wt"));
        assert_eq!(item.to_string(), "wt:os/*/stable");
        assert_eq!(AllowItem::parse(&item.to_string()).unwrap(), item);
    }

    /// A malformed allow item is refused with its text in the message.
    #[test]
    fn a_malformed_allow_item_is_refused() {
        for text in [
            "", ":a", "x:a", "R:a", "rr:a", "r:", "r:a:b", "r:a//b", "r:a*", "r:-a", "r:!a",
            "r: a", " r:a",
        ] {
            let Err(Error::InvalidFormat(message)) = AllowItem::parse(text) else {
                panic!("{text:?} was accepted");
            };
            assert!(
                message.starts_with(&format!("malformed allow item '{text}': ")),
                "{message}"
            );
        }
    }

    /// The rights are the union of the modes of each item that matches.
    #[test]
    fn the_rights_are_the_union_of_the_matching_items() {
        let r = rules(&["r:**", "d:devel/**", "rd:devel/x"], &[]);
        assert_eq!(r.rights("main"), modes("r"));
        assert_eq!(r.rights("devel/y"), modes("rd"));
        assert_eq!(r.rights("devel/x"), modes("rd"));
        let r = rules(&["d:devel/**"], &[]);
        assert_eq!(r.rights("main"), AccessModes::NONE);
    }

    /// `w`, `c`, and `t` add `r`, and `d` does not.
    #[test]
    fn w_c_and_t_add_read_and_d_does_not() {
        for (mode, want) in [
            ("w", "rw"),
            ("c", "rc"),
            ("t", "rt"),
            ("d", "d"),
            ("wd", "rwd"),
        ] {
            let r = rules(&[&format!("{mode}:a")], &[]);
            assert_eq!(r.rights("a"), modes(want), "{mode}");
        }
    }

    /// A deny item that matches the ref removes each mode.
    #[test]
    fn a_deny_item_wins_over_each_allow_item() {
        let r = rules(&["rwctd:**"], &["secret/**"]);
        assert_eq!(r.rights("secret/a"), AccessModes::NONE);
        assert_eq!(r.rights("public"), modes("rwctd"));
        let r = rules(&[], &["**"]);
        assert_eq!(r.rights("a"), AccessModes::NONE);
    }

    /// The read selector is `None` when no allow item gives `r`. Else it
    /// selects a name exactly when the rights of the name hold `r`.
    #[test]
    fn the_read_selector_selects_the_refs_with_read() {
        assert!(rules(&[], &[]).read_selector().is_none());
        assert!(rules(&[], &["secret/**"]).read_selector().is_none());
        assert!(rules(&["d:**"], &[]).read_selector().is_none());
        let names = [
            "main",
            "os/stable",
            "os/secret",
            "os/secret/a",
            "devel/x",
            "devel/x/y",
            "tmp/a",
            "a",
            "b/c",
        ];
        for (allow, deny) in [
            (&["r:**"][..], &[][..]),
            (&["w:os/**", "d:tmp/**", "t:devel/*"], &["os/secret/**"]),
            (&["d:**", "c:main", "r:a"], &["a", "tmp/**"]),
            (&["rd:b/*", "r:os/*", "w:os/**"], &["os/secret"]),
        ] {
            let r = rules(allow, deny);
            let selector = r.read_selector().unwrap();
            for name in names {
                assert_eq!(
                    selector.matches(name),
                    r.rights(name).contains(AccessModes::READ),
                    "{allow:?} {deny:?} {name:?}"
                );
            }
        }
        let r = rules(&["w:b", "d:x", "a"], &["y", "z"]);
        let selector = r.read_selector().unwrap();
        let positive: Vec<&str> = selector.positive().iter().map(RefPattern::as_str).collect();
        let negative: Vec<&str> = selector.negative().iter().map(RefPattern::as_str).collect();
        assert_eq!(positive, ["b", "a"]);
        assert_eq!(negative, ["y", "z"]);
    }

    /// A push session needs an allow item with `w`, `c`, `t`, or `d`, and a
    /// deny item does not take it away.
    #[test]
    fn a_push_session_needs_a_push_mode_in_an_allow_item() {
        assert!(!rules(&[], &[]).opens_push_session());
        assert!(!rules(&["r:**"], &[]).opens_push_session());
        for mode in ["w", "c", "t", "d"] {
            assert!(
                rules(&["r:**", &format!("{mode}:a")], &["**"]).opens_push_session(),
                "{mode}"
            );
        }
    }
}