block.go 5.18 KB
Newer Older
1
package tests
2 3 4 5 6 7 8

import (
	"context"
	"io/ioutil"
	"strings"
	"testing"

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

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

15
func (tp *provider) TestBlock(t *testing.T) {
16 17 18 19 20 21 22
	tp.hasApi(t, func(api coreiface.CoreAPI) error {
		if api.Block() == nil {
			return apiNotImplemented
		}
		return nil
	})

23 24 25 26 27 28
	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
29
	t.Run("TestBlockPin", tp.TestBlockPin)
30 31
}

32
func (tp *provider) TestBlockPut(t *testing.T) {
33 34
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
35
	api, err := tp.makeAPI(ctx)
36
	if err != nil {
37
		t.Fatal(err)
38 39 40 41
	}

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

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

50
func (tp *provider) TestBlockPutFormat(t *testing.T) {
51 52
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
53
	api, err := tp.makeAPI(ctx)
54
	if err != nil {
55
		t.Fatal(err)
56 57 58 59
	}

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

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

68
func (tp *provider) TestBlockPutHash(t *testing.T) {
69 70
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
71
	api, err := tp.makeAPI(ctx)
72
	if err != nil {
73
		t.Fatal(err)
74 75 76 77 78 79 80 81 82 83 84 85
	}

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

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

86
func (tp *provider) TestBlockGet(t *testing.T) {
87 88
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
89
	api, err := tp.makeAPI(ctx)
90
	if err != nil {
91
		t.Fatal(err)
92 93 94 95
	}

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

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

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
113
	p := coreiface.ParsePath("/ipfs/" + res.Path().Cid().String())
114 115 116 117 118 119 120 121 122 123

	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")
	}
}

124
func (tp *provider) TestBlockRm(t *testing.T) {
125 126
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
127
	api, err := tp.makeAPI(ctx)
128
	if err != nil {
129
		t.Fatal(err)
130 131 132 133
	}

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

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

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

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

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

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

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

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

178
func (tp *provider) TestBlockStat(t *testing.T) {
179 180
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
181
	api, err := tp.makeAPI(ctx)
182
	if err != nil {
183
		t.Fatal(err)
184 185 186 187
	}

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

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

	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
204 205 206 207 208 209

func (tp *provider) TestBlockPin(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	api, err := tp.makeAPI(ctx)
	if err != nil {
210
		t.Fatal(err)
Łukasz Magiera's avatar
Łukasz Magiera committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	}

	_, 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)
	}

	pins, err := api.Pin().Ls(ctx)
	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")
	}
}