The tests across archives
These tests use several archives in neighbouring folders of a test: one of
authors, and others that collect, mirror, branch or accept their
documents. Each archive has a base URL https://example.org/<name>/, which
collections and URIs record, and the locations of the
resolver map https://example.org/ to the folder of
the test, so that no network is needed; one test serves the folder over
HTTP instead, with a local server, to read archives as a remote reader
does. They are in tests/test_across.py.
⟨across: the imports and the helpers⟩ ⟨across: resolution⟩ ⟨across: collections⟩ ⟨across: mirrors and key logs⟩ ⟨across: signing again⟩ ⟨across: branches and accepted copies⟩ ⟨across: owner catalogues⟩ ⟨across: drafts⟩ ⟨across: takedowns and indexes⟩
The helpers
The keys of the tests are those of the other tests:
predictable, in a key store of files of the test, with no wallet, GitHub
or clipboard within reach. locations maps the
base URLs to the folder of the test. make creates an archive named
name in that folder, with a new owner whose rotation keys go to
<name>-keys; deposit deposits a PDF alone, with a licence, as a new
work or a new version; line gives the identifier of a line by its label,
and uri a sheaf: URI of it, with the archive's base URL as at.
"""Tests across archives: resolution, collections, mirrors, key logs met, branches, drafts, takedowns and indexes (specification 0.2).""" import functools import http.server import json import shutil import socketserver import threading import urllib.parse from pathlib import Path import pytest from opensheaf import cli, core as P, keys, names, owners, remote, sign, wallet @pytest.fixture(autouse=True) def keys_of_the_tests(tmp_path, monkeypatch, request): monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) monkeypatch.setenv(keys.KEY_STORE, "file") monkeypatch.delenv(keys.SIGNING_KEY, raising=False) monkeypatch.delenv(P.PASSPHRASE, raising=False) monkeypatch.delenv(wallet.WALLET, raising=False) monkeypatch.setattr(wallet, "GH", "/nonexistent/gh") monkeypatch.setattr(wallet, "COPY", ["/nonexistent/pbcopy"]) monkeypatch.setattr(wallet, "PASTE", ["/nonexistent/pbpaste"]) monkeypatch.setattr(owners, "fetch_json", lambda url: pytest.fail(f"the network was read: {url}")) monkeypatch.setenv(keys.TEST_KEYS, ",".join(f"{request.node.name} {i}" for i in range(120))) monkeypatch.setenv(remote.LOCATIONS, f"https://example.org/={tmp_path}/") def run(*argv): cli.main(list(argv)) def make(tmp_path, name, *extra): root = tmp_path / name run("init", str(root), "--name", name.title(), "--base-url", f"https://example.org/{name}/", "--curator", "Doe, Jane", "--new-owner", "--export", str(tmp_path / f"{name}-keys"), "--no-passphrase", "--yes", *extra) return root def deposit(root, label, text, *extra, new=False, rights="cc-by-4.0"): pdf = root.parent / f"{label}.pdf" pdf.write_bytes(b"%PDF-1.4 " + text.encode()) run("--root", str(root), "deposit", label, "--pdf", str(pdf), "--no-source", "x", "--title", label.title(), "--author", "Doe, J", "--issued", "2020-01-01", "--rights", rights, *extra, *(["--new"] if new else [])) def line(root, label): P.configure(root) return P.LABELS[label] def uri(root, label, version=None, pin=None): _, nm, owner = line(root, label).split(":") base = json.loads((root / P.CONFIG_NAME).read_text())["baseUrl"] s = f"sheaf:{nm}:{owner}" + (f"/v{version}" if version else "") query = ([f"sha512={pin}"] if pin else []) + ["at=" + urllib.parse.quote(base, safe="")] return s + "?" + "&".join(query) def head_digest(root, label, version): return P.digest(P.obj_dir(line(root, label)) / version / "inventory.json") def errors(root): P.configure(root) return P.verify_errors() def findings(root, remote_=False): P.configure(root) return P.verify_findings(remote=remote_) def members(root, label): d = P.obj_dir(line(root, label)) return P.read_json(d, P.load_inventory(d), "members.json") def rotation_files(folder): return sorted(str(p) for p in Path(folder).glob("rotation-*.txt"))
Resolution
A paper of archive a, in two versions, is resolved from another archive,
c, by its sheaf: URI (section 12.3): the originator's line, without
owner part, resolves to its latest signed version; /v1 to the object
version holding document version 1; a pin to that version, and a wrong
digest to a mismatch. A short form is expanded among the lines the
archives read list. osheaf resolve prints the result as JSON, with the
full form of the pin.
def test_resolution_across_two_archives(tmp_path, capsys): a, c = make(tmp_path, "a"), make(tmp_path, "c") deposit(a, "paper", "one", new=True) deposit(a, "paper", "two") _, nm, owner = line(a, "paper").split(":") at = ["https://example.org/a/"] P.configure(c) with remote.Resolver() as r: lid, found = r.resolve(f"sheaf:{nm}", at) assert lid == f"sheaf:{nm}:{owner}" and (found.status, found.version) == ("verified", "v2") assert found.copy.where == "https://example.org/a/" _, f1 = r.resolve(f"sheaf:{nm}/v1", at) assert (f1.status, f1.version) == ("verified", "v1") d1 = head_digest(a, "paper", "v1") P.configure(c) _, fp = r.resolve(f"sheaf:{nm}/v1?sha512={d1}", at) assert (fp.status, fp.version) == ("verified", "v1") _, fm = r.resolve(f"sheaf:{nm}/v1?sha512={'0' * 128}", at) assert fm.status == "mismatch" _, fs = r.resolve(f"sheaf:{nm[:10]}:{owner[:10]}/v2", at) assert fs.version == "v2" _, fo = r.resolve(f"sheaf:{owner}", at) assert (fo.status, fo.version) == ("verified", "v1") with remote.Resolver() as r: assert r.resolve(f"sheaf:{nm}:{owner}", ["https://example.org/nowhere/"])[1].status == "unavailable" capsys.readouterr() with pytest.raises(SystemExit) as e: run("--root", str(c), "resolve", uri(a, "paper", 2), "--json") assert e.value.code == 0 out = json.loads(capsys.readouterr().out) assert out["status"] == "verified" and out["pin"] == f"sheaf:{nm}:{owner}/v2?sha512={head_digest(a, 'paper', 'v2')}" shutil.rmtree(P.obj_dir(line(a, "paper")) / sign.WITNESSES / "v2") with pytest.raises(SystemExit): run("--root", str(c), "resolve", uri(a, "paper")) assert "latest signed v1; after it: v2 unsigned (section 7.4)" in capsys.readouterr().out
The versions a copy holds after its latest signed one are not versions of
the line, and are no fork, but they are reported with their status (section
7.4): above, the second version of the paper, its signature file removed,
is resolved past, and osheaf resolve says that the copy holds it,
unsigned.
What another archive says is checked before it is used: a storage root
that leaves the archive is refused; every path of the manifest of every
inventory must be a content path of the object, in a copy as in a key log
read from disk; and one copy brings a bounded number of files.
def test_what_another_archive_says_is_checked(tmp_path, monkeypatch): a = make(tmp_path, "a") deposit(a, "paper", "one", new=True) for good in ("v1/content/paper.tm", "v12/content/a/b"): assert owners.content_path(good, {}, 12) for bad in ("v1/content/../../x", "/v1/content/x", "v1/other/x", "v2/content/x", "x/content/y", "v1/content", "v1/content//x", "v1\\content\\x", None): assert not owners.content_path(bad, {}, 1), bad assert owners.content_path("v1/data/x", {"contentDirectory": "data"}, 1) for root in ("../x/", "/etc/", "//evil.example/", "https://evil.example/", "ocfl", "a/../../"): folder = tmp_path / ("root" + str(abs(hash(root)))) folder.mkdir() (folder / "opensheaf.json").write_text(json.dumps({"specification": "0.2", "storageRoot": root})) with pytest.raises(remote.Unavailable, match="storage root"): remote.Source(str(folder)).object_path(line(a, "paper")) src = remote.Source(str(a)) monkeypatch.setattr(remote, "MAX_FETCH_FILES", 3) with pytest.raises(remote.Unavailable, match="more than 3 files"): remote.fetch_object(src, line(a, "paper"), tmp_path / "copy1") monkeypatch.setattr(remote, "MAX_FETCH_FILES", 20000) tampered = tmp_path / "tampered" shutil.copytree(a, tampered) d = tampered / remote.Source(str(a)).object_path(line(a, "paper")) inv = json.loads((d / "v1" / "inventory.json").read_bytes()) first = next(iter(inv["manifest"])) inv["manifest"][first] = ["v1/content/../../../../escaped"] data = json.dumps(inv).encode() for f in (d / "v1" / "inventory.json", d / "inventory.json"): f.write_bytes(data) f.with_name(f.name + ".sha512").write_bytes(owners.sidecar(data)) with pytest.raises(remote.Unavailable, match="out of the object"): remote.fetch_object(remote.Source(str(tampered)), line(a, "paper"), tmp_path / "copy2") assert not (tmp_path / "escaped").exists() owner = P.ARCHIVE["owner"] kd = tmp_path / "keylog" shutil.copytree(a / remote.Source(str(a)).object_path(f"sheaf:{owner}:{owner}"), kd) kinv = json.loads((kd / "v1" / "inventory.json").read_bytes()) state = kinv["versions"]["v1"]["state"] k0 = next(dg for dg, paths in state.items() if paths != ["genesis.json"]) kinv["manifest"][k0] = ["v1/content/../../../outside"] kdata = json.dumps(kinv).encode() (kd / "v1" / "inventory.json").write_bytes(kdata) (kd / "v1" / "inventory.json.sha512").write_bytes(owners.sidecar(kdata)) (tmp_path / "outside").write_text("not read") log = owners.load_key_log(kd) assert any("a content path out of the object" in p for p in log.problems)
Over HTTP, a reader cannot list folders: it reads the discovery document,
computes the path of each object in the archive's layout (here the hashed
layout, whose folder names hold %3a, encoded again in URLs), and fetches
the witnesses whose names it can compute from the owner's key log. The
archive is served by a local HTTP server, and the locations send the base
URL there.
class Quiet(http.server.SimpleHTTPRequestHandler): def log_message(self, *args): pass class Server(http.server.ThreadingHTTPServer): def server_bind(self): # without the slow lookup of the host's name socketserver.TCPServer.server_bind(self) self.server_name, self.server_port = "127.0.0.1", self.server_address[1] @pytest.fixture def server(tmp_path): httpd = Server(("127.0.0.1", 0), functools.partial(Quiet, directory=str(tmp_path))) threading.Thread(target=httpd.serve_forever, daemon=True).start() yield f"http://127.0.0.1:{httpd.server_address[1]}/" httpd.shutdown() def test_resolution_over_http(tmp_path, monkeypatch, server): h = make(tmp_path, "h", "--layout", "hashed") deposit(h, "paper", "one", new=True) deposit(h, "paper", "two") u = uri(h, "paper") monkeypatch.setenv(remote.LOCATIONS, f"https://example.org/={server}") c = make(tmp_path, "c") P.configure(c) with remote.Resolver() as r: _, found = r.resolve(u) assert (found.status, found.version) == ("verified", "v2") assert found.copy.src.folder is None and found.copy.statuses == [sign.SIGNED, sign.SIGNED]
A short form is expanded first: without an owner part, the reader copies the first version of each candidate to read the originator of its genesis record. That copy serves only the expansion; the line is then copied whole, with its witnesses, and resolves as its full URI does.
def test_short_uri_over_http(tmp_path, monkeypatch, server): h = make(tmp_path, "h", "--layout", "hashed") deposit(h, "paper", "one", new=True) deposit(h, "paper", "two") _, nm, owner = line(h, "paper").split(":") at = "?at=" + urllib.parse.quote("https://example.org/h/", safe="") monkeypatch.setenv(remote.LOCATIONS, f"https://example.org/={server}") c = make(tmp_path, "c") P.configure(c) for text in (f"sheaf:{nm}", f"sheaf:{nm[:16]}", f"sheaf:{nm[:16]}:{owner[:16]}"): with remote.Resolver() as r: obj_id, found = r.resolve(text + at) assert obj_id == f"sheaf:{nm}:{owner}" assert (found.status, found.version) == ("verified", "v2") assert found.copy.statuses == [sign.SIGNED, sign.SIGNED]
Collections
Archive papers has two documents. Archive reading makes a catalogue
from a collection.yaml listing them by their URIs, the first followed
(section 8.3). Its members record the lines, never a label, and the base
URL where copies are found. A new version of the followed paper is
followed by collection update, and so is a withdrawal, a new signed
version like any other; the paper not followed keeps its pin. Every
member is verified across archives (verify --remote, section 14.5), and
collection status says so.
def test_curated_collection_across_archives(tmp_path, capsys): a = make(tmp_path, "papers") for label in ("one", "two"): deposit(a, label, label, new=True) c = make(tmp_path, "reading", "--curated") spec = tmp_path / "collection.yaml" spec.write_text(f"title: Reading\ntype: catalogue\nmembers:\n" f" - {{uri: '{uri(a, 'two')}', follow: yes}}\n - '{uri(a, 'one')}'\n") run("--root", str(c), "collection", "sync", "reading-list", "--from", str(spec)) mem = members(c, "reading-list") _, nm2, owner = line(a, "two").split(":") assert mem["type"] == "catalogue" and [m["name"] for m in mem["members"]][0] == nm2 assert mem["members"][0]["at"] == ["https://example.org/papers/"] assert mem["members"][0]["follow"] == "latest" and "follow" not in mem["members"][1] assert errors(c) == [] assert members(c, "main")["order"] == "manual" deposit(a, "two", "two again") run("--root", str(c), "collection", "update") new = members(c, "reading-list")["members"] assert (new[0]["version"], new[1]["version"]) == ("v2", "v1") run("--root", str(a), "withdraw", "two", "--reason", "a mistake") run("--root", str(c), "collection", "update") new = members(c, "reading-list")["members"] assert new[0]["version"] == "v3" and new[0]["inventorySha512"] == head_digest(a, "two", "v3") found = findings(c, remote_=True) assert [x for x in found if x["severity"] == "error"] == [] assert sum(1 for x in found if x["message"].startswith("verified at https://example.org/papers/")) == 2 capsys.readouterr() run("--root", str(c), "collection", "status", "reading-list") assert capsys.readouterr().out.count("verified: ") == 2
The statuses of section 8.5, across archives: a member whose line is taken down in its archive is removed; a pin of another digest is a mismatch; a member whose archive has gone is unavailable; a version signed with a key its owner later exposes is contested.
def test_member_statuses(tmp_path): a = make(tmp_path, "a") for label in ("one", "two", "three"): deposit(a, label, label, new=True) c = make(tmp_path, "c") run("--root", str(c), "collection", "new", "list", "--type", "catalogue", "--title", "List") run("--root", str(c), "collection", "add", "list", uri(a, "one"), uri(a, "two"), uri(a, "three")) assert errors(c) == [] from opensheaf import collections run("--root", str(a), "takedown", "one", "--reason", "Taken down at the request of the holder.", "--yes") P.configure(c) with remote.Resolver() as r: assert sorted(s for _, _, s in collections.statuses(members(c, "list"), r)) == \ ["removed", "verified", "verified"] m = members(c, "list")["members"][1] assert r.pin(m["name"], m["owner"], "v1", "0" * 128, m["at"]).status == "mismatch" P.configure(a) first = P.key_log(P.ARCHIVE["owner"]).latest().keys["keys"][0] rk = rotation_files(tmp_path / "a-keys") P.configure(a) third = P.key_new(P.argparse_namespace(owner=None)).did run("--root", str(a), "owner", "expose", first, "--add", third, "--rotation-key", rk[0], "--rotation-key", rk[1], "--export", str(tmp_path / "a-keys-2"), "--no-passphrase", "--yes") P.configure(c) with remote.Resolver() as r: assert sorted(s for _, _, s in collections.statuses(members(c, "list"), r)) == \ ["contested", "contested", "removed"] shutil.move(str(a), str(tmp_path / "gone")) P.configure(c) with remote.Resolver() as r: assert {s for _, _, s in collections.statuses(members(c, "list"), r)} == {"unavailable"}
A pin is decided by the bytes of the inventory (section 12.3). A copy whose checksum file is missing still matches, with a warning naming where it is, and copied, gets one that states it; a copy whose inventory was changed does not match, and is reported with its location, but a copy elsewhere that matches decides the status; alone, it is a mismatch.
def test_copies_that_disagree_with_a_pin(tmp_path): a = make(tmp_path, "a") deposit(a, "paper", "one", new=True) obj_id = line(a, "paper") _, nm, owner = obj_id.split(":") dig = head_digest(a, "paper", "v1") path = remote.Source(str(a)).object_path(obj_id) nocheck, tampered = tmp_path / "nocheck", tmp_path / "tampered" shutil.copytree(a, nocheck) (nocheck / path / "v1" / "inventory.json.sha512").unlink() shutil.copytree(a, tampered) inv = tampered / path / "v1" / "inventory.json" inv.write_bytes(inv.read_bytes() + b" ") (tampered / path / "v1" / "inventory.json.sha512").write_bytes(owners.sidecar(inv.read_bytes())) P.configure(a) with remote.Resolver(here=False) as r: assert r.pin(nm, owner, "v1", dig, [str(nocheck)]).status == "verified" assert remote.checksum_warning(str(nocheck), obj_id, 1) in [m for _, m in r.notes] with remote.Resolver(here=False) as r: assert r.pin(nm, owner, "v1", dig, [str(tampered), str(a)]).status == "verified" assert f"{tampered}: {obj_id} v1 does not match the pin (section 12.3)" in [m for _, m in r.notes] with remote.Resolver(here=False) as r: assert r.pin(nm, owner, "v1", dig, [str(tampered)]).status == "mismatch" warned, copy = [], tmp_path / "copy" remote.fetch_object(remote.Source(str(nocheck)), obj_id, copy, warn=warned.append) assert len(warned) == 1 and "checksum file" in warned[0] assert (copy / "v1" / "inventory.json.sha512").read_bytes() == \ owners.sidecar((copy / "v1" / "inventory.json").read_bytes())
A selection pins the revisions its curator vetted (section 8.4). Archive
digest vets document versions 2 and 1 of a paper with three; they are
kept in the order of the paper's history, each with the object version
that held it and its digest. Later versions of the paper change nothing:
update only reports the versions not vetted, and vetting one more is
the curator's act, a new version of the selection. Removing one revision
keeps the member with the others; removing its last revision removes the
member; a revision the member does not have is refused.
def test_selection_pins_revisions(tmp_path, capsys): a = make(tmp_path, "papers") for k in (1, 2, 3): deposit(a, "paper", f"version {k}", new=k == 1) s = make(tmp_path, "digest") spec = tmp_path / "digest.yaml" spec.write_text(f"type: selection\ntitle: Digest\nmembers:\n - {{uri: '{uri(a, 'paper')}', " "revisions: [2, 1]}\n") run("--root", str(s), "collection", "sync", "digest", "--from", str(spec)) m = members(s, "digest")["members"][0] assert "version" not in m and "follow" not in m assert [(x["documentVersion"], x["version"], x["inventorySha512"]) for x in m["revisions"]] == \ [(n, f"v{n}", head_digest(a, "paper", f"v{n}")) for n in (1, 2)] assert errors(s) == [] deposit(a, "paper", "version 4") head = P.load_inventory(P.obj_dir(line(s, "digest")))["head"] capsys.readouterr() run("--root", str(s), "collection", "update", "digest") assert "document version(s) 3, 4 not vetted" in capsys.readouterr().out assert P.load_inventory(P.obj_dir(line(s, "digest")))["head"] == head run("--root", str(s), "collection", "add", "digest", uri(a, "paper"), "--revision", "3") new = members(s, "digest")["members"][0]["revisions"] assert new[:2] == m["revisions"] and (new[2]["documentVersion"], new[2]["version"]) == (3, "v3") found = findings(s, remote_=True) assert [x for x in found if x["severity"] == "error"] == [] with pytest.raises(SystemExit): run("--root", str(s), "collection", "add", "digest", uri(a, "paper"), "--revision", "9") run("--root", str(s), "collection", "remove", "digest", uri(a, "paper"), "--revision", "2") assert [x["documentVersion"] for x in members(s, "digest")["members"][0]["revisions"]] == [1, 3] with pytest.raises(SystemExit, match="no member matches"): run("--root", str(s), "collection", "remove", "digest", uri(a, "paper"), "--revision", "2") run("--root", str(s), "collection", "remove", "digest", uri(a, "paper"), "--revision", "1", "--revision", "3") assert members(s, "digest")["members"] == []
A featured collection
An author's archive author holds a paper of its own and a reading list,
a selection with a paper of another archive and the author's own paper. The
owner catalogue lists the archive's own documents (the reading list is a
line of the archive, not a member of the owner catalogue); with
index.html.featured naming the reading list by its label, the home page
lists it first, under its title and with its description as the lead
paragraph, then the owner catalogue, under its own heading. Both show their
pins, and the verifier, which checks the owner catalogue's, is satisfied.
The paper of the other archive is read from there, checked against its
pin: its title links its own landing page, there. A collection of the
archive inside the featured one gets a landing page listing its members by
their lines, and a collection inside that one gets a page too. A setting
that names something other than a collection of the archive stops osheaf
index.
def test_featured_collection_on_the_home_page(tmp_path): p, a = make(tmp_path, "papers"), make(tmp_path, "author") deposit(p, "one", "one", new=True) deposit(a, "mine", "mine", new=True) run("--root", str(a), "collect") (tmp_path / "reading.yaml").write_text( "title: A reading list\ndescription: Papers to read first.\ntype: selection\n" f"members:\n - '{uri(p, 'one')}'\n - {{label: mine}}\n") run("--root", str(a), "collection", "sync", "reading-list", "--from", str(tmp_path / "reading.yaml")) main = members(a, "main")["members"] assert [m["name"] for m in main] == [line(a, "mine").split(":")[1]] cfg = json.loads((a / "osheaf.json").read_text()) cfg["index"] = {"html": {"templates": "templates", "featured": "reading-list"}} (a / "osheaf.json").write_text(json.dumps(cfg)) run("--root", str(a), "index") home = (a / "index.html").read_text() assert home.index("<h2>A reading list</h2>") < home.index("<h2>In this archive</h2>") assert "Papers to read first." in home assert f'<a class="paper-title" href="https://example.org/papers/paper/{line(p, "one").split(":")[1]}/">One</a>' \ in home assert "in <a href=\"https://example.org/papers/\">Papers</a>" in home for m in members(a, "reading-list")["members"] + main: assert P.member_pins(m)[-1][1][:32] in home assert errors(a) == [] (tmp_path / "sub2.yaml").write_text("title: Part two\ntype: catalogue\nmembers: []\n") run("--root", str(a), "collection", "sync", "part-two", "--from", str(tmp_path / "sub2.yaml")) (tmp_path / "sub.yaml").write_text( "title: Part one\ndescription: The first part.\ntype: catalogue\n" f"members:\n - {{label: mine}}\n - '{uri(p, 'one')}'\n - {{label: part-two}}\n") run("--root", str(a), "collection", "sync", "part-one", "--from", str(tmp_path / "sub.yaml")) (tmp_path / "reading.yaml").write_text( "title: A reading list\ndescription: Papers to read first.\ntype: selection\n" f"members:\n - '{uri(p, 'one')}'\n - {{label: mine}}\n - {{label: part-one}}\n") run("--root", str(a), "collection", "sync", "reading-list", "--from", str(tmp_path / "reading.yaml")) run("--root", str(a), "index") part = (a / "paper" / "part-one" / "index.html").read_text() assert "A catalogue curated by" in part and "3 members" in part and "The first part." in part assert 'href="../../paper/mine/"' in part and 'href="../../paper/part-two/"' in part assert ">One</a>" in part and "PDF</a>" not in part.split("<h2>Members</h2>")[0] assert line(a, "part-one").rsplit(":", 1)[0] + "</code>" in part assert (a / "paper" / "part-two" / "index.html").exists() and (a / "paper" / "mine" / "index.html").exists() assert errors(a) == [] cfg["index"]["html"]["featured"] = "mine" (a / "osheaf.json").write_text(json.dumps(cfg)) with pytest.raises(SystemExit, match="not a collection"): run("--root", str(a), "index")
Only http and https addresses become links
A file of members that adds a javascript: URL, or a plain http one, to
the at of a member already in a collection does not get it recorded: it
is left out with a note, as collection add leaves it out. On the
website, the addresses read from a member's other archive, or from its
cached at, become links only if they are http or https URLs;
anything else is shown as text.
def test_only_http_links(tmp_path, monkeypatch, capsys): from opensheaf import html p, a = make(tmp_path, "papers"), make(tmp_path, "author") deposit(p, "one", "one", new=True) spec = tmp_path / "list.yaml" spec.write_text(f"title: A list\nmembers:\n - '{uri(p, 'one')}'\n") run("--root", str(a), "collection", "sync", "list", "--from", str(spec)) at = members(a, "list")["members"][0]["at"] spec.write_text(f"title: A list\nmembers:\n - uri: '{uri(p, 'one')}'\n" " at: ['javascript:alert(1)//', 'http://example.com/']\n") capsys.readouterr() run("--root", str(a), "collection", "sync", "list", "--from", str(spec)) assert members(a, "list")["members"][0]["at"] == at assert capsys.readouterr().out.count("not recorded") == 2 assert html.safe_url("javascript:alert(1)") == "" and html.safe_url(" https://x/") == "" assert html.safe_url("HTTP://x/") == "HTTP://x/" assert html.link("javascript:alert(1)", "x") == "x" assert html.link("data:text/html,x", "x", "paper-title") == '<span class="paper-title">x</span>' assert html.link("https://x/?a=1&b", "x") == '<a href="https://x/?a=1&b">x</a>' m = dict(members(a, "list")["members"][0], at=["javascript:alert(1)"]) monkeypatch.setattr(html, "remote_view", lambda m: None) assert "href" not in html.remote_entry(m) monkeypatch.setattr(html, "remote_view", lambda m: { "archive": "Elsewhere", "at": "javascript:alert(2)", "pdf": "data:text/html,x", "title": "T", "by": "", "abstract": "", "landing": "javascript:alert(3)", "withdrawn": ""}) out = html.remote_entry(m) assert "href" not in out and "Elsewhere" in out and ">T<" in out
What the website shows of a member elsewhere
The pin fixes the inventory of the version pinned, and the inventory fixes
its files: a datacite.json served in place of the one the inventory
names (here, with another title) is not shown, and the member shows its
cached citation. A description of the form not expected, pinned and
listed by the inventory (no title), fails that member only: the website
is written, the member with its cached citation.
def forge(d, ver, logical, data): """Put DATA in place of LOGICAL in vVER of the object in D, its inventory rewritten to match.""" p = d / ver / "inventory.json" inv = json.loads(p.read_text()) old = next(dg for dg, ps in inv["versions"][ver]["state"].items() if logical in ps) cp = inv["manifest"].pop(old)[0] (d / cp).write_bytes(data) new = names.sha512_hex(data) inv["manifest"][new] = [cp] inv["versions"][ver]["state"][new] = inv["versions"][ver]["state"].pop(old) raw = json.dumps(inv).encode() p.write_bytes(raw) (d / "inventory.json").write_bytes(raw) return names.sha512_hex(raw) def test_a_member_elsewhere_whose_files_are_not_those_pinned(tmp_path): from opensheaf import collections p, a = make(tmp_path, "papers"), make(tmp_path, "author") deposit(p, "one", "one", new=True) spec = tmp_path / "list.yaml" spec.write_text(f"title: A list\ntype: catalogue\nmembers:\n - '{uri(p, 'one')}'\n") run("--root", str(a), "collection", "sync", "list", "--from", str(spec)) cfg = json.loads((a / "osheaf.json").read_text()) cfg["index"] = {"html": {"featured": "list"}} (a / "osheaf.json").write_text(json.dumps(cfg)) run("--root", str(a), "index") home = (a / "index.html").read_text() assert ">One</a>" in home and "cached citation" not in home d = P.obj_dir(line(p, "one")) inv = P.load_inventory(d) dc_path = d / P.content_path(inv, "v1", "datacite.json") saved = dc_path.read_bytes() dc_path.write_bytes(saved.replace(b'"One"', b'"Forged"')) run("--root", str(a), "index") home = (a / "index.html").read_text() assert "Forged" not in home and "archive unavailable: cached citation" in home dc = json.loads(saved) dc["titles"] = [] pin = forge(d, "v1", "datacite.json", json.dumps(dc).encode()) P.configure(a) m = members(a, "list")["members"][0] collections.write(line(a, "list"), dict(members(a, "list"), members=[dict(m, inventorySha512=pin)]), message="a pin by hand") run("--root", str(a), "index") home = (a / "index.html").read_text() assert "archive unavailable: cached citation" in home and ">One<" in home
A withdrawn member of an archive that pages its objects
The status of a member elsewhere is read from the discovery document of its archive, from its pages of objects when it pages them (#19): a line withdrawn in an archive with a page per object is shown withdrawn, with its reason, on the home page of the archive that features it.
def test_a_withdrawn_member_of_a_paged_archive(tmp_path, monkeypatch): monkeypatch.setattr(P, "OBJECTS_PER_PAGE", 1) p, a = make(tmp_path, "papers"), make(tmp_path, "author") deposit(p, "one", "one", new=True) deposit(p, "two", "two", new=True) spec = tmp_path / "list.yaml" spec.write_text(f"title: A list\ntype: catalogue\nmembers:\n - '{uri(p, 'one')}'\n") run("--root", str(a), "collection", "sync", "list", "--from", str(spec)) run("--root", str(p), "withdraw", "one", "--reason", "a mistake") disc = json.loads((p / "opensheaf.json").read_text()) assert "objects" not in disc and disc["objectPages"] cfg = json.loads((a / "osheaf.json").read_text()) cfg["index"] = {"html": {"featured": "list"}} (a / "osheaf.json").write_text(json.dumps(cfg)) run("--root", str(a), "index") home = (a / "index.html").read_text() assert "Withdrawn by its owner" in home and "a mistake" in home
Mirrors and key logs
A library mirrors an openly licensed paper of the authors (section 11):
every version up to the latest signed, with the authors' key log beside
it; a paper whose licence is not known to allow redistribution is copied
with a warning, since the library answers for the copy (section 11.2). The
mirror is listed like any line, without mirrorOf; the owner catalogue
leaves it out, since its owner does not write here. Run again, the mirror
gets the new versions; a version without signatures is not added
(section 7.5).
def test_mirror(tmp_path, capsys): a, lib = make(tmp_path, "authors"), make(tmp_path, "library") deposit(a, "open", "one", new=True) deposit(a, "open", "two") deposit(a, "closed", "closed", new=True, rights="All rights reserved") run("--root", str(lib), "mirror", uri(a, "closed")) assert "not one known to allow redistribution" in capsys.readouterr().err run("--root", str(lib), "mirror", uri(a, "open"), "--label", "open-copy") obj_id = line(a, "open") a_owner = obj_id.split(":")[2] P.configure(lib) assert (P.obj_dir(obj_id) / "v2").is_dir() and P.key_log(a_owner) is not None assert P.LABELS["open-copy"] == obj_id disc = json.loads((lib / "opensheaf.json").read_text()) assert all("mirrorOf" not in o for o in disc["objects"]) and "mirrors" not in disc assert errors(lib) == [] assert members(lib, "main")["members"] == [] deposit(a, "open", "three") d = P.obj_dir(line(a, "open")) deposit(a, "open", "four") for f in (P.obj_dir(line(a, "open")) / sign.WITNESSES / "v4").iterdir(): f.unlink() run("--root", str(lib), "mirror", "--all") P.configure(lib) inv = P.load_inventory(P.obj_dir(obj_id)) assert inv["head"] == "v3" assert errors(lib) == []
Two copies of a line that do not agree are a fork of the line (section 7.4): the paper is signed twice at version 3, once in each of two copies of the authors' archive; the library, which mirrored one, refuses the other, and keeps its side. A resolver given both copies notes the fork, and does not choose.
def test_mirror_refuses_a_fork_of_the_line(tmp_path): a, lib = make(tmp_path, "authors"), make(tmp_path, "library") deposit(a, "open", "one", new=True) deposit(a, "open", "two") shutil.copytree(a, tmp_path / "twin") cfg = json.loads((tmp_path / "twin" / P.CONFIG_NAME).read_text()) cfg["baseUrl"] = "https://example.org/twin/" (tmp_path / "twin" / P.CONFIG_NAME).write_text(json.dumps(cfg)) deposit(a, "open", "three") deposit(tmp_path / "twin", "open", "three, otherwise") run("--root", str(lib), "mirror", uri(a, "open")) with pytest.raises(SystemExit, match="fork of the line"): run("--root", str(lib), "mirror", uri(a, "open"), "--at", "https://example.org/twin/") _, nm, owner = line(a, "open").split(":") P.configure(lib) with remote.Resolver() as r: found = r.latest(nm, owner, ["https://example.org/authors/", "https://example.org/twin/"]) assert found.status == "fork" and any("a fork of the line" in m for _, m in r.notes)
A mirror copies what it judged. When the authors' copy changes between the reading that judges it and the fetch that copies it (here, the inventory of v2 gets another byte), nothing is copied. A signature file that does not verify, put beside the good one with a file that goes with it, is left out of the mirror, which verifies.
def test_mirror_copies_what_it_judged(tmp_path, monkeypatch, capsys): a, lib = make(tmp_path, "authors"), make(tmp_path, "library") deposit(a, "open", "one", new=True) deposit(a, "open", "two") obj_id = line(a, "open") src = P.obj_dir(obj_id) fetch = remote.fetch_object def changing(*args, **kw): p = src / "v2" / "inventory.json" p.write_bytes(p.read_bytes() + b"\n") return fetch(*args, **kw) monkeypatch.setattr(remote, "fetch_object", changing) with pytest.raises(SystemExit, match="not the one verified"): run("--root", str(lib), "mirror", uri(a, "open")) P.configure(lib) assert not P.obj_dir(obj_id).exists() monkeypatch.setattr(remote, "fetch_object", fetch) p = src / "v2" / "inventory.json" p.write_bytes(p.read_bytes()[:-1]) w = src / sign.WITNESSES / "v1" good = sorted(w.iterdir())[0] forged = json.loads(good.read_text()) forged["inventorySha512"] = "0" * 128 (w / "signature-forged.json").write_text(json.dumps(forged)) (w / "signature-forged.json.ots").write_bytes(b"x") capsys.readouterr() run("--root", str(lib), "mirror", uri(a, "open")) assert "signature-forged.json" in capsys.readouterr().err P.configure(lib) mw = P.obj_dir(obj_id) / sign.WITNESSES / "v1" assert (mw / good.name).exists() and not (mw / "signature-forged.json").exists() assert not (mw / "signature-forged.json.ots").exists() assert errors(lib) == []
A copy is checked against pins too: a member pinned in archive a is
found at a mirror in the library when a loses it, and is unavailable
when both are gone (copies that agree, section 7.4).
def test_check_remote_tries_the_copies(tmp_path): a, lib, c = make(tmp_path, "a"), make(tmp_path, "lib"), make(tmp_path, "c") deposit(a, "open", "one", new=True) run("--root", str(lib), "mirror", uri(a, "open")) run("--root", str(c), "collection", "new", "list", "--type", "catalogue", "--title", "List") run("--root", str(c), "collection", "add", "list", uri(a, "open"), "--at", "https://example.org/lib/") m = members(c, "list")["members"][0] assert m["at"] == ["https://example.org/a/", "https://example.org/lib/"] obj_dir = P.obj_dir(line(a, "open")) shutil.move(str(obj_dir), str(tmp_path / "lost")) assert [x for x in findings(c, True) if x["severity"] == "warning" and "member" in x["place"]] == [] shutil.move(str(lib), str(tmp_path / "lib-gone")) assert any(x["message"] == "unavailable (section 8.5)" for x in findings(c, True))
An owner that writes in two archives keeps a copy of its key log in each
(section 6.5). Keys changed in one reach the other by owner pull, which
extends its copy. When two copies are signed differently at the same
number, by rotation keys stolen before use, the key log forks (section
6.3): the archive keeps the side it saw first, and appends a fork record
carrying the other side, which ends the owner. Nothing is then signed for
it, and its lines are frozen: no archive mirrors them (section 7.5). The
fork record is a valid version, which the verifier reports, and the
indexes list the version it carries.
def test_owner_pull_and_a_fork_of_the_key_log(tmp_path, capsys): a = make(tmp_path, "a") owner = P.ARCHIVE["owner"] deposit(a, "paper", "one", new=True) b = tmp_path / "b" run("init", str(b), "--name", "B", "--base-url", "https://example.org/b/", "--curator", "Doe, Jane", "--owner", owner, "--key-log", str(a / "ocfl" / owner / owner)) rk = rotation_files(tmp_path / "a-keys") run("--root", str(a), "owner", "rotate", "--rotation-key", rk[0], "--rotation-key", rk[1], "--export", str(tmp_path / "k2"), "--no-passphrase", "--yes") run("--root", str(b), "owner", "pull", "https://example.org/a/") P.configure(b) assert len(P.key_log(owner).versions) == 2 and errors(b) == [] twin = tmp_path / "twin" shutil.copytree(a, twin) run("--root", str(twin), "owner", "rotate", "--rotation-key", rotation_files(tmp_path / "k2")[0], "--rotation-key", rotation_files(tmp_path / "k2")[1], "--export", str(tmp_path / "k3a"), "--no-passphrase", "--yes") run("--root", str(a), "owner", "rotate", "--rotation-key", rotation_files(tmp_path / "k2")[0], "--rotation-key", rotation_files(tmp_path / "k2")[2], "--export", str(tmp_path / "k3b"), "--no-passphrase", "--yes") capsys.readouterr() run("--root", str(b), "owner", "pull", str(a / "ocfl" / owner / owner)) run("--root", str(b), "owner", "pull", str(twin)) assert "a fork at v3" in capsys.readouterr().out P.configure(b) log = P.key_log(owner) assert log.fork_point == 3 and log.ended and log.versions[-1].fork_record found = findings(b) assert [x for x in found if x["severity"] == "error"] == [] assert any(x["message"] == "ended by a fork of its key log at v3 (section 6.3)" for x in found) carried = log.versions[-1].carried[0] assert any(e["inventorySha512"] == carried.digest and e["version"] == "v3" for e in P.read_index("sha512")) with pytest.raises(SystemExit, match="has ended"): deposit(b, "new-paper", "two", new=True) lib = make(tmp_path, "lib") run("--root", str(lib), "owner", "pull", "https://example.org/b/", "--owner", owner) with pytest.raises(SystemExit, match="frozen"): run("--root", str(lib), "mirror", uri(a, "paper")) run("--root", str(a), "owner", "pull", "https://example.org/twin/") P.configure(a) assert P.key_log(owner).fork_point == 3
A pin of a key-log version resolves only to a version of the key log. Once the signatures of v2 are gone, v2 is still held with the number and digest pinned, but it is no longer a version of the key log: the pin is "not a version", never verified. A collection held here that pins it (by hand: the tools pin only documents and collections, and the schema says so) is reported by the verifier.
def test_a_pin_to_a_key_log_version_that_is_not_one(tmp_path): from opensheaf import collections a = make(tmp_path, "a") owner = P.ARCHIVE["owner"] rk = rotation_files(tmp_path / "a-keys") run("--root", str(a), "owner", "rotate", "--rotation-key", rk[0], "--rotation-key", rk[1], "--export", str(tmp_path / "k2"), "--no-passphrase", "--yes") P.configure(a) kd = P.obj_dir(names.object_id(owner, owner)) dig = P.digest(kd / "v2" / "inventory.json") member = {"kind": "owner", "name": owner, "owner": owner, "at": ["https://example.org/a/"], "version": "v2", "inventorySha512": dig} collections.write(None, {"format": collections.FORMAT, "type": "catalogue", "order": "manual", "members": [member]}, message="test", title="Keys", owner=owner, label="keys") with remote.Resolver(here=False) as r: assert r.pin(owner, owner, "v2", dig, [str(a)]).status == "verified" assert not [e for e in errors(a) if "not a version of the key log" in e] shutil.rmtree(kd / sign.WITNESSES / "v2") P.configure(a) assert P.key_log(owner).lookup(2, dig) is not None and P.key_log(owner).verified(2, dig) is None with remote.Resolver(here=False) as r: assert r.pin(owner, owner, "v2", dig, [str(a)]).status == "not a version" assert any("not a version of the key log" in e for e in errors(a))
Signing again
A version signed with a key the owner then exposes is contested (section 7.3); while the owner is not ended, it signs the version again with a current key not exposed, and the version is signed (section 7.1). A version that is not contested is not signed again.
def test_sign_again(tmp_path): a = make(tmp_path, "a") first = P.key_log(P.ARCHIVE["owner"]).latest().keys["keys"][0] deposit(a, "paper", "one", new=True) P.configure(a) third = P.key_new(P.argparse_namespace(owner=None)).did rk = rotation_files(tmp_path / "a-keys") run("--root", str(a), "owner", "expose", first, "--add", third, "--rotation-key", rk[0], "--rotation-key", rk[1], "--export", str(tmp_path / "k2"), "--no-passphrase", "--yes") d = P.obj_dir(line(a, "paper")) assert P.line_statuses(d, P.load_inventory(d))[1] == [sign.CONTESTED] run("--root", str(a), "sign", "paper", "--version", "v1", "--yes") P.configure(a) assert P.line_statuses(d, P.load_inventory(d))[1] == [sign.SIGNED] assert errors(a) == [] with pytest.raises(SystemExit, match="signing again is for a version contested"): run("--root", str(a), "sign", "paper", "--version", "v1", "--yes")
Branches and accepted copies
A journal accepts version 2 of the authors' paper (section 9.6): the
accepted copy is a branch under the journal's owner, named by the paper's
genesis name, holding the files of the authors' version 2, its genesis
record and the evidence of the branch point, and listed by the issue, a
selection, with the revision it holds and, with --with-original, the
authors' revision set. The accepted copy is the journal's document
version 1, parent.documentVersion keeping the authors' 2 (section 9.4).
The authors' version 3 changes nothing in it; the journal adds a notice to
its copy, with a file; accepting version 3 takes a new branch point
(section 9.4), the next document version of the branch, 2, which keeps the
journal's notice and its file, and adds the revision. The verifier
confirms the branch point at both versions.
def test_accept_vetted_versions(tmp_path): a, j = make(tmp_path, "authors"), make(tmp_path, "journal") deposit(a, "paper", "one", new=True) deposit(a, "paper", "two") run("--root", str(j), "collection", "new", "issue-1", "--type", "selection", "--title", "Issue 1") run("--root", str(j), "accept", uri(a, "paper", 2), "--selection", "issue-1", "--label", "paper", "--with-original") _, nm, a_owner = line(a, "paper").split(":") branch_id = line(j, "paper") j_owner = P.ARCHIVE["owner"] assert branch_id == f"sheaf:{nm}:{j_owner}" d = P.obj_dir(branch_id) inv = P.load_inventory(d) obj = P.read_json(d, inv, P.OBJ) assert obj["parent"]["owner"] == a_owner and obj["parent"]["objectVersion"] == "v2" assert obj["parent"]["url"] == f"https://example.org/authors/ocfl/{a_owner}/{nm}/v2/" assert obj["documentVersion"] == 1 and obj["parent"]["documentVersion"] == 2 and "previous" not in obj jbase = json.loads((j / P.CONFIG_NAME).read_text())["baseUrl"] assert P.read_json(d, inv, "datacite.json")["url"] == f"{jbase}paper/{nm}-{j_owner}/" held = {p for ps in inv["versions"]["v1"]["state"].values() for p in ps} assert {"genesis.json", "paper.pdf", "parent/inventory.json", "parent/object.json"} <= held assert any(p.startswith("parent/signatures/signature-") for p in held) assert P.read_logical(d, inv, "parent/inventory.json").read_bytes() == \ (P.obj_dir(line(a, "paper")) / "v2" / "inventory.json").read_bytes() P.configure(j) assert P.key_log(a_owner) is not None mem = members(j, "issue-1")["members"] assert [(m["owner"], [r["documentVersion"] for r in m["revisions"]]) for m in mem] == \ [(j_owner, [1]), (a_owner, [2])] found = findings(j) assert [x for x in found if x["severity"] == "error"] == [] assert any(x["message"].endswith(": confirmed (section 9.3)") for x in found) deposit(a, "paper", "three") P.configure(j) assert list(P.load_inventory(P.obj_dir(branch_id))["versions"]) == ["v1"] remark = tmp_path / "remark.md" remark.write_text("Accepted with a remark.") run("--root", str(j), "notice", "paper", "--about", "1", "--text", "A remark.", "--file", str(remark)) run("--root", str(j), "accept", uri(a, "paper", 3), "--selection", "issue-1") P.configure(j) inv = P.load_inventory(P.obj_dir(branch_id)) obj = P.read_json(P.obj_dir(branch_id), inv, P.OBJ) assert inv["head"] == "v3" and obj["documentVersion"] == 2 and obj["parent"]["objectVersion"] == "v3" assert obj["parent"]["documentVersion"] == 3 and obj["notices"][0]["text"] == "A remark." assert P.read_logical(P.obj_dir(branch_id), inv, obj["notices"][0]["path"]).read_text() == \ "Accepted with a remark." assert [r["documentVersion"] for r in members(j, "issue-1")["members"][0]["revisions"]] == [1, 2] assert errors(j) == [] with pytest.raises(SystemExit, match="the branch point of the branch already"): run("--root", str(j), "accept", uri(a, "paper", 3), "--selection", "issue-1") run("--root", str(a), "notice", "paper", "--about", "3", "--text", "A remark.") with pytest.raises(SystemExit, match="not a new branch point"): run("--root", str(j), "accept", uri(a, "paper"), "--selection", "issue-1")
A branch of a branch carries the evidence of its parent in parent/parent/
(section 9.2), and the key logs of every owner whose signatures the
evidence holds beside it (section 6.5). And when the owner of a branch
already has a line of the name, from another parent line, the new branch
is renamed by the next algorithm number (section 5.3); the verifier finds
the rename justified.
def test_branch_of_a_branch_and_a_clash(tmp_path): a, j, b = make(tmp_path, "authors"), make(tmp_path, "journal"), make(tmp_path, "b") deposit(a, "paper", "one", new=True) run("--root", str(j), "branch", uri(a, "paper", 1), "--label", "accepted") run("--root", str(b), "branch", uri(j, "accepted", 1), "--label", "continued") _, nm, a_owner = line(a, "paper").split(":") j_owner = line(j, "accepted").split(":")[2] d = P.obj_dir(line(b, "continued")) inv = P.load_inventory(d) held = {p for ps in inv["versions"]["v1"]["state"].values() for p in ps} assert "parent/parent/inventory.json" in held and "parent/parent/object.json" in held assert any(p.startswith("parent/parent/signatures/") for p in held) P.configure(b) assert P.key_log(a_owner) is not None and P.key_log(j_owner) is not None found = findings(b) assert [x for x in found if x["severity"] == "error"] == [] assert any(x["message"].endswith(": confirmed (section 9.3)") for x in found) run("--root", str(j), "branch", uri(b, "continued", 1), "--label", "again") renamed = line(j, "again").split(":")[1] assert renamed != nm and names.algorithm(renamed) == 1 assert names.is_name_of(renamed, P.read_logical(d, inv, P.GENESIS).read_bytes()) found = findings(j) assert [x for x in found if x["severity"] == "error"] == [] assert not any("renamed by algorithm" in x["message"] for x in found) with pytest.raises(SystemExit, match="neither the parent's owner nor the originator"): run("--root", str(a), "branch", uri(j, "accepted", 1), "--label", "back")
Each owner an archive hosts has its owner catalogue (section 8.1). A
second owner set up in the archive with osheaf owner init gets one at
once, empty, signed by its own key, and the discovery document names both
owners with their catalogues, in order, without mainCollection. A
deposit by the second owner and a branch it makes of the first owner's
paper (section 9) go into its catalogue only: osheaf collect leaves the
first owner's catalogue at the version it had, and --owner takes one
owner's catalogue alone. The second owner's catalogue has no label until
the owner gets one, jane: it is then jane-catalogue, and follows the
owner's label when it changes or goes, or is given again. The home page lists the documents of both
catalogues together, each naming its owner, or, with index.owners
by-owner, under a heading for each owner, in the order of owners
(appendix A.3), and the archive verifies. A discovery document giving one
owner's catalogue to another, or naming an owner twice, is an error; an
owner catalogue listing another owner's line is warned of (section 14.6);
a discovery document without owners is an error (section 3.1).
def test_owner_catalogues(tmp_path): a = make(tmp_path, "a") r = ["--root", str(a)] first = line(a, "main").split(":")[2] deposit(a, "paper", "one", new=True) run(*r, "collect") run(*r, "owner", "init", "--export", str(tmp_path / "second-keys"), "--no-passphrase", "--yes") P.configure(a) second = P.ARCHIVE["owners"][0] disc = json.loads((a / "opensheaf.json").read_text()) assert [e["owner"] for e in disc["owners"]] == [first, second] and "mainCollection" not in disc cat1, cat2 = (e["catalogue"] for e in disc["owners"]) assert cat1 == line(a, "main") and cat2.split(":")[2] == second and P.label_of(cat2) is None run(*r, "label", "--owner", second, "jane") P.configure(a) assert P.label_of(cat2) == "jane-catalogue" assert cat2 not in (P.ARCHIVE.get("formerAddresses") or {}).values() # it had no page before run(*r, "label", "--owner", second, "jo") P.configure(a) assert P.label_of(cat2) == "jo-catalogue" and P.label_of(cat1) == "main" run(*r, "label", "--owner", second, "--unset") P.configure(a) assert P.label_of(cat2) is None run(*r, "label", "--owner", second, "jo") P.configure(a) assert P.label_of(cat2) == "jo-catalogue" run(*r, "label", "jo-catalogue", "--unset") run(*r, "label", "--owner", second, "jo") P.configure(a) assert P.label_of(cat2) == "jo-catalogue" def listed(cat): d = P.obj_dir(cat) return [(m["name"], m["owner"]) for m in P.read_json(d, P.load_inventory(d), "members.json")["members"]] def head(cat): return P.load_inventory(P.obj_dir(cat))["head"] assert listed(cat2) == [] and head(cat2) == "v1" head1 = head(cat1) deposit(a, "theirs", "two", "--owner", second, new=True) run(*r, "branch", uri(a, "paper", 1), "--owner", second, "--label", "continued") run(*r, "collect", "--owner", second) P.configure(a) assert head(cat1) == head1 and head(cat2) == "v2" run(*r, "collect") P.configure(a) assert head(cat1) == head1 assert listed(cat1) == [tuple(line(a, "paper").split(":")[1:])] assert sorted(listed(cat2)) == sorted(tuple(line(a, x).split(":")[1:]) for x in ("theirs", "continued")) assert errors(a) == [] home = (a / "index.html").read_text() pins = [head_digest(a, x, "v1")[:32] for x in ("paper", "theirs")] assert all(p in home for p in pins) and 'class="owner-heading"' not in home assert f'<a class="owner" href="owner/{second}/">jo</a>' in home assert f'<a class="owner" href="owner/{first}/">' in home cfg = json.loads((a / "osheaf.json").read_text()) cfg["index"]["owners"] = "by-owner" (a / "osheaf.json").write_text(json.dumps(cfg, indent=2) + "\n") run(*r, "index") home = (a / "index.html").read_text() headings = [i for i in range(len(home)) if home.startswith('<h2 class="owner-heading">', i)] assert len(headings) == 2 and headings[0] < home.index(pins[0]) < headings[1] < home.index(pins[1]) assert '>jo</a></h2>' in home and '<a class="owner" href="owner/' in home[headings[1]:] cfg["index"]["owners"] = "mixed" (a / "osheaf.json").write_text(json.dumps(cfg, indent=2) + "\n") with pytest.raises(SystemExit, match="together or by-owner"): run(*r, "index") cfg["index"]["owners"] = "together" (a / "osheaf.json").write_text(json.dumps(cfg, indent=2) + "\n") with pytest.raises(SystemExit, match="not an owner this archive hosts"): run(*r, "collect", "--owner", "a" * 51 + "q") disc = json.loads((a / "opensheaf.json").read_text()) wrong = dict(disc, owners=[disc["owners"][0], {"owner": second, "catalogue": cat1}, disc["owners"][0]]) (a / "opensheaf.json").write_text(json.dumps(wrong)) messages = [x["message"] for x in findings(a) if x["severity"] == "error"] assert any(f"the catalogue of {second} in owners" in m for m in messages) assert any(f"owners lists {first} twice" in m for m in messages) (a / "opensheaf.json").write_text(json.dumps(dict(disc, owners=disc["owners"][::-1]))) assert errors(a) == [] d = P.obj_dir(cat2) mem = P.read_json(d, P.load_inventory(d), "members.json") theirs = next(m for m in mem["members"] if m["name"] == line(a, "theirs").split(":")[1]) mine = dict(theirs, name=line(a, "paper").split(":")[1], owner=first, version="v1", inventorySha512=head_digest(a, "paper", "v1")) from opensheaf import collections collections.write(cat2, dict(mem, order="manual", members=mem["members"] + [mine]), message="Another's") found = findings(a) assert any(x["severity"] == "warning" and f"the owner catalogue of {second} lists" in x["message"] for x in found) (a / "opensheaf.json").write_text(json.dumps({k: v for k, v in disc.items() if k != "owners"})) assert any(x["severity"] == "error" and "no owners" in x["message"] for x in findings(a))
A branch point whose evidence is tampered with is inconsistent, an error.
def test_inconsistent_branch_point(tmp_path): a, j = make(tmp_path, "authors"), make(tmp_path, "journal") deposit(a, "paper", "one", new=True) run("--root", str(j), "branch", uri(a, "paper", 1), "--label", "accepted") d = P.obj_dir(line(j, "accepted")) inv = P.load_inventory(d) from opensheaf import branch level = branch.level_of_version(d, inv, "v1") obj, genesis = P.read_json(d, inv, P.OBJ), P.read_logical(d, inv, P.GENESIS).read_bytes() assert branch.verdict(level, obj, genesis, P.key_log)[0] == branch.CONFIRMED unsigned = {p: level.read(p) for p in level.digests if not p.startswith("parent/signatures/")} v, problems = branch.verdict(branch.level_of_files(unsigned), obj, genesis, P.key_log) assert v == branch.INCONSISTENT and any("not a version: unsigned" in p for p in problems) changed = {p: level.read(p) for p in level.digests} changed["paper.pdf"] = b"%PDF-1.4 another" v, problems = branch.verdict(branch.level_of_files(changed), obj, genesis, P.key_log) assert v == branch.INCONSISTENT and any(p.startswith("paper.pdf") for p in problems) assert branch.verdict(level, obj, genesis, lambda o: None)[0] == branch.UNCHECKED
A branch numbers its own document versions from 1 (section 9.4), and holds none of the parent's earlier versions: its history, once written, lists its document versions from 1. Here a branch of the authors' version 2 is the branch's version 1; it gets version 2, then a version found later placed after 1, which writes the history; the archive verifies.
def test_the_history_of_a_branch(tmp_path): a, j = make(tmp_path, "authors"), make(tmp_path, "journal") deposit(a, "paper", "one", new=True) deposit(a, "paper", "two") run("--root", str(j), "branch", uri(a, "paper", 2), "--label", "continued") deposit(j, "continued", "three") deposit(j, "continued", "earlier", "--after", "1") d = P.obj_dir(line(j, "continued")) inv = P.load_inventory(d) hist = P.read_json(d, inv, P.OBJ)["history"] assert [P.doc_version_of(d, inv, v) for v in hist] == [1, 3, 2] assert P.document_version_errors(line(j, "continued"), d, inv) == [] assert errors(j) == []
The parent of a branch is found from parent.url, the URL of the parent
version (section 9.1), in the archive that holds it, whose base URL the
resolver derives from the discovery documents it finds above that URL
(section 3.1): here a copy of an archive whose storage root is objects/,
not ocfl/. The answer is kept; the storage root itself, or a URL in no
archive, has none.
def test_the_archive_of_a_parent_url(tmp_path): a = make(tmp_path, "authors") deposit(a, "paper", "one", new=True) obj_id = line(a, "paper") s = tmp_path / "store" shutil.copytree(a, s) (s / "ocfl").rename(s / "objects") disc = json.loads((s / "opensheaf.json").read_text()) disc["storageRoot"] = "objects/" (s / "opensheaf.json").write_text(json.dumps(disc)) at = P.path_of(obj_id) url = f"https://example.org/store/objects/{at}/v1/" with remote.Resolver(here=False) as r: assert r.archive_of(url) == ["https://example.org/store/"] assert r.archive_of(url) is r.archive_of(url) found = r.latest(*obj_id.split(":")[1:], r.archive_of(url)) assert found.status == "verified" and found.copy.where == "https://example.org/store/" assert r.archive_of(f"https://example.org/authors/ocfl/{at}/v1/") == ["https://example.org/authors/"] assert r.archive_of("https://example.org/store/objects/") == [] assert r.archive_of(f"https://example.org/nowhere/ocfl/{at}/v1/") == [] assert not [n for n in r.notes if "unavailable" in n[1]]
Drafts
A draft (section 10.1) is deposited into like a document, unsigned, at
draft/<id>/, listed apart in the discovery document; its head may be
rewritten. Sealing it makes a new line, signed, whose first version holds
the draft's files as document version 1 (section 10.2). The draft is then
deleted, and leaves no tombstone.
def test_draft_and_seal(tmp_path): a = make(tmp_path, "a") pdf = tmp_path / "d.pdf" pdf.write_bytes(b"%PDF-1.4 draft one") base = ["--pdf", str(pdf), "--no-source", "x", "--title", "Notes", "--author", "Doe, J"] run("--root", str(a), "draft", "new", "notes", *base) pdf.write_bytes(b"%PDF-1.4 draft two") run("--root", str(a), "draft", "deposit", "notes", *base) pdf.write_bytes(b"%PDF-1.4 draft two, corrected") run("--root", str(a), "draft", "deposit", "notes", *base, "--replace") d = P.OCFL / "draft" / "notes" inv = P.load_inventory(d) assert inv["head"] == "v2" and not (d / sign.WITNESSES).exists() obj = P.read_json(d, inv, P.OBJ) assert "name" not in obj and "owner" not in obj and "previous" not in obj disc = json.loads((a / "opensheaf.json").read_text()) assert disc["drafts"] == [{"id": "sheaf:draft:notes", "kind": "document", "head": "v2"}] assert all(not e["id"].startswith("sheaf:draft") for e in P.read_index("sha512")) assert errors(a) == [] run("--root", str(a), "seal", "notes", "--label", "lecture-notes") sealed = P.obj_dir(line(a, "lecture-notes")) sinv = P.load_inventory(sealed) sobj = P.read_json(sealed, sinv, P.OBJ) assert sobj["documentVersion"] == 1 and sobj["owner"] == P.ARCHIVE["owner"] assert P.read_logical(sealed, sinv, "paper.pdf").read_bytes() == b"%PDF-1.4 draft two, corrected" assert P.line_statuses(sealed, sinv)[1] == [sign.SIGNED] run("--root", str(a), "draft", "delete", "notes") disc = json.loads((a / "opensheaf.json").read_text()) assert "drafts" not in disc and "tombstones" not in disc run("--root", str(a), "collect") assert errors(a) == []
Takedowns and indexes
A takedown deletes the object, with its witness folder, and writes a
tombstone with the fields of section 13.3, which the schema of the
discovery document accepts; the indexes keep the line's entries with the
status removed, and owners/ drops its name. The key log of the owner
stays, since the archive's other lines need it, and taking it down is
refused but by law or at the keeper's request. The library's mirror is
not reached: brought up to date, it reports the tombstone, and stays
(section 13.3). A line taken down returns
to a library from a copy holding the version its tombstone pins, and its
tombstone is dropped (section 13.2).
def test_takedown_and_return(tmp_path, capsys): a, lib = make(tmp_path, "a"), make(tmp_path, "lib") deposit(a, "paper", "one", new=True) deposit(a, "other", "two", new=True) obj_id = line(a, "paper") _, nm, owner = obj_id.split(":") run("--root", str(lib), "mirror", uri(a, "paper")) run("--root", str(a), "takedown", "paper", "--reason", "At the request of the holder of the rights.", "--yes") P.configure(a) assert not P.obj_dir(obj_id).exists() and P.key_log(owner) is not None tomb = json.loads((a / "opensheaf.json").read_text())["tombstones"][0] assert set(tomb) == {"id", "kind", "name", "owner", "genesisName", "lastVersion", "inventorySha512", "date", "reason"} and tomb["genesisName"] == nm assert [e["status"] for e in P.read_index("names") if e["id"] == obj_id] == ["removed"] assert all(nm not in e["names"] for e in P.read_index("owners")) found = findings(a) assert [x for x in found if x["severity"] == "error"] == [] assert any(x["message"].startswith(f"removed: {obj_id}") for x in found) with pytest.raises(SystemExit, match="by law or at its keeper's request"): run("--root", str(a), "takedown", f"sheaf:{owner}:{owner}", "--reason", "x", "--yes") capsys.readouterr() run("--root", str(lib), "mirror", "--all") assert f"{obj_id} was taken down at" in capsys.readouterr().err P.configure(lib) assert P.obj_dir(obj_id).exists() pin = P.digest(P.obj_dir(obj_id) / "v1" / "inventory.json") run("--root", str(lib), "takedown", obj_id, "--reason", "Stopped keeping it.", "--yes") assert json.loads((lib / "opensheaf.json").read_text())["tombstones"][0]["inventorySha512"] == pin assert errors(lib) == [] run("--root", str(lib), "takedown", f"sheaf:{owner}:{owner}", "--reason", "A court order.", "--by-law", "--yes") found = findings(lib) assert [x for x in found if x["severity"] == "error"] == []
The return of a line: a library that took a mirror down mirrors it again from a copy holding the version its tombstone pins.
def test_line_returns(tmp_path): a, lib = make(tmp_path, "a"), make(tmp_path, "lib") deposit(a, "paper", "one", new=True) obj_id = line(a, "paper") run("--root", str(lib), "mirror", uri(a, "paper")) run("--root", str(lib), "takedown", obj_id, "--reason", "Stopped keeping it.", "--yes") P.configure(lib) assert P.TOMBSTONES and not P.obj_dir(obj_id).exists() run("--root", str(lib), "mirror", uri(a, "paper")) P.configure(lib) assert not P.TOMBSTONES and P.obj_dir(obj_id).exists() assert errors(lib) == []
An archive with more objects than a page holds lists them in pages (section 3.1): here a page holds two objects. The pages pass verification, the labels are read from them, and another archive resolves a line through them; a page missing is an error, and makes the archive unavailable to resolvers. With a page missing, the labels are not known: a command that writes stops before it writes anything (#19), and the discovery document and the landing pages stay as they were. Back under the threshold, the pages go.
def test_object_pages(tmp_path, monkeypatch): monkeypatch.setattr(P, "OBJECTS_PER_PAGE", 2) a = make(tmp_path, "a") for label in ("one", "two", "three"): deposit(a, label, label, new=True) disc = json.loads((a / "opensheaf.json").read_text()) assert "objects" not in disc and disc["objectPages"] pages = [json.loads((a / p).read_text()) for p in disc["objectPages"]] assert all(p["format"] == "opensheaf-objects/0.2" and len(p["objects"]) <= 2 for p in pages) listed = [o["id"] for p in pages for o in p["objects"]] assert len(listed) == len(set(listed)) == len(P.discovery()["objects"]) assert errors(a) == [] P.configure(a) assert P.LABELS["two"] == line(a, "two") line_id_two = line(a, "two") _, nm, owner = line_id_two.split(":") with remote.Resolver(here=False) as r: assert r.pin(nm, owner, "v1", head_digest(a, "two", "v1"), [str(a)]).status == "verified" first = a / disc["objectPages"][0] saved = first.read_bytes() first.unlink() assert any("is missing (section 3.1)" in e for e in errors(a)) with remote.Resolver(here=False) as r: assert r.source(str(a)) is None before = (a / "opensheaf.json").read_bytes() assert (a / "paper" / "two" / "index.html").exists() for argv in (["index"], ["deposit", "two", "--pdf", str(a.parent / "two.pdf"), "--no-source", "x", "--title", "Two", "--author", "Doe, J", "--issued", "2020-01-01"]): with pytest.raises(SystemExit, match="the labels of the lines are not known"): run("--root", str(a), *argv) P.configure(a) with pytest.raises(SystemExit, match="the labels of the lines are not known"): P.write_discovery() assert (a / "opensheaf.json").read_bytes() == before and (a / "paper" / "two" / "index.html").exists() assert not (P.obj_dir(line_id_two) / "v2").exists() first.write_bytes(saved) monkeypatch.setattr(P, "OBJECTS_PER_PAGE", 1000) P.configure(a) P.write_discovery() disc = json.loads((a / "opensheaf.json").read_text()) assert "objectPages" not in disc and not (a / "objects").exists() assert errors(a) == []
The indexes (section 12.5): names/ lists every object with a genesis
record, with its genesis text; owners/ the lines of each owner; sha512/
every version. A tampered index is reported by the verifier.
def test_indexes(tmp_path): a = make(tmp_path, "a") deposit(a, "paper", "one", new=True) deposit(a, "paper", "two") run("--root", str(a), "collect") run("--root", str(a), "index") P.configure(a) disc = json.loads((a / "opensheaf.json").read_text()) assert disc["indexes"] == {"names": 2, "owners": 2, "sha512": 2} obj_id = line(a, "paper") _, nm, owner = obj_id.split(":") entry = json.loads((a / "names" / f"{nm[:2]}.json").read_text()) e = next(x for x in entry["entries"] if x["id"] == obj_id) d = P.obj_dir(obj_id) assert e["genesis"].encode() == P.read_logical(d, P.load_inventory(d), P.GENESIS).read_bytes() assert e["originator"] == owner and e["label"] == "paper" and e["status"] == "active" owners_ = json.loads((a / "owners" / f"{owner[:2]}.json").read_text())["entries"] assert set(next(x for x in owners_ if x["owner"] == owner)["names"]) >= {nm, owner} shas = [x for x in P.read_index("sha512") if x["id"] == obj_id] assert sorted(x["version"] for x in shas) == ["v1", "v2"] assert errors(a) == [] target = a / "sha512" / f"{shas[0]['inventorySha512'][:2]}.json" target.unlink() assert any("no entry, or a stale one" in e for e in errors(a))