Commit a479105a authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi: move tests to interface subpackage

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent a1cf89b7
package tests_test
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"
)
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.Path().Cid().String() != "QmPyo15ynbVrSTVdJL9th7JysHaAbXt9dM9tXk1bMHbRtk" {
t.Errorf("got wrong cid: %s", res.Path().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`), opt.Block.Format("cbor"))
if err != nil {
t.Error(err)
}
if res.Path().Cid().String() != "zdpuAn4amuLWo8Widi5v6VQpuo2dnpnwbVE3oB6qqs7mDSeoa" {
t.Errorf("got wrong cid: %s", res.Path().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`), 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())
}
}
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`), opt.Block.Hash(mh.KECCAK_512, -1))
if err != nil {
t.Error(err)
}
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")
}
}
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.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)
}
}
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.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")
}
}
package tests_test
import (
"context"
"path"
"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"
)
var (
treeExpected = map[string]struct{}{
"a": {},
"b": {},
"c": {},
"c/d": {},
"c/e": {},
}
)
func TestPut(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`))
if err != nil {
t.Error(err)
}
if res.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
t.Errorf("got wrong cid: %s", res.Cid().String())
}
}
func TestPutWithHash(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), opt.Dag.Hash(mh.ID, -1))
if err != nil {
t.Error(err)
}
if res.Cid().String() != "z5hRLNd2sv4z1c" {
t.Errorf("got wrong cid: %s", res.Cid().String())
}
}
func TestPath(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`))
if err != nil {
t.Error(err)
}
res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub.Cid().String()+`"}}`))
if err != nil {
t.Error(err)
}
p, err := coreiface.ParsePath(path.Join(res.Cid().String(), "lnk"))
if err != nil {
t.Error(err)
}
nd, err := api.Dag().Get(ctx, p)
if err != nil {
t.Error(err)
}
if nd.Cid().String() != sub.Cid().String() {
t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub.Cid().String())
}
}
func TestTree(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
c, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`))
if err != nil {
t.Error(err)
}
res, err := api.Dag().Get(ctx, c)
if err != nil {
t.Error(err)
}
lst := res.Tree("", -1)
if len(lst) != len(treeExpected) {
t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
}
for _, ent := range lst {
if _, ok := treeExpected[ent]; !ok {
t.Errorf("unexpected tree entry %s", ent)
}
}
}
func TestBatch(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
batch := api.Dag().Batch(ctx)
c, err := batch.Put(ctx, strings.NewReader(`"Hello"`))
if err != nil {
t.Error(err)
}
if c.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
t.Errorf("got wrong cid: %s", c.Cid().String())
}
_, err = api.Dag().Get(ctx, c)
if err == nil || err.Error() != "merkledag: not found" {
t.Error(err)
}
if err := batch.Commit(ctx); err != nil {
t.Error(err)
}
_, err = api.Dag().Get(ctx, c)
if err != nil {
t.Error(err)
}
}
package tests_test
import (
"context"
"io"
"testing"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)
func TestDhtFindPeer(t *testing.T) {
ctx := context.Background()
apis, err := makeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
}
self0, err := apis[0].Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
pi, err := apis[2].Dht().FindPeer(ctx, self0.ID())
if err != nil {
t.Fatal(err)
}
if pi.Addrs[0].String() != "/ip4/127.0.0.1/tcp/4001" {
t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String())
}
self2, err := apis[2].Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
pi, err = apis[1].Dht().FindPeer(ctx, self2.ID())
if err != nil {
t.Fatal(err)
}
if pi.Addrs[0].String() != "/ip4/127.0.2.1/tcp/4001" {
t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String())
}
}
func TestDhtFindProviders(t *testing.T) {
ctx := context.Background()
apis, err := makeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
}
p, err := addTestObject(ctx, apis[0])
if err != nil {
t.Fatal(err)
}
out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}
provider := <-out
self0, err := apis[0].Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if provider.ID.String() != self0.ID().String() {
t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String())
}
}
func TestDhtProvide(t *testing.T) {
ctx := context.Background()
apis, err := makeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
}
off0, err := apis[0].WithOptions(options.Api.Offline(true))
if err != nil {
t.Fatal(err)
}
s, err := off0.Block().Put(ctx, &io.LimitedReader{R: rnd, N: 4092})
if err != nil {
t.Fatal(err)
}
p := s.Path()
out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}
provider := <-out
self0, err := apis[0].Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if provider.ID.String() != "<peer.ID >" {
t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String())
}
err = apis[0].Dht().Provide(ctx, p)
if err != nil {
t.Fatal(err)
}
out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}
provider = <-out
if provider.ID.String() != self0.ID().String() {
t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String())
}
}
package tests_test
import (
"context"
"strings"
"testing"
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)
func TestListSelf(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
return
}
keys, err := api.Key().List(ctx)
if err != nil {
t.Fatalf("failed to list keys: %s", err)
return
}
if len(keys) != 1 {
t.Fatalf("there should be 1 key (self), got %d", len(keys))
return
}
if keys[0].Name() != "self" {
t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name())
}
if keys[0].Path().String() != "/ipns/"+testPeerID {
t.Errorf("expected the key to have path '/ipns/%s', got '%s'", testPeerID, keys[0].Path().String())
}
}
func TestRenameSelf(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
return
}
_, _, err = api.Key().Rename(ctx, "self", "foo")
if err == nil {
t.Error("expected error to not be nil")
} else {
if err.Error() != "cannot rename key with name 'self'" {
t.Fatalf("expected error 'cannot rename key with name 'self'', got '%s'", err.Error())
}
}
_, _, err = api.Key().Rename(ctx, "self", "foo", opt.Key.Force(true))
if err == nil {
t.Error("expected error to not be nil")
} else {
if err.Error() != "cannot rename key with name 'self'" {
t.Fatalf("expected error 'cannot rename key with name 'self'', got '%s'", err.Error())
}
}
}
func TestRemoveSelf(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
return
}
_, err = api.Key().Remove(ctx, "self")
if err == nil {
t.Error("expected error to not be nil")
} else {
if err.Error() != "cannot remove key with name 'self'" {
t.Fatalf("expected error 'cannot remove key with name 'self'', got '%s'", err.Error())
}
}
}
func TestGenerate(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
k, err := api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
if k.Name() != "foo" {
t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
}
if !strings.HasPrefix(k.Path().String(), "/ipns/Qm") {
t.Errorf("expected the key to be prefixed with '/ipns/Qm', got '%s'", k.Path().String())
}
}
func TestGenerateSize(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
k, err := api.Key().Generate(ctx, "foo", opt.Key.Size(1024))
if err != nil {
t.Fatal(err)
return
}
if k.Name() != "foo" {
t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
}
if !strings.HasPrefix(k.Path().String(), "/ipns/Qm") {
t.Errorf("expected the key to be prefixed with '/ipns/Qm', got '%s'", k.Path().String())
}
}
func TestGenerateType(t *testing.T) {
ctx := context.Background()
t.Skip("disabled until libp2p/specs#111 is fixed")
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
k, err := api.Key().Generate(ctx, "bar", opt.Key.Type(opt.Ed25519Key))
if err != nil {
t.Fatal(err)
return
}
if k.Name() != "bar" {
t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
}
// Expected to be an inlined identity hash.
if !strings.HasPrefix(k.Path().String(), "/ipns/12") {
t.Errorf("expected the key to be prefixed with '/ipns/12', got '%s'", k.Path().String())
}
}
func TestGenerateExisting(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
_, err = api.Key().Generate(ctx, "foo")
if err == nil {
t.Error("expected error to not be nil")
} else {
if err.Error() != "key with name 'foo' already exists" {
t.Fatalf("expected error 'key with name 'foo' already exists', got '%s'", err.Error())
}
}
_, err = api.Key().Generate(ctx, "self")
if err == nil {
t.Error("expected error to not be nil")
} else {
if err.Error() != "cannot create key with name 'self'" {
t.Fatalf("expected error 'cannot create key with name 'self'', got '%s'", err.Error())
}
}
}
func TestList(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
l, err := api.Key().List(ctx)
if err != nil {
t.Fatal(err)
return
}
if len(l) != 2 {
t.Fatalf("expected to get 2 keys, got %d", len(l))
return
}
if l[0].Name() != "self" {
t.Fatalf("expected key 0 to be called 'self', got '%s'", l[0].Name())
return
}
if l[1].Name() != "foo" {
t.Fatalf("expected key 1 to be called 'foo', got '%s'", l[1].Name())
return
}
if !strings.HasPrefix(l[0].Path().String(), "/ipns/Qm") {
t.Fatalf("expected key 0 to be prefixed with '/ipns/Qm', got '%s'", l[0].Name())
return
}
if !strings.HasPrefix(l[1].Path().String(), "/ipns/Qm") {
t.Fatalf("expected key 1 to be prefixed with '/ipns/Qm', got '%s'", l[1].Name())
return
}
}
func TestRename(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
k, overwrote, err := api.Key().Rename(ctx, "foo", "bar")
if err != nil {
t.Fatal(err)
return
}
if overwrote {
t.Error("overwrote should be false")
}
if k.Name() != "bar" {
t.Errorf("returned key should be called 'bar', got '%s'", k.Name())
}
}
func TestRenameToSelf(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
_, _, err = api.Key().Rename(ctx, "foo", "self")
if err == nil {
t.Error("expected error to not be nil")
} else {
if err.Error() != "cannot overwrite key with name 'self'" {
t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error())
}
}
}
func TestRenameToSelfForce(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
_, _, err = api.Key().Rename(ctx, "foo", "self", opt.Key.Force(true))
if err == nil {
t.Error("expected error to not be nil")
} else {
if err.Error() != "cannot overwrite key with name 'self'" {
t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error())
}
}
}
func TestRenameOverwriteNoForce(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
_, err = api.Key().Generate(ctx, "bar")
if err != nil {
t.Fatal(err)
return
}
_, _, err = api.Key().Rename(ctx, "foo", "bar")
if err == nil {
t.Error("expected error to not be nil")
} else {
if err.Error() != "key by that name already exists, refusing to overwrite" {
t.Fatalf("expected error 'key by that name already exists, refusing to overwrite', got '%s'", err.Error())
}
}
}
func TestRenameOverwrite(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
kfoo, err := api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
_, err = api.Key().Generate(ctx, "bar")
if err != nil {
t.Fatal(err)
return
}
k, overwrote, err := api.Key().Rename(ctx, "foo", "bar", opt.Key.Force(true))
if err != nil {
t.Fatal(err)
return
}
if !overwrote {
t.Error("overwrote should be true")
}
if k.Name() != "bar" {
t.Errorf("returned key should be called 'bar', got '%s'", k.Name())
}
if k.Path().String() != kfoo.Path().String() {
t.Errorf("k and kfoo should have equal paths, '%s'!='%s'", k.Path().String(), kfoo.Path().String())
}
}
func TestRenameSameNameNoForce(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
k, overwrote, err := api.Key().Rename(ctx, "foo", "foo")
if err != nil {
t.Fatal(err)
return
}
if overwrote {
t.Error("overwrote should be false")
}
if k.Name() != "foo" {
t.Errorf("returned key should be called 'foo', got '%s'", k.Name())
}
}
func TestRenameSameName(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
k, overwrote, err := api.Key().Rename(ctx, "foo", "foo", opt.Key.Force(true))
if err != nil {
t.Fatal(err)
return
}
if overwrote {
t.Error("overwrote should be false")
}
if k.Name() != "foo" {
t.Errorf("returned key should be called 'foo', got '%s'", k.Name())
}
}
func TestRemove(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
k, err := api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
l, err := api.Key().List(ctx)
if err != nil {
t.Fatal(err)
return
}
if len(l) != 2 {
t.Fatalf("expected to get 2 keys, got %d", len(l))
return
}
p, err := api.Key().Remove(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}
if k.Path().String() != p.Path().String() {
t.Errorf("k and p should have equal paths, '%s'!='%s'", k.Path().String(), p.Path().String())
}
l, err = api.Key().List(ctx)
if err != nil {
t.Fatal(err)
return
}
if len(l) != 1 {
t.Fatalf("expected to get 1 key, got %d", len(l))
return
}
if l[0].Name() != "self" {
t.Errorf("expected the key to be called 'self', got '%s'", l[0].Name())
}
}
package tests_test
import (
"context"
"io"
"math/rand"
"path"
"testing"
"time"
"gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
ipath "gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)
var rnd = rand.New(rand.NewSource(0x62796532303137))
func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) {
return api.Unixfs().Add(ctx, files.NewReaderFile(&io.LimitedReader{R: rnd, N: 4092}))
}
func appendPath(p coreiface.Path, sub string) coreiface.Path {
p, err := coreiface.ParsePath(path.Join(p.String(), sub))
if err != nil {
panic(err)
}
return p
}
func TestPublishResolve(t *testing.T) {
ctx := context.Background()
init := func() (coreiface.CoreAPI, coreiface.Path) {
apis, err := makeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
return nil, nil
}
api := apis[0]
p, err := addTestObject(ctx, api)
if err != nil {
t.Fatal(err)
return nil, nil
}
return api, p
}
run := func(t *testing.T, ropts []opt.NameResolveOption) {
t.Run("basic", func(t *testing.T) {
api, p := init()
e, err := api.Name().Publish(ctx, p)
if err != nil {
t.Fatal(err)
}
self, err := api.Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
}
if e.Value().String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...)
if err != nil {
t.Fatal(err)
}
if resPath.String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
}
})
t.Run("publishPath", func(t *testing.T) {
api, p := init()
e, err := api.Name().Publish(ctx, appendPath(p, "/test"))
if err != nil {
t.Fatal(err)
}
self, err := api.Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
}
if e.Value().String() != p.String()+"/test" {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...)
if err != nil {
t.Fatal(err)
}
if resPath.String() != p.String()+"/test" {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test")
}
})
t.Run("revolvePath", func(t *testing.T) {
api, p := init()
e, err := api.Name().Publish(ctx, p)
if err != nil {
t.Fatal(err)
}
self, err := api.Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
}
if e.Value().String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
resPath, err := api.Name().Resolve(ctx, e.Name()+"/test", ropts...)
if err != nil {
t.Fatal(err)
}
if resPath.String() != p.String()+"/test" {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test")
}
})
t.Run("publishRevolvePath", func(t *testing.T) {
api, p := init()
e, err := api.Name().Publish(ctx, appendPath(p, "/a"))
if err != nil {
t.Fatal(err)
}
self, err := api.Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
}
if e.Value().String() != p.String()+"/a" {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
resPath, err := api.Name().Resolve(ctx, e.Name()+"/b", ropts...)
if err != nil {
t.Fatal(err)
}
if resPath.String() != p.String()+"/a/b" {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/a/b")
}
})
}
t.Run("default", func(t *testing.T) {
run(t, []opt.NameResolveOption{})
})
t.Run("nocache", func(t *testing.T) {
run(t, []opt.NameResolveOption{opt.Name.Cache(false)})
})
}
func TestBasicPublishResolveKey(t *testing.T) {
ctx := context.Background()
apis, err := makeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
}
api := apis[0]
k, err := api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
}
p, err := addTestObject(ctx, api)
if err != nil {
t.Fatal(err)
}
e, err := api.Name().Publish(ctx, p, opt.Name.Key(k.Name()))
if err != nil {
t.Fatal(err)
}
if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() {
t.Errorf("expected e.Name to equal '%s', got '%s'", e.Name(), k.Path().String())
}
if e.Value().String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
resPath, err := api.Name().Resolve(ctx, e.Name())
if err != nil {
t.Fatal(err)
}
if resPath.String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
}
}
func TestBasicPublishResolveTimeout(t *testing.T) {
t.Skip("ValidTime doesn't appear to work at this time resolution")
ctx := context.Background()
apis, err := makeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
}
api := apis[0]
p, err := addTestObject(ctx, api)
if err != nil {
t.Fatal(err)
}
e, err := api.Name().Publish(ctx, p, opt.Name.ValidTime(time.Millisecond*100))
if err != nil {
t.Fatal(err)
}
self, err := api.Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if e.Name() != self.ID().Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
}
if e.Value().String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}
time.Sleep(time.Second)
_, err = api.Name().Resolve(ctx, e.Name())
if err == nil {
t.Fatal("Expected an error")
}
}
//TODO: When swarm api is created, add multinode tests
package tests_test
import (
"bytes"
"context"
"encoding/hex"
"io/ioutil"
"strings"
"testing"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)
func TestNew(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
emptyNode, err := api.Object().New(ctx)
if err != nil {
t.Fatal(err)
}
dirNode, err := api.Object().New(ctx, opt.Object.Type("unixfs-dir"))
if err != nil {
t.Fatal(err)
}
if emptyNode.String() != "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n" {
t.Errorf("Unexpected emptyNode path: %s", emptyNode.String())
}
if dirNode.String() != "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" {
t.Errorf("Unexpected dirNode path: %s", dirNode.String())
}
}
func TestObjectPut(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"YmFy"}`), opt.Object.DataType("base64")) //bar
if err != nil {
t.Fatal(err)
}
pbBytes, err := hex.DecodeString("0a0362617a")
if err != nil {
t.Fatal(err)
}
p3, err := api.Object().Put(ctx, bytes.NewReader(pbBytes), opt.Object.InputEnc("protobuf"))
if err != nil {
t.Fatal(err)
}
if p1.String() != "/ipfs/QmQeGyS87nyijii7kFt1zbe4n2PsXTFimzsdxyE9qh9TST" {
t.Errorf("unexpected path: %s", p1.String())
}
if p2.String() != "/ipfs/QmNeYRbCibmaMMK6Du6ChfServcLqFvLJF76PzzF76SPrZ" {
t.Errorf("unexpected path: %s", p2.String())
}
if p3.String() != "/ipfs/QmZreR7M2t7bFXAdb1V5FtQhjk4t36GnrvueLJowJbQM9m" {
t.Errorf("unexpected path: %s", p3.String())
}
}
func TestObjectGet(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
nd, err := api.Object().Get(ctx, p1)
if err != nil {
t.Fatal(err)
}
if string(nd.RawData()[len(nd.RawData())-3:]) != "foo" {
t.Fatal("got non-matching data")
}
}
func TestObjectData(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
r, err := api.Object().Data(ctx, p1)
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
if string(data) != "foo" {
t.Fatal("got non-matching data")
}
}
func TestObjectLinks(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().Put(ctx, strings.NewReader(`{"Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`"}]}`))
if err != nil {
t.Fatal(err)
}
links, err := api.Object().Links(ctx, p2)
if err != nil {
t.Fatal(err)
}
if len(links) != 1 {
t.Errorf("unexpected number of links: %d", len(links))
}
if links[0].Cid.String() != p1.Cid().String() {
t.Fatal("cids didn't batch")
}
if links[0].Name != "bar" {
t.Fatal("unexpected link name")
}
}
func TestObjectStat(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`))
if err != nil {
t.Fatal(err)
}
stat, err := api.Object().Stat(ctx, p2)
if err != nil {
t.Fatal(err)
}
if stat.Cid.String() != p2.Cid().String() {
t.Error("unexpected stat.Cid")
}
if stat.NumLinks != 1 {
t.Errorf("unexpected stat.NumLinks")
}
if stat.BlockSize != 51 {
t.Error("unexpected stat.BlockSize")
}
if stat.LinksSize != 47 {
t.Errorf("unexpected stat.LinksSize: %d", stat.LinksSize)
}
if stat.DataSize != 4 {
t.Error("unexpected stat.DataSize")
}
if stat.CumulativeSize != 54 {
t.Error("unexpected stat.DataSize")
}
}
func TestObjectAddLink(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`))
if err != nil {
t.Fatal(err)
}
p3, err := api.Object().AddLink(ctx, p2, "abc", p2)
if err != nil {
t.Fatal(err)
}
links, err := api.Object().Links(ctx, p3)
if err != nil {
t.Fatal(err)
}
if len(links) != 2 {
t.Errorf("unexpected number of links: %d", len(links))
}
if links[0].Name != "abc" {
t.Errorf("unexpected link 0 name: %s", links[0].Name)
}
if links[1].Name != "bar" {
t.Errorf("unexpected link 1 name: %s", links[1].Name)
}
}
func TestObjectAddLinkCreate(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`))
if err != nil {
t.Fatal(err)
}
p3, err := api.Object().AddLink(ctx, p2, "abc/d", p2)
if err == nil {
t.Fatal("expected an error")
}
if err.Error() != "no link by that name" {
t.Fatalf("unexpected error: %s", err.Error())
}
p3, err = api.Object().AddLink(ctx, p2, "abc/d", p2, opt.Object.Create(true))
if err != nil {
t.Fatal(err)
}
links, err := api.Object().Links(ctx, p3)
if err != nil {
t.Fatal(err)
}
if len(links) != 2 {
t.Errorf("unexpected number of links: %d", len(links))
}
if links[0].Name != "abc" {
t.Errorf("unexpected link 0 name: %s", links[0].Name)
}
if links[1].Name != "bar" {
t.Errorf("unexpected link 1 name: %s", links[1].Name)
}
}
func TestObjectRmLink(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`))
if err != nil {
t.Fatal(err)
}
p3, err := api.Object().RmLink(ctx, p2, "bar")
if err != nil {
t.Fatal(err)
}
links, err := api.Object().Links(ctx, p3)
if err != nil {
t.Fatal(err)
}
if len(links) != 0 {
t.Errorf("unexpected number of links: %d", len(links))
}
}
func TestObjectAddData(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().AppendData(ctx, p1, strings.NewReader("bar"))
if err != nil {
t.Fatal(err)
}
r, err := api.Object().Data(ctx, p2)
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(r)
if string(data) != "foobar" {
t.Error("unexpected data")
}
}
func TestObjectSetData(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().SetData(ctx, p1, strings.NewReader("bar"))
if err != nil {
t.Fatal(err)
}
r, err := api.Object().Data(ctx, p2)
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(r)
if string(data) != "bar" {
t.Error("unexpected data")
}
}
func TestDiffTest(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
if err != nil {
t.Fatal(err)
}
p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bar"}`))
if err != nil {
t.Fatal(err)
}
changes, err := api.Object().Diff(ctx, p1, p2)
if err != nil {
t.Fatal(err)
}
if len(changes) != 1 {
t.Fatal("unexpected changes len")
}
if changes[0].Type != iface.DiffMod {
t.Fatal("unexpected change type")
}
if changes[0].Before.String() != p1.String() {
t.Fatal("unexpected before path")
}
if changes[0].After.String() != p2.String() {
t.Fatal("unexpected before path")
}
}
package tests_test
import (
"context"
"strings"
"testing"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)
func TestMutablePath(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
// get self /ipns path
keys, err := api.Key().List(ctx)
if err != nil {
t.Fatal(err)
}
if !keys[0].Path().Mutable() {
t.Error("expected self /ipns path to be mutable")
}
blk, err := api.Block().Put(ctx, strings.NewReader(`foo`))
if err != nil {
t.Error(err)
}
if blk.Path().Mutable() {
t.Error("expected /ipld path to be immutable")
}
}
func TestPathRemainder(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
if err != nil {
t.Fatal(err)
}
p1, err := coreiface.ParsePath(obj.String() + "/foo/bar")
if err != nil {
t.Error(err)
}
rp1, err := api.ResolvePath(ctx, p1)
if err != nil {
t.Fatal(err)
}
if rp1.Remainder() != "foo/bar" {
t.Error("expected to get path remainder")
}
}
func TestEmptyPathRemainder(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
if err != nil {
t.Fatal(err)
}
if obj.Remainder() != "" {
t.Error("expected the resolved path to not have a remainder")
}
p1, err := coreiface.ParsePath(obj.String())
if err != nil {
t.Error(err)
}
rp1, err := api.ResolvePath(ctx, p1)
if err != nil {
t.Fatal(err)
}
if rp1.Remainder() != "" {
t.Error("expected the resolved path to not have a remainder")
}
}
func TestInvalidPathRemainder(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
if err != nil {
t.Fatal(err)
}
p1, err := coreiface.ParsePath(obj.String() + "/bar/baz")
if err != nil {
t.Error(err)
}
_, err = api.ResolvePath(ctx, p1)
if err == nil || err.Error() != "no such link found" {
t.Fatalf("unexpected error: %s", err)
}
}
func TestPathRoot(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
blk, err := api.Block().Put(ctx, strings.NewReader(`foo`), options.Block.Format("raw"))
if err != nil {
t.Error(err)
}
obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"/": "`+blk.Path().Cid().String()+`"}}`))
if err != nil {
t.Fatal(err)
}
p1, err := coreiface.ParsePath(obj.String() + "/foo")
if err != nil {
t.Error(err)
}
rp, err := api.ResolvePath(ctx, p1)
if err != nil {
t.Fatal(err)
}
if rp.Root().String() != obj.Cid().String() {
t.Error("unexpected path root")
}
if rp.Cid().String() != blk.Path().Cid().String() {
t.Error("unexpected path cid")
}
}
package tests_test
import (
"context"
"strings"
"testing"
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)
func TestPinAdd(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
p, err := api.Unixfs().Add(ctx, strFile("foo")())
if err != nil {
t.Error(err)
}
err = api.Pin().Add(ctx, p)
if err != nil {
t.Error(err)
}
}
func TestPinSimple(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
p, err := api.Unixfs().Add(ctx, strFile("foo")())
if err != nil {
t.Error(err)
}
err = api.Pin().Add(ctx, p)
if err != nil {
t.Error(err)
}
list, err := api.Pin().Ls(ctx)
if err != nil {
t.Fatal(err)
}
if len(list) != 1 {
t.Errorf("unexpected pin list len: %d", len(list))
}
if list[0].Path().Cid().String() != p.Cid().String() {
t.Error("paths don't match")
}
if list[0].Type() != "recursive" {
t.Error("unexpected pin type")
}
err = api.Pin().Rm(ctx, p)
if err != nil {
t.Fatal(err)
}
list, err = api.Pin().Ls(ctx)
if err != nil {
t.Fatal(err)
}
if len(list) != 0 {
t.Errorf("unexpected pin list len: %d", len(list))
}
}
func TestPinRecursive(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
p0, err := api.Unixfs().Add(ctx, strFile("foo")())
if err != nil {
t.Error(err)
}
p1, err := api.Unixfs().Add(ctx, strFile("bar")())
if err != nil {
t.Error(err)
}
p2, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+p0.Cid().String()+`"}}`))
if err != nil {
t.Error(err)
}
p3, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+p1.Cid().String()+`"}}`))
if err != nil {
t.Error(err)
}
err = api.Pin().Add(ctx, p2)
if err != nil {
t.Error(err)
}
err = api.Pin().Add(ctx, p3, opt.Pin.Recursive(false))
if err != nil {
t.Error(err)
}
list, err := api.Pin().Ls(ctx)
if err != nil {
t.Fatal(err)
}
if len(list) != 3 {
t.Errorf("unexpected pin list len: %d", len(list))
}
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Direct())
if err != nil {
t.Fatal(err)
}
if len(list) != 1 {
t.Errorf("unexpected pin list len: %d", len(list))
}
if list[0].Path().String() != p3.String() {
t.Error("unexpected path")
}
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Recursive())
if err != nil {
t.Fatal(err)
}
if len(list) != 1 {
t.Errorf("unexpected pin list len: %d", len(list))
}
if list[0].Path().String() != p2.String() {
t.Error("unexpected path")
}
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Indirect())
if err != nil {
t.Fatal(err)
}
if len(list) != 1 {
t.Errorf("unexpected pin list len: %d", len(list))
}
if list[0].Path().Cid().String() != p0.Cid().String() {
t.Error("unexpected path")
}
res, err := api.Pin().Verify(ctx)
if err != nil {
t.Fatal(err)
}
n := 0
for r := range res {
if !r.Ok() {
t.Error("expected pin to be ok")
}
n++
}
if n != 1 {
t.Errorf("unexpected verify result count: %d", n)
}
//TODO: figure out a way to test verify without touching IpfsNode
/*
err = api.Block().Rm(ctx, p0, opt.Block.Force(true))
if err != nil {
t.Fatal(err)
}
res, err = api.Pin().Verify(ctx)
if err != nil {
t.Fatal(err)
}
n = 0
for r := range res {
if r.Ok() {
t.Error("expected pin to not be ok")
}
if len(r.BadNodes()) != 1 {
t.Fatalf("unexpected badNodes len")
}
if r.BadNodes()[0].Path().Cid().String() != p0.Cid().String() {
t.Error("unexpected badNode path")
}
if r.BadNodes()[0].Err().Error() != "merkledag: not found" {
t.Errorf("unexpected badNode error: %s", r.BadNodes()[0].Err().Error())
}
n++
}
if n != 1 {
t.Errorf("unexpected verify result count: %d", n)
}
*/
}
package tests_test
import (
"context"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
"testing"
"time"
)
func TestBasicPubSub(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
apis, err := makeAPISwarm(ctx, true, 2)
if err != nil {
t.Fatal(err)
}
sub, err := apis[0].PubSub().Subscribe(ctx, "testch")
if err != nil {
t.Fatal(err)
}
go func() {
tick := time.Tick(100 * time.Millisecond)
for {
err = apis[1].PubSub().Publish(ctx, "testch", []byte("hello world"))
if err != nil {
t.Fatal(err)
}
select {
case <-tick:
case <-ctx.Done():
return
}
}
}()
m, err := sub.Next(ctx)
if err != nil {
t.Fatal(err)
}
if string(m.Data()) != "hello world" {
t.Errorf("got invalid data: %s", string(m.Data()))
}
self1, err := apis[1].Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if m.From() != self1.ID() {
t.Errorf("m.From didn't match")
}
peers, err := apis[1].PubSub().Peers(ctx, options.PubSub.Topic("testch"))
if err != nil {
t.Fatal(err)
}
if len(peers) != 1 {
t.Fatalf("got incorrect number of peers: %d", len(peers))
}
self0, err := apis[0].Key().Self(ctx)
if err != nil {
t.Fatal(err)
}
if peers[0] != self0.ID() {
t.Errorf("peer didn't match")
}
peers, err = apis[1].PubSub().Peers(ctx, options.PubSub.Topic("nottestch"))
if err != nil {
t.Fatal(err)
}
if len(peers) != 0 {
t.Fatalf("got incorrect number of peers: %d", len(peers))
}
topics, err := apis[0].PubSub().Ls(ctx)
if err != nil {
t.Fatal(err)
}
if len(topics) != 1 {
t.Fatalf("got incorrect number of topics: %d", len(peers))
}
if topics[0] != "testch" {
t.Errorf("topic didn't match")
}
topics, err = apis[1].PubSub().Ls(ctx)
if err != nil {
t.Fatal(err)
}
if len(topics) != 0 {
t.Fatalf("got incorrect number of topics: %d", len(peers))
}
}
package tests_test
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
"io"
"io/ioutil"
"math"
"os"
"strconv"
"strings"
"sync"
"testing"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/keystore"
"github.com/ipfs/go-ipfs/repo"
ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
"gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p/p2p/net/mock"
cbor "gx/ipfs/QmRoARq3nkUb13HSKZGepCZSWe5GrVPwx7xURJGZ7KWv9V/go-ipld-cbor"
"gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
"gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
"gx/ipfs/Qmbvw7kpSM2p6rbQ57WGRhhqNfCiNGW6EKH4xgHLw4bsnB/go-unixfs"
"gx/ipfs/QmcZfkbgwwwH5ZLTQRHkSQBDiDqd3skY2eU6MZRgWuXcse/go-ipfs-config"
mdag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
"gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
syncds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/sync"
)
const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe"
// `echo -n 'hello, world!' | ipfs add`
var hello = "/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
var helloStr = "hello, world!"
// `echo -n | ipfs add`
var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) {
mn := mocknet.New(ctx)
nodes := make([]*core.IpfsNode, n)
apis := make([]coreiface.CoreAPI, n)
for i := 0; i < n; i++ {
var ident config.Identity
if fullIdentity {
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {
return nil, err
}
id, err := peer.IDFromPublicKey(pk)
if err != nil {
return nil, err
}
kbytes, err := sk.Bytes()
if err != nil {
return nil, err
}
ident = config.Identity{
PeerID: id.Pretty(),
PrivKey: base64.StdEncoding.EncodeToString(kbytes),
}
} else {
ident = config.Identity{
PeerID: testPeerID,
}
}
c := config.Config{}
c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)}
c.Identity = ident
r := &repo.Mock{
C: c,
D: syncds.MutexWrap(datastore.NewMapDatastore()),
K: keystore.NewMemKeystore(),
}
node, err := core.NewNode(ctx, &core.BuildCfg{
Repo: r,
Host: mock.MockHostOption(mn),
Online: fullIdentity,
ExtraOpts: map[string]bool{
"pubsub": true,
},
})
if err != nil {
return nil, err
}
nodes[i] = node
apis[i], err = coreapi.NewCoreAPI(node)
if err != nil {
return nil, err
}
}
err := mn.LinkAll()
if err != nil {
return nil, err
}
bsinf := core.BootstrapConfigWithPeers(
[]pstore.PeerInfo{
nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
},
)
for _, n := range nodes[1:] {
if err := n.Bootstrap(bsinf); err != nil {
return nil, err
}
}
return apis, nil
}
func makeAPI(ctx context.Context) (coreiface.CoreAPI, error) {
api, err := makeAPISwarm(ctx, false, 1)
if err != nil {
return nil, err
}
return api[0], nil
}
func strFile(data string) func() files.Node {
return func() files.Node {
return files.NewBytesFile([]byte(data))
}
}
func twoLevelDir() func() files.Node {
return func() files.Node {
return files.NewMapDirectory(map[string]files.Node{
"abc": files.NewMapDirectory(map[string]files.Node{
"def": files.NewBytesFile([]byte("world")),
}),
"bar": files.NewBytesFile([]byte("hello2")),
"foo": files.NewBytesFile([]byte("hello1")),
})
}
}
func flatDir() files.Node {
return files.NewMapDirectory(map[string]files.Node{
"bar": files.NewBytesFile([]byte("hello2")),
"foo": files.NewBytesFile([]byte("hello1")),
})
}
func wrapped(name string) func(f files.Node) files.Node {
return func(f files.Node) files.Node {
return files.NewMapDirectory(map[string]files.Node{
name: f,
})
}
}
func TestAdd(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
p := func(h string) coreiface.ResolvedPath {
c, err := cid.Parse(h)
if err != nil {
t.Fatal(err)
}
return coreiface.IpfsPath(c)
}
cases := []struct {
name string
data func() files.Node
expect func(files.Node) files.Node
apiOpts []options.ApiOption
path string
err string
wrap string
events []coreiface.AddEvent
opts []options.UnixfsAddOption
}{
// Simple cases
{
name: "simpleAdd",
data: strFile(helloStr),
path: hello,
opts: []options.UnixfsAddOption{},
},
{
name: "addEmpty",
data: strFile(""),
path: emptyFile,
},
// CIDv1 version / rawLeaves
{
name: "addCidV1",
data: strFile(helloStr),
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1)},
},
{
name: "addCidV1NoLeaves",
data: strFile(helloStr),
path: "/ipfs/zdj7WY4GbN8NDbTW1dfCShAQNVovams2xhq9hVCx5vXcjvT8g",
opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1), options.Unixfs.RawLeaves(false)},
},
// Non sha256 hash vs CID
{
name: "addCidSha3",
data: strFile(helloStr),
path: "/ipfs/zb2wwnYtXBxpndNABjtYxWAPt3cwWNRnc11iT63fvkYV78iRb",
opts: []options.UnixfsAddOption{options.Unixfs.Hash(mh.SHA3_256)},
},
{
name: "addCidSha3Cid0",
data: strFile(helloStr),
err: "CIDv0 only supports sha2-256",
opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(0), options.Unixfs.Hash(mh.SHA3_256)},
},
// Inline
{
name: "addInline",
data: strFile(helloStr),
path: "/ipfs/zaYomJdLndMku8P9LHngHB5w2CQ7NenLbv",
opts: []options.UnixfsAddOption{options.Unixfs.Inline(true)},
},
{
name: "addInlineLimit",
data: strFile(helloStr),
path: "/ipfs/zaYomJdLndMku8P9LHngHB5w2CQ7NenLbv",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.Inline(true)},
},
{
name: "addInlineZero",
data: strFile(""),
path: "/ipfs/z2yYDV",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(0), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true)},
},
{ //TODO: after coreapi add is used in `ipfs add`, consider making this default for inline
name: "addInlineRaw",
data: strFile(helloStr),
path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true)},
},
// Chunker / Layout
{
name: "addChunks",
data: strFile(strings.Repeat("aoeuidhtns", 200)),
path: "/ipfs/QmRo11d4QJrST47aaiGVJYwPhoNA4ihRpJ5WaxBWjWDwbX",
opts: []options.UnixfsAddOption{options.Unixfs.Chunker("size-4")},
},
{
name: "addChunksTrickle",
data: strFile(strings.Repeat("aoeuidhtns", 200)),
path: "/ipfs/QmNNhDGttafX3M1wKWixGre6PrLFGjnoPEDXjBYpTv93HP",
opts: []options.UnixfsAddOption{options.Unixfs.Chunker("size-4"), options.Unixfs.Layout(options.TrickleLayout)},
},
// Local
{
name: "addLocal", // better cases in sharness
data: strFile(helloStr),
path: hello,
apiOpts: []options.ApiOption{options.Api.Offline(true)},
},
{
name: "hashOnly", // test (non)fetchability
data: strFile(helloStr),
path: hello,
opts: []options.UnixfsAddOption{options.Unixfs.HashOnly(true)},
},
// multi file
{
name: "simpleDir",
data: flatDir,
wrap: "t",
path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp",
},
{
name: "twoLevelDir",
data: twoLevelDir(),
wrap: "t",
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
},
// wrapped
{
name: "addWrapped",
path: "/ipfs/QmVE9rNpj5doj7XHzp5zMUxD7BJgXEqx4pe3xZ3JBReWHE",
data: func() files.Node {
return files.NewBytesFile([]byte(helloStr))
},
wrap: "foo",
expect: wrapped("foo"),
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "addNotWrappedDirFile",
path: hello,
data: func() files.Node {
return files.NewBytesFile([]byte(helloStr))
},
wrap: "foo",
},
{
name: "stdinWrapped",
path: "/ipfs/QmU3r81oZycjHS9oaSHw37ootMFuFUw1DvMLKXPsezdtqU",
data: func() files.Node {
return files.NewBytesFile([]byte(helloStr))
},
expect: func(files.Node) files.Node {
return files.NewMapDirectory(map[string]files.Node{
"QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk": files.NewBytesFile([]byte(helloStr)),
})
},
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "stdinNamed",
path: "/ipfs/QmQ6cGBmb3ZbdrQW1MRm1RJnYnaxCqfssz7CrTa9NEhQyS",
data: func() files.Node {
rf, err := files.NewReaderPathFile(os.Stdin.Name(), ioutil.NopCloser(strings.NewReader(helloStr)), nil)
if err != nil {
panic(err)
}
return rf
},
expect: func(files.Node) files.Node {
return files.NewMapDirectory(map[string]files.Node{
"test": files.NewBytesFile([]byte(helloStr)),
})
},
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.StdinName("test")},
},
{
name: "twoLevelDirWrapped",
data: twoLevelDir(),
wrap: "t",
expect: wrapped("t"),
path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "twoLevelInlineHash",
data: twoLevelDir(),
wrap: "t",
expect: wrapped("t"),
path: "/ipfs/zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)},
},
// hidden
{
name: "hiddenFiles",
data: func() files.Node {
return files.NewMapDirectory(map[string]files.Node{
".bar": files.NewBytesFile([]byte("hello2")),
"bar": files.NewBytesFile([]byte("hello2")),
"foo": files.NewBytesFile([]byte("hello1")),
})
},
wrap: "t",
path: "/ipfs/QmehGvpf2hY196MzDFmjL8Wy27S4jbgGDUAhBJyvXAwr3g",
opts: []options.UnixfsAddOption{options.Unixfs.Hidden(true)},
},
{
name: "hiddenFileAlwaysAdded",
data: func() files.Node {
return files.NewBytesFile([]byte(helloStr))
},
wrap: ".foo",
path: hello,
},
{
name: "hiddenFilesNotAdded",
data: func() files.Node {
return files.NewMapDirectory(map[string]files.Node{
".bar": files.NewBytesFile([]byte("hello2")),
"bar": files.NewBytesFile([]byte("hello2")),
"foo": files.NewBytesFile([]byte("hello1")),
})
},
expect: func(files.Node) files.Node {
return flatDir()
},
wrap: "t",
path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp",
opts: []options.UnixfsAddOption{options.Unixfs.Hidden(false)},
},
// Events / Progress
{
name: "simpleAddEvent",
data: strFile(helloStr),
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
events: []coreiface.AddEvent{
{Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Path: p("zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd"), Size: strconv.Itoa(len(helloStr))},
},
opts: []options.UnixfsAddOption{options.Unixfs.RawLeaves(true)},
},
{
name: "silentAddEvent",
data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
},
wrap: "t",
opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)},
},
{
name: "dirAddEvents",
data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{
{Name: "t/abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"},
{Name: "t/bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"},
{Name: "t/foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"},
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
},
wrap: "t",
},
{
name: "progress1M",
data: func() files.Node {
return files.NewReaderFile(bytes.NewReader(bytes.Repeat([]byte{0}, 1000000)))
},
path: "/ipfs/QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD",
events: []coreiface.AddEvent{
{Name: "", Bytes: 262144},
{Name: "", Bytes: 524288},
{Name: "", Bytes: 786432},
{Name: "", Bytes: 1000000},
{Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Path: p("QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD"), Size: "1000256"},
},
wrap: "",
opts: []options.UnixfsAddOption{options.Unixfs.Progress(true)},
},
}
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// recursive logic
data := testCase.data()
if testCase.wrap != "" {
data = files.NewMapDirectory(map[string]files.Node{
testCase.wrap: data,
})
}
// handle events if relevant to test case
opts := testCase.opts
eventOut := make(chan interface{})
var evtWg sync.WaitGroup
if len(testCase.events) > 0 {
opts = append(opts, options.Unixfs.Events(eventOut))
evtWg.Add(1)
go func() {
defer evtWg.Done()
expected := testCase.events
for evt := range eventOut {
event, ok := evt.(*coreiface.AddEvent)
if !ok {
t.Fatal("unexpected event type")
}
if len(expected) < 1 {
t.Fatal("got more events than expected")
}
if expected[0].Size != event.Size {
t.Errorf("Event.Size didn't match, %s != %s", expected[0].Size, event.Size)
}
if expected[0].Name != event.Name {
t.Errorf("Event.Name didn't match, %s != %s", expected[0].Name, event.Name)
}
if expected[0].Path != nil && event.Path != nil {
if expected[0].Path.Cid().String() != event.Path.Cid().String() {
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path)
}
} else if event.Path != expected[0].Path {
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path)
}
if expected[0].Bytes != event.Bytes {
t.Errorf("Event.Bytes didn't match, %d != %d", expected[0].Bytes, event.Bytes)
}
expected = expected[1:]
}
if len(expected) > 0 {
t.Fatalf("%d event(s) didn't arrive", len(expected))
}
}()
}
tapi, err := api.WithOptions(testCase.apiOpts...)
if err != nil {
t.Fatal(err)
}
// Add!
p, err := tapi.Unixfs().Add(ctx, data, opts...)
close(eventOut)
evtWg.Wait()
if testCase.err != "" {
if err == nil {
t.Fatalf("expected an error: %s", testCase.err)
}
if err.Error() != testCase.err {
t.Fatalf("expected an error: '%s' != '%s'", err.Error(), testCase.err)
}
return
}
if err != nil {
t.Fatal(err)
}
if p.String() != testCase.path {
t.Errorf("expected path %s, got: %s", testCase.path, p)
}
// compare file structure with Unixfs().Get
var cmpFile func(origName string, orig files.Node, gotName string, got files.Node)
cmpFile = func(origName string, orig files.Node, gotName string, got files.Node) {
_, origDir := orig.(files.Directory)
_, gotDir := got.(files.Directory)
if origDir != gotDir {
t.Fatal("file type mismatch")
}
if origName != gotName {
t.Errorf("file name mismatch, orig='%s', got='%s'", origName, gotName)
}
if !gotDir {
defer orig.Close()
defer got.Close()
do, err := ioutil.ReadAll(orig.(files.File))
if err != nil {
t.Fatal(err)
}
dg, err := ioutil.ReadAll(got.(files.File))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(do, dg) {
t.Fatal("data not equal")
}
return
}
origIt := orig.(files.Directory).Entries()
gotIt := got.(files.Directory).Entries()
for {
if origIt.Next() {
if !gotIt.Next() {
t.Fatal("gotIt out of entries before origIt")
}
} else {
if gotIt.Next() {
t.Fatal("origIt out of entries before gotIt")
}
break
}
cmpFile(origIt.Name(), origIt.Node(), gotIt.Name(), gotIt.Node())
}
if origIt.Err() != nil {
t.Fatal(origIt.Err())
}
if gotIt.Err() != nil {
t.Fatal(gotIt.Err())
}
}
f, err := tapi.Unixfs().Get(ctx, p)
if err != nil {
t.Fatal(err)
}
orig := testCase.data()
if testCase.expect != nil {
orig = testCase.expect(orig)
}
cmpFile("", orig, "", f)
})
}
}
func TestAddPinned(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Unixfs().Add(ctx, strFile(helloStr)(), options.Unixfs.Pin(true))
if err != nil {
t.Error(err)
}
pins, err := api.Pin().Ls(ctx)
if len(pins) != 1 {
t.Fatalf("expected 1 pin, got %d", len(pins))
}
if pins[0].Path().String() != "/ipld/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk" {
t.Fatalf("got unexpected pin: %s", pins[0].Path().String())
}
}
func TestAddHashOnly(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
p, err := api.Unixfs().Add(ctx, strFile(helloStr)(), options.Unixfs.HashOnly(true))
if err != nil {
t.Error(err)
}
if p.String() != hello {
t.Errorf("unxepected path: %s", p.String())
}
_, err = api.Block().Get(ctx, p)
if err == nil {
t.Fatal("expected an error")
}
if err.Error() != "blockservice: key not found" {
t.Errorf("unxepected error: %s", err.Error())
}
}
func TestGetEmptyFile(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Fatal(err)
}
_, err = api.Unixfs().Add(ctx, files.NewBytesFile([]byte{}))
if err != nil {
t.Fatal(err)
}
emptyFilePath, err := coreiface.ParsePath(emptyFile)
if err != nil {
t.Fatal(err)
}
r, err := api.Unixfs().Get(ctx, emptyFilePath)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, 1) // non-zero so that Read() actually tries to read
n, err := io.ReadFull(r.(files.File), buf)
if err != nil && err != io.EOF {
t.Error(err)
}
if !bytes.HasPrefix(buf, []byte{0x00}) {
t.Fatalf("expected empty data, got [%s] [read=%d]", buf, n)
}
}
func TestGetDir(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
edir := unixfs.EmptyDirNode()
_, err = api.Dag().Put(ctx, bytes.NewReader(edir.RawData()), options.Dag.Codec(cid.DagProtobuf), options.Dag.InputEnc("raw"))
if err != nil {
t.Error(err)
}
p := coreiface.IpfsPath(edir.Cid())
emptyDir, err := api.Object().New(ctx, options.Object.Type("unixfs-dir"))
if err != nil {
t.Error(err)
}
if p.String() != coreiface.IpfsPath(emptyDir.Cid()).String() {
t.Fatalf("expected path %s, got: %s", emptyDir.Cid(), p.String())
}
r, err := api.Unixfs().Get(ctx, coreiface.IpfsPath(emptyDir.Cid()))
if err != nil {
t.Error(err)
}
if _, ok := r.(files.Directory); !ok {
t.Fatalf("expected a directory")
}
}
func TestGetNonUnixfs(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
nd := new(mdag.ProtoNode)
_, err = api.Dag().Put(ctx, bytes.NewReader(nd.RawData()), options.Dag.Codec(nd.CidBuilder().GetCodec()), options.Dag.InputEnc("raw"))
if err != nil {
t.Error(err)
}
_, err = api.Unixfs().Get(ctx, coreiface.IpfsPath(nd.Cid()))
if !strings.Contains(err.Error(), "proto: required field") {
t.Fatalf("expected protobuf error, got: %s", err)
}
}
func TestLs(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
r := strings.NewReader("content-of-file")
p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{
"0": files.NewMapDirectory(map[string]files.Node{
"name-of-file": files.NewReaderFile(r),
}),
}))
if err != nil {
t.Error(err)
}
links, err := api.Unixfs().Ls(ctx, p)
if err != nil {
t.Error(err)
}
if len(links) != 1 {
t.Fatalf("expected 1 link, got %d", len(links))
}
if links[0].Size != 23 {
t.Fatalf("expected size = 23, got %d", links[0].Size)
}
if links[0].Name != "name-of-file" {
t.Fatalf("expected name = name-of-file, got %s", links[0].Name)
}
if links[0].Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", links[0].Cid)
}
}
func TestEntriesExpired(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
r := strings.NewReader("content-of-file")
p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{
"0": files.NewMapDirectory(map[string]files.Node{
"name-of-file": files.NewReaderFile(r),
}),
}))
if err != nil {
t.Error(err)
}
ctx, cancel := context.WithCancel(ctx)
nd, err := api.Unixfs().Get(ctx, p)
if err != nil {
t.Error(err)
}
cancel()
it := files.ToDir(nd).Entries()
if it == nil {
t.Fatal("it was nil")
}
if it.Next() {
t.Fatal("Next succeeded")
}
if it.Err() != context.Canceled {
t.Fatalf("unexpected error %s", it.Err())
}
if it.Next() {
t.Fatal("Next succeeded")
}
}
func TestLsEmptyDir(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
_, err = api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{"0": files.NewSliceDirectory([]files.DirEntry{})}))
if err != nil {
t.Error(err)
}
emptyDir, err := api.Object().New(ctx, options.Object.Type("unixfs-dir"))
if err != nil {
t.Error(err)
}
links, err := api.Unixfs().Ls(ctx, coreiface.IpfsPath(emptyDir.Cid()))
if err != nil {
t.Error(err)
}
if len(links) != 0 {
t.Fatalf("expected 0 links, got %d", len(links))
}
}
// TODO(lgierth) this should test properly, with len(links) > 0
func TestLsNonUnixfs(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
nd, err := cbor.WrapObject(map[string]interface{}{"foo": "bar"}, math.MaxUint64, -1)
if err != nil {
t.Fatal(err)
}
_, err = api.Dag().Put(ctx, bytes.NewReader(nd.RawData()), options.Dag.Codec(cid.DagCBOR), options.Dag.InputEnc("raw"))
if err != nil {
t.Error(err)
}
links, err := api.Unixfs().Ls(ctx, coreiface.IpfsPath(nd.Cid()))
if err != nil {
t.Error(err)
}
if len(links) != 0 {
t.Fatalf("expected 0 links, got %d", len(links))
}
}
type closeTestF struct {
files.File
closed bool
t *testing.T
}
type closeTestD struct {
files.Directory
closed bool
t *testing.T
}
func (f *closeTestD) Close() error {
if f.closed {
f.t.Fatal("already closed")
}
f.closed = true
return nil
}
func (f *closeTestF) Close() error {
if f.closed {
f.t.Fatal("already closed")
}
f.closed = true
return nil
}
func TestAddCloses(t *testing.T) {
ctx := context.Background()
api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
n4 := &closeTestF{files.NewBytesFile([]byte("foo")), false, t}
d3 := &closeTestD{files.NewMapDirectory(map[string]files.Node{
"sub": n4,
}), false, t}
n2 := &closeTestF{files.NewBytesFile([]byte("bar")), false, t}
n1 := &closeTestF{files.NewBytesFile([]byte("baz")), false, t}
d0 := &closeTestD{files.NewMapDirectory(map[string]files.Node{
"a": d3,
"b": n1,
"c": n2,
}), false, t}
_, err = api.Unixfs().Add(ctx, d0)
if err != nil {
t.Error(err)
}
d0.Close() // Adder doesn't close top-level file
for i, n := range []*closeTestF{n1, n2, n4} {
if !n.closed {
t.Errorf("file %d not closed!", i)
}
}
for i, n := range []*closeTestD{d0, d3} {
if !n.closed {
t.Errorf("dir %d not closed!", i)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment