660 lines
30 KiB
Text
660 lines
30 KiB
Text
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;; bjomarks/tests/store.bjo — (bjomarks store): vaults in folders of the
|
|
;; temp directory, with two or three devices that sync through one shared
|
|
;; folder, and files that were changed, replaced or renamed.
|
|
;;
|
|
;; bjo run tests/store.bjo
|
|
;;
|
|
;; The tests use a cost of 1 iteration over 8 KiB, so that they are quick.
|
|
;; `at` sets the clock, so that the order of changes is known.
|
|
|
|
(import (std prelude))
|
|
(import (std test))
|
|
(import (std os))
|
|
(import (std datetime))
|
|
(import (std uuid))
|
|
(import (text json))
|
|
(import (bjosodium core))
|
|
(import (bjomarks binary))
|
|
(import (bjomarks bookmark))
|
|
(import (bjomarks keys))
|
|
(import (bjomarks store))
|
|
|
|
(def cheap (KeyCost (iterations 1) (memory-kib 8)))
|
|
|
|
(: (-> string Instant))
|
|
(defun (instant text)
|
|
(match (iso8601->instant text)
|
|
((Some i) i)
|
|
(None (panic! (str "not an instant: " text)))))
|
|
|
|
;; Calls `f` with the clock at `text`.
|
|
(: (-> string (-> %a) %a))
|
|
(defun (at text f)
|
|
(parameterize ((current-clock (fixed-clock (instant text) utc))) (f)))
|
|
|
|
;; Three folders: the shared one and one for each of two devices.
|
|
(type (: Folders (Record (: shared string) (: one string) (: two string) (: root string))))
|
|
|
|
(: (-> Folders))
|
|
(defun (folders)
|
|
(def root (make-temp-directory #:prefix "bjomarks-test-"))
|
|
(Folders (shared (path-combine root "shared")) (one (path-combine root "one"))
|
|
(two (path-combine root "two")) (root root)))
|
|
|
|
(: (-> StoreError string))
|
|
(defun (store-error e)
|
|
(match e
|
|
(WrongPassword "WrongPassword")
|
|
((NoVault why) (str "NoVault: " why))
|
|
((VaultExists why) (str "VaultExists: " why))
|
|
((OtherVault why) (str "OtherVault: " why))
|
|
((BadFile why) (str "BadFile: " why))))
|
|
|
|
(: (-> (Result StoreError Vault) Vault))
|
|
(defun (must r)
|
|
(match r
|
|
((Ok v) v)
|
|
((Err e) (panic! (store-error e)))))
|
|
|
|
(: (-> (Result StoreError (Tuple Vault SyncReport)) Vault))
|
|
(defun (must-join r)
|
|
(match r
|
|
((Ok (Tuple v _)) v)
|
|
((Err e) (panic! (store-error e)))))
|
|
|
|
(: (-> Folders string Vault))
|
|
(defun (create f local) (must (create-vault (folder-storage (.shared f)) local "hunter2" #:cost cheap)))
|
|
|
|
(: (-> Folders string Vault))
|
|
(defun (join f local) (must-join (join-vault (folder-storage (.shared f)) local "hunter2")))
|
|
|
|
(: (-> Folders string string (Result StoreError Vault)))
|
|
(defun (reopen f local password) (open-vault (folder-storage (.shared f)) local password))
|
|
|
|
;; A new bookmark, at the clock's time.
|
|
(: (-> Vault string (Vec string) (Tuple Vault Bookmark)))
|
|
(defun (add-bookmark v url tags)
|
|
(match (vault-add v url #:tags tags)
|
|
((Ok r) r)
|
|
((Err why) (panic! why))))
|
|
|
|
(: (-> Vault (Tuple Vault SyncReport)))
|
|
(defun (sync v) (vault-sync v))
|
|
|
|
(: (-> Vault Vault))
|
|
(defun (synced v) (fst (vault-sync v)))
|
|
|
|
(: (-> Vault (Vec string)))
|
|
(defun (urls v) (vec-map #(.url &) (vault-bookmarks v)))
|
|
|
|
(: (-> Vault Uuid (Vec string)))
|
|
(defun (tags-of v id)
|
|
(match (vault-find v id)
|
|
((Some b) (.tags b))
|
|
(None ["<deleted>"])))
|
|
|
|
(: (-> Folders Vault string))
|
|
(defun (file-of f v) (path-combine (.shared f) "devices" (device-file-name (vault-device v))))
|
|
|
|
;; ---- One device -----------------------------------------------------------------
|
|
|
|
(deftest "a new vault keeps its bookmarks"
|
|
(def f (folders))
|
|
(def v0 (create f (.one f)))
|
|
(check (file-exists? (path-combine (.shared f) "keys.bin")))
|
|
(check (file-exists? (file-of f v0)))
|
|
(check (= (vault-revision v0) 1L))
|
|
(def (Tuple v1 b1) (at "2026-11-11T09:00:00Z" #(add-bookmark v0 "https://example.org/a" ["x"])))
|
|
(def (Tuple v2 _b2) (at "2026-11-11T10:00:00Z" #(add-bookmark v1 "https://example.org/b" [])))
|
|
(check (vault-dirty? v2))
|
|
(def v3 (vault-save v2))
|
|
(check (not (vault-dirty? v3)))
|
|
(check (= (vault-revision v3) 2L))
|
|
(check (= (vault-revision (vault-save v3)) 2L))
|
|
(vault-close v3)
|
|
(def again (must (reopen f (.one f) "hunter2")))
|
|
(check (= (urls again) ["https://example.org/b" "https://example.org/a"]))
|
|
(check (= (tags-of again (.id b1)) ["x"]))
|
|
(check (= (vault-id again) (vault-id v0)))
|
|
(check (= (vault-device again) (vault-device v0)))
|
|
(check (= (vault-revision again) 2L))
|
|
(check (not (vault-dirty? again)))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a wrong password, and a device with no vault"
|
|
(def f (folders))
|
|
(ignore (create f (.one f)))
|
|
(check (= (match (reopen f (.one f) "hunter3") ((Ok _) "Ok") ((Err e) (store-error e))) "WrongPassword"))
|
|
(check-match (reopen f (.two f) "hunter2") (Err (NoVault _)))
|
|
(check-match (join-vault (folder-storage (.two f)) (.two f) "hunter2") (Err (NoVault _)))
|
|
(check (= (match (join-vault (folder-storage (.shared f)) (path-combine (.root f) "three") "nope")
|
|
((Ok _) "Ok") ((Err e) (store-error e)))
|
|
"WrongPassword"))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a vault is not made twice"
|
|
(def f (folders))
|
|
(ignore (create f (.one f)))
|
|
(check-match (create-vault (folder-storage (.shared f)) (.two f) "pw" #:cost cheap) (Err (VaultExists _)))
|
|
(check-match (create-vault (folder-storage (path-combine (.root f) "elsewhere")) (.one f) "pw" #:cost cheap)
|
|
(Err (VaultExists _)))
|
|
(check-match (join-vault (folder-storage (.shared f)) (.one f) "hunter2") (Err (VaultExists _)))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "changes are deleted and put"
|
|
(def f (folders))
|
|
(def v0 (create f (.one f)))
|
|
(def (Tuple v1 b) (add-bookmark v0 "https://example.org/a" ["x"]))
|
|
(def v2 (vault-put v1 (bookmark-with-tags b ["y" " z" "y"])))
|
|
(check (= (tags-of v2 (.id b)) ["y" "z"]))
|
|
(def v3 (vault-delete v2 (.id b)))
|
|
(check (= (urls v3) []))
|
|
(check (= (vault-find v3 (.id b)) None))
|
|
;; The tombstone stays, so that the delete reaches the other devices.
|
|
(check (= (map-length (vault-entries v3)) 1))
|
|
(check (= (vault-delete v3 (.id b)) v3))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a delete is undone, and the other devices see it"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Tuple a1 b) (at "2026-11-11T09:00:00Z" #(add-bookmark a0 "https://example.org/" ["x"])))
|
|
(def before (match (map-try-ref (vault-entries a1) (uuid->string (.id b)))
|
|
((Some e) e)
|
|
(None (panic! "no entry"))))
|
|
(def a2 (synced (at "2026-11-11T09:01:00Z" #(vault-delete a1 (.id b)))))
|
|
(def b0 (join f (.two f)))
|
|
(check (= (urls b0) []))
|
|
(def a3 (synced (at "2026-11-11T09:02:00Z" #(vault-restore a2 before))))
|
|
(check (= (urls a3) ["https://example.org/"]))
|
|
(check (= (tags-of a3 (.id b)) ["x"]))
|
|
;; Bound first: `(.stamp (match ...))` makes C# that does not compile.
|
|
(def restored (match (map-try-ref (vault-entries a3) (uuid->string (.id b))) ((Some e) e) (None before)))
|
|
(check (> (stamp-compare (.stamp restored) (.stamp before)) 0))
|
|
(def b1 (synced b0))
|
|
(check (= (urls b1) ["https://example.org/"]))
|
|
(check (= (vault-restore a3 (deleted-entry (.stamp before) (.id b))) a3))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "the files are padded"
|
|
(def f (folders))
|
|
(def v0 (create f (.one f)))
|
|
(def size (array-length (file-read-bytes (file-of f v0))))
|
|
;; The header, the nonce and the tag are 51 + 2 * 32 bytes for AEGIS-256,
|
|
;; and 51 + 24 + 16 for XChaCha20-Poly1305.
|
|
(def overhead (+ 51 (cipher-nonce-length (preferred-cipher)) (cipher-tag-length (preferred-cipher))))
|
|
(check (= (- size overhead) 16384))
|
|
(def more (vec-fold (fun (i v) (fst (add-bookmark v (str "https://example.org/" (int->string i)) ["tag"])))
|
|
v0 (->vec (range 0 20))))
|
|
(def saved (vault-save more))
|
|
(check (= (- (array-length (file-read-bytes (file-of f saved))) overhead) 16384))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
;; ---- Two devices -------------------------------------------------------------------
|
|
|
|
(deftest "two devices see each other's changes"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Tuple a1 b1) (at "2026-11-11T09:00:00Z" #(add-bookmark a0 "https://example.org/one" ["x"])))
|
|
(def a2 (vault-save a1))
|
|
(def b0 (join f (.two f)))
|
|
(check (= (urls b0) ["https://example.org/one"]))
|
|
(check (not (= (vault-device b0) (vault-device a2))))
|
|
(def (Tuple b1x b2) (at "2026-11-11T10:00:00Z" #(add-bookmark b0 "https://example.org/two" [])))
|
|
(def b2x (at "2026-11-11T10:01:00Z" #(vault-delete b1x (.id b1))))
|
|
(def (Tuple b3 report-b) (sync b2x))
|
|
(check (.wrote report-b))
|
|
(def (Tuple a3 report-a) (sync a2))
|
|
(check (= (.files-merged report-a) 2))
|
|
(check (= (.entries-changed report-a) 2))
|
|
(check (= (.problems report-a) []))
|
|
(check (= (urls a3) ["https://example.org/two"]))
|
|
(def a4 (at "2026-11-11T11:00:00Z" #(vault-put a3 (bookmark-with-tags b2 ["new"]))))
|
|
(def a5 (synced a4))
|
|
(def b4 (synced b3))
|
|
(check (= (tags-of b4 (.id b2)) ["new"]))
|
|
;; Nothing new: nothing is written.
|
|
(def (Tuple _b5 again) (sync b4))
|
|
(check (= (.entries-changed again) 0))
|
|
(check (not (.wrote again)))
|
|
(check (= (vault-revision a5) (vault-revision (synced a5))))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a title reaches the other devices, and a later title wins"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Ok (Tuple a1 b)) (vault-add a0 "https://example.org/" #:title (Some " Example\nDomain "))
|
|
:leave-with (check-fail "vault-add refused a good address"))
|
|
(check (= (.title b) (Some "Example Domain")))
|
|
(def a2 (vault-save a1))
|
|
(def b0 (join f (.two f)))
|
|
(check (= (vec-map #(.title &) (vault-bookmarks b0)) [(Some "Example Domain")]))
|
|
(def b1 (synced (vault-put b0 (bookmark-with-title b (Some "Fetched")))))
|
|
(def a3 (synced a2))
|
|
(check (= (vec-map #(.title &) (vault-bookmarks a3)) [(Some "Fetched")]))
|
|
(check (= (vec-map #(.title &) (vault-bookmarks b1)) [(Some "Fetched")]))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "the later change wins, whichever device syncs first"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Tuple a1 b) (at "2026-11-11T09:00:00Z" #(add-bookmark a0 "https://example.org/" [])))
|
|
(def a2 (synced a1))
|
|
(def b0 (join f (.two f)))
|
|
;; Both change the bookmark before either syncs. The phone's change is later.
|
|
(def a3 (at "2026-11-11T12:00:00Z" #(vault-put a2 (bookmark-with-tags b ["laptop"]))))
|
|
(def b1 (at "2026-11-11T12:00:05Z" #(vault-put b0 (bookmark-with-tags b ["phone"]))))
|
|
(def a4 (synced a3))
|
|
(def b2 (synced b1))
|
|
(def a5 (synced a4))
|
|
(check (= (tags-of a5 (.id b)) ["phone"]))
|
|
(check (= (tags-of b2 (.id b)) ["phone"]))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a delete wins over an older change, and a later change brings it back"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Tuple a1 b) (at "2026-11-11T09:00:00Z" #(add-bookmark a0 "https://example.org/" [])))
|
|
(def a2 (synced a1))
|
|
(def b0 (join f (.two f)))
|
|
(def a3 (at "2026-11-11T12:00:00Z" #(vault-put a2 (bookmark-with-tags b ["edited"]))))
|
|
(def b1 (at "2026-11-11T12:00:05Z" #(vault-delete b0 (.id b))))
|
|
(def a4 (synced (synced a3)))
|
|
(def b2 (synced b1))
|
|
(def a5 (synced a4))
|
|
(check (= (urls a5) []))
|
|
(check (= (urls b2) []))
|
|
(def a6 (at "2026-11-11T13:00:00Z" #(vault-put a5 (bookmark-with-tags b ["back"]))))
|
|
(def a7 (synced a6))
|
|
(def b3 (synced b2))
|
|
(check (= (tags-of b3 (.id b)) ["back"]))
|
|
(check (= (tags-of a7 (.id b)) ["back"]))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "the clock does not go back when the wall clock does"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Tuple a1 b) (at "2026-11-11T12:00:00Z" #(add-bookmark a0 "https://example.org/" [])))
|
|
(def a2 (synced a1))
|
|
(def b0 (join f (.two f)))
|
|
;; This device's clock is an hour behind. Its change is still the later one.
|
|
(def b1 (at "2026-11-11T11:00:00Z" #(vault-put b0 (bookmark-with-tags b ["after"]))))
|
|
(def b2 (synced b1))
|
|
(def a3 (synced a2))
|
|
(check (= (tags-of a3 (.id b)) ["after"]))
|
|
(check (= (tags-of b2 (.id b)) ["after"]))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a sync in the background, and the changes made while it ran"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def b0 (join f (.two f)))
|
|
(def (Tuple b1 _) (at "2026-11-11T08:00:00Z" #(add-bookmark b0 "https://example.org/from-b" [])))
|
|
(def _b2 (synced b1))
|
|
(def (Tuple a1 first) (at "2026-11-11T09:00:00Z" #(add-bookmark a0 "https://example.org/first" [])))
|
|
;; The sync runs on a1; meanwhile the vault becomes a2.
|
|
(def (Tuple done _) (sync a1))
|
|
(def (Tuple a2 _) (at "2026-11-11T09:00:01Z" #(add-bookmark a1 "https://example.org/meanwhile" [])))
|
|
(def a3 (at "2026-11-11T09:00:02Z" #(vault-put a2 (bookmark-with-tags first ["edited"]))))
|
|
(def merged (vault-merge a3 done))
|
|
(check (= (urls merged) ["https://example.org/meanwhile" "https://example.org/first"
|
|
"https://example.org/from-b"]))
|
|
(check (= (tags-of merged (.id first)) ["edited"]))
|
|
(check (vault-dirty? merged))
|
|
(check (= (vault-revision merged) (vault-revision done)))
|
|
;; The next sync writes the changes made while the first ran.
|
|
(def (Tuple again report) (sync merged))
|
|
(check (.wrote report))
|
|
(check (= (vault-revision again) (+ (vault-revision done) 1L)))
|
|
(check (not (vault-dirty? (vault-merge again again))))
|
|
;; Nothing changed while a sync ran: nothing more to write.
|
|
(def (Tuple done2 _) (sync again))
|
|
(check (not (vault-dirty? (vault-merge again done2))))
|
|
(def c0 (join f (path-combine (.root f) "three")))
|
|
(check (= (tags-of c0 (.id first)) ["edited"]))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
;; ---- Files that are not right ------------------------------------------------------
|
|
|
|
(: (-> SyncReport string))
|
|
(defun (problems-of r) (string-join (.problems r) " | "))
|
|
|
|
(deftest "a changed file is skipped and reported"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def b0 (join f (.two f)))
|
|
(def (Tuple b1 _) (add-bookmark b0 "https://example.org/secret" []))
|
|
(def b2 (synced b1))
|
|
(def path (file-of f b2))
|
|
(def bytes (file-read-bytes path))
|
|
(put-u8! bytes 200 (bitwise-xor (get-u8 bytes 200) 1))
|
|
(file-write-bytes path bytes)
|
|
(def (Tuple a1 report) (sync a0))
|
|
(check (= (urls a1) []))
|
|
(check (= (.files-merged report) 1))
|
|
(check (string-contains? (problems-of report) "it does not decrypt"))
|
|
(check (string-contains? (problems-of report) (device-file-name (vault-device b2))))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "an older copy of a file is reported, and does not undo anything"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def b0 (join f (.two f)))
|
|
(def path (file-of f b0))
|
|
(def old (file-read-bytes path))
|
|
(def (Tuple b1 _) (at "2026-11-11T09:00:00Z" #(add-bookmark b0 "https://example.org/kept" [])))
|
|
(def _b2 (synced b1))
|
|
(def a1 (synced a0))
|
|
(check (= (urls a1) ["https://example.org/kept"]))
|
|
(file-write-bytes path old)
|
|
(def (Tuple a2 report) (sync a1))
|
|
(check (string-contains? (problems-of report) "revision 1 is older than revision 2"))
|
|
(check (= (urls a2) ["https://example.org/kept"]))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a file of another vault, and a file under another name"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def b0 (join f (.two f)))
|
|
(def elsewhere (path-combine (.root f) "elsewhere"))
|
|
(def other (must (create-vault (folder-storage elsewhere) (path-combine (.root f) "three") "pw" #:cost cheap)))
|
|
(def devices (path-combine (.shared f) "devices"))
|
|
(file-copy (path-combine elsewhere "devices" (device-file-name (vault-device other)))
|
|
(path-combine devices (device-file-name (vault-device other))))
|
|
(file-copy (file-of f b0) (path-combine devices (device-file-name (uuid-v4))))
|
|
(file-copy (file-of f b0) (path-combine devices "laptop (conflicted copy).bin"))
|
|
(file-write-text (path-combine devices "notes.txt") "not a device file")
|
|
(def (Tuple _a1 report) (sync a0))
|
|
(def text (problems-of report))
|
|
(check (= (vec-length (.problems report)) 3))
|
|
(check (string-contains? text "it belongs to another vault"))
|
|
(check (string-contains? text (str "it holds the file of the device " (uuid->string (vault-device b0)))))
|
|
(check (string-contains? text "laptop (conflicted copy).bin: the name is not <device uuid>.bin"))
|
|
(check (= (.files-merged report) 2))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a file from a newer version is reported"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def b0 (join f (.two f)))
|
|
(def path (file-of f b0))
|
|
(def bytes (file-read-bytes path))
|
|
(put-u16! bytes 4 2)
|
|
(file-write-bytes path bytes)
|
|
(def (Tuple _a1 report) (sync a0))
|
|
(check (string-contains? (problems-of report) "device file version 2 is newer than this version reads (1)"))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
;; ---- Losing files ------------------------------------------------------------------
|
|
|
|
(deftest "a device's file that the storage lost is written again"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Tuple a1 _) (add-bookmark a0 "https://example.org/" []))
|
|
(def a2 (synced a1))
|
|
(file-delete (file-of f a2))
|
|
(def (Tuple a3 report) (sync a2))
|
|
(check (.wrote report))
|
|
(check (file-exists? (file-of f a3)))
|
|
(def b0 (join f (.two f)))
|
|
(check (= (urls b0) ["https://example.org/"]))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a vault opens from the storage when state.bin is gone, and from the local keys when keys.bin is"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Tuple a1 _) (add-bookmark a0 "https://example.org/" []))
|
|
(def a2 (synced a1))
|
|
(file-delete (path-combine (.one f) "state.bin"))
|
|
(def a3 (must (reopen f (.one f) "hunter2")))
|
|
(check (= (urls a3) ["https://example.org/"]))
|
|
(check (= (vault-revision a3) (vault-revision a2)))
|
|
(file-delete (path-combine (.shared f) "keys.bin"))
|
|
(def a4 (must (reopen f (.one f) "hunter2")))
|
|
(def (Tuple _a5 report) (sync a4))
|
|
(check (string-contains? (problems-of report) "keys.bin was missing"))
|
|
(check (file-exists? (path-combine (.shared f) "keys.bin")))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a storage of another vault does not open"
|
|
(def f (folders))
|
|
(ignore (create f (.one f)))
|
|
(def elsewhere (path-combine (.root f) "elsewhere"))
|
|
(ignore (create-vault (folder-storage elsewhere) (path-combine (.root f) "three") "hunter2" #:cost cheap))
|
|
(check-match (open-vault (folder-storage elsewhere) (.one f) "hunter2") (Err (OtherVault _)))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
;; ---- Passwords --------------------------------------------------------------------
|
|
|
|
(: (-> Folders (Array byte)))
|
|
(defun (shared-keys f) (file-read-bytes (path-combine (.shared f) "keys.bin")))
|
|
|
|
(: (-> Folders (Array byte) void))
|
|
(defun (set-shared-keys! f keys) (file-write-bytes (path-combine (.shared f) "keys.bin") keys))
|
|
|
|
(: (-> Folders string (Array byte)))
|
|
(defun (own-keys f local) (file-read-bytes (path-combine local "keys.bin")))
|
|
|
|
(: (-> (Result string Vault) Vault))
|
|
(defun (changed r)
|
|
(match r
|
|
((Ok v) v)
|
|
((Err why) (panic! why))))
|
|
|
|
(: (-> SyncReport string bool))
|
|
(defun (told? report text)
|
|
(not (vec-empty? (vec-filter (fun (p) (string-contains? p text)) (.problems report)))))
|
|
|
|
(: (-> (Result StoreError Vault) Vault))
|
|
(defun (opened r) (must r))
|
|
|
|
(deftest "a new password comes with a new key, and the other devices read on"
|
|
(def f (folders))
|
|
(def a0 (fst (add-bookmark (create f (.one f)) "https://example.org/before" [])))
|
|
(def b0 (join f (.two f)))
|
|
(def old-keys (shared-keys f))
|
|
(def a1 (changed (vault-change-password (vault-save a0) "correct horse" #:cost cheap)))
|
|
(check-match (reopen f (.one f) "hunter2") (Err WrongPassword))
|
|
(check-match (reopen f (.one f) "correct horse") (Ok _))
|
|
(def a2 (vault-save (fst (add-bookmark a1 "https://example.org/after" []))))
|
|
;; An old copy of keys.bin and the old password do not open what was
|
|
;; written after the change.
|
|
(def (Ok old) (unlock-keys old-keys "hunter2") :leave-with (check-fail "the old keys did not open"))
|
|
(check-match (decode-device-file (file-read-bytes (file-of f a2)) (.vault-id old)
|
|
[(Tuple (.key-id old) (.dek old))])
|
|
(Err _))
|
|
;; The other device, open, reads on with the key it has, and is told.
|
|
(def (Tuple b1 report) (sync b0))
|
|
(check (told? report "The password was changed on another device"))
|
|
(def b2 (synced (fst (add-bookmark b1 "https://example.org/from-b" []))))
|
|
;; Its file is written with the earlier key, which the first device reads.
|
|
(def a3 (synced a2))
|
|
(check (vec-contains? (urls a3) "https://example.org/from-b"))
|
|
(check (not (= (vault-change-password b2 "another" #:cost cheap) (Ok b2))))
|
|
(check-match (vault-change-password b2 "another" #:cost cheap) (Err _))
|
|
;; The old password no longer opens it.
|
|
(check-match (reopen f (.two f) "hunter2") (Err WrongPassword))
|
|
(def (Tuple b3 report3) (sync (opened (reopen f (.two f) "correct horse"))))
|
|
(check (= (.problems report3) []))
|
|
(check (vec-contains? (urls b3) "https://example.org/after"))
|
|
;; Its copy of keys.bin is the new one, so it opens without the storage too.
|
|
(check (bytes=? (own-keys f (.two f)) (shared-keys f)))
|
|
(file-delete (path-combine (.shared f) "keys.bin"))
|
|
(check-match (reopen f (.two f) "correct horse") (Ok _))
|
|
(check-match (reopen f (.two f) "hunter2") (Err WrongPassword))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "two devices change the password at once, to two passwords"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def b0 (synced (fst (add-bookmark (join f (.two f)) "https://example.org/b-before" []))))
|
|
(def original (shared-keys f))
|
|
(def a1 (changed (vault-change-password (synced a0) "pw-a" #:cost cheap)))
|
|
(def keys-a (shared-keys f))
|
|
;; The other device changes it too, before it sees the first change.
|
|
(set-shared-keys! f original)
|
|
(def b1 (changed (vault-change-password b0 "pw-b" #:cost cheap)))
|
|
(def b2 (vault-save (fst (add-bookmark b1 "https://example.org/b-after" []))))
|
|
;; The sync keeps the first device's keys.bin.
|
|
(set-shared-keys! f keys-a)
|
|
(def (Tuple b3 report) (sync b2))
|
|
(check (told? report "at the same time"))
|
|
;; It writes with the key that both have, so the first device reads it.
|
|
(def a2 (synced a1))
|
|
(check (vec-contains? (urls a2) "https://example.org/b-after"))
|
|
(vault-close b3)
|
|
;; Its own password still opens it, from its copy of keys.bin.
|
|
(def (Tuple b4 report4) (sync (opened (reopen f (.two f) "pw-b"))))
|
|
(check (told? report4 "at the same time"))
|
|
(vault-close b4)
|
|
;; The other device's password opens all of it.
|
|
(def (Tuple b5 report5) (sync (opened (reopen f (.two f) "pw-a"))))
|
|
(check (= (.problems report5) []))
|
|
(check (vec-contains? (urls b5) "https://example.org/b-after"))
|
|
(check (vec-contains? (urls b5) "https://example.org/b-before"))
|
|
(check (bytes=? (own-keys f (.two f)) keys-a))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "two devices change the password at once, to the same password"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def b0 (synced (join f (.two f))))
|
|
(def original (shared-keys f))
|
|
(def a1 (changed (vault-change-password (synced a0) "same" #:cost cheap)))
|
|
(def keys-a (shared-keys f))
|
|
(set-shared-keys! f original)
|
|
(def b1 (changed (vault-change-password b0 "same" #:cost cheap)))
|
|
(vault-close (vault-save (fst (add-bookmark b1 "https://example.org/b-after" []))))
|
|
(set-shared-keys! f keys-a)
|
|
;; It opens with both keys files: its file is written with its own key.
|
|
(def (Tuple b2 report) (sync (opened (reopen f (.two f) "same"))))
|
|
(check (= (.problems report) []))
|
|
(check (vec-contains? (urls b2) "https://example.org/b-after"))
|
|
(check (vec-contains? (urls (synced a1)) "https://example.org/b-after"))
|
|
(check (bytes=? (own-keys f (.two f)) keys-a))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "a keys.bin that was replaced does not lock a device out"
|
|
(def f (folders))
|
|
(def a0 (vault-save (fst (add-bookmark (create f (.one f)) "https://example.org/a" []))))
|
|
(def mine (own-keys f (.one f)))
|
|
;; The same vault id, another key, and another password.
|
|
(set-shared-keys! f (wrap-keys (vault-id a0) 7 (make-aead-key XChaCha20Poly1305) "not yours" #:cost cheap))
|
|
(def (Tuple a1 report) (sync (opened (reopen f (.one f) "hunter2"))))
|
|
(check (told? report "holds none of this device's keys"))
|
|
(check (= (urls a1) ["https://example.org/a"]))
|
|
(check (bytes=? (own-keys f (.one f)) mine))
|
|
(check-match (reopen f (.one f) "not yours") (Err _))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
;; ---- Fields from a newer version --------------------------------------------------
|
|
|
|
(deftest "fields that a newer version wrote are kept through a change"
|
|
(def f (folders))
|
|
(def a0 (create f (.one f)))
|
|
(def (Ok unlocked) (unlock-keys (file-read-bytes (path-combine (.shared f) "keys.bin")) "hunter2")
|
|
:leave-with (check-fail "the keys did not open"))
|
|
;; A third device, of a newer version, wrote a bookmark with a rating.
|
|
(def newer (uuid-v4))
|
|
(def id (uuid-v4))
|
|
(def b (Bookmark (id id) (url "https://example.org/") (title (Some "Example"))
|
|
(added (instant "2026-11-11T09:00:00Z")) (tags []) (archive None)))
|
|
(def e (Entry (stamp (Stamp (ms 1000L) (counter 0) (device newer))) (state (Live b))
|
|
(extra [(Tuple "rating" (JsonInt 4L))])))
|
|
(def file (encode-device-file (FileHead (head-vault (.vault-id unlocked)) (head-device newer)
|
|
(head-revision 1L) (head-key-id (.key-id unlocked))
|
|
(head-cipher XChaCha20Poly1305))
|
|
(.dek unlocked)
|
|
(DeviceDoc (device newer) (seen (map-empty)) (entries [e]))))
|
|
(file-write-bytes (path-combine (.shared f) "devices" (device-file-name newer)) file)
|
|
(def a1 (synced a0))
|
|
(def a2 (synced (vault-put a1 (bookmark-with-tags b ["tagged"]))))
|
|
(match (map-try-ref (vault-entries a2) (uuid->string id))
|
|
((Some kept)
|
|
(check (= (.extra kept) [(Tuple "rating" (JsonInt 4L))]))
|
|
(check (= (match (.state kept) ((Live k) (.title k)) ((Deleted _) None)) (Some "Example")))
|
|
(check (> (stamp-compare (.stamp kept) (.stamp e)) 0)))
|
|
(None (check-fail "the bookmark is gone")))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
;; ---- A storage of functions ---------------------------------------------------------
|
|
|
|
;; A `FunctionStorage` that keeps its files in memory, by name, as the
|
|
;; folder of Android's Storage Access Framework is kept behind Avalonia.
|
|
(: (-> (Box (Map string (Array byte))) Storage))
|
|
(defun (memory-storage files)
|
|
(: (-> string (Option (Array byte))))
|
|
(defun (read name) (map-try-ref (box-ref files) name))
|
|
(: (-> string (Array byte) void))
|
|
(defun (write name bytes) (box-set! files (map-set (box-ref files) name bytes)))
|
|
(: (-> string (Vec string)))
|
|
(defun (list-names dir)
|
|
(def prefix (str dir "/"))
|
|
(vec-map (fun (k) (string-drop k (string-count prefix)))
|
|
(vec-filter (fun (k) (string-starts-with? k prefix)) (->vec (map-keys (box-ref files))))))
|
|
(function-storage "memory" read write list-names))
|
|
|
|
(deftest "two devices sync through a storage of functions"
|
|
(def root (make-temp-directory #:prefix "bjomarks-test-"))
|
|
(def files (make-box (: (map-empty) (Map string (Array byte)))))
|
|
(def storage (memory-storage files))
|
|
(check (= (storage-label storage) "memory"))
|
|
(def a0 (must (create-vault storage (path-combine root "one") "hunter2" #:cost cheap)))
|
|
(check (some? (storage-read storage "keys.bin")))
|
|
(check (= (storage-list storage devices-dir) [(device-file-name (vault-device a0))]))
|
|
(def a1 (synced (fst (add-bookmark a0 "https://example.org/a" ["x"]))))
|
|
(def b0 (must-join (join-vault storage (path-combine root "two") "hunter2")))
|
|
(check (= (urls b0) ["https://example.org/a"]))
|
|
(def b1 (synced (fst (at "2026-11-11T09:00:00Z" #(add-bookmark b0 "https://example.org/b" [])))))
|
|
(check (= (vec-length (storage-list storage devices-dir)) 2))
|
|
(def a2 (synced a1))
|
|
(check (= (vec-sort (urls a2)) ["https://example.org/a" "https://example.org/b"]))
|
|
;; A temporary file of a syncer, beginning with ".", is not a device's.
|
|
(storage-write storage "devices/.syncthing.x.tmp" (make-array 3))
|
|
(check (= (vec-length (storage-list storage devices-dir)) 2))
|
|
(def (Tuple _ report) (vault-sync b1))
|
|
(check (vec-empty? (.problems report)))
|
|
(directory-delete-tree root))
|
|
|
|
(deftest "each shared folder has a device folder of its own"
|
|
(def f (folders))
|
|
(def first (folder-storage (.shared f)))
|
|
(def second-path (path-combine (.root f) "second"))
|
|
(def second (folder-storage second-path))
|
|
(def a (device-folder (.one f) (.shared f) first))
|
|
(def b (device-folder (.one f) second-path second))
|
|
(check (not (= a b)))
|
|
(check (= a (device-folder (.one f) (.shared f) first)))
|
|
(ignore (must (create-vault first a "hunter2" #:cost cheap)))
|
|
;; The second folder is empty: a new vault, not the first one.
|
|
(check-match (open-vault second b "hunter2") (Err (NoVault _)))
|
|
(ignore (must (create-vault second b "other" #:cost cheap)))
|
|
(check (= (vec-length (vault-bookmarks (must (open-vault first a "hunter2")))) 0))
|
|
(check-match (open-vault second b "other") (Ok _))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(deftest "the files of the one device folder before move to its shared folder's"
|
|
(def f (folders))
|
|
(def first (folder-storage (.shared f)))
|
|
(def v (must (create-vault first (.one f) "hunter2" #:cost cheap)))
|
|
(def other-path (path-combine (.root f) "other"))
|
|
(def other (folder-storage other-path))
|
|
(ignore (must (create-vault other (.two f) "pw" #:cost cheap)))
|
|
;; A folder of another vault leaves them where they are.
|
|
(def b (device-folder (.one f) other-path other))
|
|
(check (not (file-exists? (path-combine b "device.json"))))
|
|
(check (file-exists? (path-combine (.one f) "device.json")))
|
|
;; The folder of their vault gets them.
|
|
(def a (device-folder (.one f) (.shared f) first))
|
|
(check (file-exists? (path-combine a "device.json")))
|
|
(check (file-exists? (path-combine a "keys.bin")))
|
|
(check (not (file-exists? (path-combine (.one f) "device.json"))))
|
|
(def again (must (open-vault first a "hunter2")))
|
|
(check (= (vault-id again) (vault-id v)))
|
|
(directory-delete-tree (.root f)))
|
|
|
|
(run-tests)
|