block.go 5.65 KB
Newer Older
1
package tests
2 3

import (
Steven Allen's avatar
Steven Allen committed
4
	"bytes"
5
	"context"
Steven Allen's avatar
Steven Allen committed
6
	"io"
7 8 9 10
	"io/ioutil"
	"strings"
	"testing"

Łukasz Magiera's avatar
Łukasz Magiera committed
11 12
	coreiface "github.com/ipfs/interface-go-ipfs-core"
	opt "github.com/ipfs/interface-go-ipfs-core/options"
Steven Allen's avatar
Steven Allen committed
13
	"github.com/ipfs/interface-go-ipfs-core/path"
14

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

Steven Allen's avatar
Steven Allen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31
var (
	pbCid    = "QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN"
	cborCid  = "bafyreicnga62zhxnmnlt6ymq5hcbsg7gdhqdu6z4ehu3wpjhvqnflfy6nm"
	cborKCid = "bafyr2qgsohbwdlk7ajmmbb4lhoytmest4wdbe5xnexfvtxeatuyqqmwv3fgxp3pmhpc27gwey2cct56gloqefoqwcf3yqiqzsaqb7p4jefhcw"
)

func pbBlock() io.Reader {
	return bytes.NewReader([]byte{10, 12, 8, 2, 18, 6, 104, 101, 108, 108, 111, 10, 24, 6})
}

func cborBlock() io.Reader {
	return bytes.NewReader([]byte{101, 72, 101, 108, 108, 111})
}

Łukasz Magiera's avatar
Łukasz Magiera committed
32
func (tp *TestSuite) TestBlock(t *testing.T) {
33 34 35 36 37 38 39
	tp.hasApi(t, func(api coreiface.CoreAPI) error {
		if api.Block() == nil {
			return apiNotImplemented
		}
		return nil
	})

40 41 42 43 44 45
	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
46
	t.Run("TestBlockPin", tp.TestBlockPin)
47 48
}

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

Steven Allen's avatar
Steven Allen committed
57
	res, err := api.Block().Put(ctx, pbBlock())
58
	if err != nil {
59
		t.Fatal(err)
60 61
	}

Steven Allen's avatar
Steven Allen committed
62
	if res.Path().Cid().String() != pbCid {
63 64 65 66
		t.Errorf("got wrong cid: %s", res.Path().Cid().String())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
67
func (tp *TestSuite) TestBlockPutFormat(t *testing.T) {
68 69
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
70
	api, err := tp.makeAPI(ctx)
71
	if err != nil {
72
		t.Fatal(err)
73 74
	}

Steven Allen's avatar
Steven Allen committed
75
	res, err := api.Block().Put(ctx, cborBlock(), opt.Block.Format("cbor"))
76
	if err != nil {
77
		t.Fatal(err)
78 79
	}

Steven Allen's avatar
Steven Allen committed
80
	if res.Path().Cid().String() != cborCid {
81 82 83 84
		t.Errorf("got wrong cid: %s", res.Path().Cid().String())
	}
}

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

Steven Allen's avatar
Steven Allen committed
93 94 95 96 97 98
	res, err := api.Block().Put(
		ctx,
		cborBlock(),
		opt.Block.Hash(mh.KECCAK_512, -1),
		opt.Block.Format("cbor"),
	)
99 100 101 102
	if err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
103
	if res.Path().Cid().String() != cborKCid {
104 105 106 107
		t.Errorf("got wrong cid: %s", res.Path().Cid().String())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
108
func (tp *TestSuite) TestBlockGet(t *testing.T) {
109 110
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
111
	api, err := tp.makeAPI(ctx)
112
	if err != nil {
113
		t.Fatal(err)
114 115
	}

Steven Allen's avatar
Steven Allen committed
116
	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw"))
117
	if err != nil {
118
		t.Fatal(err)
119 120 121 122
	}

	r, err := api.Block().Get(ctx, res.Path())
	if err != nil {
123
		t.Fatal(err)
124 125 126 127
	}

	d, err := ioutil.ReadAll(r)
	if err != nil {
128
		t.Fatal(err)
129 130 131 132 133 134
	}

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

135
	p := path.New("/ipfs/" + res.Path().Cid().String())
136 137 138 139 140 141 142 143 144 145

	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
146
func (tp *TestSuite) TestBlockRm(t *testing.T) {
147 148
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
149
	api, err := tp.makeAPI(ctx)
150
	if err != nil {
151
		t.Fatal(err)
152 153
	}

Steven Allen's avatar
Steven Allen committed
154
	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw"))
155
	if err != nil {
156
		t.Fatal(err)
157 158 159 160
	}

	r, err := api.Block().Get(ctx, res.Path())
	if err != nil {
161
		t.Fatal(err)
162 163 164 165
	}

	d, err := ioutil.ReadAll(r)
	if err != nil {
166
		t.Fatal(err)
167 168 169 170 171 172 173 174
	}

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

	err = api.Block().Rm(ctx, res.Path())
	if err != nil {
175
		t.Fatal(err)
176 177 178 179 180 181
	}

	_, err = api.Block().Get(ctx, res.Path())
	if err == nil {
		t.Error("expected err to exist")
	}
182
	if !strings.Contains(err.Error(), "blockservice: key not found") {
183 184 185 186 187 188 189
		t.Errorf("unexpected error; %s", err.Error())
	}

	err = api.Block().Rm(ctx, res.Path())
	if err == nil {
		t.Error("expected err to exist")
	}
190
	if !strings.Contains(err.Error(), "blockstore: block not found") {
191 192 193 194 195
		t.Errorf("unexpected error; %s", err.Error())
	}

	err = api.Block().Rm(ctx, res.Path(), opt.Block.Force(true))
	if err != nil {
196
		t.Fatal(err)
197 198 199
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
200
func (tp *TestSuite) TestBlockStat(t *testing.T) {
201 202
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
203
	api, err := tp.makeAPI(ctx)
204
	if err != nil {
205
		t.Fatal(err)
206 207
	}

Steven Allen's avatar
Steven Allen committed
208
	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw"))
209
	if err != nil {
210
		t.Fatal(err)
211 212 213 214
	}

	stat, err := api.Block().Stat(ctx, res.Path())
	if err != nil {
215
		t.Fatal(err)
216 217 218 219 220 221 222 223 224 225
	}

	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
226

Łukasz Magiera's avatar
Łukasz Magiera committed
227
func (tp *TestSuite) TestBlockPin(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
228 229 230 231
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	api, err := tp.makeAPI(ctx)
	if err != nil {
232
		t.Fatal(err)
Łukasz Magiera's avatar
Łukasz Magiera committed
233 234
	}

Steven Allen's avatar
Steven Allen committed
235
	_, err = api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw"))
Łukasz Magiera's avatar
Łukasz Magiera committed
236 237 238 239 240 241 242 243
	if err != nil {
		t.Fatal(err)
	}

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

Steven Allen's avatar
Steven Allen committed
244 245 246 247 248 249
	res, err := api.Block().Put(
		ctx,
		strings.NewReader(`Hello`),
		opt.Block.Pin(true),
		opt.Block.Format("raw"),
	)
Łukasz Magiera's avatar
Łukasz Magiera committed
250 251 252 253
	if err != nil {
		t.Fatal(err)
	}

254
	pins, err := accPins(api.Pin().Ls(ctx))
Łukasz Magiera's avatar
Łukasz Magiera committed
255
	if err != nil {
Steven Allen's avatar
Steven Allen committed
256
		t.Fatal(err)
Łukasz Magiera's avatar
Łukasz Magiera committed
257 258 259 260 261 262 263 264 265 266 267
	}
	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")
	}
}