block.go 5.26 KB
Newer Older
1
package tests
2 3 4

import (
	"context"
Łukasz Magiera's avatar
Łukasz Magiera committed
5
	"github.com/ipfs/interface-go-ipfs-core/path"
6 7 8 9
	"io/ioutil"
	"strings"
	"testing"

Łukasz Magiera's avatar
Łukasz Magiera committed
10 11
	coreiface "github.com/ipfs/interface-go-ipfs-core"
	opt "github.com/ipfs/interface-go-ipfs-core/options"
12

Łukasz Magiera's avatar
Łukasz Magiera committed
13
	mh "github.com/multiformats/go-multihash"
14 15
)

Łukasz Magiera's avatar
Łukasz Magiera committed
16
func (tp *TestSuite) TestBlock(t *testing.T) {
17 18 19 20 21 22 23
	tp.hasApi(t, func(api coreiface.CoreAPI) error {
		if api.Block() == nil {
			return apiNotImplemented
		}
		return nil
	})

24 25 26 27 28 29
	t.Run("TestBlockPut", tp.TestBlockPut)
	t.Run("TestBlockPutFormat", tp.TestBlockPutFormat)
	t.Run("TestBlockPutHash", tp.TestBlockPutHash)
	t.Run("TestBlockGet", tp.TestBlockGet)
	t.Run("TestBlockRm", tp.TestBlockRm)
	t.Run("TestBlockStat", tp.TestBlockStat)
Łukasz Magiera's avatar
Łukasz Magiera committed
30
	t.Run("TestBlockPin", tp.TestBlockPin)
31 32
}

Łukasz Magiera's avatar
Łukasz Magiera committed
33
func (tp *TestSuite) TestBlockPut(t *testing.T) {
34 35
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
36
	api, err := tp.makeAPI(ctx)
37
	if err != nil {
38
		t.Fatal(err)
39 40 41 42
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
	if err != nil {
43
		t.Fatal(err)
44 45 46 47 48 49 50
	}

	if res.Path().Cid().String() != "QmPyo15ynbVrSTVdJL9th7JysHaAbXt9dM9tXk1bMHbRtk" {
		t.Errorf("got wrong cid: %s", res.Path().Cid().String())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
51
func (tp *TestSuite) TestBlockPutFormat(t *testing.T) {
52 53
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
54
	api, err := tp.makeAPI(ctx)
55
	if err != nil {
56
		t.Fatal(err)
57 58 59 60
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("cbor"))
	if err != nil {
61
		t.Fatal(err)
62 63
	}

64
	if res.Path().Cid().String() != "bafyreiayl6g3gitr7ys7kyng7sjywlrgimdoymco3jiyab6rozecmoazne" {
65 66 67 68
		t.Errorf("got wrong cid: %s", res.Path().Cid().String())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
69
func (tp *TestSuite) TestBlockPutHash(t *testing.T) {
70 71
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
72
	api, err := tp.makeAPI(ctx)
73
	if err != nil {
74
		t.Fatal(err)
75 76 77 78 79 80 81
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
	if err != nil {
		t.Fatal(err)
	}

82
	if res.Path().Cid().String() != "bafyb2qgdh7w6dcq24u65xbtdoehyavegnpvxcqce7ttvs6ielgmwdfxrahmu37d33atik57x5y6s7d7qz32aasuwgirh3ocn6ywswqdifvu6e" {
83 84 85 86
		t.Errorf("got wrong cid: %s", res.Path().Cid().String())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
87
func (tp *TestSuite) TestBlockGet(t *testing.T) {
88 89
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
90
	api, err := tp.makeAPI(ctx)
91
	if err != nil {
92
		t.Fatal(err)
93 94 95 96
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
	if err != nil {
97
		t.Fatal(err)
98 99 100 101
	}

	r, err := api.Block().Get(ctx, res.Path())
	if err != nil {
102
		t.Fatal(err)
103 104 105 106
	}

	d, err := ioutil.ReadAll(r)
	if err != nil {
107
		t.Fatal(err)
108 109 110 111 112 113
	}

	if string(d) != "Hello" {
		t.Error("didn't get correct data back")
	}

114
	p := path.New("/ipfs/" + res.Path().Cid().String())
115 116 117 118 119 120 121 122 123 124

	rp, err := api.ResolvePath(ctx, p)
	if err != nil {
		t.Fatal(err)
	}
	if rp.Cid().String() != res.Path().Cid().String() {
		t.Error("paths didn't match")
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
125
func (tp *TestSuite) TestBlockRm(t *testing.T) {
126 127
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
128
	api, err := tp.makeAPI(ctx)
129
	if err != nil {
130
		t.Fatal(err)
131 132 133 134
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
	if err != nil {
135
		t.Fatal(err)
136 137 138 139
	}

	r, err := api.Block().Get(ctx, res.Path())
	if err != nil {
140
		t.Fatal(err)
141 142 143 144
	}

	d, err := ioutil.ReadAll(r)
	if err != nil {
145
		t.Fatal(err)
146 147 148 149 150 151 152 153
	}

	if string(d) != "Hello" {
		t.Error("didn't get correct data back")
	}

	err = api.Block().Rm(ctx, res.Path())
	if err != nil {
154
		t.Fatal(err)
155 156 157 158 159 160
	}

	_, err = api.Block().Get(ctx, res.Path())
	if err == nil {
		t.Error("expected err to exist")
	}
161
	if !strings.Contains(err.Error(), "blockservice: key not found") {
162 163 164 165 166 167 168
		t.Errorf("unexpected error; %s", err.Error())
	}

	err = api.Block().Rm(ctx, res.Path())
	if err == nil {
		t.Error("expected err to exist")
	}
169
	if !strings.Contains(err.Error(), "blockstore: block not found") {
170 171 172 173 174
		t.Errorf("unexpected error; %s", err.Error())
	}

	err = api.Block().Rm(ctx, res.Path(), opt.Block.Force(true))
	if err != nil {
175
		t.Fatal(err)
176 177 178
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
179
func (tp *TestSuite) TestBlockStat(t *testing.T) {
180 181
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
182
	api, err := tp.makeAPI(ctx)
183
	if err != nil {
184
		t.Fatal(err)
185 186 187 188
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
	if err != nil {
189
		t.Fatal(err)
190 191 192 193
	}

	stat, err := api.Block().Stat(ctx, res.Path())
	if err != nil {
194
		t.Fatal(err)
195 196 197 198 199 200 201 202 203 204
	}

	if stat.Path().String() != res.Path().String() {
		t.Error("paths don't match")
	}

	if stat.Size() != len("Hello") {
		t.Error("length doesn't match")
	}
}
Łukasz Magiera's avatar
Łukasz Magiera committed
205

Łukasz Magiera's avatar
Łukasz Magiera committed
206
func (tp *TestSuite) TestBlockPin(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
207 208 209 210
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	api, err := tp.makeAPI(ctx)
	if err != nil {
211
		t.Fatal(err)
Łukasz Magiera's avatar
Łukasz Magiera committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	}

	_, err = api.Block().Put(ctx, strings.NewReader(`Hello`))
	if err != nil {
		t.Fatal(err)
	}

	if pins, err := api.Pin().Ls(ctx); err != nil || len(pins) != 0 {
		t.Fatal("expected 0 pins")
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Pin(true))
	if err != nil {
		t.Fatal(err)
	}

228
	pins, err := accPins(api.Pin().Ls(ctx))
Łukasz Magiera's avatar
Łukasz Magiera committed
229 230 231 232 233 234 235 236 237 238 239 240 241
	if err != nil {
		return
	}
	if len(pins) != 1 {
		t.Fatal("expected 1 pin")
	}
	if pins[0].Type() != "recursive" {
		t.Error("expected a recursive pin")
	}
	if pins[0].Path().String() != res.Path().String() {
		t.Error("pin path didn't match")
	}
}