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))
}
}
This diff is collapsed.
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