Skip to content

Commit

Permalink
coreapi: block tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Jan 8, 2018
1 parent e50f5cf commit c8ae0d9
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

mh "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
blocks "gx/ipfs/QmYsEQydGrsxNZfAiskvQ76N2xE9hDQtSAkRSynwMiUK3c/go-block-format"
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
)
Expand Down Expand Up @@ -44,7 +45,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
if !ok {
return nil, fmt.Errorf("unrecognized format: %s", settings.Codec)
}
if settings.Codec == "v0" {
if settings.Codec == "v0" && settings.MhType == mh.SHA2_256 {
pref.Version = 0
}
pref.Codec = formatval
Expand Down Expand Up @@ -93,7 +94,11 @@ func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.Bl
}

select {
case res := <-out:
case res, ok := <-out:
if !ok {
return nil
}

remBlock, ok := res.(*util.RemovedBlock)
if !ok {
return errors.New("got unexpected output from util.RmBlocks")
Expand Down
167 changes: 167 additions & 0 deletions core/coreapi/block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package coreapi_test

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

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

func TestBlockPut(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

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

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

func TestBlockPutFormat(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), api.Block().WithFormat("cbor"))
if err != nil {
t.Error(err)
}

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

func TestBlockPutHash(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

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

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

func TestBlockGet(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

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

r, err := api.Block().Get(ctx, res)
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")
}
}

func TestBlockRm(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

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

r, err := api.Block().Get(ctx, res)
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)
if err != nil {
t.Error(err)
}

_, err = api.Block().Get(ctx, res)
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)
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, api.Block().WithForce(true))
if err != nil {
t.Error(err)
}
}

func TestBlockStat(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

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

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

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

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

0 comments on commit c8ae0d9

Please sign in to comment.