@cryptotaxi247 / kubo / commits / bb6bf4ce5

coreapi: block tests

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Jan 8, 2018 at 14:01 UTC bb6bf4ce53c6a6a06d27997514807b379cade1c1
2 files changed +174 -2
core/coreapi/block.go
+7 -2
@@ -12,6 +12,7 @@ import (
12 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
13 caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
14
15 + mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
16 blocks "gx/ipfs/QmYsEQydGrsxNZfAiskvQ76N2xE9hDQtSAkRSynwMiUK3c/go-block-format"
17 cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
18 )
@@ -44,7 +45,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
45 if !ok {
46 return nil, fmt.Errorf("unrecognized format: %s", settings.Codec)
47 }
47 - if settings.Codec == "v0" {
48 + if settings.Codec == "v0" && settings.MhType == mh.SHA2_256 {
49 pref.Version = 0
50 }
51 pref.Codec = formatval
@@ -93,7 +94,11 @@ func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.Bl
94 }
95
96 select {
96 - case res := <-out:
97 + case res, ok := <-out:
98 + if !ok {
99 + return nil
100 + }
101 +
102 remBlock, ok := res.(*util.RemovedBlock)
103 if !ok {
104 return errors.New("got unexpected output from util.RmBlocks")
core/coreapi/block_test.go new
+167
@@ -0,0 +1,167 @@
1 +package coreapi_test
2 +
3 +import (
4 + "context"
5 + "io/ioutil"
6 + "strings"
7 + "testing"
8 +
9 + mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
10 +)
11 +
12 +func TestBlockPut(t *testing.T) {
13 + ctx := context.Background()
14 + _, api, err := makeAPI(ctx)
15 + if err != nil {
16 + t.Error(err)
17 + }
18 +
19 + res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
20 + if err != nil {
21 + t.Error(err)
22 + }
23 +
24 + if res.Cid().String() != "QmPyo15ynbVrSTVdJL9th7JysHaAbXt9dM9tXk1bMHbRtk" {
25 + t.Errorf("got wrong cid: %s", res.Cid().String())
26 + }
27 +}
28 +
29 +func TestBlockPutFormat(t *testing.T) {
30 + ctx := context.Background()
31 + _, api, err := makeAPI(ctx)
32 + if err != nil {
33 + t.Error(err)
34 + }
35 +
36 + res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), api.Block().WithFormat("cbor"))
37 + if err != nil {
38 + t.Error(err)
39 + }
40 +
41 + if res.Cid().String() != "zdpuAn4amuLWo8Widi5v6VQpuo2dnpnwbVE3oB6qqs7mDSeoa" {
42 + t.Errorf("got wrong cid: %s", res.Cid().String())
43 + }
44 +}
45 +
46 +func TestBlockPutHash(t *testing.T) {
47 + ctx := context.Background()
48 + _, api, err := makeAPI(ctx)
49 + if err != nil {
50 + t.Error(err)
51 + }
52 +
53 + res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), api.Block().WithHash(mh.KECCAK_512, -1))
54 + if err != nil {
55 + t.Error(err)
56 + }
57 +
58 + if res.Cid().String() != "zBurKB9YZkcDf6xa53WBE8CFX4ydVqAyf9KPXBFZt5stJzEstaS8Hukkhu4gwpMtc1xHNDbzP7sPtQKyWsP3C8fbhkmrZ" {
59 + t.Errorf("got wrong cid: %s", res.Cid().String())
60 + }
61 +}
62 +
63 +func TestBlockGet(t *testing.T) {
64 + ctx := context.Background()
65 + _, api, err := makeAPI(ctx)
66 + if err != nil {
67 + t.Error(err)
68 + }
69 +
70 + res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), api.Block().WithHash(mh.KECCAK_512, -1))
71 + if err != nil {
72 + t.Error(err)
73 + }
74 +
75 + r, err := api.Block().Get(ctx, res)
76 + if err != nil {
77 + t.Error(err)
78 + }
79 +
80 + d, err := ioutil.ReadAll(r)
81 + if err != nil {
82 + t.Error(err)
83 + }
84 +
85 + if string(d) != "Hello" {
86 + t.Error("didn't get correct data back")
87 + }
88 +}
89 +
90 +func TestBlockRm(t *testing.T) {
91 + ctx := context.Background()
92 + _, api, err := makeAPI(ctx)
93 + if err != nil {
94 + t.Error(err)
95 + }
96 +
97 + res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
98 + if err != nil {
99 + t.Error(err)
100 + }
101 +
102 + r, err := api.Block().Get(ctx, res)
103 + if err != nil {
104 + t.Error(err)
105 + }
106 +
107 + d, err := ioutil.ReadAll(r)
108 + if err != nil {
109 + t.Error(err)
110 + }
111 +
112 + if string(d) != "Hello" {
113 + t.Error("didn't get correct data back")
114 + }
115 +
116 + err = api.Block().Rm(ctx, res)
117 + if err != nil {
118 + t.Error(err)
119 + }
120 +
121 + _, err = api.Block().Get(ctx, res)
122 + if err == nil {
123 + t.Error("expected err to exist")
124 + }
125 + if err.Error() != "blockservice: key not found" {
126 + t.Errorf("unexpected error; %s", err.Error())
127 + }
128 +
129 + err = api.Block().Rm(ctx, res)
130 + if err == nil {
131 + t.Error("expected err to exist")
132 + }
133 + if err.Error() != "blockstore: block not found" {
134 + t.Errorf("unexpected error; %s", err.Error())
135 + }
136 +
137 + err = api.Block().Rm(ctx, res, api.Block().WithForce(true))
138 + if err != nil {
139 + t.Error(err)
140 + }
141 +}
142 +
143 +func TestBlockStat(t *testing.T) {
144 + ctx := context.Background()
145 + _, api, err := makeAPI(ctx)
146 + if err != nil {
147 + t.Error(err)
148 + }
149 +
150 + res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
151 + if err != nil {
152 + t.Error(err)
153 + }
154 +
155 + stat, err := api.Block().Stat(ctx, res)
156 + if err != nil {
157 + t.Error(err)
158 + }
159 +
160 + if stat.Path().String() != res.String() {
161 + t.Error("paths don't match")
162 + }
163 +
164 + if stat.Size() != len("Hello") {
165 + t.Error("length doesn't match")
166 + }
167 +}