Skip to content

Commit

Permalink
Merge pull request ipfs/interface-go-ipfs-core#65 from ipfs/petar/nam…
Browse files Browse the repository at this point in the history
…efmt

Add ID formatting functions, used by various IPFS cli commands

This commit was moved from ipfs/interface-go-ipfs-core@b935dfe
  • Loading branch information
aschmahmann authored Jul 29, 2020
2 parents 150e1b6 + 7bcd643 commit d15ee99
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 32 deletions.
19 changes: 19 additions & 0 deletions coreiface/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 coreiface/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 coreiface/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 d15ee99

Please sign in to comment.