link-cli 3.2.1

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
444
//! Permissions stored in links (issue #149). The cases are the "permissions"
//! tests of `@link-foundation/ui-links` plus what a store with cascades,
//! merges and names adds; the C# twin is
//! `csharp/Foundation.Data.Doublets.Cli.Tests/PermissionsDecoratorTests.cs`,
//! case for case.

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

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

use world::{all, World};

fn error_of<T>(result: Result<T>) -> Option<&'static str> {
    let error = result.err()?;
    if error.downcast_ref::<Forbidden>().is_some() {
        Some("forbidden")
    } else if error.to_string().starts_with("Link not found") {
        Some("not-found")
    } else {
        Some("bad-request")
    }
}

fn indexes(links: &mut dyn NamedTypeLinks) -> Vec<u32> {
    all(links).into_iter().map(|link| link.index).collect()
}

/// A policy of the application's own: anyone reads, no one writes.
struct ReadOnly(PermissionMarkers);

impl AccessPolicy for ReadOnly {
    fn markers(&self) -> PermissionMarkers {
        self.0
    }

    fn can_read(&mut self, _: &mut dyn NamedTypeLinks, _: u32, _: &Link) -> bool {
        true
    }

    fn can_write(&mut self, _: &mut dyn NamedTypeLinks, _: u32, _: &Link) -> bool {
        false
    }

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

#[test]
fn implements_named_type_links() -> Result<()> {
    fn takes(links: &mut dyn NamedTypeLinks) -> usize {
        links.all_links().len()
    }
    let mut w = World::new(false)?;
    let alice = w.v["alice"];
    let mut view = w.as_(alice);
    assert_eq!(3, takes(&mut view));
    // Any policy will do, the stored one or an application's own.
    let markers = w.markers;
    let total = w.links.all_links().len();
    let mut read_only = PermissionsDecorator::new(&mut w.links, ReadOnly(markers), 0);
    assert_eq!(total, takes(&mut read_only));
    assert_eq!(Some("forbidden"), error_of(read_only.try_create(0, 0)));
    Ok(())
}

#[test]
fn markers_are_named_points() -> Result<()> {
    let mut w = World::new(false)?;
    let m = w.markers;
    for (marker, name) in [
        (m.allow_read, "AllowRead"),
        (m.allow_write, "AllowWrite"),
        (m.anything, "Anything"),
        (m.allow_reference, "AllowReference"),
    ] {
        assert_eq!(Link::new(marker, marker, marker), w.link(marker));
        assert_eq!(
            Some(marker),
            NamedTypeLinks::get_by_name(&mut w.links, name)?
        );
    }
    assert_eq!(m, PermissionMarkers::ensure(&mut w.links)?);
    Ok(())
}

fn answers_by_type(separate_rules: bool) -> Result<()> {
    let mut w = World::new(separate_rules)?;
    let (alice, boss, note, shared, secret) = (
        w.v["alice"],
        w.v["boss"],
        w.v["note"],
        w.v["shared"],
        w.v["secret"],
    );
    let (note, shared, secret) = (w.link(note), w.link(shared), w.link(secret));
    let links = &mut w.links;
    let policy = &mut w.policy;
    assert!(policy.can_write(links, alice, &note));
    assert!(policy.can_read(links, alice, &shared));
    assert!(!policy.can_read(links, alice, &secret));
    assert!(policy.can_write(links, boss, &secret));
    assert!(!policy.can_read(links, 0, &note));
    assert!(policy.can_create(links, alice));
    assert!(!policy.can_create(links, 0));
    if separate_rules {
        // Three rules, each a pair and its mark.
        assert_eq!(6, w.rules().all_links().len());
    }
    Ok(())
}

#[test]
fn answers_by_type_rules_in_data() -> Result<()> {
    answers_by_type(false)
}

#[test]
fn answers_by_type_rules_apart() -> Result<()> {
    answers_by_type(true)
}

fn shows_a_subject_only_what_it_may_read(separate_rules: bool) -> Result<()> {
    let mut w = World::new(separate_rules)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    let mut view = w.as_(v["alice"]);
    // The sub-type Shared is of type Note; its Extends pair is not.
    assert_eq!(
        vec![t["Shared"], v["note"], v["shared"]],
        indexes(&mut view)
    );
    assert_eq!(3, view.query_links(None, None, None).len());
    assert!(!view.exists(v["secret"]));
    assert!(view.exists(v["note"]));
    assert_eq!(Some("not-found"), error_of(view.delete(v["secret"])));
    assert_eq!(
        Some("not-found"),
        error_of(view.update(v["secret"], t["Note"], v["secret"]))
    );
    Ok(())
}

#[test]
fn shows_a_subject_only_what_it_may_read_rules_in_data() -> Result<()> {
    shows_a_subject_only_what_it_may_read(false)
}

#[test]
fn shows_a_subject_only_what_it_may_read_rules_apart() -> Result<()> {
    shows_a_subject_only_what_it_may_read(true)
}

fn refuses_writes_the_store_unchanged(separate_rules: bool) -> Result<()> {
    let mut w = World::new(separate_rules)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    let before = all(&mut w.links);
    let mut view = w.as_(v["alice"]);
    assert_eq!(
        Some("forbidden"),
        error_of(view.update(v["note"], t["Secret"], v["note"]))
    );
    assert_eq!(
        Some("forbidden"),
        error_of(view.update(v["shared"], t["Public"], v["shared"]))
    );
    view.update(v["shared"], t["Note"], v["shared"])?;
    view.update(v["shared"], t["Shared"], v["shared"])?;
    assert_eq!(before, all(view.inner_mut()));
    let id = view.try_create(0, 0)?;
    view.update(id, 0, id)?;
    view.update(id, t["Note"], id)?;
    assert_eq!(
        Some("forbidden"),
        error_of(view.update(id, t["Secret"], id))
    );
    view.delete(id)?;
    assert_eq!(before, all(view.inner_mut()));
    let mut guest = w.as_(0);
    assert_eq!(Some("forbidden"), error_of(guest.try_create(0, 0)));
    assert_eq!(before, all(&mut w.links));
    Ok(())
}

#[test]
fn refuses_writes_the_store_unchanged_rules_in_data() -> Result<()> {
    refuses_writes_the_store_unchanged(false)
}

#[test]
fn refuses_writes_the_store_unchanged_rules_apart() -> Result<()> {
    refuses_writes_the_store_unchanged(true)
}

#[test]
fn keeps_the_hierarchy_from_who_may_write_its_values() -> Result<()> {
    let mut w = World::new(false)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    let extends = w.types.extends.expect("an Extends marker");
    let pair = w.links.search(t["Shared"], t["Public"]).expect("the pair");
    w.allow(PermissionAccess::Reference, t["User"], t["Admin"])?;
    let mut view = w.as_(v["alice"]);
    // The link of Note itself is of type Doc.
    assert_eq!(
        Some("not-found"),
        error_of(view.update(t["Note"], t["Admin"], t["Note"]))
    );
    // The pair Shared -> Public is of type Extends.
    assert_eq!(
        Some("not-found"),
        error_of(view.update(pair, t["Shared"], t["Admin"]))
    );
    let more = view.try_create(0, 0)?;
    view.update(more, t["Shared"], t["Admin"])?;
    let mark = view.try_create(0, 0)?;
    assert_eq!(
        Some("forbidden"),
        error_of(view.update(mark, extends, more))
    );
    let mut boss = w.as_(v["boss"]);
    boss.update(pair, t["Shared"], t["Public"])?;
    Ok(())
}

#[test]
fn refuses_a_marker_as_a_subject() -> Result<()> {
    let mut w = World::new(false)?;
    let m = w.markers;
    assert_eq!(
        Some("bad-request"),
        error_of(w.allow(PermissionAccess::Read, m.allow_read, 1))
    );
    assert_eq!(
        Some("bad-request"),
        error_of(w.allow(PermissionAccess::Write, m.allow_write, 1))
    );
    assert_eq!(
        Some("bad-request"),
        error_of(w.allow(PermissionAccess::Reference, m.allow_reference, 1))
    );
    assert_eq!(
        Some("bad-request"),
        error_of(PermissionPolicy::parse_access("own"))
    );
    // An access other than these cannot be written in Rust.
    assert_eq!(
        PermissionAccess::Write,
        PermissionPolicy::parse_access("write")?
    );
    assert_eq!(
        PermissionAccess::Reference,
        PermissionPolicy::parse_access("reference")?
    );
    Ok(())
}

fn gives_a_rule_given_twice_once(separate_rules: bool) -> Result<()> {
    let mut w = World::new(separate_rules)?;
    let t = w.t.clone();
    let count = w.rules().all_links().len();
    let rule = w.rules().search(t["User"], t["Note"]).expect("the rule");
    assert_eq!(
        rule,
        w.allow(PermissionAccess::Write, t["User"], t["Note"])?
    );
    assert_eq!(count, w.rules().all_links().len());
    let (alice, note) = (w.v["alice"], w.link(w.v["note"]));
    assert!(w.policy.can_write(&mut w.links, alice, &note));
    // The same pair may carry both marks.
    assert_eq!(rule, w.allow(PermissionAccess::Read, t["User"], t["Note"])?);
    assert_eq!(count + 1, w.rules().all_links().len());
    Ok(())
}

#[test]
fn gives_a_rule_given_twice_once_rules_in_data() -> Result<()> {
    gives_a_rule_given_twice_once(false)
}

#[test]
fn gives_a_rule_given_twice_once_rules_apart() -> Result<()> {
    gives_a_rule_given_twice_once(true)
}

#[test]
fn owns_what_it_created_while_untyped() -> Result<()> {
    let mut w = World::new(false)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    let mut view = w.as_(v["alice"]);
    let id = view.try_create(0, 0)?;
    assert!(view.exists(id));
    let link = view.inner_mut().get_link(id).unwrap();
    assert!(view.owns(&link));
    let created = view.created().clone();
    // Another subject does not own it, nor does a new view.
    assert!(!w.as_(v["alice"]).exists(id));
    let mut view = w.as_(v["alice"]).with_created(created);
    view.update(id, t["Note"], id)?;
    let link = view.inner_mut().get_link(id).unwrap();
    assert!(!view.owns(&link));
    // Undoing the update that typed it makes it its own again.
    view.inner_mut().update(id, 0, 0)?;
    let link = view.inner_mut().get_link(id).unwrap();
    assert!(view.owns(&link));
    // A self-reference has a type, itself, so the policy judges it (#152: only
    // a store read as triplets has points of its own).
    assert!(!view.owns(&Link::new(id, id, id)));
    view.forget(id);
    assert!(!view.exists(id));
    assert_eq!(Some("forbidden"), error_of(w.as_(0).try_create(0, 0)));
    Ok(())
}

#[test]
fn refuses_a_delete_that_would_take_a_hidden_link_with_it() -> Result<()> {
    let mut w = World::new(false)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    // The secret refers to alice's note: deleting the note deletes it.
    let mut boss = w.as_(v["boss"]);
    let hidden_use = boss.try_create(0, 0)?;
    boss.update(hidden_use, t["Secret"], v["note"])?;
    NamedTypeLinks::set_name(&mut w.links, hidden_use, "use")?;
    let before = all(&mut w.links);
    let mut view = w.as_(v["alice"]);
    assert_eq!(Some("forbidden"), error_of(view.delete(v["note"])));
    assert_eq!(before, all(&mut w.links));
    assert_eq!(
        Some("note".to_string()),
        NamedTypeLinks::get_name(&mut w.links, v["note"])?
    );
    assert_eq!(
        Some("use".to_string()),
        NamedTypeLinks::get_name(&mut w.links, hidden_use)?
    );
    // Boss may delete both.
    w.as_(v["boss"]).delete(v["note"])?;
    assert!(!w.links.exists(hidden_use));
    assert!(!w.links.exists(v["note"]));
    Ok(())
}

#[test]
fn refuses_a_merge_that_would_repoint_a_hidden_link() -> Result<()> {
    let mut w = World::new(false)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    let mut boss = w.as_(v["boss"]);
    // Two equal notes but for their targets; the secret refers to one.
    let other = boss.try_create(0, 0)?;
    boss.update(other, t["Note"], v["shared"])?;
    let hidden_use = boss.try_create(0, 0)?;
    boss.update(hidden_use, t["Secret"], other)?;
    let before = all(&mut w.links);
    // Making `other` equal to the note merges it into the note, repointing
    // the secret.
    assert_eq!(
        Link::new(v["note"], t["Note"], v["note"]),
        w.link(v["note"])
    );
    let mut view = w.as_(v["alice"]);
    assert_eq!(
        Some("forbidden"),
        error_of(view.update(other, t["Note"], v["note"]))
    );
    assert_eq!(before, all(&mut w.links));
    // Boss may.
    w.as_(v["boss"]).update(other, t["Note"], v["note"])?;
    assert!(!w.links.exists(other));
    assert_eq!(
        Link::new(hidden_use, t["Secret"], v["note"]),
        w.link(hidden_use)
    );
    Ok(())
}

#[test]
fn reports_every_change_of_a_write() -> Result<()> {
    let mut w = World::new(false)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    let mut view = w.as_(v["alice"]);
    let id = view.try_create(0, 0)?;
    let mut changes = Vec::new();
    view.update_observed(id, t["Note"], id, &mut |before, after| {
        changes.push((before, after))
    })?;
    assert_eq!(
        vec![(Link::new(id, 0, 0), Link::new(id, t["Note"], id))],
        changes
    );
    Ok(())
}

#[test]
fn names_need_the_right_to_write() -> Result<()> {
    let mut w = World::new(false)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    let mut view = w.as_(v["alice"]);
    // Names follow references: the name of a hidden link is an unknown name.
    assert_eq!(None, view.get_by_name("secret")?);
    assert_eq!(Some("Note".to_string()), view.get_name(t["Note"])?);
    view.set_name(v["note"], "my note")?;
    assert_eq!(
        Some("my note".to_string()),
        view.inner_mut().get_name(v["note"])?
    );
    assert_eq!(Some("not-found"), error_of(view.set_name(v["secret"], "x")));
    assert_eq!(
        Some("not-found"),
        error_of(w.as_(0).set_name(v["note"], "y"))
    );
    let mut view = w.as_(v["alice"]);
    // Taking the name of a link it may not write is taking a write.
    assert_eq!(
        Some("forbidden"),
        error_of(view.set_name(v["note"], "secret"))
    );
    assert_eq!(Some(v["secret"]), view.inner_mut().get_by_name("secret")?);
    view.remove_name(v["secret"])?;
    assert_eq!(
        Some("secret".to_string()),
        NamedTypeLinks::get_name(&mut w.links, v["secret"])?
    );
    w.as_(v["alice"]).remove_name(v["note"])?;
    assert_eq!(None, NamedTypeLinks::get_name(&mut w.links, v["note"])?);
    Ok(())
}

#[test]
fn writes_at_the_null_link_are_not_found_and_change_nothing() -> Result<()> {
    let mut w = World::new(false)?;
    let (t, v) = (w.t.clone(), w.v.clone());
    let before = all(&mut w.links);
    let mut view = w.as_(v["alice"]);
    assert_eq!(
        Some("not-found"),
        error_of(view.update(0, t["Note"], v["note"]))
    );
    assert_eq!(Some("not-found"), error_of(view.delete(0)));
    assert_eq!(before, all(&mut w.links));
    Ok(())
}