Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
add id and key formatting utils; format keys as b36cid by default; up…
Browse files Browse the repository at this point in the history
…date tests
  • Loading branch information
petar authored and aschmahmann committed Jul 29, 2020
1 parent d736047 commit c604c5b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/ipfs/go-unixfs v0.2.4
github.com/libp2p/go-libp2p-core v0.6.0
github.com/multiformats/go-multiaddr v0.2.2
github.com/multiformats/go-multibase v0.0.3
github.com/multiformats/go-multihash v0.0.13
)

Expand Down
19 changes: 19 additions & 0 deletions idfmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package iface

import (
peer "github.com/libp2p/go-libp2p-core/peer"
mbase "github.com/multiformats/go-multibase"
)

func FormatKeyID(id peer.ID) string {
if s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil {
panic(err)
} else {
return s
}
}

// FormatKey formats the given IPNS key in a canonical way.
func FormatKey(key Key) string {
return FormatKeyID(key.ID())
}
49 changes: 32 additions & 17 deletions tests/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"strings"
"testing"

"github.com/ipfs/interface-go-ipfs-core"
cid "github.com/ipfs/go-cid"
coreiface "github.com/ipfs/interface-go-ipfs-core"
iface "github.com/ipfs/interface-go-ipfs-core"
opt "github.com/ipfs/interface-go-ipfs-core/options"
mbase "github.com/multiformats/go-multibase"
)

func (tp *TestSuite) TestKey(t *testing.T) {
Expand Down Expand Up @@ -64,8 +67,8 @@ func (tp *TestSuite) TestListSelf(t *testing.T) {
t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name())
}

if keys[0].Path().String() != "/ipns/"+self.ID().Pretty() {
t.Errorf("expected the key to have path '/ipns/%s', got '%s'", self.ID().Pretty(), keys[0].Path().String())
if keys[0].Path().String() != "/ipns/"+coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected the key to have path '/ipns/%s', got '%s'", coreiface.FormatKeyID(self.ID()), keys[0].Path().String())
}
}

Expand Down Expand Up @@ -134,9 +137,30 @@ func (tp *TestSuite) TestGenerate(t *testing.T) {
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())
verifyIPNSPath(t, k.Path().String())
}

func verifyIPNSPath(t *testing.T, p string) bool {
t.Helper()
if !strings.HasPrefix(p, "/ipns/") {
t.Errorf("path %q does not look like an IPNS path", p)
return false
}
k := p[len("/ipns/"):]
c, err := cid.Decode(k)
if err != nil {
t.Errorf("failed to decode IPNS key %q (%v)", k, err)
return false
}
b36, err := c.StringOfBase(mbase.Base36)
if err != nil {
t.Fatalf("cid cannot format itself in b36")
return false
}
if b36 != k {
t.Errorf("IPNS key is not base36")
}
return true
}

func (tp *TestSuite) TestGenerateSize(t *testing.T) {
Expand All @@ -157,9 +181,7 @@ func (tp *TestSuite) TestGenerateSize(t *testing.T) {
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())
}
verifyIPNSPath(t, k.Path().String())
}

func (tp *TestSuite) TestGenerateType(t *testing.T) {
Expand Down Expand Up @@ -256,15 +278,8 @@ func (tp *TestSuite) TestList(t *testing.T) {
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
}
verifyIPNSPath(t, l[0].Path().String())
verifyIPNSPath(t, l[1].Path().String())
}

func (tp *TestSuite) TestRename(t *testing.T) {
Expand Down
30 changes: 15 additions & 15 deletions tests/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package tests

import (
"context"
path "github.com/ipfs/interface-go-ipfs-core/path"
"io"
"math/rand"
gopath "path"
"testing"
"time"

"github.com/ipfs/go-ipfs-files"
ipath "github.com/ipfs/go-path"
path "github.com/ipfs/interface-go-ipfs-core/path"

files "github.com/ipfs/go-ipfs-files"

coreiface "github.com/ipfs/interface-go-ipfs-core"
opt "github.com/ipfs/interface-go-ipfs-core/options"
Expand Down Expand Up @@ -70,8 +70,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
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.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String() {
Expand Down Expand Up @@ -100,8 +100,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
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.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String()+"/test" {
Expand Down Expand Up @@ -130,8 +130,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
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.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String() {
Expand Down Expand Up @@ -160,8 +160,8 @@ func (tp *TestSuite) TestPublishResolve(t *testing.T) {
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.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String()+"/a" {
Expand Down Expand Up @@ -212,8 +212,8 @@ func (tp *TestSuite) TestBasicPublishResolveKey(t *testing.T) {
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.Name() != coreiface.FormatKey(k) {
t.Errorf("expected e.Name to equal %s, got '%s'", e.Name(), coreiface.FormatKey(k))
}

if e.Value().String() != p.String() {
Expand Down Expand Up @@ -255,8 +255,8 @@ func (tp *TestSuite) TestBasicPublishResolveTimeout(t *testing.T) {
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.Name() != coreiface.FormatKeyID(self.ID()) {
t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name())
}

if e.Value().String() != p.String() {
Expand Down

0 comments on commit c604c5b

Please sign in to comment.