block.go 4.31 KB
Newer Older
1
package tests
2 3 4 5 6 7 8 9 10 11 12 13 14

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

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
	opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

	mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
)

15 16 17 18 19 20 21
func (tp *provider) TestBlock(t *testing.T) {
	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)
22 23
}

24
func (tp *provider) TestBlockPut(t *testing.T) {
25 26
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
27
	api, err := tp.makeAPI(ctx)
28
	if err != nil {
29
		t.Fatal(err)
30 31 32 33
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
	if err != nil {
34
		t.Fatal(err)
35 36 37 38 39 40 41
	}

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

42
func (tp *provider) TestBlockPutFormat(t *testing.T) {
43 44
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
45
	api, err := tp.makeAPI(ctx)
46 47 48 49 50 51
	if err != nil {
		t.Error(err)
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("cbor"))
	if err != nil {
52
		t.Fatal(err)
53 54 55 56 57 58 59
	}

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

60
func (tp *provider) TestBlockPutHash(t *testing.T) {
61 62
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
63
	api, err := tp.makeAPI(ctx)
64 65 66 67 68 69 70 71 72 73 74 75 76 77
	if err != nil {
		t.Error(err)
	}

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

78
func (tp *provider) TestBlockGet(t *testing.T) {
79 80
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
81
	api, err := tp.makeAPI(ctx)
82 83 84 85 86 87
	if err != nil {
		t.Error(err)
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
	if err != nil {
88
		t.Fatal(err)
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
	}

	r, err := api.Block().Get(ctx, res.Path())
	if err != nil {
		t.Error(err)
	}

	d, err := ioutil.ReadAll(r)
	if err != nil {
		t.Error(err)
	}

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

	p, err := coreiface.ParsePath("/ipfs/" + res.Path().Cid().String())
	if err != nil {
		t.Error(err)
	}

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

119
func (tp *provider) TestBlockRm(t *testing.T) {
120 121
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
122
	api, err := tp.makeAPI(ctx)
123 124 125 126 127 128
	if err != nil {
		t.Error(err)
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
	if err != nil {
129
		t.Fatal(err)
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 163 164 165 166 167 168 169 170 171 172
	}

	r, err := api.Block().Get(ctx, res.Path())
	if err != nil {
		t.Error(err)
	}

	d, err := ioutil.ReadAll(r)
	if err != nil {
		t.Error(err)
	}

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

	err = api.Block().Rm(ctx, res.Path())
	if err != nil {
		t.Error(err)
	}

	_, err = api.Block().Get(ctx, res.Path())
	if err == nil {
		t.Error("expected err to exist")
	}
	if err.Error() != "blockservice: key not found" {
		t.Errorf("unexpected error; %s", err.Error())
	}

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

	err = api.Block().Rm(ctx, res.Path(), opt.Block.Force(true))
	if err != nil {
		t.Error(err)
	}
}

173
func (tp *provider) TestBlockStat(t *testing.T) {
174 175
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
176
	api, err := tp.makeAPI(ctx)
177 178 179 180 181 182
	if err != nil {
		t.Error(err)
	}

	res, err := api.Block().Put(ctx, strings.NewReader(`Hello`))
	if err != nil {
183
		t.Fatal(err)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	}

	stat, err := api.Block().Stat(ctx, res.Path())
	if err != nil {
		t.Error(err)
	}

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

	if stat.Size() != len("Hello") {
		t.Error("length doesn't match")
	}
}