This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
idstore_test.go
162 lines (135 loc) · 3.55 KB
/
idstore_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package blockstore
import (
"context"
"testing"
blk "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
mh "github.com/multiformats/go-multihash"
)
func createTestStores() (Blockstore, *callbackDatastore) {
cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()}
ids := NewIdStore(NewBlockstore(cd))
return ids, cd
}
func TestIdStore(t *testing.T) {
idhash1, _ := cid.NewPrefixV1(cid.Raw, mh.IDENTITY).Sum([]byte("idhash1"))
idblock1, _ := blk.NewBlockWithCid([]byte("idhash1"), idhash1)
hash1, _ := cid.NewPrefixV1(cid.Raw, mh.SHA2_256).Sum([]byte("hash1"))
block1, _ := blk.NewBlockWithCid([]byte("hash1"), hash1)
emptyHash, _ := cid.NewPrefixV1(cid.Raw, mh.SHA2_256).Sum([]byte("emptyHash"))
emptyBlock, _ := blk.NewBlockWithCid([]byte{}, emptyHash)
ids, cb := createTestStores()
have, _ := ids.Has(bg, idhash1)
if !have {
t.Fatal("Has() failed on idhash")
}
_, err := ids.Get(bg, idhash1)
if err != nil {
t.Fatalf("Get() failed on idhash: %v", err)
}
noop := func() {}
failIfPassThough := func() {
t.Fatal("operation on identity hash passed though to datastore")
}
cb.f = failIfPassThough
err = ids.Put(bg, idblock1)
if err != nil {
t.Fatal(err)
}
cb.f = noop
err = ids.Put(bg, block1)
if err != nil {
t.Fatalf("Put() failed on normal block: %v", err)
}
have, _ = ids.Has(bg, hash1)
if !have {
t.Fatal("normal block not added to datastore")
}
blockSize, _ := ids.GetSize(bg, hash1)
if blockSize == -1 {
t.Fatal("normal block not added to datastore")
}
_, err = ids.Get(bg, hash1)
if err != nil {
t.Fatal(err)
}
err = ids.Put(bg, emptyBlock)
if err != nil {
t.Fatalf("Put() failed on normal block: %v", err)
}
have, _ = ids.Has(bg, emptyHash)
if !have {
t.Fatal("normal block not added to datastore")
}
blockSize, _ = ids.GetSize(bg, emptyHash)
if blockSize != 0 {
t.Fatal("normal block not added to datastore")
}
cb.f = failIfPassThough
err = ids.DeleteBlock(bg, idhash1)
if err != nil {
t.Fatal(err)
}
cb.f = noop
err = ids.DeleteBlock(bg, hash1)
if err != nil {
t.Fatal(err)
}
have, _ = ids.Has(bg, hash1)
if have {
t.Fatal("normal block not deleted from datastore")
}
blockSize, _ = ids.GetSize(bg, hash1)
if blockSize > -1 {
t.Fatal("normal block not deleted from datastore")
}
err = ids.DeleteBlock(bg, emptyHash)
if err != nil {
t.Fatal(err)
}
idhash2, _ := cid.NewPrefixV1(cid.Raw, mh.IDENTITY).Sum([]byte("idhash2"))
idblock2, _ := blk.NewBlockWithCid([]byte("idhash2"), idhash2)
hash2, _ := cid.NewPrefixV1(cid.Raw, mh.SHA2_256).Sum([]byte("hash2"))
block2, _ := blk.NewBlockWithCid([]byte("hash2"), hash2)
cb.f = failIfPassThough
err = ids.PutMany(bg, []blk.Block{idblock1, idblock2})
if err != nil {
t.Fatal(err)
}
opCount := 0
cb.f = func() {
opCount++
}
err = ids.PutMany(bg, []blk.Block{block1, block2})
if err != nil {
t.Fatal(err)
}
if opCount != 4 {
// one call to Has and Put for each Cid
t.Fatalf("expected exactly 4 operations got %d", opCount)
}
opCount = 0
err = ids.PutMany(bg, []blk.Block{idblock1, block1})
if err != nil {
t.Fatal(err)
}
if opCount != 1 {
// just one call to Put from the normal (non-id) block
t.Fatalf("expected exactly 1 operations got %d", opCount)
}
ch, err := ids.AllKeysChan(context.TODO())
if err != nil {
t.Fatal(err)
}
cnt := 0
for c := range ch {
cnt++
if c.Prefix().MhType == mh.IDENTITY {
t.Fatalf("block with identity hash found in blockstore")
}
}
if cnt != 2 {
t.Fatalf("expected exactly two keys returned by AllKeysChan got %d", cnt)
}
}