link-cli 3.5.0

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
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
//! What a permissions view answers at its edges, each right asked of a policy
//! that states it plainly, over a store whose writes can change other links
//! or fail. `permissions_decorator_tests.rs` has the rules themselves.

use std::collections::HashSet;
use std::panic::{catch_unwind, AssertUnwindSafe};

use anyhow::{bail, Result};
use link_cli::{
    AccessPolicy, Forbidden, Link, LinkStorage, NamedTypeLinks, PermissionAccess,
    PermissionMarkers, PermissionsDecorator,
};

#[path = "common/permissions_world.rs"]
mod world;

const MARKERS: PermissionMarkers = PermissionMarkers {
    allow_read: 1,
    allow_write: 2,
    anything: 3,
    allow_reference: 4,
};

/// Every right as plain data.
#[derive(Default)]
struct Plain {
    hidden: HashSet<u32>,
    locked: HashSet<u32>,
    referable: HashSet<u32>,
    no_create: bool,
}

impl AccessPolicy for Plain {
    fn markers(&self) -> PermissionMarkers {
        MARKERS
    }

    fn can_read(&mut self, _: &mut dyn NamedTypeLinks, _: u32, link: &Link) -> bool {
        !self.hidden.contains(&link.index)
    }

    fn can_write(&mut self, _: &mut dyn NamedTypeLinks, _: u32, link: &Link) -> bool {
        !self.locked.contains(&link.index)
    }

    fn can_create(&mut self, _: &mut dyn NamedTypeLinks, _: u32) -> bool {
        !self.no_create
    }

    fn can_reference(&mut self, _: &mut dyn NamedTypeLinks, _: u32, link: &Link) -> bool {
        self.referable.contains(&link.index)
    }
}

/// A step a write of [`Scripted`] takes besides the one it was asked for.
#[derive(Clone, Copy)]
enum Also {
    Create(u32, u32),
    Update(u32, u32, u32),
    Delete(u32),
    Nothing,
    Fail,
}

/// A store whose update also runs a script, and whose deletes can fail.
struct Scripted {
    links: LinkStorage,
    also: Vec<Also>,
    deletes_fail: bool,
}

impl Scripted {
    fn with_rows(count: u32) -> Self {
        let mut links = LinkStorage::in_memory(false);
        for _ in 0..count {
            links.create(0, 0);
        }
        Self {
            links,
            also: Vec::new(),
            deletes_fail: false,
        }
    }
}

impl NamedTypeLinks for Scripted {
    fn create(&mut self, source: u32, target: u32) -> u32 {
        self.links.create(source, target)
    }
    fn ensure_created(&mut self, id: u32) -> u32 {
        NamedTypeLinks::ensure_created(&mut self.links, id)
    }
    fn get_link(&mut self, id: u32) -> Option<Link> {
        NamedTypeLinks::get_link(&mut self.links, id)
    }
    fn exists(&mut self, id: u32) -> bool {
        NamedTypeLinks::exists(&mut self.links, id)
    }
    fn update_observed(
        &mut self,
        id: u32,
        source: u32,
        target: u32,
        observer: &mut dyn FnMut(Link, Link),
    ) -> Result<Link> {
        let before = self.links.update_observed(id, source, target, observer)?;
        // The script runs once: the undo of this write comes through here too.
        for step in std::mem::take(&mut self.also) {
            match step {
                Also::Create(s, t) => {
                    let made = self.links.create(s, t);
                    observer(Link::null(), Link::new(made, s, t));
                }
                Also::Update(at, s, t) => {
                    self.links.update_observed(at, s, t, observer)?;
                }
                Also::Delete(at) => {
                    self.links.delete_observed(at, observer)?;
                }
                Also::Nothing => observer(Link::null(), Link::null()),
                Also::Fail => bail!("the store refused"),
            }
        }
        Ok(before)
    }
    fn delete_observed(&mut self, id: u32, observer: &mut dyn FnMut(Link, Link)) -> Result<Link> {
        if self.deletes_fail {
            bail!("deletes fail");
        }
        self.links.delete_observed(id, observer)
    }
    fn all_links(&mut self) -> Vec<Link> {
        NamedTypeLinks::all_links(&mut self.links)
    }
    fn search(&mut self, source: u32, target: u32) -> Option<u32> {
        NamedTypeLinks::search(&mut self.links, source, target)
    }
    fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
        NamedTypeLinks::get_or_create(&mut self.links, source, target)
    }
    fn get_name(&mut self, id: u32) -> Result<Option<String>> {
        NamedTypeLinks::get_name(&mut self.links, id)
    }
    fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
        NamedTypeLinks::set_name(&mut self.links, id, name)
    }
    fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
        NamedTypeLinks::get_by_name(&mut self.links, name)
    }
    fn remove_name(&mut self, id: u32) -> Result<()> {
        NamedTypeLinks::remove_name(&mut self.links, id)
    }
    fn save(&mut self) -> Result<()> {
        NamedTypeLinks::save(&mut self.links)
    }
}

fn view(links: Scripted, policy: Plain) -> PermissionsDecorator<Scripted, Plain> {
    PermissionsDecorator::new(links, policy, 0)
}

fn is_forbidden<T: std::fmt::Debug>(result: Result<T>) -> bool {
    result.unwrap_err().downcast_ref::<Forbidden>().is_some()
}

fn is_not_found<T: std::fmt::Debug>(result: Result<T>) -> bool {
    result
        .unwrap_err()
        .to_string()
        .starts_with("Link not found")
}

fn set(items: &[u32]) -> HashSet<u32> {
    items.iter().copied().collect()
}

#[test]
fn a_boxed_policy_is_asked_the_same_questions() -> Result<()> {
    let mut boxed: Box<dyn AccessPolicy> = Box::new(Plain {
        hidden: set(&[1]),
        locked: set(&[2]),
        referable: set(&[1]),
        no_create: true,
    });
    let mut data = LinkStorage::in_memory(false);
    assert_eq!(boxed.markers().allow_read, MARKERS.allow_read);
    assert!(!boxed.can_read(&mut data, 0, &Link::new(1, 0, 0)));
    assert!(!boxed.can_write(&mut data, 0, &Link::new(2, 0, 0)));
    assert!(!boxed.can_create(&mut data, 0));
    assert!(boxed.can_reference(&mut data, 0, &Link::new(1, 0, 0)));
    Ok(())
}

#[test]
fn a_view_hands_out_its_parts() {
    let mut links = view(Scripted::with_rows(1), Plain::default());
    links.policy_mut().no_create = true;
    assert!(is_forbidden(links.try_create(0, 0)));
    assert!(links.inner().also.is_empty());
    links.inner_mut().also.push(Also::Nothing);
    assert_eq!(links.into_inner().also.len(), 1);
}

#[test]
fn the_rules_keep_to_their_own_store_and_skip_marks_of_nothing() -> Result<()> {
    let mut apart = world::World::new(true)?;
    assert!(apart.policy.rules_mut().is_some());
    let mut together = world::World::new(false)?;
    assert!(together.policy.rules_mut().is_none());

    let marker = together.markers.of(PermissionAccess::Read);
    let granted = together.policy.granted(&mut together.links, marker);
    together.links.create(marker, 0);
    together.links.create(marker, 9_999);
    assert_eq!(
        together.policy.granted(&mut together.links, marker),
        granted
    );
    Ok(())
}

#[test]
fn a_write_to_nothing_answers_what_the_store_answers() {
    let mut links = view(Scripted::with_rows(1), Plain::default());
    assert!(links.update(0, 0, 0).is_err());
    assert!(is_not_found(links.update(99, 0, 0)));
    assert!(links.delete(0).is_err());
    assert!(is_not_found(links.delete(99)));
}

#[test]
fn a_hidden_link_is_not_found_and_a_locked_one_is_forbidden() -> Result<()> {
    let policy = Plain {
        hidden: set(&[1]),
        locked: set(&[2, 3]),
        ..Plain::default()
    };
    let mut links = view(Scripted::with_rows(4), policy);
    assert!(is_not_found(links.update(1, 0, 0)));
    assert!(is_not_found(links.delete(1)));
    assert!(is_forbidden(links.update(2, 0, 0)));
    assert!(is_forbidden(links.delete(2)));
    // A link it may write cannot be made into a link it may not write.
    links.policy_mut().locked = set(&[3]);
    assert!(is_forbidden(links.update(3, 4, 4)));
    assert_eq!(links.update(2, 4, 4)?, Link::new(2, 0, 0));
    Ok(())
}

#[test]
fn a_point_at_an_address_needs_every_right_and_a_free_address() -> Result<()> {
    let mut links = view(Scripted::with_rows(2), Plain::default());
    assert_eq!(links.try_ensure_created(2)?, 2, "it is there");
    let null = links.try_ensure_created(0).unwrap_err().to_string();
    assert!(null.contains("unsupported link address 0"), "{null}");
    assert!(!links.created().contains(&0));

    links.policy_mut().hidden = set(&[1, 7]);
    assert!(
        is_not_found(links.try_ensure_created(7)),
        "hidden and not referable"
    );
    links.policy_mut().referable = set(&[1, 7]);
    let taken = links.try_ensure_created(1).unwrap_err().to_string();
    assert!(taken.contains("Link 1 exists"), "{taken}");
    assert_eq!(links.ensure_created(7), 7);
    assert!(links.created().contains(&7));

    links.policy_mut().no_create = true;
    assert!(is_forbidden(links.try_ensure_created(9)));
    Ok(())
}

#[test]
fn the_calls_that_cannot_answer_an_error_panic_with_the_refusal() {
    let policy = Plain {
        no_create: true,
        ..Plain::default()
    };
    let mut links = view(Scripted::with_rows(1), policy);
    let panicked = |run: &mut dyn FnMut()| {
        let payload = catch_unwind(AssertUnwindSafe(run)).unwrap_err();
        let text = payload
            .downcast_ref::<String>()
            .cloned()
            .unwrap_or_else(|| payload.downcast_ref::<&str>().unwrap_or(&"").to_string());
        assert!(text.contains("Forbidden"), "{text}");
    };
    panicked(&mut || {
        links.create(0, 0);
    });
    panicked(&mut || {
        links.ensure_created(5);
    });
    panicked(&mut || {
        links.get_or_create(9, 9);
    });
}

#[test]
fn the_answers_about_addresses_and_saving_come_from_the_store() -> Result<()> {
    let mut links = view(Scripted::with_rows(1), Plain::default());
    assert_eq!(links.claims(1), None);
    assert_eq!(links.next_created_addresses(1)?, [2]);
    assert!(links.reference_exists(1));
    links.save()?;
    Ok(())
}

#[test]
fn names_follow_the_rights_of_their_links() -> Result<()> {
    let mut store = Scripted::with_rows(4);
    NamedTypeLinks::set_name(&mut store, 1, "hidden")?;
    NamedTypeLinks::set_name(&mut store, 2, "locked")?;
    NamedTypeLinks::set_name(&mut store, 3, "free")?;
    let policy = Plain {
        hidden: set(&[1]),
        locked: set(&[2]),
        ..Plain::default()
    };
    let mut links = view(store, policy);

    assert_eq!(links.get_name(1)?, None);
    assert_eq!(links.get_name(2)?.as_deref(), Some("locked"));
    assert_eq!(links.get_by_name("hidden")?, None);
    assert_eq!(links.get_by_name("locked")?, Some(2));
    assert_eq!(links.get_by_name("absent")?, None);

    assert!(is_not_found(links.set_name(1, "x")));
    assert!(is_not_found(links.set_name(99, "x")));
    assert!(is_forbidden(links.set_name(2, "x")));
    assert!(
        is_forbidden(links.set_name(4, "locked")),
        "it takes a name from a locked link"
    );
    assert_eq!(
        links.set_name(4, "hidden")?,
        4,
        "the rules below decide a hidden holder's name"
    );
    assert_eq!(links.set_name(3, "free")?, 3, "a link's own name");

    links.remove_name(1)?;
    assert_eq!(
        links.inner_mut().get_name(1)?,
        None,
        "the name moved to link 4"
    );
    assert!(is_forbidden(links.remove_name(2)));
    links.remove_name(3)?;
    assert_eq!(links.get_name(3)?, None);
    links.remove_name(99)?;
    Ok(())
}

fn scripted(also: Vec<Also>, policy: Plain) -> PermissionsDecorator<Scripted, Plain> {
    let mut store = Scripted::with_rows(4);
    store.also = also;
    view(store, policy)
}

#[test]
fn a_write_that_creates_another_link_needs_the_right_to_create_it() -> Result<()> {
    let mut links = scripted(vec![Also::Create(0, 0)], Plain::default());
    links.update(1, 1, 1)?;
    assert!(links.inner_mut().exists(5));

    let policy = Plain {
        no_create: true,
        ..Plain::default()
    };
    let mut links = scripted(vec![Also::Create(0, 0)], policy);
    assert!(is_forbidden(links.update(1, 1, 1)));
    assert!(!links.inner_mut().exists(5), "undone");
    assert_eq!(links.inner_mut().get_link(1), Some(Link::new(1, 0, 0)));

    let policy = Plain {
        locked: set(&[5]),
        ..Plain::default()
    };
    let mut links = scripted(vec![Also::Create(1, 1)], policy);
    assert!(
        is_forbidden(links.update(1, 1, 1)),
        "a new link it may not write"
    );
    assert!(!links.inner_mut().exists(5));
    Ok(())
}

#[test]
fn a_write_that_changes_another_link_needs_the_right_to_write_it() -> Result<()> {
    let locked = || Plain {
        locked: set(&[2]),
        ..Plain::default()
    };
    let mut links = scripted(vec![Also::Update(2, 2, 2)], locked());
    assert!(is_forbidden(links.update(1, 1, 1)));
    assert_eq!(links.inner_mut().get_link(2), Some(Link::new(2, 0, 0)));

    let mut links = scripted(vec![Also::Delete(2)], locked());
    assert!(is_forbidden(links.update(1, 1, 1)));
    assert!(links.inner_mut().exists(2), "undone");

    // Unchanged in the end, or still its own without a type.
    let steps = vec![Also::Update(2, 3, 3), Also::Update(2, 0, 0), Also::Nothing];
    let mut links = scripted(steps, locked());
    links.update(1, 1, 1)?;
    let mut links = scripted(vec![Also::Update(2, 0, 0)], locked()).with_created(set(&[2]));
    links.update(1, 1, 1)?;

    // A link that went away stops being the subject's own.
    let mut links = scripted(vec![Also::Delete(3)], Plain::default()).with_created(set(&[3]));
    let mut seen = Vec::new();
    links.update_observed(1, 1, 1, &mut |before, after| seen.push((before, after)))?;
    assert!(!links.created().contains(&3));
    assert_eq!(seen[0], (Link::new(1, 0, 0), Link::new(1, 1, 1)));
    Ok(())
}

#[test]
fn a_failed_write_is_undone_and_a_failed_undo_is_told() {
    let steps = vec![Also::Create(0, 0), Also::Fail];
    let mut links = scripted(steps.clone(), Plain::default());
    let refused = links.update(1, 1, 1).unwrap_err().to_string();
    assert_eq!(refused, "the store refused");
    assert!(!links.inner_mut().exists(5), "undone");
    assert_eq!(links.inner_mut().get_link(1), Some(Link::new(1, 0, 0)));

    let mut links = scripted(steps, Plain::default());
    links.inner_mut().deletes_fail = true;
    let told = format!("{:#}", links.update(1, 1, 1).unwrap_err());
    assert!(
        told.contains("and undoing it failed: deletes fail"),
        "{told}"
    );
    assert!(told.contains("the store refused"), "{told}");

    let mut links = scripted(vec![Also::Delete(2)], Plain::default());
    links.policy_mut().locked = set(&[2]);
    links.inner_mut().deletes_fail = true;
    // Deleting is the store's step, so a store that refuses it refuses the write.
    assert!(links.update(1, 1, 1).is_err());
}