forked from
gazagnaire.org/irmin
Persistent store with Git semantics: lazy reads, delayed writes, content-addressing
1open Irmin
2
3let test_proof_produce_verify () =
4 let backend = Backend.Memory.create_sha1 () in
5 let tree = Tree.Git.empty () in
6 let tree = Tree.Git.add tree [ "foo"; "bar" ] "hello" in
7 let tree = Tree.Git.add tree [ "foo"; "baz" ] "world" in
8 let root_hash = Tree.Git.hash tree ~backend in
9 let proof, result =
10 Proof.Git.produce backend root_hash (fun t ->
11 let v = Proof.Git.Tree.find t [ "foo"; "bar" ] in
12 (t, v))
13 in
14 Alcotest.(check (option string)) "found value" (Some "hello") result;
15 match
16 Proof.Git.verify proof (fun t ->
17 let v = Proof.Git.Tree.find t [ "foo"; "bar" ] in
18 (t, v))
19 with
20 | Ok (_, v) ->
21 Alcotest.(check (option string)) "verified value" (Some "hello") v
22 | Error (`Proof_mismatch msg) -> Alcotest.fail ("proof mismatch: " ^ msg)
23
24let test_proof_blinded () =
25 let backend = Backend.Memory.create_sha1 () in
26 let tree = Tree.Git.empty () in
27 let tree = Tree.Git.add tree [ "a" ] "1" in
28 let tree = Tree.Git.add tree [ "b" ] "2" in
29 let root_hash = Tree.Git.hash tree ~backend in
30 let proof, _ =
31 Proof.Git.produce backend root_hash (fun t ->
32 let _ = Proof.Git.Tree.find t [ "a" ] in
33 (t, ()))
34 in
35 let state = Proof.state proof in
36 match state with
37 | Proof.Node entries ->
38 let has_a =
39 List.exists
40 (fun (k, v) ->
41 k = "a" && match v with Proof.Contents "1" -> true | _ -> false)
42 entries
43 in
44 let has_blinded_b =
45 List.exists
46 (fun (k, v) ->
47 k = "b"
48 && match v with Proof.Blinded_contents _ -> true | _ -> false)
49 entries
50 in
51 Alcotest.(check bool) "has a" true has_a;
52 Alcotest.(check bool) "b is blinded" true has_blinded_b
53 | _ -> Alcotest.fail "expected Node"
54
55let test_proof_mst () =
56 let backend = Backend.Memory.create_sha256 () in
57 let tree = Tree.Mst.empty () in
58 let tree = Tree.Mst.add tree [ "key1" ] "value1" in
59 let tree = Tree.Mst.add tree [ "key2" ] "value2" in
60 let root_hash = Tree.Mst.hash tree ~backend in
61 let proof, result =
62 Proof.Mst.produce backend root_hash (fun t ->
63 let v = Proof.Mst.Tree.find t [ "key1" ] in
64 (t, v))
65 in
66 Alcotest.(check (option string)) "found value" (Some "value1") result;
67 match
68 Proof.Mst.verify proof (fun t ->
69 let v = Proof.Mst.Tree.find t [ "key1" ] in
70 (t, v))
71 with
72 | Ok (_, v) ->
73 Alcotest.(check (option string)) "verified value" (Some "value1") v
74 | Error (`Proof_mismatch msg) -> Alcotest.fail ("proof mismatch: " ^ msg)
75
76let suite =
77 ( "Proof",
78 [
79 Alcotest.test_case "produce/verify" `Quick test_proof_produce_verify;
80 Alcotest.test_case "blinded nodes" `Quick test_proof_blinded;
81 Alcotest.test_case "mst proofs" `Quick test_proof_mst;
82 ] )