-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proof_test.go
98 lines (84 loc) · 2.49 KB
/
proof_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package unixfsproof
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"testing"
"github.com/ipfs/go-cid"
chunker "github.com/ipfs/go-ipfs-chunker"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-unixfs/importer/balanced"
h "github.com/ipfs/go-unixfs/importer/helpers"
testu "github.com/ipfs/go-unixfs/test"
"github.com/stretchr/testify/require"
)
func TestProofVerify(t *testing.T) {
t.Parallel()
ctx := context.Background()
dataSize := int64(100000)
chunkSize := int64(256)
rootCid, dserv := setupData(t, dataSize, chunkSize)
tests := []struct {
proofOffset uint64
verifOffset uint64
notOkVerif bool
notOkProof bool
}{
// Correct proofs.
{proofOffset: 40, verifOffset: 40},
{proofOffset: 500, verifOffset: 500},
{proofOffset: 6000, verifOffset: 6000},
{proofOffset: 70000, verifOffset: 70000},
{proofOffset: uint64(dataSize), verifOffset: uint64(dataSize)},
// Correct proof due to being in same block
{proofOffset: 40, verifOffset: 41},
{proofOffset: 41, verifOffset: 40},
// Indirectly correct proofs; this should work unless we change
// the verification to not allow unvisited blocks; not clear if that's
// entirely useful.
{proofOffset: 868, verifOffset: 1124},
// Definitely wrong proofs.
{proofOffset: 40, verifOffset: 50000, notOkVerif: true},
{proofOffset: 70000, verifOffset: 10, notOkVerif: true},
// Offset bigger than file size.
{proofOffset: uint64(dataSize) + 1, verifOffset: 0, notOkProof: true},
}
for _, test := range tests {
test := test
tname := fmt.Sprintf("%d %d", test.proofOffset, test.verifOffset)
t.Run(tname, func(t *testing.T) {
t.Parallel()
proof, err := Prove(ctx, rootCid, test.proofOffset, dserv)
if test.notOkProof {
require.Error(t, err)
return
}
require.NoError(t, err)
ok, err := Verify(ctx, rootCid, test.verifOffset, proof)
require.NoError(t, err)
require.Equal(t, !test.notOkVerif, ok)
})
}
}
func setupData(t *testing.T, dataSize, chunkSize int64) (cid.Cid, ipld.DAGService) {
r := rand.New(rand.NewSource(22))
data := make([]byte, dataSize)
_, err := io.ReadFull(r, data)
require.NoError(t, err)
in := bytes.NewReader(data)
opts := testu.UseCidV1
dserv := testu.GetDAGServ()
dbp := h.DagBuilderParams{
Dagserv: dserv,
Maxlinks: 3,
CidBuilder: opts.Prefix,
RawLeaves: opts.RawLeavesUsed,
}
db, err := dbp.New(chunker.NewSizeSplitter(in, chunkSize))
require.NoError(t, err)
n, err := balanced.Layout(db)
require.NoError(t, err)
return n.Cid(), dserv
}